--- jsr166/src/test/tck/JSR166TestCase.java 2015/06/14 20:58:14 1.134 +++ jsr166/src/test/tck/JSR166TestCase.java 2015/09/06 21:14:12 1.139 @@ -37,6 +37,8 @@ import java.util.concurrent.BlockingQueu import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.concurrent.RecursiveAction; @@ -281,7 +283,15 @@ 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() { return JAVA_CLASS_VERSION >= 53.0; } + public static boolean atLeastJava9() { + return JAVA_CLASS_VERSION >= 53.0 + // As of 2015-09, java9 still uses 52.0 class file version + || JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?(9|[0-9][0-9])$"); + } + public static boolean atLeastJava10() { + return JAVA_CLASS_VERSION >= 54.0 + || JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?[0-9][0-9]$"); + } /** * Collects all JSR166 unit tests as one suite. @@ -377,7 +387,7 @@ public class JSR166TestCase extends Test // Java9+ test classes if (atLeastJava9()) { String[] java9TestClassNames = { - "ThreadPoolExecutor9Test", + // Currently empty }; addNamedTestClasses(suite, java9TestClassNames); } @@ -481,11 +491,12 @@ public class JSR166TestCase extends Test } /** - * Returns a new Date instance representing a time delayMillis - * milliseconds in the future. + * Returns a new Date instance representing a time at least + * delayMillis milliseconds in the future. */ Date delayedDate(long delayMillis) { - return new Date(System.currentTimeMillis() + delayMillis); + // Add 1 because currentTimeMillis is known to round into the past. + return new Date(System.currentTimeMillis() + delayMillis + 1); } /** @@ -718,11 +729,11 @@ public class JSR166TestCase extends Test /** * Waits out termination of a thread pool or fails doing so. */ - void joinPool(ExecutorService exec) { + void joinPool(ExecutorService pool) { try { - exec.shutdown(); - if (!exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) - fail("ExecutorService " + exec + + pool.shutdown(); + if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) + fail("ExecutorService " + pool + " did not terminate in a timely manner"); } catch (SecurityException ok) { // Allowed in case test doesn't have privs @@ -731,6 +742,34 @@ public class JSR166TestCase extends Test } } + /** Like Runnable, but with the freedom to throw anything */ + interface Thunk { public void run() throws Throwable; } + + /** + * Runs all the given tasks in parallel, failing if any fail. + * Useful for running multiple variants of tests that are + * necessarily individually slow because they must block. + */ + void testInParallel(Thunk ... thunks) { + ExecutorService pool = Executors.newCachedThreadPool(); + try { + ArrayList> futures = new ArrayList<>(thunks.length); + for (final Thunk thunk : thunks) + futures.add(pool.submit(new CheckedRunnable() { + public void realRun() throws Throwable { thunk.run();}})); + for (Future future : futures) + try { + assertNull(future.get(LONG_DELAY_MS, MILLISECONDS)); + } catch (ExecutionException ex) { + threadUnexpectedException(ex.getCause()); + } catch (Exception ex) { + threadUnexpectedException(ex); + } + } finally { + joinPool(pool); + } + } + /** * A debugging tool to print all stack traces, as jstack does. */