--- jsr166/src/test/tck/JSR166TestCase.java 2011/05/09 20:00:19 1.79 +++ jsr166/src/test/tck/JSR166TestCase.java 2011/05/27 19:29:59 1.83 @@ -12,9 +12,11 @@ import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Arrays; +import java.util.Date; import java.util.NoSuchElementException; import java.util.PropertyPermission; import java.util.concurrent.*; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.NANOSECONDS; @@ -258,7 +260,6 @@ public class JSR166TestCase extends Test return 50; } - /** * Sets delays as multiples of SHORT_DELAY. */ @@ -270,6 +271,22 @@ public class JSR166TestCase extends Test } /** + * Returns a timeout in milliseconds to be used in tests that + * verify that operations block or time out. + */ + long timeoutMillis() { + return SHORT_DELAY_MS / 4; + } + + /** + * Returns a new Date instance representing a time delayMillis + * milliseconds in the future. + */ + Date delayedDate(long delayMillis) { + return new Date(new Date().getTime() + delayMillis); + } + + /** * The first exception encountered if any threadAssertXXX method fails. */ private final AtomicReference threadFailure @@ -441,27 +458,27 @@ public class JSR166TestCase extends Test else { AssertionFailedError afe = new AssertionFailedError("unexpected exception: " + t); - t.initCause(t); + afe.initCause(t); throw afe; } } /** - * Delays, via Thread.sleep for the given millisecond delay, but + * 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 { + 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; } @@ -470,7 +487,7 @@ public class JSR166TestCase extends Test /** * Waits out termination of a thread pool or fails doing so. */ - public void joinPool(ExecutorService exec) { + void joinPool(ExecutorService exec) { try { exec.shutdown(); assertTrue("ExecutorService did not terminate in a timely manner", @@ -483,21 +500,50 @@ 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 default + * millisecond delay of {@code timeoutMillis()}. */ - public void assertThreadJoinTimesOut(Thread thread, long timeoutMillis) { + void assertThreadStaysAlive(Thread thread) { + assertThreadStaysAlive(thread, timeoutMillis()); + } + + /** + * Checks that thread does not terminate within the given millisecond delay. + */ + 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"); } } /** + * Checks that future.get times out, with the default timeout of + * {@code timeoutMillis()}. + */ + void assertFutureTimesOut(Future future) { + assertFutureTimesOut(future, timeoutMillis()); + } + + /** + * Checks that future.get times out, with the given millisecond timeout. + */ + void assertFutureTimesOut(Future future, long timeoutMillis) { + long startTime = System.nanoTime(); + try { + future.get(timeoutMillis, MILLISECONDS); + shouldThrow(); + } catch (TimeoutException success) { + } catch (Exception e) { + threadUnexpectedException(e); + } finally { future.cancel(true); } + assertTrue(millisElapsedSince(startTime) >= timeoutMillis); + } + + /** * Fails with message "should throw exception". */ public void shouldThrow() { @@ -637,16 +683,6 @@ public class JSR166TestCase extends Test } /** - * Sleeps until the timeout has elapsed, or interrupted. - * Does NOT throw InterruptedException. - */ - void sleepTillInterrupted(long timeoutMillis) { - try { - Thread.sleep(timeoutMillis); - } catch (InterruptedException wakeup) {} - } - - /** * Waits up to the specified number of milliseconds for the given * thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. */ @@ -707,7 +743,7 @@ public class JSR166TestCase extends Test } catch (InterruptedException ie) { threadUnexpectedException(ie); } finally { - if (t.isAlive()) { + if (t.getState() != Thread.State.TERMINATED) { t.interrupt(); fail("Test timed out"); } @@ -785,6 +821,7 @@ public class JSR166TestCase extends Test realRun(); threadShouldThrow("InterruptedException"); } catch (InterruptedException success) { + threadAssertFalse(Thread.interrupted()); } catch (Throwable t) { threadUnexpectedException(t); } @@ -814,6 +851,7 @@ public class JSR166TestCase extends Test threadShouldThrow("InterruptedException"); return result; } catch (InterruptedException success) { + threadAssertFalse(Thread.interrupted()); } catch (Throwable t) { threadUnexpectedException(t); } @@ -860,6 +898,25 @@ public class JSR166TestCase extends Test } } +// /** +// * Spin-waits up to LONG_DELAY_MS until flag becomes true. +// */ +// public void await(AtomicBoolean flag) { +// await(flag, LONG_DELAY_MS); +// } + +// /** +// * Spin-waits up to the specified timeout until flag becomes true. +// */ +// public void await(AtomicBoolean flag, long timeoutMillis) { +// long startTime = System.nanoTime(); +// while (!flag.get()) { +// if (millisElapsedSince(startTime) > timeoutMillis) +// throw new AssertionFailedError("timed out"); +// Thread.yield(); +// } +// } + public static class NPETask implements Callable { public String call() { throw new NullPointerException(); } } @@ -1080,7 +1137,7 @@ public class JSR166TestCase extends Test } } - public void checkEmpty(BlockingQueue q) { + void checkEmpty(BlockingQueue q) { try { assertTrue(q.isEmpty()); assertEquals(0, q.size()); @@ -1108,7 +1165,7 @@ public class JSR166TestCase extends Test } @SuppressWarnings("unchecked") - public T serialClone(T o) { + T serialClone(T o) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos);