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.19 by jsr166, Mon Sep 27 19:15:16 2010 UTC vs.
Revision 1.45 by jsr166, Wed Oct 7 22:39:31 2015 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.concurrent.ExecutionException;
6 >
7 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 > import static java.util.concurrent.TimeUnit.SECONDS;
9 >
10 > import java.util.Arrays;
11 > import java.util.Collections;
12 > import java.util.HashSet;
13 > import java.util.List;
14   import java.util.concurrent.CancellationException;
15 + import java.util.concurrent.ExecutionException;
16   import java.util.concurrent.ForkJoinPool;
17   import java.util.concurrent.ForkJoinTask;
10 import java.util.concurrent.ForkJoinWorkerThread;
18   import java.util.concurrent.RecursiveAction;
19 < import java.util.concurrent.TimeUnit;
19 > import java.util.concurrent.TimeoutException;
20   import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
21 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
22 < import java.util.HashSet;
23 < import junit.framework.*;
21 >
22 > import junit.framework.Test;
23 > import junit.framework.TestSuite;
24  
25   public class ForkJoinTaskTest extends JSR166TestCase {
26  
27      public static void main(String[] args) {
28 <        junit.textui.TestRunner.run(suite());
28 >        main(suite(), args);
29      }
30  
31      public static Test suite() {
32          return new TestSuite(ForkJoinTaskTest.class);
33      }
34  
35 +    // Runs with "mainPool" use > 1 thread. singletonPool tests use 1
36 +    static final int mainPoolSize =
37 +        Math.max(2, Runtime.getRuntime().availableProcessors());
38 +
39      private static ForkJoinPool mainPool() {
40 <        return new ForkJoinPool();
40 >        return new ForkJoinPool(mainPoolSize);
41      }
42  
43      private static ForkJoinPool singletonPool() {
# Line 40 | Line 51 | public class ForkJoinTaskTest extends JS
51      }
52  
53      private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
54 <        try {
54 >        try (PoolCleaner cleaner = cleaner(pool)) {
55              assertFalse(a.isDone());
56              assertFalse(a.isCompletedNormally());
57              assertFalse(a.isCompletedAbnormally());
58              assertFalse(a.isCancelled());
59              assertNull(a.getException());
60 +            assertNull(a.getRawResult());
61  
62              assertNull(pool.invoke(a));
63  
# Line 54 | Line 66 | public class ForkJoinTaskTest extends JS
66              assertFalse(a.isCompletedAbnormally());
67              assertFalse(a.isCancelled());
68              assertNull(a.getException());
69 <        } finally {
70 <            joinPool(pool);
69 >            assertNull(a.getRawResult());
70 >        }
71 >    }
72 >
73 >    void checkNotDone(ForkJoinTask a) {
74 >        assertFalse(a.isDone());
75 >        assertFalse(a.isCompletedNormally());
76 >        assertFalse(a.isCompletedAbnormally());
77 >        assertFalse(a.isCancelled());
78 >        assertNull(a.getException());
79 >        assertNull(a.getRawResult());
80 >
81 >        try {
82 >            a.get(0L, SECONDS);
83 >            shouldThrow();
84 >        } catch (TimeoutException success) {
85 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
86 >    }
87 >
88 >    <T> void checkCompletedNormally(ForkJoinTask<T> a) {
89 >        checkCompletedNormally(a, null);
90 >    }
91 >
92 >    <T> void checkCompletedNormally(ForkJoinTask<T> a, T expected) {
93 >        assertTrue(a.isDone());
94 >        assertFalse(a.isCancelled());
95 >        assertTrue(a.isCompletedNormally());
96 >        assertFalse(a.isCompletedAbnormally());
97 >        assertNull(a.getException());
98 >        assertSame(expected, a.getRawResult());
99 >
100 >        {
101 >            Thread.currentThread().interrupt();
102 >            long startTime = System.nanoTime();
103 >            assertSame(expected, a.join());
104 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
105 >            Thread.interrupted();
106 >        }
107 >
108 >        {
109 >            Thread.currentThread().interrupt();
110 >            long startTime = System.nanoTime();
111 >            a.quietlyJoin();        // should be no-op
112 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
113 >            Thread.interrupted();
114          }
115 +
116 +        assertFalse(a.cancel(false));
117 +        assertFalse(a.cancel(true));
118 +        try {
119 +            assertSame(expected, a.get());
120 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
121 +        try {
122 +            assertSame(expected, a.get(5L, SECONDS));
123 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
124 +    }
125 +
126 +    void checkCancelled(ForkJoinTask a) {
127 +        assertTrue(a.isDone());
128 +        assertTrue(a.isCancelled());
129 +        assertFalse(a.isCompletedNormally());
130 +        assertTrue(a.isCompletedAbnormally());
131 +        assertTrue(a.getException() instanceof CancellationException);
132 +        assertNull(a.getRawResult());
133 +        assertTrue(a.cancel(false));
134 +        assertTrue(a.cancel(true));
135 +
136 +        try {
137 +            Thread.currentThread().interrupt();
138 +            a.join();
139 +            shouldThrow();
140 +        } catch (CancellationException success) {
141 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
142 +        Thread.interrupted();
143 +
144 +        {
145 +            long startTime = System.nanoTime();
146 +            a.quietlyJoin();        // should be no-op
147 +            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
148 +        }
149 +
150 +        try {
151 +            a.get();
152 +            shouldThrow();
153 +        } catch (CancellationException success) {
154 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
155 +
156 +        try {
157 +            a.get(5L, SECONDS);
158 +            shouldThrow();
159 +        } catch (CancellationException success) {
160 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
161 +    }
162 +
163 +    void checkCompletedAbnormally(ForkJoinTask a, Throwable t) {
164 +        assertTrue(a.isDone());
165 +        assertFalse(a.isCancelled());
166 +        assertFalse(a.isCompletedNormally());
167 +        assertTrue(a.isCompletedAbnormally());
168 +        assertSame(t.getClass(), a.getException().getClass());
169 +        assertNull(a.getRawResult());
170 +        assertFalse(a.cancel(false));
171 +        assertFalse(a.cancel(true));
172 +
173 +        try {
174 +            Thread.currentThread().interrupt();
175 +            a.join();
176 +            shouldThrow();
177 +        } catch (Throwable expected) {
178 +            assertSame(t.getClass(), expected.getClass());
179 +        }
180 +        Thread.interrupted();
181 +
182 +        {
183 +            long startTime = System.nanoTime();
184 +            a.quietlyJoin();        // should be no-op
185 +            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
186 +        }
187 +
188 +        try {
189 +            a.get();
190 +            shouldThrow();
191 +        } catch (ExecutionException success) {
192 +            assertSame(t.getClass(), success.getCause().getClass());
193 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
194 +
195 +        try {
196 +            a.get(5L, SECONDS);
197 +            shouldThrow();
198 +        } catch (ExecutionException success) {
199 +            assertSame(t.getClass(), success.getCause().getClass());
200 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
201      }
202  
203      /*
# Line 67 | Line 208 | public class ForkJoinTaskTest extends JS
208       * differently than supplied Recursive forms.
209       */
210  
211 <    static final class FJException extends RuntimeException {
211 >    public static final class FJException extends RuntimeException {
212          FJException() { super(); }
213      }
214  
# Line 78 | Line 219 | public class ForkJoinTaskTest extends JS
219              AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class,
220                                                   "controlState");
221  
222 <        private BinaryAsyncAction parent;
222 >        private volatile BinaryAsyncAction parent;
223  
224 <        private BinaryAsyncAction sibling;
224 >        private volatile BinaryAsyncAction sibling;
225  
226          protected BinaryAsyncAction() {
227          }
# Line 211 | Line 352 | public class ForkJoinTaskTest extends JS
352          }
353      }
354  
214
355      static final class FailingAsyncFib extends BinaryAsyncAction {
356          int number;
357          public FailingAsyncFib(int n) {
# Line 243 | Line 383 | public class ForkJoinTaskTest extends JS
383      /**
384       * invoke returns when task completes normally.
385       * isCompletedAbnormally and isCancelled return false for normally
386 <     * completed tasks. getRawResult of a RecursiveAction returns null;
386 >     * completed tasks; getRawResult returns null.
387       */
388      public void testInvoke() {
389          RecursiveAction a = new CheckedRecursiveAction() {
390 <            public void realCompute() {
390 >            protected void realCompute() {
391                  AsyncFib f = new AsyncFib(8);
392                  assertNull(f.invoke());
393                  assertEquals(21, f.number);
394 <                assertTrue(f.isDone());
255 <                assertFalse(f.isCancelled());
256 <                assertFalse(f.isCompletedAbnormally());
257 <                assertNull(f.getRawResult());
394 >                checkCompletedNormally(f);
395              }};
396          testInvokeOnPool(mainPool(), a);
397      }
# Line 266 | Line 403 | public class ForkJoinTaskTest extends JS
403       */
404      public void testQuietlyInvoke() {
405          RecursiveAction a = new CheckedRecursiveAction() {
406 <            public void realCompute() {
406 >            protected void realCompute() {
407                  AsyncFib f = new AsyncFib(8);
408                  f.quietlyInvoke();
409                  assertEquals(21, f.number);
410 <                assertTrue(f.isDone());
274 <                assertFalse(f.isCancelled());
275 <                assertFalse(f.isCompletedAbnormally());
276 <                assertNull(f.getRawResult());
410 >                checkCompletedNormally(f);
411              }};
412          testInvokeOnPool(mainPool(), a);
413      }
# Line 283 | Line 417 | public class ForkJoinTaskTest extends JS
417       */
418      public void testForkJoin() {
419          RecursiveAction a = new CheckedRecursiveAction() {
420 <            public void realCompute() {
420 >            protected void realCompute() {
421                  AsyncFib f = new AsyncFib(8);
422                  assertSame(f, f.fork());
423                  assertNull(f.join());
424                  assertEquals(21, f.number);
425 <                assertTrue(f.isDone());
292 <                assertNull(f.getRawResult());
425 >                checkCompletedNormally(f);
426              }};
427          testInvokeOnPool(mainPool(), a);
428      }
# Line 299 | Line 432 | public class ForkJoinTaskTest extends JS
432       */
433      public void testForkGet() {
434          RecursiveAction a = new CheckedRecursiveAction() {
435 <            public void realCompute() throws Exception {
435 >            protected void realCompute() throws Exception {
436                  AsyncFib f = new AsyncFib(8);
437                  assertSame(f, f.fork());
438                  assertNull(f.get());
439                  assertEquals(21, f.number);
440 <                assertTrue(f.isDone());
440 >                checkCompletedNormally(f);
441              }};
442          testInvokeOnPool(mainPool(), a);
443      }
# Line 314 | Line 447 | public class ForkJoinTaskTest extends JS
447       */
448      public void testForkTimedGet() {
449          RecursiveAction a = new CheckedRecursiveAction() {
450 <            public void realCompute() throws Exception {
450 >            protected void realCompute() throws Exception {
451                  AsyncFib f = new AsyncFib(8);
452                  assertSame(f, f.fork());
453                  assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
454                  assertEquals(21, f.number);
455 <                assertTrue(f.isDone());
455 >                checkCompletedNormally(f);
456              }};
457          testInvokeOnPool(mainPool(), a);
458      }
# Line 329 | Line 462 | public class ForkJoinTaskTest extends JS
462       */
463      public void testForkTimedGetNPE() {
464          RecursiveAction a = new CheckedRecursiveAction() {
465 <            public void realCompute() throws Exception {
465 >            protected void realCompute() throws Exception {
466                  AsyncFib f = new AsyncFib(8);
467                  assertSame(f, f.fork());
468                  try {
# Line 345 | Line 478 | public class ForkJoinTaskTest extends JS
478       */
479      public void testForkQuietlyJoin() {
480          RecursiveAction a = new CheckedRecursiveAction() {
481 <            public void realCompute() {
481 >            protected void realCompute() {
482                  AsyncFib f = new AsyncFib(8);
483                  assertSame(f, f.fork());
484                  f.quietlyJoin();
485                  assertEquals(21, f.number);
486 <                assertTrue(f.isDone());
486 >                checkCompletedNormally(f);
487              }};
488          testInvokeOnPool(mainPool(), a);
489      }
490  
358
491      /**
492       * helpQuiesce returns when tasks are complete.
493       * getQueuedTaskCount returns 0 when quiescent
494       */
495      public void testForkHelpQuiesce() {
496          RecursiveAction a = new CheckedRecursiveAction() {
497 <            public void realCompute() {
497 >            protected void realCompute() {
498                  AsyncFib f = new AsyncFib(8);
499                  assertSame(f, f.fork());
500 <                f.helpQuiesce();
500 >                helpQuiesce();
501                  assertEquals(21, f.number);
370                assertTrue(f.isDone());
502                  assertEquals(0, getQueuedTaskCount());
503 +                checkCompletedNormally(f);
504              }};
505          testInvokeOnPool(mainPool(), a);
506      }
507  
376
508      /**
509       * invoke task throws exception when task completes abnormally
510       */
511      public void testAbnormalInvoke() {
512          RecursiveAction a = new CheckedRecursiveAction() {
513 <            public void realCompute() {
513 >            protected void realCompute() {
514                  FailingAsyncFib f = new FailingAsyncFib(8);
515                  try {
516                      f.invoke();
517                      shouldThrow();
518 <                } catch (FJException success) {}
518 >                } catch (FJException success) {
519 >                    checkCompletedAbnormally(f, success);
520 >                }
521              }};
522          testInvokeOnPool(mainPool(), a);
523      }
# Line 394 | Line 527 | public class ForkJoinTaskTest extends JS
527       */
528      public void testAbnormalQuietlyInvoke() {
529          RecursiveAction a = new CheckedRecursiveAction() {
530 <            public void realCompute() {
530 >            protected void realCompute() {
531                  FailingAsyncFib f = new FailingAsyncFib(8);
532                  f.quietlyInvoke();
533 <                assertTrue(f.isDone());
533 >                assertTrue(f.getException() instanceof FJException);
534 >                checkCompletedAbnormally(f, f.getException());
535              }};
536          testInvokeOnPool(mainPool(), a);
537      }
# Line 407 | Line 541 | public class ForkJoinTaskTest extends JS
541       */
542      public void testAbnormalForkJoin() {
543          RecursiveAction a = new CheckedRecursiveAction() {
544 <            public void realCompute() {
544 >            protected void realCompute() {
545                  FailingAsyncFib f = new FailingAsyncFib(8);
546                  assertSame(f, f.fork());
547                  try {
548                      f.join();
549                      shouldThrow();
550 <                } catch (FJException success) {}
550 >                } catch (FJException success) {
551 >                    checkCompletedAbnormally(f, success);
552 >                }
553              }};
554          testInvokeOnPool(mainPool(), a);
555      }
# Line 423 | Line 559 | public class ForkJoinTaskTest extends JS
559       */
560      public void testAbnormalForkGet() {
561          RecursiveAction a = new CheckedRecursiveAction() {
562 <            public void realCompute() throws Exception {
562 >            protected void realCompute() throws Exception {
563                  FailingAsyncFib f = new FailingAsyncFib(8);
564                  assertSame(f, f.fork());
565                  try {
# Line 432 | Line 568 | public class ForkJoinTaskTest extends JS
568                  } catch (ExecutionException success) {
569                      Throwable cause = success.getCause();
570                      assertTrue(cause instanceof FJException);
571 <                    assertTrue(f.isDone());
436 <                    assertTrue(f.isCompletedAbnormally());
437 <                    assertSame(cause, f.getException());
571 >                    checkCompletedAbnormally(f, cause);
572                  }
573              }};
574          testInvokeOnPool(mainPool(), a);
# Line 445 | Line 579 | public class ForkJoinTaskTest extends JS
579       */
580      public void testAbnormalForkTimedGet() {
581          RecursiveAction a = new CheckedRecursiveAction() {
582 <            public void realCompute() throws Exception {
582 >            protected void realCompute() throws Exception {
583                  FailingAsyncFib f = new FailingAsyncFib(8);
584                  assertSame(f, f.fork());
585                  try {
# Line 454 | Line 588 | public class ForkJoinTaskTest extends JS
588                  } catch (ExecutionException success) {
589                      Throwable cause = success.getCause();
590                      assertTrue(cause instanceof FJException);
591 <                    assertTrue(f.isDone());
458 <                    assertTrue(f.isCompletedAbnormally());
459 <                    assertSame(cause, f.getException());
591 >                    checkCompletedAbnormally(f, cause);
592                  }
593              }};
594          testInvokeOnPool(mainPool(), a);
# Line 467 | Line 599 | public class ForkJoinTaskTest extends JS
599       */
600      public void testAbnormalForkQuietlyJoin() {
601          RecursiveAction a = new CheckedRecursiveAction() {
602 <            public void realCompute() {
602 >            protected void realCompute() {
603                  FailingAsyncFib f = new FailingAsyncFib(8);
604                  assertSame(f, f.fork());
605                  f.quietlyJoin();
474                assertTrue(f.isDone());
475                assertTrue(f.isCompletedAbnormally());
606                  assertTrue(f.getException() instanceof FJException);
607 +                checkCompletedAbnormally(f, f.getException());
608              }};
609          testInvokeOnPool(mainPool(), a);
610      }
# Line 483 | Line 614 | public class ForkJoinTaskTest extends JS
614       */
615      public void testCancelledInvoke() {
616          RecursiveAction a = new CheckedRecursiveAction() {
617 <            public void realCompute() {
617 >            protected void realCompute() {
618                  AsyncFib f = new AsyncFib(8);
619                  assertTrue(f.cancel(true));
620                  try {
621                      f.invoke();
622                      shouldThrow();
623                  } catch (CancellationException success) {
624 <                    assertTrue(f.isDone());
494 <                    assertTrue(f.isCancelled());
495 <                    assertTrue(f.isCompletedAbnormally());
496 <                    assertTrue(f.getException() instanceof CancellationException);
624 >                    checkCancelled(f);
625                  }
626              }};
627          testInvokeOnPool(mainPool(), a);
# Line 504 | Line 632 | public class ForkJoinTaskTest extends JS
632       */
633      public void testCancelledForkJoin() {
634          RecursiveAction a = new CheckedRecursiveAction() {
635 <            public void realCompute() {
635 >            protected void realCompute() {
636                  AsyncFib f = new AsyncFib(8);
637                  assertTrue(f.cancel(true));
638                  assertSame(f, f.fork());
# Line 512 | Line 640 | public class ForkJoinTaskTest extends JS
640                      f.join();
641                      shouldThrow();
642                  } catch (CancellationException success) {
643 <                    assertTrue(f.isDone());
516 <                    assertTrue(f.isCancelled());
517 <                    assertTrue(f.isCompletedAbnormally());
518 <                    assertTrue(f.getException() instanceof CancellationException);
643 >                    checkCancelled(f);
644                  }
645              }};
646          testInvokeOnPool(mainPool(), a);
# Line 526 | Line 651 | public class ForkJoinTaskTest extends JS
651       */
652      public void testCancelledForkGet() {
653          RecursiveAction a = new CheckedRecursiveAction() {
654 <            public void realCompute() throws Exception {
654 >            protected void realCompute() throws Exception {
655                  AsyncFib f = new AsyncFib(8);
656                  assertTrue(f.cancel(true));
657                  assertSame(f, f.fork());
# Line 534 | Line 659 | public class ForkJoinTaskTest extends JS
659                      f.get();
660                      shouldThrow();
661                  } catch (CancellationException success) {
662 <                    assertTrue(f.isDone());
538 <                    assertTrue(f.isCancelled());
539 <                    assertTrue(f.isCompletedAbnormally());
540 <                    assertTrue(f.getException() instanceof CancellationException);
662 >                    checkCancelled(f);
663                  }
664              }};
665          testInvokeOnPool(mainPool(), a);
# Line 548 | Line 670 | public class ForkJoinTaskTest extends JS
670       */
671      public void testCancelledForkTimedGet() throws Exception {
672          RecursiveAction a = new CheckedRecursiveAction() {
673 <            public void realCompute() throws Exception {
673 >            protected void realCompute() throws Exception {
674                  AsyncFib f = new AsyncFib(8);
675                  assertTrue(f.cancel(true));
676                  assertSame(f, f.fork());
# Line 556 | Line 678 | public class ForkJoinTaskTest extends JS
678                      f.get(LONG_DELAY_MS, MILLISECONDS);
679                      shouldThrow();
680                  } catch (CancellationException success) {
681 <                    assertTrue(f.isDone());
560 <                    assertTrue(f.isCancelled());
561 <                    assertTrue(f.isCompletedAbnormally());
562 <                    assertTrue(f.getException() instanceof CancellationException);
681 >                    checkCancelled(f);
682                  }
683              }};
684          testInvokeOnPool(mainPool(), a);
# Line 570 | Line 689 | public class ForkJoinTaskTest extends JS
689       */
690      public void testCancelledForkQuietlyJoin() {
691          RecursiveAction a = new CheckedRecursiveAction() {
692 <            public void realCompute() {
692 >            protected void realCompute() {
693                  AsyncFib f = new AsyncFib(8);
694                  assertTrue(f.cancel(true));
695                  assertSame(f, f.fork());
696                  f.quietlyJoin();
697 <                assertTrue(f.isDone());
579 <                assertTrue(f.isCompletedAbnormally());
580 <                assertTrue(f.isCancelled());
581 <                assertTrue(f.getException() instanceof CancellationException);
697 >                checkCancelled(f);
698              }};
699          testInvokeOnPool(mainPool(), a);
700      }
# Line 589 | Line 705 | public class ForkJoinTaskTest extends JS
705      public void testGetPool() {
706          final ForkJoinPool mainPool = mainPool();
707          RecursiveAction a = new CheckedRecursiveAction() {
708 <            public void realCompute() {
708 >            protected void realCompute() {
709                  assertSame(mainPool, getPool());
710              }};
711          testInvokeOnPool(mainPool, a);
# Line 600 | Line 716 | public class ForkJoinTaskTest extends JS
716       */
717      public void testGetPool2() {
718          RecursiveAction a = new CheckedRecursiveAction() {
719 <            public void realCompute() {
719 >            protected void realCompute() {
720                  assertNull(getPool());
721              }};
722          assertNull(a.invoke());
# Line 611 | Line 727 | public class ForkJoinTaskTest extends JS
727       */
728      public void testInForkJoinPool() {
729          RecursiveAction a = new CheckedRecursiveAction() {
730 <            public void realCompute() {
730 >            protected void realCompute() {
731                  assertTrue(inForkJoinPool());
732              }};
733          testInvokeOnPool(mainPool(), a);
# Line 622 | Line 738 | public class ForkJoinTaskTest extends JS
738       */
739      public void testInForkJoinPool2() {
740          RecursiveAction a = new CheckedRecursiveAction() {
741 <            public void realCompute() {
742 <                assertTrue(!inForkJoinPool());
741 >            protected void realCompute() {
742 >                assertFalse(inForkJoinPool());
743              }};
744          assertNull(a.invoke());
745      }
# Line 633 | Line 749 | public class ForkJoinTaskTest extends JS
749       */
750      public void testSetRawResult() {
751          RecursiveAction a = new CheckedRecursiveAction() {
752 <            public void realCompute() {
752 >            protected void realCompute() {
753                  setRawResult(null);
754 +                assertNull(getRawResult());
755              }};
756          assertNull(a.invoke());
757      }
# Line 644 | Line 761 | public class ForkJoinTaskTest extends JS
761       */
762      public void testCompleteExceptionally() {
763          RecursiveAction a = new CheckedRecursiveAction() {
764 <            public void realCompute() {
764 >            protected void realCompute() {
765                  AsyncFib f = new AsyncFib(8);
766                  f.completeExceptionally(new FJException());
767                  try {
768                      f.invoke();
769                      shouldThrow();
770 <                } catch (FJException success) {}
770 >                } catch (FJException success) {
771 >                    checkCompletedAbnormally(f, success);
772 >                }
773              }};
774          testInvokeOnPool(mainPool(), a);
775      }
# Line 660 | Line 779 | public class ForkJoinTaskTest extends JS
779       */
780      public void testInvokeAll2() {
781          RecursiveAction a = new CheckedRecursiveAction() {
782 <            public void realCompute() {
782 >            protected void realCompute() {
783                  AsyncFib f = new AsyncFib(8);
784                  AsyncFib g = new AsyncFib(9);
785                  invokeAll(f, g);
667                assertTrue(f.isDone());
786                  assertEquals(21, f.number);
669                assertTrue(g.isDone());
787                  assertEquals(34, g.number);
788 +                checkCompletedNormally(f);
789 +                checkCompletedNormally(g);
790              }};
791          testInvokeOnPool(mainPool(), a);
792      }
# Line 677 | Line 796 | public class ForkJoinTaskTest extends JS
796       */
797      public void testInvokeAll1() {
798          RecursiveAction a = new CheckedRecursiveAction() {
799 <            public void realCompute() {
799 >            protected void realCompute() {
800                  AsyncFib f = new AsyncFib(8);
801                  invokeAll(f);
802 <                assertTrue(f.isDone());
802 >                checkCompletedNormally(f);
803                  assertEquals(21, f.number);
804              }};
805          testInvokeOnPool(mainPool(), a);
# Line 691 | Line 810 | public class ForkJoinTaskTest extends JS
810       */
811      public void testInvokeAll3() {
812          RecursiveAction a = new CheckedRecursiveAction() {
813 <            public void realCompute() {
813 >            protected void realCompute() {
814                  AsyncFib f = new AsyncFib(8);
815                  AsyncFib g = new AsyncFib(9);
816                  AsyncFib h = new AsyncFib(7);
817                  invokeAll(f, g, h);
699                assertTrue(f.isDone());
818                  assertEquals(21, f.number);
701                assertTrue(g.isDone());
819                  assertEquals(34, g.number);
703                assertTrue(h.isDone());
820                  assertEquals(13, h.number);
821 +                checkCompletedNormally(f);
822 +                checkCompletedNormally(g);
823 +                checkCompletedNormally(h);
824              }};
825          testInvokeOnPool(mainPool(), a);
826      }
# Line 711 | Line 830 | public class ForkJoinTaskTest extends JS
830       */
831      public void testInvokeAllCollection() {
832          RecursiveAction a = new CheckedRecursiveAction() {
833 <            public void realCompute() {
833 >            protected void realCompute() {
834                  AsyncFib f = new AsyncFib(8);
835                  AsyncFib g = new AsyncFib(9);
836                  AsyncFib h = new AsyncFib(7);
# Line 720 | Line 839 | public class ForkJoinTaskTest extends JS
839                  set.add(g);
840                  set.add(h);
841                  invokeAll(set);
723                assertTrue(f.isDone());
842                  assertEquals(21, f.number);
725                assertTrue(g.isDone());
843                  assertEquals(34, g.number);
727                assertTrue(h.isDone());
844                  assertEquals(13, h.number);
845 +                checkCompletedNormally(f);
846 +                checkCompletedNormally(g);
847 +                checkCompletedNormally(h);
848              }};
849          testInvokeOnPool(mainPool(), a);
850      }
851  
733
852      /**
853       * invokeAll(tasks) with any null task throws NPE
854       */
855      public void testInvokeAllNPE() {
856          RecursiveAction a = new CheckedRecursiveAction() {
857 <            public void realCompute() {
857 >            protected void realCompute() {
858                  AsyncFib f = new AsyncFib(8);
859                  AsyncFib g = new AsyncFib(9);
860                  AsyncFib h = null;
# Line 753 | Line 871 | public class ForkJoinTaskTest extends JS
871       */
872      public void testAbnormalInvokeAll2() {
873          RecursiveAction a = new CheckedRecursiveAction() {
874 <            public void realCompute() {
874 >            protected void realCompute() {
875                  AsyncFib f = new AsyncFib(8);
876                  FailingAsyncFib g = new FailingAsyncFib(9);
877 +                ForkJoinTask[] tasks = { f, g };
878 +                Collections.shuffle(Arrays.asList(tasks));
879                  try {
880 <                    invokeAll(f, g);
880 >                    invokeAll(tasks);
881                      shouldThrow();
882 <                } catch (FJException success) {}
882 >                } catch (FJException success) {
883 >                    checkCompletedAbnormally(g, success);
884 >                }
885              }};
886          testInvokeOnPool(mainPool(), a);
887      }
# Line 769 | Line 891 | public class ForkJoinTaskTest extends JS
891       */
892      public void testAbnormalInvokeAll1() {
893          RecursiveAction a = new CheckedRecursiveAction() {
894 <            public void realCompute() {
894 >            protected void realCompute() {
895                  FailingAsyncFib g = new FailingAsyncFib(9);
896                  try {
897                      invokeAll(g);
898                      shouldThrow();
899 <                } catch (FJException success) {}
899 >                } catch (FJException success) {
900 >                    checkCompletedAbnormally(g, success);
901 >                }
902              }};
903          testInvokeOnPool(mainPool(), a);
904      }
# Line 784 | Line 908 | public class ForkJoinTaskTest extends JS
908       */
909      public void testAbnormalInvokeAll3() {
910          RecursiveAction a = new CheckedRecursiveAction() {
911 <            public void realCompute() {
911 >            protected void realCompute() {
912                  AsyncFib f = new AsyncFib(8);
913                  FailingAsyncFib g = new FailingAsyncFib(9);
914                  AsyncFib h = new AsyncFib(7);
915 +                ForkJoinTask[] tasks = { f, g, h };
916 +                Collections.shuffle(Arrays.asList(tasks));
917                  try {
918 <                    invokeAll(f, g, h);
918 >                    invokeAll(tasks);
919                      shouldThrow();
920 <                } catch (FJException success) {}
920 >                } catch (FJException success) {
921 >                    checkCompletedAbnormally(g, success);
922 >                }
923              }};
924          testInvokeOnPool(mainPool(), a);
925      }
926  
927      /**
928 <     * invokeAll(collection)  throws exception if any task does
928 >     * invokeAll(collection) throws exception if any task does
929       */
930      public void testAbnormalInvokeAllCollection() {
931          RecursiveAction a = new CheckedRecursiveAction() {
932 <            public void realCompute() {
932 >            protected void realCompute() {
933                  FailingAsyncFib f = new FailingAsyncFib(8);
934                  AsyncFib g = new AsyncFib(9);
935                  AsyncFib h = new AsyncFib(7);
936 <                HashSet set = new HashSet();
937 <                set.add(f);
938 <                set.add(g);
811 <                set.add(h);
936 >                ForkJoinTask[] tasks = { f, g, h };
937 >                List taskList = Arrays.asList(tasks);
938 >                Collections.shuffle(taskList);
939                  try {
940 <                    invokeAll(set);
940 >                    invokeAll(taskList);
941                      shouldThrow();
942 <                } catch (FJException success) {}
942 >                } catch (FJException success) {
943 >                    checkCompletedAbnormally(f, success);
944 >                }
945              }};
946          testInvokeOnPool(mainPool(), a);
947      }
# Line 823 | Line 952 | public class ForkJoinTaskTest extends JS
952       */
953      public void testTryUnfork() {
954          RecursiveAction a = new CheckedRecursiveAction() {
955 <            public void realCompute() {
955 >            protected void realCompute() {
956                  AsyncFib g = new AsyncFib(9);
957                  assertSame(g, g.fork());
958                  AsyncFib f = new AsyncFib(8);
959                  assertSame(f, f.fork());
960                  assertTrue(f.tryUnfork());
961                  helpQuiesce();
962 <                assertFalse(f.isDone());
963 <                assertTrue(g.isDone());
962 >                checkNotDone(f);
963 >                checkCompletedNormally(g);
964              }};
965          testInvokeOnPool(singletonPool(), a);
966      }
# Line 842 | Line 971 | public class ForkJoinTaskTest extends JS
971       */
972      public void testGetSurplusQueuedTaskCount() {
973          RecursiveAction a = new CheckedRecursiveAction() {
974 <            public void realCompute() {
974 >            protected void realCompute() {
975                  AsyncFib h = new AsyncFib(7);
976                  assertSame(h, h.fork());
977                  AsyncFib g = new AsyncFib(9);
# Line 851 | Line 980 | public class ForkJoinTaskTest extends JS
980                  assertSame(f, f.fork());
981                  assertTrue(getSurplusQueuedTaskCount() > 0);
982                  helpQuiesce();
983 +                assertEquals(0, getSurplusQueuedTaskCount());
984 +                checkCompletedNormally(f);
985 +                checkCompletedNormally(g);
986 +                checkCompletedNormally(h);
987              }};
988          testInvokeOnPool(singletonPool(), a);
989      }
# Line 860 | Line 993 | public class ForkJoinTaskTest extends JS
993       */
994      public void testPeekNextLocalTask() {
995          RecursiveAction a = new CheckedRecursiveAction() {
996 <            public void realCompute() {
996 >            protected void realCompute() {
997                  AsyncFib g = new AsyncFib(9);
998                  assertSame(g, g.fork());
999                  AsyncFib f = new AsyncFib(8);
1000                  assertSame(f, f.fork());
1001                  assertSame(f, peekNextLocalTask());
1002                  assertNull(f.join());
1003 <                assertTrue(f.isDone());
1003 >                checkCompletedNormally(f);
1004                  helpQuiesce();
1005 +                checkCompletedNormally(g);
1006              }};
1007          testInvokeOnPool(singletonPool(), a);
1008      }
1009  
1010      /**
1011 <     * pollNextLocalTask returns most recent unexecuted task
1012 <     * without executing it
1011 >     * pollNextLocalTask returns most recent unexecuted task without
1012 >     * executing it
1013       */
1014      public void testPollNextLocalTask() {
1015          RecursiveAction a = new CheckedRecursiveAction() {
1016 <            public void realCompute() {
1016 >            protected void realCompute() {
1017                  AsyncFib g = new AsyncFib(9);
1018                  assertSame(g, g.fork());
1019                  AsyncFib f = new AsyncFib(8);
1020                  assertSame(f, f.fork());
1021                  assertSame(f, pollNextLocalTask());
1022                  helpQuiesce();
1023 <                assertFalse(f.isDone());
1023 >                checkNotDone(f);
1024 >                assertEquals(34, g.number);
1025 >                checkCompletedNormally(g);
1026              }};
1027          testInvokeOnPool(singletonPool(), a);
1028      }
1029  
1030      /**
1031 <     * pollTask returns an unexecuted task
896 <     * without executing it
1031 >     * pollTask returns an unexecuted task without executing it
1032       */
1033      public void testPollTask() {
1034          RecursiveAction a = new CheckedRecursiveAction() {
1035 <            public void realCompute() {
1035 >            protected void realCompute() {
1036                  AsyncFib g = new AsyncFib(9);
1037                  assertSame(g, g.fork());
1038                  AsyncFib f = new AsyncFib(8);
1039                  assertSame(f, f.fork());
1040                  assertSame(f, pollTask());
1041                  helpQuiesce();
1042 <                assertFalse(f.isDone());
1043 <                assertTrue(g.isDone());
1042 >                checkNotDone(f);
1043 >                checkCompletedNormally(g);
1044              }};
1045          testInvokeOnPool(singletonPool(), a);
1046      }
# Line 915 | Line 1050 | public class ForkJoinTaskTest extends JS
1050       */
1051      public void testPeekNextLocalTaskAsync() {
1052          RecursiveAction a = new CheckedRecursiveAction() {
1053 <            public void realCompute() {
1053 >            protected void realCompute() {
1054                  AsyncFib g = new AsyncFib(9);
1055                  assertSame(g, g.fork());
1056                  AsyncFib f = new AsyncFib(8);
# Line 923 | Line 1058 | public class ForkJoinTaskTest extends JS
1058                  assertSame(g, peekNextLocalTask());
1059                  assertNull(f.join());
1060                  helpQuiesce();
1061 <                assertTrue(f.isDone());
1061 >                checkCompletedNormally(f);
1062 >                assertEquals(34, g.number);
1063 >                checkCompletedNormally(g);
1064              }};
1065          testInvokeOnPool(asyncSingletonPool(), a);
1066      }
1067  
1068      /**
1069 <     * pollNextLocalTask returns least recent unexecuted task
1070 <     * without executing it, in async mode
1069 >     * pollNextLocalTask returns least recent unexecuted task without
1070 >     * executing it, in async mode
1071       */
1072      public void testPollNextLocalTaskAsync() {
1073          RecursiveAction a = new CheckedRecursiveAction() {
1074 <            public void realCompute() {
1074 >            protected void realCompute() {
1075                  AsyncFib g = new AsyncFib(9);
1076                  assertSame(g, g.fork());
1077                  AsyncFib f = new AsyncFib(8);
1078                  assertSame(f, f.fork());
1079                  assertSame(g, pollNextLocalTask());
1080                  helpQuiesce();
1081 <                assertTrue(f.isDone());
1082 <                assertFalse(g.isDone());
1081 >                assertEquals(21, f.number);
1082 >                checkCompletedNormally(f);
1083 >                checkNotDone(g);
1084              }};
1085          testInvokeOnPool(asyncSingletonPool(), a);
1086      }
1087  
1088      /**
1089 <     * pollTask returns an unexecuted task
1090 <     * without executing it, in async mode
1089 >     * pollTask returns an unexecuted task without executing it, in
1090 >     * async mode
1091       */
1092      public void testPollTaskAsync() {
1093          RecursiveAction a = new CheckedRecursiveAction() {
1094 <            public void realCompute() {
1094 >            protected void realCompute() {
1095                  AsyncFib g = new AsyncFib(9);
1096                  assertSame(g, g.fork());
1097                  AsyncFib f = new AsyncFib(8);
1098                  assertSame(f, f.fork());
1099                  assertSame(g, pollTask());
1100                  helpQuiesce();
1101 <                assertTrue(f.isDone());
1102 <                assertFalse(g.isDone());
1101 >                assertEquals(21, f.number);
1102 >                checkCompletedNormally(f);
1103 >                checkNotDone(g);
1104              }};
1105          testInvokeOnPool(asyncSingletonPool(), a);
1106      }
1107 +
1108 +    // versions for singleton pools
1109 +
1110 +    /**
1111 +     * invoke returns when task completes normally.
1112 +     * isCompletedAbnormally and isCancelled return false for normally
1113 +     * completed tasks; getRawResult returns null.
1114 +     */
1115 +    public void testInvokeSingleton() {
1116 +        RecursiveAction a = new CheckedRecursiveAction() {
1117 +            protected void realCompute() {
1118 +                AsyncFib f = new AsyncFib(8);
1119 +                assertNull(f.invoke());
1120 +                assertEquals(21, f.number);
1121 +                checkCompletedNormally(f);
1122 +            }};
1123 +        testInvokeOnPool(singletonPool(), a);
1124 +    }
1125 +
1126 +    /**
1127 +     * quietlyInvoke task returns when task completes normally.
1128 +     * isCompletedAbnormally and isCancelled return false for normally
1129 +     * completed tasks
1130 +     */
1131 +    public void testQuietlyInvokeSingleton() {
1132 +        RecursiveAction a = new CheckedRecursiveAction() {
1133 +            protected void realCompute() {
1134 +                AsyncFib f = new AsyncFib(8);
1135 +                f.quietlyInvoke();
1136 +                assertEquals(21, f.number);
1137 +                checkCompletedNormally(f);
1138 +            }};
1139 +        testInvokeOnPool(singletonPool(), a);
1140 +    }
1141 +
1142 +    /**
1143 +     * join of a forked task returns when task completes
1144 +     */
1145 +    public void testForkJoinSingleton() {
1146 +        RecursiveAction a = new CheckedRecursiveAction() {
1147 +            protected void realCompute() {
1148 +                AsyncFib f = new AsyncFib(8);
1149 +                assertSame(f, f.fork());
1150 +                assertNull(f.join());
1151 +                assertEquals(21, f.number);
1152 +                checkCompletedNormally(f);
1153 +            }};
1154 +        testInvokeOnPool(singletonPool(), a);
1155 +    }
1156 +
1157 +    /**
1158 +     * get of a forked task returns when task completes
1159 +     */
1160 +    public void testForkGetSingleton() {
1161 +        RecursiveAction a = new CheckedRecursiveAction() {
1162 +            protected void realCompute() throws Exception {
1163 +                AsyncFib f = new AsyncFib(8);
1164 +                assertSame(f, f.fork());
1165 +                assertNull(f.get());
1166 +                assertEquals(21, f.number);
1167 +                checkCompletedNormally(f);
1168 +            }};
1169 +        testInvokeOnPool(singletonPool(), a);
1170 +    }
1171 +
1172 +    /**
1173 +     * timed get of a forked task returns when task completes
1174 +     */
1175 +    public void testForkTimedGetSingleton() {
1176 +        RecursiveAction a = new CheckedRecursiveAction() {
1177 +            protected void realCompute() throws Exception {
1178 +                AsyncFib f = new AsyncFib(8);
1179 +                assertSame(f, f.fork());
1180 +                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1181 +                assertEquals(21, f.number);
1182 +                checkCompletedNormally(f);
1183 +            }};
1184 +        testInvokeOnPool(singletonPool(), a);
1185 +    }
1186 +
1187 +    /**
1188 +     * timed get with null time unit throws NPE
1189 +     */
1190 +    public void testForkTimedGetNPESingleton() {
1191 +        RecursiveAction a = new CheckedRecursiveAction() {
1192 +            protected void realCompute() throws Exception {
1193 +                AsyncFib f = new AsyncFib(8);
1194 +                assertSame(f, f.fork());
1195 +                try {
1196 +                    f.get(5L, null);
1197 +                    shouldThrow();
1198 +                } catch (NullPointerException success) {}
1199 +            }};
1200 +        testInvokeOnPool(singletonPool(), a);
1201 +    }
1202 +
1203 +    /**
1204 +     * quietlyJoin of a forked task returns when task completes
1205 +     */
1206 +    public void testForkQuietlyJoinSingleton() {
1207 +        RecursiveAction a = new CheckedRecursiveAction() {
1208 +            protected void realCompute() {
1209 +                AsyncFib f = new AsyncFib(8);
1210 +                assertSame(f, f.fork());
1211 +                f.quietlyJoin();
1212 +                assertEquals(21, f.number);
1213 +                checkCompletedNormally(f);
1214 +            }};
1215 +        testInvokeOnPool(singletonPool(), a);
1216 +    }
1217 +
1218 +    /**
1219 +     * helpQuiesce returns when tasks are complete.
1220 +     * getQueuedTaskCount returns 0 when quiescent
1221 +     */
1222 +    public void testForkHelpQuiesceSingleton() {
1223 +        RecursiveAction a = new CheckedRecursiveAction() {
1224 +            protected void realCompute() {
1225 +                AsyncFib f = new AsyncFib(8);
1226 +                assertSame(f, f.fork());
1227 +                helpQuiesce();
1228 +                assertEquals(0, getQueuedTaskCount());
1229 +                assertEquals(21, f.number);
1230 +                checkCompletedNormally(f);
1231 +            }};
1232 +        testInvokeOnPool(singletonPool(), a);
1233 +    }
1234 +
1235 +    /**
1236 +     * invoke task throws exception when task completes abnormally
1237 +     */
1238 +    public void testAbnormalInvokeSingleton() {
1239 +        RecursiveAction a = new CheckedRecursiveAction() {
1240 +            protected void realCompute() {
1241 +                FailingAsyncFib f = new FailingAsyncFib(8);
1242 +                try {
1243 +                    f.invoke();
1244 +                    shouldThrow();
1245 +                } catch (FJException success) {
1246 +                    checkCompletedAbnormally(f, success);
1247 +                }
1248 +            }};
1249 +        testInvokeOnPool(singletonPool(), a);
1250 +    }
1251 +
1252 +    /**
1253 +     * quietlyInvoke task returns when task completes abnormally
1254 +     */
1255 +    public void testAbnormalQuietlyInvokeSingleton() {
1256 +        RecursiveAction a = new CheckedRecursiveAction() {
1257 +            protected void realCompute() {
1258 +                FailingAsyncFib f = new FailingAsyncFib(8);
1259 +                f.quietlyInvoke();
1260 +                assertTrue(f.getException() instanceof FJException);
1261 +                checkCompletedAbnormally(f, f.getException());
1262 +            }};
1263 +        testInvokeOnPool(singletonPool(), a);
1264 +    }
1265 +
1266 +    /**
1267 +     * join of a forked task throws exception when task completes abnormally
1268 +     */
1269 +    public void testAbnormalForkJoinSingleton() {
1270 +        RecursiveAction a = new CheckedRecursiveAction() {
1271 +            protected void realCompute() {
1272 +                FailingAsyncFib f = new FailingAsyncFib(8);
1273 +                assertSame(f, f.fork());
1274 +                try {
1275 +                    f.join();
1276 +                    shouldThrow();
1277 +                } catch (FJException success) {
1278 +                    checkCompletedAbnormally(f, success);
1279 +                }
1280 +            }};
1281 +        testInvokeOnPool(singletonPool(), a);
1282 +    }
1283 +
1284 +    /**
1285 +     * get of a forked task throws exception when task completes abnormally
1286 +     */
1287 +    public void testAbnormalForkGetSingleton() {
1288 +        RecursiveAction a = new CheckedRecursiveAction() {
1289 +            protected void realCompute() throws Exception {
1290 +                FailingAsyncFib f = new FailingAsyncFib(8);
1291 +                assertSame(f, f.fork());
1292 +                try {
1293 +                    f.get();
1294 +                    shouldThrow();
1295 +                } catch (ExecutionException success) {
1296 +                    Throwable cause = success.getCause();
1297 +                    assertTrue(cause instanceof FJException);
1298 +                    checkCompletedAbnormally(f, cause);
1299 +                }
1300 +            }};
1301 +        testInvokeOnPool(singletonPool(), a);
1302 +    }
1303 +
1304 +    /**
1305 +     * timed get of a forked task throws exception when task completes abnormally
1306 +     */
1307 +    public void testAbnormalForkTimedGetSingleton() {
1308 +        RecursiveAction a = new CheckedRecursiveAction() {
1309 +            protected void realCompute() throws Exception {
1310 +                FailingAsyncFib f = new FailingAsyncFib(8);
1311 +                assertSame(f, f.fork());
1312 +                try {
1313 +                    f.get(LONG_DELAY_MS, MILLISECONDS);
1314 +                    shouldThrow();
1315 +                } catch (ExecutionException success) {
1316 +                    Throwable cause = success.getCause();
1317 +                    assertTrue(cause instanceof FJException);
1318 +                    checkCompletedAbnormally(f, cause);
1319 +                }
1320 +            }};
1321 +        testInvokeOnPool(singletonPool(), a);
1322 +    }
1323 +
1324 +    /**
1325 +     * quietlyJoin of a forked task returns when task completes abnormally
1326 +     */
1327 +    public void testAbnormalForkQuietlyJoinSingleton() {
1328 +        RecursiveAction a = new CheckedRecursiveAction() {
1329 +            protected void realCompute() {
1330 +                FailingAsyncFib f = new FailingAsyncFib(8);
1331 +                assertSame(f, f.fork());
1332 +                f.quietlyJoin();
1333 +                assertTrue(f.getException() instanceof FJException);
1334 +                checkCompletedAbnormally(f, f.getException());
1335 +            }};
1336 +        testInvokeOnPool(singletonPool(), a);
1337 +    }
1338 +
1339 +    /**
1340 +     * invoke task throws exception when task cancelled
1341 +     */
1342 +    public void testCancelledInvokeSingleton() {
1343 +        RecursiveAction a = new CheckedRecursiveAction() {
1344 +            protected void realCompute() {
1345 +                AsyncFib f = new AsyncFib(8);
1346 +                assertTrue(f.cancel(true));
1347 +                try {
1348 +                    f.invoke();
1349 +                    shouldThrow();
1350 +                } catch (CancellationException success) {
1351 +                    checkCancelled(f);
1352 +                }
1353 +            }};
1354 +        testInvokeOnPool(singletonPool(), a);
1355 +    }
1356 +
1357 +    /**
1358 +     * join of a forked task throws exception when task cancelled
1359 +     */
1360 +    public void testCancelledForkJoinSingleton() {
1361 +        RecursiveAction a = new CheckedRecursiveAction() {
1362 +            protected void realCompute() {
1363 +                AsyncFib f = new AsyncFib(8);
1364 +                assertTrue(f.cancel(true));
1365 +                assertSame(f, f.fork());
1366 +                try {
1367 +                    f.join();
1368 +                    shouldThrow();
1369 +                } catch (CancellationException success) {
1370 +                    checkCancelled(f);
1371 +                }
1372 +            }};
1373 +        testInvokeOnPool(singletonPool(), a);
1374 +    }
1375 +
1376 +    /**
1377 +     * get of a forked task throws exception when task cancelled
1378 +     */
1379 +    public void testCancelledForkGetSingleton() {
1380 +        RecursiveAction a = new CheckedRecursiveAction() {
1381 +            protected void realCompute() throws Exception {
1382 +                AsyncFib f = new AsyncFib(8);
1383 +                assertTrue(f.cancel(true));
1384 +                assertSame(f, f.fork());
1385 +                try {
1386 +                    f.get();
1387 +                    shouldThrow();
1388 +                } catch (CancellationException success) {
1389 +                    checkCancelled(f);
1390 +                }
1391 +            }};
1392 +        testInvokeOnPool(singletonPool(), a);
1393 +    }
1394 +
1395 +    /**
1396 +     * timed get of a forked task throws exception when task cancelled
1397 +     */
1398 +    public void testCancelledForkTimedGetSingleton() throws Exception {
1399 +        RecursiveAction a = new CheckedRecursiveAction() {
1400 +            protected void realCompute() throws Exception {
1401 +                AsyncFib f = new AsyncFib(8);
1402 +                assertTrue(f.cancel(true));
1403 +                assertSame(f, f.fork());
1404 +                try {
1405 +                    f.get(LONG_DELAY_MS, MILLISECONDS);
1406 +                    shouldThrow();
1407 +                } catch (CancellationException success) {
1408 +                    checkCancelled(f);
1409 +                }
1410 +            }};
1411 +        testInvokeOnPool(singletonPool(), a);
1412 +    }
1413 +
1414 +    /**
1415 +     * quietlyJoin of a forked task returns when task cancelled
1416 +     */
1417 +    public void testCancelledForkQuietlyJoinSingleton() {
1418 +        RecursiveAction a = new CheckedRecursiveAction() {
1419 +            protected void realCompute() {
1420 +                AsyncFib f = new AsyncFib(8);
1421 +                assertTrue(f.cancel(true));
1422 +                assertSame(f, f.fork());
1423 +                f.quietlyJoin();
1424 +                checkCancelled(f);
1425 +            }};
1426 +        testInvokeOnPool(singletonPool(), a);
1427 +    }
1428 +
1429 +    /**
1430 +     * invoke task throws exception after invoking completeExceptionally
1431 +     */
1432 +    public void testCompleteExceptionallySingleton() {
1433 +        RecursiveAction a = new CheckedRecursiveAction() {
1434 +            protected void realCompute() {
1435 +                AsyncFib f = new AsyncFib(8);
1436 +                f.completeExceptionally(new FJException());
1437 +                try {
1438 +                    f.invoke();
1439 +                    shouldThrow();
1440 +                } catch (FJException success) {
1441 +                    checkCompletedAbnormally(f, success);
1442 +                }
1443 +            }};
1444 +        testInvokeOnPool(singletonPool(), a);
1445 +    }
1446 +
1447 +    /**
1448 +     * invokeAll(t1, t2) invokes all task arguments
1449 +     */
1450 +    public void testInvokeAll2Singleton() {
1451 +        RecursiveAction a = new CheckedRecursiveAction() {
1452 +            protected void realCompute() {
1453 +                AsyncFib f = new AsyncFib(8);
1454 +                AsyncFib g = new AsyncFib(9);
1455 +                invokeAll(f, g);
1456 +                assertEquals(21, f.number);
1457 +                assertEquals(34, g.number);
1458 +                checkCompletedNormally(f);
1459 +                checkCompletedNormally(g);
1460 +            }};
1461 +        testInvokeOnPool(singletonPool(), a);
1462 +    }
1463 +
1464 +    /**
1465 +     * invokeAll(tasks) with 1 argument invokes task
1466 +     */
1467 +    public void testInvokeAll1Singleton() {
1468 +        RecursiveAction a = new CheckedRecursiveAction() {
1469 +            protected void realCompute() {
1470 +                AsyncFib f = new AsyncFib(8);
1471 +                invokeAll(f);
1472 +                checkCompletedNormally(f);
1473 +                assertEquals(21, f.number);
1474 +            }};
1475 +        testInvokeOnPool(singletonPool(), a);
1476 +    }
1477 +
1478 +    /**
1479 +     * invokeAll(tasks) with > 2 argument invokes tasks
1480 +     */
1481 +    public void testInvokeAll3Singleton() {
1482 +        RecursiveAction a = new CheckedRecursiveAction() {
1483 +            protected void realCompute() {
1484 +                AsyncFib f = new AsyncFib(8);
1485 +                AsyncFib g = new AsyncFib(9);
1486 +                AsyncFib h = new AsyncFib(7);
1487 +                invokeAll(f, g, h);
1488 +                assertEquals(21, f.number);
1489 +                assertEquals(34, g.number);
1490 +                assertEquals(13, h.number);
1491 +                checkCompletedNormally(f);
1492 +                checkCompletedNormally(g);
1493 +                checkCompletedNormally(h);
1494 +            }};
1495 +        testInvokeOnPool(singletonPool(), a);
1496 +    }
1497 +
1498 +    /**
1499 +     * invokeAll(collection) invokes all tasks in the collection
1500 +     */
1501 +    public void testInvokeAllCollectionSingleton() {
1502 +        RecursiveAction a = new CheckedRecursiveAction() {
1503 +            protected void realCompute() {
1504 +                AsyncFib f = new AsyncFib(8);
1505 +                AsyncFib g = new AsyncFib(9);
1506 +                AsyncFib h = new AsyncFib(7);
1507 +                HashSet set = new HashSet();
1508 +                set.add(f);
1509 +                set.add(g);
1510 +                set.add(h);
1511 +                invokeAll(set);
1512 +                assertEquals(21, f.number);
1513 +                assertEquals(34, g.number);
1514 +                assertEquals(13, h.number);
1515 +                checkCompletedNormally(f);
1516 +                checkCompletedNormally(g);
1517 +                checkCompletedNormally(h);
1518 +            }};
1519 +        testInvokeOnPool(singletonPool(), a);
1520 +    }
1521 +
1522 +    /**
1523 +     * invokeAll(tasks) with any null task throws NPE
1524 +     */
1525 +    public void testInvokeAllNPESingleton() {
1526 +        RecursiveAction a = new CheckedRecursiveAction() {
1527 +            protected void realCompute() {
1528 +                AsyncFib f = new AsyncFib(8);
1529 +                AsyncFib g = new AsyncFib(9);
1530 +                AsyncFib h = null;
1531 +                try {
1532 +                    invokeAll(f, g, h);
1533 +                    shouldThrow();
1534 +                } catch (NullPointerException success) {}
1535 +            }};
1536 +        testInvokeOnPool(singletonPool(), a);
1537 +    }
1538 +
1539 +    /**
1540 +     * invokeAll(t1, t2) throw exception if any task does
1541 +     */
1542 +    public void testAbnormalInvokeAll2Singleton() {
1543 +        RecursiveAction a = new CheckedRecursiveAction() {
1544 +            protected void realCompute() {
1545 +                AsyncFib f = new AsyncFib(8);
1546 +                FailingAsyncFib g = new FailingAsyncFib(9);
1547 +                ForkJoinTask[] tasks = { f, g };
1548 +                Collections.shuffle(Arrays.asList(tasks));
1549 +                try {
1550 +                    invokeAll(tasks);
1551 +                    shouldThrow();
1552 +                } catch (FJException success) {
1553 +                    checkCompletedAbnormally(g, success);
1554 +                }
1555 +            }};
1556 +        testInvokeOnPool(singletonPool(), a);
1557 +    }
1558 +
1559 +    /**
1560 +     * invokeAll(tasks) with 1 argument throws exception if task does
1561 +     */
1562 +    public void testAbnormalInvokeAll1Singleton() {
1563 +        RecursiveAction a = new CheckedRecursiveAction() {
1564 +            protected void realCompute() {
1565 +                FailingAsyncFib g = new FailingAsyncFib(9);
1566 +                try {
1567 +                    invokeAll(g);
1568 +                    shouldThrow();
1569 +                } catch (FJException success) {
1570 +                    checkCompletedAbnormally(g, success);
1571 +                }
1572 +            }};
1573 +        testInvokeOnPool(singletonPool(), a);
1574 +    }
1575 +
1576 +    /**
1577 +     * invokeAll(tasks) with > 2 argument throws exception if any task does
1578 +     */
1579 +    public void testAbnormalInvokeAll3Singleton() {
1580 +        RecursiveAction a = new CheckedRecursiveAction() {
1581 +            protected void realCompute() {
1582 +                AsyncFib f = new AsyncFib(8);
1583 +                FailingAsyncFib g = new FailingAsyncFib(9);
1584 +                AsyncFib h = new AsyncFib(7);
1585 +                ForkJoinTask[] tasks = { f, g, h };
1586 +                Collections.shuffle(Arrays.asList(tasks));
1587 +                try {
1588 +                    invokeAll(tasks);
1589 +                    shouldThrow();
1590 +                } catch (FJException success) {
1591 +                    checkCompletedAbnormally(g, success);
1592 +                }
1593 +            }};
1594 +        testInvokeOnPool(singletonPool(), a);
1595 +    }
1596 +
1597 +    /**
1598 +     * invokeAll(collection) throws exception if any task does
1599 +     */
1600 +    public void testAbnormalInvokeAllCollectionSingleton() {
1601 +        RecursiveAction a = new CheckedRecursiveAction() {
1602 +            protected void realCompute() {
1603 +                FailingAsyncFib f = new FailingAsyncFib(8);
1604 +                AsyncFib g = new AsyncFib(9);
1605 +                AsyncFib h = new AsyncFib(7);
1606 +                ForkJoinTask[] tasks = { f, g, h };
1607 +                List taskList = Arrays.asList(tasks);
1608 +                Collections.shuffle(taskList);
1609 +                try {
1610 +                    invokeAll(taskList);
1611 +                    shouldThrow();
1612 +                } catch (FJException success) {
1613 +                    checkCompletedAbnormally(f, success);
1614 +                }
1615 +            }};
1616 +        testInvokeOnPool(singletonPool(), a);
1617 +    }
1618 +
1619 +    /**
1620 +     * ForkJoinTask.quietlyComplete returns when task completes
1621 +     * normally without setting a value. The most recent value
1622 +     * established by setRawResult(V) (or null by default) is returned
1623 +     * from invoke.
1624 +     */
1625 +    public void testQuietlyComplete() {
1626 +        RecursiveAction a = new CheckedRecursiveAction() {
1627 +                protected void realCompute() {
1628 +                    AsyncFib f = new AsyncFib(8);
1629 +                    f.quietlyComplete();
1630 +                    assertEquals(8, f.number);
1631 +                    checkCompletedNormally(f);
1632 +                }};
1633 +        testInvokeOnPool(mainPool(), a);
1634 +    }
1635 +
1636   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines