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.142 by jsr166, Tue Sep 8 16:53:43 2015 UTC vs.
Revision 1.181 by jsr166, Mon Nov 9 06:06:54 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 40 | Line 44 | import java.util.concurrent.CyclicBarrie
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 49 | 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;
# Line 67 | 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 89 | 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
110 < * isolation.</li>
115 > * cover multiple methods when they cannot be tested in isolation.
116   *
117 < * <li> The documentation style for testcases is to provide as javadoc
117 > * <li>The documentation style for testcases is to provide as javadoc
118   * a simple sentence or two describing the property that the testcase
119   * method purports to test. The javadocs do not say anything about how
120 < * the property is tested. To find out, read the code.</li>
120 > * the property is tested. To find out, read the code.
121   *
122 < * <li> These tests are "conformance tests", and do not attempt to
122 > * <li>These tests are "conformance tests", and do not attempt to
123   * test throughput, latency, scalability or other performance factors
124   * (see the separate "jtreg" tests for a set intended to check these
125   * for the most central aspects of functionality.) So, most tests use
126   * the smallest sensible numbers of threads, collection sizes, etc
127 < * needed to check basic conformance.</li>
127 > * needed to check basic conformance.
128   *
129   * <li>The test classes currently do not declare inclusion in
130   * any particular package to simplify things for people integrating
131 < * them in TCK test suites.</li>
131 > * them in TCK test suites.
132   *
133 < * <li> As a convenience, the {@code main} of this class (JSR166TestCase)
134 < * runs all JSR166 unit tests.</li>
133 > * <li>As a convenience, the {@code main} of this class (JSR166TestCase)
134 > * runs all JSR166 unit tests.
135   *
136   * </ul>
137   */
# Line 170 | Line 175 | public class JSR166TestCase extends Test
175      private static final int suiteRuns =
176          Integer.getInteger("jsr166.suiteRuns", 1);
177  
178 +    /**
179 +     * The scaling factor to apply to standard delays used in tests.
180 +     */
181 +    private static final int delayFactor =
182 +        Integer.getInteger("jsr166.delay.factor", 1);
183 +
184      public JSR166TestCase() { super(); }
185      public JSR166TestCase(String name) { super(name); }
186  
# Line 185 | Line 196 | public class JSR166TestCase extends Test
196          return (regex == null) ? null : Pattern.compile(regex);
197      }
198  
199 <    protected void runTest() throws Throwable {
199 >    // Instrumentation to debug very rare, but very annoying hung test runs.
200 >    static volatile TestCase currentTestCase;
201 >    // static volatile int currentRun = 0;
202 >    static {
203 >        Runnable checkForWedgedTest = new Runnable() { public void run() {
204 >            // Avoid spurious reports with enormous runsPerTest.
205 >            // A single test case run should never take more than 1 second.
206 >            // But let's cap it at the high end too ...
207 >            final int timeoutMinutes =
208 >                Math.min(15, Math.max(runsPerTest / 60, 1));
209 >            for (TestCase lastTestCase = currentTestCase;;) {
210 >                try { MINUTES.sleep(timeoutMinutes); }
211 >                catch (InterruptedException unexpected) { break; }
212 >                if (lastTestCase == currentTestCase) {
213 >                    System.err.printf(
214 >                        "Looks like we're stuck running test: %s%n",
215 >                        lastTestCase);
216 > //                     System.err.printf(
217 > //                         "Looks like we're stuck running test: %s (%d/%d)%n",
218 > //                         lastTestCase, currentRun, runsPerTest);
219 > //                     System.err.println("availableProcessors=" +
220 > //                         Runtime.getRuntime().availableProcessors());
221 > //                     System.err.printf("cpu model = %s%n", cpuModel());
222 >                    dumpTestThreads();
223 >                    // one stack dump is probably enough; more would be spam
224 >                    break;
225 >                }
226 >                lastTestCase = currentTestCase;
227 >            }}};
228 >        Thread thread = new Thread(checkForWedgedTest, "checkForWedgedTest");
229 >        thread.setDaemon(true);
230 >        thread.start();
231 >    }
232 >
233 > //     public static String cpuModel() {
234 > //         try {
235 > //             Matcher matcher = Pattern.compile("model name\\s*: (.*)")
236 > //                 .matcher(new String(
237 > //                      Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8"));
238 > //             matcher.find();
239 > //             return matcher.group(1);
240 > //         } catch (Exception ex) { return null; }
241 > //     }
242 >
243 >    public void runBare() throws Throwable {
244 >        currentTestCase = this;
245          if (methodFilter == null
246 <            || methodFilter.matcher(toString()).find()) {
247 <            for (int i = 0; i < runsPerTest; i++) {
248 <                if (profileTests)
249 <                    runTestProfiled();
250 <                else
251 <                    super.runTest();
252 <            }
246 >            || methodFilter.matcher(toString()).find())
247 >            super.runBare();
248 >    }
249 >
250 >    protected void runTest() throws Throwable {
251 >        for (int i = 0; i < runsPerTest; i++) {
252 >            // currentRun = i;
253 >            if (profileTests)
254 >                runTestProfiled();
255 >            else
256 >                super.runTest();
257          }
258      }
259  
260      protected void runTestProfiled() throws Throwable {
261 <        // Warmup run, notably to trigger all needed classloading.
262 <        super.runTest();
203 <        long t0 = System.nanoTime();
204 <        try {
261 >        for (int i = 0; i < 2; i++) {
262 >            long startTime = System.nanoTime();
263              super.runTest();
264 <        } finally {
265 <            long elapsedMillis = millisElapsedSince(t0);
266 <            if (elapsedMillis >= profileThreshold)
264 >            long elapsedMillis = millisElapsedSince(startTime);
265 >            if (elapsedMillis < profileThreshold)
266 >                break;
267 >            // Never report first run of any test; treat it as a
268 >            // warmup run, notably to trigger all needed classloading,
269 >            if (i > 0)
270                  System.out.printf("%n%s: %d%n", toString(), elapsedMillis);
271          }
272      }
# Line 217 | Line 278 | public class JSR166TestCase extends Test
278          main(suite(), args);
279      }
280  
281 +    static class PithyResultPrinter extends junit.textui.ResultPrinter {
282 +        PithyResultPrinter(java.io.PrintStream writer) { super(writer); }
283 +        long runTime;
284 +        public void startTest(Test test) {}
285 +        protected void printHeader(long runTime) {
286 +            this.runTime = runTime; // defer printing for later
287 +        }
288 +        protected void printFooter(TestResult result) {
289 +            if (result.wasSuccessful()) {
290 +                getWriter().println("OK (" + result.runCount() + " tests)"
291 +                    + "  Time: " + elapsedTimeAsString(runTime));
292 +            } else {
293 +                getWriter().println("Time: " + elapsedTimeAsString(runTime));
294 +                super.printFooter(result);
295 +            }
296 +        }
297 +    }
298 +
299 +    /**
300 +     * Returns a TestRunner that doesn't bother with unnecessary
301 +     * fluff, like printing a "." for each test case.
302 +     */
303 +    static junit.textui.TestRunner newPithyTestRunner() {
304 +        junit.textui.TestRunner runner = new junit.textui.TestRunner();
305 +        runner.setPrinter(new PithyResultPrinter(System.out));
306 +        return runner;
307 +    }
308 +
309      /**
310       * Runs all unit tests in the given test suite.
311       * Actual behavior influenced by jsr166.* system properties.
# Line 228 | Line 317 | public class JSR166TestCase extends Test
317              System.setSecurityManager(new SecurityManager());
318          }
319          for (int i = 0; i < suiteRuns; i++) {
320 <            TestResult result = junit.textui.TestRunner.run(suite);
320 >            TestResult result = newPithyTestRunner().doRun(suite);
321              if (!result.wasSuccessful())
322                  System.exit(1);
323              System.gc();
# Line 455 | Line 544 | public class JSR166TestCase extends Test
544          } else {
545              return new TestSuite();
546          }
458
547      }
548  
549      // Delays for timing-dependent tests, in milliseconds.
# Line 466 | Line 554 | public class JSR166TestCase extends Test
554      public static long LONG_DELAY_MS;
555  
556      /**
557 <     * Returns the shortest timed delay. This could
558 <     * be reimplemented to use for example a Property.
557 >     * Returns the shortest timed delay. This can be scaled up for
558 >     * slow machines using the jsr166.delay.factor system property.
559       */
560      protected long getShortDelay() {
561 <        return 50;
561 >        return 50 * delayFactor;
562      }
563  
564      /**
# Line 513 | Line 601 | public class JSR166TestCase extends Test
601       * the same test have no effect.
602       */
603      public void threadRecordFailure(Throwable t) {
604 +        System.err.println(t);
605 +        dumpTestThreads();
606          threadFailure.compareAndSet(null, t);
607      }
608  
# Line 520 | Line 610 | public class JSR166TestCase extends Test
610          setDelays();
611      }
612  
613 +    void tearDownFail(String format, Object... args) {
614 +        String msg = toString() + ": " + String.format(format, args);
615 +        System.err.println(msg);
616 +        dumpTestThreads();
617 +        throw new AssertionFailedError(msg);
618 +    }
619 +
620      /**
621       * Extra checks that get done for all test cases.
622       *
# Line 547 | Line 644 | public class JSR166TestCase extends Test
644          }
645  
646          if (Thread.interrupted())
647 <            throw new AssertionFailedError("interrupt status set in main thread");
647 >            tearDownFail("interrupt status set in main thread");
648  
649          checkForkJoinPoolThreadLeaks();
650      }
651  
652      /**
653 <     * Finds missing try { ... } finally { joinPool(e); }
653 >     * Finds missing PoolCleaners
654       */
655      void checkForkJoinPoolThreadLeaks() throws InterruptedException {
656 <        Thread[] survivors = new Thread[5];
656 >        Thread[] survivors = new Thread[7];
657          int count = Thread.enumerate(survivors);
658          for (int i = 0; i < count; i++) {
659              Thread thread = survivors[i];
# Line 564 | Line 661 | public class JSR166TestCase extends Test
661              if (name.startsWith("ForkJoinPool-")) {
662                  // give thread some time to terminate
663                  thread.join(LONG_DELAY_MS);
664 <                if (!thread.isAlive()) continue;
665 <                throw new AssertionFailedError
666 <                    (String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n",
570 <                                   toString(), name));
664 >                if (thread.isAlive())
665 >                    tearDownFail("Found leaked ForkJoinPool thread thread=%s",
666 >                                 thread);
667              }
668          }
669 +
670 +        if (!ForkJoinPool.commonPool()
671 +            .awaitQuiescence(LONG_DELAY_MS, MILLISECONDS))
672 +            tearDownFail("ForkJoin common pool thread stuck");
673      }
674  
675      /**
# Line 582 | Line 682 | public class JSR166TestCase extends Test
682              fail(reason);
683          } catch (AssertionFailedError t) {
684              threadRecordFailure(t);
685 <            fail(reason);
685 >            throw t;
686          }
687      }
688  
# Line 709 | Line 809 | public class JSR166TestCase extends Test
809      /**
810       * Delays, via Thread.sleep, for the given millisecond delay, but
811       * if the sleep is shorter than specified, may re-sleep or yield
812 <     * until time elapses.
812 >     * until time elapses.  Ensures that the given time, as measured
813 >     * by System.nanoTime(), has elapsed.
814       */
815      static void delay(long millis) throws InterruptedException {
816 <        long startTime = System.nanoTime();
817 <        long ns = millis * 1000 * 1000;
818 <        for (;;) {
816 >        long nanos = millis * (1000 * 1000);
817 >        final long wakeupTime = System.nanoTime() + nanos;
818 >        do {
819              if (millis > 0L)
820                  Thread.sleep(millis);
821              else // too short to sleep
822                  Thread.yield();
823 <            long d = ns - (System.nanoTime() - startTime);
824 <            if (d > 0L)
825 <                millis = d / (1000 * 1000);
826 <            else
827 <                break;
823 >            nanos = wakeupTime - System.nanoTime();
824 >            millis = nanos / (1000 * 1000);
825 >        } while (nanos >= 0L);
826 >    }
827 >
828 >    /**
829 >     * Allows use of try-with-resources with per-test thread pools.
830 >     */
831 >    class PoolCleaner implements AutoCloseable {
832 >        private final ExecutorService pool;
833 >        public PoolCleaner(ExecutorService pool) { this.pool = pool; }
834 >        public void close() { joinPool(pool); }
835 >    }
836 >
837 >    /**
838 >     * An extension of PoolCleaner that has an action to release the pool.
839 >     */
840 >    class PoolCleanerWithReleaser extends PoolCleaner {
841 >        private final Runnable releaser;
842 >        public PoolCleanerWithReleaser(ExecutorService pool, Runnable releaser) {
843 >            super(pool);
844 >            this.releaser = releaser;
845 >        }
846 >        public void close() {
847 >            try {
848 >                releaser.run();
849 >            } finally {
850 >                super.close();
851 >            }
852          }
853      }
854  
855 +    PoolCleaner cleaner(ExecutorService pool) {
856 +        return new PoolCleaner(pool);
857 +    }
858 +
859 +    PoolCleaner cleaner(ExecutorService pool, Runnable releaser) {
860 +        return new PoolCleanerWithReleaser(pool, releaser);
861 +    }
862 +
863 +    PoolCleaner cleaner(ExecutorService pool, CountDownLatch latch) {
864 +        return new PoolCleanerWithReleaser(pool, releaser(latch));
865 +    }
866 +
867 +    Runnable releaser(final CountDownLatch latch) {
868 +        return new Runnable() { public void run() {
869 +            do { latch.countDown(); }
870 +            while (latch.getCount() > 0);
871 +        }};
872 +    }
873 +
874      /**
875       * Waits out termination of a thread pool or fails doing so.
876       */
877      void joinPool(ExecutorService pool) {
878          try {
879              pool.shutdown();
880 <            if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS))
881 <                fail("ExecutorService " + pool +
882 <                     " did not terminate in a timely manner");
880 >            if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) {
881 >                try {
882 >                    threadFail("ExecutorService " + pool +
883 >                               " did not terminate in a timely manner");
884 >                } finally {
885 >                    // last resort, for the benefit of subsequent tests
886 >                    pool.shutdownNow();
887 >                    pool.awaitTermination(MEDIUM_DELAY_MS, MILLISECONDS);
888 >                }
889 >            }
890          } catch (SecurityException ok) {
891              // Allowed in case test doesn't have privs
892          } catch (InterruptedException fail) {
893 <            fail("Unexpected InterruptedException");
893 >            threadFail("Unexpected InterruptedException");
894          }
895      }
896  
# Line 753 | Line 904 | public class JSR166TestCase extends Test
904       */
905      void testInParallel(Action ... actions) {
906          ExecutorService pool = Executors.newCachedThreadPool();
907 <        try {
907 >        try (PoolCleaner cleaner = cleaner(pool)) {
908              ArrayList<Future<?>> futures = new ArrayList<>(actions.length);
909              for (final Action action : actions)
910                  futures.add(pool.submit(new CheckedRunnable() {
# Line 766 | Line 917 | public class JSR166TestCase extends Test
917                  } catch (Exception ex) {
918                      threadUnexpectedException(ex);
919                  }
769        } finally {
770            joinPool(pool);
920          }
921      }
922  
923      /**
924 <     * A debugging tool to print all stack traces, as jstack does.
924 >     * A debugging tool to print stack traces of most threads, as jstack does.
925 >     * Uninteresting threads are filtered out.
926       */
927 <    static void printAllStackTraces() {
928 <        for (ThreadInfo info :
929 <                 ManagementFactory.getThreadMXBean()
930 <                 .dumpAllThreads(true, true))
927 >    static void dumpTestThreads() {
928 >        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
929 >        System.err.println("------ stacktrace dump start ------");
930 >        for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) {
931 >            String name = info.getThreadName();
932 >            if ("Signal Dispatcher".equals(name))
933 >                continue;
934 >            if ("Reference Handler".equals(name)
935 >                && info.getLockName().startsWith("java.lang.ref.Reference$Lock"))
936 >                continue;
937 >            if ("Finalizer".equals(name)
938 >                && info.getLockName().startsWith("java.lang.ref.ReferenceQueue$Lock"))
939 >                continue;
940 >            if ("checkForWedgedTest".equals(name))
941 >                continue;
942              System.err.print(info);
943 +        }
944 +        System.err.println("------ stacktrace dump end ------");
945      }
946  
947      /**
# Line 798 | Line 961 | public class JSR166TestCase extends Test
961              delay(millis);
962              assertTrue(thread.isAlive());
963          } catch (InterruptedException fail) {
964 <            fail("Unexpected InterruptedException");
964 >            threadFail("Unexpected InterruptedException");
965          }
966      }
967  
# Line 820 | Line 983 | public class JSR166TestCase extends Test
983              for (Thread thread : threads)
984                  assertTrue(thread.isAlive());
985          } catch (InterruptedException fail) {
986 <            fail("Unexpected InterruptedException");
986 >            threadFail("Unexpected InterruptedException");
987          }
988      }
989  
# Line 1098 | Line 1261 | public class JSR166TestCase extends Test
1261          } finally {
1262              if (t.getState() != Thread.State.TERMINATED) {
1263                  t.interrupt();
1264 <                fail("Test timed out");
1264 >                threadFail("timed out waiting for thread to terminate");
1265              }
1266          }
1267      }
# Line 1223 | Line 1386 | public class JSR166TestCase extends Test
1386      public static final String TEST_STRING = "a test string";
1387  
1388      public static class StringTask implements Callable<String> {
1389 <        public String call() { return TEST_STRING; }
1389 >        final String value;
1390 >        public StringTask() { this(TEST_STRING); }
1391 >        public StringTask(String value) { this.value = value; }
1392 >        public String call() { return value; }
1393      }
1394  
1395      public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) {
# Line 1236 | Line 1402 | public class JSR166TestCase extends Test
1402              }};
1403      }
1404  
1405 <    public Runnable awaiter(final CountDownLatch latch) {
1405 >    public Runnable countDowner(final CountDownLatch latch) {
1406          return new CheckedRunnable() {
1407              public void realRun() throws InterruptedException {
1408 <                await(latch);
1408 >                latch.countDown();
1409              }};
1410      }
1411  
1412 +    class LatchAwaiter extends CheckedRunnable {
1413 +        static final int NEW = 0;
1414 +        static final int RUNNING = 1;
1415 +        static final int DONE = 2;
1416 +        final CountDownLatch latch;
1417 +        int state = NEW;
1418 +        LatchAwaiter(CountDownLatch latch) { this.latch = latch; }
1419 +        public void realRun() throws InterruptedException {
1420 +            state = 1;
1421 +            await(latch);
1422 +            state = 2;
1423 +        }
1424 +    }
1425 +
1426 +    public LatchAwaiter awaiter(CountDownLatch latch) {
1427 +        return new LatchAwaiter(latch);
1428 +    }
1429 +
1430      public void await(CountDownLatch latch) {
1431          try {
1432 <            assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS));
1432 >            if (!latch.await(LONG_DELAY_MS, MILLISECONDS))
1433 >                fail("timed out waiting for CountDownLatch for "
1434 >                     + (LONG_DELAY_MS/1000) + " sec");
1435          } catch (Throwable fail) {
1436              threadUnexpectedException(fail);
1437          }
# Line 1253 | Line 1439 | public class JSR166TestCase extends Test
1439  
1440      public void await(Semaphore semaphore) {
1441          try {
1442 <            assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS));
1442 >            if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS))
1443 >                fail("timed out waiting for Semaphore for "
1444 >                     + (LONG_DELAY_MS/1000) + " sec");
1445          } catch (Throwable fail) {
1446              threadUnexpectedException(fail);
1447          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines