--- jsr166/src/test/tck/JSR166TestCase.java 2015/09/25 23:32:15 1.146 +++ jsr166/src/test/tck/JSR166TestCase.java 2015/10/03 19:59:49 1.155 @@ -15,6 +15,7 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.lang.management.ManagementFactory; import java.lang.management.ThreadInfo; +import java.lang.management.ThreadMXBean; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.lang.reflect.Modifier; @@ -186,15 +187,18 @@ public class JSR166TestCase extends Test return (regex == null) ? null : Pattern.compile(regex); } - protected void runTest() throws Throwable { + public void runBare() throws Throwable { if (methodFilter == null - || methodFilter.matcher(toString()).find()) { - for (int i = 0; i < runsPerTest; i++) { - if (profileTests) - runTestProfiled(); - else - super.runTest(); - } + || methodFilter.matcher(toString()).find()) + super.runBare(); + } + + protected void runTest() throws Throwable { + for (int i = 0; i < runsPerTest; i++) { + if (profileTests) + runTestProfiled(); + else + super.runTest(); } } @@ -457,7 +461,6 @@ public class JSR166TestCase extends Test } else { return new TestSuite(); } - } // Delays for timing-dependent tests, in milliseconds. @@ -515,6 +518,7 @@ public class JSR166TestCase extends Test * the same test have no effect. */ public void threadRecordFailure(Throwable t) { + threadDump(); threadFailure.compareAndSet(null, t); } @@ -525,7 +529,7 @@ public class JSR166TestCase extends Test void tearDownFail(String format, Object... args) { String msg = toString() + ": " + String.format(format, args); System.err.println(msg); - printAllStackTraces(); + threadDump(); throw new AssertionFailedError(msg); } @@ -594,7 +598,7 @@ public class JSR166TestCase extends Test fail(reason); } catch (AssertionFailedError t) { threadRecordFailure(t); - fail(reason); + throw t; } } @@ -740,9 +744,19 @@ public class JSR166TestCase extends Test } /** + * Allows use of try-with-resources with per-test thread pools. + */ + static class PoolCloser + implements AutoCloseable { + public final T pool; + public PoolCloser(T pool) { this.pool = pool; } + public void close() { joinPool(pool); } + } + + /** * Waits out termination of a thread pool or fails doing so. */ - void joinPool(ExecutorService pool) { + static void joinPool(ExecutorService pool) { try { pool.shutdown(); if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) @@ -764,8 +778,9 @@ public class JSR166TestCase extends Test * necessarily individually slow because they must block. */ void testInParallel(Action ... actions) { - ExecutorService pool = Executors.newCachedThreadPool(); - try { + try (PoolCloser poolCloser + = new PoolCloser<>(Executors.newCachedThreadPool())) { + ExecutorService pool = poolCloser.pool; ArrayList> futures = new ArrayList<>(actions.length); for (final Action action : actions) futures.add(pool.submit(new CheckedRunnable() { @@ -778,19 +793,29 @@ public class JSR166TestCase extends Test } catch (Exception ex) { threadUnexpectedException(ex); } - } finally { - joinPool(pool); } } /** * A debugging tool to print all stack traces, as jstack does. + * Uninteresting threads are filtered out. */ - static void printAllStackTraces() { - for (ThreadInfo info : - ManagementFactory.getThreadMXBean() - .dumpAllThreads(true, true)) + static void threadDump() { + ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); + System.err.println("------ stacktrace dump start ------"); + for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { + String name = info.getThreadName(); + if ("Signal Dispatcher".equals(name)) + continue; + if ("Reference Handler".equals(name) + && info.getLockName().startsWith("java.lang.ref.Reference$Lock")) + continue; + if ("Finalizer".equals(name) + && info.getLockName().startsWith("java.lang.ref.ReferenceQueue$Lock")) + continue; System.err.print(info); + } + System.err.println("------ stacktrace dump end ------"); } /** @@ -1251,6 +1276,13 @@ public class JSR166TestCase extends Test }}; } + public Runnable countDowner(final CountDownLatch latch) { + return new CheckedRunnable() { + public void realRun() throws InterruptedException { + latch.countDown(); + }}; + } + public Runnable awaiter(final CountDownLatch latch) { return new CheckedRunnable() { public void realRun() throws InterruptedException {