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.25 by jsr166, Sun Nov 21 20:32:15 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 227 | Line 352 | public class ForkJoinTaskTest extends JS
352       * completed tasks. getRawResult of a RecursiveAction 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              }};
723 <        a.invoke();
723 >        assertNull(a.invoke());
724      }
725  
726      /**
727       * invoke task throws exception after invoking completeExceptionally
728       */
729      public void testCompleteExceptionally() {
730 <        RecursiveAction a = new RecursiveAction() {
731 <            public void compute() {
730 >        RecursiveAction a = new CheckedRecursiveAction() {
731 >            public void realCompute() {
732 >                AsyncFib f = new AsyncFib(8);
733 >                f.completeExceptionally(new FJException());
734                  try {
623                    AsyncFib f = new AsyncFib(8);
624                    f.completeExceptionally(new FJException());
735                      f.invoke();
736                      shouldThrow();
737                  } catch (FJException success) {
738 +                    checkCompletedAbnormally(f, success);
739                  }
740              }};
741 <        mainPool.invoke(a);
741 >        testInvokeOnPool(mainPool(), a);
742      }
743  
744      /**
745       * invokeAll(t1, t2) invokes all task arguments
746       */
747      public void testInvokeAll2() {
748 <        RecursiveAction a = new RecursiveAction() {
749 <            public void compute() {
748 >        RecursiveAction a = new CheckedRecursiveAction() {
749 >            public void realCompute() {
750                  AsyncFib f = new AsyncFib(8);
751                  AsyncFib g = new AsyncFib(9);
752                  invokeAll(f, g);
753 <                threadAssertTrue(f.isDone());
754 <                threadAssertTrue(f.number == 21);
755 <                threadAssertTrue(g.isDone());
756 <                threadAssertTrue(g.number == 34);
753 >                assertEquals(21, f.number);
754 >                assertEquals(34, g.number);
755 >                checkCompletedNormally(f);
756 >                checkCompletedNormally(g);
757              }};
758 <        mainPool.invoke(a);
758 >        testInvokeOnPool(mainPool(), a);
759      }
760  
761      /**
762       * invokeAll(tasks) with 1 argument invokes task
763       */
764      public void testInvokeAll1() {
765 <        RecursiveAction a = new RecursiveAction() {
766 <            public void compute() {
765 >        RecursiveAction a = new CheckedRecursiveAction() {
766 >            public void realCompute() {
767                  AsyncFib f = new AsyncFib(8);
768                  invokeAll(f);
769 <                threadAssertTrue(f.isDone());
770 <                threadAssertTrue(f.number == 21);
769 >                checkCompletedNormally(f);
770 >                assertEquals(21, f.number);
771              }};
772 <        mainPool.invoke(a);
772 >        testInvokeOnPool(mainPool(), a);
773      }
774  
775      /**
776       * invokeAll(tasks) with > 2 argument invokes tasks
777       */
778      public void testInvokeAll3() {
779 <        RecursiveAction a = new RecursiveAction() {
780 <            public void compute() {
779 >        RecursiveAction a = new CheckedRecursiveAction() {
780 >            public void realCompute() {
781                  AsyncFib f = new AsyncFib(8);
782                  AsyncFib g = new AsyncFib(9);
783                  AsyncFib h = new AsyncFib(7);
784                  invokeAll(f, g, h);
785 <                threadAssertTrue(f.isDone());
786 <                threadAssertTrue(f.number == 21);
787 <                threadAssertTrue(g.isDone());
788 <                threadAssertTrue(g.number == 34);
789 <                threadAssertTrue(h.isDone());
790 <                threadAssertTrue(h.number == 13);
785 >                assertEquals(21, f.number);
786 >                assertEquals(34, g.number);
787 >                assertEquals(13, h.number);
788 >                checkCompletedNormally(f);
789 >                checkCompletedNormally(g);
790 >                checkCompletedNormally(h);
791              }};
792 <        mainPool.invoke(a);
792 >        testInvokeOnPool(mainPool(), a);
793      }
794  
795      /**
796       * invokeAll(collection) invokes all tasks in the collection
797       */
798      public void testInvokeAllCollection() {
799 <        RecursiveAction a = new RecursiveAction() {
800 <            public void compute() {
799 >        RecursiveAction a = new CheckedRecursiveAction() {
800 >            public void realCompute() {
801                  AsyncFib f = new AsyncFib(8);
802                  AsyncFib g = new AsyncFib(9);
803                  AsyncFib h = new AsyncFib(7);
# Line 695 | Line 806 | public class ForkJoinTaskTest extends JS
806                  set.add(g);
807                  set.add(h);
808                  invokeAll(set);
809 <                threadAssertTrue(f.isDone());
810 <                threadAssertTrue(f.number == 21);
811 <                threadAssertTrue(g.isDone());
812 <                threadAssertTrue(g.number == 34);
813 <                threadAssertTrue(h.isDone());
814 <                threadAssertTrue(h.number == 13);
809 >                assertEquals(21, f.number);
810 >                assertEquals(34, g.number);
811 >                assertEquals(13, h.number);
812 >                checkCompletedNormally(f);
813 >                checkCompletedNormally(g);
814 >                checkCompletedNormally(h);
815              }};
816 <        mainPool.invoke(a);
816 >        testInvokeOnPool(mainPool(), a);
817      }
818  
819  
# Line 710 | Line 821 | public class ForkJoinTaskTest extends JS
821       * invokeAll(tasks) with any null task throws NPE
822       */
823      public void testInvokeAllNPE() {
824 <        RecursiveAction a = new RecursiveAction() {
825 <            public void compute() {
824 >        RecursiveAction a = new CheckedRecursiveAction() {
825 >            public void realCompute() {
826 >                AsyncFib f = new AsyncFib(8);
827 >                AsyncFib g = new AsyncFib(9);
828 >                AsyncFib h = null;
829                  try {
716                    AsyncFib f = new AsyncFib(8);
717                    AsyncFib g = new AsyncFib(9);
718                    AsyncFib h = null;
830                      invokeAll(f, g, h);
831                      shouldThrow();
832 <                } catch (NullPointerException success) {
722 <                }
832 >                } catch (NullPointerException success) {}
833              }};
834 <        mainPool.invoke(a);
834 >        testInvokeOnPool(mainPool(), a);
835      }
836  
837      /**
838       * invokeAll(t1, t2) throw exception if any task does
839       */
840      public void testAbnormalInvokeAll2() {
841 <        RecursiveAction a = new RecursiveAction() {
842 <            public void compute() {
841 >        RecursiveAction a = new CheckedRecursiveAction() {
842 >            public void realCompute() {
843 >                AsyncFib f = new AsyncFib(8);
844 >                FailingAsyncFib g = new FailingAsyncFib(9);
845                  try {
734                    AsyncFib f = new AsyncFib(8);
735                    FailingAsyncFib g = new FailingAsyncFib(9);
846                      invokeAll(f, g);
847                      shouldThrow();
848                  } catch (FJException success) {
849 +                    checkCompletedAbnormally(g, success);
850                  }
851              }};
852 <        mainPool.invoke(a);
852 >        testInvokeOnPool(mainPool(), a);
853      }
854  
855      /**
856       * invokeAll(tasks) with 1 argument throws exception if task does
857       */
858      public void testAbnormalInvokeAll1() {
859 <        RecursiveAction a = new RecursiveAction() {
860 <            public void compute() {
859 >        RecursiveAction a = new CheckedRecursiveAction() {
860 >            public void realCompute() {
861 >                FailingAsyncFib g = new FailingAsyncFib(9);
862                  try {
751                    FailingAsyncFib g = new FailingAsyncFib(9);
863                      invokeAll(g);
864                      shouldThrow();
865                  } catch (FJException success) {
866 +                    checkCompletedAbnormally(g, success);
867                  }
868              }};
869 <        mainPool.invoke(a);
869 >        testInvokeOnPool(mainPool(), a);
870      }
871  
872      /**
873       * invokeAll(tasks) with > 2 argument throws exception if any task does
874       */
875      public void testAbnormalInvokeAll3() {
876 <        RecursiveAction a = new RecursiveAction() {
877 <            public void compute() {
876 >        RecursiveAction a = new CheckedRecursiveAction() {
877 >            public void realCompute() {
878 >                AsyncFib f = new AsyncFib(8);
879 >                FailingAsyncFib g = new FailingAsyncFib(9);
880 >                AsyncFib h = new AsyncFib(7);
881                  try {
767                    AsyncFib f = new AsyncFib(8);
768                    FailingAsyncFib g = new FailingAsyncFib(9);
769                    AsyncFib h = new AsyncFib(7);
882                      invokeAll(f, g, h);
883                      shouldThrow();
884                  } catch (FJException success) {
885 +                    checkCompletedAbnormally(g, success);
886                  }
887              }};
888 <        mainPool.invoke(a);
888 >        testInvokeOnPool(mainPool(), a);
889      }
890  
891      /**
892       * invokeAll(collection)  throws exception if any task does
893       */
894      public void testAbnormalInvokeAllCollection() {
895 <        RecursiveAction a = new RecursiveAction() {
896 <            public void compute() {
895 >        RecursiveAction a = new CheckedRecursiveAction() {
896 >            public void realCompute() {
897 >                FailingAsyncFib f = new FailingAsyncFib(8);
898 >                AsyncFib g = new AsyncFib(9);
899 >                AsyncFib h = new AsyncFib(7);
900 >                HashSet set = new HashSet();
901 >                set.add(f);
902 >                set.add(g);
903 >                set.add(h);
904                  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);
905                      invokeAll(set);
906                      shouldThrow();
907                  } catch (FJException success) {
908 +                    checkCompletedAbnormally(f, success);
909                  }
910              }};
911 <        mainPool.invoke(a);
911 >        testInvokeOnPool(mainPool(), a);
912      }
913  
914      /**
# Line 802 | Line 916 | public class ForkJoinTaskTest extends JS
916       * and suppresses execution
917       */
918      public void testTryUnfork() {
919 <        RecursiveAction a = new RecursiveAction() {
920 <            public void compute() {
919 >        RecursiveAction a = new CheckedRecursiveAction() {
920 >            public void realCompute() {
921                  AsyncFib g = new AsyncFib(9);
922 <                g.fork();
922 >                assertSame(g, g.fork());
923                  AsyncFib f = new AsyncFib(8);
924 <                f.fork();
925 <                threadAssertTrue(f.tryUnfork());
924 >                assertSame(f, f.fork());
925 >                assertTrue(f.tryUnfork());
926                  helpQuiesce();
927 <                threadAssertFalse(f.isDone());
928 <                threadAssertTrue(g.isDone());
927 >                checkNotDone(f);
928 >                checkCompletedNormally(g);
929              }};
930 <        singletonPool.invoke(a);
930 >        testInvokeOnPool(singletonPool(), a);
931      }
932  
933      /**
# Line 821 | Line 935 | public class ForkJoinTaskTest extends JS
935       * there are more tasks than threads
936       */
937      public void testGetSurplusQueuedTaskCount() {
938 <        RecursiveAction a = new RecursiveAction() {
939 <            public void compute() {
938 >        RecursiveAction a = new CheckedRecursiveAction() {
939 >            public void realCompute() {
940                  AsyncFib h = new AsyncFib(7);
941 <                h.fork();
941 >                assertSame(h, h.fork());
942                  AsyncFib g = new AsyncFib(9);
943 <                g.fork();
943 >                assertSame(g, g.fork());
944                  AsyncFib f = new AsyncFib(8);
945 <                f.fork();
946 <                threadAssertTrue(getSurplusQueuedTaskCount() > 0);
945 >                assertSame(f, f.fork());
946 >                assertTrue(getSurplusQueuedTaskCount() > 0);
947                  helpQuiesce();
948 +                assertEquals(0, getSurplusQueuedTaskCount());
949 +                checkCompletedNormally(f);
950 +                checkCompletedNormally(g);
951 +                checkCompletedNormally(h);
952              }};
953 <        singletonPool.invoke(a);
953 >        testInvokeOnPool(singletonPool(), a);
954      }
955  
956      /**
957       * peekNextLocalTask returns most recent unexecuted task.
958       */
959      public void testPeekNextLocalTask() {
960 <        RecursiveAction a = new RecursiveAction() {
961 <            public void compute() {
960 >        RecursiveAction a = new CheckedRecursiveAction() {
961 >            public void realCompute() {
962                  AsyncFib g = new AsyncFib(9);
963 <                g.fork();
963 >                assertSame(g, g.fork());
964                  AsyncFib f = new AsyncFib(8);
965 <                f.fork();
966 <                threadAssertTrue(peekNextLocalTask() == f);
967 <                f.join();
968 <                threadAssertTrue(f.isDone());
965 >                assertSame(f, f.fork());
966 >                assertSame(f, peekNextLocalTask());
967 >                assertNull(f.join());
968 >                checkCompletedNormally(f);
969                  helpQuiesce();
970 +                checkCompletedNormally(g);
971              }};
972 <        singletonPool.invoke(a);
972 >        testInvokeOnPool(singletonPool(), a);
973      }
974  
975      /**
976 <     * pollNextLocalTask returns most recent unexecuted task
977 <     * without executing it
976 >     * pollNextLocalTask returns most recent unexecuted task without
977 >     * executing it
978       */
979      public void testPollNextLocalTask() {
980 <        RecursiveAction a = new RecursiveAction() {
981 <            public void compute() {
980 >        RecursiveAction a = new CheckedRecursiveAction() {
981 >            public void realCompute() {
982                  AsyncFib g = new AsyncFib(9);
983 <                g.fork();
983 >                assertSame(g, g.fork());
984                  AsyncFib f = new AsyncFib(8);
985 <                f.fork();
986 <                threadAssertTrue(pollNextLocalTask() == f);
985 >                assertSame(f, f.fork());
986 >                assertSame(f, pollNextLocalTask());
987                  helpQuiesce();
988 <                threadAssertFalse(f.isDone());
988 >                checkNotDone(f);
989 >                assertEquals(34, g.number);
990 >                checkCompletedNormally(g);
991              }};
992 <        singletonPool.invoke(a);
992 >        testInvokeOnPool(singletonPool(), a);
993      }
994  
995      /**
996 <     * pollTask returns an unexecuted task
876 <     * without executing it
996 >     * pollTask returns an unexecuted task without executing it
997       */
998      public void testPollTask() {
999 <        RecursiveAction a = new RecursiveAction() {
1000 <            public void compute() {
999 >        RecursiveAction a = new CheckedRecursiveAction() {
1000 >            public void realCompute() {
1001                  AsyncFib g = new AsyncFib(9);
1002 <                g.fork();
1002 >                assertSame(g, g.fork());
1003                  AsyncFib f = new AsyncFib(8);
1004 <                f.fork();
1005 <                threadAssertTrue(pollTask() == f);
1004 >                assertSame(f, f.fork());
1005 >                assertSame(f, pollTask());
1006                  helpQuiesce();
1007 <                threadAssertFalse(f.isDone());
1008 <                threadAssertTrue(g.isDone());
1007 >                checkNotDone(f);
1008 >                checkCompletedNormally(g);
1009              }};
1010 <        singletonPool.invoke(a);
1010 >        testInvokeOnPool(singletonPool(), a);
1011      }
1012  
1013      /**
1014       * peekNextLocalTask returns least recent unexecuted task in async mode
1015       */
1016      public void testPeekNextLocalTaskAsync() {
1017 <        RecursiveAction a = new RecursiveAction() {
1018 <            public void compute() {
1017 >        RecursiveAction a = new CheckedRecursiveAction() {
1018 >            public void realCompute() {
1019                  AsyncFib g = new AsyncFib(9);
1020 <                g.fork();
1020 >                assertSame(g, g.fork());
1021                  AsyncFib f = new AsyncFib(8);
1022 <                f.fork();
1023 <                threadAssertTrue(peekNextLocalTask() == g);
1024 <                f.join();
1022 >                assertSame(f, f.fork());
1023 >                assertSame(g, peekNextLocalTask());
1024 >                assertNull(f.join());
1025                  helpQuiesce();
1026 <                threadAssertTrue(f.isDone());
1026 >                checkCompletedNormally(f);
1027 >                assertEquals(34, g.number);
1028 >                checkCompletedNormally(g);
1029              }};
1030 <        asyncSingletonPool.invoke(a);
1030 >        testInvokeOnPool(asyncSingletonPool(), a);
1031      }
1032  
1033      /**
1034 <     * pollNextLocalTask returns least recent unexecuted task
1035 <     * without executing it, in async mode
1034 >     * pollNextLocalTask returns least recent unexecuted task without
1035 >     * executing it, in async mode
1036       */
1037      public void testPollNextLocalTaskAsync() {
1038 <        RecursiveAction a = new RecursiveAction() {
1039 <            public void compute() {
1038 >        RecursiveAction a = new CheckedRecursiveAction() {
1039 >            public void realCompute() {
1040                  AsyncFib g = new AsyncFib(9);
1041 <                g.fork();
1041 >                assertSame(g, g.fork());
1042                  AsyncFib f = new AsyncFib(8);
1043 <                f.fork();
1044 <                threadAssertTrue(pollNextLocalTask() == g);
1043 >                assertSame(f, f.fork());
1044 >                assertSame(g, pollNextLocalTask());
1045                  helpQuiesce();
1046 <                threadAssertTrue(f.isDone());
1047 <                threadAssertFalse(g.isDone());
1046 >                assertEquals(21, f.number);
1047 >                checkCompletedNormally(f);
1048 >                checkNotDone(g);
1049              }};
1050 <        asyncSingletonPool.invoke(a);
1050 >        testInvokeOnPool(asyncSingletonPool(), a);
1051      }
1052  
1053      /**
1054 <     * pollTask returns an unexecuted task
1055 <     * without executing it, in async mode
1054 >     * pollTask returns an unexecuted task without executing it, in
1055 >     * async mode
1056       */
1057      public void testPollTaskAsync() {
1058 <        RecursiveAction a = new RecursiveAction() {
1059 <            public void compute() {
1058 >        RecursiveAction a = new CheckedRecursiveAction() {
1059 >            public void realCompute() {
1060                  AsyncFib g = new AsyncFib(9);
1061 <                g.fork();
1061 >                assertSame(g, g.fork());
1062                  AsyncFib f = new AsyncFib(8);
1063 <                f.fork();
1064 <                threadAssertTrue(pollTask() == g);
1063 >                assertSame(f, f.fork());
1064 >                assertSame(g, pollTask());
1065                  helpQuiesce();
1066 <                threadAssertTrue(f.isDone());
1067 <                threadAssertFalse(g.isDone());
1066 >                assertEquals(21, f.number);
1067 >                checkCompletedNormally(f);
1068 >                checkNotDone(g);
1069 >            }};
1070 >        testInvokeOnPool(asyncSingletonPool(), a);
1071 >    }
1072 >
1073 >    // versions for singleton pools
1074 >
1075 >    /**
1076 >     * invoke returns when task completes normally.
1077 >     * isCompletedAbnormally and isCancelled return false for normally
1078 >     * completed tasks. getRawResult of a RecursiveAction returns null;
1079 >     */
1080 >    public void testInvokeSingleton() {
1081 >        RecursiveAction a = new CheckedRecursiveAction() {
1082 >            public void realCompute() {
1083 >                AsyncFib f = new AsyncFib(8);
1084 >                assertNull(f.invoke());
1085 >                assertEquals(21, f.number);
1086 >                checkCompletedNormally(f);
1087 >            }};
1088 >        testInvokeOnPool(singletonPool(), a);
1089 >    }
1090 >
1091 >    /**
1092 >     * quietlyInvoke task returns when task completes normally.
1093 >     * isCompletedAbnormally and isCancelled return false for normally
1094 >     * completed tasks
1095 >     */
1096 >    public void testQuietlyInvokeSingleton() {
1097 >        RecursiveAction a = new CheckedRecursiveAction() {
1098 >            public void realCompute() {
1099 >                AsyncFib f = new AsyncFib(8);
1100 >                f.quietlyInvoke();
1101 >                assertEquals(21, f.number);
1102 >                checkCompletedNormally(f);
1103 >            }};
1104 >        testInvokeOnPool(singletonPool(), a);
1105 >    }
1106 >
1107 >    /**
1108 >     * join of a forked task returns when task completes
1109 >     */
1110 >    public void testForkJoinSingleton() {
1111 >        RecursiveAction a = new CheckedRecursiveAction() {
1112 >            public void realCompute() {
1113 >                AsyncFib f = new AsyncFib(8);
1114 >                assertSame(f, f.fork());
1115 >                assertNull(f.join());
1116 >                assertEquals(21, f.number);
1117 >                checkCompletedNormally(f);
1118 >            }};
1119 >        testInvokeOnPool(singletonPool(), a);
1120 >    }
1121 >
1122 >    /**
1123 >     * get of a forked task returns when task completes
1124 >     */
1125 >    public void testForkGetSingleton() {
1126 >        RecursiveAction a = new CheckedRecursiveAction() {
1127 >            public void realCompute() throws Exception {
1128 >                AsyncFib f = new AsyncFib(8);
1129 >                assertSame(f, f.fork());
1130 >                assertNull(f.get());
1131 >                assertEquals(21, f.number);
1132 >                checkCompletedNormally(f);
1133 >            }};
1134 >        testInvokeOnPool(singletonPool(), a);
1135 >    }
1136 >
1137 >    /**
1138 >     * timed get of a forked task returns when task completes
1139 >     */
1140 >    public void testForkTimedGetSingleton() {
1141 >        RecursiveAction a = new CheckedRecursiveAction() {
1142 >            public void realCompute() throws Exception {
1143 >                AsyncFib f = new AsyncFib(8);
1144 >                assertSame(f, f.fork());
1145 >                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1146 >                assertEquals(21, f.number);
1147 >                checkCompletedNormally(f);
1148 >            }};
1149 >        testInvokeOnPool(singletonPool(), a);
1150 >    }
1151 >
1152 >    /**
1153 >     * timed get with null time unit throws NPE
1154 >     */
1155 >    public void testForkTimedGetNPESingleton() {
1156 >        RecursiveAction a = new CheckedRecursiveAction() {
1157 >            public void realCompute() throws Exception {
1158 >                AsyncFib f = new AsyncFib(8);
1159 >                assertSame(f, f.fork());
1160 >                try {
1161 >                    f.get(5L, null);
1162 >                    shouldThrow();
1163 >                } catch (NullPointerException success) {}
1164 >            }};
1165 >        testInvokeOnPool(singletonPool(), a);
1166 >    }
1167 >
1168 >    /**
1169 >     * quietlyJoin of a forked task returns when task completes
1170 >     */
1171 >    public void testForkQuietlyJoinSingleton() {
1172 >        RecursiveAction a = new CheckedRecursiveAction() {
1173 >            public void realCompute() {
1174 >                AsyncFib f = new AsyncFib(8);
1175 >                assertSame(f, f.fork());
1176 >                f.quietlyJoin();
1177 >                assertEquals(21, f.number);
1178 >                checkCompletedNormally(f);
1179 >            }};
1180 >        testInvokeOnPool(singletonPool(), a);
1181 >    }
1182 >
1183 >
1184 >    /**
1185 >     * helpQuiesce returns when tasks are complete.
1186 >     * getQueuedTaskCount returns 0 when quiescent
1187 >     */
1188 >    public void testForkHelpQuiesceSingleton() {
1189 >        RecursiveAction a = new CheckedRecursiveAction() {
1190 >            public void realCompute() {
1191 >                AsyncFib f = new AsyncFib(8);
1192 >                assertSame(f, f.fork());
1193 >                f.helpQuiesce();
1194 >                assertEquals(0, getQueuedTaskCount());
1195 >                assertEquals(21, f.number);
1196 >                checkCompletedNormally(f);
1197 >            }};
1198 >        testInvokeOnPool(singletonPool(), a);
1199 >    }
1200 >
1201 >
1202 >    /**
1203 >     * invoke task throws exception when task completes abnormally
1204 >     */
1205 >    public void testAbnormalInvokeSingleton() {
1206 >        RecursiveAction a = new CheckedRecursiveAction() {
1207 >            public void realCompute() {
1208 >                FailingAsyncFib f = new FailingAsyncFib(8);
1209 >                try {
1210 >                    f.invoke();
1211 >                    shouldThrow();
1212 >                } catch (FJException success) {
1213 >                    checkCompletedAbnormally(f, success);
1214 >                }
1215 >            }};
1216 >        testInvokeOnPool(singletonPool(), a);
1217 >    }
1218 >
1219 >    /**
1220 >     * quietlyInvoke task returns when task completes abnormally
1221 >     */
1222 >    public void testAbnormalQuietlyInvokeSingleton() {
1223 >        RecursiveAction a = new CheckedRecursiveAction() {
1224 >            public void realCompute() {
1225 >                FailingAsyncFib f = new FailingAsyncFib(8);
1226 >                f.quietlyInvoke();
1227 >                assertTrue(f.getException() instanceof FJException);
1228 >                checkCompletedAbnormally(f, f.getException());
1229 >            }};
1230 >        testInvokeOnPool(singletonPool(), a);
1231 >    }
1232 >
1233 >    /**
1234 >     * join of a forked task throws exception when task completes abnormally
1235 >     */
1236 >    public void testAbnormalForkJoinSingleton() {
1237 >        RecursiveAction a = new CheckedRecursiveAction() {
1238 >            public void realCompute() {
1239 >                FailingAsyncFib f = new FailingAsyncFib(8);
1240 >                assertSame(f, f.fork());
1241 >                try {
1242 >                    f.join();
1243 >                    shouldThrow();
1244 >                } catch (FJException success) {
1245 >                    checkCompletedAbnormally(f, success);
1246 >                }
1247 >            }};
1248 >        testInvokeOnPool(singletonPool(), a);
1249 >    }
1250 >
1251 >    /**
1252 >     * get of a forked task throws exception when task completes abnormally
1253 >     */
1254 >    public void testAbnormalForkGetSingleton() {
1255 >        RecursiveAction a = new CheckedRecursiveAction() {
1256 >            public void realCompute() throws Exception {
1257 >                FailingAsyncFib f = new FailingAsyncFib(8);
1258 >                assertSame(f, f.fork());
1259 >                try {
1260 >                    f.get();
1261 >                    shouldThrow();
1262 >                } catch (ExecutionException success) {
1263 >                    Throwable cause = success.getCause();
1264 >                    assertTrue(cause instanceof FJException);
1265 >                    checkCompletedAbnormally(f, cause);
1266 >                }
1267 >            }};
1268 >        testInvokeOnPool(singletonPool(), a);
1269 >    }
1270 >
1271 >    /**
1272 >     * timed get of a forked task throws exception when task completes abnormally
1273 >     */
1274 >    public void testAbnormalForkTimedGetSingleton() {
1275 >        RecursiveAction a = new CheckedRecursiveAction() {
1276 >            public void realCompute() throws Exception {
1277 >                FailingAsyncFib f = new FailingAsyncFib(8);
1278 >                assertSame(f, f.fork());
1279 >                try {
1280 >                    f.get(LONG_DELAY_MS, MILLISECONDS);
1281 >                    shouldThrow();
1282 >                } catch (ExecutionException success) {
1283 >                    Throwable cause = success.getCause();
1284 >                    assertTrue(cause instanceof FJException);
1285 >                    checkCompletedAbnormally(f, cause);
1286 >                }
1287 >            }};
1288 >        testInvokeOnPool(singletonPool(), a);
1289 >    }
1290 >
1291 >    /**
1292 >     * quietlyJoin of a forked task returns when task completes abnormally
1293 >     */
1294 >    public void testAbnormalForkQuietlyJoinSingleton() {
1295 >        RecursiveAction a = new CheckedRecursiveAction() {
1296 >            public void realCompute() {
1297 >                FailingAsyncFib f = new FailingAsyncFib(8);
1298 >                assertSame(f, f.fork());
1299 >                f.quietlyJoin();
1300 >                assertTrue(f.getException() instanceof FJException);
1301 >                checkCompletedAbnormally(f, f.getException());
1302 >            }};
1303 >        testInvokeOnPool(singletonPool(), a);
1304 >    }
1305 >
1306 >    /**
1307 >     * invoke task throws exception when task cancelled
1308 >     */
1309 >    public void testCancelledInvokeSingleton() {
1310 >        RecursiveAction a = new CheckedRecursiveAction() {
1311 >            public void realCompute() {
1312 >                AsyncFib f = new AsyncFib(8);
1313 >                assertTrue(f.cancel(true));
1314 >                try {
1315 >                    f.invoke();
1316 >                    shouldThrow();
1317 >                } catch (CancellationException success) {
1318 >                    checkCancelled(f);
1319 >                }
1320 >            }};
1321 >        testInvokeOnPool(singletonPool(), a);
1322 >    }
1323 >
1324 >    /**
1325 >     * join of a forked task throws exception when task cancelled
1326 >     */
1327 >    public void testCancelledForkJoinSingleton() {
1328 >        RecursiveAction a = new CheckedRecursiveAction() {
1329 >            public void realCompute() {
1330 >                AsyncFib f = new AsyncFib(8);
1331 >                assertTrue(f.cancel(true));
1332 >                assertSame(f, f.fork());
1333 >                try {
1334 >                    f.join();
1335 >                    shouldThrow();
1336 >                } catch (CancellationException success) {
1337 >                    checkCancelled(f);
1338 >                }
1339 >            }};
1340 >        testInvokeOnPool(singletonPool(), a);
1341 >    }
1342 >
1343 >    /**
1344 >     * get of a forked task throws exception when task cancelled
1345 >     */
1346 >    public void testCancelledForkGetSingleton() {
1347 >        RecursiveAction a = new CheckedRecursiveAction() {
1348 >            public void realCompute() throws Exception {
1349 >                AsyncFib f = new AsyncFib(8);
1350 >                assertTrue(f.cancel(true));
1351 >                assertSame(f, f.fork());
1352 >                try {
1353 >                    f.get();
1354 >                    shouldThrow();
1355 >                } catch (CancellationException success) {
1356 >                    checkCancelled(f);
1357 >                }
1358 >            }};
1359 >        testInvokeOnPool(singletonPool(), a);
1360 >    }
1361 >
1362 >    /**
1363 >     * timed get of a forked task throws exception when task cancelled
1364 >     */
1365 >    public void testCancelledForkTimedGetSingleton() throws Exception {
1366 >        RecursiveAction a = new CheckedRecursiveAction() {
1367 >            public void realCompute() throws Exception {
1368 >                AsyncFib f = new AsyncFib(8);
1369 >                assertTrue(f.cancel(true));
1370 >                assertSame(f, f.fork());
1371 >                try {
1372 >                    f.get(LONG_DELAY_MS, MILLISECONDS);
1373 >                    shouldThrow();
1374 >                } catch (CancellationException success) {
1375 >                    checkCancelled(f);
1376 >                }
1377 >            }};
1378 >        testInvokeOnPool(singletonPool(), a);
1379 >    }
1380 >
1381 >    /**
1382 >     * quietlyJoin of a forked task returns when task cancelled
1383 >     */
1384 >    public void testCancelledForkQuietlyJoinSingleton() {
1385 >        RecursiveAction a = new CheckedRecursiveAction() {
1386 >            public void realCompute() {
1387 >                AsyncFib f = new AsyncFib(8);
1388 >                assertTrue(f.cancel(true));
1389 >                assertSame(f, f.fork());
1390 >                f.quietlyJoin();
1391 >                checkCancelled(f);
1392 >            }};
1393 >        testInvokeOnPool(singletonPool(), a);
1394 >    }
1395 >
1396 >    /**
1397 >     * invoke task throws exception after invoking completeExceptionally
1398 >     */
1399 >    public void testCompleteExceptionallySingleton() {
1400 >        RecursiveAction a = new CheckedRecursiveAction() {
1401 >            public void realCompute() {
1402 >                AsyncFib f = new AsyncFib(8);
1403 >                f.completeExceptionally(new FJException());
1404 >                try {
1405 >                    f.invoke();
1406 >                    shouldThrow();
1407 >                } catch (FJException success) {
1408 >                    checkCompletedAbnormally(f, success);
1409 >                }
1410 >            }};
1411 >        testInvokeOnPool(singletonPool(), a);
1412 >    }
1413 >
1414 >    /**
1415 >     * invokeAll(t1, t2) invokes all task arguments
1416 >     */
1417 >    public void testInvokeAll2Singleton() {
1418 >        RecursiveAction a = new CheckedRecursiveAction() {
1419 >            public void realCompute() {
1420 >                AsyncFib f = new AsyncFib(8);
1421 >                AsyncFib g = new AsyncFib(9);
1422 >                invokeAll(f, g);
1423 >                assertEquals(21, f.number);
1424 >                assertEquals(34, g.number);
1425 >                checkCompletedNormally(f);
1426 >                checkCompletedNormally(g);
1427 >            }};
1428 >        testInvokeOnPool(singletonPool(), a);
1429 >    }
1430 >
1431 >    /**
1432 >     * invokeAll(tasks) with 1 argument invokes task
1433 >     */
1434 >    public void testInvokeAll1Singleton() {
1435 >        RecursiveAction a = new CheckedRecursiveAction() {
1436 >            public void realCompute() {
1437 >                AsyncFib f = new AsyncFib(8);
1438 >                invokeAll(f);
1439 >                checkCompletedNormally(f);
1440 >                assertEquals(21, f.number);
1441 >            }};
1442 >        testInvokeOnPool(singletonPool(), a);
1443 >    }
1444 >
1445 >    /**
1446 >     * invokeAll(tasks) with > 2 argument invokes tasks
1447 >     */
1448 >    public void testInvokeAll3Singleton() {
1449 >        RecursiveAction a = new CheckedRecursiveAction() {
1450 >            public void realCompute() {
1451 >                AsyncFib f = new AsyncFib(8);
1452 >                AsyncFib g = new AsyncFib(9);
1453 >                AsyncFib h = new AsyncFib(7);
1454 >                invokeAll(f, g, h);
1455 >                assertEquals(21, f.number);
1456 >                assertEquals(34, g.number);
1457 >                assertEquals(13, h.number);
1458 >                checkCompletedNormally(f);
1459 >                checkCompletedNormally(g);
1460 >                checkCompletedNormally(h);
1461 >            }};
1462 >        testInvokeOnPool(singletonPool(), a);
1463 >    }
1464 >
1465 >    /**
1466 >     * invokeAll(collection) invokes all tasks in the collection
1467 >     */
1468 >    public void testInvokeAllCollectionSingleton() {
1469 >        RecursiveAction a = new CheckedRecursiveAction() {
1470 >            public void realCompute() {
1471 >                AsyncFib f = new AsyncFib(8);
1472 >                AsyncFib g = new AsyncFib(9);
1473 >                AsyncFib h = new AsyncFib(7);
1474 >                HashSet set = new HashSet();
1475 >                set.add(f);
1476 >                set.add(g);
1477 >                set.add(h);
1478 >                invokeAll(set);
1479 >                assertEquals(21, f.number);
1480 >                assertEquals(34, g.number);
1481 >                assertEquals(13, h.number);
1482 >                checkCompletedNormally(f);
1483 >                checkCompletedNormally(g);
1484 >                checkCompletedNormally(h);
1485 >            }};
1486 >        testInvokeOnPool(singletonPool(), a);
1487 >    }
1488 >
1489 >
1490 >    /**
1491 >     * invokeAll(tasks) with any null task throws NPE
1492 >     */
1493 >    public void testInvokeAllNPESingleton() {
1494 >        RecursiveAction a = new CheckedRecursiveAction() {
1495 >            public void realCompute() {
1496 >                AsyncFib f = new AsyncFib(8);
1497 >                AsyncFib g = new AsyncFib(9);
1498 >                AsyncFib h = null;
1499 >                try {
1500 >                    invokeAll(f, g, h);
1501 >                    shouldThrow();
1502 >                } catch (NullPointerException success) {}
1503 >            }};
1504 >        testInvokeOnPool(singletonPool(), a);
1505 >    }
1506 >
1507 >    /**
1508 >     * invokeAll(t1, t2) throw exception if any task does
1509 >     */
1510 >    public void testAbnormalInvokeAll2Singleton() {
1511 >        RecursiveAction a = new CheckedRecursiveAction() {
1512 >            public void realCompute() {
1513 >                AsyncFib f = new AsyncFib(8);
1514 >                FailingAsyncFib g = new FailingAsyncFib(9);
1515 >                try {
1516 >                    invokeAll(f, g);
1517 >                    shouldThrow();
1518 >                } catch (FJException success) {
1519 >                    checkCompletedAbnormally(g, success);
1520 >                }
1521              }};
1522 <        asyncSingletonPool.invoke(a);
1522 >        testInvokeOnPool(singletonPool(), a);
1523      }
1524 +
1525 +    /**
1526 +     * invokeAll(tasks) with 1 argument throws exception if task does
1527 +     */
1528 +    public void testAbnormalInvokeAll1Singleton() {
1529 +        RecursiveAction a = new CheckedRecursiveAction() {
1530 +            public void realCompute() {
1531 +                FailingAsyncFib g = new FailingAsyncFib(9);
1532 +                try {
1533 +                    invokeAll(g);
1534 +                    shouldThrow();
1535 +                } catch (FJException success) {
1536 +                    checkCompletedAbnormally(g, success);
1537 +                }
1538 +            }};
1539 +        testInvokeOnPool(singletonPool(), a);
1540 +    }
1541 +
1542 +    /**
1543 +     * invokeAll(tasks) with > 2 argument throws exception if any task does
1544 +     */
1545 +    public void testAbnormalInvokeAll3Singleton() {
1546 +        RecursiveAction a = new CheckedRecursiveAction() {
1547 +            public void realCompute() {
1548 +                AsyncFib f = new AsyncFib(8);
1549 +                FailingAsyncFib g = new FailingAsyncFib(9);
1550 +                AsyncFib h = new AsyncFib(7);
1551 +                try {
1552 +                    invokeAll(f, g, h);
1553 +                    shouldThrow();
1554 +                } catch (FJException success) {
1555 +                    checkCompletedAbnormally(g, success);
1556 +                }
1557 +            }};
1558 +        testInvokeOnPool(singletonPool(), a);
1559 +    }
1560 +
1561 +    /**
1562 +     * invokeAll(collection)  throws exception if any task does
1563 +     */
1564 +    public void testAbnormalInvokeAllCollectionSingleton() {
1565 +        RecursiveAction a = new CheckedRecursiveAction() {
1566 +            public void realCompute() {
1567 +                FailingAsyncFib f = new FailingAsyncFib(8);
1568 +                AsyncFib g = new AsyncFib(9);
1569 +                AsyncFib h = new AsyncFib(7);
1570 +                HashSet set = new HashSet();
1571 +                set.add(f);
1572 +                set.add(g);
1573 +                set.add(h);
1574 +                try {
1575 +                    invokeAll(set);
1576 +                    shouldThrow();
1577 +                } catch (FJException success) {
1578 +                    checkCompletedAbnormally(f, success);
1579 +                }
1580 +            }};
1581 +        testInvokeOnPool(singletonPool(), a);
1582 +    }
1583 +
1584   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines