--- jsr166/src/test/tck/JSR166TestCase.java 2017/02/18 16:37:49 1.219 +++ jsr166/src/test/tck/JSR166TestCase.java 2017/05/14 03:14:25 1.228 @@ -1,6 +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 + * Written by Doug Lea and Martin Buchholz with assistance from + * members of JCP JSR-166 Expert Group and released to the public + * domain, as explained at * http://creativecommons.org/publicdomain/zero/1.0/ * Other contributors include Andrew Wright, Jeffrey Hayes, * Pat Fisher, Mike Judd. @@ -8,32 +9,33 @@ /* * @test - * @summary JSR-166 tck tests (conformance testing mode) + * @summary JSR-166 tck tests, in a number of variations. + * The first is the conformance testing variant, + * while others also test implementation details. * @build * * @modules java.management * @run junit/othervm/timeout=1000 JSR166TestCase - */ - -/* - * @test - * @summary JSR-166 tck tests (whitebox tests allowed) - * @build * - * @modules java.base/java.util.concurrent:open - * java.base/java.lang:open - * java.management * @run junit/othervm/timeout=1000 + * --add-opens java.base/java.util.concurrent=ALL-UNNAMED + * --add-opens java.base/java.lang=ALL-UNNAMED * -Djsr166.testImplementationDetails=true * JSR166TestCase * @run junit/othervm/timeout=1000 + * --add-opens java.base/java.util.concurrent=ALL-UNNAMED + * --add-opens java.base/java.lang=ALL-UNNAMED * -Djsr166.testImplementationDetails=true * -Djava.util.concurrent.ForkJoinPool.common.parallelism=0 * JSR166TestCase * @run junit/othervm/timeout=1000 + * --add-opens java.base/java.util.concurrent=ALL-UNNAMED + * --add-opens java.base/java.lang=ALL-UNNAMED * -Djsr166.testImplementationDetails=true * -Djava.util.concurrent.ForkJoinPool.common.parallelism=1 * -Djava.util.secureRandomSeed=true * JSR166TestCase * @run junit/othervm/timeout=1000/policy=tck.policy + * --add-opens java.base/java.util.concurrent=ALL-UNNAMED + * --add-opens java.base/java.lang=ALL-UNNAMED * -Djsr166.testImplementationDetails=true * JSR166TestCase */ @@ -52,8 +54,6 @@ import java.lang.management.ThreadMXBean import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.nio.file.Files; -import java.nio.file.Paths; import java.security.CodeSource; import java.security.Permission; import java.security.PermissionCollection; @@ -91,7 +91,6 @@ import java.util.concurrent.ThreadPoolEx import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; -import java.util.regex.Matcher; import java.util.regex.Pattern; import junit.framework.AssertionFailedError; @@ -301,9 +300,11 @@ public class JSR166TestCase extends Test // public static String cpuModel() { // try { -// Matcher matcher = Pattern.compile("model name\\s*: (.*)") +// java.util.regex.Matcher matcher +// = Pattern.compile("model name\\s*: (.*)") // .matcher(new String( -// Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8")); +// java.nio.file.Files.readAllBytes( +// java.nio.file.Paths.get("/proc/cpuinfo")), "UTF-8")); // matcher.find(); // return matcher.group(1); // } catch (Exception ex) { return null; } @@ -657,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; } /** @@ -1057,6 +1063,26 @@ 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()}. */ @@ -1078,28 +1104,6 @@ public class JSR166TestCase extends Test } /** - * Checks that the threads do not terminate within the default - * millisecond delay of {@code timeoutMillis()}. - */ - void assertThreadsStayAlive(Thread... threads) { - assertThreadsStayAlive(timeoutMillis(), threads); - } - - /** - * Checks that the threads do not terminate within the given millisecond delay. - */ - void assertThreadsStayAlive(long millis, Thread... threads) { - try { - // No need to optimize the failing case via Thread.join. - delay(millis); - for (Thread thread : threads) - assertTrue(thread.isAlive()); - } catch (InterruptedException fail) { - threadFail("Unexpected InterruptedException"); - } - } - - /** * Checks that future.get times out, with the default timeout of * {@code timeoutMillis()}. */ @@ -1137,6 +1141,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; @@ -1603,6 +1613,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); + } } // /**