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.50 by jsr166, Wed Aug 25 21:40:03 2010 UTC vs.
Revision 1.61 by jsr166, Mon Oct 11 03:54:10 2010 UTC

# Line 7 | Line 7
7   */
8  
9   import junit.framework.*;
10 < import java.util.*;
10 > import java.util.PropertyPermission;
11   import java.util.concurrent.*;
12 + import java.util.concurrent.atomic.AtomicReference;
13   import static java.util.concurrent.TimeUnit.MILLISECONDS;
14 < import java.io.*;
15 < import java.security.*;
14 > import java.security.CodeSource;
15 > import java.security.Permission;
16 > import java.security.PermissionCollection;
17 > import java.security.Permissions;
18 > import java.security.Policy;
19 > import java.security.ProtectionDomain;
20 > import java.security.SecurityPermission;
21  
22   /**
23   * Base class for JSR166 Junit TCK tests.  Defines some constants,
# Line 36 | Line 42 | import java.security.*;
42   * them. These methods are used to clear and check for thread
43   * assertion failures.</li>
44   *
45 < * <li>All delays and timeouts must use one of the constants {@code
45 > * <li>All delays and timeouts must use one of the constants {@code
46   * SHORT_DELAY_MS}, {@code SMALL_DELAY_MS}, {@code MEDIUM_DELAY_MS},
47   * {@code LONG_DELAY_MS}. The idea here is that a SHORT is always
48   * discriminable from zero time, and always allows enough time for the
# Line 91 | Line 97 | public class JSR166TestCase extends Test
97          Boolean.getBoolean("jsr166.useSecurityManager");
98  
99      /**
100 +     * If true, report on stdout all "slow" tests, that is, ones that
101 +     * take more than profileThreshold milliseconds to execute.
102 +     */
103 +    private static final boolean profileTests =
104 +        Boolean.getBoolean("jsr166.profileTests");
105 +
106 +    /**
107 +     * The number of milliseconds that tests are permitted for
108 +     * execution without being reported, when profileTests is set.
109 +     */
110 +    private static final long profileThreshold =
111 +        Long.getLong("jsr166.profileThreshold", 100);
112 +
113 +    protected void runTest() throws Throwable {
114 +        if (profileTests)
115 +            runTestProfiled();
116 +        else
117 +            super.runTest();
118 +    }
119 +
120 +    protected void runTestProfiled() throws Throwable {
121 +        long t0 = System.nanoTime();
122 +        try {
123 +            super.runTest();
124 +        } finally {
125 +            long elapsedMillis =
126 +                (System.nanoTime() - t0) / (1000L * 1000L);
127 +            if (elapsedMillis >= profileThreshold)
128 +                System.out.printf("%n%s: %d%n", toString(), elapsedMillis);
129 +        }
130 +    }
131 +    
132 +    /**
133       * Runs all JSR166 unit tests using junit.textui.TestRunner
134       */
135      public static void main(String[] args) {
# Line 99 | Line 138 | public class JSR166TestCase extends Test
138              Policy.setPolicy(permissivePolicy());
139              System.setSecurityManager(new SecurityManager());
140          }
141 <        int iters = 1;
142 <        if (args.length > 0)
104 <            iters = Integer.parseInt(args[0]);
141 >        int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]);
142 >
143          Test s = suite();
144          for (int i = 0; i < iters; ++i) {
145              junit.textui.TestRunner.run(s);
# Line 111 | Line 149 | public class JSR166TestCase extends Test
149          System.exit(0);
150      }
151  
152 +    public static TestSuite newTestSuite(Object... suiteOrClasses) {
153 +        TestSuite suite = new TestSuite();
154 +        for (Object suiteOrClass : suiteOrClasses) {
155 +            if (suiteOrClass instanceof TestSuite)
156 +                suite.addTest((TestSuite) suiteOrClass);
157 +            else if (suiteOrClass instanceof Class)
158 +                suite.addTest(new TestSuite((Class<?>) suiteOrClass));
159 +            else
160 +                throw new ClassCastException("not a test suite or class");
161 +        }
162 +        return suite;
163 +    }
164 +
165      /**
166 <     * Collects all JSR166 unit tests as one suite
166 >     * Collects all JSR166 unit tests as one suite.
167       */
168      public static Test suite() {
169 <        TestSuite suite = new TestSuite("JSR166 Unit Tests");
170 <
171 <        suite.addTest(new TestSuite(ForkJoinPoolTest.class));
172 <        suite.addTest(new TestSuite(ForkJoinTaskTest.class));
173 <        suite.addTest(new TestSuite(RecursiveActionTest.class));
174 <        suite.addTest(new TestSuite(RecursiveTaskTest.class));
175 <        suite.addTest(new TestSuite(LinkedTransferQueueTest.class));
176 <        suite.addTest(new TestSuite(PhaserTest.class));
177 <        suite.addTest(new TestSuite(ThreadLocalRandomTest.class));
178 <        suite.addTest(new TestSuite(AbstractExecutorServiceTest.class));
179 <        suite.addTest(new TestSuite(AbstractQueueTest.class));
180 <        suite.addTest(new TestSuite(AbstractQueuedSynchronizerTest.class));
181 <        suite.addTest(new TestSuite(AbstractQueuedLongSynchronizerTest.class));
182 <        suite.addTest(new TestSuite(ArrayBlockingQueueTest.class));
183 <        suite.addTest(new TestSuite(ArrayDequeTest.class));
184 <        suite.addTest(new TestSuite(AtomicBooleanTest.class));
185 <        suite.addTest(new TestSuite(AtomicIntegerArrayTest.class));
186 <        suite.addTest(new TestSuite(AtomicIntegerFieldUpdaterTest.class));
187 <        suite.addTest(new TestSuite(AtomicIntegerTest.class));
188 <        suite.addTest(new TestSuite(AtomicLongArrayTest.class));
189 <        suite.addTest(new TestSuite(AtomicLongFieldUpdaterTest.class));
190 <        suite.addTest(new TestSuite(AtomicLongTest.class));
191 <        suite.addTest(new TestSuite(AtomicMarkableReferenceTest.class));
192 <        suite.addTest(new TestSuite(AtomicReferenceArrayTest.class));
193 <        suite.addTest(new TestSuite(AtomicReferenceFieldUpdaterTest.class));
194 <        suite.addTest(new TestSuite(AtomicReferenceTest.class));
195 <        suite.addTest(new TestSuite(AtomicStampedReferenceTest.class));
196 <        suite.addTest(new TestSuite(ConcurrentHashMapTest.class));
197 <        suite.addTest(new TestSuite(ConcurrentLinkedDequeTest.class));
198 <        suite.addTest(new TestSuite(ConcurrentLinkedQueueTest.class));
199 <        suite.addTest(new TestSuite(ConcurrentSkipListMapTest.class));
200 <        suite.addTest(new TestSuite(ConcurrentSkipListSubMapTest.class));
201 <        suite.addTest(new TestSuite(ConcurrentSkipListSetTest.class));
202 <        suite.addTest(new TestSuite(ConcurrentSkipListSubSetTest.class));
203 <        suite.addTest(new TestSuite(CopyOnWriteArrayListTest.class));
204 <        suite.addTest(new TestSuite(CopyOnWriteArraySetTest.class));
205 <        suite.addTest(new TestSuite(CountDownLatchTest.class));
206 <        suite.addTest(new TestSuite(CyclicBarrierTest.class));
207 <        suite.addTest(new TestSuite(DelayQueueTest.class));
208 <        suite.addTest(new TestSuite(EntryTest.class));
209 <        suite.addTest(new TestSuite(ExchangerTest.class));
210 <        suite.addTest(new TestSuite(ExecutorsTest.class));
211 <        suite.addTest(new TestSuite(ExecutorCompletionServiceTest.class));
212 <        suite.addTest(new TestSuite(FutureTaskTest.class));
213 <        suite.addTest(new TestSuite(LinkedBlockingDequeTest.class));
214 <        suite.addTest(new TestSuite(LinkedBlockingQueueTest.class));
215 <        suite.addTest(new TestSuite(LinkedListTest.class));
216 <        suite.addTest(new TestSuite(LockSupportTest.class));
217 <        suite.addTest(new TestSuite(PriorityBlockingQueueTest.class));
218 <        suite.addTest(new TestSuite(PriorityQueueTest.class));
219 <        suite.addTest(new TestSuite(ReentrantLockTest.class));
220 <        suite.addTest(new TestSuite(ReentrantReadWriteLockTest.class));
221 <        suite.addTest(new TestSuite(ScheduledExecutorTest.class));
222 <        suite.addTest(new TestSuite(ScheduledExecutorSubclassTest.class));
223 <        suite.addTest(new TestSuite(SemaphoreTest.class));
224 <        suite.addTest(new TestSuite(SynchronousQueueTest.class));
225 <        suite.addTest(new TestSuite(SystemTest.class));
226 <        suite.addTest(new TestSuite(ThreadLocalTest.class));
227 <        suite.addTest(new TestSuite(ThreadPoolExecutorTest.class));
228 <        suite.addTest(new TestSuite(ThreadPoolExecutorSubclassTest.class));
229 <        suite.addTest(new TestSuite(ThreadTest.class));
230 <        suite.addTest(new TestSuite(TimeUnitTest.class));
231 <        suite.addTest(new TestSuite(TreeMapTest.class));
232 <        suite.addTest(new TestSuite(TreeSetTest.class));
233 <        suite.addTest(new TestSuite(TreeSubMapTest.class));
183 <        suite.addTest(new TestSuite(TreeSubSetTest.class));
184 <
185 <        return suite;
169 >        return newTestSuite(
170 >            ForkJoinPoolTest.suite(),
171 >            ForkJoinTaskTest.suite(),
172 >            RecursiveActionTest.suite(),
173 >            RecursiveTaskTest.suite(),
174 >            LinkedTransferQueueTest.suite(),
175 >            PhaserTest.suite(),
176 >            ThreadLocalRandomTest.suite(),
177 >            AbstractExecutorServiceTest.suite(),
178 >            AbstractQueueTest.suite(),
179 >            AbstractQueuedSynchronizerTest.suite(),
180 >            AbstractQueuedLongSynchronizerTest.suite(),
181 >            ArrayBlockingQueueTest.suite(),
182 >            ArrayDequeTest.suite(),
183 >            AtomicBooleanTest.suite(),
184 >            AtomicIntegerArrayTest.suite(),
185 >            AtomicIntegerFieldUpdaterTest.suite(),
186 >            AtomicIntegerTest.suite(),
187 >            AtomicLongArrayTest.suite(),
188 >            AtomicLongFieldUpdaterTest.suite(),
189 >            AtomicLongTest.suite(),
190 >            AtomicMarkableReferenceTest.suite(),
191 >            AtomicReferenceArrayTest.suite(),
192 >            AtomicReferenceFieldUpdaterTest.suite(),
193 >            AtomicReferenceTest.suite(),
194 >            AtomicStampedReferenceTest.suite(),
195 >            ConcurrentHashMapTest.suite(),
196 >            ConcurrentLinkedDequeTest.suite(),
197 >            ConcurrentLinkedQueueTest.suite(),
198 >            ConcurrentSkipListMapTest.suite(),
199 >            ConcurrentSkipListSubMapTest.suite(),
200 >            ConcurrentSkipListSetTest.suite(),
201 >            ConcurrentSkipListSubSetTest.suite(),
202 >            CopyOnWriteArrayListTest.suite(),
203 >            CopyOnWriteArraySetTest.suite(),
204 >            CountDownLatchTest.suite(),
205 >            CyclicBarrierTest.suite(),
206 >            DelayQueueTest.suite(),
207 >            EntryTest.suite(),
208 >            ExchangerTest.suite(),
209 >            ExecutorsTest.suite(),
210 >            ExecutorCompletionServiceTest.suite(),
211 >            FutureTaskTest.suite(),
212 >            LinkedBlockingDequeTest.suite(),
213 >            LinkedBlockingQueueTest.suite(),
214 >            LinkedListTest.suite(),
215 >            LockSupportTest.suite(),
216 >            PriorityBlockingQueueTest.suite(),
217 >            PriorityQueueTest.suite(),
218 >            ReentrantLockTest.suite(),
219 >            ReentrantReadWriteLockTest.suite(),
220 >            ScheduledExecutorTest.suite(),
221 >            ScheduledExecutorSubclassTest.suite(),
222 >            SemaphoreTest.suite(),
223 >            SynchronousQueueTest.suite(),
224 >            SystemTest.suite(),
225 >            ThreadLocalTest.suite(),
226 >            ThreadPoolExecutorTest.suite(),
227 >            ThreadPoolExecutorSubclassTest.suite(),
228 >            ThreadTest.suite(),
229 >            TimeUnitTest.suite(),
230 >            TreeMapTest.suite(),
231 >            TreeSetTest.suite(),
232 >            TreeSubMapTest.suite(),
233 >            TreeSubSetTest.suite());
234      }
235  
236  
# Line 206 | Line 254 | public class JSR166TestCase extends Test
254       */
255      protected void setDelays() {
256          SHORT_DELAY_MS = getShortDelay();
257 <        SMALL_DELAY_MS = SHORT_DELAY_MS * 5;
257 >        SMALL_DELAY_MS  = SHORT_DELAY_MS * 5;
258          MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10;
259 <        LONG_DELAY_MS = SHORT_DELAY_MS * 50;
259 >        LONG_DELAY_MS   = SHORT_DELAY_MS * 50;
260      }
261  
262      /**
263 <     * Flag set true if any threadAssert methods fail
263 >     * The first exception encountered if any threadAssertXXX method fails.
264       */
265 <    volatile boolean threadFailed;
265 >    private final AtomicReference<Throwable> threadFailure
266 >        = new AtomicReference<Throwable>(null);
267  
268      /**
269 <     * Initializes test to indicate that no thread assertions have failed
269 >     * Records an exception so that it can be rethrown later in the test
270 >     * harness thread, triggering a test case failure.  Only the first
271 >     * failure is recorded; subsequent calls to this method from within
272 >     * the same test have no effect.
273       */
274 +    public void threadRecordFailure(Throwable t) {
275 +        threadFailure.compareAndSet(null, t);
276 +    }
277 +
278      public void setUp() {
279          setDelays();
224        threadFailed = false;
280      }
281  
282      /**
283 <     * Triggers test case failure if any thread assertions have failed
284 <     */
285 <    public void tearDown() {
286 <        assertFalse(threadFailed);
283 >     * Triggers test case failure if any thread assertions have failed,
284 >     * by rethrowing, in the test harness thread, any exception recorded
285 >     * earlier by threadRecordFailure.
286 >     */
287 >    public void tearDown() throws Exception {
288 >        Throwable t = threadFailure.get();
289 >        if (t != null) {
290 >            if (t instanceof Error)
291 >                throw (Error) t;
292 >            else if (t instanceof RuntimeException)
293 >                throw (RuntimeException) t;
294 >            else if (t instanceof Exception)
295 >                throw (Exception) t;
296 >            else {
297 >                AssertionFailedError afe =
298 >                    new AssertionFailedError(t.toString());
299 >                afe.initCause(t);
300 >                throw afe;
301 >            }
302 >        }
303      }
304  
305      /**
306 <     * Fail, also setting status to indicate current testcase should fail
306 >     * Just like fail(reason), but additionally recording (using
307 >     * threadRecordFailure) any AssertionFailedError thrown, so that
308 >     * the current testcase will fail.
309       */
310      public void threadFail(String reason) {
311 <        threadFailed = true;
312 <        fail(reason);
311 >        try {
312 >            fail(reason);
313 >        } catch (AssertionFailedError t) {
314 >            threadRecordFailure(t);
315 >            fail(reason);
316 >        }
317      }
318  
319      /**
320 <     * If expression not true, set status to indicate current testcase
321 <     * should fail
320 >     * Just like assertTrue(b), but additionally recording (using
321 >     * threadRecordFailure) any AssertionFailedError thrown, so that
322 >     * the current testcase will fail.
323       */
324      public void threadAssertTrue(boolean b) {
325 <        if (!b) {
248 <            threadFailed = true;
325 >        try {
326              assertTrue(b);
327 +        } catch (AssertionFailedError t) {
328 +            threadRecordFailure(t);
329 +            throw t;
330          }
331      }
332  
333      /**
334 <     * If expression not false, set status to indicate current testcase
335 <     * should fail
334 >     * Just like assertFalse(b), but additionally recording (using
335 >     * threadRecordFailure) any AssertionFailedError thrown, so that
336 >     * the current testcase will fail.
337       */
338      public void threadAssertFalse(boolean b) {
339 <        if (b) {
259 <            threadFailed = true;
339 >        try {
340              assertFalse(b);
341 +        } catch (AssertionFailedError t) {
342 +            threadRecordFailure(t);
343 +            throw t;
344          }
345      }
346  
347      /**
348 <     * If argument not null, set status to indicate current testcase
349 <     * should fail
348 >     * Just like assertNull(x), but additionally recording (using
349 >     * threadRecordFailure) any AssertionFailedError thrown, so that
350 >     * the current testcase will fail.
351       */
352      public void threadAssertNull(Object x) {
353 <        if (x != null) {
270 <            threadFailed = true;
353 >        try {
354              assertNull(x);
355 +        } catch (AssertionFailedError t) {
356 +            threadRecordFailure(t);
357 +            throw t;
358          }
359      }
360  
361      /**
362 <     * If arguments not equal, set status to indicate current testcase
363 <     * should fail
362 >     * Just like assertEquals(x, y), but additionally recording (using
363 >     * threadRecordFailure) any AssertionFailedError thrown, so that
364 >     * the current testcase will fail.
365       */
366      public void threadAssertEquals(long x, long y) {
367 <        if (x != y) {
281 <            threadFailed = true;
367 >        try {
368              assertEquals(x, y);
369 +        } catch (AssertionFailedError t) {
370 +            threadRecordFailure(t);
371 +            throw t;
372          }
373      }
374  
375      /**
376 <     * If arguments not equal, set status to indicate current testcase
377 <     * should fail
376 >     * Just like assertEquals(x, y), but additionally recording (using
377 >     * threadRecordFailure) any AssertionFailedError thrown, so that
378 >     * the current testcase will fail.
379       */
380      public void threadAssertEquals(Object x, Object y) {
381 <        if (x != y && (x == null || !x.equals(y))) {
292 <            threadFailed = true;
381 >        try {
382              assertEquals(x, y);
383 +        } catch (AssertionFailedError t) {
384 +            threadRecordFailure(t);
385 +            throw t;
386 +        } catch (Throwable t) {
387 +            threadUnexpectedException(t);
388          }
389      }
390  
391      /**
392 <     * threadFail with message "should throw exception"
392 >     * Just like assertSame(x, y), but additionally recording (using
393 >     * threadRecordFailure) any AssertionFailedError thrown, so that
394 >     * the current testcase will fail.
395       */
396 <    public void threadShouldThrow() {
397 <        threadFailed = true;
398 <        fail("should throw exception");
396 >    public void threadAssertSame(Object x, Object y) {
397 >        try {
398 >            assertSame(x, y);
399 >        } catch (AssertionFailedError t) {
400 >            threadRecordFailure(t);
401 >            throw t;
402 >        }
403      }
404  
405      /**
406 <     * threadFail with message "should throw" + exceptionName
406 >     * Calls threadFail with message "should throw exception".
407       */
408 <    public void threadShouldThrow(String exceptionName) {
409 <        threadFailed = true;
310 <        fail("should throw " + exceptionName);
408 >    public void threadShouldThrow() {
409 >        threadFail("should throw exception");
410      }
411  
412      /**
413 <     * threadFail with message "Unexpected exception"
413 >     * Calls threadFail with message "should throw" + exceptionName.
414       */
415 <    public void threadUnexpectedException() {
416 <        threadFailed = true;
318 <        fail("Unexpected exception");
415 >    public void threadShouldThrow(String exceptionName) {
416 >        threadFail("should throw " + exceptionName);
417      }
418  
419      /**
420 <     * threadFail with message "Unexpected exception", with argument
420 >     * Records the given exception using {@link #threadRecordFailure},
421 >     * then rethrows the exception, wrapping it in an
422 >     * AssertionFailedError if necessary.
423       */
424 <    public void threadUnexpectedException(Throwable ex) {
425 <        threadFailed = true;
426 <        ex.printStackTrace();
427 <        fail("Unexpected exception: " + ex);
424 >    public void threadUnexpectedException(Throwable t) {
425 >        threadRecordFailure(t);
426 >        t.printStackTrace();
427 >        if (t instanceof RuntimeException)
428 >            throw (RuntimeException) t;
429 >        else if (t instanceof Error)
430 >            throw (Error) t;
431 >        else {
432 >            AssertionFailedError afe =
433 >                new AssertionFailedError("unexpected exception: " + t);
434 >            t.initCause(t);
435 >            throw afe;
436 >        }
437      }
438  
439      /**
440 <     * Wait out termination of a thread pool or fail doing so
440 >     * Waits out termination of a thread pool or fails doing so.
441       */
442      public void joinPool(ExecutorService exec) {
443          try {
444              exec.shutdown();
445 <            assertTrue(exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
445 >            assertTrue("ExecutorService did not terminate in a timely manner",
446 >                       exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
447          } catch (SecurityException ok) {
448              // Allowed in case test doesn't have privs
449          } catch (InterruptedException ie) {
# Line 343 | Line 453 | public class JSR166TestCase extends Test
453  
454  
455      /**
456 <     * fail with message "should throw exception"
456 >     * Fails with message "should throw exception".
457       */
458      public void shouldThrow() {
459          fail("Should throw exception");
460      }
461  
462      /**
463 <     * fail with message "should throw " + exceptionName
463 >     * Fails with message "should throw " + exceptionName.
464       */
465      public void shouldThrow(String exceptionName) {
466          fail("Should throw " + exceptionName);
467      }
468  
469      /**
360     * fail with message "Unexpected exception"
361     */
362    public void unexpectedException() {
363        fail("Unexpected exception");
364    }
365
366    /**
367     * fail with message "Unexpected exception", with argument
368     */
369    public void unexpectedException(Throwable ex) {
370        ex.printStackTrace();
371        fail("Unexpected exception: " + ex);
372    }
373
374
375    /**
470       * The number of elements to place in collections, arrays, etc.
471       */
472      public static final int SIZE = 20;
# Line 483 | Line 577 | public class JSR166TestCase extends Test
577      }
578  
579      /**
580 <     * Sleep until the timeout has elapsed, or interrupted.
580 >     * Sleeps until the given time has elapsed.
581 >     * Throws AssertionFailedError if interrupted.
582 >     */
583 >    void sleep(long millis) {
584 >        try {
585 >            Thread.sleep(millis);
586 >        } catch (InterruptedException ie) {
587 >            AssertionFailedError afe =
588 >                new AssertionFailedError("Unexpected InterruptedException");
589 >            afe.initCause(ie);
590 >            throw afe;
591 >        }
592 >    }
593 >
594 >    /**
595 >     * Sleeps until the timeout has elapsed, or interrupted.
596       * Does <em>NOT</em> throw InterruptedException.
597       */
598      void sleepTillInterrupted(long timeoutMillis) {
# Line 493 | Line 602 | public class JSR166TestCase extends Test
602      }
603  
604      /**
605 <     * Returns a new started Thread running the given runnable.
605 >     * Returns a new started daemon Thread running the given runnable.
606       */
607      Thread newStartedThread(Runnable runnable) {
608          Thread t = new Thread(runnable);
609 +        t.setDaemon(true);
610          t.start();
611          return t;
612      }
613  
614 +    /**
615 +     * Waits for the specified time (in milliseconds) for the thread
616 +     * to terminate (using {@link Thread#join(long)}), else interrupts
617 +     * the thread (in the hope that it may terminate later) and fails.
618 +     */
619 +    void awaitTermination(Thread t, long timeoutMillis) {
620 +        try {
621 +            t.join(timeoutMillis);
622 +        } catch (InterruptedException ie) {
623 +            threadUnexpectedException(ie);
624 +        } finally {
625 +            if (t.isAlive()) {
626 +                t.interrupt();
627 +                fail("Test timed out");
628 +            }
629 +        }
630 +    }
631 +
632      // Some convenient Runnable classes
633  
634      public abstract class CheckedRunnable implements Runnable {
# Line 577 | Line 705 | public class JSR166TestCase extends Test
705                  return realCall();
706              } catch (Throwable t) {
707                  threadUnexpectedException(t);
708 +                return null;
709              }
581            return null;
710          }
711      }
712  
713 <    public abstract class CheckedInterruptedCallable<T> implements Callable<T> {
713 >    public abstract class CheckedInterruptedCallable<T>
714 >        implements Callable<T> {
715          protected abstract T realCall() throws Throwable;
716  
717          public final T call() {
# Line 663 | Line 792 | public class JSR166TestCase extends Test
792          }
793      }
794  
666    public class SmallInterruptedRunnable extends CheckedInterruptedRunnable {
667        protected void realRun() throws InterruptedException {
668            Thread.sleep(SMALL_DELAY_MS);
669        }
670    }
671
795      public class MediumRunnable extends CheckedRunnable {
796          protected void realRun() throws Throwable {
797              Thread.sleep(MEDIUM_DELAY_MS);
# Line 706 | Line 829 | public class JSR166TestCase extends Test
829          }
830      }
831  
832 +    public interface TrackedRunnable extends Runnable {
833 +        boolean isDone();
834 +    }
835 +
836 +    public static TrackedRunnable trackedRunnable(final long timeoutMillis) {
837 +        return new TrackedRunnable() {
838 +                private volatile boolean done = false;
839 +                public boolean isDone() { return done; }
840 +                public void run() {
841 +                    try {
842 +                        Thread.sleep(timeoutMillis);
843 +                        done = true;
844 +                    } catch (InterruptedException ok) {}
845 +                }
846 +            };
847 +    }
848 +
849      public static class TrackedShortRunnable implements Runnable {
850          public volatile boolean done = false;
851          public void run() {
852              try {
853 +                Thread.sleep(SHORT_DELAY_MS);
854 +                done = true;
855 +            } catch (InterruptedException ok) {}
856 +        }
857 +    }
858 +
859 +    public static class TrackedSmallRunnable implements Runnable {
860 +        public volatile boolean done = false;
861 +        public void run() {
862 +            try {
863                  Thread.sleep(SMALL_DELAY_MS);
864                  done = true;
865              } catch (InterruptedException ok) {}
# Line 754 | Line 904 | public class JSR166TestCase extends Test
904          }
905      }
906  
907 +    /**
908 +     * Analog of CheckedRunnable for RecursiveAction
909 +     */
910 +    public abstract class CheckedRecursiveAction extends RecursiveAction {
911 +        protected abstract void realCompute() throws Throwable;
912 +
913 +        public final void compute() {
914 +            try {
915 +                realCompute();
916 +            } catch (Throwable t) {
917 +                threadUnexpectedException(t);
918 +            }
919 +        }
920 +    }
921 +
922 +    /**
923 +     * Analog of CheckedCallable for RecursiveTask
924 +     */
925 +    public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> {
926 +        protected abstract T realCompute() throws Throwable;
927 +
928 +        public final T compute() {
929 +            try {
930 +                return realCompute();
931 +            } catch (Throwable t) {
932 +                threadUnexpectedException(t);
933 +                return null;
934 +            }
935 +        }
936 +    }
937  
938      /**
939       * For use as RejectedExecutionHandler in constructors
# Line 763 | Line 943 | public class JSR166TestCase extends Test
943                                        ThreadPoolExecutor executor) {}
944      }
945  
946 +    /**
947 +     * A CyclicBarrier that fails with AssertionFailedErrors instead
948 +     * of throwing checked exceptions.
949 +     */
950 +    public class CheckedBarrier extends CyclicBarrier {
951 +        public CheckedBarrier(int parties) { super(parties); }
952 +
953 +        public int await() {
954 +            try {
955 +                return super.await();
956 +            } catch (Exception e) {
957 +                AssertionFailedError afe =
958 +                    new AssertionFailedError("Unexpected exception: " + e);
959 +                afe.initCause(e);
960 +                throw afe;
961 +            }
962 +        }
963 +    }
964 +
965   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines