--- jsr166/src/test/tck/JSR166TestCase.java 2013/01/21 19:51:46 1.96 +++ jsr166/src/test/tck/JSR166TestCase.java 2013/07/14 22:39:31 1.110 @@ -13,6 +13,7 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.lang.management.ManagementFactory; import java.lang.management.ThreadInfo; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; @@ -127,11 +128,19 @@ 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); + protected void runTest() throws Throwable { - if (profileTests) - runTestProfiled(); - else - super.runTest(); + for (int i = 0; i < runsPerTest; i++) { + if (profileTests) + runTestProfiled(); + else + super.runTest(); + } } protected void runTestProfiled() throws Throwable { @@ -181,11 +190,42 @@ public class JSR166TestCase extends Test return suite; } + public static void addNamedTestClasses(TestSuite suite, + String... testClassNames) { + for (String testClassName : testClassNames) { + try { + Class testClass = Class.forName(testClassName); + Method m = testClass.getDeclaredMethod("suite", + new Class[0]); + suite.addTest(newTestSuite((Test)m.invoke(null))); + } catch (Exception e) { + throw new Error("Missing test class", e); + } + } + } + + public static final double JAVA_CLASS_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"));}}); + } catch (Throwable t) { + throw new Error(t); + } + } + + 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; } + /** * Collects all JSR166 unit tests as one suite. */ public static Test suite() { - return newTestSuite( + // Java7+ test classes + TestSuite suite = newTestSuite( ForkJoinPoolTest.suite(), ForkJoinTaskTest.suite(), RecursiveActionTest.suite(), @@ -250,15 +290,34 @@ public class JSR166TestCase extends Test TreeSetTest.suite(), TreeSubMapTest.suite(), TreeSubSetTest.suite()); + + // Java8+ test classes + if (atLeastJava8()) { + String[] java8TestClassNames = { + "CompletableFutureTest", + "ConcurrentHashMap8Test", + "CountedCompleterTest", + "DoubleAccumulatorTest", + "DoubleAdderTest", + "ForkJoinPool8Test", + "LongAccumulatorTest", + "LongAdderTest", + "SplittableRandomTest", + "StampedLockTest", + }; + addNamedTestClasses(suite, java8TestClassNames); + } + + 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. @@ -341,6 +400,29 @@ public class JSR166TestCase extends Test if (Thread.interrupted()) throw new AssertionFailedError("interrupt status set in main thread"); + + checkForkJoinPoolThreadLeaks(); + } + + /** + * Find missing try { ... } finally { joinPool(e); } + */ + void checkForkJoinPoolThreadLeaks() throws InterruptedException { + Thread[] survivors = new Thread[5]; + int count = Thread.enumerate(survivors); + for (int i = 0; i < count; i++) { + Thread thread = survivors[i]; + String name = thread.getName(); + if (name.startsWith("ForkJoinPool-")) { + // give thread some time to terminate + thread.join(LONG_DELAY_MS); + if (!thread.isAlive()) continue; + thread.stop(); + throw new AssertionFailedError + (String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", + toString(), name)); + } + } } /** @@ -628,7 +710,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 @@ -1159,7 +1240,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) { @@ -1174,7 +1255,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) { @@ -1275,4 +1356,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()); + } + } }