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.118 by jsr166, Mon Jun 16 18:01:38 2014 UTC vs.
Revision 1.153 by jsr166, Sat Oct 3 19:37:43 2015 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 < import junit.framework.*;
9 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 > import static java.util.concurrent.TimeUnit.NANOSECONDS;
11 >
12   import java.io.ByteArrayInputStream;
13   import java.io.ByteArrayOutputStream;
14   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.management.ThreadMXBean;
19 + import java.lang.reflect.Constructor;
20   import java.lang.reflect.Method;
21 + import java.lang.reflect.Modifier;
22 + import java.security.CodeSource;
23 + import java.security.Permission;
24 + import java.security.PermissionCollection;
25 + import java.security.Permissions;
26 + import java.security.Policy;
27 + import java.security.ProtectionDomain;
28 + import java.security.SecurityPermission;
29   import java.util.ArrayList;
30   import java.util.Arrays;
31   import java.util.Date;
32   import java.util.Enumeration;
33 + import java.util.Iterator;
34   import java.util.List;
35   import java.util.NoSuchElementException;
36   import java.util.PropertyPermission;
37 < import java.util.concurrent.*;
38 < import java.util.concurrent.atomic.AtomicBoolean;
37 > import java.util.concurrent.BlockingQueue;
38 > import java.util.concurrent.Callable;
39 > import java.util.concurrent.CountDownLatch;
40 > import java.util.concurrent.CyclicBarrier;
41 > import java.util.concurrent.ExecutionException;
42 > import java.util.concurrent.Executors;
43 > import java.util.concurrent.ExecutorService;
44 > import java.util.concurrent.ForkJoinPool;
45 > import java.util.concurrent.Future;
46 > import java.util.concurrent.RecursiveAction;
47 > import java.util.concurrent.RecursiveTask;
48 > import java.util.concurrent.RejectedExecutionHandler;
49 > import java.util.concurrent.Semaphore;
50 > import java.util.concurrent.ThreadFactory;
51 > import java.util.concurrent.ThreadPoolExecutor;
52 > import java.util.concurrent.TimeoutException;
53   import java.util.concurrent.atomic.AtomicReference;
27 import static java.util.concurrent.TimeUnit.MILLISECONDS;
28 import static java.util.concurrent.TimeUnit.NANOSECONDS;
54   import java.util.regex.Pattern;
55 < import java.security.CodeSource;
56 < import java.security.Permission;
57 < import java.security.PermissionCollection;
58 < import java.security.Permissions;
59 < import java.security.Policy;
60 < import java.security.ProtectionDomain;
36 < import java.security.SecurityPermission;
55 >
56 > import junit.framework.AssertionFailedError;
57 > import junit.framework.Test;
58 > import junit.framework.TestCase;
59 > import junit.framework.TestResult;
60 > import junit.framework.TestSuite;
61  
62   /**
63   * Base class for JSR166 Junit TCK tests.  Defines some constants,
# Line 45 | Line 69 | import java.security.SecurityPermission;
69   *
70   * <ol>
71   *
72 < * <li> All assertions in code running in generated threads must use
72 > * <li>All assertions in code running in generated threads must use
73   * the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link
74   * #threadAssertEquals}, or {@link #threadAssertNull}, (not
75   * {@code fail}, {@code assertTrue}, etc.) It is OK (but not
76   * particularly recommended) for other code to use these forms too.
77   * Only the most typically used JUnit assertion methods are defined
78 < * this way, but enough to live with.</li>
78 > * this way, but enough to live with.
79   *
80 < * <li> If you override {@link #setUp} or {@link #tearDown}, make sure
80 > * <li>If you override {@link #setUp} or {@link #tearDown}, make sure
81   * to invoke {@code super.setUp} and {@code super.tearDown} within
82   * them. These methods are used to clear and check for thread
83 < * assertion failures.</li>
83 > * assertion failures.
84   *
85   * <li>All delays and timeouts must use one of the constants {@code
86   * SHORT_DELAY_MS}, {@code SMALL_DELAY_MS}, {@code MEDIUM_DELAY_MS},
# Line 67 | Line 91 | import java.security.SecurityPermission;
91   * is always discriminable as larger than SHORT and smaller than
92   * MEDIUM.  And so on. These constants are set to conservative values,
93   * but even so, if there is ever any doubt, they can all be increased
94 < * in one spot to rerun tests on slower platforms.</li>
94 > * in one spot to rerun tests on slower platforms.
95   *
96 < * <li> All threads generated must be joined inside each test case
96 > * <li>All threads generated must be joined inside each test case
97   * method (or {@code fail} to do so) before returning from the
98   * method. The {@code joinPool} method can be used to do this when
99 < * using Executors.</li>
99 > * using Executors.
100   *
101   * </ol>
102   *
103   * <p><b>Other notes</b>
104   * <ul>
105   *
106 < * <li> Usually, there is one testcase method per JSR166 method
106 > * <li>Usually, there is one testcase method per JSR166 method
107   * covering "normal" operation, and then as many exception-testing
108   * methods as there are exceptions the method can throw. Sometimes
109   * there are multiple tests per JSR166 method when the different
110   * "normal" behaviors differ significantly. And sometimes testcases
111   * cover multiple methods when they cannot be tested in
112 < * isolation.</li>
112 > * isolation.
113   *
114 < * <li> The documentation style for testcases is to provide as javadoc
114 > * <li>The documentation style for testcases is to provide as javadoc
115   * a simple sentence or two describing the property that the testcase
116   * method purports to test. The javadocs do not say anything about how
117 < * the property is tested. To find out, read the code.</li>
117 > * the property is tested. To find out, read the code.
118   *
119 < * <li> These tests are "conformance tests", and do not attempt to
119 > * <li>These tests are "conformance tests", and do not attempt to
120   * test throughput, latency, scalability or other performance factors
121   * (see the separate "jtreg" tests for a set intended to check these
122   * for the most central aspects of functionality.) So, most tests use
123   * the smallest sensible numbers of threads, collection sizes, etc
124 < * needed to check basic conformance.</li>
124 > * needed to check basic conformance.
125   *
126   * <li>The test classes currently do not declare inclusion in
127   * any particular package to simplify things for people integrating
128 < * them in TCK test suites.</li>
128 > * them in TCK test suites.
129   *
130 < * <li> As a convenience, the {@code main} of this class (JSR166TestCase)
131 < * runs all JSR166 unit tests.</li>
130 > * <li>As a convenience, the {@code main} of this class (JSR166TestCase)
131 > * runs all JSR166 unit tests.
132   *
133   * </ul>
134   */
# Line 115 | Line 139 | public class JSR166TestCase extends Test
139      protected static final boolean expensiveTests =
140          Boolean.getBoolean("jsr166.expensiveTests");
141  
142 +    /**
143 +     * If true, also run tests that are not part of the official tck
144 +     * because they test unspecified implementation details.
145 +     */
146      protected static final boolean testImplementationDetails =
147          Boolean.getBoolean("jsr166.testImplementationDetails");
148  
# Line 139 | Line 167 | public class JSR166TestCase extends Test
167          Integer.getInteger("jsr166.runsPerTest", 1);
168  
169      /**
170 +     * The number of repetitions of the test suite (for finding leaks?).
171 +     */
172 +    private static final int suiteRuns =
173 +        Integer.getInteger("jsr166.suiteRuns", 1);
174 +
175 +    public JSR166TestCase() { super(); }
176 +    public JSR166TestCase(String name) { super(name); }
177 +
178 +    /**
179       * A filter for tests to run, matching strings of the form
180       * methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)"
181       * Usefully combined with jsr166.runsPerTest.
# Line 150 | Line 187 | public class JSR166TestCase extends Test
187          return (regex == null) ? null : Pattern.compile(regex);
188      }
189  
190 <    protected void runTest() throws Throwable {
190 >    public void runBare() throws Throwable {
191          if (methodFilter == null
192 <            || methodFilter.matcher(toString()).find()) {
193 <            for (int i = 0; i < runsPerTest; i++) {
194 <                if (profileTests)
195 <                    runTestProfiled();
196 <                else
197 <                    super.runTest();
198 <            }
192 >            || methodFilter.matcher(toString()).find())
193 >            super.runBare();
194 >    }
195 >
196 >    protected void runTest() throws Throwable {
197 >        for (int i = 0; i < runsPerTest; i++) {
198 >            if (profileTests)
199 >                runTestProfiled();
200 >            else
201 >                super.runTest();
202          }
203      }
204  
205      protected void runTestProfiled() throws Throwable {
206 <        // Warmup run, notably to trigger all needed classloading.
207 <        super.runTest();
168 <        long t0 = System.nanoTime();
169 <        try {
206 >        for (int i = 0; i < 2; i++) {
207 >            long startTime = System.nanoTime();
208              super.runTest();
209 <        } finally {
210 <            long elapsedMillis = millisElapsedSince(t0);
211 <            if (elapsedMillis >= profileThreshold)
209 >            long elapsedMillis = millisElapsedSince(startTime);
210 >            if (elapsedMillis < profileThreshold)
211 >                break;
212 >            // Never report first run of any test; treat it as a
213 >            // warmup run, notably to trigger all needed classloading,
214 >            if (i > 0)
215                  System.out.printf("%n%s: %d%n", toString(), elapsedMillis);
216          }
217      }
218  
219      /**
220       * Runs all JSR166 unit tests using junit.textui.TestRunner.
180     * Optional command line arg provides the number of iterations to
181     * repeat running the tests.
221       */
222      public static void main(String[] args) {
223 +        main(suite(), args);
224 +    }
225 +
226 +    /**
227 +     * Runs all unit tests in the given test suite.
228 +     * Actual behavior influenced by jsr166.* system properties.
229 +     */
230 +    static void main(Test suite, String[] args) {
231          if (useSecurityManager) {
232              System.err.println("Setting a permissive security manager");
233              Policy.setPolicy(permissivePolicy());
234              System.setSecurityManager(new SecurityManager());
235          }
236 <        int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]);
237 <
238 <        Test s = suite();
239 <        for (int i = 0; i < iters; ++i) {
193 <            junit.textui.TestRunner.run(s);
236 >        for (int i = 0; i < suiteRuns; i++) {
237 >            TestResult result = junit.textui.TestRunner.run(suite);
238 >            if (!result.wasSuccessful())
239 >                System.exit(1);
240              System.gc();
241              System.runFinalization();
242          }
197        System.exit(0);
243      }
244  
245      public static TestSuite newTestSuite(Object... suiteOrClasses) {
# Line 245 | Line 290 | public class JSR166TestCase extends Test
290      public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; }
291      public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; }
292      public static boolean atLeastJava9() {
293 <        // As of 2014-05, java9 still uses 52.0 class file version
294 <        return JAVA_SPECIFICATION_VERSION.startsWith("1.9");
293 >        return JAVA_CLASS_VERSION >= 53.0
294 >            // As of 2015-09, java9 still uses 52.0 class file version
295 >            || JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?(9|[0-9][0-9])$");
296 >    }
297 >    public static boolean atLeastJava10() {
298 >        return JAVA_CLASS_VERSION >= 54.0
299 >            || JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?[0-9][0-9]$");
300      }
301  
302      /**
# Line 335 | Line 385 | public class JSR166TestCase extends Test
385                  "LongAdderTest",
386                  "SplittableRandomTest",
387                  "StampedLockTest",
388 +                "SubmissionPublisherTest",
389                  "ThreadLocalRandom8Test",
390              };
391              addNamedTestClasses(suite, java8TestClassNames);
# Line 343 | Line 394 | public class JSR166TestCase extends Test
394          // Java9+ test classes
395          if (atLeastJava9()) {
396              String[] java9TestClassNames = {
397 <                "ThreadPoolExecutor9Test",
397 >                // Currently empty, but expecting varhandle tests
398              };
399              addNamedTestClasses(suite, java9TestClassNames);
400          }
# Line 351 | Line 402 | public class JSR166TestCase extends Test
402          return suite;
403      }
404  
405 +    /** Returns list of junit-style test method names in given class. */
406 +    public static ArrayList<String> testMethodNames(Class<?> testClass) {
407 +        Method[] methods = testClass.getDeclaredMethods();
408 +        ArrayList<String> names = new ArrayList<String>(methods.length);
409 +        for (Method method : methods) {
410 +            if (method.getName().startsWith("test")
411 +                && Modifier.isPublic(method.getModifiers())
412 +                // method.getParameterCount() requires jdk8+
413 +                && method.getParameterTypes().length == 0) {
414 +                names.add(method.getName());
415 +            }
416 +        }
417 +        return names;
418 +    }
419 +
420 +    /**
421 +     * Returns junit-style testSuite for the given test class, but
422 +     * parameterized by passing extra data to each test.
423 +     */
424 +    public static <ExtraData> Test parameterizedTestSuite
425 +        (Class<? extends JSR166TestCase> testClass,
426 +         Class<ExtraData> dataClass,
427 +         ExtraData data) {
428 +        try {
429 +            TestSuite suite = new TestSuite();
430 +            Constructor c =
431 +                testClass.getDeclaredConstructor(dataClass, String.class);
432 +            for (String methodName : testMethodNames(testClass))
433 +                suite.addTest((Test) c.newInstance(data, methodName));
434 +            return suite;
435 +        } catch (Exception e) {
436 +            throw new Error(e);
437 +        }
438 +    }
439 +
440 +    /**
441 +     * Returns junit-style testSuite for the jdk8 extension of the
442 +     * given test class, but parameterized by passing extra data to
443 +     * each test.  Uses reflection to allow compilation in jdk7.
444 +     */
445 +    public static <ExtraData> Test jdk8ParameterizedTestSuite
446 +        (Class<? extends JSR166TestCase> testClass,
447 +         Class<ExtraData> dataClass,
448 +         ExtraData data) {
449 +        if (atLeastJava8()) {
450 +            String name = testClass.getName();
451 +            String name8 = name.replaceAll("Test$", "8Test");
452 +            if (name.equals(name8)) throw new Error(name);
453 +            try {
454 +                return (Test)
455 +                    Class.forName(name8)
456 +                    .getMethod("testSuite", new Class[] { dataClass })
457 +                    .invoke(null, data);
458 +            } catch (Exception e) {
459 +                throw new Error(e);
460 +            }
461 +        } else {
462 +            return new TestSuite();
463 +        }
464 +
465 +    }
466 +
467      // Delays for timing-dependent tests, in milliseconds.
468  
469      public static long SHORT_DELAY_MS;
# Line 385 | Line 498 | public class JSR166TestCase extends Test
498      }
499  
500      /**
501 <     * Returns a new Date instance representing a time delayMillis
502 <     * milliseconds in the future.
501 >     * Returns a new Date instance representing a time at least
502 >     * delayMillis milliseconds in the future.
503       */
504      Date delayedDate(long delayMillis) {
505 <        return new Date(System.currentTimeMillis() + delayMillis);
505 >        // Add 1 because currentTimeMillis is known to round into the past.
506 >        return new Date(System.currentTimeMillis() + delayMillis + 1);
507      }
508  
509      /**
# Line 405 | Line 519 | public class JSR166TestCase extends Test
519       * the same test have no effect.
520       */
521      public void threadRecordFailure(Throwable t) {
522 +        printAllStackTraces();
523          threadFailure.compareAndSet(null, t);
524      }
525  
# Line 412 | Line 527 | public class JSR166TestCase extends Test
527          setDelays();
528      }
529  
530 +    void tearDownFail(String format, Object... args) {
531 +        String msg = toString() + ": " + String.format(format, args);
532 +        System.err.println(msg);
533 +        printAllStackTraces();
534 +        throw new AssertionFailedError(msg);
535 +    }
536 +
537      /**
538       * Extra checks that get done for all test cases.
539       *
# Line 439 | Line 561 | public class JSR166TestCase extends Test
561          }
562  
563          if (Thread.interrupted())
564 <            throw new AssertionFailedError("interrupt status set in main thread");
564 >            tearDownFail("interrupt status set in main thread");
565  
566          checkForkJoinPoolThreadLeaks();
567      }
568  
569      /**
570 <     * Find missing try { ... } finally { joinPool(e); }
570 >     * Finds missing try { ... } finally { joinPool(e); }
571       */
572      void checkForkJoinPoolThreadLeaks() throws InterruptedException {
573 <        Thread[] survivors = new Thread[5];
573 >        Thread[] survivors = new Thread[7];
574          int count = Thread.enumerate(survivors);
575          for (int i = 0; i < count; i++) {
576              Thread thread = survivors[i];
# Line 456 | Line 578 | public class JSR166TestCase extends Test
578              if (name.startsWith("ForkJoinPool-")) {
579                  // give thread some time to terminate
580                  thread.join(LONG_DELAY_MS);
581 <                if (!thread.isAlive()) continue;
582 <                thread.stop();
583 <                throw new AssertionFailedError
462 <                    (String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n",
463 <                                   toString(), name));
581 >                if (thread.isAlive())
582 >                    tearDownFail("Found leaked ForkJoinPool thread thread=%s",
583 >                                 thread);
584              }
585          }
586 +
587 +        if (!ForkJoinPool.commonPool()
588 +            .awaitQuiescence(LONG_DELAY_MS, MILLISECONDS))
589 +            tearDownFail("ForkJoin common pool thread stuck");
590      }
591  
592      /**
# Line 475 | Line 599 | public class JSR166TestCase extends Test
599              fail(reason);
600          } catch (AssertionFailedError t) {
601              threadRecordFailure(t);
602 <            fail(reason);
602 >            throw t;
603          }
604      }
605  
# Line 543 | Line 667 | public class JSR166TestCase extends Test
667      public void threadAssertEquals(Object x, Object y) {
668          try {
669              assertEquals(x, y);
670 <        } catch (AssertionFailedError t) {
671 <            threadRecordFailure(t);
672 <            throw t;
673 <        } catch (Throwable t) {
674 <            threadUnexpectedException(t);
670 >        } catch (AssertionFailedError fail) {
671 >            threadRecordFailure(fail);
672 >            throw fail;
673 >        } catch (Throwable fail) {
674 >            threadUnexpectedException(fail);
675          }
676      }
677  
# Line 559 | Line 683 | public class JSR166TestCase extends Test
683      public void threadAssertSame(Object x, Object y) {
684          try {
685              assertSame(x, y);
686 <        } catch (AssertionFailedError t) {
687 <            threadRecordFailure(t);
688 <            throw t;
686 >        } catch (AssertionFailedError fail) {
687 >            threadRecordFailure(fail);
688 >            throw fail;
689          }
690      }
691  
# Line 621 | Line 745 | public class JSR166TestCase extends Test
745      }
746  
747      /**
748 +     * Allows use of try-with-resources with per-test thread pools.
749 +     */
750 +    static class PoolCloser<T extends ExecutorService>
751 +            implements AutoCloseable {
752 +        public final T pool;
753 +        public PoolCloser(T pool) { this.pool = pool; }
754 +        public void close() { joinPool(pool); }
755 +    }
756 +
757 +    /**
758       * Waits out termination of a thread pool or fails doing so.
759       */
760 <    void joinPool(ExecutorService exec) {
760 >    static void joinPool(ExecutorService pool) {
761          try {
762 <            exec.shutdown();
763 <            assertTrue("ExecutorService did not terminate in a timely manner",
764 <                       exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS));
762 >            pool.shutdown();
763 >            if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS))
764 >                fail("ExecutorService " + pool +
765 >                     " did not terminate in a timely manner");
766          } catch (SecurityException ok) {
767              // Allowed in case test doesn't have privs
768 <        } catch (InterruptedException ie) {
768 >        } catch (InterruptedException fail) {
769              fail("Unexpected InterruptedException");
770          }
771      }
772  
773 +    /** Like Runnable, but with the freedom to throw anything */
774 +    interface Action { public void run() throws Throwable; }
775 +
776 +    /**
777 +     * Runs all the given actions in parallel, failing if any fail.
778 +     * Useful for running multiple variants of tests that are
779 +     * necessarily individually slow because they must block.
780 +     */
781 +    void testInParallel(Action ... actions) {
782 +        try (PoolCloser<ExecutorService> poolCloser
783 +             = new PoolCloser<>(Executors.newCachedThreadPool())) {
784 +            ExecutorService pool = poolCloser.pool;
785 +            ArrayList<Future<?>> futures = new ArrayList<>(actions.length);
786 +            for (final Action action : actions)
787 +                futures.add(pool.submit(new CheckedRunnable() {
788 +                    public void realRun() throws Throwable { action.run();}}));
789 +            for (Future<?> future : futures)
790 +                try {
791 +                    assertNull(future.get(LONG_DELAY_MS, MILLISECONDS));
792 +                } catch (ExecutionException ex) {
793 +                    threadUnexpectedException(ex.getCause());
794 +                } catch (Exception ex) {
795 +                    threadUnexpectedException(ex);
796 +                }
797 +        }
798 +    }
799 +
800      /**
801       * A debugging tool to print all stack traces, as jstack does.
802 +     * Uninteresting threads are filtered out.
803       */
804      static void printAllStackTraces() {
805 <        for (ThreadInfo info :
806 <                 ManagementFactory.getThreadMXBean()
807 <                 .dumpAllThreads(true, true))
805 >        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
806 >        System.err.println("------ stacktrace dump start ------");
807 >        for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) {
808 >            String name = info.getThreadName();
809 >            if ("Signal Dispatcher".equals(name))
810 >                continue;
811 >            if ("Reference Handler".equals(name)
812 >                && info.getLockName().startsWith("java.lang.ref.Reference$Lock"))
813 >                continue;
814 >            if ("Finalizer".equals(name)
815 >                && info.getLockName().startsWith("java.lang.ref.ReferenceQueue$Lock"))
816 >                continue;
817              System.err.print(info);
818 +        }
819 +        System.err.println("------ stacktrace dump end ------");
820      }
821  
822      /**
# Line 661 | Line 835 | public class JSR166TestCase extends Test
835              // No need to optimize the failing case via Thread.join.
836              delay(millis);
837              assertTrue(thread.isAlive());
838 <        } catch (InterruptedException ie) {
838 >        } catch (InterruptedException fail) {
839              fail("Unexpected InterruptedException");
840          }
841      }
# Line 683 | Line 857 | public class JSR166TestCase extends Test
857              delay(millis);
858              for (Thread thread : threads)
859                  assertTrue(thread.isAlive());
860 <        } catch (InterruptedException ie) {
860 >        } catch (InterruptedException fail) {
861              fail("Unexpected InterruptedException");
862          }
863      }
# Line 705 | Line 879 | public class JSR166TestCase extends Test
879              future.get(timeoutMillis, MILLISECONDS);
880              shouldThrow();
881          } catch (TimeoutException success) {
882 <        } catch (Exception e) {
883 <            threadUnexpectedException(e);
882 >        } catch (Exception fail) {
883 >            threadUnexpectedException(fail);
884          } finally { future.cancel(true); }
885          assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
886      }
# Line 862 | Line 1036 | public class JSR166TestCase extends Test
1036      void sleep(long millis) {
1037          try {
1038              delay(millis);
1039 <        } catch (InterruptedException ie) {
1039 >        } catch (InterruptedException fail) {
1040              AssertionFailedError afe =
1041                  new AssertionFailedError("Unexpected InterruptedException");
1042 <            afe.initCause(ie);
1042 >            afe.initCause(fail);
1043              throw afe;
1044          }
1045      }
# Line 903 | Line 1077 | public class JSR166TestCase extends Test
1077      /**
1078       * Returns the number of milliseconds since time given by
1079       * startNanoTime, which must have been previously returned from a
1080 <     * call to {@link System.nanoTime()}.
1080 >     * call to {@link System#nanoTime()}.
1081       */
1082      static long millisElapsedSince(long startNanoTime) {
1083          return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime);
1084      }
1085  
1086 + //     void assertTerminatesPromptly(long timeoutMillis, Runnable r) {
1087 + //         long startTime = System.nanoTime();
1088 + //         try {
1089 + //             r.run();
1090 + //         } catch (Throwable fail) { threadUnexpectedException(fail); }
1091 + //         if (millisElapsedSince(startTime) > timeoutMillis/2)
1092 + //             throw new AssertionFailedError("did not return promptly");
1093 + //     }
1094 +
1095 + //     void assertTerminatesPromptly(Runnable r) {
1096 + //         assertTerminatesPromptly(LONG_DELAY_MS/2, r);
1097 + //     }
1098 +
1099 +    /**
1100 +     * Checks that timed f.get() returns the expected value, and does not
1101 +     * wait for the timeout to elapse before returning.
1102 +     */
1103 +    <T> void checkTimedGet(Future<T> f, T expectedValue, long timeoutMillis) {
1104 +        long startTime = System.nanoTime();
1105 +        try {
1106 +            assertEquals(expectedValue, f.get(timeoutMillis, MILLISECONDS));
1107 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
1108 +        if (millisElapsedSince(startTime) > timeoutMillis/2)
1109 +            throw new AssertionFailedError("timed get did not return promptly");
1110 +    }
1111 +
1112 +    <T> void checkTimedGet(Future<T> f, T expectedValue) {
1113 +        checkTimedGet(f, expectedValue, LONG_DELAY_MS);
1114 +    }
1115 +
1116      /**
1117       * Returns a new started daemon Thread running the given runnable.
1118       */
# Line 927 | Line 1131 | public class JSR166TestCase extends Test
1131      void awaitTermination(Thread t, long timeoutMillis) {
1132          try {
1133              t.join(timeoutMillis);
1134 <        } catch (InterruptedException ie) {
1135 <            threadUnexpectedException(ie);
1134 >        } catch (InterruptedException fail) {
1135 >            threadUnexpectedException(fail);
1136          } finally {
1137              if (t.getState() != Thread.State.TERMINATED) {
1138                  t.interrupt();
# Line 954 | Line 1158 | public class JSR166TestCase extends Test
1158          public final void run() {
1159              try {
1160                  realRun();
1161 <            } catch (Throwable t) {
1162 <                threadUnexpectedException(t);
1161 >            } catch (Throwable fail) {
1162 >                threadUnexpectedException(fail);
1163              }
1164          }
1165      }
# Line 1009 | Line 1213 | public class JSR166TestCase extends Test
1213                  threadShouldThrow("InterruptedException");
1214              } catch (InterruptedException success) {
1215                  threadAssertFalse(Thread.interrupted());
1216 <            } catch (Throwable t) {
1217 <                threadUnexpectedException(t);
1216 >            } catch (Throwable fail) {
1217 >                threadUnexpectedException(fail);
1218              }
1219          }
1220      }
# Line 1021 | Line 1225 | public class JSR166TestCase extends Test
1225          public final T call() {
1226              try {
1227                  return realCall();
1228 <            } catch (Throwable t) {
1229 <                threadUnexpectedException(t);
1228 >            } catch (Throwable fail) {
1229 >                threadUnexpectedException(fail);
1230                  return null;
1231              }
1232          }
# Line 1039 | Line 1243 | public class JSR166TestCase extends Test
1243                  return result;
1244              } catch (InterruptedException success) {
1245                  threadAssertFalse(Thread.interrupted());
1246 <            } catch (Throwable t) {
1247 <                threadUnexpectedException(t);
1246 >            } catch (Throwable fail) {
1247 >                threadUnexpectedException(fail);
1248              }
1249              return null;
1250          }
# Line 1057 | Line 1261 | public class JSR166TestCase extends Test
1261      public static final String TEST_STRING = "a test string";
1262  
1263      public static class StringTask implements Callable<String> {
1264 <        public String call() { return TEST_STRING; }
1264 >        final String value;
1265 >        public StringTask() { this(TEST_STRING); }
1266 >        public StringTask(String value) { this.value = value; }
1267 >        public String call() { return value; }
1268      }
1269  
1270      public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) {
# Line 1070 | Line 1277 | public class JSR166TestCase extends Test
1277              }};
1278      }
1279  
1280 +    public Runnable countDowner(final CountDownLatch latch) {
1281 +        return new CheckedRunnable() {
1282 +            public void realRun() throws InterruptedException {
1283 +                latch.countDown();
1284 +            }};
1285 +    }
1286 +
1287      public Runnable awaiter(final CountDownLatch latch) {
1288          return new CheckedRunnable() {
1289              public void realRun() throws InterruptedException {
# Line 1080 | Line 1294 | public class JSR166TestCase extends Test
1294      public void await(CountDownLatch latch) {
1295          try {
1296              assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS));
1297 <        } catch (Throwable t) {
1298 <            threadUnexpectedException(t);
1297 >        } catch (Throwable fail) {
1298 >            threadUnexpectedException(fail);
1299          }
1300      }
1301  
1302      public void await(Semaphore semaphore) {
1303          try {
1304              assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS));
1305 <        } catch (Throwable t) {
1306 <            threadUnexpectedException(t);
1305 >        } catch (Throwable fail) {
1306 >            threadUnexpectedException(fail);
1307          }
1308      }
1309  
# Line 1283 | Line 1497 | public class JSR166TestCase extends Test
1497          @Override protected final void compute() {
1498              try {
1499                  realCompute();
1500 <            } catch (Throwable t) {
1501 <                threadUnexpectedException(t);
1500 >            } catch (Throwable fail) {
1501 >                threadUnexpectedException(fail);
1502              }
1503          }
1504      }
# Line 1298 | Line 1512 | public class JSR166TestCase extends Test
1512          @Override protected final T compute() {
1513              try {
1514                  return realCompute();
1515 <            } catch (Throwable t) {
1516 <                threadUnexpectedException(t);
1515 >            } catch (Throwable fail) {
1516 >                threadUnexpectedException(fail);
1517                  return null;
1518              }
1519          }
# Line 1323 | Line 1537 | public class JSR166TestCase extends Test
1537          public int await() {
1538              try {
1539                  return super.await(2 * LONG_DELAY_MS, MILLISECONDS);
1540 <            } catch (TimeoutException e) {
1540 >            } catch (TimeoutException timedOut) {
1541                  throw new AssertionFailedError("timed out");
1542 <            } catch (Exception e) {
1542 >            } catch (Exception fail) {
1543                  AssertionFailedError afe =
1544 <                    new AssertionFailedError("Unexpected exception: " + e);
1545 <                afe.initCause(e);
1544 >                    new AssertionFailedError("Unexpected exception: " + fail);
1545 >                afe.initCause(fail);
1546                  throw afe;
1547              }
1548          }
# Line 1356 | Line 1570 | public class JSR166TestCase extends Test
1570                  q.remove();
1571                  shouldThrow();
1572              } catch (NoSuchElementException success) {}
1573 <        } catch (InterruptedException ie) {
1360 <            threadUnexpectedException(ie);
1361 <        }
1573 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
1574      }
1575  
1576      void assertSerialEquals(Object x, Object y) {
# Line 1377 | Line 1589 | public class JSR166TestCase extends Test
1589              oos.flush();
1590              oos.close();
1591              return bos.toByteArray();
1592 <        } catch (Throwable t) {
1593 <            threadUnexpectedException(t);
1592 >        } catch (Throwable fail) {
1593 >            threadUnexpectedException(fail);
1594              return new byte[0];
1595          }
1596      }
# Line 1391 | Line 1603 | public class JSR166TestCase extends Test
1603              T clone = (T) ois.readObject();
1604              assertSame(o.getClass(), clone.getClass());
1605              return clone;
1606 <        } catch (Throwable t) {
1607 <            threadUnexpectedException(t);
1606 >        } catch (Throwable fail) {
1607 >            threadUnexpectedException(fail);
1608              return null;
1609          }
1610      }
# Line 1417 | Line 1629 | public class JSR166TestCase extends Test
1629                  shouldThrow(expectedExceptionClass.getName());
1630          }
1631      }
1632 +
1633 +    public void assertIteratorExhausted(Iterator<?> it) {
1634 +        try {
1635 +            it.next();
1636 +            shouldThrow();
1637 +        } catch (NoSuchElementException success) {}
1638 +        assertFalse(it.hasNext());
1639 +    }
1640   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines