--- jsr166/src/test/tck/JSR166TestCase.java 2010/10/21 23:22:49 1.65 +++ jsr166/src/test/tck/JSR166TestCase.java 2011/05/30 22:53:21 1.87 @@ -1,16 +1,25 @@ /* * 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. */ 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.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; import java.security.CodeSource; import java.security.Permission; import java.security.PermissionCollection; @@ -251,7 +260,6 @@ public class JSR166TestCase extends Test return 50; } - /** * Sets delays as multiples of SHORT_DELAY. */ @@ -259,7 +267,23 @@ public class JSR166TestCase extends Test SHORT_DELAY_MS = getShortDelay(); SMALL_DELAY_MS = SHORT_DELAY_MS * 5; MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; - LONG_DELAY_MS = SHORT_DELAY_MS * 50; + LONG_DELAY_MS = SHORT_DELAY_MS * 200; + } + + /** + * 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(System.currentTimeMillis() + delayMillis); } /** @@ -283,12 +307,16 @@ 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.get(); + Throwable t = threadFailure.getAndSet(null); if (t != null) { if (t instanceof Error) throw (Error) t; @@ -303,6 +331,9 @@ public class JSR166TestCase extends Test throw afe; } } + + if (Thread.interrupted()) + throw new AssertionFailedError("interrupt status set in main thread"); } /** @@ -434,19 +465,40 @@ 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 + * if the sleep is shorter than specified, may re-sleep or yield + * until time elapses. + */ + static void delay(long millis) throws InterruptedException { + long startTime = System.nanoTime(); + long ns = millis * 1000 * 1000; + for (;;) { + if (millis > 0L) + Thread.sleep(millis); + else // too short to sleep + Thread.yield(); + long d = ns - (System.nanoTime() - startTime); + if (d > 0L) + millis = d / (1000 * 1000); + else + break; + } + } + + /** * 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", - exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); + exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); } catch (SecurityException ok) { // Allowed in case test doesn't have privs } catch (InterruptedException ie) { @@ -454,6 +506,49 @@ public class JSR166TestCase extends Test } } + /** + * Checks that thread does not terminate within the default + * millisecond delay of {@code 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 { + // No need to optimize the failing case via Thread.join. + delay(millis); + assertTrue(thread.isAlive()); + } 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". @@ -585,7 +680,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"); @@ -595,16 +690,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. */ @@ -615,14 +700,36 @@ public class JSR166TestCase extends Test Thread.State s = thread.getState(); if (s == Thread.State.BLOCKED || s == Thread.State.WAITING || - s == Thread.State.TIMED_WAITING || - System.nanoTime() - t0 > timeoutNanos) + s == Thread.State.TIMED_WAITING) return; + else if (s == Thread.State.TERMINATED) + fail("Unexpected thread termination"); + else if (System.nanoTime() - t0 > timeoutNanos) { + threadAssertTrue(thread.isAlive()); + return; + } Thread.yield(); } } /** + * 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()}. + */ + long millisElapsedSince(long startNanoTime) { + return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); + } + + /** * Returns a new started daemon Thread running the given runnable. */ Thread newStartedThread(Runnable runnable) { @@ -643,13 +750,22 @@ 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"); } } } + /** + * 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 { @@ -712,6 +828,7 @@ public class JSR166TestCase extends Test realRun(); threadShouldThrow("InterruptedException"); } catch (InterruptedException success) { + threadAssertFalse(Thread.interrupted()); } catch (Throwable t) { threadUnexpectedException(t); } @@ -741,6 +858,7 @@ public class JSR166TestCase extends Test threadShouldThrow("InterruptedException"); return result; } catch (InterruptedException success) { + threadAssertFalse(Thread.interrupted()); } catch (Throwable t) { threadUnexpectedException(t); } @@ -772,6 +890,40 @@ public class JSR166TestCase extends Test }}; } + public Runnable awaiter(final CountDownLatch latch) { + return new CheckedRunnable() { + public void realRun() throws InterruptedException { + await(latch); + }}; + } + + public void await(CountDownLatch latch) { + try { + assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); + } catch (Throwable t) { + threadUnexpectedException(t); + } + } + +// /** +// * 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(); } } @@ -782,46 +934,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); } } @@ -829,7 +981,7 @@ public class JSR166TestCase extends Test return new CheckedRunnable() { protected void realRun() { try { - Thread.sleep(timeoutMillis); + delay(timeoutMillis); } catch (InterruptedException ok) {} }}; } @@ -837,7 +989,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) {} } } @@ -845,7 +997,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) {} } } @@ -869,7 +1021,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) {} } @@ -880,7 +1032,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) {} } @@ -890,7 +1042,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) {} } @@ -900,7 +1052,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) {} } @@ -910,7 +1062,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) {} } @@ -927,7 +1079,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; @@ -974,15 +1126,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); @@ -992,4 +1146,49 @@ public class JSR166TestCase extends Test } } + void checkEmpty(BlockingQueue q) { + try { + assertTrue(q.isEmpty()); + assertEquals(0, q.size()); + assertNull(q.peek()); + assertNull(q.poll()); + assertNull(q.poll(0, MILLISECONDS)); + assertEquals(q.toString(), "[]"); + assertTrue(Arrays.equals(q.toArray(), new Object[0])); + assertFalse(q.iterator().hasNext()); + try { + q.element(); + shouldThrow(); + } catch (NoSuchElementException success) {} + try { + q.iterator().next(); + shouldThrow(); + } catch (NoSuchElementException success) {} + try { + q.remove(); + shouldThrow(); + } catch (NoSuchElementException success) {} + } catch (InterruptedException ie) { + threadUnexpectedException(ie); + } + } + + @SuppressWarnings("unchecked") + T serialClone(T o) { + try { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(bos); + oos.writeObject(o); + oos.flush(); + oos.close(); + ObjectInputStream ois = new ObjectInputStream + (new ByteArrayInputStream(bos.toByteArray())); + T clone = (T) ois.readObject(); + assertSame(o.getClass(), clone.getClass()); + return clone; + } catch (Throwable t) { + threadUnexpectedException(t); + return null; + } + } }