ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/JSR166TestCase.java
(Generate patch)

Comparing jsr166/src/test/tck/JSR166TestCase.java (file contents):
Revision 1.126 by jsr166, Sat Jan 17 22:55:06 2015 UTC vs.
Revision 1.174 by jsr166, Fri Oct 9 19:09:59 2015 UTC

# Line 7 | Line 7
7   */
8  
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 + import static java.util.concurrent.TimeUnit.MINUTES;
11   import static java.util.concurrent.TimeUnit.NANOSECONDS;
12  
13   import java.io.ByteArrayInputStream;
# Line 15 | Line 16 | import java.io.ObjectInputStream;
16   import java.io.ObjectOutputStream;
17   import java.lang.management.ManagementFactory;
18   import java.lang.management.ThreadInfo;
19 + import java.lang.management.ThreadMXBean;
20 + import java.lang.reflect.Constructor;
21   import java.lang.reflect.Method;
22 + import java.lang.reflect.Modifier;
23 + import java.nio.file.Files;
24 + import java.nio.file.Paths;
25   import java.security.CodeSource;
26   import java.security.Permission;
27   import java.security.PermissionCollection;
# Line 35 | Line 41 | import java.util.concurrent.BlockingQueu
41   import java.util.concurrent.Callable;
42   import java.util.concurrent.CountDownLatch;
43   import java.util.concurrent.CyclicBarrier;
44 + import java.util.concurrent.ExecutionException;
45 + import java.util.concurrent.Executors;
46   import java.util.concurrent.ExecutorService;
47 + import java.util.concurrent.ForkJoinPool;
48   import java.util.concurrent.Future;
49   import java.util.concurrent.RecursiveAction;
50   import java.util.concurrent.RecursiveTask;
# Line 45 | Line 54 | import java.util.concurrent.ThreadFactor
54   import java.util.concurrent.ThreadPoolExecutor;
55   import java.util.concurrent.TimeoutException;
56   import java.util.concurrent.atomic.AtomicReference;
57 + import java.util.regex.Matcher;
58   import java.util.regex.Pattern;
59  
60   import junit.framework.AssertionFailedError;
61   import junit.framework.Test;
62   import junit.framework.TestCase;
63 + import junit.framework.TestResult;
64   import junit.framework.TestSuite;
65  
66   /**
# Line 62 | Line 73 | import junit.framework.TestSuite;
73   *
74   * <ol>
75   *
76 < * <li> All assertions in code running in generated threads must use
76 > * <li>All assertions in code running in generated threads must use
77   * the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link
78   * #threadAssertEquals}, or {@link #threadAssertNull}, (not
79   * {@code fail}, {@code assertTrue}, etc.) It is OK (but not
80   * particularly recommended) for other code to use these forms too.
81   * Only the most typically used JUnit assertion methods are defined
82 < * this way, but enough to live with.</li>
82 > * this way, but enough to live with.
83   *
84 < * <li> If you override {@link #setUp} or {@link #tearDown}, make sure
84 > * <li>If you override {@link #setUp} or {@link #tearDown}, make sure
85   * to invoke {@code super.setUp} and {@code super.tearDown} within
86   * them. These methods are used to clear and check for thread
87 < * assertion failures.</li>
87 > * assertion failures.
88   *
89   * <li>All delays and timeouts must use one of the constants {@code
90   * SHORT_DELAY_MS}, {@code SMALL_DELAY_MS}, {@code MEDIUM_DELAY_MS},
# Line 84 | Line 95 | import junit.framework.TestSuite;
95   * is always discriminable as larger than SHORT and smaller than
96   * MEDIUM.  And so on. These constants are set to conservative values,
97   * but even so, if there is ever any doubt, they can all be increased
98 < * in one spot to rerun tests on slower platforms.</li>
98 > * in one spot to rerun tests on slower platforms.
99   *
100 < * <li> All threads generated must be joined inside each test case
100 > * <li>All threads generated must be joined inside each test case
101   * method (or {@code fail} to do so) before returning from the
102   * method. The {@code joinPool} method can be used to do this when
103 < * using Executors.</li>
103 > * using Executors.
104   *
105   * </ol>
106   *
107   * <p><b>Other notes</b>
108   * <ul>
109   *
110 < * <li> Usually, there is one testcase method per JSR166 method
110 > * <li>Usually, there is one testcase method per JSR166 method
111   * covering "normal" operation, and then as many exception-testing
112   * methods as there are exceptions the method can throw. Sometimes
113   * there are multiple tests per JSR166 method when the different
114   * "normal" behaviors differ significantly. And sometimes testcases
115   * cover multiple methods when they cannot be tested in
116 < * isolation.</li>
116 > * isolation.
117   *
118 < * <li> The documentation style for testcases is to provide as javadoc
118 > * <li>The documentation style for testcases is to provide as javadoc
119   * a simple sentence or two describing the property that the testcase
120   * method purports to test. The javadocs do not say anything about how
121 < * the property is tested. To find out, read the code.</li>
121 > * the property is tested. To find out, read the code.
122   *
123 < * <li> These tests are "conformance tests", and do not attempt to
123 > * <li>These tests are "conformance tests", and do not attempt to
124   * test throughput, latency, scalability or other performance factors
125   * (see the separate "jtreg" tests for a set intended to check these
126   * for the most central aspects of functionality.) So, most tests use
127   * the smallest sensible numbers of threads, collection sizes, etc
128 < * needed to check basic conformance.</li>
128 > * needed to check basic conformance.
129   *
130   * <li>The test classes currently do not declare inclusion in
131   * any particular package to simplify things for people integrating
132 < * them in TCK test suites.</li>
132 > * them in TCK test suites.
133   *
134 < * <li> As a convenience, the {@code main} of this class (JSR166TestCase)
135 < * runs all JSR166 unit tests.</li>
134 > * <li>As a convenience, the {@code main} of this class (JSR166TestCase)
135 > * runs all JSR166 unit tests.
136   *
137   * </ul>
138   */
# Line 160 | Line 171 | public class JSR166TestCase extends Test
171          Integer.getInteger("jsr166.runsPerTest", 1);
172  
173      /**
174 +     * The number of repetitions of the test suite (for finding leaks?).
175 +     */
176 +    private static final int suiteRuns =
177 +        Integer.getInteger("jsr166.suiteRuns", 1);
178 +
179 +    public JSR166TestCase() { super(); }
180 +    public JSR166TestCase(String name) { super(name); }
181 +
182 +    /**
183       * A filter for tests to run, matching strings of the form
184       * methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)"
185       * Usefully combined with jsr166.runsPerTest.
# Line 171 | Line 191 | public class JSR166TestCase extends Test
191          return (regex == null) ? null : Pattern.compile(regex);
192      }
193  
194 <    protected void runTest() throws Throwable {
194 >    // Instrumentation to debug very rare, but very annoying hung test runs.
195 >    static volatile TestCase currentTestCase;
196 >    // static volatile int currentRun = 0;
197 >    static {
198 >        Runnable checkForWedgedTest = new Runnable() { public void run() {
199 >            // Avoid spurious reports with enormous runsPerTest.
200 >            // A single test case run should never take more than 1 second.
201 >            // But let's cap it at the high end too ...
202 >            final int timeoutMinutes =
203 >                Math.min(15, Math.max(runsPerTest / 60, 1));
204 >            for (TestCase lastTestCase = currentTestCase;;) {
205 >                try { MINUTES.sleep(timeoutMinutes); }
206 >                catch (InterruptedException unexpected) { break; }
207 >                if (lastTestCase == currentTestCase) {
208 >                    System.err.printf(
209 >                        "Looks like we're stuck running test: %s%n",
210 >                        lastTestCase);
211 > //                     System.err.printf(
212 > //                         "Looks like we're stuck running test: %s (%d/%d)%n",
213 > //                         lastTestCase, currentRun, runsPerTest);
214 >                    System.err.println("availableProcessors=" +
215 >                        Runtime.getRuntime().availableProcessors());
216 >                    System.err.printf("cpu model = %s%n", cpuModel());
217 >                    dumpTestThreads();
218 >                    // one stack dump is probably enough; more would be spam
219 >                    break;
220 >                }
221 >                lastTestCase = currentTestCase;
222 >            }}};
223 >        Thread thread = new Thread(checkForWedgedTest, "checkForWedgedTest");
224 >        thread.setDaemon(true);
225 >        thread.start();
226 >    }
227 >
228 >    public static String cpuModel() {
229 >        try {
230 >            Matcher matcher = Pattern.compile("model name\\s*: (.*)")
231 >                .matcher(new String(
232 >                     Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8"));
233 >            matcher.find();
234 >            return matcher.group(1);
235 >        } catch (Exception ex) { return null; }
236 >    }
237 >
238 >    public void runBare() throws Throwable {
239 >        currentTestCase = this;
240          if (methodFilter == null
241 <            || methodFilter.matcher(toString()).find()) {
242 <            for (int i = 0; i < runsPerTest; i++) {
243 <                if (profileTests)
244 <                    runTestProfiled();
245 <                else
246 <                    super.runTest();
247 <            }
241 >            || methodFilter.matcher(toString()).find())
242 >            super.runBare();
243 >    }
244 >
245 >    protected void runTest() throws Throwable {
246 >        for (int i = 0; i < runsPerTest; i++) {
247 >            // currentRun = i;
248 >            if (profileTests)
249 >                runTestProfiled();
250 >            else
251 >                super.runTest();
252          }
253      }
254  
255      protected void runTestProfiled() throws Throwable {
256 <        // Warmup run, notably to trigger all needed classloading.
257 <        super.runTest();
189 <        long t0 = System.nanoTime();
190 <        try {
256 >        for (int i = 0; i < 2; i++) {
257 >            long startTime = System.nanoTime();
258              super.runTest();
259 <        } finally {
260 <            long elapsedMillis = millisElapsedSince(t0);
261 <            if (elapsedMillis >= profileThreshold)
259 >            long elapsedMillis = millisElapsedSince(startTime);
260 >            if (elapsedMillis < profileThreshold)
261 >                break;
262 >            // Never report first run of any test; treat it as a
263 >            // warmup run, notably to trigger all needed classloading,
264 >            if (i > 0)
265                  System.out.printf("%n%s: %d%n", toString(), elapsedMillis);
266          }
267      }
268  
269      /**
270       * Runs all JSR166 unit tests using junit.textui.TestRunner.
201     * Optional command line arg provides the number of iterations to
202     * repeat running the tests.
271       */
272      public static void main(String[] args) {
273 +        main(suite(), args);
274 +    }
275 +
276 +    /**
277 +     * Runs all unit tests in the given test suite.
278 +     * Actual behavior influenced by jsr166.* system properties.
279 +     */
280 +    static void main(Test suite, String[] args) {
281          if (useSecurityManager) {
282              System.err.println("Setting a permissive security manager");
283              Policy.setPolicy(permissivePolicy());
284              System.setSecurityManager(new SecurityManager());
285          }
286 <        int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]);
287 <
288 <        Test s = suite();
289 <        for (int i = 0; i < iters; ++i) {
214 <            junit.textui.TestRunner.run(s);
286 >        for (int i = 0; i < suiteRuns; i++) {
287 >            TestResult result = junit.textui.TestRunner.run(suite);
288 >            if (!result.wasSuccessful())
289 >                System.exit(1);
290              System.gc();
291              System.runFinalization();
292          }
218        System.exit(0);
293      }
294  
295      public static TestSuite newTestSuite(Object... suiteOrClasses) {
# Line 266 | Line 340 | public class JSR166TestCase extends Test
340      public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; }
341      public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; }
342      public static boolean atLeastJava9() {
343 <        // As of 2014-05, java9 still uses 52.0 class file version
344 <        return JAVA_SPECIFICATION_VERSION.startsWith("1.9");
343 >        return JAVA_CLASS_VERSION >= 53.0
344 >            // As of 2015-09, java9 still uses 52.0 class file version
345 >            || JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?(9|[0-9][0-9])$");
346 >    }
347 >    public static boolean atLeastJava10() {
348 >        return JAVA_CLASS_VERSION >= 54.0
349 >            || JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?[0-9][0-9]$");
350      }
351  
352      /**
# Line 356 | Line 435 | public class JSR166TestCase extends Test
435                  "LongAdderTest",
436                  "SplittableRandomTest",
437                  "StampedLockTest",
438 +                "SubmissionPublisherTest",
439                  "ThreadLocalRandom8Test",
440              };
441              addNamedTestClasses(suite, java8TestClassNames);
# Line 364 | Line 444 | public class JSR166TestCase extends Test
444          // Java9+ test classes
445          if (atLeastJava9()) {
446              String[] java9TestClassNames = {
447 <                "ThreadPoolExecutor9Test",
447 >                // Currently empty, but expecting varhandle tests
448              };
449              addNamedTestClasses(suite, java9TestClassNames);
450          }
# Line 372 | Line 452 | public class JSR166TestCase extends Test
452          return suite;
453      }
454  
455 +    /** Returns list of junit-style test method names in given class. */
456 +    public static ArrayList<String> testMethodNames(Class<?> testClass) {
457 +        Method[] methods = testClass.getDeclaredMethods();
458 +        ArrayList<String> names = new ArrayList<String>(methods.length);
459 +        for (Method method : methods) {
460 +            if (method.getName().startsWith("test")
461 +                && Modifier.isPublic(method.getModifiers())
462 +                // method.getParameterCount() requires jdk8+
463 +                && method.getParameterTypes().length == 0) {
464 +                names.add(method.getName());
465 +            }
466 +        }
467 +        return names;
468 +    }
469 +
470 +    /**
471 +     * Returns junit-style testSuite for the given test class, but
472 +     * parameterized by passing extra data to each test.
473 +     */
474 +    public static <ExtraData> Test parameterizedTestSuite
475 +        (Class<? extends JSR166TestCase> testClass,
476 +         Class<ExtraData> dataClass,
477 +         ExtraData data) {
478 +        try {
479 +            TestSuite suite = new TestSuite();
480 +            Constructor c =
481 +                testClass.getDeclaredConstructor(dataClass, String.class);
482 +            for (String methodName : testMethodNames(testClass))
483 +                suite.addTest((Test) c.newInstance(data, methodName));
484 +            return suite;
485 +        } catch (Exception e) {
486 +            throw new Error(e);
487 +        }
488 +    }
489 +
490 +    /**
491 +     * Returns junit-style testSuite for the jdk8 extension of the
492 +     * given test class, but parameterized by passing extra data to
493 +     * each test.  Uses reflection to allow compilation in jdk7.
494 +     */
495 +    public static <ExtraData> Test jdk8ParameterizedTestSuite
496 +        (Class<? extends JSR166TestCase> testClass,
497 +         Class<ExtraData> dataClass,
498 +         ExtraData data) {
499 +        if (atLeastJava8()) {
500 +            String name = testClass.getName();
501 +            String name8 = name.replaceAll("Test$", "8Test");
502 +            if (name.equals(name8)) throw new Error(name);
503 +            try {
504 +                return (Test)
505 +                    Class.forName(name8)
506 +                    .getMethod("testSuite", new Class[] { dataClass })
507 +                    .invoke(null, data);
508 +            } catch (Exception e) {
509 +                throw new Error(e);
510 +            }
511 +        } else {
512 +            return new TestSuite();
513 +        }
514 +    }
515 +
516      // Delays for timing-dependent tests, in milliseconds.
517  
518      public static long SHORT_DELAY_MS;
# Line 406 | Line 547 | public class JSR166TestCase extends Test
547      }
548  
549      /**
550 <     * Returns a new Date instance representing a time delayMillis
551 <     * milliseconds in the future.
550 >     * Returns a new Date instance representing a time at least
551 >     * delayMillis milliseconds in the future.
552       */
553      Date delayedDate(long delayMillis) {
554 <        return new Date(System.currentTimeMillis() + delayMillis);
554 >        // Add 1 because currentTimeMillis is known to round into the past.
555 >        return new Date(System.currentTimeMillis() + delayMillis + 1);
556      }
557  
558      /**
# Line 426 | Line 568 | public class JSR166TestCase extends Test
568       * the same test have no effect.
569       */
570      public void threadRecordFailure(Throwable t) {
571 +        System.err.println(t);
572 +        dumpTestThreads();
573          threadFailure.compareAndSet(null, t);
574      }
575  
# Line 433 | Line 577 | public class JSR166TestCase extends Test
577          setDelays();
578      }
579  
580 +    void tearDownFail(String format, Object... args) {
581 +        String msg = toString() + ": " + String.format(format, args);
582 +        System.err.println(msg);
583 +        dumpTestThreads();
584 +        throw new AssertionFailedError(msg);
585 +    }
586 +
587      /**
588       * Extra checks that get done for all test cases.
589       *
# Line 460 | Line 611 | public class JSR166TestCase extends Test
611          }
612  
613          if (Thread.interrupted())
614 <            throw new AssertionFailedError("interrupt status set in main thread");
614 >            tearDownFail("interrupt status set in main thread");
615  
616          checkForkJoinPoolThreadLeaks();
617      }
618  
619      /**
620 <     * Finds missing try { ... } finally { joinPool(e); }
620 >     * Finds missing PoolCleaners
621       */
622      void checkForkJoinPoolThreadLeaks() throws InterruptedException {
623 <        Thread[] survivors = new Thread[5];
623 >        Thread[] survivors = new Thread[7];
624          int count = Thread.enumerate(survivors);
625          for (int i = 0; i < count; i++) {
626              Thread thread = survivors[i];
# Line 477 | Line 628 | public class JSR166TestCase extends Test
628              if (name.startsWith("ForkJoinPool-")) {
629                  // give thread some time to terminate
630                  thread.join(LONG_DELAY_MS);
631 <                if (!thread.isAlive()) continue;
632 <                thread.stop();
633 <                throw new AssertionFailedError
483 <                    (String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n",
484 <                                   toString(), name));
631 >                if (thread.isAlive())
632 >                    tearDownFail("Found leaked ForkJoinPool thread thread=%s",
633 >                                 thread);
634              }
635          }
636 +
637 +        if (!ForkJoinPool.commonPool()
638 +            .awaitQuiescence(LONG_DELAY_MS, MILLISECONDS))
639 +            tearDownFail("ForkJoin common pool thread stuck");
640      }
641  
642      /**
# Line 496 | Line 649 | public class JSR166TestCase extends Test
649              fail(reason);
650          } catch (AssertionFailedError t) {
651              threadRecordFailure(t);
652 <            fail(reason);
652 >            throw t;
653          }
654      }
655  
# Line 564 | Line 717 | public class JSR166TestCase extends Test
717      public void threadAssertEquals(Object x, Object y) {
718          try {
719              assertEquals(x, y);
720 <        } catch (AssertionFailedError t) {
721 <            threadRecordFailure(t);
722 <            throw t;
723 <        } catch (Throwable t) {
724 <            threadUnexpectedException(t);
720 >        } catch (AssertionFailedError fail) {
721 >            threadRecordFailure(fail);
722 >            throw fail;
723 >        } catch (Throwable fail) {
724 >            threadUnexpectedException(fail);
725          }
726      }
727  
# Line 580 | Line 733 | public class JSR166TestCase extends Test
733      public void threadAssertSame(Object x, Object y) {
734          try {
735              assertSame(x, y);
736 <        } catch (AssertionFailedError t) {
737 <            threadRecordFailure(t);
738 <            throw t;
736 >        } catch (AssertionFailedError fail) {
737 >            threadRecordFailure(fail);
738 >            throw fail;
739          }
740      }
741  
# Line 623 | Line 776 | public class JSR166TestCase extends Test
776      /**
777       * Delays, via Thread.sleep, for the given millisecond delay, but
778       * if the sleep is shorter than specified, may re-sleep or yield
779 <     * until time elapses.
779 >     * until time elapses.  Ensures that the given time, as measured
780 >     * by System.nanoTime(), has elapsed.
781       */
782      static void delay(long millis) throws InterruptedException {
783 <        long startTime = System.nanoTime();
784 <        long ns = millis * 1000 * 1000;
785 <        for (;;) {
783 >        long nanos = millis * (1000 * 1000);
784 >        final long wakeupTime = System.nanoTime() + nanos;
785 >        do {
786              if (millis > 0L)
787                  Thread.sleep(millis);
788              else // too short to sleep
789                  Thread.yield();
790 <            long d = ns - (System.nanoTime() - startTime);
791 <            if (d > 0L)
792 <                millis = d / (1000 * 1000);
793 <            else
794 <                break;
790 >            nanos = wakeupTime - System.nanoTime();
791 >            millis = nanos / (1000 * 1000);
792 >        } while (nanos >= 0L);
793 >    }
794 >
795 >    /**
796 >     * Allows use of try-with-resources with per-test thread pools.
797 >     */
798 >    class PoolCleaner implements AutoCloseable {
799 >        private final ExecutorService pool;
800 >        public PoolCleaner(ExecutorService pool) { this.pool = pool; }
801 >        public void close() { joinPool(pool); }
802 >    }
803 >
804 >    /**
805 >     * An extension of PoolCleaner that has an action to release the pool.
806 >     */
807 >    class PoolCleanerWithReleaser extends PoolCleaner {
808 >        private final Runnable releaser;
809 >        public PoolCleanerWithReleaser(ExecutorService pool, Runnable releaser) {
810 >            super(pool);
811 >            this.releaser = releaser;
812 >        }
813 >        public void close() {
814 >            try {
815 >                releaser.run();
816 >            } finally {
817 >                super.close();
818 >            }
819          }
820      }
821  
822 +    PoolCleaner cleaner(ExecutorService pool) {
823 +        return new PoolCleaner(pool);
824 +    }
825 +
826 +    PoolCleaner cleaner(ExecutorService pool, Runnable releaser) {
827 +        return new PoolCleanerWithReleaser(pool, releaser);
828 +    }
829 +
830 +    PoolCleaner cleaner(ExecutorService pool, CountDownLatch latch) {
831 +        return new PoolCleanerWithReleaser(pool, releaser(latch));
832 +    }
833 +
834 +    Runnable releaser(final CountDownLatch latch) {
835 +        return new Runnable() { public void run() {
836 +            do { latch.countDown(); }
837 +            while (latch.getCount() > 0);
838 +        }};
839 +    }
840 +
841      /**
842       * Waits out termination of a thread pool or fails doing so.
843       */
844 <    void joinPool(ExecutorService exec) {
844 >    void joinPool(ExecutorService pool) {
845          try {
846 <            exec.shutdown();
847 <            if (!exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS))
848 <                fail("ExecutorService " + exec +
849 <                     " did not terminate in a timely manner");
846 >            pool.shutdown();
847 >            if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) {
848 >                try {
849 >                    threadFail("ExecutorService " + pool +
850 >                               " did not terminate in a timely manner");
851 >                } finally {
852 >                    // last resort, for the benefit of subsequent tests
853 >                    pool.shutdownNow();
854 >                    pool.awaitTermination(MEDIUM_DELAY_MS, MILLISECONDS);
855 >                }
856 >            }
857          } catch (SecurityException ok) {
858              // Allowed in case test doesn't have privs
859 <        } catch (InterruptedException ie) {
860 <            fail("Unexpected InterruptedException");
859 >        } catch (InterruptedException fail) {
860 >            threadFail("Unexpected InterruptedException");
861 >        }
862 >    }
863 >
864 >    /** Like Runnable, but with the freedom to throw anything */
865 >    interface Action { public void run() throws Throwable; }
866 >
867 >    /**
868 >     * Runs all the given actions in parallel, failing if any fail.
869 >     * Useful for running multiple variants of tests that are
870 >     * necessarily individually slow because they must block.
871 >     */
872 >    void testInParallel(Action ... actions) {
873 >        ExecutorService pool = Executors.newCachedThreadPool();
874 >        try (PoolCleaner cleaner = cleaner(pool)) {
875 >            ArrayList<Future<?>> futures = new ArrayList<>(actions.length);
876 >            for (final Action action : actions)
877 >                futures.add(pool.submit(new CheckedRunnable() {
878 >                    public void realRun() throws Throwable { action.run();}}));
879 >            for (Future<?> future : futures)
880 >                try {
881 >                    assertNull(future.get(LONG_DELAY_MS, MILLISECONDS));
882 >                } catch (ExecutionException ex) {
883 >                    threadUnexpectedException(ex.getCause());
884 >                } catch (Exception ex) {
885 >                    threadUnexpectedException(ex);
886 >                }
887          }
888      }
889  
890      /**
891 <     * A debugging tool to print all stack traces, as jstack does.
891 >     * A debugging tool to print stack traces of most threads, as jstack does.
892 >     * Uninteresting threads are filtered out.
893       */
894 <    static void printAllStackTraces() {
895 <        for (ThreadInfo info :
896 <                 ManagementFactory.getThreadMXBean()
897 <                 .dumpAllThreads(true, true))
894 >    static void dumpTestThreads() {
895 >        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
896 >        System.err.println("------ stacktrace dump start ------");
897 >        for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) {
898 >            String name = info.getThreadName();
899 >            if ("Signal Dispatcher".equals(name))
900 >                continue;
901 >            if ("Reference Handler".equals(name)
902 >                && info.getLockName().startsWith("java.lang.ref.Reference$Lock"))
903 >                continue;
904 >            if ("Finalizer".equals(name)
905 >                && info.getLockName().startsWith("java.lang.ref.ReferenceQueue$Lock"))
906 >                continue;
907 >            if ("checkForWedgedTest".equals(name))
908 >                continue;
909              System.err.print(info);
910 +        }
911 +        System.err.println("------ stacktrace dump end ------");
912      }
913  
914      /**
# Line 683 | Line 927 | public class JSR166TestCase extends Test
927              // No need to optimize the failing case via Thread.join.
928              delay(millis);
929              assertTrue(thread.isAlive());
930 <        } catch (InterruptedException ie) {
931 <            fail("Unexpected InterruptedException");
930 >        } catch (InterruptedException fail) {
931 >            threadFail("Unexpected InterruptedException");
932          }
933      }
934  
# Line 705 | Line 949 | public class JSR166TestCase extends Test
949              delay(millis);
950              for (Thread thread : threads)
951                  assertTrue(thread.isAlive());
952 <        } catch (InterruptedException ie) {
953 <            fail("Unexpected InterruptedException");
952 >        } catch (InterruptedException fail) {
953 >            threadFail("Unexpected InterruptedException");
954          }
955      }
956  
# Line 727 | Line 971 | public class JSR166TestCase extends Test
971              future.get(timeoutMillis, MILLISECONDS);
972              shouldThrow();
973          } catch (TimeoutException success) {
974 <        } catch (Exception e) {
975 <            threadUnexpectedException(e);
974 >        } catch (Exception fail) {
975 >            threadUnexpectedException(fail);
976          } finally { future.cancel(true); }
977          assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
978      }
# Line 884 | Line 1128 | public class JSR166TestCase extends Test
1128      void sleep(long millis) {
1129          try {
1130              delay(millis);
1131 <        } catch (InterruptedException ie) {
1131 >        } catch (InterruptedException fail) {
1132              AssertionFailedError afe =
1133                  new AssertionFailedError("Unexpected InterruptedException");
1134 <            afe.initCause(ie);
1134 >            afe.initCause(fail);
1135              throw afe;
1136          }
1137      }
# Line 979 | Line 1223 | public class JSR166TestCase extends Test
1223      void awaitTermination(Thread t, long timeoutMillis) {
1224          try {
1225              t.join(timeoutMillis);
1226 <        } catch (InterruptedException ie) {
1227 <            threadUnexpectedException(ie);
1226 >        } catch (InterruptedException fail) {
1227 >            threadUnexpectedException(fail);
1228          } finally {
1229              if (t.getState() != Thread.State.TERMINATED) {
1230                  t.interrupt();
1231 <                fail("Test timed out");
1231 >                threadFail("Test timed out");
1232              }
1233          }
1234      }
# Line 1006 | Line 1250 | public class JSR166TestCase extends Test
1250          public final void run() {
1251              try {
1252                  realRun();
1253 <            } catch (Throwable t) {
1254 <                threadUnexpectedException(t);
1253 >            } catch (Throwable fail) {
1254 >                threadUnexpectedException(fail);
1255              }
1256          }
1257      }
# Line 1061 | Line 1305 | public class JSR166TestCase extends Test
1305                  threadShouldThrow("InterruptedException");
1306              } catch (InterruptedException success) {
1307                  threadAssertFalse(Thread.interrupted());
1308 <            } catch (Throwable t) {
1309 <                threadUnexpectedException(t);
1308 >            } catch (Throwable fail) {
1309 >                threadUnexpectedException(fail);
1310              }
1311          }
1312      }
# Line 1073 | Line 1317 | public class JSR166TestCase extends Test
1317          public final T call() {
1318              try {
1319                  return realCall();
1320 <            } catch (Throwable t) {
1321 <                threadUnexpectedException(t);
1320 >            } catch (Throwable fail) {
1321 >                threadUnexpectedException(fail);
1322                  return null;
1323              }
1324          }
# Line 1091 | Line 1335 | public class JSR166TestCase extends Test
1335                  return result;
1336              } catch (InterruptedException success) {
1337                  threadAssertFalse(Thread.interrupted());
1338 <            } catch (Throwable t) {
1339 <                threadUnexpectedException(t);
1338 >            } catch (Throwable fail) {
1339 >                threadUnexpectedException(fail);
1340              }
1341              return null;
1342          }
# Line 1109 | Line 1353 | public class JSR166TestCase extends Test
1353      public static final String TEST_STRING = "a test string";
1354  
1355      public static class StringTask implements Callable<String> {
1356 <        public String call() { return TEST_STRING; }
1356 >        final String value;
1357 >        public StringTask() { this(TEST_STRING); }
1358 >        public StringTask(String value) { this.value = value; }
1359 >        public String call() { return value; }
1360      }
1361  
1362      public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) {
# Line 1122 | Line 1369 | public class JSR166TestCase extends Test
1369              }};
1370      }
1371  
1372 <    public Runnable awaiter(final CountDownLatch latch) {
1372 >    public Runnable countDowner(final CountDownLatch latch) {
1373          return new CheckedRunnable() {
1374              public void realRun() throws InterruptedException {
1375 <                await(latch);
1375 >                latch.countDown();
1376              }};
1377      }
1378  
1379 +    class LatchAwaiter extends CheckedRunnable {
1380 +        static final int NEW = 0;
1381 +        static final int RUNNING = 1;
1382 +        static final int DONE = 2;
1383 +        final CountDownLatch latch;
1384 +        int state = NEW;
1385 +        LatchAwaiter(CountDownLatch latch) { this.latch = latch; }
1386 +        public void realRun() throws InterruptedException {
1387 +            state = 1;
1388 +            await(latch);
1389 +            state = 2;
1390 +        }
1391 +    }
1392 +
1393 +    public LatchAwaiter awaiter(CountDownLatch latch) {
1394 +        return new LatchAwaiter(latch);
1395 +    }
1396 +
1397      public void await(CountDownLatch latch) {
1398          try {
1399              assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS));
1400 <        } catch (Throwable t) {
1401 <            threadUnexpectedException(t);
1400 >        } catch (Throwable fail) {
1401 >            threadUnexpectedException(fail);
1402          }
1403      }
1404  
1405      public void await(Semaphore semaphore) {
1406          try {
1407              assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS));
1408 <        } catch (Throwable t) {
1409 <            threadUnexpectedException(t);
1408 >        } catch (Throwable fail) {
1409 >            threadUnexpectedException(fail);
1410          }
1411      }
1412  
# Line 1335 | Line 1600 | public class JSR166TestCase extends Test
1600          @Override protected final void compute() {
1601              try {
1602                  realCompute();
1603 <            } catch (Throwable t) {
1604 <                threadUnexpectedException(t);
1603 >            } catch (Throwable fail) {
1604 >                threadUnexpectedException(fail);
1605              }
1606          }
1607      }
# Line 1350 | Line 1615 | public class JSR166TestCase extends Test
1615          @Override protected final T compute() {
1616              try {
1617                  return realCompute();
1618 <            } catch (Throwable t) {
1619 <                threadUnexpectedException(t);
1618 >            } catch (Throwable fail) {
1619 >                threadUnexpectedException(fail);
1620                  return null;
1621              }
1622          }
# Line 1375 | Line 1640 | public class JSR166TestCase extends Test
1640          public int await() {
1641              try {
1642                  return super.await(2 * LONG_DELAY_MS, MILLISECONDS);
1643 <            } catch (TimeoutException e) {
1643 >            } catch (TimeoutException timedOut) {
1644                  throw new AssertionFailedError("timed out");
1645 <            } catch (Exception e) {
1645 >            } catch (Exception fail) {
1646                  AssertionFailedError afe =
1647 <                    new AssertionFailedError("Unexpected exception: " + e);
1648 <                afe.initCause(e);
1647 >                    new AssertionFailedError("Unexpected exception: " + fail);
1648 >                afe.initCause(fail);
1649                  throw afe;
1650              }
1651          }
# Line 1408 | Line 1673 | public class JSR166TestCase extends Test
1673                  q.remove();
1674                  shouldThrow();
1675              } catch (NoSuchElementException success) {}
1676 <        } catch (InterruptedException ie) {
1412 <            threadUnexpectedException(ie);
1413 <        }
1676 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
1677      }
1678  
1679      void assertSerialEquals(Object x, Object y) {
# Line 1429 | Line 1692 | public class JSR166TestCase extends Test
1692              oos.flush();
1693              oos.close();
1694              return bos.toByteArray();
1695 <        } catch (Throwable t) {
1696 <            threadUnexpectedException(t);
1695 >        } catch (Throwable fail) {
1696 >            threadUnexpectedException(fail);
1697              return new byte[0];
1698          }
1699      }
# Line 1443 | Line 1706 | public class JSR166TestCase extends Test
1706              T clone = (T) ois.readObject();
1707              assertSame(o.getClass(), clone.getClass());
1708              return clone;
1709 <        } catch (Throwable t) {
1710 <            threadUnexpectedException(t);
1709 >        } catch (Throwable fail) {
1710 >            threadUnexpectedException(fail);
1711              return null;
1712          }
1713      }
# Line 1476 | Line 1739 | public class JSR166TestCase extends Test
1739              shouldThrow();
1740          } catch (NoSuchElementException success) {}
1741          assertFalse(it.hasNext());
1742 <    }        
1742 >    }
1743   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines