--- jsr166/src/test/tck/JSR166TestCase.java 2011/05/27 19:29:59 1.83
+++ jsr166/src/test/tck/JSR166TestCase.java 2013/01/21 19:51:46 1.96
@@ -11,8 +11,13 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
+import java.lang.management.ManagementFactory;
+import java.lang.management.ThreadInfo;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
+import java.util.Enumeration;
+import java.util.List;
import java.util.NoSuchElementException;
import java.util.PropertyPermission;
import java.util.concurrent.*;
@@ -69,7 +74,7 @@ import java.security.SecurityPermission;
*
*
*
- *
Other notes
+ *
Other notes
*
*
* - Usually, there is one testcase method per JSR166 method
@@ -142,7 +147,9 @@ public class JSR166TestCase extends Test
}
/**
- * Runs all JSR166 unit tests using junit.textui.TestRunner
+ * Runs all JSR166 unit tests using junit.textui.TestRunner.
+ * Optional command line arg provides the number of iterations to
+ * repeat running the tests.
*/
public static void main(String[] args) {
if (useSecurityManager) {
@@ -283,7 +290,7 @@ public class JSR166TestCase extends Test
* milliseconds in the future.
*/
Date delayedDate(long delayMillis) {
- return new Date(new Date().getTime() + delayMillis);
+ return new Date(System.currentTimeMillis() + delayMillis);
}
/**
@@ -307,9 +314,13 @@ public class JSR166TestCase extends Test
}
/**
+ * Extra checks that get done for all test cases.
+ *
* Triggers test case failure if any thread assertions have failed,
* by rethrowing, in the test harness thread, any exception recorded
* earlier by threadRecordFailure.
+ *
+ * Triggers test case failure if interrupt status is set in the main thread.
*/
public void tearDown() throws Exception {
Throwable t = threadFailure.getAndSet(null);
@@ -327,6 +338,9 @@ public class JSR166TestCase extends Test
throw afe;
}
}
+
+ if (Thread.interrupted())
+ throw new AssertionFailedError("interrupt status set in main thread");
}
/**
@@ -500,6 +514,16 @@ public class JSR166TestCase extends Test
}
/**
+ * A debugging tool to print all stack traces, as jstack does.
+ */
+ static void printAllStackTraces() {
+ for (ThreadInfo info :
+ ManagementFactory.getThreadMXBean()
+ .dumpAllThreads(true, true))
+ System.err.print(info);
+ }
+
+ /**
* Checks that thread does not terminate within the default
* millisecond delay of {@code timeoutMillis()}.
*/
@@ -521,6 +545,28 @@ public class JSR166TestCase extends Test
}
/**
+ * Checks that the threads do not terminate within the default
+ * millisecond delay of {@code timeoutMillis()}.
+ */
+ void assertThreadsStayAlive(Thread... threads) {
+ assertThreadsStayAlive(timeoutMillis(), threads);
+ }
+
+ /**
+ * Checks that the threads do not terminate within the given millisecond delay.
+ */
+ void assertThreadsStayAlive(long millis, Thread... threads) {
+ try {
+ // No need to optimize the failing case via Thread.join.
+ delay(millis);
+ for (Thread thread : threads)
+ assertTrue(thread.isAlive());
+ } catch (InterruptedException ie) {
+ fail("Unexpected InterruptedException");
+ }
+ }
+
+ /**
* Checks that future.get times out, with the default timeout of
* {@code timeoutMillis()}.
*/
@@ -594,11 +640,26 @@ public class JSR166TestCase extends Test
SecurityManager sm = System.getSecurityManager();
if (sm == null) {
r.run();
+ }
+ runWithSecurityManagerWithPermissions(r, permissions);
+ }
+
+ /**
+ * Runs Runnable r with a security policy that permits precisely
+ * the specified permissions. If there is no current security
+ * manager, a temporary one is set for the duration of the
+ * Runnable. We require that any security manager permit
+ * getPolicy/setPolicy.
+ */
+ public void runWithSecurityManagerWithPermissions(Runnable r,
+ Permission... permissions) {
+ SecurityManager sm = System.getSecurityManager();
+ if (sm == null) {
Policy savedPolicy = Policy.getPolicy();
try {
Policy.setPolicy(permissivePolicy());
System.setSecurityManager(new SecurityManager());
- runWithPermissions(r, permissions);
+ runWithSecurityManagerWithPermissions(r, permissions);
} finally {
System.setSecurityManager(null);
Policy.setPolicy(savedPolicy);
@@ -646,6 +707,12 @@ public class JSR166TestCase extends Test
return perms.implies(p);
}
public void refresh() {}
+ public String toString() {
+ List ps = new ArrayList();
+ for (Enumeration e = perms.elements(); e.hasMoreElements();)
+ ps.add(e.nextElement());
+ return "AdjustablePolicy with permissions " + ps;
+ }
}
/**
@@ -683,12 +750,11 @@ public class JSR166TestCase extends Test
}
/**
- * Waits up to the specified number of milliseconds for the given
+ * Spin-waits up to the specified number of milliseconds for the given
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING.
*/
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) {
- long timeoutNanos = timeoutMillis * 1000L * 1000L;
- long t0 = System.nanoTime();
+ long startTime = System.nanoTime();
for (;;) {
Thread.State s = thread.getState();
if (s == Thread.State.BLOCKED ||
@@ -697,7 +763,7 @@ public class JSR166TestCase extends Test
return;
else if (s == Thread.State.TERMINATED)
fail("Unexpected thread termination");
- else if (System.nanoTime() - t0 > timeoutNanos) {
+ else if (millisElapsedSince(startTime) > timeoutMillis) {
threadAssertTrue(thread.isAlive());
return;
}
@@ -898,6 +964,14 @@ public class JSR166TestCase extends Test
}
}
+ public void await(Semaphore semaphore) {
+ try {
+ assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS));
+ } catch (Throwable t) {
+ threadUnexpectedException(t);
+ }
+ }
+
// /**
// * Spin-waits up to LONG_DELAY_MS until flag becomes true.
// */
@@ -1119,15 +1193,17 @@ public class JSR166TestCase extends Test
}
/**
- * A CyclicBarrier that fails with AssertionFailedErrors instead
- * of throwing checked exceptions.
+ * A CyclicBarrier that uses timed await and fails with
+ * AssertionFailedErrors instead of throwing checked exceptions.
*/
public class CheckedBarrier extends CyclicBarrier {
public CheckedBarrier(int parties) { super(parties); }
public int await() {
try {
- return super.await();
+ return super.await(2 * LONG_DELAY_MS, MILLISECONDS);
+ } catch (TimeoutException e) {
+ throw new AssertionFailedError("timed out");
} catch (Exception e) {
AssertionFailedError afe =
new AssertionFailedError("Unexpected exception: " + e);
@@ -1164,18 +1240,36 @@ public class JSR166TestCase extends Test
}
}
- @SuppressWarnings("unchecked")
- T serialClone(T o) {
+ void assertSerialEquals(Object x, Object y) {
+ assertTrue(Arrays.equals(serialBytes(x), serialBytes(y)));
+ }
+
+ void assertNotSerialEquals(Object x, Object y) {
+ assertFalse(Arrays.equals(serialBytes(x), serialBytes(y)));
+ }
+
+ byte[] serialBytes(Object o) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(o);
oos.flush();
oos.close();
- ByteArrayInputStream bin =
- new ByteArrayInputStream(bos.toByteArray());
- ObjectInputStream ois = new ObjectInputStream(bin);
- return (T) ois.readObject();
+ return bos.toByteArray();
+ } catch (Throwable t) {
+ threadUnexpectedException(t);
+ return new byte[0];
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ T serialClone(T o) {
+ try {
+ ObjectInputStream ois = new ObjectInputStream
+ (new ByteArrayInputStream(serialBytes(o)));
+ T clone = (T) ois.readObject();
+ assertSame(o.getClass(), clone.getClass());
+ return clone;
} catch (Throwable t) {
threadUnexpectedException(t);
return null;