--- jsr166/src/test/tck/JSR166TestCase.java 2011/05/07 19:03:26 1.78 +++ jsr166/src/test/tck/JSR166TestCase.java 2011/05/13 21:48:58 1.80 @@ -7,6 +7,10 @@ */ import junit.framework.*; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.util.Arrays; import java.util.NoSuchElementException; import java.util.PropertyPermission; @@ -447,17 +451,17 @@ public class JSR166TestCase extends Test * if the sleep is shorter than specified, may re-sleep or yield * until time elapses. */ - public static void delay(long ms) throws InterruptedException { + public static void delay(long millis) throws InterruptedException { long startTime = System.nanoTime(); - long ns = ms * 1000 * 1000; + long ns = millis * 1000 * 1000; for (;;) { - if (ms > 0L) - Thread.sleep(ms); + if (millis > 0L) + Thread.sleep(millis); else // too short to sleep Thread.yield(); long d = ns - (System.nanoTime() - startTime); if (d > 0L) - ms = d / (1000 * 1000); + millis = d / (1000 * 1000); else break; } @@ -479,15 +483,13 @@ public class JSR166TestCase extends Test } /** - * Checks that thread does not terminate within timeoutMillis - * milliseconds (that is, Thread.join times out). + * Checks that thread does not terminate within the given millisecond delay. */ - public void assertThreadJoinTimesOut(Thread thread, long timeoutMillis) { + public void assertThreadStaysAlive(Thread thread, long millis) { try { - long startTime = System.nanoTime(); - thread.join(timeoutMillis); + // No need to optimize the failing case via Thread.join. + delay(millis); assertTrue(thread.isAlive()); - assertTrue(millisElapsedSince(startTime) >= timeoutMillis); } catch (InterruptedException ie) { fail("Unexpected InterruptedException"); } @@ -844,10 +846,18 @@ public class JSR166TestCase extends Test public Runnable awaiter(final CountDownLatch latch) { return new CheckedRunnable() { public void realRun() throws InterruptedException { - latch.await(); + await(latch); }}; } + public void await(CountDownLatch latch) { + try { + assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); + } catch (Throwable t) { + threadUnexpectedException(t); + } + } + public static class NPETask implements Callable { public String call() { throw new NullPointerException(); } } @@ -1095,4 +1105,21 @@ public class JSR166TestCase extends Test } } + @SuppressWarnings("unchecked") + public T serialClone(T 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(); + } catch (Throwable t) { + threadUnexpectedException(t); + return null; + } + } }