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.132 by jsr166, Mon Apr 27 06:01:31 2015 UTC vs.
Revision 1.146 by jsr166, Fri Sep 25 23:32:15 2015 UTC

# Line 15 | Line 15 | import java.io.ObjectInputStream;
15   import java.io.ObjectOutputStream;
16   import java.lang.management.ManagementFactory;
17   import java.lang.management.ThreadInfo;
18 + import java.lang.reflect.Constructor;
19   import java.lang.reflect.Method;
20 + import java.lang.reflect.Modifier;
21   import java.security.CodeSource;
22   import java.security.Permission;
23   import java.security.PermissionCollection;
# Line 35 | Line 37 | import java.util.concurrent.BlockingQueu
37   import java.util.concurrent.Callable;
38   import java.util.concurrent.CountDownLatch;
39   import java.util.concurrent.CyclicBarrier;
40 + import java.util.concurrent.ExecutionException;
41 + import java.util.concurrent.Executors;
42   import java.util.concurrent.ExecutorService;
43 + import java.util.concurrent.ForkJoinPool;
44   import java.util.concurrent.Future;
45   import java.util.concurrent.RecursiveAction;
46   import java.util.concurrent.RecursiveTask;
# Line 63 | Line 68 | import junit.framework.TestSuite;
68   *
69   * <ol>
70   *
71 < * <li> All assertions in code running in generated threads must use
71 > * <li>All assertions in code running in generated threads must use
72   * the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link
73   * #threadAssertEquals}, or {@link #threadAssertNull}, (not
74   * {@code fail}, {@code assertTrue}, etc.) It is OK (but not
75   * particularly recommended) for other code to use these forms too.
76   * Only the most typically used JUnit assertion methods are defined
77 < * this way, but enough to live with.</li>
77 > * this way, but enough to live with.
78   *
79 < * <li> If you override {@link #setUp} or {@link #tearDown}, make sure
79 > * <li>If you override {@link #setUp} or {@link #tearDown}, make sure
80   * to invoke {@code super.setUp} and {@code super.tearDown} within
81   * them. These methods are used to clear and check for thread
82 < * assertion failures.</li>
82 > * assertion failures.
83   *
84   * <li>All delays and timeouts must use one of the constants {@code
85   * SHORT_DELAY_MS}, {@code SMALL_DELAY_MS}, {@code MEDIUM_DELAY_MS},
# Line 85 | Line 90 | import junit.framework.TestSuite;
90   * is always discriminable as larger than SHORT and smaller than
91   * MEDIUM.  And so on. These constants are set to conservative values,
92   * but even so, if there is ever any doubt, they can all be increased
93 < * in one spot to rerun tests on slower platforms.</li>
93 > * in one spot to rerun tests on slower platforms.
94   *
95 < * <li> All threads generated must be joined inside each test case
95 > * <li>All threads generated must be joined inside each test case
96   * method (or {@code fail} to do so) before returning from the
97   * method. The {@code joinPool} method can be used to do this when
98 < * using Executors.</li>
98 > * using Executors.
99   *
100   * </ol>
101   *
102   * <p><b>Other notes</b>
103   * <ul>
104   *
105 < * <li> Usually, there is one testcase method per JSR166 method
105 > * <li>Usually, there is one testcase method per JSR166 method
106   * covering "normal" operation, and then as many exception-testing
107   * methods as there are exceptions the method can throw. Sometimes
108   * there are multiple tests per JSR166 method when the different
109   * "normal" behaviors differ significantly. And sometimes testcases
110   * cover multiple methods when they cannot be tested in
111 < * isolation.</li>
111 > * isolation.
112   *
113 < * <li> The documentation style for testcases is to provide as javadoc
113 > * <li>The documentation style for testcases is to provide as javadoc
114   * a simple sentence or two describing the property that the testcase
115   * method purports to test. The javadocs do not say anything about how
116 < * the property is tested. To find out, read the code.</li>
116 > * the property is tested. To find out, read the code.
117   *
118 < * <li> These tests are "conformance tests", and do not attempt to
118 > * <li>These tests are "conformance tests", and do not attempt to
119   * test throughput, latency, scalability or other performance factors
120   * (see the separate "jtreg" tests for a set intended to check these
121   * for the most central aspects of functionality.) So, most tests use
122   * the smallest sensible numbers of threads, collection sizes, etc
123 < * needed to check basic conformance.</li>
123 > * needed to check basic conformance.
124   *
125   * <li>The test classes currently do not declare inclusion in
126   * any particular package to simplify things for people integrating
127 < * them in TCK test suites.</li>
127 > * them in TCK test suites.
128   *
129 < * <li> As a convenience, the {@code main} of this class (JSR166TestCase)
130 < * runs all JSR166 unit tests.</li>
129 > * <li>As a convenience, the {@code main} of this class (JSR166TestCase)
130 > * runs all JSR166 unit tests.
131   *
132   * </ul>
133   */
# Line 166 | Line 171 | public class JSR166TestCase extends Test
171      private static final int suiteRuns =
172          Integer.getInteger("jsr166.suiteRuns", 1);
173  
174 +    public JSR166TestCase() { super(); }
175 +    public JSR166TestCase(String name) { super(name); }
176 +
177      /**
178       * A filter for tests to run, matching strings of the form
179       * methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)"
# Line 191 | Line 199 | public class JSR166TestCase extends Test
199      }
200  
201      protected void runTestProfiled() throws Throwable {
202 <        // Warmup run, notably to trigger all needed classloading.
203 <        super.runTest();
196 <        long t0 = System.nanoTime();
197 <        try {
202 >        for (int i = 0; i < 2; i++) {
203 >            long startTime = System.nanoTime();
204              super.runTest();
205 <        } finally {
206 <            long elapsedMillis = millisElapsedSince(t0);
207 <            if (elapsedMillis >= profileThreshold)
205 >            long elapsedMillis = millisElapsedSince(startTime);
206 >            if (elapsedMillis < profileThreshold)
207 >                break;
208 >            // Never report first run of any test; treat it as a
209 >            // warmup run, notably to trigger all needed classloading,
210 >            if (i > 0)
211                  System.out.printf("%n%s: %d%n", toString(), elapsedMillis);
212          }
213      }
# Line 276 | Line 285 | public class JSR166TestCase extends Test
285      public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; }
286      public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; }
287      public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; }
288 <    public static boolean atLeastJava9() { return JAVA_CLASS_VERSION >= 53.0; }
288 >    public static boolean atLeastJava9() {
289 >        return JAVA_CLASS_VERSION >= 53.0
290 >            // As of 2015-09, java9 still uses 52.0 class file version
291 >            || JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?(9|[0-9][0-9])$");
292 >    }
293 >    public static boolean atLeastJava10() {
294 >        return JAVA_CLASS_VERSION >= 54.0
295 >            || JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?[0-9][0-9]$");
296 >    }
297  
298      /**
299       * Collects all JSR166 unit tests as one suite.
# Line 364 | Line 381 | public class JSR166TestCase extends Test
381                  "LongAdderTest",
382                  "SplittableRandomTest",
383                  "StampedLockTest",
384 +                "SubmissionPublisherTest",
385                  "ThreadLocalRandom8Test",
386              };
387              addNamedTestClasses(suite, java8TestClassNames);
# Line 372 | Line 390 | public class JSR166TestCase extends Test
390          // Java9+ test classes
391          if (atLeastJava9()) {
392              String[] java9TestClassNames = {
393 <                "ThreadPoolExecutor9Test",
393 >                // Currently empty, but expecting varhandle tests
394              };
395              addNamedTestClasses(suite, java9TestClassNames);
396          }
# Line 380 | Line 398 | public class JSR166TestCase extends Test
398          return suite;
399      }
400  
401 +    /** Returns list of junit-style test method names in given class. */
402 +    public static ArrayList<String> testMethodNames(Class<?> testClass) {
403 +        Method[] methods = testClass.getDeclaredMethods();
404 +        ArrayList<String> names = new ArrayList<String>(methods.length);
405 +        for (Method method : methods) {
406 +            if (method.getName().startsWith("test")
407 +                && Modifier.isPublic(method.getModifiers())
408 +                // method.getParameterCount() requires jdk8+
409 +                && method.getParameterTypes().length == 0) {
410 +                names.add(method.getName());
411 +            }
412 +        }
413 +        return names;
414 +    }
415 +
416 +    /**
417 +     * Returns junit-style testSuite for the given test class, but
418 +     * parameterized by passing extra data to each test.
419 +     */
420 +    public static <ExtraData> Test parameterizedTestSuite
421 +        (Class<? extends JSR166TestCase> testClass,
422 +         Class<ExtraData> dataClass,
423 +         ExtraData data) {
424 +        try {
425 +            TestSuite suite = new TestSuite();
426 +            Constructor c =
427 +                testClass.getDeclaredConstructor(dataClass, String.class);
428 +            for (String methodName : testMethodNames(testClass))
429 +                suite.addTest((Test) c.newInstance(data, methodName));
430 +            return suite;
431 +        } catch (Exception e) {
432 +            throw new Error(e);
433 +        }
434 +    }
435 +
436 +    /**
437 +     * Returns junit-style testSuite for the jdk8 extension of the
438 +     * given test class, but parameterized by passing extra data to
439 +     * each test.  Uses reflection to allow compilation in jdk7.
440 +     */
441 +    public static <ExtraData> Test jdk8ParameterizedTestSuite
442 +        (Class<? extends JSR166TestCase> testClass,
443 +         Class<ExtraData> dataClass,
444 +         ExtraData data) {
445 +        if (atLeastJava8()) {
446 +            String name = testClass.getName();
447 +            String name8 = name.replaceAll("Test$", "8Test");
448 +            if (name.equals(name8)) throw new Error(name);
449 +            try {
450 +                return (Test)
451 +                    Class.forName(name8)
452 +                    .getMethod("testSuite", new Class[] { dataClass })
453 +                    .invoke(null, data);
454 +            } catch (Exception e) {
455 +                throw new Error(e);
456 +            }
457 +        } else {
458 +            return new TestSuite();
459 +        }
460 +
461 +    }
462 +
463      // Delays for timing-dependent tests, in milliseconds.
464  
465      public static long SHORT_DELAY_MS;
# Line 414 | Line 494 | public class JSR166TestCase extends Test
494      }
495  
496      /**
497 <     * Returns a new Date instance representing a time delayMillis
498 <     * milliseconds in the future.
497 >     * Returns a new Date instance representing a time at least
498 >     * delayMillis milliseconds in the future.
499       */
500      Date delayedDate(long delayMillis) {
501 <        return new Date(System.currentTimeMillis() + delayMillis);
501 >        // Add 1 because currentTimeMillis is known to round into the past.
502 >        return new Date(System.currentTimeMillis() + delayMillis + 1);
503      }
504  
505      /**
# Line 441 | Line 522 | public class JSR166TestCase extends Test
522          setDelays();
523      }
524  
525 +    void tearDownFail(String format, Object... args) {
526 +        String msg = toString() + ": " + String.format(format, args);
527 +        System.err.println(msg);
528 +        printAllStackTraces();
529 +        throw new AssertionFailedError(msg);
530 +    }
531 +
532      /**
533       * Extra checks that get done for all test cases.
534       *
# Line 468 | Line 556 | public class JSR166TestCase extends Test
556          }
557  
558          if (Thread.interrupted())
559 <            throw new AssertionFailedError("interrupt status set in main thread");
559 >            tearDownFail("interrupt status set in main thread");
560  
561          checkForkJoinPoolThreadLeaks();
562      }
# Line 477 | Line 565 | public class JSR166TestCase extends Test
565       * Finds missing try { ... } finally { joinPool(e); }
566       */
567      void checkForkJoinPoolThreadLeaks() throws InterruptedException {
568 <        Thread[] survivors = new Thread[5];
568 >        Thread[] survivors = new Thread[7];
569          int count = Thread.enumerate(survivors);
570          for (int i = 0; i < count; i++) {
571              Thread thread = survivors[i];
# Line 485 | Line 573 | public class JSR166TestCase extends Test
573              if (name.startsWith("ForkJoinPool-")) {
574                  // give thread some time to terminate
575                  thread.join(LONG_DELAY_MS);
576 <                if (!thread.isAlive()) continue;
577 <                thread.stop();
578 <                throw new AssertionFailedError
491 <                    (String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n",
492 <                                   toString(), name));
576 >                if (thread.isAlive())
577 >                    tearDownFail("Found leaked ForkJoinPool thread thread=%s",
578 >                                 thread);
579              }
580          }
581 +
582 +        if (!ForkJoinPool.commonPool()
583 +            .awaitQuiescence(LONG_DELAY_MS, MILLISECONDS))
584 +            tearDownFail("ForkJoin common pool thread stuck");
585      }
586  
587      /**
# Line 652 | Line 742 | public class JSR166TestCase extends Test
742      /**
743       * Waits out termination of a thread pool or fails doing so.
744       */
745 <    void joinPool(ExecutorService exec) {
745 >    void joinPool(ExecutorService pool) {
746          try {
747 <            exec.shutdown();
748 <            if (!exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS))
749 <                fail("ExecutorService " + exec +
747 >            pool.shutdown();
748 >            if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS))
749 >                fail("ExecutorService " + pool +
750                       " did not terminate in a timely manner");
751          } catch (SecurityException ok) {
752              // Allowed in case test doesn't have privs
# Line 665 | Line 755 | public class JSR166TestCase extends Test
755          }
756      }
757  
758 +    /** Like Runnable, but with the freedom to throw anything */
759 +    interface Action { public void run() throws Throwable; }
760 +
761 +    /**
762 +     * Runs all the given actions in parallel, failing if any fail.
763 +     * Useful for running multiple variants of tests that are
764 +     * necessarily individually slow because they must block.
765 +     */
766 +    void testInParallel(Action ... actions) {
767 +        ExecutorService pool = Executors.newCachedThreadPool();
768 +        try {
769 +            ArrayList<Future<?>> futures = new ArrayList<>(actions.length);
770 +            for (final Action action : actions)
771 +                futures.add(pool.submit(new CheckedRunnable() {
772 +                    public void realRun() throws Throwable { action.run();}}));
773 +            for (Future<?> future : futures)
774 +                try {
775 +                    assertNull(future.get(LONG_DELAY_MS, MILLISECONDS));
776 +                } catch (ExecutionException ex) {
777 +                    threadUnexpectedException(ex.getCause());
778 +                } catch (Exception ex) {
779 +                    threadUnexpectedException(ex);
780 +                }
781 +        } finally {
782 +            joinPool(pool);
783 +        }
784 +    }
785 +
786      /**
787       * A debugging tool to print all stack traces, as jstack does.
788       */
# Line 1117 | Line 1235 | public class JSR166TestCase extends Test
1235      public static final String TEST_STRING = "a test string";
1236  
1237      public static class StringTask implements Callable<String> {
1238 <        public String call() { return TEST_STRING; }
1238 >        final String value;
1239 >        public StringTask() { this(TEST_STRING); }
1240 >        public StringTask(String value) { this.value = value; }
1241 >        public String call() { return value; }
1242      }
1243  
1244      public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines