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.33 by jsr166, Fri May 27 19:42:42 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines