--- jsr166/src/test/tck/JSR166TestCase.java 2011/05/06 17:26:29 1.77 +++ jsr166/src/test/tck/JSR166TestCase.java 2011/05/30 22:53:21 1.87 @@ -7,10 +7,16 @@ */ 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; @@ -254,7 +260,6 @@ public class JSR166TestCase extends Test return 50; } - /** * Sets delays as multiples of SHORT_DELAY. */ @@ -266,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(System.currentTimeMillis() + delayMillis); + } + + /** * The first exception encountered if any threadAssertXXX method fails. */ private final AtomicReference threadFailure @@ -286,9 +307,13 @@ 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.getAndSet(null); @@ -306,6 +331,9 @@ public class JSR166TestCase extends Test throw afe; } } + + if (Thread.interrupted()) + throw new AssertionFailedError("interrupt status set in main thread"); } /** @@ -437,27 +465,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; } @@ -466,7 +494,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", @@ -478,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". @@ -619,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. */ @@ -689,7 +750,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"); } @@ -767,6 +828,7 @@ public class JSR166TestCase extends Test realRun(); threadShouldThrow("InterruptedException"); } catch (InterruptedException success) { + threadAssertFalse(Thread.interrupted()); } catch (Throwable t) { threadUnexpectedException(t); } @@ -796,6 +858,7 @@ public class JSR166TestCase extends Test threadShouldThrow("InterruptedException"); return result; } catch (InterruptedException success) { + threadAssertFalse(Thread.interrupted()); } catch (Throwable t) { threadUnexpectedException(t); } @@ -830,10 +893,37 @@ 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); + } + } + +// /** +// * 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(); } } @@ -1036,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); @@ -1054,7 +1146,7 @@ public class JSR166TestCase extends Test } } - public void checkEmpty(BlockingQueue q) { + void checkEmpty(BlockingQueue q) { try { assertTrue(q.isEmpty()); assertEquals(0, q.size()); @@ -1081,4 +1173,22 @@ public class JSR166TestCase extends Test } } + @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; + } + } }