--- jsr166/src/test/tck/JSR166TestCase.java 2015/10/04 00:30:50 1.159 +++ jsr166/src/test/tck/JSR166TestCase.java 2015/10/09 01:26:36 1.172 @@ -20,6 +20,8 @@ import java.lang.management.ThreadMXBean import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.nio.file.Files; +import java.nio.file.Paths; import java.security.CodeSource; import java.security.Permission; import java.security.PermissionCollection; @@ -52,6 +54,7 @@ import java.util.concurrent.ThreadFactor import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicReference; +import java.util.regex.Matcher; import java.util.regex.Pattern; import junit.framework.AssertionFailedError; @@ -188,7 +191,9 @@ public class JSR166TestCase extends Test return (regex == null) ? null : Pattern.compile(regex); } + // Instrumentation to debug very rare, but very annoying hung test runs. static volatile TestCase currentTestCase; + static volatile int currentRun = 0; static { Runnable checkForWedgedTest = new Runnable() { public void run() { // avoid spurious reports with enormous runsPerTest @@ -197,10 +202,15 @@ public class JSR166TestCase extends Test try { MINUTES.sleep(timeoutMinutes); } catch (InterruptedException unexpected) { break; } if (lastTestCase == currentTestCase) { - System.err.println - ("Looks like we're stuck running test: " - + lastTestCase); + System.err.printf( + "Looks like we're stuck running test: %s (%d/%d)%n", + lastTestCase, currentRun, runsPerTest); + System.err.println("availableProcessors=" + + Runtime.getRuntime().availableProcessors()); + System.err.printf("cpu model = %s%n", cpuModel()); dumpTestThreads(); + // one stack dump is probably enough; more would be spam + break; } lastTestCase = currentTestCase; }}}; @@ -209,6 +219,16 @@ public class JSR166TestCase extends Test thread.start(); } + public static String cpuModel() { + try { + Matcher matcher = Pattern.compile("model name\\s*: (.*)") + .matcher(new String( + Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8")); + matcher.find(); + return matcher.group(1); + } catch (Exception ex) { return null; } + } + public void runBare() throws Throwable { currentTestCase = this; if (methodFilter == null @@ -218,6 +238,7 @@ public class JSR166TestCase extends Test protected void runTest() throws Throwable { for (int i = 0; i < runsPerTest; i++) { + currentRun = i; if (profileTests) runTestProfiled(); else @@ -590,7 +611,7 @@ public class JSR166TestCase extends Test } /** - * Finds missing try { ... } finally { joinPool(e); } + * Finds missing PoolCleaners */ void checkForkJoinPoolThreadLeaks() throws InterruptedException { Thread[] survivors = new Thread[7]; @@ -749,36 +770,66 @@ public class JSR166TestCase extends Test /** * Delays, via Thread.sleep, for the given millisecond delay, but * if the sleep is shorter than specified, may re-sleep or yield - * until time elapses. + * until time elapses. Ensures that the given time, as measured + * by System.nanoTime(), has elapsed. */ static void delay(long millis) throws InterruptedException { - long startTime = System.nanoTime(); - long ns = millis * 1000 * 1000; - for (;;) { + long nanos = millis * (1000 * 1000); + final long wakeupTime = System.nanoTime() + nanos; + do { if (millis > 0L) Thread.sleep(millis); else // too short to sleep Thread.yield(); - long d = ns - (System.nanoTime() - startTime); - if (d > 0L) - millis = d / (1000 * 1000); - else - break; - } + nanos = wakeupTime - System.nanoTime(); + millis = nanos / (1000 * 1000); + } while (nanos >= 0L); } /** * Allows use of try-with-resources with per-test thread pools. */ - class PoolCleaner - implements AutoCloseable { - public final T pool; - public PoolCleaner(T pool) { this.pool = pool; } + class PoolCleaner implements AutoCloseable { + private final ExecutorService pool; + public PoolCleaner(ExecutorService pool) { this.pool = pool; } public void close() { joinPool(pool); } } - PoolCleaner cleaner(T pool) { - return new PoolCleaner(pool); + /** + * An extension of PoolCleaner that has an action to release the pool. + */ + class PoolCleanerWithReleaser extends PoolCleaner { + private final Runnable releaser; + public PoolCleanerWithReleaser(ExecutorService pool, Runnable releaser) { + super(pool); + this.releaser = releaser; + } + public void close() { + try { + releaser.run(); + } finally { + super.close(); + } + } + } + + PoolCleaner cleaner(ExecutorService pool) { + return new PoolCleaner(pool); + } + + PoolCleaner cleaner(ExecutorService pool, Runnable releaser) { + return new PoolCleanerWithReleaser(pool, releaser); + } + + PoolCleaner cleaner(ExecutorService pool, CountDownLatch latch) { + return new PoolCleanerWithReleaser(pool, releaser(latch)); + } + + Runnable releaser(final CountDownLatch latch) { + return new Runnable() { public void run() { + do { latch.countDown(); } + while (latch.getCount() > 0); + }}; } /** @@ -794,7 +845,7 @@ public class JSR166TestCase extends Test } finally { // last resort, for the benefit of subsequent tests pool.shutdownNow(); - pool.awaitTermination(SMALL_DELAY_MS, MILLISECONDS); + pool.awaitTermination(MEDIUM_DELAY_MS, MILLISECONDS); } } } catch (SecurityException ok) { @@ -813,9 +864,8 @@ public class JSR166TestCase extends Test * necessarily individually slow because they must block. */ void testInParallel(Action ... actions) { - try (PoolCleaner cleaner - = cleaner(Executors.newCachedThreadPool())) { - ExecutorService pool = cleaner.pool; + ExecutorService pool = Executors.newCachedThreadPool(); + try (PoolCleaner cleaner = cleaner(pool)) { ArrayList> futures = new ArrayList<>(actions.length); for (final Action action : actions) futures.add(pool.submit(new CheckedRunnable() { @@ -1172,7 +1222,7 @@ public class JSR166TestCase extends Test } finally { if (t.getState() != Thread.State.TERMINATED) { t.interrupt(); - fail("Test timed out"); + threadFail("Test timed out"); } } } @@ -1320,11 +1370,22 @@ public class JSR166TestCase extends Test }}; } - public Runnable awaiter(final CountDownLatch latch) { - return new CheckedRunnable() { - public void realRun() throws InterruptedException { - await(latch); - }}; + class LatchAwaiter extends CheckedRunnable { + static final int NEW = 0; + static final int RUNNING = 1; + static final int DONE = 2; + final CountDownLatch latch; + int state = NEW; + LatchAwaiter(CountDownLatch latch) { this.latch = latch; } + public void realRun() throws InterruptedException { + state = 1; + await(latch); + state = 2; + } + } + + public LatchAwaiter awaiter(CountDownLatch latch) { + return new LatchAwaiter(latch); } public void await(CountDownLatch latch) {