--- jsr166/src/test/tck/JSR166TestCase.java 2017/03/14 00:54:27 1.221 +++ jsr166/src/test/tck/JSR166TestCase.java 2017/05/15 17:02:46 1.231 @@ -658,12 +658,17 @@ public class JSR166TestCase extends Test LONG_DELAY_MS = SHORT_DELAY_MS * 200; } + private static final long TIMEOUT_DELAY_MS + = (long) (12.0 * Math.cbrt(delayFactor)); + /** - * Returns a timeout in milliseconds to be used in tests that - * verify that operations block or time out. + * Returns a timeout in milliseconds to be used in tests that verify + * that operations block or time out. We want this to be longer + * than the OS scheduling quantum, but not too long, so don't scale + * linearly with delayFactor; we use "crazy" cube root instead. */ - long timeoutMillis() { - return SHORT_DELAY_MS / 4; + static long timeoutMillis() { + return TIMEOUT_DELAY_MS; } /** @@ -1058,8 +1063,29 @@ public class JSR166TestCase extends Test } /** + * Checks that thread eventually enters the expected blocked thread state. + */ + void assertThreadBlocks(Thread thread, Thread.State expected) { + // always sleep at least 1 ms, with high probability avoiding + // transitory states + for (long retries = LONG_DELAY_MS * 3 / 4; retries-->0; ) { + try { delay(1); } + catch (InterruptedException fail) { + fail("Unexpected InterruptedException"); + } + Thread.State s = thread.getState(); + if (s == expected) + return; + else if (s == Thread.State.TERMINATED) + fail("Unexpected thread termination"); + } + fail("timed out waiting for thread to enter thread state " + expected); + } + + /** * Checks that thread does not terminate within the default * millisecond delay of {@code timeoutMillis()}. + * TODO: REMOVEME */ void assertThreadStaysAlive(Thread thread) { assertThreadStaysAlive(thread, timeoutMillis()); @@ -1067,6 +1093,7 @@ public class JSR166TestCase extends Test /** * Checks that thread does not terminate within the given millisecond delay. + * TODO: REMOVEME */ void assertThreadStaysAlive(Thread thread, long millis) { try { @@ -1081,6 +1108,7 @@ public class JSR166TestCase extends Test /** * Checks that the threads do not terminate within the default * millisecond delay of {@code timeoutMillis()}. + * TODO: REMOVEME */ void assertThreadsStayAlive(Thread... threads) { assertThreadsStayAlive(timeoutMillis(), threads); @@ -1088,6 +1116,7 @@ public class JSR166TestCase extends Test /** * Checks that the threads do not terminate within the given millisecond delay. + * TODO: REMOVEME */ void assertThreadsStayAlive(long millis, Thread... threads) { try { @@ -1138,6 +1167,12 @@ public class JSR166TestCase extends Test } /** + * The maximum number of consecutive spurious wakeups we should + * tolerate (from APIs like LockSupport.park) before failing a test. + */ + static final int MAX_SPURIOUS_WAKEUPS = 10; + + /** * The number of elements to place in collections, arrays, etc. */ public static final int SIZE = 20; @@ -1604,6 +1639,14 @@ public class JSR166TestCase extends Test } catch (Throwable fail) { threadUnexpectedException(fail); } + } + + public void await(CyclicBarrier barrier) { + try { + barrier.await(LONG_DELAY_MS, MILLISECONDS); + } catch (Throwable fail) { + threadUnexpectedException(fail); + } } // /**