ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinTaskTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ForkJoinTaskTest.java (file contents):
Revision 1.12 by jsr166, Mon Sep 13 07:22:29 2010 UTC vs.
Revision 1.27 by jsr166, Mon Nov 22 07:50:50 2010 UTC

# Line 3 | Line 3
3   * Expert Group and released to the public domain, as explained at
4   * http://creativecommons.org/licenses/publicdomain
5   */
6 import java.util.*;
7 import java.util.concurrent.Executor;
8 import java.util.concurrent.Executors;
9 import java.util.concurrent.ExecutorService;
10 import java.util.concurrent.AbstractExecutorService;
11 import java.util.concurrent.CountDownLatch;
12 import java.util.concurrent.Callable;
13 import java.util.concurrent.Future;
6   import java.util.concurrent.ExecutionException;
7   import java.util.concurrent.CancellationException;
16 import java.util.concurrent.RejectedExecutionException;
8   import java.util.concurrent.ForkJoinPool;
9   import java.util.concurrent.ForkJoinTask;
10   import java.util.concurrent.ForkJoinWorkerThread;
11   import java.util.concurrent.RecursiveAction;
21 import java.util.concurrent.RecursiveTask;
12   import java.util.concurrent.TimeUnit;
13 + import java.util.concurrent.TimeoutException;
14 + import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
15 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
16 + import static java.util.concurrent.TimeUnit.SECONDS;
17 + import java.util.HashSet;
18   import junit.framework.*;
24 import java.util.concurrent.TimeUnit;
25 import java.util.concurrent.atomic.*;
26 import java.util.*;
19  
20   public class ForkJoinTaskTest extends JSR166TestCase {
21  
# Line 35 | Line 27 | public class ForkJoinTaskTest extends JS
27          return new TestSuite(ForkJoinTaskTest.class);
28      }
29  
30 <    /**
30 >    // Runs with "mainPool" use > 1 thread. singletonPool tests use 1
31 >    static final int mainPoolSize =
32 >        Math.max(2, Runtime.getRuntime().availableProcessors());
33 >
34 >    private static ForkJoinPool mainPool() {
35 >        return new ForkJoinPool(mainPoolSize);
36 >    }
37 >
38 >    private static ForkJoinPool singletonPool() {
39 >        return new ForkJoinPool(1);
40 >    }
41 >
42 >    private static ForkJoinPool asyncSingletonPool() {
43 >        return new ForkJoinPool(1,
44 >                                ForkJoinPool.defaultForkJoinWorkerThreadFactory,
45 >                                null, true);
46 >    }
47 >
48 >    private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
49 >        try {
50 >            assertFalse(a.isDone());
51 >            assertFalse(a.isCompletedNormally());
52 >            assertFalse(a.isCompletedAbnormally());
53 >            assertFalse(a.isCancelled());
54 >            assertNull(a.getException());
55 >            assertNull(a.getRawResult());
56 >
57 >            assertNull(pool.invoke(a));
58 >
59 >            assertTrue(a.isDone());
60 >            assertTrue(a.isCompletedNormally());
61 >            assertFalse(a.isCompletedAbnormally());
62 >            assertFalse(a.isCancelled());
63 >            assertNull(a.getException());
64 >            assertNull(a.getRawResult());
65 >        } finally {
66 >            joinPool(pool);
67 >        }
68 >    }
69 >
70 >    void checkNotDone(ForkJoinTask a) {
71 >        assertFalse(a.isDone());
72 >        assertFalse(a.isCompletedNormally());
73 >        assertFalse(a.isCompletedAbnormally());
74 >        assertFalse(a.isCancelled());
75 >        assertNull(a.getException());
76 >        assertNull(a.getRawResult());
77 >
78 >        try {
79 >            a.get(0L, SECONDS);
80 >            shouldThrow();
81 >        } catch (TimeoutException success) {
82 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
83 >    }
84 >
85 >    <T> void checkCompletedNormally(ForkJoinTask<T> a) {
86 >        checkCompletedNormally(a, null);
87 >    }
88 >
89 >    <T> void checkCompletedNormally(ForkJoinTask<T> a, T expected) {
90 >        assertTrue(a.isDone());
91 >        assertFalse(a.isCancelled());
92 >        assertTrue(a.isCompletedNormally());
93 >        assertFalse(a.isCompletedAbnormally());
94 >        assertNull(a.getException());
95 >        assertSame(expected, a.getRawResult());
96 >        assertSame(expected, a.join());
97 >        assertFalse(a.cancel(false));
98 >        assertFalse(a.cancel(true));
99 >        try {
100 >            assertSame(expected, a.get());
101 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
102 >        try {
103 >            assertSame(expected, a.get(5L, SECONDS));
104 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
105 >    }
106 >
107 >    void checkCancelled(ForkJoinTask a) {
108 >        assertTrue(a.isDone());
109 >        assertTrue(a.isCancelled());
110 >        assertFalse(a.isCompletedNormally());
111 >        assertTrue(a.isCompletedAbnormally());
112 >        assertTrue(a.getException() instanceof CancellationException);
113 >        assertNull(a.getRawResult());
114 >        assertTrue(a.cancel(false));
115 >        assertTrue(a.cancel(true));
116 >
117 >        try {
118 >            a.join();
119 >            shouldThrow();
120 >        } catch (CancellationException success) {
121 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
122 >
123 >        try {
124 >            a.get();
125 >            shouldThrow();
126 >        } catch (CancellationException success) {
127 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
128 >
129 >        try {
130 >            a.get(5L, SECONDS);
131 >            shouldThrow();
132 >        } catch (CancellationException success) {
133 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
134 >    }
135 >
136 >    void checkCompletedAbnormally(ForkJoinTask a, Throwable t) {
137 >        assertTrue(a.isDone());
138 >        assertFalse(a.isCancelled());
139 >        assertFalse(a.isCompletedNormally());
140 >        assertTrue(a.isCompletedAbnormally());
141 >        assertSame(t, a.getException());
142 >        assertNull(a.getRawResult());
143 >        assertFalse(a.cancel(false));
144 >        assertFalse(a.cancel(true));
145 >
146 >        try {
147 >            a.join();
148 >            shouldThrow();
149 >        } catch (Throwable expected) {
150 >            assertSame(t, expected);
151 >        }
152 >
153 >        try {
154 >            a.get();
155 >            shouldThrow();
156 >        } catch (ExecutionException success) {
157 >            assertSame(t, success.getCause());
158 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
159 >
160 >        try {
161 >            a.get(5L, SECONDS);
162 >            shouldThrow();
163 >        } catch (ExecutionException success) {
164 >            assertSame(t, success.getCause());
165 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
166 >    }
167 >
168 >    /*
169       * Testing coverage notes:
170       *
171       * To test extension methods and overrides, most tests use
# Line 43 | Line 173 | public class ForkJoinTaskTest extends JS
173       * differently than supplied Recursive forms.
174       */
175  
46    static final ForkJoinPool mainPool = new ForkJoinPool();
47    static final ForkJoinPool singletonPool = new ForkJoinPool(1);
48    static final ForkJoinPool asyncSingletonPool =
49        new ForkJoinPool(1, ForkJoinPool.defaultForkJoinWorkerThreadFactory,
50                         null, true);
176      static final class FJException extends RuntimeException {
177          FJException() { super(); }
178      }
179  
180 <    static abstract class BinaryAsyncAction extends ForkJoinTask<Void> {
180 >    abstract static class BinaryAsyncAction extends ForkJoinTask<Void> {
181          private volatile int controlState;
182  
183          static final AtomicIntegerFieldUpdater<BinaryAsyncAction> controlStateUpdater =
# Line 224 | Line 349 | public class ForkJoinTaskTest extends JS
349      /**
350       * invoke returns when task completes normally.
351       * isCompletedAbnormally and isCancelled return false for normally
352 <     * completed tasks. getRawResult of a RecursiveAction returns null;
352 >     * completed tasks; getRawResult returns null.
353       */
354      public void testInvoke() {
355 <        RecursiveAction a = new RecursiveAction() {
356 <            public void compute() {
355 >        RecursiveAction a = new CheckedRecursiveAction() {
356 >            public void realCompute() {
357                  AsyncFib f = new AsyncFib(8);
358 <                f.invoke();
359 <                threadAssertTrue(f.number == 21);
360 <                threadAssertTrue(f.isDone());
236 <                threadAssertFalse(f.isCancelled());
237 <                threadAssertFalse(f.isCompletedAbnormally());
238 <                threadAssertTrue(f.getRawResult() == null);
358 >                assertNull(f.invoke());
359 >                assertEquals(21, f.number);
360 >                checkCompletedNormally(f);
361              }};
362 <        mainPool.invoke(a);
362 >        testInvokeOnPool(mainPool(), a);
363      }
364  
365      /**
# Line 246 | Line 368 | public class ForkJoinTaskTest extends JS
368       * completed tasks
369       */
370      public void testQuietlyInvoke() {
371 <        RecursiveAction a = new RecursiveAction() {
372 <            public void compute() {
371 >        RecursiveAction a = new CheckedRecursiveAction() {
372 >            public void realCompute() {
373                  AsyncFib f = new AsyncFib(8);
374                  f.quietlyInvoke();
375 <                threadAssertTrue(f.number == 21);
376 <                threadAssertTrue(f.isDone());
255 <                threadAssertFalse(f.isCancelled());
256 <                threadAssertFalse(f.isCompletedAbnormally());
257 <                threadAssertTrue(f.getRawResult() == null);
375 >                assertEquals(21, f.number);
376 >                checkCompletedNormally(f);
377              }};
378 <        mainPool.invoke(a);
378 >        testInvokeOnPool(mainPool(), a);
379      }
380  
381      /**
382       * join of a forked task returns when task completes
383       */
384      public void testForkJoin() {
385 <        RecursiveAction a = new RecursiveAction() {
386 <            public void compute() {
385 >        RecursiveAction a = new CheckedRecursiveAction() {
386 >            public void realCompute() {
387                  AsyncFib f = new AsyncFib(8);
388 <                f.fork();
389 <                f.join();
390 <                threadAssertTrue(f.number == 21);
391 <                threadAssertTrue(f.isDone());
273 <                threadAssertTrue(f.getRawResult() == null);
388 >                assertSame(f, f.fork());
389 >                assertNull(f.join());
390 >                assertEquals(21, f.number);
391 >                checkCompletedNormally(f);
392              }};
393 <        mainPool.invoke(a);
393 >        testInvokeOnPool(mainPool(), a);
394      }
395  
396      /**
397       * get of a forked task returns when task completes
398       */
399      public void testForkGet() {
400 <        RecursiveAction a = new RecursiveAction() {
401 <            public void compute() {
402 <                try {
403 <                    AsyncFib f = new AsyncFib(8);
404 <                    f.fork();
405 <                    f.get();
406 <                    threadAssertTrue(f.number == 21);
289 <                    threadAssertTrue(f.isDone());
290 <                } catch (Exception ex) {
291 <                    unexpectedException(ex);
292 <                }
400 >        RecursiveAction a = new CheckedRecursiveAction() {
401 >            public void realCompute() throws Exception {
402 >                AsyncFib f = new AsyncFib(8);
403 >                assertSame(f, f.fork());
404 >                assertNull(f.get());
405 >                assertEquals(21, f.number);
406 >                checkCompletedNormally(f);
407              }};
408 <        mainPool.invoke(a);
408 >        testInvokeOnPool(mainPool(), a);
409      }
410  
411      /**
412       * timed get of a forked task returns when task completes
413       */
414      public void testForkTimedGet() {
415 <        RecursiveAction a = new RecursiveAction() {
416 <            public void compute() {
417 <                try {
418 <                    AsyncFib f = new AsyncFib(8);
419 <                    f.fork();
420 <                    f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
421 <                    threadAssertTrue(f.number == 21);
308 <                    threadAssertTrue(f.isDone());
309 <                } catch (Exception ex) {
310 <                    unexpectedException(ex);
311 <                }
415 >        RecursiveAction a = new CheckedRecursiveAction() {
416 >            public void realCompute() throws Exception {
417 >                AsyncFib f = new AsyncFib(8);
418 >                assertSame(f, f.fork());
419 >                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
420 >                assertEquals(21, f.number);
421 >                checkCompletedNormally(f);
422              }};
423 <        mainPool.invoke(a);
423 >        testInvokeOnPool(mainPool(), a);
424      }
425  
426      /**
427       * timed get with null time unit throws NPE
428       */
429      public void testForkTimedGetNPE() {
430 <        RecursiveAction a = new RecursiveAction() {
431 <            public void compute() {
430 >        RecursiveAction a = new CheckedRecursiveAction() {
431 >            public void realCompute() throws Exception {
432 >                AsyncFib f = new AsyncFib(8);
433 >                assertSame(f, f.fork());
434                  try {
323                    AsyncFib f = new AsyncFib(8);
324                    f.fork();
435                      f.get(5L, null);
436                      shouldThrow();
437 <                } catch (NullPointerException success) {
328 <                } catch (Exception ex) {
329 <                    unexpectedException(ex);
330 <                }
437 >                } catch (NullPointerException success) {}
438              }};
439 <        mainPool.invoke(a);
439 >        testInvokeOnPool(mainPool(), a);
440      }
441  
442      /**
443       * quietlyJoin of a forked task returns when task completes
444       */
445      public void testForkQuietlyJoin() {
446 <        RecursiveAction a = new RecursiveAction() {
447 <            public void compute() {
446 >        RecursiveAction a = new CheckedRecursiveAction() {
447 >            public void realCompute() {
448                  AsyncFib f = new AsyncFib(8);
449 <                f.fork();
449 >                assertSame(f, f.fork());
450                  f.quietlyJoin();
451 <                threadAssertTrue(f.number == 21);
452 <                threadAssertTrue(f.isDone());
451 >                assertEquals(21, f.number);
452 >                checkCompletedNormally(f);
453              }};
454 <        mainPool.invoke(a);
454 >        testInvokeOnPool(mainPool(), a);
455      }
456  
457  
# Line 353 | Line 460 | public class ForkJoinTaskTest extends JS
460       * getQueuedTaskCount returns 0 when quiescent
461       */
462      public void testForkHelpQuiesce() {
463 <        RecursiveAction a = new RecursiveAction() {
464 <            public void compute() {
463 >        RecursiveAction a = new CheckedRecursiveAction() {
464 >            public void realCompute() {
465                  AsyncFib f = new AsyncFib(8);
466 <                f.fork();
466 >                assertSame(f, f.fork());
467                  f.helpQuiesce();
468 <                threadAssertTrue(f.number == 21);
469 <                threadAssertTrue(f.isDone());
470 <                threadAssertTrue(getQueuedTaskCount() == 0);
468 >                assertEquals(21, f.number);
469 >                assertEquals(0, getQueuedTaskCount());
470 >                checkCompletedNormally(f);
471              }};
472 <        mainPool.invoke(a);
472 >        testInvokeOnPool(mainPool(), a);
473      }
474  
475  
# Line 370 | Line 477 | public class ForkJoinTaskTest extends JS
477       * invoke task throws exception when task completes abnormally
478       */
479      public void testAbnormalInvoke() {
480 <        RecursiveAction a = new RecursiveAction() {
481 <            public void compute() {
480 >        RecursiveAction a = new CheckedRecursiveAction() {
481 >            public void realCompute() {
482 >                FailingAsyncFib f = new FailingAsyncFib(8);
483                  try {
376                    FailingAsyncFib f = new FailingAsyncFib(8);
484                      f.invoke();
485                      shouldThrow();
486                  } catch (FJException success) {
487 +                    checkCompletedAbnormally(f, success);
488                  }
489              }};
490 <        mainPool.invoke(a);
490 >        testInvokeOnPool(mainPool(), a);
491      }
492  
493      /**
494       * quietlyInvoke task returns when task completes abnormally
495       */
496      public void testAbnormalQuietlyInvoke() {
497 <        RecursiveAction a = new RecursiveAction() {
498 <            public void compute() {
497 >        RecursiveAction a = new CheckedRecursiveAction() {
498 >            public void realCompute() {
499                  FailingAsyncFib f = new FailingAsyncFib(8);
500                  f.quietlyInvoke();
501 <                threadAssertTrue(f.isDone());
501 >                assertTrue(f.getException() instanceof FJException);
502 >                checkCompletedAbnormally(f, f.getException());
503              }};
504 <        mainPool.invoke(a);
504 >        testInvokeOnPool(mainPool(), a);
505      }
506  
507      /**
508       * join of a forked task throws exception when task completes abnormally
509       */
510      public void testAbnormalForkJoin() {
511 <        RecursiveAction a = new RecursiveAction() {
512 <            public void compute() {
511 >        RecursiveAction a = new CheckedRecursiveAction() {
512 >            public void realCompute() {
513 >                FailingAsyncFib f = new FailingAsyncFib(8);
514 >                assertSame(f, f.fork());
515                  try {
405                    FailingAsyncFib f = new FailingAsyncFib(8);
406                    f.fork();
516                      f.join();
517                      shouldThrow();
518                  } catch (FJException success) {
519 +                    checkCompletedAbnormally(f, success);
520                  }
521              }};
522 <        mainPool.invoke(a);
522 >        testInvokeOnPool(mainPool(), a);
523      }
524  
525      /**
526       * get of a forked task throws exception when task completes abnormally
527       */
528      public void testAbnormalForkGet() {
529 <        RecursiveAction a = new RecursiveAction() {
530 <            public void compute() {
529 >        RecursiveAction a = new CheckedRecursiveAction() {
530 >            public void realCompute() throws Exception {
531 >                FailingAsyncFib f = new FailingAsyncFib(8);
532 >                assertSame(f, f.fork());
533                  try {
422                    FailingAsyncFib f = new FailingAsyncFib(8);
423                    f.fork();
534                      f.get();
535                      shouldThrow();
536                  } catch (ExecutionException success) {
537 <                } catch (Exception ex) {
538 <                    unexpectedException(ex);
537 >                    Throwable cause = success.getCause();
538 >                    assertTrue(cause instanceof FJException);
539 >                    checkCompletedAbnormally(f, cause);
540                  }
541              }};
542 <        mainPool.invoke(a);
542 >        testInvokeOnPool(mainPool(), a);
543      }
544  
545      /**
546       * timed get of a forked task throws exception when task completes abnormally
547       */
548      public void testAbnormalForkTimedGet() {
549 <        RecursiveAction a = new RecursiveAction() {
550 <            public void compute() {
549 >        RecursiveAction a = new CheckedRecursiveAction() {
550 >            public void realCompute() throws Exception {
551 >                FailingAsyncFib f = new FailingAsyncFib(8);
552 >                assertSame(f, f.fork());
553                  try {
554 <                    FailingAsyncFib f = new FailingAsyncFib(8);
442 <                    f.fork();
443 <                    f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
554 >                    f.get(LONG_DELAY_MS, MILLISECONDS);
555                      shouldThrow();
556                  } catch (ExecutionException success) {
557 <                } catch (Exception ex) {
558 <                    unexpectedException(ex);
557 >                    Throwable cause = success.getCause();
558 >                    assertTrue(cause instanceof FJException);
559 >                    checkCompletedAbnormally(f, cause);
560                  }
561              }};
562 <        mainPool.invoke(a);
562 >        testInvokeOnPool(mainPool(), a);
563      }
564  
565      /**
566       * quietlyJoin of a forked task returns when task completes abnormally
567       */
568      public void testAbnormalForkQuietlyJoin() {
569 <        RecursiveAction a = new RecursiveAction() {
570 <            public void compute() {
569 >        RecursiveAction a = new CheckedRecursiveAction() {
570 >            public void realCompute() {
571                  FailingAsyncFib f = new FailingAsyncFib(8);
572 <                f.fork();
572 >                assertSame(f, f.fork());
573                  f.quietlyJoin();
574 <                threadAssertTrue(f.isDone());
575 <                threadAssertTrue(f.isCompletedAbnormally());
464 <                threadAssertTrue(f.getException() instanceof FJException);
574 >                assertTrue(f.getException() instanceof FJException);
575 >                checkCompletedAbnormally(f, f.getException());
576              }};
577 <        mainPool.invoke(a);
577 >        testInvokeOnPool(mainPool(), a);
578      }
579  
580      /**
581       * invoke task throws exception when task cancelled
582       */
583      public void testCancelledInvoke() {
584 <        RecursiveAction a = new RecursiveAction() {
585 <            public void compute() {
584 >        RecursiveAction a = new CheckedRecursiveAction() {
585 >            public void realCompute() {
586 >                AsyncFib f = new AsyncFib(8);
587 >                assertTrue(f.cancel(true));
588                  try {
476                    AsyncFib f = new AsyncFib(8);
477                    f.cancel(true);
589                      f.invoke();
590                      shouldThrow();
591                  } catch (CancellationException success) {
592 +                    checkCancelled(f);
593                  }
594              }};
595 <        mainPool.invoke(a);
595 >        testInvokeOnPool(mainPool(), a);
596      }
597  
598      /**
599       * join of a forked task throws exception when task cancelled
600       */
601      public void testCancelledForkJoin() {
602 <        RecursiveAction a = new RecursiveAction() {
603 <            public void compute() {
602 >        RecursiveAction a = new CheckedRecursiveAction() {
603 >            public void realCompute() {
604 >                AsyncFib f = new AsyncFib(8);
605 >                assertTrue(f.cancel(true));
606 >                assertSame(f, f.fork());
607                  try {
493                    AsyncFib f = new AsyncFib(8);
494                    f.cancel(true);
495                    f.fork();
608                      f.join();
609                      shouldThrow();
610                  } catch (CancellationException success) {
611 +                    checkCancelled(f);
612                  }
613              }};
614 <        mainPool.invoke(a);
614 >        testInvokeOnPool(mainPool(), a);
615      }
616  
617      /**
618       * get of a forked task throws exception when task cancelled
619       */
620      public void testCancelledForkGet() {
621 <        RecursiveAction a = new RecursiveAction() {
622 <            public void compute() {
621 >        RecursiveAction a = new CheckedRecursiveAction() {
622 >            public void realCompute() throws Exception {
623 >                AsyncFib f = new AsyncFib(8);
624 >                assertTrue(f.cancel(true));
625 >                assertSame(f, f.fork());
626                  try {
511                    AsyncFib f = new AsyncFib(8);
512                    f.cancel(true);
513                    f.fork();
627                      f.get();
628                      shouldThrow();
629                  } catch (CancellationException success) {
630 <                } catch (Exception ex) {
518 <                    unexpectedException(ex);
630 >                    checkCancelled(f);
631                  }
632              }};
633 <        mainPool.invoke(a);
633 >        testInvokeOnPool(mainPool(), a);
634      }
635  
636      /**
637       * timed get of a forked task throws exception when task cancelled
638       */
639 <    public void testCancelledForkTimedGet() {
640 <        RecursiveAction a = new RecursiveAction() {
641 <            public void compute() {
642 <                try {
643 <                    AsyncFib f = new AsyncFib(8);
644 <                    f.cancel(true);
645 <                    f.fork();
646 <                    f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
639 >    public void testCancelledForkTimedGet() throws Exception {
640 >        RecursiveAction a = new CheckedRecursiveAction() {
641 >            public void realCompute() throws Exception {
642 >                AsyncFib f = new AsyncFib(8);
643 >                assertTrue(f.cancel(true));
644 >                assertSame(f, f.fork());
645 >                try {
646 >                    f.get(LONG_DELAY_MS, MILLISECONDS);
647                      shouldThrow();
648                  } catch (CancellationException success) {
649 <                } catch (Exception ex) {
538 <                    unexpectedException(ex);
649 >                    checkCancelled(f);
650                  }
651              }};
652 <        mainPool.invoke(a);
652 >        testInvokeOnPool(mainPool(), a);
653      }
654  
655      /**
656       * quietlyJoin of a forked task returns when task cancelled
657       */
658      public void testCancelledForkQuietlyJoin() {
659 <        RecursiveAction a = new RecursiveAction() {
660 <            public void compute() {
659 >        RecursiveAction a = new CheckedRecursiveAction() {
660 >            public void realCompute() {
661                  AsyncFib f = new AsyncFib(8);
662 <                f.cancel(true);
663 <                f.fork();
662 >                assertTrue(f.cancel(true));
663 >                assertSame(f, f.fork());
664                  f.quietlyJoin();
665 <                threadAssertTrue(f.isDone());
555 <                threadAssertTrue(f.isCompletedAbnormally());
556 <                threadAssertTrue(f.getException() instanceof CancellationException);
665 >                checkCancelled(f);
666              }};
667 <        mainPool.invoke(a);
667 >        testInvokeOnPool(mainPool(), a);
668      }
669  
670      /**
671       * getPool of executing task returns its pool
672       */
673      public void testGetPool() {
674 <        RecursiveAction a = new RecursiveAction() {
675 <            public void compute() {
676 <                threadAssertTrue(getPool() == mainPool);
674 >        final ForkJoinPool mainPool = mainPool();
675 >        RecursiveAction a = new CheckedRecursiveAction() {
676 >            public void realCompute() {
677 >                assertSame(mainPool, getPool());
678              }};
679 <        mainPool.invoke(a);
679 >        testInvokeOnPool(mainPool, a);
680      }
681  
682      /**
683       * getPool of non-FJ task returns null
684       */
685      public void testGetPool2() {
686 <        RecursiveAction a = new RecursiveAction() {
687 <            public void compute() {
688 <                threadAssertTrue(getPool() == null);
686 >        RecursiveAction a = new CheckedRecursiveAction() {
687 >            public void realCompute() {
688 >                assertNull(getPool());
689              }};
690 <        a.invoke();
690 >        assertNull(a.invoke());
691      }
692  
693      /**
694       * inForkJoinPool of executing task returns true
695       */
696      public void testInForkJoinPool() {
697 <        RecursiveAction a = new RecursiveAction() {
698 <            public void compute() {
699 <                threadAssertTrue(inForkJoinPool());
697 >        RecursiveAction a = new CheckedRecursiveAction() {
698 >            public void realCompute() {
699 >                assertTrue(inForkJoinPool());
700              }};
701 <        mainPool.invoke(a);
701 >        testInvokeOnPool(mainPool(), a);
702      }
703  
704      /**
705       * inForkJoinPool of non-FJ task returns false
706       */
707      public void testInForkJoinPool2() {
708 <        RecursiveAction a = new RecursiveAction() {
709 <            public void compute() {
710 <                threadAssertTrue(!inForkJoinPool());
708 >        RecursiveAction a = new CheckedRecursiveAction() {
709 >            public void realCompute() {
710 >                assertFalse(inForkJoinPool());
711              }};
712 <        a.invoke();
712 >        assertNull(a.invoke());
713      }
714  
715      /**
716       * setRawResult(null) succeeds
717       */
718      public void testSetRawResult() {
719 <        RecursiveAction a = new RecursiveAction() {
720 <            public void compute() {
719 >        RecursiveAction a = new CheckedRecursiveAction() {
720 >            public void realCompute() {
721                  setRawResult(null);
722 +                assertNull(getRawResult());
723              }};
724 <        a.invoke();
724 >        assertNull(a.invoke());
725      }
726  
727      /**
728       * invoke task throws exception after invoking completeExceptionally
729       */
730      public void testCompleteExceptionally() {
731 <        RecursiveAction a = new RecursiveAction() {
732 <            public void compute() {
731 >        RecursiveAction a = new CheckedRecursiveAction() {
732 >            public void realCompute() {
733 >                AsyncFib f = new AsyncFib(8);
734 >                f.completeExceptionally(new FJException());
735                  try {
623                    AsyncFib f = new AsyncFib(8);
624                    f.completeExceptionally(new FJException());
736                      f.invoke();
737                      shouldThrow();
738                  } catch (FJException success) {
739 +                    checkCompletedAbnormally(f, success);
740                  }
741              }};
742 <        mainPool.invoke(a);
742 >        testInvokeOnPool(mainPool(), a);
743      }
744  
745      /**
746       * invokeAll(t1, t2) invokes all task arguments
747       */
748      public void testInvokeAll2() {
749 <        RecursiveAction a = new RecursiveAction() {
750 <            public void compute() {
749 >        RecursiveAction a = new CheckedRecursiveAction() {
750 >            public void realCompute() {
751                  AsyncFib f = new AsyncFib(8);
752                  AsyncFib g = new AsyncFib(9);
753                  invokeAll(f, g);
754 <                threadAssertTrue(f.isDone());
755 <                threadAssertTrue(f.number == 21);
756 <                threadAssertTrue(g.isDone());
757 <                threadAssertTrue(g.number == 34);
754 >                assertEquals(21, f.number);
755 >                assertEquals(34, g.number);
756 >                checkCompletedNormally(f);
757 >                checkCompletedNormally(g);
758              }};
759 <        mainPool.invoke(a);
759 >        testInvokeOnPool(mainPool(), a);
760      }
761  
762      /**
763       * invokeAll(tasks) with 1 argument invokes task
764       */
765      public void testInvokeAll1() {
766 <        RecursiveAction a = new RecursiveAction() {
767 <            public void compute() {
766 >        RecursiveAction a = new CheckedRecursiveAction() {
767 >            public void realCompute() {
768                  AsyncFib f = new AsyncFib(8);
769                  invokeAll(f);
770 <                threadAssertTrue(f.isDone());
771 <                threadAssertTrue(f.number == 21);
770 >                checkCompletedNormally(f);
771 >                assertEquals(21, f.number);
772              }};
773 <        mainPool.invoke(a);
773 >        testInvokeOnPool(mainPool(), a);
774      }
775  
776      /**
777       * invokeAll(tasks) with > 2 argument invokes tasks
778       */
779      public void testInvokeAll3() {
780 <        RecursiveAction a = new RecursiveAction() {
781 <            public void compute() {
780 >        RecursiveAction a = new CheckedRecursiveAction() {
781 >            public void realCompute() {
782                  AsyncFib f = new AsyncFib(8);
783                  AsyncFib g = new AsyncFib(9);
784                  AsyncFib h = new AsyncFib(7);
785                  invokeAll(f, g, h);
786 <                threadAssertTrue(f.isDone());
787 <                threadAssertTrue(f.number == 21);
788 <                threadAssertTrue(g.isDone());
789 <                threadAssertTrue(g.number == 34);
790 <                threadAssertTrue(h.isDone());
791 <                threadAssertTrue(h.number == 13);
786 >                assertEquals(21, f.number);
787 >                assertEquals(34, g.number);
788 >                assertEquals(13, h.number);
789 >                checkCompletedNormally(f);
790 >                checkCompletedNormally(g);
791 >                checkCompletedNormally(h);
792              }};
793 <        mainPool.invoke(a);
793 >        testInvokeOnPool(mainPool(), a);
794      }
795  
796      /**
797       * invokeAll(collection) invokes all tasks in the collection
798       */
799      public void testInvokeAllCollection() {
800 <        RecursiveAction a = new RecursiveAction() {
801 <            public void compute() {
800 >        RecursiveAction a = new CheckedRecursiveAction() {
801 >            public void realCompute() {
802                  AsyncFib f = new AsyncFib(8);
803                  AsyncFib g = new AsyncFib(9);
804                  AsyncFib h = new AsyncFib(7);
# Line 695 | Line 807 | public class ForkJoinTaskTest extends JS
807                  set.add(g);
808                  set.add(h);
809                  invokeAll(set);
810 <                threadAssertTrue(f.isDone());
811 <                threadAssertTrue(f.number == 21);
812 <                threadAssertTrue(g.isDone());
813 <                threadAssertTrue(g.number == 34);
814 <                threadAssertTrue(h.isDone());
815 <                threadAssertTrue(h.number == 13);
810 >                assertEquals(21, f.number);
811 >                assertEquals(34, g.number);
812 >                assertEquals(13, h.number);
813 >                checkCompletedNormally(f);
814 >                checkCompletedNormally(g);
815 >                checkCompletedNormally(h);
816              }};
817 <        mainPool.invoke(a);
817 >        testInvokeOnPool(mainPool(), a);
818      }
819  
820  
# Line 710 | Line 822 | public class ForkJoinTaskTest extends JS
822       * invokeAll(tasks) with any null task throws NPE
823       */
824      public void testInvokeAllNPE() {
825 <        RecursiveAction a = new RecursiveAction() {
826 <            public void compute() {
825 >        RecursiveAction a = new CheckedRecursiveAction() {
826 >            public void realCompute() {
827 >                AsyncFib f = new AsyncFib(8);
828 >                AsyncFib g = new AsyncFib(9);
829 >                AsyncFib h = null;
830                  try {
716                    AsyncFib f = new AsyncFib(8);
717                    AsyncFib g = new AsyncFib(9);
718                    AsyncFib h = null;
831                      invokeAll(f, g, h);
832                      shouldThrow();
833 <                } catch (NullPointerException success) {
722 <                }
833 >                } catch (NullPointerException success) {}
834              }};
835 <        mainPool.invoke(a);
835 >        testInvokeOnPool(mainPool(), a);
836      }
837  
838      /**
839       * invokeAll(t1, t2) throw exception if any task does
840       */
841      public void testAbnormalInvokeAll2() {
842 <        RecursiveAction a = new RecursiveAction() {
843 <            public void compute() {
842 >        RecursiveAction a = new CheckedRecursiveAction() {
843 >            public void realCompute() {
844 >                AsyncFib f = new AsyncFib(8);
845 >                FailingAsyncFib g = new FailingAsyncFib(9);
846                  try {
734                    AsyncFib f = new AsyncFib(8);
735                    FailingAsyncFib g = new FailingAsyncFib(9);
847                      invokeAll(f, g);
848                      shouldThrow();
849                  } catch (FJException success) {
850 +                    checkCompletedAbnormally(g, success);
851                  }
852              }};
853 <        mainPool.invoke(a);
853 >        testInvokeOnPool(mainPool(), a);
854      }
855  
856      /**
857       * invokeAll(tasks) with 1 argument throws exception if task does
858       */
859      public void testAbnormalInvokeAll1() {
860 <        RecursiveAction a = new RecursiveAction() {
861 <            public void compute() {
860 >        RecursiveAction a = new CheckedRecursiveAction() {
861 >            public void realCompute() {
862 >                FailingAsyncFib g = new FailingAsyncFib(9);
863                  try {
751                    FailingAsyncFib g = new FailingAsyncFib(9);
864                      invokeAll(g);
865                      shouldThrow();
866                  } catch (FJException success) {
867 +                    checkCompletedAbnormally(g, success);
868                  }
869              }};
870 <        mainPool.invoke(a);
870 >        testInvokeOnPool(mainPool(), a);
871      }
872  
873      /**
874       * invokeAll(tasks) with > 2 argument throws exception if any task does
875       */
876      public void testAbnormalInvokeAll3() {
877 <        RecursiveAction a = new RecursiveAction() {
878 <            public void compute() {
877 >        RecursiveAction a = new CheckedRecursiveAction() {
878 >            public void realCompute() {
879 >                AsyncFib f = new AsyncFib(8);
880 >                FailingAsyncFib g = new FailingAsyncFib(9);
881 >                AsyncFib h = new AsyncFib(7);
882                  try {
767                    AsyncFib f = new AsyncFib(8);
768                    FailingAsyncFib g = new FailingAsyncFib(9);
769                    AsyncFib h = new AsyncFib(7);
883                      invokeAll(f, g, h);
884                      shouldThrow();
885                  } catch (FJException success) {
886 +                    checkCompletedAbnormally(g, success);
887                  }
888              }};
889 <        mainPool.invoke(a);
889 >        testInvokeOnPool(mainPool(), a);
890      }
891  
892      /**
893       * invokeAll(collection)  throws exception if any task does
894       */
895      public void testAbnormalInvokeAllCollection() {
896 <        RecursiveAction a = new RecursiveAction() {
897 <            public void compute() {
896 >        RecursiveAction a = new CheckedRecursiveAction() {
897 >            public void realCompute() {
898 >                FailingAsyncFib f = new FailingAsyncFib(8);
899 >                AsyncFib g = new AsyncFib(9);
900 >                AsyncFib h = new AsyncFib(7);
901 >                HashSet set = new HashSet();
902 >                set.add(f);
903 >                set.add(g);
904 >                set.add(h);
905                  try {
785                    FailingAsyncFib f = new FailingAsyncFib(8);
786                    AsyncFib g = new AsyncFib(9);
787                    AsyncFib h = new AsyncFib(7);
788                    HashSet set = new HashSet();
789                    set.add(f);
790                    set.add(g);
791                    set.add(h);
906                      invokeAll(set);
907                      shouldThrow();
908                  } catch (FJException success) {
909 +                    checkCompletedAbnormally(f, success);
910                  }
911              }};
912 <        mainPool.invoke(a);
912 >        testInvokeOnPool(mainPool(), a);
913      }
914  
915      /**
# Line 802 | Line 917 | public class ForkJoinTaskTest extends JS
917       * and suppresses execution
918       */
919      public void testTryUnfork() {
920 <        RecursiveAction a = new RecursiveAction() {
921 <            public void compute() {
920 >        RecursiveAction a = new CheckedRecursiveAction() {
921 >            public void realCompute() {
922                  AsyncFib g = new AsyncFib(9);
923 <                g.fork();
923 >                assertSame(g, g.fork());
924                  AsyncFib f = new AsyncFib(8);
925 <                f.fork();
926 <                threadAssertTrue(f.tryUnfork());
925 >                assertSame(f, f.fork());
926 >                assertTrue(f.tryUnfork());
927                  helpQuiesce();
928 <                threadAssertFalse(f.isDone());
929 <                threadAssertTrue(g.isDone());
928 >                checkNotDone(f);
929 >                checkCompletedNormally(g);
930              }};
931 <        singletonPool.invoke(a);
931 >        testInvokeOnPool(singletonPool(), a);
932      }
933  
934      /**
# Line 821 | Line 936 | public class ForkJoinTaskTest extends JS
936       * there are more tasks than threads
937       */
938      public void testGetSurplusQueuedTaskCount() {
939 <        RecursiveAction a = new RecursiveAction() {
940 <            public void compute() {
939 >        RecursiveAction a = new CheckedRecursiveAction() {
940 >            public void realCompute() {
941                  AsyncFib h = new AsyncFib(7);
942 <                h.fork();
942 >                assertSame(h, h.fork());
943                  AsyncFib g = new AsyncFib(9);
944 <                g.fork();
944 >                assertSame(g, g.fork());
945                  AsyncFib f = new AsyncFib(8);
946 <                f.fork();
947 <                threadAssertTrue(getSurplusQueuedTaskCount() > 0);
946 >                assertSame(f, f.fork());
947 >                assertTrue(getSurplusQueuedTaskCount() > 0);
948                  helpQuiesce();
949 +                assertEquals(0, getSurplusQueuedTaskCount());
950 +                checkCompletedNormally(f);
951 +                checkCompletedNormally(g);
952 +                checkCompletedNormally(h);
953              }};
954 <        singletonPool.invoke(a);
954 >        testInvokeOnPool(singletonPool(), a);
955      }
956  
957      /**
958       * peekNextLocalTask returns most recent unexecuted task.
959       */
960      public void testPeekNextLocalTask() {
961 <        RecursiveAction a = new RecursiveAction() {
962 <            public void compute() {
961 >        RecursiveAction a = new CheckedRecursiveAction() {
962 >            public void realCompute() {
963                  AsyncFib g = new AsyncFib(9);
964 <                g.fork();
964 >                assertSame(g, g.fork());
965                  AsyncFib f = new AsyncFib(8);
966 <                f.fork();
967 <                threadAssertTrue(peekNextLocalTask() == f);
968 <                f.join();
969 <                threadAssertTrue(f.isDone());
966 >                assertSame(f, f.fork());
967 >                assertSame(f, peekNextLocalTask());
968 >                assertNull(f.join());
969 >                checkCompletedNormally(f);
970                  helpQuiesce();
971 +                checkCompletedNormally(g);
972              }};
973 <        singletonPool.invoke(a);
973 >        testInvokeOnPool(singletonPool(), a);
974      }
975  
976      /**
977 <     * pollNextLocalTask returns most recent unexecuted task
978 <     * without executing it
977 >     * pollNextLocalTask returns most recent unexecuted task without
978 >     * executing it
979       */
980      public void testPollNextLocalTask() {
981 <        RecursiveAction a = new RecursiveAction() {
982 <            public void compute() {
981 >        RecursiveAction a = new CheckedRecursiveAction() {
982 >            public void realCompute() {
983                  AsyncFib g = new AsyncFib(9);
984 <                g.fork();
984 >                assertSame(g, g.fork());
985                  AsyncFib f = new AsyncFib(8);
986 <                f.fork();
987 <                threadAssertTrue(pollNextLocalTask() == f);
986 >                assertSame(f, f.fork());
987 >                assertSame(f, pollNextLocalTask());
988                  helpQuiesce();
989 <                threadAssertFalse(f.isDone());
989 >                checkNotDone(f);
990 >                assertEquals(34, g.number);
991 >                checkCompletedNormally(g);
992              }};
993 <        singletonPool.invoke(a);
993 >        testInvokeOnPool(singletonPool(), a);
994      }
995  
996      /**
997 <     * pollTask returns an unexecuted task
876 <     * without executing it
997 >     * pollTask returns an unexecuted task without executing it
998       */
999      public void testPollTask() {
1000 <        RecursiveAction a = new RecursiveAction() {
1001 <            public void compute() {
1000 >        RecursiveAction a = new CheckedRecursiveAction() {
1001 >            public void realCompute() {
1002                  AsyncFib g = new AsyncFib(9);
1003 <                g.fork();
1003 >                assertSame(g, g.fork());
1004                  AsyncFib f = new AsyncFib(8);
1005 <                f.fork();
1006 <                threadAssertTrue(pollTask() == f);
1005 >                assertSame(f, f.fork());
1006 >                assertSame(f, pollTask());
1007                  helpQuiesce();
1008 <                threadAssertFalse(f.isDone());
1009 <                threadAssertTrue(g.isDone());
1008 >                checkNotDone(f);
1009 >                checkCompletedNormally(g);
1010              }};
1011 <        singletonPool.invoke(a);
1011 >        testInvokeOnPool(singletonPool(), a);
1012      }
1013  
1014      /**
1015       * peekNextLocalTask returns least recent unexecuted task in async mode
1016       */
1017      public void testPeekNextLocalTaskAsync() {
1018 <        RecursiveAction a = new RecursiveAction() {
1019 <            public void compute() {
1018 >        RecursiveAction a = new CheckedRecursiveAction() {
1019 >            public void realCompute() {
1020                  AsyncFib g = new AsyncFib(9);
1021 <                g.fork();
1021 >                assertSame(g, g.fork());
1022                  AsyncFib f = new AsyncFib(8);
1023 <                f.fork();
1024 <                threadAssertTrue(peekNextLocalTask() == g);
1025 <                f.join();
1023 >                assertSame(f, f.fork());
1024 >                assertSame(g, peekNextLocalTask());
1025 >                assertNull(f.join());
1026                  helpQuiesce();
1027 <                threadAssertTrue(f.isDone());
1027 >                checkCompletedNormally(f);
1028 >                assertEquals(34, g.number);
1029 >                checkCompletedNormally(g);
1030              }};
1031 <        asyncSingletonPool.invoke(a);
1031 >        testInvokeOnPool(asyncSingletonPool(), a);
1032      }
1033  
1034      /**
1035 <     * pollNextLocalTask returns least recent unexecuted task
1036 <     * without executing it, in async mode
1035 >     * pollNextLocalTask returns least recent unexecuted task without
1036 >     * executing it, in async mode
1037       */
1038      public void testPollNextLocalTaskAsync() {
1039 <        RecursiveAction a = new RecursiveAction() {
1040 <            public void compute() {
1039 >        RecursiveAction a = new CheckedRecursiveAction() {
1040 >            public void realCompute() {
1041                  AsyncFib g = new AsyncFib(9);
1042 <                g.fork();
1042 >                assertSame(g, g.fork());
1043                  AsyncFib f = new AsyncFib(8);
1044 <                f.fork();
1045 <                threadAssertTrue(pollNextLocalTask() == g);
1044 >                assertSame(f, f.fork());
1045 >                assertSame(g, pollNextLocalTask());
1046                  helpQuiesce();
1047 <                threadAssertTrue(f.isDone());
1048 <                threadAssertFalse(g.isDone());
1047 >                assertEquals(21, f.number);
1048 >                checkCompletedNormally(f);
1049 >                checkNotDone(g);
1050              }};
1051 <        asyncSingletonPool.invoke(a);
1051 >        testInvokeOnPool(asyncSingletonPool(), a);
1052      }
1053  
1054      /**
1055 <     * pollTask returns an unexecuted task
1056 <     * without executing it, in async mode
1055 >     * pollTask returns an unexecuted task without executing it, in
1056 >     * async mode
1057       */
1058      public void testPollTaskAsync() {
1059 <        RecursiveAction a = new RecursiveAction() {
1060 <            public void compute() {
1059 >        RecursiveAction a = new CheckedRecursiveAction() {
1060 >            public void realCompute() {
1061                  AsyncFib g = new AsyncFib(9);
1062 <                g.fork();
1062 >                assertSame(g, g.fork());
1063                  AsyncFib f = new AsyncFib(8);
1064 <                f.fork();
1065 <                threadAssertTrue(pollTask() == g);
1064 >                assertSame(f, f.fork());
1065 >                assertSame(g, pollTask());
1066                  helpQuiesce();
1067 <                threadAssertTrue(f.isDone());
1068 <                threadAssertFalse(g.isDone());
1067 >                assertEquals(21, f.number);
1068 >                checkCompletedNormally(f);
1069 >                checkNotDone(g);
1070 >            }};
1071 >        testInvokeOnPool(asyncSingletonPool(), a);
1072 >    }
1073 >
1074 >    // versions for singleton pools
1075 >
1076 >    /**
1077 >     * invoke returns when task completes normally.
1078 >     * isCompletedAbnormally and isCancelled return false for normally
1079 >     * completed tasks; getRawResult returns null.
1080 >     */
1081 >    public void testInvokeSingleton() {
1082 >        RecursiveAction a = new CheckedRecursiveAction() {
1083 >            public void realCompute() {
1084 >                AsyncFib f = new AsyncFib(8);
1085 >                assertNull(f.invoke());
1086 >                assertEquals(21, f.number);
1087 >                checkCompletedNormally(f);
1088 >            }};
1089 >        testInvokeOnPool(singletonPool(), a);
1090 >    }
1091 >
1092 >    /**
1093 >     * quietlyInvoke task returns when task completes normally.
1094 >     * isCompletedAbnormally and isCancelled return false for normally
1095 >     * completed tasks
1096 >     */
1097 >    public void testQuietlyInvokeSingleton() {
1098 >        RecursiveAction a = new CheckedRecursiveAction() {
1099 >            public void realCompute() {
1100 >                AsyncFib f = new AsyncFib(8);
1101 >                f.quietlyInvoke();
1102 >                assertEquals(21, f.number);
1103 >                checkCompletedNormally(f);
1104 >            }};
1105 >        testInvokeOnPool(singletonPool(), a);
1106 >    }
1107 >
1108 >    /**
1109 >     * join of a forked task returns when task completes
1110 >     */
1111 >    public void testForkJoinSingleton() {
1112 >        RecursiveAction a = new CheckedRecursiveAction() {
1113 >            public void realCompute() {
1114 >                AsyncFib f = new AsyncFib(8);
1115 >                assertSame(f, f.fork());
1116 >                assertNull(f.join());
1117 >                assertEquals(21, f.number);
1118 >                checkCompletedNormally(f);
1119 >            }};
1120 >        testInvokeOnPool(singletonPool(), a);
1121 >    }
1122 >
1123 >    /**
1124 >     * get of a forked task returns when task completes
1125 >     */
1126 >    public void testForkGetSingleton() {
1127 >        RecursiveAction a = new CheckedRecursiveAction() {
1128 >            public void realCompute() throws Exception {
1129 >                AsyncFib f = new AsyncFib(8);
1130 >                assertSame(f, f.fork());
1131 >                assertNull(f.get());
1132 >                assertEquals(21, f.number);
1133 >                checkCompletedNormally(f);
1134 >            }};
1135 >        testInvokeOnPool(singletonPool(), a);
1136 >    }
1137 >
1138 >    /**
1139 >     * timed get of a forked task returns when task completes
1140 >     */
1141 >    public void testForkTimedGetSingleton() {
1142 >        RecursiveAction a = new CheckedRecursiveAction() {
1143 >            public void realCompute() throws Exception {
1144 >                AsyncFib f = new AsyncFib(8);
1145 >                assertSame(f, f.fork());
1146 >                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1147 >                assertEquals(21, f.number);
1148 >                checkCompletedNormally(f);
1149 >            }};
1150 >        testInvokeOnPool(singletonPool(), a);
1151 >    }
1152 >
1153 >    /**
1154 >     * timed get with null time unit throws NPE
1155 >     */
1156 >    public void testForkTimedGetNPESingleton() {
1157 >        RecursiveAction a = new CheckedRecursiveAction() {
1158 >            public void realCompute() throws Exception {
1159 >                AsyncFib f = new AsyncFib(8);
1160 >                assertSame(f, f.fork());
1161 >                try {
1162 >                    f.get(5L, null);
1163 >                    shouldThrow();
1164 >                } catch (NullPointerException success) {}
1165              }};
1166 <        asyncSingletonPool.invoke(a);
1166 >        testInvokeOnPool(singletonPool(), a);
1167      }
1168 +
1169 +    /**
1170 +     * quietlyJoin of a forked task returns when task completes
1171 +     */
1172 +    public void testForkQuietlyJoinSingleton() {
1173 +        RecursiveAction a = new CheckedRecursiveAction() {
1174 +            public void realCompute() {
1175 +                AsyncFib f = new AsyncFib(8);
1176 +                assertSame(f, f.fork());
1177 +                f.quietlyJoin();
1178 +                assertEquals(21, f.number);
1179 +                checkCompletedNormally(f);
1180 +            }};
1181 +        testInvokeOnPool(singletonPool(), a);
1182 +    }
1183 +
1184 +
1185 +    /**
1186 +     * helpQuiesce returns when tasks are complete.
1187 +     * getQueuedTaskCount returns 0 when quiescent
1188 +     */
1189 +    public void testForkHelpQuiesceSingleton() {
1190 +        RecursiveAction a = new CheckedRecursiveAction() {
1191 +            public void realCompute() {
1192 +                AsyncFib f = new AsyncFib(8);
1193 +                assertSame(f, f.fork());
1194 +                f.helpQuiesce();
1195 +                assertEquals(0, getQueuedTaskCount());
1196 +                assertEquals(21, f.number);
1197 +                checkCompletedNormally(f);
1198 +            }};
1199 +        testInvokeOnPool(singletonPool(), a);
1200 +    }
1201 +
1202 +
1203 +    /**
1204 +     * invoke task throws exception when task completes abnormally
1205 +     */
1206 +    public void testAbnormalInvokeSingleton() {
1207 +        RecursiveAction a = new CheckedRecursiveAction() {
1208 +            public void realCompute() {
1209 +                FailingAsyncFib f = new FailingAsyncFib(8);
1210 +                try {
1211 +                    f.invoke();
1212 +                    shouldThrow();
1213 +                } catch (FJException success) {
1214 +                    checkCompletedAbnormally(f, success);
1215 +                }
1216 +            }};
1217 +        testInvokeOnPool(singletonPool(), a);
1218 +    }
1219 +
1220 +    /**
1221 +     * quietlyInvoke task returns when task completes abnormally
1222 +     */
1223 +    public void testAbnormalQuietlyInvokeSingleton() {
1224 +        RecursiveAction a = new CheckedRecursiveAction() {
1225 +            public void realCompute() {
1226 +                FailingAsyncFib f = new FailingAsyncFib(8);
1227 +                f.quietlyInvoke();
1228 +                assertTrue(f.getException() instanceof FJException);
1229 +                checkCompletedAbnormally(f, f.getException());
1230 +            }};
1231 +        testInvokeOnPool(singletonPool(), a);
1232 +    }
1233 +
1234 +    /**
1235 +     * join of a forked task throws exception when task completes abnormally
1236 +     */
1237 +    public void testAbnormalForkJoinSingleton() {
1238 +        RecursiveAction a = new CheckedRecursiveAction() {
1239 +            public void realCompute() {
1240 +                FailingAsyncFib f = new FailingAsyncFib(8);
1241 +                assertSame(f, f.fork());
1242 +                try {
1243 +                    f.join();
1244 +                    shouldThrow();
1245 +                } catch (FJException success) {
1246 +                    checkCompletedAbnormally(f, success);
1247 +                }
1248 +            }};
1249 +        testInvokeOnPool(singletonPool(), a);
1250 +    }
1251 +
1252 +    /**
1253 +     * get of a forked task throws exception when task completes abnormally
1254 +     */
1255 +    public void testAbnormalForkGetSingleton() {
1256 +        RecursiveAction a = new CheckedRecursiveAction() {
1257 +            public void realCompute() throws Exception {
1258 +                FailingAsyncFib f = new FailingAsyncFib(8);
1259 +                assertSame(f, f.fork());
1260 +                try {
1261 +                    f.get();
1262 +                    shouldThrow();
1263 +                } catch (ExecutionException success) {
1264 +                    Throwable cause = success.getCause();
1265 +                    assertTrue(cause instanceof FJException);
1266 +                    checkCompletedAbnormally(f, cause);
1267 +                }
1268 +            }};
1269 +        testInvokeOnPool(singletonPool(), a);
1270 +    }
1271 +
1272 +    /**
1273 +     * timed get of a forked task throws exception when task completes abnormally
1274 +     */
1275 +    public void testAbnormalForkTimedGetSingleton() {
1276 +        RecursiveAction a = new CheckedRecursiveAction() {
1277 +            public void realCompute() throws Exception {
1278 +                FailingAsyncFib f = new FailingAsyncFib(8);
1279 +                assertSame(f, f.fork());
1280 +                try {
1281 +                    f.get(LONG_DELAY_MS, MILLISECONDS);
1282 +                    shouldThrow();
1283 +                } catch (ExecutionException success) {
1284 +                    Throwable cause = success.getCause();
1285 +                    assertTrue(cause instanceof FJException);
1286 +                    checkCompletedAbnormally(f, cause);
1287 +                }
1288 +            }};
1289 +        testInvokeOnPool(singletonPool(), a);
1290 +    }
1291 +
1292 +    /**
1293 +     * quietlyJoin of a forked task returns when task completes abnormally
1294 +     */
1295 +    public void testAbnormalForkQuietlyJoinSingleton() {
1296 +        RecursiveAction a = new CheckedRecursiveAction() {
1297 +            public void realCompute() {
1298 +                FailingAsyncFib f = new FailingAsyncFib(8);
1299 +                assertSame(f, f.fork());
1300 +                f.quietlyJoin();
1301 +                assertTrue(f.getException() instanceof FJException);
1302 +                checkCompletedAbnormally(f, f.getException());
1303 +            }};
1304 +        testInvokeOnPool(singletonPool(), a);
1305 +    }
1306 +
1307 +    /**
1308 +     * invoke task throws exception when task cancelled
1309 +     */
1310 +    public void testCancelledInvokeSingleton() {
1311 +        RecursiveAction a = new CheckedRecursiveAction() {
1312 +            public void realCompute() {
1313 +                AsyncFib f = new AsyncFib(8);
1314 +                assertTrue(f.cancel(true));
1315 +                try {
1316 +                    f.invoke();
1317 +                    shouldThrow();
1318 +                } catch (CancellationException success) {
1319 +                    checkCancelled(f);
1320 +                }
1321 +            }};
1322 +        testInvokeOnPool(singletonPool(), a);
1323 +    }
1324 +
1325 +    /**
1326 +     * join of a forked task throws exception when task cancelled
1327 +     */
1328 +    public void testCancelledForkJoinSingleton() {
1329 +        RecursiveAction a = new CheckedRecursiveAction() {
1330 +            public void realCompute() {
1331 +                AsyncFib f = new AsyncFib(8);
1332 +                assertTrue(f.cancel(true));
1333 +                assertSame(f, f.fork());
1334 +                try {
1335 +                    f.join();
1336 +                    shouldThrow();
1337 +                } catch (CancellationException success) {
1338 +                    checkCancelled(f);
1339 +                }
1340 +            }};
1341 +        testInvokeOnPool(singletonPool(), a);
1342 +    }
1343 +
1344 +    /**
1345 +     * get of a forked task throws exception when task cancelled
1346 +     */
1347 +    public void testCancelledForkGetSingleton() {
1348 +        RecursiveAction a = new CheckedRecursiveAction() {
1349 +            public void realCompute() throws Exception {
1350 +                AsyncFib f = new AsyncFib(8);
1351 +                assertTrue(f.cancel(true));
1352 +                assertSame(f, f.fork());
1353 +                try {
1354 +                    f.get();
1355 +                    shouldThrow();
1356 +                } catch (CancellationException success) {
1357 +                    checkCancelled(f);
1358 +                }
1359 +            }};
1360 +        testInvokeOnPool(singletonPool(), a);
1361 +    }
1362 +
1363 +    /**
1364 +     * timed get of a forked task throws exception when task cancelled
1365 +     */
1366 +    public void testCancelledForkTimedGetSingleton() throws Exception {
1367 +        RecursiveAction a = new CheckedRecursiveAction() {
1368 +            public void realCompute() throws Exception {
1369 +                AsyncFib f = new AsyncFib(8);
1370 +                assertTrue(f.cancel(true));
1371 +                assertSame(f, f.fork());
1372 +                try {
1373 +                    f.get(LONG_DELAY_MS, MILLISECONDS);
1374 +                    shouldThrow();
1375 +                } catch (CancellationException success) {
1376 +                    checkCancelled(f);
1377 +                }
1378 +            }};
1379 +        testInvokeOnPool(singletonPool(), a);
1380 +    }
1381 +
1382 +    /**
1383 +     * quietlyJoin of a forked task returns when task cancelled
1384 +     */
1385 +    public void testCancelledForkQuietlyJoinSingleton() {
1386 +        RecursiveAction a = new CheckedRecursiveAction() {
1387 +            public void realCompute() {
1388 +                AsyncFib f = new AsyncFib(8);
1389 +                assertTrue(f.cancel(true));
1390 +                assertSame(f, f.fork());
1391 +                f.quietlyJoin();
1392 +                checkCancelled(f);
1393 +            }};
1394 +        testInvokeOnPool(singletonPool(), a);
1395 +    }
1396 +
1397 +    /**
1398 +     * invoke task throws exception after invoking completeExceptionally
1399 +     */
1400 +    public void testCompleteExceptionallySingleton() {
1401 +        RecursiveAction a = new CheckedRecursiveAction() {
1402 +            public void realCompute() {
1403 +                AsyncFib f = new AsyncFib(8);
1404 +                f.completeExceptionally(new FJException());
1405 +                try {
1406 +                    f.invoke();
1407 +                    shouldThrow();
1408 +                } catch (FJException success) {
1409 +                    checkCompletedAbnormally(f, success);
1410 +                }
1411 +            }};
1412 +        testInvokeOnPool(singletonPool(), a);
1413 +    }
1414 +
1415 +    /**
1416 +     * invokeAll(t1, t2) invokes all task arguments
1417 +     */
1418 +    public void testInvokeAll2Singleton() {
1419 +        RecursiveAction a = new CheckedRecursiveAction() {
1420 +            public void realCompute() {
1421 +                AsyncFib f = new AsyncFib(8);
1422 +                AsyncFib g = new AsyncFib(9);
1423 +                invokeAll(f, g);
1424 +                assertEquals(21, f.number);
1425 +                assertEquals(34, g.number);
1426 +                checkCompletedNormally(f);
1427 +                checkCompletedNormally(g);
1428 +            }};
1429 +        testInvokeOnPool(singletonPool(), a);
1430 +    }
1431 +
1432 +    /**
1433 +     * invokeAll(tasks) with 1 argument invokes task
1434 +     */
1435 +    public void testInvokeAll1Singleton() {
1436 +        RecursiveAction a = new CheckedRecursiveAction() {
1437 +            public void realCompute() {
1438 +                AsyncFib f = new AsyncFib(8);
1439 +                invokeAll(f);
1440 +                checkCompletedNormally(f);
1441 +                assertEquals(21, f.number);
1442 +            }};
1443 +        testInvokeOnPool(singletonPool(), a);
1444 +    }
1445 +
1446 +    /**
1447 +     * invokeAll(tasks) with > 2 argument invokes tasks
1448 +     */
1449 +    public void testInvokeAll3Singleton() {
1450 +        RecursiveAction a = new CheckedRecursiveAction() {
1451 +            public void realCompute() {
1452 +                AsyncFib f = new AsyncFib(8);
1453 +                AsyncFib g = new AsyncFib(9);
1454 +                AsyncFib h = new AsyncFib(7);
1455 +                invokeAll(f, g, h);
1456 +                assertEquals(21, f.number);
1457 +                assertEquals(34, g.number);
1458 +                assertEquals(13, h.number);
1459 +                checkCompletedNormally(f);
1460 +                checkCompletedNormally(g);
1461 +                checkCompletedNormally(h);
1462 +            }};
1463 +        testInvokeOnPool(singletonPool(), a);
1464 +    }
1465 +
1466 +    /**
1467 +     * invokeAll(collection) invokes all tasks in the collection
1468 +     */
1469 +    public void testInvokeAllCollectionSingleton() {
1470 +        RecursiveAction a = new CheckedRecursiveAction() {
1471 +            public void realCompute() {
1472 +                AsyncFib f = new AsyncFib(8);
1473 +                AsyncFib g = new AsyncFib(9);
1474 +                AsyncFib h = new AsyncFib(7);
1475 +                HashSet set = new HashSet();
1476 +                set.add(f);
1477 +                set.add(g);
1478 +                set.add(h);
1479 +                invokeAll(set);
1480 +                assertEquals(21, f.number);
1481 +                assertEquals(34, g.number);
1482 +                assertEquals(13, h.number);
1483 +                checkCompletedNormally(f);
1484 +                checkCompletedNormally(g);
1485 +                checkCompletedNormally(h);
1486 +            }};
1487 +        testInvokeOnPool(singletonPool(), a);
1488 +    }
1489 +
1490 +
1491 +    /**
1492 +     * invokeAll(tasks) with any null task throws NPE
1493 +     */
1494 +    public void testInvokeAllNPESingleton() {
1495 +        RecursiveAction a = new CheckedRecursiveAction() {
1496 +            public void realCompute() {
1497 +                AsyncFib f = new AsyncFib(8);
1498 +                AsyncFib g = new AsyncFib(9);
1499 +                AsyncFib h = null;
1500 +                try {
1501 +                    invokeAll(f, g, h);
1502 +                    shouldThrow();
1503 +                } catch (NullPointerException success) {}
1504 +            }};
1505 +        testInvokeOnPool(singletonPool(), a);
1506 +    }
1507 +
1508 +    /**
1509 +     * invokeAll(t1, t2) throw exception if any task does
1510 +     */
1511 +    public void testAbnormalInvokeAll2Singleton() {
1512 +        RecursiveAction a = new CheckedRecursiveAction() {
1513 +            public void realCompute() {
1514 +                AsyncFib f = new AsyncFib(8);
1515 +                FailingAsyncFib g = new FailingAsyncFib(9);
1516 +                try {
1517 +                    invokeAll(f, g);
1518 +                    shouldThrow();
1519 +                } catch (FJException success) {
1520 +                    checkCompletedAbnormally(g, success);
1521 +                }
1522 +            }};
1523 +        testInvokeOnPool(singletonPool(), a);
1524 +    }
1525 +
1526 +    /**
1527 +     * invokeAll(tasks) with 1 argument throws exception if task does
1528 +     */
1529 +    public void testAbnormalInvokeAll1Singleton() {
1530 +        RecursiveAction a = new CheckedRecursiveAction() {
1531 +            public void realCompute() {
1532 +                FailingAsyncFib g = new FailingAsyncFib(9);
1533 +                try {
1534 +                    invokeAll(g);
1535 +                    shouldThrow();
1536 +                } catch (FJException success) {
1537 +                    checkCompletedAbnormally(g, success);
1538 +                }
1539 +            }};
1540 +        testInvokeOnPool(singletonPool(), a);
1541 +    }
1542 +
1543 +    /**
1544 +     * invokeAll(tasks) with > 2 argument throws exception if any task does
1545 +     */
1546 +    public void testAbnormalInvokeAll3Singleton() {
1547 +        RecursiveAction a = new CheckedRecursiveAction() {
1548 +            public void realCompute() {
1549 +                AsyncFib f = new AsyncFib(8);
1550 +                FailingAsyncFib g = new FailingAsyncFib(9);
1551 +                AsyncFib h = new AsyncFib(7);
1552 +                try {
1553 +                    invokeAll(f, g, h);
1554 +                    shouldThrow();
1555 +                } catch (FJException success) {
1556 +                    checkCompletedAbnormally(g, success);
1557 +                }
1558 +            }};
1559 +        testInvokeOnPool(singletonPool(), a);
1560 +    }
1561 +
1562 +    /**
1563 +     * invokeAll(collection)  throws exception if any task does
1564 +     */
1565 +    public void testAbnormalInvokeAllCollectionSingleton() {
1566 +        RecursiveAction a = new CheckedRecursiveAction() {
1567 +            public void realCompute() {
1568 +                FailingAsyncFib f = new FailingAsyncFib(8);
1569 +                AsyncFib g = new AsyncFib(9);
1570 +                AsyncFib h = new AsyncFib(7);
1571 +                HashSet set = new HashSet();
1572 +                set.add(f);
1573 +                set.add(g);
1574 +                set.add(h);
1575 +                try {
1576 +                    invokeAll(set);
1577 +                    shouldThrow();
1578 +                } catch (FJException success) {
1579 +                    checkCompletedAbnormally(f, success);
1580 +                }
1581 +            }};
1582 +        testInvokeOnPool(singletonPool(), a);
1583 +    }
1584 +
1585   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines