--- jsr166/src/test/tck/JSR166TestCase.java 2013/02/06 19:55:06 1.102 +++ jsr166/src/test/tck/JSR166TestCase.java 2014/07/09 16:51:40 1.121 @@ -26,6 +26,7 @@ import java.util.concurrent.atomic.Atomi import java.util.concurrent.atomic.AtomicReference; import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.NANOSECONDS; +import java.util.regex.Pattern; import java.security.CodeSource; import java.security.Permission; import java.security.PermissionCollection; @@ -115,6 +116,13 @@ public class JSR166TestCase extends Test Boolean.getBoolean("jsr166.expensiveTests"); /** + * If true, also run tests that are not part of the official tck + * because they test unspecified implementation details. + */ + protected static final boolean testImplementationDetails = + Boolean.getBoolean("jsr166.testImplementationDetails"); + + /** * If true, report on stdout all "slow" tests, that is, ones that * take more than profileThreshold milliseconds to execute. */ @@ -128,20 +136,44 @@ public class JSR166TestCase extends Test private static final long profileThreshold = Long.getLong("jsr166.profileThreshold", 100); + /** + * The number of repetitions per test (for tickling rare bugs). + */ + private static final int runsPerTest = + Integer.getInteger("jsr166.runsPerTest", 1); + + /** + * A filter for tests to run, matching strings of the form + * methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)" + * Usefully combined with jsr166.runsPerTest. + */ + private static final Pattern methodFilter = methodFilter(); + + private static Pattern methodFilter() { + String regex = System.getProperty("jsr166.methodFilter"); + return (regex == null) ? null : Pattern.compile(regex); + } + protected void runTest() throws Throwable { - if (profileTests) - runTestProfiled(); - else - super.runTest(); + if (methodFilter == null + || methodFilter.matcher(toString()).find()) { + for (int i = 0; i < runsPerTest; i++) { + if (profileTests) + runTestProfiled(); + else + super.runTest(); + } + } } protected void runTestProfiled() throws Throwable { + // Warmup run, notably to trigger all needed classloading. + super.runTest(); long t0 = System.nanoTime(); try { super.runTest(); } finally { - long elapsedMillis = - (System.nanoTime() - t0) / (1000L * 1000L); + long elapsedMillis = millisElapsedSince(t0); if (elapsedMillis >= profileThreshold) System.out.printf("%n%s: %d%n", toString(), elapsedMillis); } @@ -197,12 +229,17 @@ public class JSR166TestCase extends Test } public static final double JAVA_CLASS_VERSION; + public static final String JAVA_SPECIFICATION_VERSION; static { try { JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged( new java.security.PrivilegedAction() { public Double run() { return Double.valueOf(System.getProperty("java.class.version"));}}); + JAVA_SPECIFICATION_VERSION = java.security.AccessController.doPrivileged( + new java.security.PrivilegedAction() { + public String run() { + return System.getProperty("java.specification.version");}}); } catch (Throwable t) { throw new Error(t); } @@ -211,6 +248,10 @@ public class JSR166TestCase extends Test public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } + public static boolean atLeastJava9() { + // As of 2014-05, java9 still uses 52.0 class file version + return JAVA_SPECIFICATION_VERSION.startsWith("1.9"); + } /** * Collects all JSR166 unit tests as one suite. @@ -286,23 +327,41 @@ public class JSR166TestCase extends Test // Java8+ test classes if (atLeastJava8()) { String[] java8TestClassNames = { + "Atomic8Test", "CompletableFutureTest", + "ConcurrentHashMap8Test", + "CountedCompleterTest", + "DoubleAccumulatorTest", + "DoubleAdderTest", "ForkJoinPool8Test", + "ForkJoinTask8Test", + "LongAccumulatorTest", + "LongAdderTest", + "SplittableRandomTest", "StampedLockTest", + "ThreadLocalRandom8Test", }; addNamedTestClasses(suite, java8TestClassNames); } + // Java9+ test classes + if (atLeastJava9()) { + String[] java9TestClassNames = { + "ThreadPoolExecutor9Test", + }; + addNamedTestClasses(suite, java9TestClassNames); + } + return suite; } + // Delays for timing-dependent tests, in milliseconds. public static long SHORT_DELAY_MS; public static long SMALL_DELAY_MS; public static long MEDIUM_DELAY_MS; public static long LONG_DELAY_MS; - /** * Returns the shortest timed delay. This could * be reimplemented to use for example a Property. @@ -571,8 +630,9 @@ public class JSR166TestCase extends Test void joinPool(ExecutorService exec) { try { exec.shutdown(); - assertTrue("ExecutorService did not terminate in a timely manner", - exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); + if (!exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) + fail("ExecutorService " + exec + + " did not terminate in a timely manner"); } catch (SecurityException ok) { // Allowed in case test doesn't have privs } catch (InterruptedException ie) { @@ -695,7 +755,6 @@ public class JSR166TestCase extends Test public static final Integer m6 = new Integer(-6); public static final Integer m10 = new Integer(-10); - /** * Runs Runnable r with a security policy that permits precisely * the specified permissions. If there is no current security @@ -851,10 +910,40 @@ public class JSR166TestCase extends Test * startNanoTime, which must have been previously returned from a * call to {@link System.nanoTime()}. */ - long millisElapsedSince(long startNanoTime) { + static long millisElapsedSince(long startNanoTime) { return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); } +// void assertTerminatesPromptly(long timeoutMillis, Runnable r) { +// long startTime = System.nanoTime(); +// try { +// r.run(); +// } catch (Throwable fail) { threadUnexpectedException(fail); } +// if (millisElapsedSince(startTime) > timeoutMillis/2) +// throw new AssertionFailedError("did not return promptly"); +// } + +// void assertTerminatesPromptly(Runnable r) { +// assertTerminatesPromptly(LONG_DELAY_MS/2, r); +// } + + /** + * Checks that timed f.get() returns the expected value, and does not + * wait for the timeout to elapse before returning. + */ + void checkTimedGet(Future f, T expectedValue, long timeoutMillis) { + long startTime = System.nanoTime(); + try { + assertEquals(expectedValue, f.get(timeoutMillis, MILLISECONDS)); + } catch (Throwable fail) { threadUnexpectedException(fail); } + if (millisElapsedSince(startTime) > timeoutMillis/2) + throw new AssertionFailedError("timed get did not return promptly"); + } + + void checkTimedGet(Future f, T expectedValue) { + checkTimedGet(f, expectedValue, LONG_DELAY_MS); + } + /** * Returns a new started daemon Thread running the given runnable. */ @@ -1226,7 +1315,7 @@ public class JSR166TestCase extends Test public abstract class CheckedRecursiveAction extends RecursiveAction { protected abstract void realCompute() throws Throwable; - public final void compute() { + @Override protected final void compute() { try { realCompute(); } catch (Throwable t) { @@ -1241,7 +1330,7 @@ public class JSR166TestCase extends Test public abstract class CheckedRecursiveTask extends RecursiveTask { protected abstract T realCompute() throws Throwable; - public final T compute() { + @Override protected final T compute() { try { return realCompute(); } catch (Throwable t) { @@ -1342,4 +1431,25 @@ public class JSR166TestCase extends Test return null; } } + + public void assertThrows(Class expectedExceptionClass, + Runnable... throwingActions) { + for (Runnable throwingAction : throwingActions) { + boolean threw = false; + try { throwingAction.run(); } + catch (Throwable t) { + threw = true; + if (!expectedExceptionClass.isInstance(t)) { + AssertionFailedError afe = + new AssertionFailedError + ("Expected " + expectedExceptionClass.getName() + + ", got " + t.getClass().getName()); + afe.initCause(t); + threadUnexpectedException(afe); + } + } + if (!threw) + shouldThrow(expectedExceptionClass.getName()); + } + } }