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.1 by dl, Fri Jul 31 23:02:49 2009 UTC vs.
Revision 1.48 by dl, Sun Oct 11 19:53:59 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);
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 <     *
205 >     *
206       * To test extension methods and overrides, most tests use
207       * BinaryAsyncAction extension class that processes joins
208       * differently than supplied Recursive forms.
209 <     */    
27 <
28 <    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 <    }
209 >     */
210  
211 <    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,
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 92 | Line 276 | public class ForkJoinTaskTest extends JS
276                      break;
277                  try {
278                      p.onComplete(a, s);
279 <                } catch(Throwable rex) {
279 >                } catch (Throwable rex) {
280                      p.completeExceptionally(rex);
281                      return;
282                  }
# 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 129 | Line 312 | public class ForkJoinTaskTest extends JS
312              return controlState;
313          }
314  
315 <        protected final boolean compareAndSetControlState(int expect,
315 >        protected final boolean compareAndSetControlState(int expect,
316                                                            int update) {
317              return controlStateUpdater.compareAndSet(this, expect, update);
318          }
# 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) {
336 >        public AsyncFib(int n) {
337              this.number = n;
338          }
339 <        
339 >
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) {
361 >        public FailingAsyncFib(int n) {
362              this.number = n;
363          }
364 <        
364 >
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 205 | Line 381 | public class ForkJoinTaskTest extends JS
381          }
382      }
383  
384 <    /**
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 <    /**
400 >    /**
401       * quietlyInvoke task returns when task completes normally.
402       * isCompletedAbnormally and isCancelled return false for normally
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 <    /**
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 <    /**
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();
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 <    /**
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();
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 <    /**
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 <                    } catch(NullPointerException success) {
472 <                    } catch(Exception ex) {
473 <                        unexpectedException();
474 <                    }
320 <                }
321 <            };
322 <        mainPool.invoke(a);
323 <    }
324 <
325 <    /**
326 <     * helpJoin of a forked task returns when task completes
327 <     */
328 <    public void testForkHelpJoin() {
329 <        RecursiveAction a = new RecursiveAction() {
330 <                public void compute() {
331 <                    AsyncFib f = new AsyncFib(8);
332 <                    f.fork();
333 <                    f.helpJoin();
334 <                    threadAssertTrue(f.number == 21);
335 <                    threadAssertTrue(f.isDone());
336 <                }
337 <            };
338 <        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 <    /**
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 <            };
354 <        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  
492 <
358 <    /**
359 <     * quietlyHelpJoin of a forked task returns when task completes
360 <     */
361 <    public void testForkQuietlyHelpJoin() {
362 <        RecursiveAction a = new RecursiveAction() {
363 <                public void compute() {
364 <                    AsyncFib f = new AsyncFib(8);
365 <                    f.fork();
366 <                    f.quietlyHelpJoin();
367 <                    threadAssertTrue(f.number == 21);
368 <                    threadAssertTrue(f.isDone());
369 <                }
370 <            };
371 <        mainPool.invoke(a);
372 <    }
373 <
374 <
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 <            };
390 <        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  
509 <
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 <     * quietelyInvoke task returns when task completes abnormally
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 <    /**
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 <    /**
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(Exception success) {
570 <                    }
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 <    /**
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(5L, TimeUnit.SECONDS);
588 <                        shouldThrow();
589 <                    } catch(Exception success) {
590 <                    }
591 <                }
592 <            };
476 <        mainPool.invoke(a);
477 <    }
478 <
479 <    /**
480 <     * join of a forked task throws exception when task completes abnormally
481 <     */
482 <    public void testAbnormalForkHelpJoin() {
483 <        RecursiveAction a = new RecursiveAction() {
484 <                public void compute() {
485 <                    try {
486 <                        FailingAsyncFib f = new FailingAsyncFib(8);
487 <                        f.fork();
488 <                        f.helpJoin();
489 <                        shouldThrow();
490 <                    } catch(FJException success) {
491 <                    }
492 <                }
493 <            };
494 <        mainPool.invoke(a);
495 <    }
496 <
497 <    /**
498 <     * quietlyHelpJoin of a forked task returns when task completes abnormally.
499 <     * getException of failed task returns its exception.
500 <     * isCompletedAbnormally of a failed task returns true.
501 <     * isCancelled of a failed uncancelled task returns false
502 <     */
503 <    public void testAbnormalForkQuietlyHelpJoin() {
504 <        RecursiveAction a = new RecursiveAction() {
505 <                public void compute() {
506 <                    FailingAsyncFib f = new FailingAsyncFib(8);
507 <                    f.fork();
508 <                    f.quietlyHelpJoin();
509 <                    threadAssertTrue(f.isDone());
510 <                    threadAssertTrue(f.isCompletedAbnormally());
511 <                    threadAssertFalse(f.isCancelled());
512 <                    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 <    /**
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 <                }
531 <            };
532 <        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 <    /**
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 <    /**
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 <    /**
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(Exception success) {
663 <                    }
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 <    /**
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(5L, TimeUnit.SECONDS);
680 <                        shouldThrow();
681 <                    } catch(Exception success) {
682 <                    }
605 <                }
606 <            };
607 <        mainPool.invoke(a);
608 <    }
609 <
610 <    /**
611 <     * join of a forked task throws exception when task cancelled
612 <     */
613 <    public void testCancelledForkHelpJoin() {
614 <        RecursiveAction a = new RecursiveAction() {
615 <                public void compute() {
616 <                    try {
617 <                        AsyncFib f = new AsyncFib(8);
618 <                        f.cancel(true);
619 <                        f.fork();
620 <                        f.helpJoin();
621 <                        shouldThrow();
622 <                    } catch(CancellationException success) {
623 <                    }
624 <                }
625 <            };
626 <        mainPool.invoke(a);
627 <    }
628 <
629 <    /**
630 <     * quietlyHelpJoin of a forked task returns when task cancelled.
631 <     * getException of cancelled task returns its exception.
632 <     * isCompletedAbnormally of a cancelled task returns true.
633 <     * isCancelled of a cancelled task returns true
634 <     */
635 <    public void testCancelledForkQuietlyHelpJoin() {
636 <        RecursiveAction a = new RecursiveAction() {
637 <                public void compute() {
638 <                    AsyncFib f = new AsyncFib(8);
639 <                    f.cancel(true);
640 <                    f.fork();
641 <                    f.quietlyHelpJoin();
642 <                    threadAssertTrue(f.isDone());
643 <                    threadAssertTrue(f.isCompletedAbnormally());
644 <                    threadAssertTrue(f.isCancelled());
645 <                    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 <    /**
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);
664 <                }
665 <            };
666 <        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 <            };
690 <        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 <            };
702 <        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 <            };
714 <        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 <    /**
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 <        mainPool.invoke(a);
774 >            }};
775 >        testInvokeOnPool(mainPool(), a);
776      }
777  
778 <    /**
778 >    /**
779       * invokeAll(t1, t2) invokes all task arguments
780       */
781      public void testInvokeAll2() {
782 <        RecursiveAction a = new RecursiveAction() {
783 <                public void compute() {
784 <                    AsyncFib f = new AsyncFib(8);
785 <                    AsyncFib g = new AsyncFib(9);
786 <                    invokeAll(f, g);
787 <                    threadAssertTrue(f.isDone());
788 <                    threadAssertTrue(f.number == 21);
789 <                    threadAssertTrue(g.isDone());
790 <                    threadAssertTrue(g.number == 34);
791 <                }
792 <            };
762 <        mainPool.invoke(a);
782 >        RecursiveAction a = new CheckedRecursiveAction() {
783 >            protected void realCompute() {
784 >                AsyncFib f = new AsyncFib(8);
785 >                AsyncFib g = new AsyncFib(9);
786 >                invokeAll(f, g);
787 >                assertEquals(21, f.number);
788 >                assertEquals(34, g.number);
789 >                checkCompletedNormally(f);
790 >                checkCompletedNormally(g);
791 >            }};
792 >        testInvokeOnPool(mainPool(), a);
793      }
794  
795 <    /**
795 >    /**
796       * invokeAll(tasks) with 1 argument invokes task
797       */
798      public void testInvokeAll1() {
799 <        RecursiveAction a = new RecursiveAction() {
800 <                public void compute() {
801 <                    AsyncFib f = new AsyncFib(8);
802 <                    invokeAll(f);
803 <                    threadAssertTrue(f.isDone());
804 <                    threadAssertTrue(f.number == 21);
805 <                }
806 <            };
777 <        mainPool.invoke(a);
799 >        RecursiveAction a = new CheckedRecursiveAction() {
800 >            protected void realCompute() {
801 >                AsyncFib f = new AsyncFib(8);
802 >                invokeAll(f);
803 >                checkCompletedNormally(f);
804 >                assertEquals(21, f.number);
805 >            }};
806 >        testInvokeOnPool(mainPool(), a);
807      }
808  
809 <    /**
809 >    /**
810       * invokeAll(tasks) with > 2 argument invokes tasks
811       */
812      public void testInvokeAll3() {
813 <        RecursiveAction a = new RecursiveAction() {
814 <                public void compute() {
815 <                    AsyncFib f = new AsyncFib(8);
816 <                    AsyncFib g = new AsyncFib(9);
817 <                    AsyncFib h = new AsyncFib(7);
818 <                    invokeAll(f, g, h);
819 <                    threadAssertTrue(f.isDone());
820 <                    threadAssertTrue(f.number == 21);
821 <                    threadAssertTrue(g.isDone());
822 <                    threadAssertTrue(g.number == 34);
823 <                    threadAssertTrue(h.isDone());
824 <                    threadAssertTrue(h.number == 13);
825 <                }
826 <            };
798 <        mainPool.invoke(a);
813 >        RecursiveAction a = new CheckedRecursiveAction() {
814 >            protected void realCompute() {
815 >                AsyncFib f = new AsyncFib(8);
816 >                AsyncFib g = new AsyncFib(9);
817 >                AsyncFib h = new AsyncFib(7);
818 >                invokeAll(f, g, h);
819 >                assertEquals(21, f.number);
820 >                assertEquals(34, g.number);
821 >                assertEquals(13, h.number);
822 >                checkCompletedNormally(f);
823 >                checkCompletedNormally(g);
824 >                checkCompletedNormally(h);
825 >            }};
826 >        testInvokeOnPool(mainPool(), a);
827      }
828  
829 <    /**
829 >    /**
830       * invokeAll(collection) invokes all tasks in the collection
831       */
832      public void testInvokeAllCollection() {
833 <        RecursiveAction a = new RecursiveAction() {
834 <                public void compute() {
835 <                    AsyncFib f = new AsyncFib(8);
836 <                    AsyncFib g = new AsyncFib(9);
837 <                    AsyncFib h = new AsyncFib(7);
838 <                    HashSet set = new HashSet();
839 <                    set.add(f);
840 <                    set.add(g);
841 <                    set.add(h);
842 <                    invokeAll(set);
843 <                    threadAssertTrue(f.isDone());
844 <                    threadAssertTrue(f.number == 21);
845 <                    threadAssertTrue(g.isDone());
846 <                    threadAssertTrue(g.number == 34);
847 <                    threadAssertTrue(h.isDone());
848 <                    threadAssertTrue(h.number == 13);
849 <                }
850 <            };
823 <        mainPool.invoke(a);
833 >        RecursiveAction a = new CheckedRecursiveAction() {
834 >            protected void realCompute() {
835 >                AsyncFib f = new AsyncFib(8);
836 >                AsyncFib g = new AsyncFib(9);
837 >                AsyncFib h = new AsyncFib(7);
838 >                HashSet set = new HashSet();
839 >                set.add(f);
840 >                set.add(g);
841 >                set.add(h);
842 >                invokeAll(set);
843 >                assertEquals(21, f.number);
844 >                assertEquals(34, g.number);
845 >                assertEquals(13, h.number);
846 >                checkCompletedNormally(f);
847 >                checkCompletedNormally(g);
848 >                checkCompletedNormally(h);
849 >            }};
850 >        testInvokeOnPool(mainPool(), a);
851      }
852  
853 <
827 <    /**
853 >    /**
854       * invokeAll(tasks) with any null task throws NPE
855       */
856      public void testInvokeAllNPE() {
857 <        RecursiveAction a = new RecursiveAction() {
858 <                public void compute() {
859 <                    try {
860 <                        AsyncFib f = new AsyncFib(8);
861 <                        AsyncFib g = new AsyncFib(9);
862 <                        AsyncFib h = null;
863 <                        invokeAll(f, g, h);
864 <                        shouldThrow();
865 <                    } catch (NullPointerException success) {
866 <                    }
867 <                }
842 <            };
843 <        mainPool.invoke(a);
857 >        RecursiveAction a = new CheckedRecursiveAction() {
858 >            protected void realCompute() {
859 >                AsyncFib f = new AsyncFib(8);
860 >                AsyncFib g = new AsyncFib(9);
861 >                AsyncFib h = null;
862 >                try {
863 >                    invokeAll(f, g, h);
864 >                    shouldThrow();
865 >                } catch (NullPointerException success) {}
866 >            }};
867 >        testInvokeOnPool(mainPool(), a);
868      }
869  
870 <    /**
870 >    /**
871       * invokeAll(t1, t2) throw exception if any task does
872       */
873      public void testAbnormalInvokeAll2() {
874 <        RecursiveAction a = new RecursiveAction() {
875 <                public void compute() {
876 <                    try {
877 <                        AsyncFib f = new AsyncFib(8);
878 <                        FailingAsyncFib g = new FailingAsyncFib(9);
879 <                        invokeAll(f, g);
880 <                        shouldThrow();
881 <                    } catch(FJException success) {
882 <                    }
874 >        RecursiveAction a = new CheckedRecursiveAction() {
875 >            protected void realCompute() {
876 >                AsyncFib f = new AsyncFib(8);
877 >                FailingAsyncFib g = new FailingAsyncFib(9);
878 >                ForkJoinTask[] tasks = { f, g };
879 >                Collections.shuffle(Arrays.asList(tasks));
880 >                try {
881 >                    invokeAll(tasks);
882 >                    shouldThrow();
883 >                } catch (FJException success) {
884 >                    checkCompletedAbnormally(g, success);
885                  }
886 <            };
887 <        mainPool.invoke(a);
886 >            }};
887 >        testInvokeOnPool(mainPool(), a);
888      }
889  
890 <    /**
890 >    /**
891       * invokeAll(tasks) with 1 argument throws exception if task does
892       */
893      public void testAbnormalInvokeAll1() {
894 <        RecursiveAction a = new RecursiveAction() {
895 <                public void compute() {
896 <                    try {
897 <                        FailingAsyncFib g = new FailingAsyncFib(9);
898 <                        invokeAll(g);
899 <                        shouldThrow();
900 <                    } catch(FJException success) {
901 <                    }
894 >        RecursiveAction a = new CheckedRecursiveAction() {
895 >            protected void realCompute() {
896 >                FailingAsyncFib g = new FailingAsyncFib(9);
897 >                try {
898 >                    invokeAll(g);
899 >                    shouldThrow();
900 >                } catch (FJException success) {
901 >                    checkCompletedAbnormally(g, success);
902                  }
903 <            };
904 <        mainPool.invoke(a);
903 >            }};
904 >        testInvokeOnPool(mainPool(), a);
905      }
906  
907 <    /**
907 >    /**
908       * invokeAll(tasks) with > 2 argument throws exception if any task does
909       */
910      public void testAbnormalInvokeAll3() {
911 <        RecursiveAction a = new RecursiveAction() {
912 <                public void compute() {
913 <                    try {
914 <                        AsyncFib f = new AsyncFib(8);
915 <                        FailingAsyncFib g = new FailingAsyncFib(9);
916 <                        AsyncFib h = new AsyncFib(7);
917 <                        invokeAll(f, g, h);
918 <                        shouldThrow();
919 <                    } catch(FJException success) {
920 <                    }
911 >        RecursiveAction a = new CheckedRecursiveAction() {
912 >            protected void realCompute() {
913 >                AsyncFib f = new AsyncFib(8);
914 >                FailingAsyncFib g = new FailingAsyncFib(9);
915 >                AsyncFib h = new AsyncFib(7);
916 >                ForkJoinTask[] tasks = { f, g, h };
917 >                Collections.shuffle(Arrays.asList(tasks));
918 >                try {
919 >                    invokeAll(tasks);
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(collection)  throws exception if any task does
928 >    /**
929 >     * invokeAll(collection) throws exception if any task does
930       */
931      public void testAbnormalInvokeAllCollection() {
932 <        RecursiveAction a = new RecursiveAction() {
933 <                public void compute() {
934 <                    try {
935 <                        FailingAsyncFib f = new FailingAsyncFib(8);
936 <                        AsyncFib g = new AsyncFib(9);
937 <                        AsyncFib h = new AsyncFib(7);
938 <                        HashSet set = new HashSet();
939 <                        set.add(f);
940 <                        set.add(g);
941 <                        set.add(h);
942 <                        invokeAll(set);
943 <                        shouldThrow();
944 <                    } catch(FJException success) {
917 <                    }
932 >        RecursiveAction a = new CheckedRecursiveAction() {
933 >            protected void realCompute() {
934 >                FailingAsyncFib f = new FailingAsyncFib(8);
935 >                AsyncFib g = new AsyncFib(9);
936 >                AsyncFib h = new AsyncFib(7);
937 >                ForkJoinTask[] tasks = { f, g, h };
938 >                List taskList = Arrays.asList(tasks);
939 >                Collections.shuffle(taskList);
940 >                try {
941 >                    invokeAll(taskList);
942 >                    shouldThrow();
943 >                } catch (FJException success) {
944 >                    checkCompletedAbnormally(f, success);
945                  }
946 <            };
947 <        mainPool.invoke(a);
946 >            }};
947 >        testInvokeOnPool(mainPool(), a);
948      }
949  
950 <    /**
950 >    /**
951       * tryUnfork returns true for most recent unexecuted task,
952       * and suppresses execution
953       */
954      public void testTryUnfork() {
955 <        RecursiveAction a = new RecursiveAction() {
956 <                public void compute() {
957 <                    AsyncFib g = new AsyncFib(9);
958 <                    g.fork();
959 <                    AsyncFib f = new AsyncFib(8);
960 <                    f.fork();
961 <                    threadAssertTrue(f.tryUnfork());
962 <                    helpQuiesce();
963 <                    threadAssertFalse(f.isDone());
964 <                    threadAssertTrue(g.isDone());
965 <                }
966 <            };
940 <        singletonPool.invoke(a);
955 >        RecursiveAction a = new CheckedRecursiveAction() {
956 >            protected void realCompute() {
957 >                AsyncFib g = new AsyncFib(9);
958 >                assertSame(g, g.fork());
959 >                AsyncFib f = new AsyncFib(8);
960 >                assertSame(f, f.fork());
961 >                assertTrue(f.tryUnfork());
962 >                helpQuiesce();
963 >                checkNotDone(f);
964 >                checkCompletedNormally(g);
965 >            }};
966 >        testInvokeOnPool(singletonPool(), a);
967      }
968  
969 <    /**
969 >    /**
970       * getSurplusQueuedTaskCount returns > 0 when
971       * there are more tasks than threads
972       */
973      public void testGetSurplusQueuedTaskCount() {
974 <        RecursiveAction a = new RecursiveAction() {
975 <                public void compute() {
976 <                    AsyncFib h = new AsyncFib(7);
977 <                    h.fork();
978 <                    AsyncFib g = new AsyncFib(9);
979 <                    g.fork();
980 <                    AsyncFib f = new AsyncFib(8);
981 <                    f.fork();
982 <                    threadAssertTrue(getSurplusQueuedTaskCount() > 0);
983 <                    helpQuiesce();
984 <                }
985 <            };
986 <        singletonPool.invoke(a);
974 >        RecursiveAction a = new CheckedRecursiveAction() {
975 >            protected void realCompute() {
976 >                AsyncFib h = new AsyncFib(7);
977 >                assertSame(h, h.fork());
978 >                AsyncFib g = new AsyncFib(9);
979 >                assertSame(g, g.fork());
980 >                AsyncFib f = new AsyncFib(8);
981 >                assertSame(f, f.fork());
982 >                assertTrue(getSurplusQueuedTaskCount() > 0);
983 >                helpQuiesce();
984 >                assertEquals(0, getSurplusQueuedTaskCount());
985 >                checkCompletedNormally(f);
986 >                checkCompletedNormally(g);
987 >                checkCompletedNormally(h);
988 >            }};
989 >        testInvokeOnPool(singletonPool(), a);
990      }
991  
992 <    /**
992 >    /**
993       * peekNextLocalTask returns most recent unexecuted task.
994       */
995      public void testPeekNextLocalTask() {
996 <        RecursiveAction a = new RecursiveAction() {
997 <                public void compute() {
998 <                    AsyncFib g = new AsyncFib(9);
999 <                    g.fork();
1000 <                    AsyncFib f = new AsyncFib(8);
1001 <                    f.fork();
1002 <                    threadAssertTrue(peekNextLocalTask() == f);
1003 <                    f.join();
1004 <                    threadAssertTrue(f.isDone());
1005 <                    helpQuiesce();
1006 <                }
1007 <            };
1008 <        singletonPool.invoke(a);
996 >        RecursiveAction a = new CheckedRecursiveAction() {
997 >            protected void realCompute() {
998 >                AsyncFib g = new AsyncFib(9);
999 >                assertSame(g, g.fork());
1000 >                AsyncFib f = new AsyncFib(8);
1001 >                assertSame(f, f.fork());
1002 >                assertSame(f, peekNextLocalTask());
1003 >                assertNull(f.join());
1004 >                checkCompletedNormally(f);
1005 >                helpQuiesce();
1006 >                checkCompletedNormally(g);
1007 >            }};
1008 >        testInvokeOnPool(singletonPool(), a);
1009      }
1010  
1011 <    /**
1012 <     * pollNextLocalTask returns most recent unexecuted task
1013 <     * without executing it
1011 >    /**
1012 >     * pollNextLocalTask returns most recent unexecuted task without
1013 >     * executing it
1014       */
1015      public void testPollNextLocalTask() {
1016 <        RecursiveAction a = new RecursiveAction() {
1017 <                public void compute() {
1018 <                    AsyncFib g = new AsyncFib(9);
1019 <                    g.fork();
1020 <                    AsyncFib f = new AsyncFib(8);
1021 <                    f.fork();
1022 <                    threadAssertTrue(pollNextLocalTask() == f);
1023 <                    helpQuiesce();
1024 <                    threadAssertFalse(f.isDone());
1025 <                }
1026 <            };
1027 <        singletonPool.invoke(a);
1016 >        RecursiveAction a = new CheckedRecursiveAction() {
1017 >            protected void realCompute() {
1018 >                AsyncFib g = new AsyncFib(9);
1019 >                assertSame(g, g.fork());
1020 >                AsyncFib f = new AsyncFib(8);
1021 >                assertSame(f, f.fork());
1022 >                assertSame(f, pollNextLocalTask());
1023 >                helpQuiesce();
1024 >                checkNotDone(f);
1025 >                assertEquals(34, g.number);
1026 >                checkCompletedNormally(g);
1027 >            }};
1028 >        testInvokeOnPool(singletonPool(), a);
1029      }
1030  
1031 <    /**
1032 <     * pollTask returns an unexecuted task
1003 <     * without executing it
1031 >    /**
1032 >     * pollTask returns an unexecuted task without executing it
1033       */
1034      public void testPollTask() {
1035 <        RecursiveAction a = new RecursiveAction() {
1036 <                public void compute() {
1037 <                    AsyncFib g = new AsyncFib(9);
1038 <                    g.fork();
1039 <                    AsyncFib f = new AsyncFib(8);
1040 <                    f.fork();
1041 <                    threadAssertTrue(pollTask() == f);
1042 <                    helpQuiesce();
1043 <                    threadAssertFalse(f.isDone());
1044 <                    threadAssertTrue(g.isDone());
1045 <                }
1046 <            };
1018 <        singletonPool.invoke(a);
1035 >        RecursiveAction a = new CheckedRecursiveAction() {
1036 >            protected void realCompute() {
1037 >                AsyncFib g = new AsyncFib(9);
1038 >                assertSame(g, g.fork());
1039 >                AsyncFib f = new AsyncFib(8);
1040 >                assertSame(f, f.fork());
1041 >                assertSame(f, pollTask());
1042 >                helpQuiesce();
1043 >                checkNotDone(f);
1044 >                checkCompletedNormally(g);
1045 >            }};
1046 >        testInvokeOnPool(singletonPool(), a);
1047      }
1048  
1049 <    /**
1049 >    /**
1050       * peekNextLocalTask returns least recent unexecuted task in async mode
1051       */
1052      public void testPeekNextLocalTaskAsync() {
1053 <        RecursiveAction a = new RecursiveAction() {
1054 <                public void compute() {
1055 <                    AsyncFib g = new AsyncFib(9);
1056 <                    g.fork();
1057 <                    AsyncFib f = new AsyncFib(8);
1058 <                    f.fork();
1059 <                    threadAssertTrue(peekNextLocalTask() == g);
1060 <                    f.join();
1061 <                    helpQuiesce();
1062 <                    threadAssertTrue(f.isDone());
1063 <                }
1064 <            };
1065 <        asyncSingletonPool.invoke(a);
1053 >        RecursiveAction a = new CheckedRecursiveAction() {
1054 >            protected void realCompute() {
1055 >                AsyncFib g = new AsyncFib(9);
1056 >                assertSame(g, g.fork());
1057 >                AsyncFib f = new AsyncFib(8);
1058 >                assertSame(f, f.fork());
1059 >                assertSame(g, peekNextLocalTask());
1060 >                assertNull(f.join());
1061 >                helpQuiesce();
1062 >                checkCompletedNormally(f);
1063 >                assertEquals(34, g.number);
1064 >                checkCompletedNormally(g);
1065 >            }};
1066 >        testInvokeOnPool(asyncSingletonPool(), a);
1067      }
1068  
1069 <    /**
1070 <     * pollNextLocalTask returns least recent unexecuted task
1071 <     * without executing it, in async mode
1069 >    /**
1070 >     * pollNextLocalTask returns least recent unexecuted task without
1071 >     * executing it, in async mode
1072       */
1073      public void testPollNextLocalTaskAsync() {
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(pollNextLocalTask() == g);
1081 <                    helpQuiesce();
1082 <                    threadAssertTrue(f.isDone());
1083 <                    threadAssertFalse(g.isDone());
1084 <                }
1085 <            };
1086 <        asyncSingletonPool.invoke(a);
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, pollNextLocalTask());
1081 >                helpQuiesce();
1082 >                assertEquals(21, f.number);
1083 >                checkCompletedNormally(f);
1084 >                checkNotDone(g);
1085 >            }};
1086 >        testInvokeOnPool(asyncSingletonPool(), a);
1087      }
1088  
1089 <    /**
1090 <     * pollTask returns an unexecuted task
1091 <     * without executing it, in async mode
1089 >    /**
1090 >     * pollTask returns an unexecuted task without executing it, in
1091 >     * async mode
1092       */
1093      public void testPollTaskAsync() {
1094 <        RecursiveAction a = new RecursiveAction() {
1095 <                public void compute() {
1096 <                    AsyncFib g = new AsyncFib(9);
1097 <                    g.fork();
1098 <                    AsyncFib f = new AsyncFib(8);
1099 <                    f.fork();
1100 <                    threadAssertTrue(pollTask() == g);
1101 <                    helpQuiesce();
1102 <                    threadAssertTrue(f.isDone());
1103 <                    threadAssertFalse(g.isDone());
1094 >        RecursiveAction a = new CheckedRecursiveAction() {
1095 >            protected void realCompute() {
1096 >                AsyncFib g = new AsyncFib(9);
1097 >                assertSame(g, g.fork());
1098 >                AsyncFib f = new AsyncFib(8);
1099 >                assertSame(f, f.fork());
1100 >                assertSame(g, pollTask());
1101 >                helpQuiesce();
1102 >                assertEquals(21, f.number);
1103 >                checkCompletedNormally(f);
1104 >                checkNotDone(g);
1105 >            }};
1106 >        testInvokeOnPool(asyncSingletonPool(), a);
1107 >    }
1108 >
1109 >    // versions for singleton pools
1110 >
1111 >    /**
1112 >     * invoke returns when task completes normally.
1113 >     * isCompletedAbnormally and isCancelled return false for normally
1114 >     * completed tasks; getRawResult returns null.
1115 >     */
1116 >    public void testInvokeSingleton() {
1117 >        RecursiveAction a = new CheckedRecursiveAction() {
1118 >            protected void realCompute() {
1119 >                AsyncFib f = new AsyncFib(8);
1120 >                assertNull(f.invoke());
1121 >                assertEquals(21, f.number);
1122 >                checkCompletedNormally(f);
1123 >            }};
1124 >        testInvokeOnPool(singletonPool(), a);
1125 >    }
1126 >
1127 >    /**
1128 >     * quietlyInvoke task returns when task completes normally.
1129 >     * isCompletedAbnormally and isCancelled return false for normally
1130 >     * completed tasks
1131 >     */
1132 >    public void testQuietlyInvokeSingleton() {
1133 >        RecursiveAction a = new CheckedRecursiveAction() {
1134 >            protected void realCompute() {
1135 >                AsyncFib f = new AsyncFib(8);
1136 >                f.quietlyInvoke();
1137 >                assertEquals(21, f.number);
1138 >                checkCompletedNormally(f);
1139 >            }};
1140 >        testInvokeOnPool(singletonPool(), a);
1141 >    }
1142 >
1143 >    /**
1144 >     * join of a forked task returns when task completes
1145 >     */
1146 >    public void testForkJoinSingleton() {
1147 >        RecursiveAction a = new CheckedRecursiveAction() {
1148 >            protected void realCompute() {
1149 >                AsyncFib f = new AsyncFib(8);
1150 >                assertSame(f, f.fork());
1151 >                assertNull(f.join());
1152 >                assertEquals(21, f.number);
1153 >                checkCompletedNormally(f);
1154 >            }};
1155 >        testInvokeOnPool(singletonPool(), a);
1156 >    }
1157 >
1158 >    /**
1159 >     * get of a forked task returns when task completes
1160 >     */
1161 >    public void testForkGetSingleton() {
1162 >        RecursiveAction a = new CheckedRecursiveAction() {
1163 >            protected void realCompute() throws Exception {
1164 >                AsyncFib f = new AsyncFib(8);
1165 >                assertSame(f, f.fork());
1166 >                assertNull(f.get());
1167 >                assertEquals(21, f.number);
1168 >                checkCompletedNormally(f);
1169 >            }};
1170 >        testInvokeOnPool(singletonPool(), a);
1171 >    }
1172 >
1173 >    /**
1174 >     * timed get of a forked task returns when task completes
1175 >     */
1176 >    public void testForkTimedGetSingleton() {
1177 >        RecursiveAction a = new CheckedRecursiveAction() {
1178 >            protected void realCompute() throws Exception {
1179 >                AsyncFib f = new AsyncFib(8);
1180 >                assertSame(f, f.fork());
1181 >                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1182 >                assertEquals(21, f.number);
1183 >                checkCompletedNormally(f);
1184 >            }};
1185 >        testInvokeOnPool(singletonPool(), a);
1186 >    }
1187 >
1188 >    /**
1189 >     * timed get with null time unit throws NPE
1190 >     */
1191 >    public void testForkTimedGetNPESingleton() {
1192 >        RecursiveAction a = new CheckedRecursiveAction() {
1193 >            protected void realCompute() throws Exception {
1194 >                AsyncFib f = new AsyncFib(8);
1195 >                assertSame(f, f.fork());
1196 >                try {
1197 >                    f.get(5L, null);
1198 >                    shouldThrow();
1199 >                } catch (NullPointerException success) {}
1200 >            }};
1201 >        testInvokeOnPool(singletonPool(), a);
1202 >    }
1203 >
1204 >    /**
1205 >     * quietlyJoin of a forked task returns when task completes
1206 >     */
1207 >    public void testForkQuietlyJoinSingleton() {
1208 >        RecursiveAction a = new CheckedRecursiveAction() {
1209 >            protected void realCompute() {
1210 >                AsyncFib f = new AsyncFib(8);
1211 >                assertSame(f, f.fork());
1212 >                f.quietlyJoin();
1213 >                assertEquals(21, f.number);
1214 >                checkCompletedNormally(f);
1215 >            }};
1216 >        testInvokeOnPool(singletonPool(), a);
1217 >    }
1218 >
1219 >    /**
1220 >     * helpQuiesce returns when tasks are complete.
1221 >     * getQueuedTaskCount returns 0 when quiescent
1222 >     */
1223 >    public void testForkHelpQuiesceSingleton() {
1224 >        RecursiveAction a = new CheckedRecursiveAction() {
1225 >            protected void realCompute() {
1226 >                AsyncFib f = new AsyncFib(8);
1227 >                assertSame(f, f.fork());
1228 >                helpQuiesce();
1229 >                assertEquals(0, getQueuedTaskCount());
1230 >                assertEquals(21, f.number);
1231 >                checkCompletedNormally(f);
1232 >            }};
1233 >        testInvokeOnPool(singletonPool(), a);
1234 >    }
1235 >
1236 >    /**
1237 >     * invoke task throws exception when task completes abnormally
1238 >     */
1239 >    public void testAbnormalInvokeSingleton() {
1240 >        RecursiveAction a = new CheckedRecursiveAction() {
1241 >            protected void realCompute() {
1242 >                FailingAsyncFib f = new FailingAsyncFib(8);
1243 >                try {
1244 >                    f.invoke();
1245 >                    shouldThrow();
1246 >                } catch (FJException success) {
1247 >                    checkCompletedAbnormally(f, success);
1248 >                }
1249 >            }};
1250 >        testInvokeOnPool(singletonPool(), a);
1251 >    }
1252 >
1253 >    /**
1254 >     * quietlyInvoke task returns when task completes abnormally
1255 >     */
1256 >    public void testAbnormalQuietlyInvokeSingleton() {
1257 >        RecursiveAction a = new CheckedRecursiveAction() {
1258 >            protected void realCompute() {
1259 >                FailingAsyncFib f = new FailingAsyncFib(8);
1260 >                f.quietlyInvoke();
1261 >                assertTrue(f.getException() instanceof FJException);
1262 >                checkCompletedAbnormally(f, f.getException());
1263 >            }};
1264 >        testInvokeOnPool(singletonPool(), a);
1265 >    }
1266 >
1267 >    /**
1268 >     * join of a forked task throws exception when task completes abnormally
1269 >     */
1270 >    public void testAbnormalForkJoinSingleton() {
1271 >        RecursiveAction a = new CheckedRecursiveAction() {
1272 >            protected void realCompute() {
1273 >                FailingAsyncFib f = new FailingAsyncFib(8);
1274 >                assertSame(f, f.fork());
1275 >                try {
1276 >                    f.join();
1277 >                    shouldThrow();
1278 >                } catch (FJException success) {
1279 >                    checkCompletedAbnormally(f, success);
1280 >                }
1281 >            }};
1282 >        testInvokeOnPool(singletonPool(), a);
1283 >    }
1284 >
1285 >    /**
1286 >     * get of a forked task throws exception when task completes abnormally
1287 >     */
1288 >    public void testAbnormalForkGetSingleton() {
1289 >        RecursiveAction a = new CheckedRecursiveAction() {
1290 >            protected void realCompute() throws Exception {
1291 >                FailingAsyncFib f = new FailingAsyncFib(8);
1292 >                assertSame(f, f.fork());
1293 >                try {
1294 >                    f.get();
1295 >                    shouldThrow();
1296 >                } catch (ExecutionException success) {
1297 >                    Throwable cause = success.getCause();
1298 >                    assertTrue(cause instanceof FJException);
1299 >                    checkCompletedAbnormally(f, cause);
1300 >                }
1301 >            }};
1302 >        testInvokeOnPool(singletonPool(), a);
1303 >    }
1304 >
1305 >    /**
1306 >     * timed get of a forked task throws exception when task completes abnormally
1307 >     */
1308 >    public void testAbnormalForkTimedGetSingleton() {
1309 >        RecursiveAction a = new CheckedRecursiveAction() {
1310 >            protected void realCompute() throws Exception {
1311 >                FailingAsyncFib f = new FailingAsyncFib(8);
1312 >                assertSame(f, f.fork());
1313 >                try {
1314 >                    f.get(LONG_DELAY_MS, MILLISECONDS);
1315 >                    shouldThrow();
1316 >                } catch (ExecutionException success) {
1317 >                    Throwable cause = success.getCause();
1318 >                    assertTrue(cause instanceof FJException);
1319 >                    checkCompletedAbnormally(f, cause);
1320 >                }
1321 >            }};
1322 >        testInvokeOnPool(singletonPool(), a);
1323 >    }
1324 >
1325 >    /**
1326 >     * quietlyJoin of a forked task returns when task completes abnormally
1327 >     */
1328 >    public void testAbnormalForkQuietlyJoinSingleton() {
1329 >        RecursiveAction a = new CheckedRecursiveAction() {
1330 >            protected void realCompute() {
1331 >                FailingAsyncFib f = new FailingAsyncFib(8);
1332 >                assertSame(f, f.fork());
1333 >                f.quietlyJoin();
1334 >                assertTrue(f.getException() instanceof FJException);
1335 >                checkCompletedAbnormally(f, f.getException());
1336 >            }};
1337 >        testInvokeOnPool(singletonPool(), a);
1338 >    }
1339 >
1340 >    /**
1341 >     * invoke task throws exception when task cancelled
1342 >     */
1343 >    public void testCancelledInvokeSingleton() {
1344 >        RecursiveAction a = new CheckedRecursiveAction() {
1345 >            protected void realCompute() {
1346 >                AsyncFib f = new AsyncFib(8);
1347 >                assertTrue(f.cancel(true));
1348 >                try {
1349 >                    f.invoke();
1350 >                    shouldThrow();
1351 >                } catch (CancellationException success) {
1352 >                    checkCancelled(f);
1353 >                }
1354 >            }};
1355 >        testInvokeOnPool(singletonPool(), a);
1356 >    }
1357 >
1358 >    /**
1359 >     * join of a forked task throws exception when task cancelled
1360 >     */
1361 >    public void testCancelledForkJoinSingleton() {
1362 >        RecursiveAction a = new CheckedRecursiveAction() {
1363 >            protected void realCompute() {
1364 >                AsyncFib f = new AsyncFib(8);
1365 >                assertTrue(f.cancel(true));
1366 >                assertSame(f, f.fork());
1367 >                try {
1368 >                    f.join();
1369 >                    shouldThrow();
1370 >                } catch (CancellationException success) {
1371 >                    checkCancelled(f);
1372 >                }
1373 >            }};
1374 >        testInvokeOnPool(singletonPool(), a);
1375 >    }
1376 >
1377 >    /**
1378 >     * get of a forked task throws exception when task cancelled
1379 >     */
1380 >    public void testCancelledForkGetSingleton() {
1381 >        RecursiveAction a = new CheckedRecursiveAction() {
1382 >            protected void realCompute() throws Exception {
1383 >                AsyncFib f = new AsyncFib(8);
1384 >                assertTrue(f.cancel(true));
1385 >                assertSame(f, f.fork());
1386 >                try {
1387 >                    f.get();
1388 >                    shouldThrow();
1389 >                } catch (CancellationException success) {
1390 >                    checkCancelled(f);
1391 >                }
1392 >            }};
1393 >        testInvokeOnPool(singletonPool(), a);
1394 >    }
1395 >
1396 >    /**
1397 >     * timed get of a forked task throws exception when task cancelled
1398 >     */
1399 >    public void testCancelledForkTimedGetSingleton() throws Exception {
1400 >        RecursiveAction a = new CheckedRecursiveAction() {
1401 >            protected void realCompute() throws Exception {
1402 >                AsyncFib f = new AsyncFib(8);
1403 >                assertTrue(f.cancel(true));
1404 >                assertSame(f, f.fork());
1405 >                try {
1406 >                    f.get(LONG_DELAY_MS, MILLISECONDS);
1407 >                    shouldThrow();
1408 >                } catch (CancellationException success) {
1409 >                    checkCancelled(f);
1410 >                }
1411 >            }};
1412 >        testInvokeOnPool(singletonPool(), a);
1413 >    }
1414 >
1415 >    /**
1416 >     * quietlyJoin of a forked task returns when task cancelled
1417 >     */
1418 >    public void testCancelledForkQuietlyJoinSingleton() {
1419 >        RecursiveAction a = new CheckedRecursiveAction() {
1420 >            protected void realCompute() {
1421 >                AsyncFib f = new AsyncFib(8);
1422 >                assertTrue(f.cancel(true));
1423 >                assertSame(f, f.fork());
1424 >                f.quietlyJoin();
1425 >                checkCancelled(f);
1426 >            }};
1427 >        testInvokeOnPool(singletonPool(), a);
1428 >    }
1429 >
1430 >    /**
1431 >     * invoke task throws exception after invoking completeExceptionally
1432 >     */
1433 >    public void testCompleteExceptionallySingleton() {
1434 >        RecursiveAction a = new CheckedRecursiveAction() {
1435 >            protected void realCompute() {
1436 >                AsyncFib f = new AsyncFib(8);
1437 >                f.completeExceptionally(new FJException());
1438 >                try {
1439 >                    f.invoke();
1440 >                    shouldThrow();
1441 >                } catch (FJException success) {
1442 >                    checkCompletedAbnormally(f, success);
1443                  }
1444 <            };
1445 <        asyncSingletonPool.invoke(a);
1444 >            }};
1445 >        testInvokeOnPool(singletonPool(), a);
1446      }
1447 +
1448 +    /**
1449 +     * invokeAll(t1, t2) invokes all task arguments
1450 +     */
1451 +    public void testInvokeAll2Singleton() {
1452 +        RecursiveAction a = new CheckedRecursiveAction() {
1453 +            protected void realCompute() {
1454 +                AsyncFib f = new AsyncFib(8);
1455 +                AsyncFib g = new AsyncFib(9);
1456 +                invokeAll(f, g);
1457 +                assertEquals(21, f.number);
1458 +                assertEquals(34, g.number);
1459 +                checkCompletedNormally(f);
1460 +                checkCompletedNormally(g);
1461 +            }};
1462 +        testInvokeOnPool(singletonPool(), a);
1463 +    }
1464 +
1465 +    /**
1466 +     * invokeAll(tasks) with 1 argument invokes task
1467 +     */
1468 +    public void testInvokeAll1Singleton() {
1469 +        RecursiveAction a = new CheckedRecursiveAction() {
1470 +            protected void realCompute() {
1471 +                AsyncFib f = new AsyncFib(8);
1472 +                invokeAll(f);
1473 +                checkCompletedNormally(f);
1474 +                assertEquals(21, f.number);
1475 +            }};
1476 +        testInvokeOnPool(singletonPool(), a);
1477 +    }
1478 +
1479 +    /**
1480 +     * invokeAll(tasks) with > 2 argument invokes tasks
1481 +     */
1482 +    public void testInvokeAll3Singleton() {
1483 +        RecursiveAction a = new CheckedRecursiveAction() {
1484 +            protected void realCompute() {
1485 +                AsyncFib f = new AsyncFib(8);
1486 +                AsyncFib g = new AsyncFib(9);
1487 +                AsyncFib h = new AsyncFib(7);
1488 +                invokeAll(f, g, h);
1489 +                assertEquals(21, f.number);
1490 +                assertEquals(34, g.number);
1491 +                assertEquals(13, h.number);
1492 +                checkCompletedNormally(f);
1493 +                checkCompletedNormally(g);
1494 +                checkCompletedNormally(h);
1495 +            }};
1496 +        testInvokeOnPool(singletonPool(), a);
1497 +    }
1498 +
1499 +    /**
1500 +     * invokeAll(collection) invokes all tasks in the collection
1501 +     */
1502 +    public void testInvokeAllCollectionSingleton() {
1503 +        RecursiveAction a = new CheckedRecursiveAction() {
1504 +            protected void realCompute() {
1505 +                AsyncFib f = new AsyncFib(8);
1506 +                AsyncFib g = new AsyncFib(9);
1507 +                AsyncFib h = new AsyncFib(7);
1508 +                HashSet set = new HashSet();
1509 +                set.add(f);
1510 +                set.add(g);
1511 +                set.add(h);
1512 +                invokeAll(set);
1513 +                assertEquals(21, f.number);
1514 +                assertEquals(34, g.number);
1515 +                assertEquals(13, h.number);
1516 +                checkCompletedNormally(f);
1517 +                checkCompletedNormally(g);
1518 +                checkCompletedNormally(h);
1519 +            }};
1520 +        testInvokeOnPool(singletonPool(), a);
1521 +    }
1522 +
1523 +    /**
1524 +     * invokeAll(tasks) with any null task throws NPE
1525 +     */
1526 +    public void testInvokeAllNPESingleton() {
1527 +        RecursiveAction a = new CheckedRecursiveAction() {
1528 +            protected void realCompute() {
1529 +                AsyncFib f = new AsyncFib(8);
1530 +                AsyncFib g = new AsyncFib(9);
1531 +                AsyncFib h = null;
1532 +                try {
1533 +                    invokeAll(f, g, h);
1534 +                    shouldThrow();
1535 +                } catch (NullPointerException success) {}
1536 +            }};
1537 +        testInvokeOnPool(singletonPool(), a);
1538 +    }
1539 +
1540 +    /**
1541 +     * invokeAll(t1, t2) throw exception if any task does
1542 +     */
1543 +    public void testAbnormalInvokeAll2Singleton() {
1544 +        RecursiveAction a = new CheckedRecursiveAction() {
1545 +            protected void realCompute() {
1546 +                AsyncFib f = new AsyncFib(8);
1547 +                FailingAsyncFib g = new FailingAsyncFib(9);
1548 +                ForkJoinTask[] tasks = { f, g };
1549 +                Collections.shuffle(Arrays.asList(tasks));
1550 +                try {
1551 +                    invokeAll(tasks);
1552 +                    shouldThrow();
1553 +                } catch (FJException success) {
1554 +                    checkCompletedAbnormally(g, success);
1555 +                }
1556 +            }};
1557 +        testInvokeOnPool(singletonPool(), a);
1558 +    }
1559 +
1560 +    /**
1561 +     * invokeAll(tasks) with 1 argument throws exception if task does
1562 +     */
1563 +    public void testAbnormalInvokeAll1Singleton() {
1564 +        RecursiveAction a = new CheckedRecursiveAction() {
1565 +            protected void realCompute() {
1566 +                FailingAsyncFib g = new FailingAsyncFib(9);
1567 +                try {
1568 +                    invokeAll(g);
1569 +                    shouldThrow();
1570 +                } catch (FJException success) {
1571 +                    checkCompletedAbnormally(g, success);
1572 +                }
1573 +            }};
1574 +        testInvokeOnPool(singletonPool(), a);
1575 +    }
1576 +
1577 +    /**
1578 +     * invokeAll(tasks) with > 2 argument throws exception if any task does
1579 +     */
1580 +    public void testAbnormalInvokeAll3Singleton() {
1581 +        RecursiveAction a = new CheckedRecursiveAction() {
1582 +            protected void realCompute() {
1583 +                AsyncFib f = new AsyncFib(8);
1584 +                FailingAsyncFib g = new FailingAsyncFib(9);
1585 +                AsyncFib h = new AsyncFib(7);
1586 +                ForkJoinTask[] tasks = { f, g, h };
1587 +                Collections.shuffle(Arrays.asList(tasks));
1588 +                try {
1589 +                    invokeAll(tasks);
1590 +                    shouldThrow();
1591 +                } catch (FJException success) {
1592 +                    checkCompletedAbnormally(g, success);
1593 +                }
1594 +            }};
1595 +        testInvokeOnPool(singletonPool(), a);
1596 +    }
1597 +
1598 +    /**
1599 +     * invokeAll(collection) throws exception if any task does
1600 +     */
1601 +    public void testAbnormalInvokeAllCollectionSingleton() {
1602 +        RecursiveAction a = new CheckedRecursiveAction() {
1603 +            protected void realCompute() {
1604 +                FailingAsyncFib f = new FailingAsyncFib(8);
1605 +                AsyncFib g = new AsyncFib(9);
1606 +                AsyncFib h = new AsyncFib(7);
1607 +                ForkJoinTask[] tasks = { f, g, h };
1608 +                List taskList = Arrays.asList(tasks);
1609 +                Collections.shuffle(taskList);
1610 +                try {
1611 +                    invokeAll(taskList);
1612 +                    shouldThrow();
1613 +                } catch (FJException success) {
1614 +                    checkCompletedAbnormally(f, success);
1615 +                }
1616 +            }};
1617 +        testInvokeOnPool(singletonPool(), a);
1618 +    }
1619 +
1620 +    /**
1621 +     * ForkJoinTask.quietlyComplete returns when task completes
1622 +     * normally without setting a value. The most recent value
1623 +     * established by setRawResult(V) (or null by default) is returned
1624 +     * from invoke.
1625 +     */
1626 +    public void testQuietlyComplete() {
1627 +        RecursiveAction a = new CheckedRecursiveAction() {
1628 +                protected void realCompute() {
1629 +                    AsyncFib f = new AsyncFib(8);
1630 +                    f.quietlyComplete();
1631 +                    assertEquals(8, f.number);
1632 +                    checkCompletedNormally(f);
1633 +                }};
1634 +        testInvokeOnPool(mainPool(), a);
1635 +    }
1636 +
1637   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines