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.7 by jsr166, Tue Dec 1 23:01:44 2009 UTC vs.
Revision 1.49 by jsr166, Sun Oct 18 16:40:57 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines