--- jsr166/src/test/tck/JSR166TestCase.java 2015/10/03 19:29:11 1.152 +++ jsr166/src/test/tck/JSR166TestCase.java 2015/10/04 00:30:50 1.159 @@ -7,6 +7,7 @@ */ import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.MINUTES; import static java.util.concurrent.TimeUnit.NANOSECONDS; import java.io.ByteArrayInputStream; @@ -187,7 +188,29 @@ public class JSR166TestCase extends Test return (regex == null) ? null : Pattern.compile(regex); } + static volatile TestCase currentTestCase; + static { + Runnable checkForWedgedTest = new Runnable() { public void run() { + // avoid spurious reports with enormous runsPerTest + final int timeoutMinutes = Math.max(runsPerTest / 10, 1); + for (TestCase lastTestCase = currentTestCase;;) { + try { MINUTES.sleep(timeoutMinutes); } + catch (InterruptedException unexpected) { break; } + if (lastTestCase == currentTestCase) { + System.err.println + ("Looks like we're stuck running test: " + + lastTestCase); + dumpTestThreads(); + } + lastTestCase = currentTestCase; + }}}; + Thread thread = new Thread(checkForWedgedTest, "checkForWedgedTest"); + thread.setDaemon(true); + thread.start(); + } + public void runBare() throws Throwable { + currentTestCase = this; if (methodFilter == null || methodFilter.matcher(toString()).find()) super.runBare(); @@ -461,7 +484,6 @@ public class JSR166TestCase extends Test } else { return new TestSuite(); } - } // Delays for timing-dependent tests, in milliseconds. @@ -519,6 +541,8 @@ public class JSR166TestCase extends Test * the same test have no effect. */ public void threadRecordFailure(Throwable t) { + System.err.println(t); + dumpTestThreads(); threadFailure.compareAndSet(null, t); } @@ -529,7 +553,7 @@ public class JSR166TestCase extends Test void tearDownFail(String format, Object... args) { String msg = toString() + ": " + String.format(format, args); System.err.println(msg); - printAllStackTraces(); + dumpTestThreads(); throw new AssertionFailedError(msg); } @@ -746,26 +770,37 @@ public class JSR166TestCase extends Test /** * Allows use of try-with-resources with per-test thread pools. */ - static class PoolCloser + class PoolCleaner implements AutoCloseable { public final T pool; - public PoolCloser(T pool) { this.pool = pool; } + public PoolCleaner(T pool) { this.pool = pool; } public void close() { joinPool(pool); } } + PoolCleaner cleaner(T pool) { + return new PoolCleaner(pool); + } + /** * Waits out termination of a thread pool or fails doing so. */ - static void joinPool(ExecutorService pool) { + void joinPool(ExecutorService pool) { try { pool.shutdown(); - if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) - fail("ExecutorService " + pool + - " did not terminate in a timely manner"); + if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) { + try { + threadFail("ExecutorService " + pool + + " did not terminate in a timely manner"); + } finally { + // last resort, for the benefit of subsequent tests + pool.shutdownNow(); + pool.awaitTermination(SMALL_DELAY_MS, MILLISECONDS); + } + } } catch (SecurityException ok) { // Allowed in case test doesn't have privs } catch (InterruptedException fail) { - fail("Unexpected InterruptedException"); + threadFail("Unexpected InterruptedException"); } } @@ -778,9 +813,9 @@ public class JSR166TestCase extends Test * necessarily individually slow because they must block. */ void testInParallel(Action ... actions) { - try (PoolCloser poolCloser - = new PoolCloser<>(Executors.newCachedThreadPool())) { - ExecutorService pool = poolCloser.pool; + try (PoolCleaner cleaner + = cleaner(Executors.newCachedThreadPool())) { + ExecutorService pool = cleaner.pool; ArrayList> futures = new ArrayList<>(actions.length); for (final Action action : actions) futures.add(pool.submit(new CheckedRunnable() { @@ -797,10 +832,10 @@ public class JSR166TestCase extends Test } /** - * A debugging tool to print all stack traces, as jstack does. + * A debugging tool to print stack traces of most threads, as jstack does. * Uninteresting threads are filtered out. */ - static void printAllStackTraces() { + static void dumpTestThreads() { ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); System.err.println("------ stacktrace dump start ------"); for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { @@ -813,6 +848,8 @@ public class JSR166TestCase extends Test if ("Finalizer".equals(name) && info.getLockName().startsWith("java.lang.ref.ReferenceQueue$Lock")) continue; + if ("checkForWedgedTest".equals(name)) + continue; System.err.print(info); } System.err.println("------ stacktrace dump end ------"); @@ -835,7 +872,7 @@ public class JSR166TestCase extends Test delay(millis); assertTrue(thread.isAlive()); } catch (InterruptedException fail) { - fail("Unexpected InterruptedException"); + threadFail("Unexpected InterruptedException"); } } @@ -857,7 +894,7 @@ public class JSR166TestCase extends Test for (Thread thread : threads) assertTrue(thread.isAlive()); } catch (InterruptedException fail) { - fail("Unexpected InterruptedException"); + threadFail("Unexpected InterruptedException"); } }