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.152 by jsr166, Sat Oct 3 19:29:11 2015 UTC vs.
Revision 1.193 by jsr166, Mon May 23 18:19:48 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 + */
16 +
17   import static java.util.concurrent.TimeUnit.MILLISECONDS;
18 + import static java.util.concurrent.TimeUnit.MINUTES;
19   import static java.util.concurrent.TimeUnit.NANOSECONDS;
20  
21   import java.io.ByteArrayInputStream;
# Line 19 | Line 28 | import java.lang.management.ThreadMXBean
28   import java.lang.reflect.Constructor;
29   import java.lang.reflect.Method;
30   import java.lang.reflect.Modifier;
31 + import java.nio.file.Files;
32 + import java.nio.file.Paths;
33   import java.security.CodeSource;
34   import java.security.Permission;
35   import java.security.PermissionCollection;
# Line 47 | Line 58 | import java.util.concurrent.RecursiveAct
58   import java.util.concurrent.RecursiveTask;
59   import java.util.concurrent.RejectedExecutionHandler;
60   import java.util.concurrent.Semaphore;
61 + import java.util.concurrent.SynchronousQueue;
62   import java.util.concurrent.ThreadFactory;
63   import java.util.concurrent.ThreadPoolExecutor;
64   import java.util.concurrent.TimeoutException;
65 + import java.util.concurrent.atomic.AtomicBoolean;
66   import java.util.concurrent.atomic.AtomicReference;
67 + import java.util.regex.Matcher;
68   import java.util.regex.Pattern;
69  
70   import junit.framework.AssertionFailedError;
# Line 108 | Line 122 | import junit.framework.TestSuite;
122   * methods as there are exceptions the method can throw. Sometimes
123   * there are multiple tests per JSR166 method when the different
124   * "normal" behaviors differ significantly. And sometimes testcases
125 < * cover multiple methods when they cannot be tested in
112 < * isolation.
125 > * cover multiple methods when they cannot be tested in isolation.
126   *
127   * <li>The documentation style for testcases is to provide as javadoc
128   * a simple sentence or two describing the property that the testcase
# Line 172 | Line 185 | public class JSR166TestCase extends Test
185      private static final int suiteRuns =
186          Integer.getInteger("jsr166.suiteRuns", 1);
187  
188 +    /**
189 +     * Returns the value of the system property, or NaN if not defined.
190 +     */
191 +    private static float systemPropertyValue(String name) {
192 +        String floatString = System.getProperty(name);
193 +        if (floatString == null)
194 +            return Float.NaN;
195 +        try {
196 +            return Float.parseFloat(floatString);
197 +        } catch (NumberFormatException ex) {
198 +            throw new IllegalArgumentException(
199 +                String.format("Bad float value in system property %s=%s",
200 +                              name, floatString));
201 +        }
202 +    }
203 +
204 +    /**
205 +     * The scaling factor to apply to standard delays used in tests.
206 +     * May be initialized from any of:
207 +     * - the "jsr166.delay.factor" system property
208 +     * - the "test.timeout.factor" system property (as used by jtreg)
209 +     *   See: http://openjdk.java.net/jtreg/tag-spec.html
210 +     * - hard-coded fuzz factor when using a known slowpoke VM
211 +     */
212 +    private static final float delayFactor = delayFactor();
213 +
214 +    private static float delayFactor() {
215 +        float x;
216 +        if (!Float.isNaN(x = systemPropertyValue("jsr166.delay.factor")))
217 +            return x;
218 +        if (!Float.isNaN(x = systemPropertyValue("test.timeout.factor")))
219 +            return x;
220 +        String prop = System.getProperty("java.vm.version");
221 +        if (prop != null && prop.matches(".*debug.*"))
222 +            return 4.0f; // How much slower is fastdebug than product?!
223 +        return 1.0f;
224 +    }
225 +
226      public JSR166TestCase() { super(); }
227      public JSR166TestCase(String name) { super(name); }
228  
# Line 187 | Line 238 | public class JSR166TestCase extends Test
238          return (regex == null) ? null : Pattern.compile(regex);
239      }
240  
241 +    // Instrumentation to debug very rare, but very annoying hung test runs.
242 +    static volatile TestCase currentTestCase;
243 +    // static volatile int currentRun = 0;
244 +    static {
245 +        Runnable checkForWedgedTest = new Runnable() { public void run() {
246 +            // Avoid spurious reports with enormous runsPerTest.
247 +            // A single test case run should never take more than 1 second.
248 +            // But let's cap it at the high end too ...
249 +            final int timeoutMinutes =
250 +                Math.min(15, Math.max(runsPerTest / 60, 1));
251 +            for (TestCase lastTestCase = currentTestCase;;) {
252 +                try { MINUTES.sleep(timeoutMinutes); }
253 +                catch (InterruptedException unexpected) { break; }
254 +                if (lastTestCase == currentTestCase) {
255 +                    System.err.printf(
256 +                        "Looks like we're stuck running test: %s%n",
257 +                        lastTestCase);
258 + //                     System.err.printf(
259 + //                         "Looks like we're stuck running test: %s (%d/%d)%n",
260 + //                         lastTestCase, currentRun, runsPerTest);
261 + //                     System.err.println("availableProcessors=" +
262 + //                         Runtime.getRuntime().availableProcessors());
263 + //                     System.err.printf("cpu model = %s%n", cpuModel());
264 +                    dumpTestThreads();
265 +                    // one stack dump is probably enough; more would be spam
266 +                    break;
267 +                }
268 +                lastTestCase = currentTestCase;
269 +            }}};
270 +        Thread thread = new Thread(checkForWedgedTest, "checkForWedgedTest");
271 +        thread.setDaemon(true);
272 +        thread.start();
273 +    }
274 +
275 + //     public static String cpuModel() {
276 + //         try {
277 + //             Matcher matcher = Pattern.compile("model name\\s*: (.*)")
278 + //                 .matcher(new String(
279 + //                      Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8"));
280 + //             matcher.find();
281 + //             return matcher.group(1);
282 + //         } catch (Exception ex) { return null; }
283 + //     }
284 +
285      public void runBare() throws Throwable {
286 +        currentTestCase = this;
287          if (methodFilter == null
288              || methodFilter.matcher(toString()).find())
289              super.runBare();
# Line 195 | Line 291 | public class JSR166TestCase extends Test
291  
292      protected void runTest() throws Throwable {
293          for (int i = 0; i < runsPerTest; i++) {
294 +            // currentRun = i;
295              if (profileTests)
296                  runTestProfiled();
297              else
# Line 223 | Line 320 | public class JSR166TestCase extends Test
320          main(suite(), args);
321      }
322  
323 +    static class PithyResultPrinter extends junit.textui.ResultPrinter {
324 +        PithyResultPrinter(java.io.PrintStream writer) { super(writer); }
325 +        long runTime;
326 +        public void startTest(Test test) {}
327 +        protected void printHeader(long runTime) {
328 +            this.runTime = runTime; // defer printing for later
329 +        }
330 +        protected void printFooter(TestResult result) {
331 +            if (result.wasSuccessful()) {
332 +                getWriter().println("OK (" + result.runCount() + " tests)"
333 +                    + "  Time: " + elapsedTimeAsString(runTime));
334 +            } else {
335 +                getWriter().println("Time: " + elapsedTimeAsString(runTime));
336 +                super.printFooter(result);
337 +            }
338 +        }
339 +    }
340 +
341 +    /**
342 +     * Returns a TestRunner that doesn't bother with unnecessary
343 +     * fluff, like printing a "." for each test case.
344 +     */
345 +    static junit.textui.TestRunner newPithyTestRunner() {
346 +        junit.textui.TestRunner runner = new junit.textui.TestRunner();
347 +        runner.setPrinter(new PithyResultPrinter(System.out));
348 +        return runner;
349 +    }
350 +
351      /**
352       * Runs all unit tests in the given test suite.
353       * Actual behavior influenced by jsr166.* system properties.
# Line 234 | Line 359 | public class JSR166TestCase extends Test
359              System.setSecurityManager(new SecurityManager());
360          }
361          for (int i = 0; i < suiteRuns; i++) {
362 <            TestResult result = junit.textui.TestRunner.run(suite);
362 >            TestResult result = newPithyTestRunner().doRun(suite);
363              if (!result.wasSuccessful())
364                  System.exit(1);
365              System.gc();
# Line 387 | Line 512 | public class JSR166TestCase extends Test
512                  "StampedLockTest",
513                  "SubmissionPublisherTest",
514                  "ThreadLocalRandom8Test",
515 +                "TimeUnit8Test",
516              };
517              addNamedTestClasses(suite, java8TestClassNames);
518          }
# Line 394 | Line 520 | public class JSR166TestCase extends Test
520          // Java9+ test classes
521          if (atLeastJava9()) {
522              String[] java9TestClassNames = {
523 <                // Currently empty, but expecting varhandle tests
523 >                "ExecutorCompletionService9Test",
524              };
525              addNamedTestClasses(suite, java9TestClassNames);
526          }
# Line 461 | Line 587 | public class JSR166TestCase extends Test
587          } else {
588              return new TestSuite();
589          }
464
590      }
591  
592      // Delays for timing-dependent tests, in milliseconds.
# Line 472 | Line 597 | public class JSR166TestCase extends Test
597      public static long LONG_DELAY_MS;
598  
599      /**
600 <     * Returns the shortest timed delay. This could
601 <     * be reimplemented to use for example a Property.
600 >     * Returns the shortest timed delay. This can be scaled up for
601 >     * slow machines using the jsr166.delay.factor system property,
602 >     * or via jtreg's -timeoutFactor: flag.
603 >     * http://openjdk.java.net/jtreg/command-help.html
604       */
605      protected long getShortDelay() {
606 <        return 50;
606 >        return (long) (50 * delayFactor);
607      }
608  
609      /**
# Line 519 | Line 646 | public class JSR166TestCase extends Test
646       * the same test have no effect.
647       */
648      public void threadRecordFailure(Throwable t) {
649 +        System.err.println(t);
650 +        dumpTestThreads();
651          threadFailure.compareAndSet(null, t);
652      }
653  
# Line 529 | Line 658 | public class JSR166TestCase extends Test
658      void tearDownFail(String format, Object... args) {
659          String msg = toString() + ": " + String.format(format, args);
660          System.err.println(msg);
661 <        printAllStackTraces();
661 >        dumpTestThreads();
662          throw new AssertionFailedError(msg);
663      }
664  
# Line 566 | Line 695 | public class JSR166TestCase extends Test
695      }
696  
697      /**
698 <     * Finds missing try { ... } finally { joinPool(e); }
698 >     * Finds missing PoolCleaners
699       */
700      void checkForkJoinPoolThreadLeaks() throws InterruptedException {
701          Thread[] survivors = new Thread[7];
# Line 725 | Line 854 | public class JSR166TestCase extends Test
854      /**
855       * Delays, via Thread.sleep, for the given millisecond delay, but
856       * if the sleep is shorter than specified, may re-sleep or yield
857 <     * until time elapses.
857 >     * until time elapses.  Ensures that the given time, as measured
858 >     * by System.nanoTime(), has elapsed.
859       */
860      static void delay(long millis) throws InterruptedException {
861 <        long startTime = System.nanoTime();
862 <        long ns = millis * 1000 * 1000;
863 <        for (;;) {
861 >        long nanos = millis * (1000 * 1000);
862 >        final long wakeupTime = System.nanoTime() + nanos;
863 >        do {
864              if (millis > 0L)
865                  Thread.sleep(millis);
866              else // too short to sleep
867                  Thread.yield();
868 <            long d = ns - (System.nanoTime() - startTime);
869 <            if (d > 0L)
870 <                millis = d / (1000 * 1000);
741 <            else
742 <                break;
743 <        }
868 >            nanos = wakeupTime - System.nanoTime();
869 >            millis = nanos / (1000 * 1000);
870 >        } while (nanos >= 0L);
871      }
872  
873      /**
874       * Allows use of try-with-resources with per-test thread pools.
875       */
876 <    static class PoolCloser<T extends ExecutorService>
877 <            implements AutoCloseable {
878 <        public final T pool;
752 <        public PoolCloser(T pool) { this.pool = pool; }
876 >    class PoolCleaner implements AutoCloseable {
877 >        private final ExecutorService pool;
878 >        public PoolCleaner(ExecutorService pool) { this.pool = pool; }
879          public void close() { joinPool(pool); }
880      }
881  
882      /**
883 +     * An extension of PoolCleaner that has an action to release the pool.
884 +     */
885 +    class PoolCleanerWithReleaser extends PoolCleaner {
886 +        private final Runnable releaser;
887 +        public PoolCleanerWithReleaser(ExecutorService pool, Runnable releaser) {
888 +            super(pool);
889 +            this.releaser = releaser;
890 +        }
891 +        public void close() {
892 +            try {
893 +                releaser.run();
894 +            } finally {
895 +                super.close();
896 +            }
897 +        }
898 +    }
899 +
900 +    PoolCleaner cleaner(ExecutorService pool) {
901 +        return new PoolCleaner(pool);
902 +    }
903 +
904 +    PoolCleaner cleaner(ExecutorService pool, Runnable releaser) {
905 +        return new PoolCleanerWithReleaser(pool, releaser);
906 +    }
907 +
908 +    PoolCleaner cleaner(ExecutorService pool, CountDownLatch latch) {
909 +        return new PoolCleanerWithReleaser(pool, releaser(latch));
910 +    }
911 +
912 +    Runnable releaser(final CountDownLatch latch) {
913 +        return new Runnable() { public void run() {
914 +            do { latch.countDown(); }
915 +            while (latch.getCount() > 0);
916 +        }};
917 +    }
918 +
919 +    PoolCleaner cleaner(ExecutorService pool, AtomicBoolean flag) {
920 +        return new PoolCleanerWithReleaser(pool, releaser(flag));
921 +    }
922 +
923 +    Runnable releaser(final AtomicBoolean flag) {
924 +        return new Runnable() { public void run() { flag.set(true); }};
925 +    }
926 +
927 +    /**
928       * Waits out termination of a thread pool or fails doing so.
929       */
930 <    static void joinPool(ExecutorService pool) {
930 >    void joinPool(ExecutorService pool) {
931          try {
932              pool.shutdown();
933 <            if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS))
934 <                fail("ExecutorService " + pool +
935 <                     " did not terminate in a timely manner");
933 >            if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) {
934 >                try {
935 >                    threadFail("ExecutorService " + pool +
936 >                               " did not terminate in a timely manner");
937 >                } finally {
938 >                    // last resort, for the benefit of subsequent tests
939 >                    pool.shutdownNow();
940 >                    pool.awaitTermination(MEDIUM_DELAY_MS, MILLISECONDS);
941 >                }
942 >            }
943          } catch (SecurityException ok) {
944              // Allowed in case test doesn't have privs
945          } catch (InterruptedException fail) {
946 <            fail("Unexpected InterruptedException");
946 >            threadFail("Unexpected InterruptedException");
947          }
948      }
949  
# Line 778 | Line 956 | public class JSR166TestCase extends Test
956       * necessarily individually slow because they must block.
957       */
958      void testInParallel(Action ... actions) {
959 <        try (PoolCloser<ExecutorService> poolCloser
960 <             = new PoolCloser<>(Executors.newCachedThreadPool())) {
783 <            ExecutorService pool = poolCloser.pool;
959 >        ExecutorService pool = Executors.newCachedThreadPool();
960 >        try (PoolCleaner cleaner = cleaner(pool)) {
961              ArrayList<Future<?>> futures = new ArrayList<>(actions.length);
962              for (final Action action : actions)
963                  futures.add(pool.submit(new CheckedRunnable() {
# Line 797 | Line 974 | public class JSR166TestCase extends Test
974      }
975  
976      /**
977 <     * A debugging tool to print all stack traces, as jstack does.
977 >     * A debugging tool to print stack traces of most threads, as jstack does.
978       * Uninteresting threads are filtered out.
979       */
980 <    static void printAllStackTraces() {
980 >    static void dumpTestThreads() {
981          ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
982          System.err.println("------ stacktrace dump start ------");
983          for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) {
# Line 813 | Line 990 | public class JSR166TestCase extends Test
990              if ("Finalizer".equals(name)
991                  && info.getLockName().startsWith("java.lang.ref.ReferenceQueue$Lock"))
992                  continue;
993 +            if ("checkForWedgedTest".equals(name))
994 +                continue;
995              System.err.print(info);
996          }
997          System.err.println("------ stacktrace dump end ------");
# Line 835 | Line 1014 | public class JSR166TestCase extends Test
1014              delay(millis);
1015              assertTrue(thread.isAlive());
1016          } catch (InterruptedException fail) {
1017 <            fail("Unexpected InterruptedException");
1017 >            threadFail("Unexpected InterruptedException");
1018          }
1019      }
1020  
# Line 857 | Line 1036 | public class JSR166TestCase extends Test
1036              for (Thread thread : threads)
1037                  assertTrue(thread.isAlive());
1038          } catch (InterruptedException fail) {
1039 <            fail("Unexpected InterruptedException");
1039 >            threadFail("Unexpected InterruptedException");
1040          }
1041      }
1042  
# Line 1135 | Line 1314 | public class JSR166TestCase extends Test
1314          } finally {
1315              if (t.getState() != Thread.State.TERMINATED) {
1316                  t.interrupt();
1317 <                fail("Test timed out");
1317 >                threadFail("timed out waiting for thread to terminate");
1318              }
1319          }
1320      }
# Line 1283 | Line 1462 | public class JSR166TestCase extends Test
1462              }};
1463      }
1464  
1465 <    public Runnable awaiter(final CountDownLatch latch) {
1466 <        return new CheckedRunnable() {
1467 <            public void realRun() throws InterruptedException {
1468 <                await(latch);
1469 <            }};
1465 >    class LatchAwaiter extends CheckedRunnable {
1466 >        static final int NEW = 0;
1467 >        static final int RUNNING = 1;
1468 >        static final int DONE = 2;
1469 >        final CountDownLatch latch;
1470 >        int state = NEW;
1471 >        LatchAwaiter(CountDownLatch latch) { this.latch = latch; }
1472 >        public void realRun() throws InterruptedException {
1473 >            state = 1;
1474 >            await(latch);
1475 >            state = 2;
1476 >        }
1477      }
1478  
1479 <    public void await(CountDownLatch latch) {
1479 >    public LatchAwaiter awaiter(CountDownLatch latch) {
1480 >        return new LatchAwaiter(latch);
1481 >    }
1482 >
1483 >    public void await(CountDownLatch latch, long timeoutMillis) {
1484          try {
1485 <            assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS));
1485 >            if (!latch.await(timeoutMillis, MILLISECONDS))
1486 >                fail("timed out waiting for CountDownLatch for "
1487 >                     + (timeoutMillis/1000) + " sec");
1488          } catch (Throwable fail) {
1489              threadUnexpectedException(fail);
1490          }
1491      }
1492  
1493 +    public void await(CountDownLatch latch) {
1494 +        await(latch, LONG_DELAY_MS);
1495 +    }
1496 +
1497      public void await(Semaphore semaphore) {
1498          try {
1499 <            assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS));
1499 >            if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS))
1500 >                fail("timed out waiting for Semaphore for "
1501 >                     + (LONG_DELAY_MS/1000) + " sec");
1502          } catch (Throwable fail) {
1503              threadUnexpectedException(fail);
1504          }
# Line 1636 | Line 1834 | public class JSR166TestCase extends Test
1834          } catch (NoSuchElementException success) {}
1835          assertFalse(it.hasNext());
1836      }
1837 +
1838 +    public <T> Callable<T> callableThrowing(final Exception ex) {
1839 +        return new Callable<T>() { public T call() throws Exception { throw ex; }};
1840 +    }
1841 +
1842 +    public Runnable runnableThrowing(final RuntimeException ex) {
1843 +        return new Runnable() { public void run() { throw ex; }};
1844 +    }
1845 +
1846 +    /** A reusable thread pool to be shared by tests. */
1847 +    static final ExecutorService cachedThreadPool =
1848 +        new ThreadPoolExecutor(0, Integer.MAX_VALUE,
1849 +                               1000L, MILLISECONDS,
1850 +                               new SynchronousQueue<Runnable>());
1851 +
1852   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines