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.5 by jsr166, Sat Nov 21 02:07:26 2009 UTC vs.
Revision 1.57 by dl, Mon Aug 12 15:08:44 2019 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 junit.framework.*;
7 < import java.util.concurrent.*;
8 < import java.util.concurrent.atomic.*;
9 < import java.util.*;
6 >
7 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 >
9 > import java.util.Arrays;
10 > import java.util.HashSet;
11 > import java.util.concurrent.Callable;
12 > import java.util.concurrent.CancellationException;
13 > import java.util.concurrent.ExecutionException;
14 > import java.util.concurrent.ForkJoinPool;
15 > import java.util.concurrent.ForkJoinTask;
16 > import java.util.concurrent.RecursiveAction;
17 > import java.util.concurrent.TimeoutException;
18 > import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
19 >
20 > import junit.framework.Test;
21 > import junit.framework.TestSuite;
22  
23   public class ForkJoinTaskTest extends JSR166TestCase {
24  
25      public static void main(String[] args) {
26 <        junit.textui.TestRunner.run (suite());
26 >        main(suite(), args);
27      }
28 +
29      public static Test suite() {
30          return new TestSuite(ForkJoinTaskTest.class);
31      }
32  
33 <    /**
33 >    // Runs with "mainPool" use > 1 thread. singletonPool tests use 1
34 >    static final int mainPoolSize =
35 >        Math.max(2, Runtime.getRuntime().availableProcessors());
36 >
37 >    private static ForkJoinPool mainPool() {
38 >        return new ForkJoinPool(mainPoolSize);
39 >    }
40 >
41 >    private static ForkJoinPool singletonPool() {
42 >        return new ForkJoinPool(1);
43 >    }
44 >
45 >    private static ForkJoinPool asyncSingletonPool() {
46 >        return new ForkJoinPool(1,
47 >                                ForkJoinPool.defaultForkJoinWorkerThreadFactory,
48 >                                null, true);
49 >    }
50 >
51 >    private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
52 >        try (PoolCleaner cleaner = cleaner(pool)) {
53 >            assertFalse(a.isDone());
54 >            assertFalse(a.isCompletedNormally());
55 >            assertFalse(a.isCompletedAbnormally());
56 >            assertFalse(a.isCancelled());
57 >            assertNull(a.getException());
58 >            assertNull(a.getRawResult());
59 >
60 >            assertNull(pool.invoke(a));
61 >
62 >            assertTrue(a.isDone());
63 >            assertTrue(a.isCompletedNormally());
64 >            assertFalse(a.isCompletedAbnormally());
65 >            assertFalse(a.isCancelled());
66 >            assertNull(a.getException());
67 >            assertNull(a.getRawResult());
68 >        }
69 >    }
70 >
71 >    void checkNotDone(ForkJoinTask a) {
72 >        assertFalse(a.isDone());
73 >        assertFalse(a.isCompletedNormally());
74 >        assertFalse(a.isCompletedAbnormally());
75 >        assertFalse(a.isCancelled());
76 >        assertNull(a.getException());
77 >        assertNull(a.getRawResult());
78 >
79 >        try {
80 >            a.get(randomExpiredTimeout(), randomTimeUnit());
81 >            shouldThrow();
82 >        } catch (TimeoutException success) {
83 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
84 >    }
85 >
86 >    <T> void checkCompletedNormally(ForkJoinTask<T> a) {
87 >        checkCompletedNormally(a, null);
88 >    }
89 >
90 >    <T> void checkCompletedNormally(ForkJoinTask<T> a, T expectedValue) {
91 >        assertTrue(a.isDone());
92 >        assertFalse(a.isCancelled());
93 >        assertTrue(a.isCompletedNormally());
94 >        assertFalse(a.isCompletedAbnormally());
95 >        assertNull(a.getException());
96 >        assertSame(expectedValue, a.getRawResult());
97 >
98 >        {
99 >            Thread.currentThread().interrupt();
100 >            long startTime = System.nanoTime();
101 >            assertSame(expectedValue, a.join());
102 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
103 >            Thread.interrupted();
104 >        }
105 >
106 >        {
107 >            Thread.currentThread().interrupt();
108 >            long startTime = System.nanoTime();
109 >            a.quietlyJoin();        // should be no-op
110 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
111 >            Thread.interrupted();
112 >        }
113 >
114 >        assertFalse(a.cancel(false));
115 >        assertFalse(a.cancel(true));
116 >
117 >        T v1 = null, v2 = null;
118 >        try {
119 >            v1 = a.get();
120 >            v2 = a.get(randomTimeout(), randomTimeUnit());
121 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
122 >        assertSame(expectedValue, v1);
123 >        assertSame(expectedValue, v2);
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) < LONG_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(randomTimeout(), randomTimeUnit());
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) < LONG_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(randomTimeout(), randomTimeUnit());
197 >            shouldThrow();
198 >        } catch (ExecutionException success) {
199 >            assertSame(t.getClass(), success.getCause().getClass());
200 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
201 >    }
202 >
203 >    /*
204       * Testing coverage notes:
205       *
206       * To test extension methods and overrides, most tests use
# Line 25 | Line 208 | public class ForkJoinTaskTest extends JS
208       * differently than supplied Recursive forms.
209       */
210  
211 <    static final ForkJoinPool mainPool = new ForkJoinPool();
29 <    static final ForkJoinPool singletonPool = new ForkJoinPool(1);
30 <    static final ForkJoinPool asyncSingletonPool = new ForkJoinPool(1);
31 <    static {
32 <        asyncSingletonPool.setAsyncMode(true);
33 <    }
34 <
35 <    static final class FJException extends RuntimeException {
211 >    public static final class FJException extends RuntimeException {
212          FJException() { super(); }
213      }
214  
215 <    static abstract class BinaryAsyncAction extends ForkJoinTask<Void> {
215 >    abstract static class BinaryAsyncAction extends ForkJoinTask<Void> {
216          private volatile int controlState;
217  
218          static final AtomicIntegerFieldUpdater<BinaryAsyncAction> controlStateUpdater =
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 80 | Line 256 | public class ForkJoinTaskTest extends JS
256              super.completeExceptionally(ex);
257          }
258  
259 +        public boolean cancel(boolean mayInterruptIfRunning) {
260 +            if (super.cancel(mayInterruptIfRunning)) {
261 +                completeExceptionally(new FJException());
262 +                return true;
263 +            }
264 +            return false;
265 +        }
266 +
267          public final void complete() {
268              BinaryAsyncAction a = this;
269              for (;;) {
# Line 101 | Line 285 | public class ForkJoinTaskTest extends JS
285          }
286  
287          public final void completeExceptionally(Throwable ex) {
288 <            BinaryAsyncAction a = this;
105 <            while (!a.isCompletedAbnormally()) {
288 >            for (BinaryAsyncAction a = this;;) {
289                  a.completeThisExceptionally(ex);
290                  BinaryAsyncAction s = a.sibling;
291 <                if (s != null)
292 <                    s.cancel(false);
293 <                if (!a.onException() || (a = a.parent) == null)
291 >                if (s != null && !s.isDone())
292 >                    s.completeExceptionally(ex);
293 >                if ((a = a.parent) == null)
294                      break;
295              }
296          }
# Line 148 | Line 331 | public class ForkJoinTaskTest extends JS
331  
332      }
333  
334 <    static final class AsyncFib  extends BinaryAsyncAction {
334 >    static final class AsyncFib extends BinaryAsyncAction {
335          int number;
336          public AsyncFib(int n) {
337              this.number = n;
# Line 157 | Line 340 | public class ForkJoinTaskTest extends JS
340          public final boolean exec() {
341              AsyncFib f = this;
342              int n = f.number;
343 <            if (n > 1) {
344 <                while (n > 1) {
345 <                    AsyncFib p = f;
346 <                    AsyncFib r = new AsyncFib(n - 2);
347 <                    f = new AsyncFib(--n);
348 <                    p.linkSubtasks(r, f);
166 <                    r.fork();
167 <                }
168 <                f.number = n;
343 >            while (n > 1) {
344 >                AsyncFib p = f;
345 >                AsyncFib r = new AsyncFib(n - 2);
346 >                f = new AsyncFib(--n);
347 >                p.linkSubtasks(r, f);
348 >                r.fork();
349              }
350              f.complete();
351              return false;
# Line 176 | Line 356 | public class ForkJoinTaskTest extends JS
356          }
357      }
358  
359 <
180 <    static final class FailingAsyncFib  extends BinaryAsyncAction {
359 >    static final class FailingAsyncFib extends BinaryAsyncAction {
360          int number;
361          public FailingAsyncFib(int n) {
362              this.number = n;
# Line 186 | Line 365 | public class ForkJoinTaskTest extends JS
365          public final boolean exec() {
366              FailingAsyncFib f = this;
367              int n = f.number;
368 <            if (n > 1) {
369 <                while (n > 1) {
370 <                    FailingAsyncFib p = f;
371 <                    FailingAsyncFib r = new FailingAsyncFib(n - 2);
372 <                    f = new FailingAsyncFib(--n);
373 <                    p.linkSubtasks(r, f);
195 <                    r.fork();
196 <                }
197 <                f.number = n;
368 >            while (n > 1) {
369 >                FailingAsyncFib p = f;
370 >                FailingAsyncFib r = new FailingAsyncFib(n - 2);
371 >                f = new FailingAsyncFib(--n);
372 >                p.linkSubtasks(r, f);
373 >                r.fork();
374              }
375              f.complete();
376              return false;
# Line 208 | Line 384 | public class ForkJoinTaskTest extends JS
384      /**
385       * invoke returns when task completes normally.
386       * isCompletedAbnormally and isCancelled return false for normally
387 <     * completed tasks. getRawResult of a RecursiveAction returns null;
212 <     *
387 >     * completed tasks; getRawResult returns null.
388       */
389      public void testInvoke() {
390 <        RecursiveAction a = new RecursiveAction() {
391 <                public void compute() {
392 <                    AsyncFib f = new AsyncFib(8);
393 <                    f.invoke();
394 <                    threadAssertTrue(f.number == 21);
395 <                    threadAssertTrue(f.isDone());
396 <                    threadAssertFalse(f.isCancelled());
397 <                    threadAssertFalse(f.isCompletedAbnormally());
223 <                    threadAssertTrue(f.getRawResult() == null);
224 <                }
225 <            };
226 <        mainPool.invoke(a);
390 >        RecursiveAction a = new CheckedRecursiveAction() {
391 >            protected void realCompute() {
392 >                AsyncFib f = new AsyncFib(8);
393 >                assertNull(f.invoke());
394 >                assertEquals(21, f.number);
395 >                checkCompletedNormally(f);
396 >            }};
397 >        testInvokeOnPool(mainPool(), a);
398      }
399  
400      /**
# Line 232 | Line 403 | public class ForkJoinTaskTest extends JS
403       * completed tasks
404       */
405      public void testQuietlyInvoke() {
406 <        RecursiveAction a = new RecursiveAction() {
407 <                public void compute() {
408 <                    AsyncFib f = new AsyncFib(8);
409 <                    f.quietlyInvoke();
410 <                    threadAssertTrue(f.number == 21);
411 <                    threadAssertTrue(f.isDone());
412 <                    threadAssertFalse(f.isCancelled());
413 <                    threadAssertFalse(f.isCompletedAbnormally());
243 <                    threadAssertTrue(f.getRawResult() == null);
244 <                }
245 <            };
246 <        mainPool.invoke(a);
406 >        RecursiveAction a = new CheckedRecursiveAction() {
407 >            protected void realCompute() {
408 >                AsyncFib f = new AsyncFib(8);
409 >                f.quietlyInvoke();
410 >                assertEquals(21, f.number);
411 >                checkCompletedNormally(f);
412 >            }};
413 >        testInvokeOnPool(mainPool(), a);
414      }
415  
416      /**
417       * join of a forked task returns when task completes
418       */
419      public void testForkJoin() {
420 <        RecursiveAction a = new RecursiveAction() {
421 <                public void compute() {
422 <                    AsyncFib f = new AsyncFib(8);
423 <                    f.fork();
424 <                    f.join();
425 <                    threadAssertTrue(f.number == 21);
426 <                    threadAssertTrue(f.isDone());
427 <                    threadAssertTrue(f.getRawResult() == null);
428 <                }
262 <            };
263 <        mainPool.invoke(a);
420 >        RecursiveAction a = new CheckedRecursiveAction() {
421 >            protected void realCompute() {
422 >                AsyncFib f = new AsyncFib(8);
423 >                assertSame(f, f.fork());
424 >                assertNull(f.join());
425 >                assertEquals(21, f.number);
426 >                checkCompletedNormally(f);
427 >            }};
428 >        testInvokeOnPool(mainPool(), a);
429      }
430  
431      /**
432       * get of a forked task returns when task completes
433       */
434      public void testForkGet() {
435 <        RecursiveAction a = new RecursiveAction() {
436 <                public void compute() {
437 <                    try {
438 <                        AsyncFib f = new AsyncFib(8);
439 <                        f.fork();
440 <                        f.get();
441 <                        threadAssertTrue(f.number == 21);
442 <                        threadAssertTrue(f.isDone());
443 <                    } catch (Exception ex) {
279 <                        unexpectedException(ex);
280 <                    }
281 <                }
282 <            };
283 <        mainPool.invoke(a);
435 >        RecursiveAction a = new CheckedRecursiveAction() {
436 >            protected void realCompute() throws Exception {
437 >                AsyncFib f = new AsyncFib(8);
438 >                assertSame(f, f.fork());
439 >                assertNull(f.get());
440 >                assertEquals(21, f.number);
441 >                checkCompletedNormally(f);
442 >            }};
443 >        testInvokeOnPool(mainPool(), a);
444      }
445  
446      /**
447       * timed get of a forked task returns when task completes
448       */
449      public void testForkTimedGet() {
450 <        RecursiveAction a = new RecursiveAction() {
451 <                public void compute() {
452 <                    try {
453 <                        AsyncFib f = new AsyncFib(8);
454 <                        f.fork();
455 <                        f.get(5L, TimeUnit.SECONDS);
456 <                        threadAssertTrue(f.number == 21);
457 <                        threadAssertTrue(f.isDone());
458 <                    } catch (Exception ex) {
299 <                        unexpectedException(ex);
300 <                    }
301 <                }
302 <            };
303 <        mainPool.invoke(a);
450 >        RecursiveAction a = new CheckedRecursiveAction() {
451 >            protected void realCompute() throws Exception {
452 >                AsyncFib f = new AsyncFib(8);
453 >                assertSame(f, f.fork());
454 >                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
455 >                assertEquals(21, f.number);
456 >                checkCompletedNormally(f);
457 >            }};
458 >        testInvokeOnPool(mainPool(), a);
459      }
460  
461      /**
462       * timed get with null time unit throws NPE
463       */
464      public void testForkTimedGetNPE() {
465 <        RecursiveAction a = new RecursiveAction() {
466 <                public void compute() {
467 <                    try {
468 <                        AsyncFib f = new AsyncFib(8);
469 <                        f.fork();
470 <                        f.get(5L, null);
471 <                        shouldThrow();
472 <                    } catch (NullPointerException success) {
473 <                    } catch (Exception ex) {
474 <                        unexpectedException(ex);
320 <                    }
321 <                }
322 <            };
323 <        mainPool.invoke(a);
324 <    }
325 <
326 <    /**
327 <     * helpJoin of a forked task returns when task completes
328 <     */
329 <    public void testForkHelpJoin() {
330 <        RecursiveAction a = new RecursiveAction() {
331 <                public void compute() {
332 <                    AsyncFib f = new AsyncFib(8);
333 <                    f.fork();
334 <                    f.helpJoin();
335 <                    threadAssertTrue(f.number == 21);
336 <                    threadAssertTrue(f.isDone());
337 <                }
338 <            };
339 <        mainPool.invoke(a);
465 >        RecursiveAction a = new CheckedRecursiveAction() {
466 >            protected void realCompute() throws Exception {
467 >                AsyncFib f = new AsyncFib(8);
468 >                assertSame(f, f.fork());
469 >                try {
470 >                    f.get(randomTimeout(), null);
471 >                    shouldThrow();
472 >                } catch (NullPointerException success) {}
473 >            }};
474 >        testInvokeOnPool(mainPool(), a);
475      }
476  
477      /**
478       * quietlyJoin of a forked task returns when task completes
479       */
480      public void testForkQuietlyJoin() {
481 <        RecursiveAction a = new RecursiveAction() {
482 <                public void compute() {
483 <                    AsyncFib f = new AsyncFib(8);
484 <                    f.fork();
485 <                    f.quietlyJoin();
486 <                    threadAssertTrue(f.number == 21);
487 <                    threadAssertTrue(f.isDone());
488 <                }
489 <            };
355 <        mainPool.invoke(a);
356 <    }
357 <
358 <
359 <    /**
360 <     * quietlyHelpJoin of a forked task returns when task completes
361 <     */
362 <    public void testForkQuietlyHelpJoin() {
363 <        RecursiveAction a = new RecursiveAction() {
364 <                public void compute() {
365 <                    AsyncFib f = new AsyncFib(8);
366 <                    f.fork();
367 <                    f.quietlyHelpJoin();
368 <                    threadAssertTrue(f.number == 21);
369 <                    threadAssertTrue(f.isDone());
370 <                }
371 <            };
372 <        mainPool.invoke(a);
481 >        RecursiveAction a = new CheckedRecursiveAction() {
482 >            protected void realCompute() {
483 >                AsyncFib f = new AsyncFib(8);
484 >                assertSame(f, f.fork());
485 >                f.quietlyJoin();
486 >                assertEquals(21, f.number);
487 >                checkCompletedNormally(f);
488 >            }};
489 >        testInvokeOnPool(mainPool(), a);
490      }
491  
375
492      /**
493       * helpQuiesce returns when tasks are complete.
494       * getQueuedTaskCount returns 0 when quiescent
495       */
496      public void testForkHelpQuiesce() {
497 <        RecursiveAction a = new RecursiveAction() {
498 <                public void compute() {
499 <                    AsyncFib f = new AsyncFib(8);
500 <                    f.fork();
501 <                    f.helpQuiesce();
502 <                    threadAssertTrue(f.number == 21);
503 <                    threadAssertTrue(f.isDone());
504 <                    threadAssertTrue(getQueuedTaskCount() == 0);
505 <                }
506 <            };
507 <        mainPool.invoke(a);
497 >        RecursiveAction a = new CheckedRecursiveAction() {
498 >            protected void realCompute() {
499 >                AsyncFib f = new AsyncFib(8);
500 >                assertSame(f, f.fork());
501 >                helpQuiesce();
502 >                while (!f.isDone()) // wait out race
503 >                    ;
504 >                assertEquals(21, f.number);
505 >                assertEquals(0, getQueuedTaskCount());
506 >                checkCompletedNormally(f);
507 >            }};
508 >        testInvokeOnPool(mainPool(), a);
509      }
510  
394
511      /**
512       * invoke task throws exception when task completes abnormally
513       */
514      public void testAbnormalInvoke() {
515 <        RecursiveAction a = new RecursiveAction() {
516 <                public void compute() {
517 <                    try {
518 <                        FailingAsyncFib f = new FailingAsyncFib(8);
519 <                        f.invoke();
520 <                        shouldThrow();
521 <                    } catch (FJException success) {
522 <                    }
515 >        RecursiveAction a = new CheckedRecursiveAction() {
516 >            protected void realCompute() {
517 >                FailingAsyncFib f = new FailingAsyncFib(8);
518 >                try {
519 >                    f.invoke();
520 >                    shouldThrow();
521 >                } catch (FJException success) {
522 >                    checkCompletedAbnormally(f, success);
523                  }
524 <            };
525 <        mainPool.invoke(a);
524 >            }};
525 >        testInvokeOnPool(mainPool(), a);
526      }
527  
528      /**
529       * quietlyInvoke task returns when task completes abnormally
530       */
531      public void testAbnormalQuietlyInvoke() {
532 <        RecursiveAction a = new RecursiveAction() {
533 <                public void compute() {
534 <                    FailingAsyncFib f = new FailingAsyncFib(8);
535 <                    f.quietlyInvoke();
536 <                    threadAssertTrue(f.isDone());
537 <                }
538 <            };
539 <        mainPool.invoke(a);
532 >        RecursiveAction a = new CheckedRecursiveAction() {
533 >            protected void realCompute() {
534 >                FailingAsyncFib f = new FailingAsyncFib(8);
535 >                f.quietlyInvoke();
536 >                assertTrue(f.getException() instanceof FJException);
537 >                checkCompletedAbnormally(f, f.getException());
538 >            }};
539 >        testInvokeOnPool(mainPool(), a);
540      }
541  
542      /**
543       * join of a forked task throws exception when task completes abnormally
544       */
545      public void testAbnormalForkJoin() {
546 <        RecursiveAction a = new RecursiveAction() {
547 <                public void compute() {
548 <                    try {
549 <                        FailingAsyncFib f = new FailingAsyncFib(8);
550 <                        f.fork();
551 <                        f.join();
552 <                        shouldThrow();
553 <                    } catch (FJException success) {
554 <                    }
546 >        RecursiveAction a = new CheckedRecursiveAction() {
547 >            protected void realCompute() {
548 >                FailingAsyncFib f = new FailingAsyncFib(8);
549 >                assertSame(f, f.fork());
550 >                try {
551 >                    f.join();
552 >                    shouldThrow();
553 >                } catch (FJException success) {
554 >                    checkCompletedAbnormally(f, success);
555                  }
556 <            };
557 <        mainPool.invoke(a);
556 >            }};
557 >        testInvokeOnPool(mainPool(), a);
558      }
559  
560      /**
561       * get of a forked task throws exception when task completes abnormally
562       */
563      public void testAbnormalForkGet() {
564 <        RecursiveAction a = new RecursiveAction() {
565 <                public void compute() {
566 <                    try {
567 <                        FailingAsyncFib f = new FailingAsyncFib(8);
568 <                        f.fork();
569 <                        f.get();
570 <                        shouldThrow();
571 <                    } catch (ExecutionException success) {
572 <                    } catch (Exception ex) {
573 <                        unexpectedException(ex);
574 <                    }
564 >        RecursiveAction a = new CheckedRecursiveAction() {
565 >            protected void realCompute() throws Exception {
566 >                FailingAsyncFib f = new FailingAsyncFib(8);
567 >                assertSame(f, f.fork());
568 >                try {
569 >                    f.get();
570 >                    shouldThrow();
571 >                } catch (ExecutionException success) {
572 >                    Throwable cause = success.getCause();
573 >                    assertTrue(cause instanceof FJException);
574 >                    checkCompletedAbnormally(f, cause);
575                  }
576 <            };
577 <        mainPool.invoke(a);
576 >            }};
577 >        testInvokeOnPool(mainPool(), a);
578      }
579  
580      /**
581       * timed get of a forked task throws exception when task completes abnormally
582       */
583      public void testAbnormalForkTimedGet() {
584 <        RecursiveAction a = new RecursiveAction() {
585 <                public void compute() {
586 <                    try {
587 <                        FailingAsyncFib f = new FailingAsyncFib(8);
588 <                        f.fork();
589 <                        f.get(5L, TimeUnit.SECONDS);
590 <                        shouldThrow();
591 <                    } catch (ExecutionException success) {
592 <                    } catch (Exception ex) {
593 <                        unexpectedException(ex);
594 <                    }
479 <                }
480 <            };
481 <        mainPool.invoke(a);
482 <    }
483 <
484 <    /**
485 <     * join of a forked task throws exception when task completes abnormally
486 <     */
487 <    public void testAbnormalForkHelpJoin() {
488 <        RecursiveAction a = new RecursiveAction() {
489 <                public void compute() {
490 <                    try {
491 <                        FailingAsyncFib f = new FailingAsyncFib(8);
492 <                        f.fork();
493 <                        f.helpJoin();
494 <                        shouldThrow();
495 <                    } catch (FJException success) {
496 <                    }
497 <                }
498 <            };
499 <        mainPool.invoke(a);
500 <    }
501 <
502 <    /**
503 <     * quietlyHelpJoin of a forked task returns when task completes abnormally.
504 <     * getException of failed task returns its exception.
505 <     * isCompletedAbnormally of a failed task returns true.
506 <     * isCancelled of a failed uncancelled task returns false
507 <     */
508 <    public void testAbnormalForkQuietlyHelpJoin() {
509 <        RecursiveAction a = new RecursiveAction() {
510 <                public void compute() {
511 <                    FailingAsyncFib f = new FailingAsyncFib(8);
512 <                    f.fork();
513 <                    f.quietlyHelpJoin();
514 <                    threadAssertTrue(f.isDone());
515 <                    threadAssertTrue(f.isCompletedAbnormally());
516 <                    threadAssertFalse(f.isCancelled());
517 <                    threadAssertTrue(f.getException() instanceof FJException);
584 >        RecursiveAction a = new CheckedRecursiveAction() {
585 >            protected void realCompute() throws Exception {
586 >                FailingAsyncFib f = new FailingAsyncFib(8);
587 >                assertSame(f, f.fork());
588 >                try {
589 >                    f.get(LONG_DELAY_MS, MILLISECONDS);
590 >                    shouldThrow();
591 >                } catch (ExecutionException success) {
592 >                    Throwable cause = success.getCause();
593 >                    assertTrue(cause instanceof FJException);
594 >                    checkCompletedAbnormally(f, cause);
595                  }
596 <            };
597 <        mainPool.invoke(a);
596 >            }};
597 >        testInvokeOnPool(mainPool(), a);
598      }
599  
600      /**
601       * quietlyJoin of a forked task returns when task completes abnormally
602       */
603      public void testAbnormalForkQuietlyJoin() {
604 <        RecursiveAction a = new RecursiveAction() {
605 <                public void compute() {
606 <                    FailingAsyncFib f = new FailingAsyncFib(8);
607 <                    f.fork();
608 <                    f.quietlyJoin();
609 <                    threadAssertTrue(f.isDone());
610 <                    threadAssertTrue(f.isCompletedAbnormally());
611 <                    threadAssertTrue(f.getException() instanceof FJException);
612 <                }
536 <            };
537 <        mainPool.invoke(a);
604 >        RecursiveAction a = new CheckedRecursiveAction() {
605 >            protected void realCompute() {
606 >                FailingAsyncFib f = new FailingAsyncFib(8);
607 >                assertSame(f, f.fork());
608 >                f.quietlyJoin();
609 >                assertTrue(f.getException() instanceof FJException);
610 >                checkCompletedAbnormally(f, f.getException());
611 >            }};
612 >        testInvokeOnPool(mainPool(), a);
613      }
614  
615      /**
616       * invoke task throws exception when task cancelled
617       */
618      public void testCancelledInvoke() {
619 <        RecursiveAction a = new RecursiveAction() {
620 <                public void compute() {
621 <                    try {
622 <                        AsyncFib f = new AsyncFib(8);
623 <                        f.cancel(true);
624 <                        f.invoke();
625 <                        shouldThrow();
626 <                    } catch (CancellationException success) {
627 <                    }
619 >        RecursiveAction a = new CheckedRecursiveAction() {
620 >            protected void realCompute() {
621 >                AsyncFib f = new AsyncFib(8);
622 >                assertTrue(f.cancel(true));
623 >                try {
624 >                    f.invoke();
625 >                    shouldThrow();
626 >                } catch (CancellationException success) {
627 >                    checkCancelled(f);
628                  }
629 <            };
630 <        mainPool.invoke(a);
629 >            }};
630 >        testInvokeOnPool(mainPool(), a);
631      }
632  
633      /**
634       * join of a forked task throws exception when task cancelled
635       */
636      public void testCancelledForkJoin() {
637 <        RecursiveAction a = new RecursiveAction() {
638 <                public void compute() {
639 <                    try {
640 <                        AsyncFib f = new AsyncFib(8);
641 <                        f.cancel(true);
642 <                        f.fork();
643 <                        f.join();
644 <                        shouldThrow();
645 <                    } catch (CancellationException success) {
646 <                    }
637 >        RecursiveAction a = new CheckedRecursiveAction() {
638 >            protected void realCompute() {
639 >                AsyncFib f = new AsyncFib(8);
640 >                assertTrue(f.cancel(true));
641 >                assertSame(f, f.fork());
642 >                try {
643 >                    f.join();
644 >                    shouldThrow();
645 >                } catch (CancellationException success) {
646 >                    checkCancelled(f);
647                  }
648 <            };
649 <        mainPool.invoke(a);
648 >            }};
649 >        testInvokeOnPool(mainPool(), a);
650      }
651  
652      /**
653       * get of a forked task throws exception when task cancelled
654       */
655      public void testCancelledForkGet() {
656 <        RecursiveAction a = new RecursiveAction() {
657 <                public void compute() {
658 <                    try {
659 <                        AsyncFib f = new AsyncFib(8);
660 <                        f.cancel(true);
661 <                        f.fork();
662 <                        f.get();
663 <                        shouldThrow();
664 <                    } catch (CancellationException success) {
665 <                    } catch (Exception ex) {
591 <                        unexpectedException(ex);
592 <                    }
656 >        RecursiveAction a = new CheckedRecursiveAction() {
657 >            protected void realCompute() throws Exception {
658 >                AsyncFib f = new AsyncFib(8);
659 >                assertTrue(f.cancel(true));
660 >                assertSame(f, f.fork());
661 >                try {
662 >                    f.get();
663 >                    shouldThrow();
664 >                } catch (CancellationException success) {
665 >                    checkCancelled(f);
666                  }
667 <            };
668 <        mainPool.invoke(a);
667 >            }};
668 >        testInvokeOnPool(mainPool(), a);
669      }
670  
671      /**
672       * timed get of a forked task throws exception when task cancelled
673       */
674 <    public void testCancelledForkTimedGet() {
675 <        RecursiveAction a = new RecursiveAction() {
676 <                public void compute() {
677 <                    try {
678 <                        AsyncFib f = new AsyncFib(8);
679 <                        f.cancel(true);
680 <                        f.fork();
681 <                        f.get(5L, TimeUnit.SECONDS);
682 <                        shouldThrow();
683 <                    } catch (CancellationException success) {
684 <                    } catch (Exception ex) {
612 <                        unexpectedException(ex);
613 <                    }
614 <                }
615 <            };
616 <        mainPool.invoke(a);
617 <    }
618 <
619 <    /**
620 <     * join of a forked task throws exception when task cancelled
621 <     */
622 <    public void testCancelledForkHelpJoin() {
623 <        RecursiveAction a = new RecursiveAction() {
624 <                public void compute() {
625 <                    try {
626 <                        AsyncFib f = new AsyncFib(8);
627 <                        f.cancel(true);
628 <                        f.fork();
629 <                        f.helpJoin();
630 <                        shouldThrow();
631 <                    } catch (CancellationException success) {
632 <                    }
633 <                }
634 <            };
635 <        mainPool.invoke(a);
636 <    }
637 <
638 <    /**
639 <     * quietlyHelpJoin of a forked task returns when task cancelled.
640 <     * getException of cancelled task returns its exception.
641 <     * isCompletedAbnormally of a cancelled task returns true.
642 <     * isCancelled of a cancelled task returns true
643 <     */
644 <    public void testCancelledForkQuietlyHelpJoin() {
645 <        RecursiveAction a = new RecursiveAction() {
646 <                public void compute() {
647 <                    AsyncFib f = new AsyncFib(8);
648 <                    f.cancel(true);
649 <                    f.fork();
650 <                    f.quietlyHelpJoin();
651 <                    threadAssertTrue(f.isDone());
652 <                    threadAssertTrue(f.isCompletedAbnormally());
653 <                    threadAssertTrue(f.isCancelled());
654 <                    threadAssertTrue(f.getException() instanceof CancellationException);
674 >    public void testCancelledForkTimedGet() throws Exception {
675 >        RecursiveAction a = new CheckedRecursiveAction() {
676 >            protected void realCompute() throws Exception {
677 >                AsyncFib f = new AsyncFib(8);
678 >                assertTrue(f.cancel(true));
679 >                assertSame(f, f.fork());
680 >                try {
681 >                    f.get(LONG_DELAY_MS, MILLISECONDS);
682 >                    shouldThrow();
683 >                } catch (CancellationException success) {
684 >                    checkCancelled(f);
685                  }
686 <            };
687 <        mainPool.invoke(a);
686 >            }};
687 >        testInvokeOnPool(mainPool(), a);
688      }
689  
690      /**
691       * quietlyJoin of a forked task returns when task cancelled
692       */
693      public void testCancelledForkQuietlyJoin() {
694 <        RecursiveAction a = new RecursiveAction() {
695 <                public void compute() {
696 <                    AsyncFib f = new AsyncFib(8);
697 <                    f.cancel(true);
698 <                    f.fork();
699 <                    f.quietlyJoin();
700 <                    threadAssertTrue(f.isDone());
701 <                    threadAssertTrue(f.isCompletedAbnormally());
702 <                    threadAssertTrue(f.getException() instanceof CancellationException);
673 <                }
674 <            };
675 <        mainPool.invoke(a);
694 >        RecursiveAction a = new CheckedRecursiveAction() {
695 >            protected void realCompute() {
696 >                AsyncFib f = new AsyncFib(8);
697 >                assertTrue(f.cancel(true));
698 >                assertSame(f, f.fork());
699 >                f.quietlyJoin();
700 >                checkCancelled(f);
701 >            }};
702 >        testInvokeOnPool(mainPool(), a);
703      }
704  
705      /**
706       * getPool of executing task returns its pool
707       */
708      public void testGetPool() {
709 <        RecursiveAction a = new RecursiveAction() {
710 <                public void compute() {
711 <                    threadAssertTrue(getPool() == mainPool);
712 <                }
713 <            };
714 <        mainPool.invoke(a);
709 >        final ForkJoinPool mainPool = mainPool();
710 >        RecursiveAction a = new CheckedRecursiveAction() {
711 >            protected void realCompute() {
712 >                assertSame(mainPool, getPool());
713 >            }};
714 >        testInvokeOnPool(mainPool, a);
715      }
716  
717      /**
718       * getPool of non-FJ task returns null
719       */
720      public void testGetPool2() {
721 <        RecursiveAction a = new RecursiveAction() {
722 <                public void compute() {
723 <                    threadAssertTrue(getPool() == null);
724 <                }
725 <            };
699 <        a.invoke();
721 >        RecursiveAction a = new CheckedRecursiveAction() {
722 >            protected void realCompute() {
723 >                assertNull(getPool());
724 >            }};
725 >        assertNull(a.invoke());
726      }
727  
728      /**
729       * inForkJoinPool of executing task returns true
730       */
731      public void testInForkJoinPool() {
732 <        RecursiveAction a = new RecursiveAction() {
733 <                public void compute() {
734 <                    threadAssertTrue(inForkJoinPool());
735 <                }
736 <            };
711 <        mainPool.invoke(a);
732 >        RecursiveAction a = new CheckedRecursiveAction() {
733 >            protected void realCompute() {
734 >                assertTrue(inForkJoinPool());
735 >            }};
736 >        testInvokeOnPool(mainPool(), a);
737      }
738  
739      /**
740       * inForkJoinPool of non-FJ task returns false
741       */
742      public void testInForkJoinPool2() {
743 <        RecursiveAction a = new RecursiveAction() {
744 <                public void compute() {
745 <                    threadAssertTrue(!inForkJoinPool());
746 <                }
747 <            };
723 <        a.invoke();
743 >        RecursiveAction a = new CheckedRecursiveAction() {
744 >            protected void realCompute() {
745 >                assertFalse(inForkJoinPool());
746 >            }};
747 >        assertNull(a.invoke());
748      }
749  
750      /**
751       * setRawResult(null) succeeds
752       */
753      public void testSetRawResult() {
754 <        RecursiveAction a = new RecursiveAction() {
755 <                public void compute() {
756 <                    setRawResult(null);
757 <                }
758 <            };
759 <        a.invoke();
754 >        RecursiveAction a = new CheckedRecursiveAction() {
755 >            protected void realCompute() {
756 >                setRawResult(null);
757 >                assertNull(getRawResult());
758 >            }};
759 >        assertNull(a.invoke());
760      }
761  
762      /**
763       * invoke task throws exception after invoking completeExceptionally
764       */
765      public void testCompleteExceptionally() {
766 <        RecursiveAction a = new RecursiveAction() {
767 <                public void compute() {
768 <                    try {
769 <                        AsyncFib f = new AsyncFib(8);
770 <                        f.completeExceptionally(new FJException());
771 <                        f.invoke();
772 <                        shouldThrow();
773 <                    } catch (FJException success) {
774 <                    }
766 >        RecursiveAction a = new CheckedRecursiveAction() {
767 >            protected void realCompute() {
768 >                AsyncFib f = new AsyncFib(8);
769 >                f.completeExceptionally(new FJException());
770 >                try {
771 >                    f.invoke();
772 >                    shouldThrow();
773 >                } catch (FJException success) {
774 >                    checkCompletedAbnormally(f, success);
775                  }
776 <            };
777 <        mainPool.invoke(a);
776 >            }};
777 >        testInvokeOnPool(mainPool(), a);
778 >    }
779 >
780 >    /**
781 >     * completeExceptionally(null) surprisingly has the same effect as
782 >     * completeExceptionally(new RuntimeException())
783 >     */
784 >    public void testCompleteExceptionally_null() {
785 >        RecursiveAction a = new CheckedRecursiveAction() {
786 >            protected void realCompute() {
787 >                AsyncFib f = new AsyncFib(8);
788 >                f.completeExceptionally(null);
789 >                try {
790 >                    f.invoke();
791 >                    shouldThrow();
792 >                } catch (RuntimeException success) {
793 >                    assertSame(success.getClass(), RuntimeException.class);
794 >                    assertNull(success.getCause());
795 >                    checkCompletedAbnormally(f, success);
796 >                }
797 >            }};
798 >        testInvokeOnPool(mainPool(), a);
799      }
800  
801      /**
802       * invokeAll(t1, t2) invokes all task arguments
803       */
804      public void testInvokeAll2() {
805 <        RecursiveAction a = new RecursiveAction() {
806 <                public void compute() {
807 <                    AsyncFib f = new AsyncFib(8);
808 <                    AsyncFib g = new AsyncFib(9);
809 <                    invokeAll(f, g);
810 <                    threadAssertTrue(f.isDone());
811 <                    threadAssertTrue(f.number == 21);
812 <                    threadAssertTrue(g.isDone());
813 <                    threadAssertTrue(g.number == 34);
814 <                }
815 <            };
771 <        mainPool.invoke(a);
805 >        RecursiveAction a = new CheckedRecursiveAction() {
806 >            protected void realCompute() {
807 >                AsyncFib f = new AsyncFib(8);
808 >                AsyncFib g = new AsyncFib(9);
809 >                invokeAll(f, g);
810 >                assertEquals(21, f.number);
811 >                assertEquals(34, g.number);
812 >                checkCompletedNormally(f);
813 >                checkCompletedNormally(g);
814 >            }};
815 >        testInvokeOnPool(mainPool(), a);
816      }
817  
818      /**
819       * invokeAll(tasks) with 1 argument invokes task
820       */
821      public void testInvokeAll1() {
822 <        RecursiveAction a = new RecursiveAction() {
823 <                public void compute() {
824 <                    AsyncFib f = new AsyncFib(8);
825 <                    invokeAll(f);
826 <                    threadAssertTrue(f.isDone());
827 <                    threadAssertTrue(f.number == 21);
828 <                }
829 <            };
786 <        mainPool.invoke(a);
822 >        RecursiveAction a = new CheckedRecursiveAction() {
823 >            protected void realCompute() {
824 >                AsyncFib f = new AsyncFib(8);
825 >                invokeAll(f);
826 >                checkCompletedNormally(f);
827 >                assertEquals(21, f.number);
828 >            }};
829 >        testInvokeOnPool(mainPool(), a);
830      }
831  
832      /**
833       * invokeAll(tasks) with > 2 argument invokes tasks
834       */
835      public void testInvokeAll3() {
836 <        RecursiveAction a = new RecursiveAction() {
837 <                public void compute() {
838 <                    AsyncFib f = new AsyncFib(8);
839 <                    AsyncFib g = new AsyncFib(9);
840 <                    AsyncFib h = new AsyncFib(7);
841 <                    invokeAll(f, g, h);
842 <                    threadAssertTrue(f.isDone());
843 <                    threadAssertTrue(f.number == 21);
844 <                    threadAssertTrue(g.isDone());
845 <                    threadAssertTrue(g.number == 34);
846 <                    threadAssertTrue(h.isDone());
847 <                    threadAssertTrue(h.number == 13);
848 <                }
849 <            };
807 <        mainPool.invoke(a);
836 >        RecursiveAction a = new CheckedRecursiveAction() {
837 >            protected void realCompute() {
838 >                AsyncFib f = new AsyncFib(8);
839 >                AsyncFib g = new AsyncFib(9);
840 >                AsyncFib h = new AsyncFib(7);
841 >                invokeAll(f, g, h);
842 >                assertEquals(21, f.number);
843 >                assertEquals(34, g.number);
844 >                assertEquals(13, h.number);
845 >                checkCompletedNormally(f);
846 >                checkCompletedNormally(g);
847 >                checkCompletedNormally(h);
848 >            }};
849 >        testInvokeOnPool(mainPool(), a);
850      }
851  
852      /**
853       * invokeAll(collection) invokes all tasks in the collection
854       */
855      public void testInvokeAllCollection() {
856 <        RecursiveAction a = new RecursiveAction() {
857 <                public void compute() {
858 <                    AsyncFib f = new AsyncFib(8);
859 <                    AsyncFib g = new AsyncFib(9);
860 <                    AsyncFib h = new AsyncFib(7);
861 <                    HashSet set = new HashSet();
862 <                    set.add(f);
863 <                    set.add(g);
864 <                    set.add(h);
865 <                    invokeAll(set);
866 <                    threadAssertTrue(f.isDone());
867 <                    threadAssertTrue(f.number == 21);
868 <                    threadAssertTrue(g.isDone());
869 <                    threadAssertTrue(g.number == 34);
870 <                    threadAssertTrue(h.isDone());
871 <                    threadAssertTrue(h.number == 13);
872 <                }
873 <            };
832 <        mainPool.invoke(a);
856 >        RecursiveAction a = new CheckedRecursiveAction() {
857 >            protected void realCompute() {
858 >                AsyncFib f = new AsyncFib(8);
859 >                AsyncFib g = new AsyncFib(9);
860 >                AsyncFib h = new AsyncFib(7);
861 >                HashSet set = new HashSet();
862 >                set.add(f);
863 >                set.add(g);
864 >                set.add(h);
865 >                invokeAll(set);
866 >                assertEquals(21, f.number);
867 >                assertEquals(34, g.number);
868 >                assertEquals(13, h.number);
869 >                checkCompletedNormally(f);
870 >                checkCompletedNormally(g);
871 >                checkCompletedNormally(h);
872 >            }};
873 >        testInvokeOnPool(mainPool(), a);
874      }
875  
835
876      /**
877       * invokeAll(tasks) with any null task throws NPE
878       */
879      public void testInvokeAllNPE() {
880 <        RecursiveAction a = new RecursiveAction() {
881 <                public void compute() {
882 <                    try {
883 <                        AsyncFib f = new AsyncFib(8);
884 <                        AsyncFib g = new AsyncFib(9);
885 <                        AsyncFib h = null;
886 <                        invokeAll(f, g, h);
887 <                        shouldThrow();
888 <                    } catch (NullPointerException success) {
889 <                    }
890 <                }
851 <            };
852 <        mainPool.invoke(a);
880 >        RecursiveAction a = new CheckedRecursiveAction() {
881 >            protected void realCompute() {
882 >                AsyncFib f = new AsyncFib(8);
883 >                AsyncFib g = new AsyncFib(9);
884 >                AsyncFib h = null;
885 >                try {
886 >                    invokeAll(f, g, h);
887 >                    shouldThrow();
888 >                } catch (NullPointerException success) {}
889 >            }};
890 >        testInvokeOnPool(mainPool(), a);
891      }
892  
893      /**
894       * invokeAll(t1, t2) throw exception if any task does
895       */
896      public void testAbnormalInvokeAll2() {
897 <        RecursiveAction a = new RecursiveAction() {
898 <                public void compute() {
899 <                    try {
900 <                        AsyncFib f = new AsyncFib(8);
901 <                        FailingAsyncFib g = new FailingAsyncFib(9);
902 <                        invokeAll(f, g);
903 <                        shouldThrow();
904 <                    } catch (FJException success) {
905 <                    }
897 >        RecursiveAction a = new CheckedRecursiveAction() {
898 >            protected void realCompute() {
899 >                AsyncFib f = new AsyncFib(8);
900 >                FailingAsyncFib g = new FailingAsyncFib(9);
901 >                ForkJoinTask[] tasks = { f, g };
902 >                shuffle(tasks);
903 >                try {
904 >                    invokeAll(tasks);
905 >                    shouldThrow();
906 >                } catch (FJException success) {
907 >                    checkCompletedAbnormally(g, success);
908                  }
909 <            };
910 <        mainPool.invoke(a);
909 >            }};
910 >        testInvokeOnPool(mainPool(), a);
911      }
912  
913      /**
914       * invokeAll(tasks) with 1 argument throws exception if task does
915       */
916      public void testAbnormalInvokeAll1() {
917 <        RecursiveAction a = new RecursiveAction() {
918 <                public void compute() {
919 <                    try {
920 <                        FailingAsyncFib g = new FailingAsyncFib(9);
921 <                        invokeAll(g);
922 <                        shouldThrow();
923 <                    } catch (FJException success) {
924 <                    }
917 >        RecursiveAction a = new CheckedRecursiveAction() {
918 >            protected void realCompute() {
919 >                FailingAsyncFib g = new FailingAsyncFib(9);
920 >                try {
921 >                    invokeAll(g);
922 >                    shouldThrow();
923 >                } catch (FJException success) {
924 >                    checkCompletedAbnormally(g, success);
925                  }
926 <            };
927 <        mainPool.invoke(a);
926 >            }};
927 >        testInvokeOnPool(mainPool(), a);
928      }
929  
930      /**
931       * invokeAll(tasks) with > 2 argument throws exception if any task does
932       */
933      public void testAbnormalInvokeAll3() {
934 <        RecursiveAction a = new RecursiveAction() {
935 <                public void compute() {
936 <                    try {
937 <                        AsyncFib f = new AsyncFib(8);
938 <                        FailingAsyncFib g = new FailingAsyncFib(9);
939 <                        AsyncFib h = new AsyncFib(7);
940 <                        invokeAll(f, g, h);
941 <                        shouldThrow();
942 <                    } catch (FJException success) {
943 <                    }
934 >        RecursiveAction a = new CheckedRecursiveAction() {
935 >            protected void realCompute() {
936 >                AsyncFib f = new AsyncFib(8);
937 >                FailingAsyncFib g = new FailingAsyncFib(9);
938 >                AsyncFib h = new AsyncFib(7);
939 >                ForkJoinTask[] tasks = { f, g, h };
940 >                shuffle(tasks);
941 >                try {
942 >                    invokeAll(tasks);
943 >                    shouldThrow();
944 >                } catch (FJException success) {
945 >                    checkCompletedAbnormally(g, success);
946                  }
947 <            };
948 <        mainPool.invoke(a);
947 >            }};
948 >        testInvokeOnPool(mainPool(), a);
949      }
950  
951      /**
952 <     * invokeAll(collection)  throws exception if any task does
952 >     * invokeAll(collection) throws exception if any task does
953       */
954      public void testAbnormalInvokeAllCollection() {
955 <        RecursiveAction a = new RecursiveAction() {
956 <                public void compute() {
957 <                    try {
958 <                        FailingAsyncFib f = new FailingAsyncFib(8);
959 <                        AsyncFib g = new AsyncFib(9);
960 <                        AsyncFib h = new AsyncFib(7);
961 <                        HashSet set = new HashSet();
962 <                        set.add(f);
963 <                        set.add(g);
964 <                        set.add(h);
965 <                        invokeAll(set);
966 <                        shouldThrow();
925 <                    } catch (FJException success) {
926 <                    }
955 >        RecursiveAction a = new CheckedRecursiveAction() {
956 >            protected void realCompute() {
957 >                FailingAsyncFib f = new FailingAsyncFib(8);
958 >                AsyncFib g = new AsyncFib(9);
959 >                AsyncFib h = new AsyncFib(7);
960 >                ForkJoinTask[] tasks = { f, g, h };
961 >                shuffle(tasks);
962 >                try {
963 >                    invokeAll(Arrays.asList(tasks));
964 >                    shouldThrow();
965 >                } catch (FJException success) {
966 >                    checkCompletedAbnormally(f, success);
967                  }
968 <            };
969 <        mainPool.invoke(a);
968 >            }};
969 >        testInvokeOnPool(mainPool(), a);
970      }
971  
972      /**
# Line 934 | Line 974 | public class ForkJoinTaskTest extends JS
974       * and suppresses execution
975       */
976      public void testTryUnfork() {
977 <        RecursiveAction a = new RecursiveAction() {
978 <                public void compute() {
979 <                    AsyncFib g = new AsyncFib(9);
980 <                    g.fork();
981 <                    AsyncFib f = new AsyncFib(8);
982 <                    f.fork();
983 <                    threadAssertTrue(f.tryUnfork());
984 <                    helpQuiesce();
985 <                    threadAssertFalse(f.isDone());
986 <                    threadAssertTrue(g.isDone());
987 <                }
988 <            };
949 <        singletonPool.invoke(a);
977 >        RecursiveAction a = new CheckedRecursiveAction() {
978 >            protected void realCompute() {
979 >                AsyncFib g = new AsyncFib(9);
980 >                assertSame(g, g.fork());
981 >                AsyncFib f = new AsyncFib(8);
982 >                assertSame(f, f.fork());
983 >                assertTrue(f.tryUnfork());
984 >                helpQuiesce();
985 >                checkNotDone(f);
986 >                checkCompletedNormally(g);
987 >            }};
988 >        testInvokeOnPool(singletonPool(), a);
989      }
990  
991      /**
# Line 954 | Line 993 | public class ForkJoinTaskTest extends JS
993       * there are more tasks than threads
994       */
995      public void testGetSurplusQueuedTaskCount() {
996 <        RecursiveAction a = new RecursiveAction() {
997 <                public void compute() {
998 <                    AsyncFib h = new AsyncFib(7);
999 <                    h.fork();
1000 <                    AsyncFib g = new AsyncFib(9);
1001 <                    g.fork();
1002 <                    AsyncFib f = new AsyncFib(8);
1003 <                    f.fork();
1004 <                    threadAssertTrue(getSurplusQueuedTaskCount() > 0);
1005 <                    helpQuiesce();
1006 <                }
1007 <            };
1008 <        singletonPool.invoke(a);
996 >        RecursiveAction a = new CheckedRecursiveAction() {
997 >            protected void realCompute() {
998 >                AsyncFib h = new AsyncFib(7);
999 >                assertSame(h, h.fork());
1000 >                AsyncFib g = new AsyncFib(9);
1001 >                assertSame(g, g.fork());
1002 >                AsyncFib f = new AsyncFib(8);
1003 >                assertSame(f, f.fork());
1004 >                assertTrue(getSurplusQueuedTaskCount() > 0);
1005 >                helpQuiesce();
1006 >                assertEquals(0, getSurplusQueuedTaskCount());
1007 >                checkCompletedNormally(f);
1008 >                checkCompletedNormally(g);
1009 >                checkCompletedNormally(h);
1010 >            }};
1011 >        testInvokeOnPool(singletonPool(), a);
1012      }
1013  
1014      /**
1015       * peekNextLocalTask returns most recent unexecuted task.
1016       */
1017      public void testPeekNextLocalTask() {
1018 <        RecursiveAction a = new RecursiveAction() {
1019 <                public void compute() {
1020 <                    AsyncFib g = new AsyncFib(9);
1021 <                    g.fork();
1022 <                    AsyncFib f = new AsyncFib(8);
1023 <                    f.fork();
1024 <                    threadAssertTrue(peekNextLocalTask() == f);
1025 <                    f.join();
1026 <                    threadAssertTrue(f.isDone());
1027 <                    helpQuiesce();
1028 <                }
1029 <            };
1030 <        singletonPool.invoke(a);
1018 >        RecursiveAction a = new CheckedRecursiveAction() {
1019 >            protected void realCompute() {
1020 >                AsyncFib g = new AsyncFib(9);
1021 >                assertSame(g, g.fork());
1022 >                AsyncFib f = new AsyncFib(8);
1023 >                assertSame(f, f.fork());
1024 >                assertSame(f, peekNextLocalTask());
1025 >                assertNull(f.join());
1026 >                checkCompletedNormally(f);
1027 >                helpQuiesce();
1028 >                checkCompletedNormally(g);
1029 >            }};
1030 >        testInvokeOnPool(singletonPool(), a);
1031      }
1032  
1033      /**
1034 <     * pollNextLocalTask returns most recent unexecuted task
1035 <     * without executing it
1034 >     * pollNextLocalTask returns most recent unexecuted task without
1035 >     * executing it
1036       */
1037      public void testPollNextLocalTask() {
1038 <        RecursiveAction a = new RecursiveAction() {
1039 <                public void compute() {
1040 <                    AsyncFib g = new AsyncFib(9);
1041 <                    g.fork();
1042 <                    AsyncFib f = new AsyncFib(8);
1043 <                    f.fork();
1044 <                    threadAssertTrue(pollNextLocalTask() == f);
1045 <                    helpQuiesce();
1046 <                    threadAssertFalse(f.isDone());
1047 <                }
1048 <            };
1049 <        singletonPool.invoke(a);
1038 >        RecursiveAction a = new CheckedRecursiveAction() {
1039 >            protected void realCompute() {
1040 >                AsyncFib g = new AsyncFib(9);
1041 >                assertSame(g, g.fork());
1042 >                AsyncFib f = new AsyncFib(8);
1043 >                assertSame(f, f.fork());
1044 >                assertSame(f, pollNextLocalTask());
1045 >                helpQuiesce();
1046 >                checkNotDone(f);
1047 >                assertEquals(34, g.number);
1048 >                checkCompletedNormally(g);
1049 >            }};
1050 >        testInvokeOnPool(singletonPool(), a);
1051      }
1052  
1053      /**
1054 <     * pollTask returns an unexecuted task
1012 <     * without executing it
1054 >     * pollTask returns an unexecuted task without executing it
1055       */
1056      public void testPollTask() {
1057 <        RecursiveAction a = new RecursiveAction() {
1058 <                public void compute() {
1059 <                    AsyncFib g = new AsyncFib(9);
1060 <                    g.fork();
1061 <                    AsyncFib f = new AsyncFib(8);
1062 <                    f.fork();
1063 <                    threadAssertTrue(pollTask() == f);
1064 <                    helpQuiesce();
1065 <                    threadAssertFalse(f.isDone());
1066 <                    threadAssertTrue(g.isDone());
1067 <                }
1068 <            };
1027 <        singletonPool.invoke(a);
1057 >        RecursiveAction a = new CheckedRecursiveAction() {
1058 >            protected void realCompute() {
1059 >                AsyncFib g = new AsyncFib(9);
1060 >                assertSame(g, g.fork());
1061 >                AsyncFib f = new AsyncFib(8);
1062 >                assertSame(f, f.fork());
1063 >                assertSame(f, pollTask());
1064 >                helpQuiesce();
1065 >                checkNotDone(f);
1066 >                checkCompletedNormally(g);
1067 >            }};
1068 >        testInvokeOnPool(singletonPool(), a);
1069      }
1070  
1071      /**
1072       * peekNextLocalTask returns least recent unexecuted task in async mode
1073       */
1074      public void testPeekNextLocalTaskAsync() {
1075 <        RecursiveAction a = new RecursiveAction() {
1076 <                public void compute() {
1077 <                    AsyncFib g = new AsyncFib(9);
1078 <                    g.fork();
1079 <                    AsyncFib f = new AsyncFib(8);
1080 <                    f.fork();
1081 <                    threadAssertTrue(peekNextLocalTask() == g);
1075 >        RecursiveAction a = new CheckedRecursiveAction() {
1076 >            protected void realCompute() {
1077 >                AsyncFib g = new AsyncFib(9);
1078 >                assertSame(g, g.fork());
1079 >                AsyncFib f = new AsyncFib(8);
1080 >                assertSame(f, f.fork());
1081 >                assertSame(g, peekNextLocalTask());
1082 >                assertNull(f.join());
1083 >                helpQuiesce();
1084 >                checkCompletedNormally(f);
1085 >                assertEquals(34, g.number);
1086 >                checkCompletedNormally(g);
1087 >            }};
1088 >        testInvokeOnPool(asyncSingletonPool(), a);
1089 >    }
1090 >
1091 >    /**
1092 >     * pollNextLocalTask returns least recent unexecuted task without
1093 >     * executing it, in async mode
1094 >     */
1095 >    public void testPollNextLocalTaskAsync() {
1096 >        RecursiveAction a = new CheckedRecursiveAction() {
1097 >            protected void realCompute() {
1098 >                AsyncFib g = new AsyncFib(9);
1099 >                assertSame(g, g.fork());
1100 >                AsyncFib f = new AsyncFib(8);
1101 >                assertSame(f, f.fork());
1102 >                assertSame(g, pollNextLocalTask());
1103 >                helpQuiesce();
1104 >                assertEquals(21, f.number);
1105 >                checkCompletedNormally(f);
1106 >                checkNotDone(g);
1107 >            }};
1108 >        testInvokeOnPool(asyncSingletonPool(), a);
1109 >    }
1110 >
1111 >    /**
1112 >     * pollTask returns an unexecuted task without executing it, in
1113 >     * async mode
1114 >     */
1115 >    public void testPollTaskAsync() {
1116 >        RecursiveAction a = new CheckedRecursiveAction() {
1117 >            protected void realCompute() {
1118 >                AsyncFib g = new AsyncFib(9);
1119 >                assertSame(g, g.fork());
1120 >                AsyncFib f = new AsyncFib(8);
1121 >                assertSame(f, f.fork());
1122 >                assertSame(g, pollTask());
1123 >                helpQuiesce();
1124 >                assertEquals(21, f.number);
1125 >                checkCompletedNormally(f);
1126 >                checkNotDone(g);
1127 >            }};
1128 >        testInvokeOnPool(asyncSingletonPool(), a);
1129 >    }
1130 >
1131 >    // versions for singleton pools
1132 >
1133 >    /**
1134 >     * invoke returns when task completes normally.
1135 >     * isCompletedAbnormally and isCancelled return false for normally
1136 >     * completed tasks; getRawResult returns null.
1137 >     */
1138 >    public void testInvokeSingleton() {
1139 >        RecursiveAction a = new CheckedRecursiveAction() {
1140 >            protected void realCompute() {
1141 >                AsyncFib f = new AsyncFib(8);
1142 >                assertNull(f.invoke());
1143 >                assertEquals(21, f.number);
1144 >                checkCompletedNormally(f);
1145 >            }};
1146 >        testInvokeOnPool(singletonPool(), a);
1147 >    }
1148 >
1149 >    /**
1150 >     * quietlyInvoke task returns when task completes normally.
1151 >     * isCompletedAbnormally and isCancelled return false for normally
1152 >     * completed tasks
1153 >     */
1154 >    public void testQuietlyInvokeSingleton() {
1155 >        RecursiveAction a = new CheckedRecursiveAction() {
1156 >            protected void realCompute() {
1157 >                AsyncFib f = new AsyncFib(8);
1158 >                f.quietlyInvoke();
1159 >                assertEquals(21, f.number);
1160 >                checkCompletedNormally(f);
1161 >            }};
1162 >        testInvokeOnPool(singletonPool(), a);
1163 >    }
1164 >
1165 >    /**
1166 >     * join of a forked task returns when task completes
1167 >     */
1168 >    public void testForkJoinSingleton() {
1169 >        RecursiveAction a = new CheckedRecursiveAction() {
1170 >            protected void realCompute() {
1171 >                AsyncFib f = new AsyncFib(8);
1172 >                assertSame(f, f.fork());
1173 >                assertNull(f.join());
1174 >                assertEquals(21, f.number);
1175 >                checkCompletedNormally(f);
1176 >            }};
1177 >        testInvokeOnPool(singletonPool(), a);
1178 >    }
1179 >
1180 >    /**
1181 >     * get of a forked task returns when task completes
1182 >     */
1183 >    public void testForkGetSingleton() {
1184 >        RecursiveAction a = new CheckedRecursiveAction() {
1185 >            protected void realCompute() throws Exception {
1186 >                AsyncFib f = new AsyncFib(8);
1187 >                assertSame(f, f.fork());
1188 >                assertNull(f.get());
1189 >                assertEquals(21, f.number);
1190 >                checkCompletedNormally(f);
1191 >            }};
1192 >        testInvokeOnPool(singletonPool(), a);
1193 >    }
1194 >
1195 >    /**
1196 >     * timed get of a forked task returns when task completes
1197 >     */
1198 >    public void testForkTimedGetSingleton() {
1199 >        RecursiveAction a = new CheckedRecursiveAction() {
1200 >            protected void realCompute() throws Exception {
1201 >                AsyncFib f = new AsyncFib(8);
1202 >                assertSame(f, f.fork());
1203 >                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1204 >                assertEquals(21, f.number);
1205 >                checkCompletedNormally(f);
1206 >            }};
1207 >        testInvokeOnPool(singletonPool(), a);
1208 >    }
1209 >
1210 >    /**
1211 >     * timed get with null time unit throws NPE
1212 >     */
1213 >    public void testForkTimedGetNPESingleton() {
1214 >        RecursiveAction a = new CheckedRecursiveAction() {
1215 >            protected void realCompute() throws Exception {
1216 >                AsyncFib f = new AsyncFib(8);
1217 >                assertSame(f, f.fork());
1218 >                try {
1219 >                    f.get(randomTimeout(), null);
1220 >                    shouldThrow();
1221 >                } catch (NullPointerException success) {}
1222 >            }};
1223 >        testInvokeOnPool(singletonPool(), a);
1224 >    }
1225 >
1226 >    /**
1227 >     * quietlyJoin of a forked task returns when task completes
1228 >     */
1229 >    public void testForkQuietlyJoinSingleton() {
1230 >        RecursiveAction a = new CheckedRecursiveAction() {
1231 >            protected void realCompute() {
1232 >                AsyncFib f = new AsyncFib(8);
1233 >                assertSame(f, f.fork());
1234 >                f.quietlyJoin();
1235 >                assertEquals(21, f.number);
1236 >                checkCompletedNormally(f);
1237 >            }};
1238 >        testInvokeOnPool(singletonPool(), a);
1239 >    }
1240 >
1241 >    /**
1242 >     * helpQuiesce returns when tasks are complete.
1243 >     * getQueuedTaskCount returns 0 when quiescent
1244 >     */
1245 >    public void testForkHelpQuiesceSingleton() {
1246 >        RecursiveAction a = new CheckedRecursiveAction() {
1247 >            protected void realCompute() {
1248 >                AsyncFib f = new AsyncFib(8);
1249 >                assertSame(f, f.fork());
1250 >                helpQuiesce();
1251 >                assertEquals(0, getQueuedTaskCount());
1252 >                assertEquals(21, f.number);
1253 >                checkCompletedNormally(f);
1254 >            }};
1255 >        testInvokeOnPool(singletonPool(), a);
1256 >    }
1257 >
1258 >    /**
1259 >     * invoke task throws exception when task completes abnormally
1260 >     */
1261 >    public void testAbnormalInvokeSingleton() {
1262 >        RecursiveAction a = new CheckedRecursiveAction() {
1263 >            protected void realCompute() {
1264 >                FailingAsyncFib f = new FailingAsyncFib(8);
1265 >                try {
1266 >                    f.invoke();
1267 >                    shouldThrow();
1268 >                } catch (FJException success) {
1269 >                    checkCompletedAbnormally(f, success);
1270 >                }
1271 >            }};
1272 >        testInvokeOnPool(singletonPool(), a);
1273 >    }
1274 >
1275 >    /**
1276 >     * quietlyInvoke task returns when task completes abnormally
1277 >     */
1278 >    public void testAbnormalQuietlyInvokeSingleton() {
1279 >        RecursiveAction a = new CheckedRecursiveAction() {
1280 >            protected void realCompute() {
1281 >                FailingAsyncFib f = new FailingAsyncFib(8);
1282 >                f.quietlyInvoke();
1283 >                assertTrue(f.getException() instanceof FJException);
1284 >                checkCompletedAbnormally(f, f.getException());
1285 >            }};
1286 >        testInvokeOnPool(singletonPool(), a);
1287 >    }
1288 >
1289 >    /**
1290 >     * join of a forked task throws exception when task completes abnormally
1291 >     */
1292 >    public void testAbnormalForkJoinSingleton() {
1293 >        RecursiveAction a = new CheckedRecursiveAction() {
1294 >            protected void realCompute() {
1295 >                FailingAsyncFib f = new FailingAsyncFib(8);
1296 >                assertSame(f, f.fork());
1297 >                try {
1298                      f.join();
1299 <                    helpQuiesce();
1300 <                    threadAssertTrue(f.isDone());
1299 >                    shouldThrow();
1300 >                } catch (FJException success) {
1301 >                    checkCompletedAbnormally(f, success);
1302                  }
1303 <            };
1304 <        asyncSingletonPool.invoke(a);
1303 >            }};
1304 >        testInvokeOnPool(singletonPool(), a);
1305      }
1306  
1307      /**
1308 <     * pollNextLocalTask returns least recent unexecuted task
1051 <     * without executing it, in async mode
1308 >     * get of a forked task throws exception when task completes abnormally
1309       */
1310 <    public void testPollNextLocalTaskAsync() {
1311 <        RecursiveAction a = new RecursiveAction() {
1312 <                public void compute() {
1313 <                    AsyncFib g = new AsyncFib(9);
1314 <                    g.fork();
1315 <                    AsyncFib f = new AsyncFib(8);
1316 <                    f.fork();
1317 <                    threadAssertTrue(pollNextLocalTask() == g);
1318 <                    helpQuiesce();
1319 <                    threadAssertTrue(f.isDone());
1320 <                    threadAssertFalse(g.isDone());
1310 >    public void testAbnormalForkGetSingleton() {
1311 >        RecursiveAction a = new CheckedRecursiveAction() {
1312 >            protected void realCompute() throws Exception {
1313 >                FailingAsyncFib f = new FailingAsyncFib(8);
1314 >                assertSame(f, f.fork());
1315 >                try {
1316 >                    f.get();
1317 >                    shouldThrow();
1318 >                } catch (ExecutionException success) {
1319 >                    Throwable cause = success.getCause();
1320 >                    assertTrue(cause instanceof FJException);
1321 >                    checkCompletedAbnormally(f, cause);
1322 >                }
1323 >            }};
1324 >        testInvokeOnPool(singletonPool(), a);
1325 >    }
1326 >
1327 >    /**
1328 >     * timed get of a forked task throws exception when task completes abnormally
1329 >     */
1330 >    public void testAbnormalForkTimedGetSingleton() {
1331 >        RecursiveAction a = new CheckedRecursiveAction() {
1332 >            protected void realCompute() throws Exception {
1333 >                FailingAsyncFib f = new FailingAsyncFib(8);
1334 >                assertSame(f, f.fork());
1335 >                try {
1336 >                    f.get(LONG_DELAY_MS, MILLISECONDS);
1337 >                    shouldThrow();
1338 >                } catch (ExecutionException success) {
1339 >                    Throwable cause = success.getCause();
1340 >                    assertTrue(cause instanceof FJException);
1341 >                    checkCompletedAbnormally(f, cause);
1342                  }
1343 <            };
1344 <        asyncSingletonPool.invoke(a);
1343 >            }};
1344 >        testInvokeOnPool(singletonPool(), a);
1345      }
1346  
1347      /**
1348 <     * pollTask returns an unexecuted task
1071 <     * without executing it, in async mode
1348 >     * quietlyJoin of a forked task returns when task completes abnormally
1349       */
1350 <    public void testPollTaskAsync() {
1351 <        RecursiveAction a = new RecursiveAction() {
1352 <                public void compute() {
1353 <                    AsyncFib g = new AsyncFib(9);
1354 <                    g.fork();
1355 <                    AsyncFib f = new AsyncFib(8);
1356 <                    f.fork();
1357 <                    threadAssertTrue(pollTask() == g);
1358 <                    helpQuiesce();
1359 <                    threadAssertTrue(f.isDone());
1360 <                    threadAssertFalse(g.isDone());
1350 >    public void testAbnormalForkQuietlyJoinSingleton() {
1351 >        RecursiveAction a = new CheckedRecursiveAction() {
1352 >            protected void realCompute() {
1353 >                FailingAsyncFib f = new FailingAsyncFib(8);
1354 >                assertSame(f, f.fork());
1355 >                f.quietlyJoin();
1356 >                assertTrue(f.getException() instanceof FJException);
1357 >                checkCompletedAbnormally(f, f.getException());
1358 >            }};
1359 >        testInvokeOnPool(singletonPool(), a);
1360 >    }
1361 >
1362 >    /**
1363 >     * invoke task throws exception when task cancelled
1364 >     */
1365 >    public void testCancelledInvokeSingleton() {
1366 >        RecursiveAction a = new CheckedRecursiveAction() {
1367 >            protected void realCompute() {
1368 >                AsyncFib f = new AsyncFib(8);
1369 >                assertTrue(f.cancel(true));
1370 >                try {
1371 >                    f.invoke();
1372 >                    shouldThrow();
1373 >                } catch (CancellationException success) {
1374 >                    checkCancelled(f);
1375 >                }
1376 >            }};
1377 >        testInvokeOnPool(singletonPool(), a);
1378 >    }
1379 >
1380 >    /**
1381 >     * join of a forked task throws exception when task cancelled
1382 >     */
1383 >    public void testCancelledForkJoinSingleton() {
1384 >        RecursiveAction a = new CheckedRecursiveAction() {
1385 >            protected void realCompute() {
1386 >                AsyncFib f = new AsyncFib(8);
1387 >                assertTrue(f.cancel(true));
1388 >                assertSame(f, f.fork());
1389 >                try {
1390 >                    f.join();
1391 >                    shouldThrow();
1392 >                } catch (CancellationException success) {
1393 >                    checkCancelled(f);
1394 >                }
1395 >            }};
1396 >        testInvokeOnPool(singletonPool(), a);
1397 >    }
1398 >
1399 >    /**
1400 >     * get of a forked task throws exception when task cancelled
1401 >     */
1402 >    public void testCancelledForkGetSingleton() {
1403 >        RecursiveAction a = new CheckedRecursiveAction() {
1404 >            protected void realCompute() throws Exception {
1405 >                AsyncFib f = new AsyncFib(8);
1406 >                assertTrue(f.cancel(true));
1407 >                assertSame(f, f.fork());
1408 >                try {
1409 >                    f.get();
1410 >                    shouldThrow();
1411 >                } catch (CancellationException success) {
1412 >                    checkCancelled(f);
1413 >                }
1414 >            }};
1415 >        testInvokeOnPool(singletonPool(), a);
1416 >    }
1417 >
1418 >    /**
1419 >     * timed get of a forked task throws exception when task cancelled
1420 >     */
1421 >    public void testCancelledForkTimedGetSingleton() throws Exception {
1422 >        RecursiveAction a = new CheckedRecursiveAction() {
1423 >            protected void realCompute() throws Exception {
1424 >                AsyncFib f = new AsyncFib(8);
1425 >                assertTrue(f.cancel(true));
1426 >                assertSame(f, f.fork());
1427 >                try {
1428 >                    f.get(LONG_DELAY_MS, MILLISECONDS);
1429 >                    shouldThrow();
1430 >                } catch (CancellationException success) {
1431 >                    checkCancelled(f);
1432 >                }
1433 >            }};
1434 >        testInvokeOnPool(singletonPool(), a);
1435 >    }
1436 >
1437 >    /**
1438 >     * quietlyJoin of a forked task returns when task cancelled
1439 >     */
1440 >    public void testCancelledForkQuietlyJoinSingleton() {
1441 >        RecursiveAction a = new CheckedRecursiveAction() {
1442 >            protected void realCompute() {
1443 >                AsyncFib f = new AsyncFib(8);
1444 >                assertTrue(f.cancel(true));
1445 >                assertSame(f, f.fork());
1446 >                f.quietlyJoin();
1447 >                checkCancelled(f);
1448 >            }};
1449 >        testInvokeOnPool(singletonPool(), a);
1450 >    }
1451 >
1452 >    /**
1453 >     * invoke task throws exception after invoking completeExceptionally
1454 >     */
1455 >    public void testCompleteExceptionallySingleton() {
1456 >        RecursiveAction a = new CheckedRecursiveAction() {
1457 >            protected void realCompute() {
1458 >                AsyncFib f = new AsyncFib(8);
1459 >                f.completeExceptionally(new FJException());
1460 >                try {
1461 >                    f.invoke();
1462 >                    shouldThrow();
1463 >                } catch (FJException success) {
1464 >                    checkCompletedAbnormally(f, success);
1465 >                }
1466 >            }};
1467 >        testInvokeOnPool(singletonPool(), a);
1468 >    }
1469 >
1470 >    /**
1471 >     * invokeAll(t1, t2) invokes all task arguments
1472 >     */
1473 >    public void testInvokeAll2Singleton() {
1474 >        RecursiveAction a = new CheckedRecursiveAction() {
1475 >            protected void realCompute() {
1476 >                AsyncFib f = new AsyncFib(8);
1477 >                AsyncFib g = new AsyncFib(9);
1478 >                invokeAll(f, g);
1479 >                assertEquals(21, f.number);
1480 >                assertEquals(34, g.number);
1481 >                checkCompletedNormally(f);
1482 >                checkCompletedNormally(g);
1483 >            }};
1484 >        testInvokeOnPool(singletonPool(), a);
1485 >    }
1486 >
1487 >    /**
1488 >     * invokeAll(tasks) with 1 argument invokes task
1489 >     */
1490 >    public void testInvokeAll1Singleton() {
1491 >        RecursiveAction a = new CheckedRecursiveAction() {
1492 >            protected void realCompute() {
1493 >                AsyncFib f = new AsyncFib(8);
1494 >                invokeAll(f);
1495 >                checkCompletedNormally(f);
1496 >                assertEquals(21, f.number);
1497 >            }};
1498 >        testInvokeOnPool(singletonPool(), a);
1499 >    }
1500 >
1501 >    /**
1502 >     * invokeAll(tasks) with > 2 argument invokes tasks
1503 >     */
1504 >    public void testInvokeAll3Singleton() {
1505 >        RecursiveAction a = new CheckedRecursiveAction() {
1506 >            protected void realCompute() {
1507 >                AsyncFib f = new AsyncFib(8);
1508 >                AsyncFib g = new AsyncFib(9);
1509 >                AsyncFib h = new AsyncFib(7);
1510 >                invokeAll(f, g, h);
1511 >                assertEquals(21, f.number);
1512 >                assertEquals(34, g.number);
1513 >                assertEquals(13, h.number);
1514 >                checkCompletedNormally(f);
1515 >                checkCompletedNormally(g);
1516 >                checkCompletedNormally(h);
1517 >            }};
1518 >        testInvokeOnPool(singletonPool(), a);
1519 >    }
1520 >
1521 >    /**
1522 >     * invokeAll(collection) invokes all tasks in the collection
1523 >     */
1524 >    public void testInvokeAllCollectionSingleton() {
1525 >        RecursiveAction a = new CheckedRecursiveAction() {
1526 >            protected void realCompute() {
1527 >                AsyncFib f = new AsyncFib(8);
1528 >                AsyncFib g = new AsyncFib(9);
1529 >                AsyncFib h = new AsyncFib(7);
1530 >                HashSet set = new HashSet();
1531 >                set.add(f);
1532 >                set.add(g);
1533 >                set.add(h);
1534 >                invokeAll(set);
1535 >                assertEquals(21, f.number);
1536 >                assertEquals(34, g.number);
1537 >                assertEquals(13, h.number);
1538 >                checkCompletedNormally(f);
1539 >                checkCompletedNormally(g);
1540 >                checkCompletedNormally(h);
1541 >            }};
1542 >        testInvokeOnPool(singletonPool(), a);
1543 >    }
1544 >
1545 >    /**
1546 >     * invokeAll(tasks) with any null task throws NPE
1547 >     */
1548 >    public void testInvokeAllNPESingleton() {
1549 >        RecursiveAction a = new CheckedRecursiveAction() {
1550 >            protected void realCompute() {
1551 >                AsyncFib f = new AsyncFib(8);
1552 >                AsyncFib g = new AsyncFib(9);
1553 >                AsyncFib h = null;
1554 >                try {
1555 >                    invokeAll(f, g, h);
1556 >                    shouldThrow();
1557 >                } catch (NullPointerException success) {}
1558 >            }};
1559 >        testInvokeOnPool(singletonPool(), a);
1560 >    }
1561 >
1562 >    /**
1563 >     * invokeAll(t1, t2) throw exception if any task does
1564 >     */
1565 >    public void testAbnormalInvokeAll2Singleton() {
1566 >        RecursiveAction a = new CheckedRecursiveAction() {
1567 >            protected void realCompute() {
1568 >                AsyncFib f = new AsyncFib(8);
1569 >                FailingAsyncFib g = new FailingAsyncFib(9);
1570 >                ForkJoinTask[] tasks = { f, g };
1571 >                shuffle(tasks);
1572 >                try {
1573 >                    invokeAll(tasks);
1574 >                    shouldThrow();
1575 >                } catch (FJException success) {
1576 >                    checkCompletedAbnormally(g, success);
1577 >                }
1578 >            }};
1579 >        testInvokeOnPool(singletonPool(), a);
1580 >    }
1581 >
1582 >    /**
1583 >     * invokeAll(tasks) with 1 argument throws exception if task does
1584 >     */
1585 >    public void testAbnormalInvokeAll1Singleton() {
1586 >        RecursiveAction a = new CheckedRecursiveAction() {
1587 >            protected void realCompute() {
1588 >                FailingAsyncFib g = new FailingAsyncFib(9);
1589 >                try {
1590 >                    invokeAll(g);
1591 >                    shouldThrow();
1592 >                } catch (FJException success) {
1593 >                    checkCompletedAbnormally(g, success);
1594 >                }
1595 >            }};
1596 >        testInvokeOnPool(singletonPool(), a);
1597 >    }
1598 >
1599 >    /**
1600 >     * invokeAll(tasks) with > 2 argument throws exception if any task does
1601 >     */
1602 >    public void testAbnormalInvokeAll3Singleton() {
1603 >        RecursiveAction a = new CheckedRecursiveAction() {
1604 >            protected void realCompute() {
1605 >                AsyncFib f = new AsyncFib(8);
1606 >                FailingAsyncFib g = new FailingAsyncFib(9);
1607 >                AsyncFib h = new AsyncFib(7);
1608 >                ForkJoinTask[] tasks = { f, g, h };
1609 >                shuffle(tasks);
1610 >                try {
1611 >                    invokeAll(tasks);
1612 >                    shouldThrow();
1613 >                } catch (FJException success) {
1614 >                    checkCompletedAbnormally(g, success);
1615 >                }
1616 >            }};
1617 >        testInvokeOnPool(singletonPool(), a);
1618 >    }
1619 >
1620 >    /**
1621 >     * invokeAll(collection) throws exception if any task does
1622 >     */
1623 >    public void testAbnormalInvokeAllCollectionSingleton() {
1624 >        RecursiveAction a = new CheckedRecursiveAction() {
1625 >            protected void realCompute() {
1626 >                FailingAsyncFib f = new FailingAsyncFib(8);
1627 >                AsyncFib g = new AsyncFib(9);
1628 >                AsyncFib h = new AsyncFib(7);
1629 >                ForkJoinTask[] tasks = { f, g, h };
1630 >                shuffle(tasks);
1631 >                try {
1632 >                    invokeAll(Arrays.asList(tasks));
1633 >                    shouldThrow();
1634 >                } catch (FJException success) {
1635 >                    checkCompletedAbnormally(f, success);
1636                  }
1637 <            };
1638 <        asyncSingletonPool.invoke(a);
1637 >            }};
1638 >        testInvokeOnPool(singletonPool(), a);
1639 >    }
1640 >
1641 >    /**
1642 >     * ForkJoinTask.quietlyComplete returns when task completes
1643 >     * normally without setting a value. The most recent value
1644 >     * established by setRawResult(V) (or null by default) is returned
1645 >     * from invoke.
1646 >     */
1647 >    public void testQuietlyComplete() {
1648 >        RecursiveAction a = new CheckedRecursiveAction() {
1649 >                protected void realCompute() {
1650 >                    AsyncFib f = new AsyncFib(8);
1651 >                    f.quietlyComplete();
1652 >                    assertEquals(8, f.number);
1653 >                    checkCompletedNormally(f);
1654 >                }};
1655 >        testInvokeOnPool(mainPool(), a);
1656 >    }
1657 >
1658 >    /**
1659 >     * adapt(runnable).toString() contains toString of wrapped task
1660 >     */
1661 >    public void testAdapt_Runnable_toString() {
1662 >        if (testImplementationDetails) {
1663 >            Runnable r = () -> {};
1664 >            ForkJoinTask<?> task = ForkJoinTask.adapt(r);
1665 >            assertEquals(
1666 >                identityString(task) + "[Wrapped task = " + r.toString() + "]",
1667 >                task.toString());
1668 >        }
1669 >    }
1670 >
1671 >    /**
1672 >     * adapt(runnable, x).toString() contains toString of wrapped task
1673 >     */
1674 >    public void testAdapt_Runnable_withResult_toString() {
1675 >        if (testImplementationDetails) {
1676 >            Runnable r = () -> {};
1677 >            ForkJoinTask<String> task = ForkJoinTask.adapt(r, "");
1678 >            assertEquals(
1679 >                identityString(task) + "[Wrapped task = " + r.toString() + "]",
1680 >                task.toString());
1681 >        }
1682 >    }
1683 >
1684 >    /**
1685 >     * adapt(callable).toString() contains toString of wrapped task
1686 >     */
1687 >    public void testAdapt_Callable_toString() {
1688 >        if (testImplementationDetails) {
1689 >            Callable<String> c = () -> "";
1690 >            ForkJoinTask<String> task = ForkJoinTask.adapt(c);
1691 >            assertEquals(
1692 >                identityString(task) + "[Wrapped task = " + c.toString() + "]",
1693 >                task.toString());
1694 >        }
1695      }
1696   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines