--- jsr166/src/test/tck/JSR166TestCase.java 2010/11/28 08:43:53 1.72 +++ jsr166/src/test/tck/JSR166TestCase.java 2011/05/07 19:03:26 1.78 @@ -1,7 +1,7 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ * Other contributors include Andrew Wright, Jeffrey Hayes, * Pat Fisher, Mike Judd. */ @@ -443,6 +443,27 @@ public class JSR166TestCase extends Test } /** + * Delays, via Thread.sleep for the given millisecond delay, but + * if the sleep is shorter than specified, may re-sleep or yield + * until time elapses. + */ + public static void delay(long ms) throws InterruptedException { + long startTime = System.nanoTime(); + long ns = ms * 1000 * 1000; + for (;;) { + if (ms > 0L) + Thread.sleep(ms); + else // too short to sleep + Thread.yield(); + long d = ns - (System.nanoTime() - startTime); + if (d > 0L) + ms = d / (1000 * 1000); + else + break; + } + } + + /** * Waits out termination of a thread pool or fails doing so. */ public void joinPool(ExecutorService exec) { @@ -457,6 +478,20 @@ public class JSR166TestCase extends Test } } + /** + * Checks that thread does not terminate within timeoutMillis + * milliseconds (that is, Thread.join times out). + */ + public void assertThreadJoinTimesOut(Thread thread, long timeoutMillis) { + try { + long startTime = System.nanoTime(); + thread.join(timeoutMillis); + assertTrue(thread.isAlive()); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis); + } catch (InterruptedException ie) { + fail("Unexpected InterruptedException"); + } + } /** * Fails with message "should throw exception". @@ -588,7 +623,7 @@ public class JSR166TestCase extends Test */ void sleep(long millis) { try { - Thread.sleep(millis); + delay(millis); } catch (InterruptedException ie) { AssertionFailedError afe = new AssertionFailedError("Unexpected InterruptedException"); @@ -631,6 +666,14 @@ public class JSR166TestCase extends Test } /** + * Waits up to LONG_DELAY_MS for the given thread to enter a wait + * state: BLOCKED, WAITING, or TIMED_WAITING. + */ + void waitForThreadToEnterWaitState(Thread thread) { + waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); + } + + /** * Returns the number of milliseconds since time given by * startNanoTime, which must have been previously returned from a * call to {@link System.nanoTime()}. @@ -667,6 +710,15 @@ public class JSR166TestCase extends Test } } + /** + * Waits for LONG_DELAY_MS milliseconds for the thread to + * terminate (using {@link Thread#join(long)}), else interrupts + * the thread (in the hope that it may terminate later) and fails. + */ + void awaitTermination(Thread t) { + awaitTermination(t, LONG_DELAY_MS); + } + // Some convenient Runnable classes public abstract class CheckedRunnable implements Runnable { @@ -789,6 +841,13 @@ public class JSR166TestCase extends Test }}; } + public Runnable awaiter(final CountDownLatch latch) { + return new CheckedRunnable() { + public void realRun() throws InterruptedException { + latch.await(); + }}; + } + public static class NPETask implements Callable { public String call() { throw new NullPointerException(); } } @@ -799,46 +858,46 @@ public class JSR166TestCase extends Test public class ShortRunnable extends CheckedRunnable { protected void realRun() throws Throwable { - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); } } public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { protected void realRun() throws InterruptedException { - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); } } public class SmallRunnable extends CheckedRunnable { protected void realRun() throws Throwable { - Thread.sleep(SMALL_DELAY_MS); + delay(SMALL_DELAY_MS); } } public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { protected void realRun() { try { - Thread.sleep(SMALL_DELAY_MS); + delay(SMALL_DELAY_MS); } catch (InterruptedException ok) {} } } public class SmallCallable extends CheckedCallable { protected Object realCall() throws InterruptedException { - Thread.sleep(SMALL_DELAY_MS); + delay(SMALL_DELAY_MS); return Boolean.TRUE; } } public class MediumRunnable extends CheckedRunnable { protected void realRun() throws Throwable { - Thread.sleep(MEDIUM_DELAY_MS); + delay(MEDIUM_DELAY_MS); } } public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { protected void realRun() throws InterruptedException { - Thread.sleep(MEDIUM_DELAY_MS); + delay(MEDIUM_DELAY_MS); } } @@ -846,7 +905,7 @@ public class JSR166TestCase extends Test return new CheckedRunnable() { protected void realRun() { try { - Thread.sleep(timeoutMillis); + delay(timeoutMillis); } catch (InterruptedException ok) {} }}; } @@ -854,7 +913,7 @@ public class JSR166TestCase extends Test public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { protected void realRun() { try { - Thread.sleep(MEDIUM_DELAY_MS); + delay(MEDIUM_DELAY_MS); } catch (InterruptedException ok) {} } } @@ -862,7 +921,7 @@ public class JSR166TestCase extends Test public class LongPossiblyInterruptedRunnable extends CheckedRunnable { protected void realRun() { try { - Thread.sleep(LONG_DELAY_MS); + delay(LONG_DELAY_MS); } catch (InterruptedException ok) {} } } @@ -886,7 +945,7 @@ public class JSR166TestCase extends Test public boolean isDone() { return done; } public void run() { try { - Thread.sleep(timeoutMillis); + delay(timeoutMillis); done = true; } catch (InterruptedException ok) {} } @@ -897,7 +956,7 @@ public class JSR166TestCase extends Test public volatile boolean done = false; public void run() { try { - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); done = true; } catch (InterruptedException ok) {} } @@ -907,7 +966,7 @@ public class JSR166TestCase extends Test public volatile boolean done = false; public void run() { try { - Thread.sleep(SMALL_DELAY_MS); + delay(SMALL_DELAY_MS); done = true; } catch (InterruptedException ok) {} } @@ -917,7 +976,7 @@ public class JSR166TestCase extends Test public volatile boolean done = false; public void run() { try { - Thread.sleep(MEDIUM_DELAY_MS); + delay(MEDIUM_DELAY_MS); done = true; } catch (InterruptedException ok) {} } @@ -927,7 +986,7 @@ public class JSR166TestCase extends Test public volatile boolean done = false; public void run() { try { - Thread.sleep(LONG_DELAY_MS); + delay(LONG_DELAY_MS); done = true; } catch (InterruptedException ok) {} } @@ -944,7 +1003,7 @@ public class JSR166TestCase extends Test public volatile boolean done = false; public Object call() { try { - Thread.sleep(SMALL_DELAY_MS); + delay(SMALL_DELAY_MS); done = true; } catch (InterruptedException ok) {} return Boolean.TRUE;