--- jsr166/src/test/tck/JSR166TestCase.java 2011/05/29 14:18:52 1.85
+++ jsr166/src/test/tck/JSR166TestCase.java 2012/11/18 18:03:11 1.92
@@ -69,7 +69,7 @@ import java.security.SecurityPermission;
*
*
*
- *
Other notes
+ *
Other notes
*
*
* - Usually, there is one testcase method per JSR166 method
@@ -528,6 +528,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()}.
*/
@@ -690,12 +712,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 ||
@@ -704,7 +725,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;
}
@@ -905,6 +926,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.
// */
@@ -1126,15 +1155,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);
@@ -1171,18 +1202,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;