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.150 by jsr166, Sat Oct 3 19:08:13 2015 UTC vs.
Revision 1.198 by jsr166, Wed Jul 27 17:16:23 2016 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 + /*
10 + * @test
11 + * @summary JSR-166 tck tests
12 + * @modules java.management
13 + * @build *
14 + * @run junit/othervm/timeout=1000 -Djsr166.testImplementationDetails=true JSR166TestCase
15 + * @run junit/othervm/timeout=1000 -Djava.util.concurrent.ForkJoinPool.common.parallelism=0 -Djsr166.testImplementationDetails=true JSR166TestCase
16 + * @run junit/othervm/timeout=1000 -Djava.util.concurrent.ForkJoinPool.common.parallelism=1 -Djava.util.secureRandomSeed=true JSR166TestCase
17 + */
18 +
19   import static java.util.concurrent.TimeUnit.MILLISECONDS;
20 + import static java.util.concurrent.TimeUnit.MINUTES;
21   import static java.util.concurrent.TimeUnit.NANOSECONDS;
22  
23   import java.io.ByteArrayInputStream;
# Line 19 | Line 30 | import java.lang.management.ThreadMXBean
30   import java.lang.reflect.Constructor;
31   import java.lang.reflect.Method;
32   import java.lang.reflect.Modifier;
33 + import java.nio.file.Files;
34 + import java.nio.file.Paths;
35   import java.security.CodeSource;
36   import java.security.Permission;
37   import java.security.PermissionCollection;
# Line 47 | Line 60 | import java.util.concurrent.RecursiveAct
60   import java.util.concurrent.RecursiveTask;
61   import java.util.concurrent.RejectedExecutionHandler;
62   import java.util.concurrent.Semaphore;
63 + import java.util.concurrent.SynchronousQueue;
64   import java.util.concurrent.ThreadFactory;
65   import java.util.concurrent.ThreadPoolExecutor;
66   import java.util.concurrent.TimeoutException;
67 + import java.util.concurrent.atomic.AtomicBoolean;
68   import java.util.concurrent.atomic.AtomicReference;
69 + import java.util.regex.Matcher;
70   import java.util.regex.Pattern;
71  
72   import junit.framework.AssertionFailedError;
# Line 108 | Line 124 | import junit.framework.TestSuite;
124   * methods as there are exceptions the method can throw. Sometimes
125   * there are multiple tests per JSR166 method when the different
126   * "normal" behaviors differ significantly. And sometimes testcases
127 < * cover multiple methods when they cannot be tested in
112 < * isolation.
127 > * cover multiple methods when they cannot be tested in isolation.
128   *
129   * <li>The documentation style for testcases is to provide as javadoc
130   * a simple sentence or two describing the property that the testcase
# Line 172 | Line 187 | public class JSR166TestCase extends Test
187      private static final int suiteRuns =
188          Integer.getInteger("jsr166.suiteRuns", 1);
189  
190 +    /**
191 +     * Returns the value of the system property, or NaN if not defined.
192 +     */
193 +    private static float systemPropertyValue(String name) {
194 +        String floatString = System.getProperty(name);
195 +        if (floatString == null)
196 +            return Float.NaN;
197 +        try {
198 +            return Float.parseFloat(floatString);
199 +        } catch (NumberFormatException ex) {
200 +            throw new IllegalArgumentException(
201 +                String.format("Bad float value in system property %s=%s",
202 +                              name, floatString));
203 +        }
204 +    }
205 +
206 +    /**
207 +     * The scaling factor to apply to standard delays used in tests.
208 +     * May be initialized from any of:
209 +     * - the "jsr166.delay.factor" system property
210 +     * - the "test.timeout.factor" system property (as used by jtreg)
211 +     *   See: http://openjdk.java.net/jtreg/tag-spec.html
212 +     * - hard-coded fuzz factor when using a known slowpoke VM
213 +     */
214 +    private static final float delayFactor = delayFactor();
215 +
216 +    private static float delayFactor() {
217 +        float x;
218 +        if (!Float.isNaN(x = systemPropertyValue("jsr166.delay.factor")))
219 +            return x;
220 +        if (!Float.isNaN(x = systemPropertyValue("test.timeout.factor")))
221 +            return x;
222 +        String prop = System.getProperty("java.vm.version");
223 +        if (prop != null && prop.matches(".*debug.*"))
224 +            return 4.0f; // How much slower is fastdebug than product?!
225 +        return 1.0f;
226 +    }
227 +
228      public JSR166TestCase() { super(); }
229      public JSR166TestCase(String name) { super(name); }
230  
# Line 187 | Line 240 | public class JSR166TestCase extends Test
240          return (regex == null) ? null : Pattern.compile(regex);
241      }
242  
243 +    // Instrumentation to debug very rare, but very annoying hung test runs.
244 +    static volatile TestCase currentTestCase;
245 +    // static volatile int currentRun = 0;
246 +    static {
247 +        Runnable checkForWedgedTest = new Runnable() { public void run() {
248 +            // Avoid spurious reports with enormous runsPerTest.
249 +            // A single test case run should never take more than 1 second.
250 +            // But let's cap it at the high end too ...
251 +            final int timeoutMinutes =
252 +                Math.min(15, Math.max(runsPerTest / 60, 1));
253 +            for (TestCase lastTestCase = currentTestCase;;) {
254 +                try { MINUTES.sleep(timeoutMinutes); }
255 +                catch (InterruptedException unexpected) { break; }
256 +                if (lastTestCase == currentTestCase) {
257 +                    System.err.printf(
258 +                        "Looks like we're stuck running test: %s%n",
259 +                        lastTestCase);
260 + //                     System.err.printf(
261 + //                         "Looks like we're stuck running test: %s (%d/%d)%n",
262 + //                         lastTestCase, currentRun, runsPerTest);
263 + //                     System.err.println("availableProcessors=" +
264 + //                         Runtime.getRuntime().availableProcessors());
265 + //                     System.err.printf("cpu model = %s%n", cpuModel());
266 +                    dumpTestThreads();
267 +                    // one stack dump is probably enough; more would be spam
268 +                    break;
269 +                }
270 +                lastTestCase = currentTestCase;
271 +            }}};
272 +        Thread thread = new Thread(checkForWedgedTest, "checkForWedgedTest");
273 +        thread.setDaemon(true);
274 +        thread.start();
275 +    }
276 +
277 + //     public static String cpuModel() {
278 + //         try {
279 + //             Matcher matcher = Pattern.compile("model name\\s*: (.*)")
280 + //                 .matcher(new String(
281 + //                      Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8"));
282 + //             matcher.find();
283 + //             return matcher.group(1);
284 + //         } catch (Exception ex) { return null; }
285 + //     }
286 +
287      public void runBare() throws Throwable {
288 +        currentTestCase = this;
289          if (methodFilter == null
290              || methodFilter.matcher(toString()).find())
291              super.runBare();
# Line 195 | Line 293 | public class JSR166TestCase extends Test
293  
294      protected void runTest() throws Throwable {
295          for (int i = 0; i < runsPerTest; i++) {
296 +            // currentRun = i;
297              if (profileTests)
298                  runTestProfiled();
299              else
# Line 223 | Line 322 | public class JSR166TestCase extends Test
322          main(suite(), args);
323      }
324  
325 +    static class PithyResultPrinter extends junit.textui.ResultPrinter {
326 +        PithyResultPrinter(java.io.PrintStream writer) { super(writer); }
327 +        long runTime;
328 +        public void startTest(Test test) {}
329 +        protected void printHeader(long runTime) {
330 +            this.runTime = runTime; // defer printing for later
331 +        }
332 +        protected void printFooter(TestResult result) {
333 +            if (result.wasSuccessful()) {
334 +                getWriter().println("OK (" + result.runCount() + " tests)"
335 +                    + "  Time: " + elapsedTimeAsString(runTime));
336 +            } else {
337 +                getWriter().println("Time: " + elapsedTimeAsString(runTime));
338 +                super.printFooter(result);
339 +            }
340 +        }
341 +    }
342 +
343 +    /**
344 +     * Returns a TestRunner that doesn't bother with unnecessary
345 +     * fluff, like printing a "." for each test case.
346 +     */
347 +    static junit.textui.TestRunner newPithyTestRunner() {
348 +        junit.textui.TestRunner runner = new junit.textui.TestRunner();
349 +        runner.setPrinter(new PithyResultPrinter(System.out));
350 +        return runner;
351 +    }
352 +
353      /**
354       * Runs all unit tests in the given test suite.
355       * Actual behavior influenced by jsr166.* system properties.
# Line 234 | Line 361 | public class JSR166TestCase extends Test
361              System.setSecurityManager(new SecurityManager());
362          }
363          for (int i = 0; i < suiteRuns; i++) {
364 <            TestResult result = junit.textui.TestRunner.run(suite);
364 >            TestResult result = newPithyTestRunner().doRun(suite);
365              if (!result.wasSuccessful())
366                  System.exit(1);
367              System.gc();
# Line 387 | Line 514 | public class JSR166TestCase extends Test
514                  "StampedLockTest",
515                  "SubmissionPublisherTest",
516                  "ThreadLocalRandom8Test",
517 +                "TimeUnit8Test",
518              };
519              addNamedTestClasses(suite, java8TestClassNames);
520          }
# Line 394 | Line 522 | public class JSR166TestCase extends Test
522          // Java9+ test classes
523          if (atLeastJava9()) {
524              String[] java9TestClassNames = {
525 <                // Currently empty, but expecting varhandle tests
525 >                "AtomicBoolean9Test",
526 >                "AtomicInteger9Test",
527 >                "AtomicIntegerArray9Test",
528 >                "AtomicLong9Test",
529 >                "AtomicLongArray9Test",
530 >                "AtomicReference9Test",
531 >                "AtomicReferenceArray9Test",
532 >                "ExecutorCompletionService9Test",
533              };
534              addNamedTestClasses(suite, java9TestClassNames);
535          }
# Line 461 | Line 596 | public class JSR166TestCase extends Test
596          } else {
597              return new TestSuite();
598          }
464
599      }
600  
601      // Delays for timing-dependent tests, in milliseconds.
# Line 472 | Line 606 | public class JSR166TestCase extends Test
606      public static long LONG_DELAY_MS;
607  
608      /**
609 <     * Returns the shortest timed delay. This could
610 <     * be reimplemented to use for example a Property.
609 >     * Returns the shortest timed delay. This can be scaled up for
610 >     * slow machines using the jsr166.delay.factor system property,
611 >     * or via jtreg's -timeoutFactor: flag.
612 >     * http://openjdk.java.net/jtreg/command-help.html
613       */
614      protected long getShortDelay() {
615 <        return 50;
615 >        return (long) (50 * delayFactor);
616      }
617  
618      /**
# Line 519 | Line 655 | public class JSR166TestCase extends Test
655       * the same test have no effect.
656       */
657      public void threadRecordFailure(Throwable t) {
658 +        System.err.println(t);
659 +        dumpTestThreads();
660          threadFailure.compareAndSet(null, t);
661      }
662  
# Line 529 | Line 667 | public class JSR166TestCase extends Test
667      void tearDownFail(String format, Object... args) {
668          String msg = toString() + ": " + String.format(format, args);
669          System.err.println(msg);
670 <        printAllStackTraces();
670 >        dumpTestThreads();
671          throw new AssertionFailedError(msg);
672      }
673  
# Line 566 | Line 704 | public class JSR166TestCase extends Test
704      }
705  
706      /**
707 <     * Finds missing try { ... } finally { joinPool(e); }
707 >     * Finds missing PoolCleaners
708       */
709      void checkForkJoinPoolThreadLeaks() throws InterruptedException {
710          Thread[] survivors = new Thread[7];
# Line 598 | Line 736 | public class JSR166TestCase extends Test
736              fail(reason);
737          } catch (AssertionFailedError t) {
738              threadRecordFailure(t);
739 <            fail(reason);
739 >            throw t;
740          }
741      }
742  
# Line 725 | Line 863 | public class JSR166TestCase extends Test
863      /**
864       * Delays, via Thread.sleep, for the given millisecond delay, but
865       * if the sleep is shorter than specified, may re-sleep or yield
866 <     * until time elapses.
866 >     * until time elapses.  Ensures that the given time, as measured
867 >     * by System.nanoTime(), has elapsed.
868       */
869      static void delay(long millis) throws InterruptedException {
870 <        long startTime = System.nanoTime();
871 <        long ns = millis * 1000 * 1000;
872 <        for (;;) {
870 >        long nanos = millis * (1000 * 1000);
871 >        final long wakeupTime = System.nanoTime() + nanos;
872 >        do {
873              if (millis > 0L)
874                  Thread.sleep(millis);
875              else // too short to sleep
876                  Thread.yield();
877 <            long d = ns - (System.nanoTime() - startTime);
878 <            if (d > 0L)
879 <                millis = d / (1000 * 1000);
741 <            else
742 <                break;
743 <        }
877 >            nanos = wakeupTime - System.nanoTime();
878 >            millis = nanos / (1000 * 1000);
879 >        } while (nanos >= 0L);
880      }
881  
882      /**
883       * Allows use of try-with-resources with per-test thread pools.
884       */
885 <    static class PoolCloser<T extends ExecutorService>
886 <            implements AutoCloseable {
887 <        public final T pool;
752 <        public PoolCloser(T pool) { this.pool = pool; }
885 >    class PoolCleaner implements AutoCloseable {
886 >        private final ExecutorService pool;
887 >        public PoolCleaner(ExecutorService pool) { this.pool = pool; }
888          public void close() { joinPool(pool); }
889      }
890  
891      /**
892 +     * An extension of PoolCleaner that has an action to release the pool.
893 +     */
894 +    class PoolCleanerWithReleaser extends PoolCleaner {
895 +        private final Runnable releaser;
896 +        public PoolCleanerWithReleaser(ExecutorService pool, Runnable releaser) {
897 +            super(pool);
898 +            this.releaser = releaser;
899 +        }
900 +        public void close() {
901 +            try {
902 +                releaser.run();
903 +            } finally {
904 +                super.close();
905 +            }
906 +        }
907 +    }
908 +
909 +    PoolCleaner cleaner(ExecutorService pool) {
910 +        return new PoolCleaner(pool);
911 +    }
912 +
913 +    PoolCleaner cleaner(ExecutorService pool, Runnable releaser) {
914 +        return new PoolCleanerWithReleaser(pool, releaser);
915 +    }
916 +
917 +    PoolCleaner cleaner(ExecutorService pool, CountDownLatch latch) {
918 +        return new PoolCleanerWithReleaser(pool, releaser(latch));
919 +    }
920 +
921 +    Runnable releaser(final CountDownLatch latch) {
922 +        return new Runnable() { public void run() {
923 +            do { latch.countDown(); }
924 +            while (latch.getCount() > 0);
925 +        }};
926 +    }
927 +
928 +    PoolCleaner cleaner(ExecutorService pool, AtomicBoolean flag) {
929 +        return new PoolCleanerWithReleaser(pool, releaser(flag));
930 +    }
931 +
932 +    Runnable releaser(final AtomicBoolean flag) {
933 +        return new Runnable() { public void run() { flag.set(true); }};
934 +    }
935 +
936 +    /**
937       * Waits out termination of a thread pool or fails doing so.
938       */
939 <    static void joinPool(ExecutorService pool) {
939 >    void joinPool(ExecutorService pool) {
940          try {
941              pool.shutdown();
942 <            if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS))
943 <                fail("ExecutorService " + pool +
944 <                     " did not terminate in a timely manner");
942 >            if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) {
943 >                try {
944 >                    threadFail("ExecutorService " + pool +
945 >                               " did not terminate in a timely manner");
946 >                } finally {
947 >                    // last resort, for the benefit of subsequent tests
948 >                    pool.shutdownNow();
949 >                    pool.awaitTermination(MEDIUM_DELAY_MS, MILLISECONDS);
950 >                }
951 >            }
952          } catch (SecurityException ok) {
953              // Allowed in case test doesn't have privs
954          } catch (InterruptedException fail) {
955 <            fail("Unexpected InterruptedException");
955 >            threadFail("Unexpected InterruptedException");
956          }
957      }
958  
959 <    /** Like Runnable, but with the freedom to throw anything */
959 >    /**
960 >     * Like Runnable, but with the freedom to throw anything.
961 >     * junit folks had the same idea:
962 >     * http://junit.org/junit5/docs/snapshot/api/org/junit/gen5/api/Executable.html
963 >     */
964      interface Action { public void run() throws Throwable; }
965  
966      /**
# Line 779 | Line 970 | public class JSR166TestCase extends Test
970       */
971      void testInParallel(Action ... actions) {
972          ExecutorService pool = Executors.newCachedThreadPool();
973 <        try {
973 >        try (PoolCleaner cleaner = cleaner(pool)) {
974              ArrayList<Future<?>> futures = new ArrayList<>(actions.length);
975              for (final Action action : actions)
976                  futures.add(pool.submit(new CheckedRunnable() {
# Line 792 | Line 983 | public class JSR166TestCase extends Test
983                  } catch (Exception ex) {
984                      threadUnexpectedException(ex);
985                  }
795        } finally {
796            joinPool(pool);
986          }
987      }
988  
989      /**
990 <     * A debugging tool to print all stack traces, as jstack does.
990 >     * A debugging tool to print stack traces of most threads, as jstack does.
991       * Uninteresting threads are filtered out.
992       */
993 <    static void printAllStackTraces() {
993 >    static void dumpTestThreads() {
994 >        SecurityManager sm = System.getSecurityManager();
995 >        if (sm != null) {
996 >            try {
997 >                System.setSecurityManager(null);
998 >            } catch (SecurityException giveUp) {
999 >                return;
1000 >            }
1001 >        }
1002 >
1003          ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
1004          System.err.println("------ stacktrace dump start ------");
1005          for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) {
# Line 814 | Line 1012 | public class JSR166TestCase extends Test
1012              if ("Finalizer".equals(name)
1013                  && info.getLockName().startsWith("java.lang.ref.ReferenceQueue$Lock"))
1014                  continue;
1015 +            if ("checkForWedgedTest".equals(name))
1016 +                continue;
1017              System.err.print(info);
1018          }
1019          System.err.println("------ stacktrace dump end ------");
1020 +
1021 +        if (sm != null) System.setSecurityManager(sm);
1022      }
1023  
1024      /**
# Line 836 | Line 1038 | public class JSR166TestCase extends Test
1038              delay(millis);
1039              assertTrue(thread.isAlive());
1040          } catch (InterruptedException fail) {
1041 <            fail("Unexpected InterruptedException");
1041 >            threadFail("Unexpected InterruptedException");
1042          }
1043      }
1044  
# Line 858 | Line 1060 | public class JSR166TestCase extends Test
1060              for (Thread thread : threads)
1061                  assertTrue(thread.isAlive());
1062          } catch (InterruptedException fail) {
1063 <            fail("Unexpected InterruptedException");
1063 >            threadFail("Unexpected InterruptedException");
1064          }
1065      }
1066  
# Line 1136 | Line 1338 | public class JSR166TestCase extends Test
1338          } finally {
1339              if (t.getState() != Thread.State.TERMINATED) {
1340                  t.interrupt();
1341 <                fail("Test timed out");
1341 >                threadFail("timed out waiting for thread to terminate");
1342              }
1343          }
1344      }
# Line 1284 | Line 1486 | public class JSR166TestCase extends Test
1486              }};
1487      }
1488  
1489 <    public Runnable awaiter(final CountDownLatch latch) {
1490 <        return new CheckedRunnable() {
1491 <            public void realRun() throws InterruptedException {
1492 <                await(latch);
1493 <            }};
1489 >    class LatchAwaiter extends CheckedRunnable {
1490 >        static final int NEW = 0;
1491 >        static final int RUNNING = 1;
1492 >        static final int DONE = 2;
1493 >        final CountDownLatch latch;
1494 >        int state = NEW;
1495 >        LatchAwaiter(CountDownLatch latch) { this.latch = latch; }
1496 >        public void realRun() throws InterruptedException {
1497 >            state = 1;
1498 >            await(latch);
1499 >            state = 2;
1500 >        }
1501      }
1502  
1503 <    public void await(CountDownLatch latch) {
1503 >    public LatchAwaiter awaiter(CountDownLatch latch) {
1504 >        return new LatchAwaiter(latch);
1505 >    }
1506 >
1507 >    public void await(CountDownLatch latch, long timeoutMillis) {
1508          try {
1509 <            assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS));
1509 >            if (!latch.await(timeoutMillis, MILLISECONDS))
1510 >                fail("timed out waiting for CountDownLatch for "
1511 >                     + (timeoutMillis/1000) + " sec");
1512          } catch (Throwable fail) {
1513              threadUnexpectedException(fail);
1514          }
1515      }
1516  
1517 +    public void await(CountDownLatch latch) {
1518 +        await(latch, LONG_DELAY_MS);
1519 +    }
1520 +
1521      public void await(Semaphore semaphore) {
1522          try {
1523 <            assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS));
1523 >            if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS))
1524 >                fail("timed out waiting for Semaphore for "
1525 >                     + (LONG_DELAY_MS/1000) + " sec");
1526          } catch (Throwable fail) {
1527              threadUnexpectedException(fail);
1528          }
# Line 1637 | Line 1858 | public class JSR166TestCase extends Test
1858          } catch (NoSuchElementException success) {}
1859          assertFalse(it.hasNext());
1860      }
1861 +
1862 +    public <T> Callable<T> callableThrowing(final Exception ex) {
1863 +        return new Callable<T>() { public T call() throws Exception { throw ex; }};
1864 +    }
1865 +
1866 +    public Runnable runnableThrowing(final RuntimeException ex) {
1867 +        return new Runnable() { public void run() { throw ex; }};
1868 +    }
1869 +
1870 +    /** A reusable thread pool to be shared by tests. */
1871 +    static final ExecutorService cachedThreadPool =
1872 +        new ThreadPoolExecutor(0, Integer.MAX_VALUE,
1873 +                               1000L, MILLISECONDS,
1874 +                               new SynchronousQueue<Runnable>());
1875 +
1876   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines