ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveActionTest.java
(Generate patch)

Comparing jsr166/src/test/tck/RecursiveActionTest.java (file contents):
Revision 1.1 by dl, Fri Jul 31 23:02:50 2009 UTC vs.
Revision 1.26 by jsr166, Tue Nov 23 07:38:40 2010 UTC

# Line 3 | Line 3
3   * Expert Group and released to the public domain, as explained at
4   * http://creativecommons.org/licenses/publicdomain
5   */
6 import junit.framework.*;
7 import java.util.concurrent.*;
8 import java.util.*;
6  
7 + import junit.framework.*;
8 + import java.util.concurrent.CancellationException;
9 + import java.util.concurrent.SynchronousQueue;
10 + import java.util.concurrent.ExecutionException;
11 + import java.util.concurrent.ForkJoinPool;
12 + import java.util.concurrent.ForkJoinWorkerThread;
13 + import java.util.concurrent.RecursiveAction;
14 + import java.util.concurrent.TimeUnit;
15 + import java.util.concurrent.TimeoutException;
16 + import static java.util.concurrent.TimeUnit.SECONDS;
17 + import java.util.HashSet;
18  
19   public class RecursiveActionTest extends JSR166TestCase {
20  
21      public static void main(String[] args) {
22 <        junit.textui.TestRunner.run (suite());  
22 >        junit.textui.TestRunner.run(suite());
23      }
24 +
25      public static Test suite() {
26 <        return new TestSuite(RecursiveActionTest.class);
26 >        return new TestSuite(RecursiveActionTest.class);
27 >    }
28 >
29 >    private static ForkJoinPool mainPool() {
30 >        return new ForkJoinPool();
31 >    }
32 >
33 >    private static ForkJoinPool singletonPool() {
34 >        return new ForkJoinPool(1);
35 >    }
36 >
37 >    private static ForkJoinPool asyncSingletonPool() {
38 >        return new ForkJoinPool(1,
39 >                                ForkJoinPool.defaultForkJoinWorkerThreadFactory,
40 >                                null, true);
41      }
42  
43 <    static final ForkJoinPool mainPool = new ForkJoinPool();
44 <    static final ForkJoinPool singletonPool = new ForkJoinPool(1);
45 <    static final ForkJoinPool asyncSingletonPool = new ForkJoinPool(1);
46 <    static {
47 <        asyncSingletonPool.setAsyncMode(true);
43 >    private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
44 >        try {
45 >            checkNotDone(a);
46 >
47 >            assertNull(pool.invoke(a));
48 >
49 >            checkCompletedNormally(a);
50 >        } finally {
51 >            joinPool(pool);
52 >        }
53 >    }
54 >
55 >    void checkNotDone(RecursiveAction a) {
56 >        assertFalse(a.isDone());
57 >        assertFalse(a.isCompletedNormally());
58 >        assertFalse(a.isCompletedAbnormally());
59 >        assertFalse(a.isCancelled());
60 >        assertNull(a.getException());
61 >        assertNull(a.getRawResult());
62 >
63 >        if (! (Thread.currentThread() instanceof ForkJoinWorkerThread)) {
64 >            Thread.currentThread().interrupt();
65 >            try {
66 >                a.get();
67 >                shouldThrow();
68 >            } catch (InterruptedException success) {
69 >            } catch (Throwable fail) { threadUnexpectedException(fail); }
70 >
71 >            Thread.currentThread().interrupt();
72 >            try {
73 >                a.get(5L, SECONDS);
74 >                shouldThrow();
75 >            } catch (InterruptedException success) {
76 >            } catch (Throwable fail) { threadUnexpectedException(fail); }
77 >        }
78 >
79 >        try {
80 >            a.get(0L, SECONDS);
81 >            shouldThrow();
82 >        } catch (TimeoutException success) {
83 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
84 >    }
85 >
86 >    void checkCompletedNormally(RecursiveAction a) {
87 >        assertTrue(a.isDone());
88 >        assertFalse(a.isCancelled());
89 >        assertTrue(a.isCompletedNormally());
90 >        assertFalse(a.isCompletedAbnormally());
91 >        assertNull(a.getException());
92 >        assertNull(a.getRawResult());
93 >        assertNull(a.join());
94 >        assertFalse(a.cancel(false));
95 >        assertFalse(a.cancel(true));
96 >        try {
97 >            assertNull(a.get());
98 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
99 >        try {
100 >            assertNull(a.get(5L, SECONDS));
101 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
102 >    }
103 >
104 >    void checkCancelled(RecursiveAction a) {
105 >        assertTrue(a.isDone());
106 >        assertTrue(a.isCancelled());
107 >        assertFalse(a.isCompletedNormally());
108 >        assertTrue(a.isCompletedAbnormally());
109 >        assertTrue(a.getException() instanceof CancellationException);
110 >        assertNull(a.getRawResult());
111 >
112 >        try {
113 >            a.join();
114 >            shouldThrow();
115 >        } catch (CancellationException success) {
116 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
117 >
118 >        try {
119 >            a.get();
120 >            shouldThrow();
121 >        } catch (CancellationException success) {
122 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
123 >
124 >        try {
125 >            a.get(5L, SECONDS);
126 >            shouldThrow();
127 >        } catch (CancellationException success) {
128 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
129 >    }
130 >
131 >    void checkCompletedAbnormally(RecursiveAction a, Throwable t) {
132 >        assertTrue(a.isDone());
133 >        assertFalse(a.isCancelled());
134 >        assertFalse(a.isCompletedNormally());
135 >        assertTrue(a.isCompletedAbnormally());
136 >        assertSame(t, a.getException());
137 >        assertNull(a.getRawResult());
138 >        assertFalse(a.cancel(false));
139 >        assertFalse(a.cancel(true));
140 >
141 >        try {
142 >            a.join();
143 >            shouldThrow();
144 >        } catch (Throwable expected) {
145 >            assertSame(t, expected);
146 >        }
147 >
148 >        try {
149 >            a.get();
150 >            shouldThrow();
151 >        } catch (ExecutionException success) {
152 >            assertSame(t, success.getCause());
153 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
154 >
155 >        try {
156 >            a.get(5L, SECONDS);
157 >            shouldThrow();
158 >        } catch (ExecutionException success) {
159 >            assertSame(t, success.getCause());
160 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
161      }
162  
163      static final class FJException extends RuntimeException {
# Line 29 | Line 165 | public class RecursiveActionTest extends
165      }
166  
167      // A simple recursive action for testing
168 <    static final class FibAction extends RecursiveAction {
168 >    final class FibAction extends CheckedRecursiveAction {
169          final int number;
170          int result;
171          FibAction(int n) { number = n; }
172 <        public void compute() {
172 >        public void realCompute() {
173              int n = number;
174              if (n <= 1)
175                  result = n;
# Line 47 | Line 183 | public class RecursiveActionTest extends
183      }
184  
185      // A recursive action failing in base case
186 <    static final class FailingFibAction extends RecursiveAction {
186 >    static final class FailingFibAction extends RecursiveAction {
187          final int number;
188          int result;
189          FailingFibAction(int n) { number = n; }
# Line 64 | Line 200 | public class RecursiveActionTest extends
200          }
201      }
202  
203 <    /**
203 >    /**
204       * invoke returns when task completes normally.
205       * isCompletedAbnormally and isCancelled return false for normally
206       * completed tasks. getRawResult of a RecursiveAction returns null;
71     *
207       */
208      public void testInvoke() {
209 <        RecursiveAction a = new RecursiveAction() {
210 <                public void compute() {
211 <                    FibAction f = new FibAction(8);
212 <                    f.invoke();
213 <                    threadAssertTrue(f.result == 21);
214 <                    threadAssertTrue(f.isDone());
215 <                    threadAssertFalse(f.isCancelled());
216 <                    threadAssertFalse(f.isCompletedAbnormally());
82 <                    threadAssertTrue(f.getRawResult() == null);
83 <                }
84 <            };
85 <        mainPool.invoke(a);
209 >        RecursiveAction a = new CheckedRecursiveAction() {
210 >            public void realCompute() {
211 >                FibAction f = new FibAction(8);
212 >                assertNull(f.invoke());
213 >                assertEquals(21, f.result);
214 >                checkCompletedNormally(f);
215 >            }};
216 >        testInvokeOnPool(mainPool(), a);
217      }
218  
219 <    /**
219 >    /**
220       * quietlyInvoke task returns when task completes normally.
221       * isCompletedAbnormally and isCancelled return false for normally
222       * completed tasks
223       */
224      public void testQuietlyInvoke() {
225 <        RecursiveAction a = new RecursiveAction() {
226 <                public void compute() {
227 <                    FibAction f = new FibAction(8);
228 <                    f.quietlyInvoke();
229 <                    threadAssertTrue(f.result == 21);
230 <                    threadAssertTrue(f.isDone());
231 <                    threadAssertFalse(f.isCancelled());
232 <                    threadAssertFalse(f.isCompletedAbnormally());
102 <                    threadAssertTrue(f.getRawResult() == null);
103 <                }
104 <            };
105 <        mainPool.invoke(a);
225 >        RecursiveAction a = new CheckedRecursiveAction() {
226 >            public void realCompute() {
227 >                FibAction f = new FibAction(8);
228 >                f.quietlyInvoke();
229 >                assertEquals(21, f.result);
230 >                checkCompletedNormally(f);
231 >            }};
232 >        testInvokeOnPool(mainPool(), a);
233      }
234  
235 <    /**
235 >    /**
236       * join of a forked task returns when task completes
237       */
238      public void testForkJoin() {
239 <        RecursiveAction a = new RecursiveAction() {
240 <                public void compute() {
241 <                    FibAction f = new FibAction(8);
242 <                    f.fork();
239 >        RecursiveAction a = new CheckedRecursiveAction() {
240 >            public void realCompute() {
241 >                FibAction f = new FibAction(8);
242 >                assertSame(f, f.fork());
243 >                assertNull(f.join());
244 >                assertEquals(21, f.result);
245 >                checkCompletedNormally(f);
246 >            }};
247 >        testInvokeOnPool(mainPool(), a);
248 >    }
249 >
250 >    /**
251 >     * join/quietlyJoin of a forked task succeeds in the presence of interrupts
252 >     */
253 >    public void testJoinIgnoresInterrupts() {
254 >        RecursiveAction a = new CheckedRecursiveAction() {
255 >            public void realCompute() {
256 >                FibAction f = new FibAction(8);
257 >
258 >                // test join()
259 >                assertSame(f, f.fork());
260 >                Thread.currentThread().interrupt();
261 >                assertNull(f.join());
262 >                Thread.interrupted();
263 >                assertEquals(21, f.result);
264 >                checkCompletedNormally(f);
265 >
266 >                f.reinitialize();
267 >                f.cancel(true);
268 >                assertSame(f, f.fork());
269 >                Thread.currentThread().interrupt();
270 >                try {
271                      f.join();
272 <                    threadAssertTrue(f.result == 21);
273 <                    threadAssertTrue(f.isDone());
274 <                    threadAssertTrue(f.getRawResult() == null);
272 >                    shouldThrow();
273 >                } catch (CancellationException success) {
274 >                    Thread.interrupted();
275 >                    checkCancelled(f);
276                  }
277 <            };
278 <        mainPool.invoke(a);
277 >
278 >                f.reinitialize();
279 >                f.completeExceptionally(new FJException());
280 >                assertSame(f, f.fork());
281 >                Thread.currentThread().interrupt();
282 >                try {
283 >                    f.join();
284 >                    shouldThrow();
285 >                } catch (FJException success) {
286 >                    Thread.interrupted();
287 >                    checkCompletedAbnormally(f, success);
288 >                }
289 >
290 >                // test quietlyJoin()
291 >                f.reinitialize();
292 >                assertSame(f, f.fork());
293 >                Thread.currentThread().interrupt();
294 >                f.quietlyJoin();
295 >                Thread.interrupted();
296 >                assertEquals(21, f.result);
297 >                checkCompletedNormally(f);
298 >
299 >                f.reinitialize();
300 >                f.cancel(true);
301 >                assertSame(f, f.fork());
302 >                Thread.currentThread().interrupt();
303 >                f.quietlyJoin();
304 >                Thread.interrupted();
305 >                checkCancelled(f);
306 >
307 >                f.reinitialize();
308 >                f.completeExceptionally(new FJException());
309 >                assertSame(f, f.fork());
310 >                Thread.currentThread().interrupt();
311 >                f.quietlyJoin();
312 >                Thread.interrupted();
313 >                checkCompletedAbnormally(f, f.getException());
314 >            }};
315 >        testInvokeOnPool(mainPool(), a);
316 >        a.reinitialize();
317 >        testInvokeOnPool(singletonPool(), a);
318      }
319  
320 <    /**
320 >    /**
321 >     * join/quietlyJoin of a forked task when not in ForkJoinPool
322 >     * succeeds in the presence of interrupts
323 >     */
324 >    public void testJoinIgnoresInterruptsOutsideForkJoinPool() {
325 >        final SynchronousQueue<FibAction[]> sq =
326 >            new SynchronousQueue<FibAction[]>();
327 >        RecursiveAction a = new CheckedRecursiveAction() {
328 >            public void realCompute() throws InterruptedException {
329 >                FibAction[] fibActions = new FibAction[6];
330 >                for (int i = 0; i < fibActions.length; i++)
331 >                    fibActions[i] = new FibAction(8);
332 >
333 >                fibActions[1].cancel(false);
334 >                fibActions[2].completeExceptionally(new FJException());
335 >                fibActions[4].cancel(true);
336 >                fibActions[5].completeExceptionally(new FJException());
337 >
338 >                for (int i = 0; i < fibActions.length; i++)
339 >                    fibActions[i].fork();
340 >
341 >                sq.put(fibActions);
342 >
343 >                helpQuiesce();
344 >            }};
345 >
346 >        Runnable r = new CheckedRunnable() {
347 >            public void realRun() throws InterruptedException {
348 >                FibAction[] fibActions = sq.take();
349 >                FibAction f;
350 >
351 >                // test join() ------------
352 >
353 >                f = fibActions[0];
354 >                assertFalse(f.inForkJoinPool());
355 >                Thread.currentThread().interrupt();
356 >                assertNull(f.join());
357 >                assertTrue(Thread.interrupted());
358 >                assertEquals(21, f.result);
359 >                checkCompletedNormally(f);
360 >
361 >                f = fibActions[1];
362 >                Thread.currentThread().interrupt();
363 >                try {
364 >                    f.join();
365 >                    shouldThrow();
366 >                } catch (CancellationException success) {
367 >                    assertTrue(Thread.interrupted());
368 >                    checkCancelled(f);
369 >                }
370 >
371 >                f = fibActions[2];
372 >                Thread.currentThread().interrupt();
373 >                try {
374 >                    f.join();
375 >                    shouldThrow();
376 >                } catch (FJException success) {
377 >                    assertTrue(Thread.interrupted());
378 >                    checkCompletedAbnormally(f, success);
379 >                }
380 >
381 >                // test quietlyJoin() ---------
382 >
383 >                f = fibActions[3];
384 >                Thread.currentThread().interrupt();
385 >                f.quietlyJoin();
386 >                assertTrue(Thread.interrupted());
387 >                assertEquals(21, f.result);
388 >                checkCompletedNormally(f);
389 >
390 >                f = fibActions[4];
391 >                Thread.currentThread().interrupt();
392 >                f.quietlyJoin();
393 >                assertTrue(Thread.interrupted());
394 >                checkCancelled(f);
395 >
396 >                f = fibActions[5];
397 >                Thread.currentThread().interrupt();
398 >                f.quietlyJoin();
399 >                assertTrue(Thread.interrupted());
400 >                assertTrue(f.getException() instanceof FJException);
401 >                checkCompletedAbnormally(f, f.getException());
402 >            }};
403 >
404 >        Thread t;
405 >
406 >        t = newStartedThread(r);
407 >        testInvokeOnPool(mainPool(), a);
408 >        awaitTermination(t, LONG_DELAY_MS);
409 >
410 >        a.reinitialize();
411 >        t = newStartedThread(r);
412 >        testInvokeOnPool(singletonPool(), a);
413 >        awaitTermination(t, LONG_DELAY_MS);
414 >    }
415 >
416 >    /**
417       * get of a forked task returns when task completes
418       */
419      public void testForkGet() {
420 <        RecursiveAction a = new RecursiveAction() {
421 <                public void compute() {
422 <                    try {
423 <                        FibAction f = new FibAction(8);
424 <                        f.fork();
425 <                        f.get();
426 <                        threadAssertTrue(f.result == 21);
427 <                        threadAssertTrue(f.isDone());
428 <                    } catch(Exception ex) {
138 <                        unexpectedException();
139 <                    }
140 <                }
141 <            };
142 <        mainPool.invoke(a);
420 >        RecursiveAction a = new CheckedRecursiveAction() {
421 >            public void realCompute() throws Exception {
422 >                FibAction f = new FibAction(8);
423 >                assertSame(f, f.fork());
424 >                assertNull(f.get());
425 >                assertEquals(21, f.result);
426 >                checkCompletedNormally(f);
427 >            }};
428 >        testInvokeOnPool(mainPool(), a);
429      }
430  
431 <    /**
431 >    /**
432       * timed get of a forked task returns when task completes
433       */
434      public void testForkTimedGet() {
435 <        RecursiveAction a = new RecursiveAction() {
436 <                public void compute() {
437 <                    try {
438 <                        FibAction f = new FibAction(8);
439 <                        f.fork();
440 <                        f.get(5L, TimeUnit.SECONDS);
441 <                        threadAssertTrue(f.result == 21);
442 <                        threadAssertTrue(f.isDone());
443 <                    } catch(Exception ex) {
158 <                        unexpectedException();
159 <                    }
160 <                }
161 <            };
162 <        mainPool.invoke(a);
435 >        RecursiveAction a = new CheckedRecursiveAction() {
436 >            public void realCompute() throws Exception {
437 >                FibAction f = new FibAction(8);
438 >                assertSame(f, f.fork());
439 >                assertNull(f.get(5L, SECONDS));
440 >                assertEquals(21, f.result);
441 >                checkCompletedNormally(f);
442 >            }};
443 >        testInvokeOnPool(mainPool(), a);
444      }
445  
446 <    /**
446 >    /**
447       * timed get with null time unit throws NPE
448       */
449      public void testForkTimedGetNPE() {
450 <        RecursiveAction a = new RecursiveAction() {
451 <                public void compute() {
452 <                    try {
453 <                        FibAction f = new FibAction(8);
454 <                        f.fork();
455 <                        f.get(5L, null);
456 <                    } catch(NullPointerException success) {
457 <                    } catch(Exception ex) {
458 <                        unexpectedException();
459 <                    }
179 <                }
180 <            };
181 <        mainPool.invoke(a);
182 <    }
183 <
184 <    /**
185 <     * helpJoin of a forked task returns when task completes
186 <     */
187 <    public void testForkHelpJoin() {
188 <        RecursiveAction a = new RecursiveAction() {
189 <                public void compute() {
190 <                    FibAction f = new FibAction(8);
191 <                    f.fork();
192 <                    f.helpJoin();
193 <                    threadAssertTrue(f.result == 21);
194 <                    threadAssertTrue(f.isDone());
195 <                }
196 <            };
197 <        mainPool.invoke(a);
450 >        RecursiveAction a = new CheckedRecursiveAction() {
451 >            public void realCompute() throws Exception {
452 >                FibAction f = new FibAction(8);
453 >                assertSame(f, f.fork());
454 >                try {
455 >                    f.get(5L, null);
456 >                    shouldThrow();
457 >                } catch (NullPointerException success) {}
458 >            }};
459 >        testInvokeOnPool(mainPool(), a);
460      }
461  
462 <    /**
462 >    /**
463       * quietlyJoin of a forked task returns when task completes
464       */
465      public void testForkQuietlyJoin() {
466 <        RecursiveAction a = new RecursiveAction() {
467 <                public void compute() {
468 <                    FibAction f = new FibAction(8);
469 <                    f.fork();
470 <                    f.quietlyJoin();
471 <                    threadAssertTrue(f.result == 21);
472 <                    threadAssertTrue(f.isDone());
473 <                }
474 <            };
213 <        mainPool.invoke(a);
214 <    }
215 <
216 <
217 <    /**
218 <     * quietlyHelpJoin of a forked task returns when task completes
219 <     */
220 <    public void testForkQuietlyHelpJoin() {
221 <        RecursiveAction a = new RecursiveAction() {
222 <                public void compute() {
223 <                    FibAction f = new FibAction(8);
224 <                    f.fork();
225 <                    f.quietlyHelpJoin();
226 <                    threadAssertTrue(f.result == 21);
227 <                    threadAssertTrue(f.isDone());
228 <                }
229 <            };
230 <        mainPool.invoke(a);
466 >        RecursiveAction a = new CheckedRecursiveAction() {
467 >            public void realCompute() {
468 >                FibAction f = new FibAction(8);
469 >                assertSame(f, f.fork());
470 >                f.quietlyJoin();
471 >                assertEquals(21, f.result);
472 >                checkCompletedNormally(f);
473 >            }};
474 >        testInvokeOnPool(mainPool(), a);
475      }
476  
477  
478 <    /**
478 >    /**
479       * helpQuiesce returns when tasks are complete.
480       * getQueuedTaskCount returns 0 when quiescent
481       */
482      public void testForkHelpQuiesce() {
483 <        RecursiveAction a = new RecursiveAction() {
484 <                public void compute() {
485 <                    FibAction f = new FibAction(8);
486 <                    f.fork();
487 <                    f.helpQuiesce();
488 <                    threadAssertTrue(f.result == 21);
489 <                    threadAssertTrue(f.isDone());
490 <                    threadAssertTrue(getQueuedTaskCount() == 0);
491 <                }
492 <            };
249 <        mainPool.invoke(a);
483 >        RecursiveAction a = new CheckedRecursiveAction() {
484 >            public void realCompute() {
485 >                FibAction f = new FibAction(8);
486 >                assertSame(f, f.fork());
487 >                f.helpQuiesce();
488 >                assertEquals(21, f.result);
489 >                assertEquals(0, getQueuedTaskCount());
490 >                checkCompletedNormally(f);
491 >            }};
492 >        testInvokeOnPool(mainPool(), a);
493      }
494  
495  
496 <    /**
496 >    /**
497       * invoke task throws exception when task completes abnormally
498       */
499      public void testAbnormalInvoke() {
500 <        RecursiveAction a = new RecursiveAction() {
501 <                public void compute() {
502 <                    try {
503 <                        FailingFibAction f = new FailingFibAction(8);
504 <                        f.invoke();
505 <                        shouldThrow();
506 <                    } catch(FJException success) {
507 <                    }
500 >        RecursiveAction a = new CheckedRecursiveAction() {
501 >            public void realCompute() {
502 >                FailingFibAction f = new FailingFibAction(8);
503 >                try {
504 >                    f.invoke();
505 >                    shouldThrow();
506 >                } catch (FJException success) {
507 >                    checkCompletedAbnormally(f, success);
508                  }
509 <            };
510 <        mainPool.invoke(a);
509 >            }};
510 >        testInvokeOnPool(mainPool(), a);
511      }
512  
513 <    /**
514 <     * quietelyInvoke task returns when task completes abnormally
513 >    /**
514 >     * quietlyInvoke task returns when task completes abnormally
515       */
516      public void testAbnormalQuietlyInvoke() {
517 <        RecursiveAction a = new RecursiveAction() {
518 <                public void compute() {
519 <                    FailingFibAction f = new FailingFibAction(8);
520 <                    f.quietlyInvoke();
521 <                    threadAssertTrue(f.isDone());
522 <                }
523 <            };
524 <        mainPool.invoke(a);
517 >        RecursiveAction a = new CheckedRecursiveAction() {
518 >            public void realCompute() {
519 >                FailingFibAction f = new FailingFibAction(8);
520 >                f.quietlyInvoke();
521 >                assertTrue(f.getException() instanceof FJException);
522 >                checkCompletedAbnormally(f, f.getException());
523 >            }};
524 >        testInvokeOnPool(mainPool(), a);
525      }
526  
527 <    /**
527 >    /**
528       * join of a forked task throws exception when task completes abnormally
529       */
530      public void testAbnormalForkJoin() {
531 <        RecursiveAction a = new RecursiveAction() {
532 <                public void compute() {
533 <                    try {
534 <                        FailingFibAction f = new FailingFibAction(8);
535 <                        f.fork();
536 <                        f.join();
537 <                        shouldThrow();
538 <                    } catch(FJException success) {
539 <                    }
531 >        RecursiveAction a = new CheckedRecursiveAction() {
532 >            public void realCompute() {
533 >                FailingFibAction f = new FailingFibAction(8);
534 >                assertSame(f, f.fork());
535 >                try {
536 >                    f.join();
537 >                    shouldThrow();
538 >                } catch (FJException success) {
539 >                    checkCompletedAbnormally(f, success);
540                  }
541 <            };
542 <        mainPool.invoke(a);
541 >            }};
542 >        testInvokeOnPool(mainPool(), a);
543      }
544  
545 <    /**
545 >    /**
546       * get of a forked task throws exception when task completes abnormally
547       */
548      public void testAbnormalForkGet() {
549 <        RecursiveAction a = new RecursiveAction() {
550 <                public void compute() {
551 <                    try {
552 <                        FailingFibAction f = new FailingFibAction(8);
553 <                        f.fork();
554 <                        f.get();
555 <                        shouldThrow();
556 <                    } catch(Exception success) {
557 <                    }
549 >        RecursiveAction a = new CheckedRecursiveAction() {
550 >            public void realCompute() throws Exception {
551 >                FailingFibAction f = new FailingFibAction(8);
552 >                assertSame(f, f.fork());
553 >                try {
554 >                    f.get();
555 >                    shouldThrow();
556 >                } catch (ExecutionException success) {
557 >                    Throwable cause = success.getCause();
558 >                    assertTrue(cause instanceof FJException);
559 >                    checkCompletedAbnormally(f, cause);
560                  }
561 <            };
562 <        mainPool.invoke(a);
561 >            }};
562 >        testInvokeOnPool(mainPool(), a);
563      }
564  
565 <    /**
565 >    /**
566       * timed get of a forked task throws exception when task completes abnormally
567       */
568      public void testAbnormalForkTimedGet() {
569 <        RecursiveAction a = new RecursiveAction() {
570 <                public void compute() {
571 <                    try {
572 <                        FailingFibAction f = new FailingFibAction(8);
573 <                        f.fork();
574 <                        f.get(5L, TimeUnit.SECONDS);
575 <                        shouldThrow();
576 <                    } catch(Exception success) {
577 <                    }
578 <                }
579 <            };
335 <        mainPool.invoke(a);
336 <    }
337 <
338 <    /**
339 <     * join of a forked task throws exception when task completes abnormally
340 <     */
341 <    public void testAbnormalForkHelpJoin() {
342 <        RecursiveAction a = new RecursiveAction() {
343 <                public void compute() {
344 <                    try {
345 <                        FailingFibAction f = new FailingFibAction(8);
346 <                        f.fork();
347 <                        f.helpJoin();
348 <                        shouldThrow();
349 <                    } catch(FJException success) {
350 <                    }
351 <                }
352 <            };
353 <        mainPool.invoke(a);
354 <    }
355 <
356 <    /**
357 <     * quietlyHelpJoin of a forked task returns when task completes abnormally.
358 <     * getException of failed task returns its exception.
359 <     * isCompletedAbnormally of a failed task returns true.
360 <     * isCancelled of a failed uncancelled task returns false
361 <     */
362 <    public void testAbnormalForkQuietlyHelpJoin() {
363 <        RecursiveAction a = new RecursiveAction() {
364 <                public void compute() {
365 <                    FailingFibAction f = new FailingFibAction(8);
366 <                    f.fork();
367 <                    f.quietlyHelpJoin();
368 <                    threadAssertTrue(f.isDone());
369 <                    threadAssertTrue(f.isCompletedAbnormally());
370 <                    threadAssertFalse(f.isCancelled());
371 <                    threadAssertTrue(f.getException() instanceof FJException);
569 >        RecursiveAction a = new CheckedRecursiveAction() {
570 >            public void realCompute() throws Exception {
571 >                FailingFibAction f = new FailingFibAction(8);
572 >                assertSame(f, f.fork());
573 >                try {
574 >                    f.get(5L, TimeUnit.SECONDS);
575 >                    shouldThrow();
576 >                } catch (ExecutionException success) {
577 >                    Throwable cause = success.getCause();
578 >                    assertTrue(cause instanceof FJException);
579 >                    checkCompletedAbnormally(f, cause);
580                  }
581 <            };
582 <        mainPool.invoke(a);
581 >            }};
582 >        testInvokeOnPool(mainPool(), a);
583      }
584  
585 <    /**
585 >    /**
586       * quietlyJoin of a forked task returns when task completes abnormally
587       */
588      public void testAbnormalForkQuietlyJoin() {
589 <        RecursiveAction a = new RecursiveAction() {
590 <                public void compute() {
591 <                    FailingFibAction f = new FailingFibAction(8);
592 <                    f.fork();
593 <                    f.quietlyJoin();
594 <                    threadAssertTrue(f.isDone());
595 <                    threadAssertTrue(f.isCompletedAbnormally());
596 <                    threadAssertTrue(f.getException() instanceof FJException);
597 <                }
390 <            };
391 <        mainPool.invoke(a);
589 >        RecursiveAction a = new CheckedRecursiveAction() {
590 >            public void realCompute() {
591 >                FailingFibAction f = new FailingFibAction(8);
592 >                assertSame(f, f.fork());
593 >                f.quietlyJoin();
594 >                assertTrue(f.getException() instanceof FJException);
595 >                checkCompletedAbnormally(f, f.getException());
596 >            }};
597 >        testInvokeOnPool(mainPool(), a);
598      }
599  
600 <    /**
600 >    /**
601       * invoke task throws exception when task cancelled
602       */
603      public void testCancelledInvoke() {
604 <        RecursiveAction a = new RecursiveAction() {
605 <                public void compute() {
606 <                    try {
607 <                        FibAction f = new FibAction(8);
608 <                        f.cancel(true);
609 <                        f.invoke();
610 <                        shouldThrow();
611 <                    } catch(CancellationException success) {
612 <                    }
604 >        RecursiveAction a = new CheckedRecursiveAction() {
605 >            public void realCompute() {
606 >                FibAction f = new FibAction(8);
607 >                assertTrue(f.cancel(true));
608 >                try {
609 >                    f.invoke();
610 >                    shouldThrow();
611 >                } catch (CancellationException success) {
612 >                    checkCancelled(f);
613                  }
614 <            };
615 <        mainPool.invoke(a);
614 >            }};
615 >        testInvokeOnPool(mainPool(), a);
616      }
617  
618 <    /**
618 >    /**
619       * join of a forked task throws exception when task cancelled
620       */
621      public void testCancelledForkJoin() {
622 <        RecursiveAction a = new RecursiveAction() {
623 <                public void compute() {
624 <                    try {
625 <                        FibAction f = new FibAction(8);
626 <                        f.cancel(true);
627 <                        f.fork();
628 <                        f.join();
629 <                        shouldThrow();
630 <                    } catch(CancellationException success) {
631 <                    }
622 >        RecursiveAction a = new CheckedRecursiveAction() {
623 >            public void realCompute() {
624 >                FibAction f = new FibAction(8);
625 >                assertTrue(f.cancel(true));
626 >                assertSame(f, f.fork());
627 >                try {
628 >                    f.join();
629 >                    shouldThrow();
630 >                } catch (CancellationException success) {
631 >                    checkCancelled(f);
632                  }
633 <            };
634 <        mainPool.invoke(a);
633 >            }};
634 >        testInvokeOnPool(mainPool(), a);
635      }
636  
637 <    /**
637 >    /**
638       * get of a forked task throws exception when task cancelled
639       */
640      public void testCancelledForkGet() {
641 <        RecursiveAction a = new RecursiveAction() {
642 <                public void compute() {
643 <                    try {
644 <                        FibAction f = new FibAction(8);
645 <                        f.cancel(true);
646 <                        f.fork();
647 <                        f.get();
648 <                        shouldThrow();
649 <                    } catch(Exception success) {
650 <                    }
641 >        RecursiveAction a = new CheckedRecursiveAction() {
642 >            public void realCompute() throws Exception {
643 >                FibAction f = new FibAction(8);
644 >                assertTrue(f.cancel(true));
645 >                assertSame(f, f.fork());
646 >                try {
647 >                    f.get();
648 >                    shouldThrow();
649 >                } catch (CancellationException success) {
650 >                    checkCancelled(f);
651                  }
652 <            };
653 <        mainPool.invoke(a);
652 >            }};
653 >        testInvokeOnPool(mainPool(), a);
654      }
655  
656 <    /**
656 >    /**
657       * timed get of a forked task throws exception when task cancelled
658       */
659      public void testCancelledForkTimedGet() {
660 <        RecursiveAction a = new RecursiveAction() {
661 <                public void compute() {
662 <                    try {
663 <                        FibAction f = new FibAction(8);
664 <                        f.cancel(true);
665 <                        f.fork();
666 <                        f.get(5L, TimeUnit.SECONDS);
667 <                        shouldThrow();
668 <                    } catch(Exception success) {
669 <                    }
464 <                }
465 <            };
466 <        mainPool.invoke(a);
467 <    }
468 <
469 <    /**
470 <     * join of a forked task throws exception when task cancelled
471 <     */
472 <    public void testCancelledForkHelpJoin() {
473 <        RecursiveAction a = new RecursiveAction() {
474 <                public void compute() {
475 <                    try {
476 <                        FibAction f = new FibAction(8);
477 <                        f.cancel(true);
478 <                        f.fork();
479 <                        f.helpJoin();
480 <                        shouldThrow();
481 <                    } catch(CancellationException success) {
482 <                    }
483 <                }
484 <            };
485 <        mainPool.invoke(a);
486 <    }
487 <
488 <    /**
489 <     * quietlyHelpJoin of a forked task returns when task cancelled.
490 <     * getException of cancelled task returns its exception.
491 <     * isCompletedAbnormally of a cancelled task returns true.
492 <     * isCancelled of a cancelled task returns true
493 <     */
494 <    public void testCancelledForkQuietlyHelpJoin() {
495 <        RecursiveAction a = new RecursiveAction() {
496 <                public void compute() {
497 <                    FibAction f = new FibAction(8);
498 <                    f.cancel(true);
499 <                    f.fork();
500 <                    f.quietlyHelpJoin();
501 <                    threadAssertTrue(f.isDone());
502 <                    threadAssertTrue(f.isCompletedAbnormally());
503 <                    threadAssertTrue(f.isCancelled());
504 <                    threadAssertTrue(f.getException() instanceof CancellationException);
660 >        RecursiveAction a = new CheckedRecursiveAction() {
661 >            public void realCompute() throws Exception {
662 >                FibAction f = new FibAction(8);
663 >                assertTrue(f.cancel(true));
664 >                assertSame(f, f.fork());
665 >                try {
666 >                    f.get(5L, SECONDS);
667 >                    shouldThrow();
668 >                } catch (CancellationException success) {
669 >                    checkCancelled(f);
670                  }
671 <            };
672 <        mainPool.invoke(a);
671 >            }};
672 >        testInvokeOnPool(mainPool(), a);
673      }
674  
675 <    /**
675 >    /**
676       * quietlyJoin of a forked task returns when task cancelled
677       */
678      public void testCancelledForkQuietlyJoin() {
679 <        RecursiveAction a = new RecursiveAction() {
680 <                public void compute() {
681 <                    FibAction f = new FibAction(8);
682 <                    f.cancel(true);
683 <                    f.fork();
684 <                    f.quietlyJoin();
685 <                    threadAssertTrue(f.isDone());
686 <                    threadAssertTrue(f.isCompletedAbnormally());
687 <                    threadAssertTrue(f.getException() instanceof CancellationException);
523 <                }
524 <            };
525 <        mainPool.invoke(a);
679 >        RecursiveAction a = new CheckedRecursiveAction() {
680 >            public void realCompute() {
681 >                FibAction f = new FibAction(8);
682 >                assertTrue(f.cancel(true));
683 >                assertSame(f, f.fork());
684 >                f.quietlyJoin();
685 >                checkCancelled(f);
686 >            }};
687 >        testInvokeOnPool(mainPool(), a);
688      }
689  
690      /**
691       * getPool of executing task returns its pool
692       */
693      public void testGetPool() {
694 <        RecursiveAction a = new RecursiveAction() {
695 <                public void compute() {
696 <                    threadAssertTrue(getPool() == mainPool);
697 <                }
698 <            };
699 <        mainPool.invoke(a);
694 >        final ForkJoinPool mainPool = mainPool();
695 >        RecursiveAction a = new CheckedRecursiveAction() {
696 >            public void realCompute() {
697 >                assertSame(mainPool, getPool());
698 >            }};
699 >        testInvokeOnPool(mainPool, a);
700      }
701  
702      /**
703       * getPool of non-FJ task returns null
704       */
705      public void testGetPool2() {
706 <        RecursiveAction a = new RecursiveAction() {
707 <                public void compute() {
708 <                    threadAssertTrue(getPool() == null);
709 <                }
710 <            };
549 <        a.invoke();
706 >        RecursiveAction a = new CheckedRecursiveAction() {
707 >            public void realCompute() {
708 >                assertNull(getPool());
709 >            }};
710 >        assertNull(a.invoke());
711      }
712  
713      /**
714       * inForkJoinPool of executing task returns true
715       */
716      public void testInForkJoinPool() {
717 <        RecursiveAction a = new RecursiveAction() {
718 <                public void compute() {
719 <                    threadAssertTrue(inForkJoinPool());
720 <                }
721 <            };
561 <        mainPool.invoke(a);
717 >        RecursiveAction a = new CheckedRecursiveAction() {
718 >            public void realCompute() {
719 >                assertTrue(inForkJoinPool());
720 >            }};
721 >        testInvokeOnPool(mainPool(), a);
722      }
723  
724      /**
725       * inForkJoinPool of non-FJ task returns false
726       */
727      public void testInForkJoinPool2() {
728 <        RecursiveAction a = new RecursiveAction() {
729 <                public void compute() {
730 <                    threadAssertTrue(!inForkJoinPool());
731 <                }
732 <            };
573 <        a.invoke();
728 >        RecursiveAction a = new CheckedRecursiveAction() {
729 >            public void realCompute() {
730 >                assertFalse(inForkJoinPool());
731 >            }};
732 >        assertNull(a.invoke());
733      }
734  
735      /**
736       * getPool of current thread in pool returns its pool
737       */
738      public void testWorkerGetPool() {
739 <        RecursiveAction a = new RecursiveAction() {
740 <                public void compute() {
741 <                    ForkJoinWorkerThread w =
742 <                        (ForkJoinWorkerThread)(Thread.currentThread());
743 <                    threadAssertTrue(w.getPool() == mainPool);
744 <                }
745 <            };
746 <        mainPool.invoke(a);
739 >        final ForkJoinPool mainPool = mainPool();
740 >        RecursiveAction a = new CheckedRecursiveAction() {
741 >            public void realCompute() {
742 >                ForkJoinWorkerThread w =
743 >                    (ForkJoinWorkerThread) Thread.currentThread();
744 >                assertSame(mainPool, w.getPool());
745 >            }};
746 >        testInvokeOnPool(mainPool, a);
747      }
748  
749      /**
750       * getPoolIndex of current thread in pool returns 0 <= value < poolSize
592     *
751       */
752      public void testWorkerGetPoolIndex() {
753 <        RecursiveAction a = new RecursiveAction() {
754 <                public void compute() {
755 <                    ForkJoinWorkerThread w =
756 <                        (ForkJoinWorkerThread)(Thread.currentThread());
757 <                    int idx = w.getPoolIndex();
758 <                    threadAssertTrue(idx >= 0);
759 <                    threadAssertTrue(idx < mainPool.getPoolSize());
760 <                }
761 <            };
604 <        mainPool.invoke(a);
753 >        final ForkJoinPool mainPool = mainPool();
754 >        RecursiveAction a = new CheckedRecursiveAction() {
755 >            public void realCompute() {
756 >                ForkJoinWorkerThread w =
757 >                    (ForkJoinWorkerThread)(Thread.currentThread());
758 >                assertTrue(w.getPoolIndex() >= 0);
759 >                assertTrue(w.getPoolIndex() < mainPool.getPoolSize());
760 >            }};
761 >        testInvokeOnPool(mainPool, a);
762      }
763  
764  
# Line 609 | Line 766 | public class RecursiveActionTest extends
766       * setRawResult(null) succeeds
767       */
768      public void testSetRawResult() {
769 <        RecursiveAction a = new RecursiveAction() {
770 <                public void compute() {
771 <                    setRawResult(null);
772 <                }
773 <            };
774 <        a.invoke();
769 >        RecursiveAction a = new CheckedRecursiveAction() {
770 >            public void realCompute() {
771 >                setRawResult(null);
772 >                assertNull(getRawResult());
773 >            }};
774 >        assertNull(a.invoke());
775      }
776  
777 <    /**
777 >    /**
778       * A reinitialized task may be re-invoked
779       */
780      public void testReinitialize() {
781 <        RecursiveAction a = new RecursiveAction() {
782 <                public void compute() {
783 <                    FibAction f = new FibAction(8);
784 <                    f.invoke();
785 <                    threadAssertTrue(f.result == 21);
786 <                    threadAssertTrue(f.isDone());
787 <                    threadAssertFalse(f.isCancelled());
788 <                    threadAssertFalse(f.isCompletedAbnormally());
781 >        RecursiveAction a = new CheckedRecursiveAction() {
782 >            public void realCompute() {
783 >                FibAction f = new FibAction(8);
784 >                checkNotDone(f);
785 >
786 >                for (int i = 0; i < 3; i++) {
787 >                    assertNull(f.invoke());
788 >                    assertEquals(21, f.result);
789 >                    checkCompletedNormally(f);
790                      f.reinitialize();
791 <                    f.invoke();
634 <                    threadAssertTrue(f.result == 21);
791 >                    checkNotDone(f);
792                  }
793 <            };
794 <        mainPool.invoke(a);
793 >            }};
794 >        testInvokeOnPool(mainPool(), a);
795      }
796  
797 <    /**
797 >    /**
798       * invoke task throws exception after invoking completeExceptionally
799       */
800      public void testCompleteExceptionally() {
801 <        RecursiveAction a = new RecursiveAction() {
802 <                public void compute() {
803 <                    try {
804 <                        FibAction f = new FibAction(8);
805 <                        f.completeExceptionally(new FJException());
806 <                        f.invoke();
807 <                        shouldThrow();
808 <                    } catch(FJException success) {
809 <                    }
801 >        RecursiveAction a = new CheckedRecursiveAction() {
802 >            public void realCompute() {
803 >                FibAction f = new FibAction(8);
804 >                f.completeExceptionally(new FJException());
805 >                try {
806 >                    f.invoke();
807 >                    shouldThrow();
808 >                } catch (FJException success) {
809 >                    checkCompletedAbnormally(f, success);
810                  }
811 <            };
812 <        mainPool.invoke(a);
811 >            }};
812 >        testInvokeOnPool(mainPool(), a);
813      }
814  
815 <    /**
815 >    /**
816       * invoke task suppresses execution invoking complete
817       */
818      public void testComplete() {
819 <        RecursiveAction a = new RecursiveAction() {
820 <                public void compute() {
821 <                    FibAction f = new FibAction(8);
822 <                    f.complete(null);
823 <                    f.invoke();
824 <                    threadAssertTrue(f.isDone());
825 <                    threadAssertTrue(f.result == 0);
826 <                }
827 <            };
671 <        mainPool.invoke(a);
819 >        RecursiveAction a = new CheckedRecursiveAction() {
820 >            public void realCompute() {
821 >                FibAction f = new FibAction(8);
822 >                f.complete(null);
823 >                assertNull(f.invoke());
824 >                assertEquals(0, f.result);
825 >                checkCompletedNormally(f);
826 >            }};
827 >        testInvokeOnPool(mainPool(), a);
828      }
829  
830 <    /**
830 >    /**
831       * invokeAll(t1, t2) invokes all task arguments
832       */
833      public void testInvokeAll2() {
834 <        RecursiveAction a = new RecursiveAction() {
835 <                public void compute() {
836 <                    FibAction f = new FibAction(8);
837 <                    FibAction g = new FibAction(9);
838 <                    invokeAll(f, g);
839 <                    threadAssertTrue(f.isDone());
840 <                    threadAssertTrue(f.result == 21);
841 <                    threadAssertTrue(g.isDone());
842 <                    threadAssertTrue(g.result == 34);
843 <                }
844 <            };
689 <        mainPool.invoke(a);
834 >        RecursiveAction a = new CheckedRecursiveAction() {
835 >            public void realCompute() {
836 >                FibAction f = new FibAction(8);
837 >                FibAction g = new FibAction(9);
838 >                invokeAll(f, g);
839 >                checkCompletedNormally(f);
840 >                assertEquals(21, f.result);
841 >                checkCompletedNormally(g);
842 >                assertEquals(34, g.result);
843 >            }};
844 >        testInvokeOnPool(mainPool(), a);
845      }
846  
847 <    /**
847 >    /**
848       * invokeAll(tasks) with 1 argument invokes task
849       */
850      public void testInvokeAll1() {
851 <        RecursiveAction a = new RecursiveAction() {
852 <                public void compute() {
853 <                    FibAction f = new FibAction(8);
854 <                    invokeAll(f);
855 <                    threadAssertTrue(f.isDone());
856 <                    threadAssertTrue(f.result == 21);
857 <                }
858 <            };
704 <        mainPool.invoke(a);
851 >        RecursiveAction a = new CheckedRecursiveAction() {
852 >            public void realCompute() {
853 >                FibAction f = new FibAction(8);
854 >                invokeAll(f);
855 >                checkCompletedNormally(f);
856 >                assertEquals(21, f.result);
857 >            }};
858 >        testInvokeOnPool(mainPool(), a);
859      }
860  
861 <    /**
861 >    /**
862       * invokeAll(tasks) with > 2 argument invokes tasks
863       */
864      public void testInvokeAll3() {
865 <        RecursiveAction a = new RecursiveAction() {
866 <                public void compute() {
867 <                    FibAction f = new FibAction(8);
868 <                    FibAction g = new FibAction(9);
869 <                    FibAction h = new FibAction(7);
870 <                    invokeAll(f, g, h);
871 <                    threadAssertTrue(f.isDone());
872 <                    threadAssertTrue(f.result == 21);
873 <                    threadAssertTrue(g.isDone());
874 <                    threadAssertTrue(g.result == 34);
875 <                    threadAssertTrue(h.isDone());
876 <                    threadAssertTrue(h.result == 13);
877 <                }
878 <            };
879 <        mainPool.invoke(a);
865 >        RecursiveAction a = new CheckedRecursiveAction() {
866 >            public void realCompute() {
867 >                FibAction f = new FibAction(8);
868 >                FibAction g = new FibAction(9);
869 >                FibAction h = new FibAction(7);
870 >                invokeAll(f, g, h);
871 >                assertTrue(f.isDone());
872 >                assertTrue(g.isDone());
873 >                assertTrue(h.isDone());
874 >                checkCompletedNormally(f);
875 >                assertEquals(21, f.result);
876 >                checkCompletedNormally(g);
877 >                assertEquals(34, g.result);
878 >                checkCompletedNormally(g);
879 >                assertEquals(13, h.result);
880 >            }};
881 >        testInvokeOnPool(mainPool(), a);
882      }
883  
884 <    /**
884 >    /**
885       * invokeAll(collection) invokes all tasks in the collection
886       */
887      public void testInvokeAllCollection() {
888 <        RecursiveAction a = new RecursiveAction() {
889 <                public void compute() {
890 <                    FibAction f = new FibAction(8);
891 <                    FibAction g = new FibAction(9);
892 <                    FibAction h = new FibAction(7);
893 <                    HashSet set = new HashSet();
894 <                    set.add(f);
895 <                    set.add(g);
896 <                    set.add(h);
897 <                    invokeAll(set);
898 <                    threadAssertTrue(f.isDone());
899 <                    threadAssertTrue(f.result == 21);
900 <                    threadAssertTrue(g.isDone());
901 <                    threadAssertTrue(g.result == 34);
902 <                    threadAssertTrue(h.isDone());
903 <                    threadAssertTrue(h.result == 13);
904 <                }
905 <            };
906 <        mainPool.invoke(a);
888 >        RecursiveAction a = new CheckedRecursiveAction() {
889 >            public void realCompute() {
890 >                FibAction f = new FibAction(8);
891 >                FibAction g = new FibAction(9);
892 >                FibAction h = new FibAction(7);
893 >                HashSet set = new HashSet();
894 >                set.add(f);
895 >                set.add(g);
896 >                set.add(h);
897 >                invokeAll(set);
898 >                assertTrue(f.isDone());
899 >                assertTrue(g.isDone());
900 >                assertTrue(h.isDone());
901 >                checkCompletedNormally(f);
902 >                assertEquals(21, f.result);
903 >                checkCompletedNormally(g);
904 >                assertEquals(34, g.result);
905 >                checkCompletedNormally(g);
906 >                assertEquals(13, h.result);
907 >            }};
908 >        testInvokeOnPool(mainPool(), a);
909      }
910  
911  
912 <    /**
912 >    /**
913       * invokeAll(tasks) with any null task throws NPE
914       */
915      public void testInvokeAllNPE() {
916 <        RecursiveAction a = new RecursiveAction() {
917 <                public void compute() {
918 <                    try {
919 <                        FibAction f = new FibAction(8);
920 <                        FibAction g = new FibAction(9);
921 <                        FibAction h = null;
922 <                        invokeAll(f, g, h);
923 <                        shouldThrow();
924 <                    } catch (NullPointerException success) {
925 <                    }
926 <                }
769 <            };
770 <        mainPool.invoke(a);
916 >        RecursiveAction a = new CheckedRecursiveAction() {
917 >            public void realCompute() {
918 >                FibAction f = new FibAction(8);
919 >                FibAction g = new FibAction(9);
920 >                FibAction h = null;
921 >                try {
922 >                    invokeAll(f, g, h);
923 >                    shouldThrow();
924 >                } catch (NullPointerException success) {}
925 >            }};
926 >        testInvokeOnPool(mainPool(), a);
927      }
928  
929 <    /**
929 >    /**
930       * invokeAll(t1, t2) throw exception if any task does
931       */
932      public void testAbnormalInvokeAll2() {
933 <        RecursiveAction a = new RecursiveAction() {
934 <                public void compute() {
935 <                    try {
936 <                        FibAction f = new FibAction(8);
937 <                        FailingFibAction g = new FailingFibAction(9);
938 <                        invokeAll(f, g);
939 <                        shouldThrow();
940 <                    } catch(FJException success) {
941 <                    }
933 >        RecursiveAction a = new CheckedRecursiveAction() {
934 >            public void realCompute() {
935 >                FibAction f = new FibAction(8);
936 >                FailingFibAction g = new FailingFibAction(9);
937 >                try {
938 >                    invokeAll(f, g);
939 >                    shouldThrow();
940 >                } catch (FJException success) {
941 >                    checkCompletedAbnormally(g, success);
942                  }
943 <            };
944 <        mainPool.invoke(a);
943 >            }};
944 >        testInvokeOnPool(mainPool(), a);
945      }
946  
947 <    /**
947 >    /**
948       * invokeAll(tasks) with 1 argument throws exception if task does
949       */
950      public void testAbnormalInvokeAll1() {
951 <        RecursiveAction a = new RecursiveAction() {
952 <                public void compute() {
953 <                    try {
954 <                        FailingFibAction g = new FailingFibAction(9);
955 <                        invokeAll(g);
956 <                        shouldThrow();
957 <                    } catch(FJException success) {
958 <                    }
951 >        RecursiveAction a = new CheckedRecursiveAction() {
952 >            public void realCompute() {
953 >                FailingFibAction g = new FailingFibAction(9);
954 >                try {
955 >                    invokeAll(g);
956 >                    shouldThrow();
957 >                } catch (FJException success) {
958 >                    checkCompletedAbnormally(g, success);
959                  }
960 <            };
961 <        mainPool.invoke(a);
960 >            }};
961 >        testInvokeOnPool(mainPool(), a);
962      }
963  
964 <    /**
964 >    /**
965       * invokeAll(tasks) with > 2 argument throws exception if any task does
966       */
967      public void testAbnormalInvokeAll3() {
968 <        RecursiveAction a = new RecursiveAction() {
969 <                public void compute() {
970 <                    try {
971 <                        FibAction f = new FibAction(8);
972 <                        FailingFibAction g = new FailingFibAction(9);
973 <                        FibAction h = new FibAction(7);
974 <                        invokeAll(f, g, h);
975 <                        shouldThrow();
976 <                    } catch(FJException success) {
977 <                    }
968 >        RecursiveAction a = new CheckedRecursiveAction() {
969 >            public void realCompute() {
970 >                FibAction f = new FibAction(8);
971 >                FailingFibAction g = new FailingFibAction(9);
972 >                FibAction h = new FibAction(7);
973 >                try {
974 >                    invokeAll(f, g, h);
975 >                    shouldThrow();
976 >                } catch (FJException success) {
977 >                    checkCompletedAbnormally(g, success);
978                  }
979 <            };
980 <        mainPool.invoke(a);
979 >            }};
980 >        testInvokeOnPool(mainPool(), a);
981      }
982  
983 <    /**
984 <     * invokeAll(collection)  throws exception if any task does
983 >    /**
984 >     * invokeAll(collection) throws exception if any task does
985       */
986      public void testAbnormalInvokeAllCollection() {
987 <        RecursiveAction a = new RecursiveAction() {
988 <                public void compute() {
989 <                    try {
990 <                        FailingFibAction f = new FailingFibAction(8);
991 <                        FibAction g = new FibAction(9);
992 <                        FibAction h = new FibAction(7);
993 <                        HashSet set = new HashSet();
994 <                        set.add(f);
995 <                        set.add(g);
996 <                        set.add(h);
997 <                        invokeAll(set);
998 <                        shouldThrow();
999 <                    } catch(FJException success) {
1000 <                    }
987 >        RecursiveAction a = new CheckedRecursiveAction() {
988 >            public void realCompute() {
989 >                FailingFibAction f = new FailingFibAction(8);
990 >                FibAction g = new FibAction(9);
991 >                FibAction h = new FibAction(7);
992 >                HashSet set = new HashSet();
993 >                set.add(f);
994 >                set.add(g);
995 >                set.add(h);
996 >                try {
997 >                    invokeAll(set);
998 >                    shouldThrow();
999 >                } catch (FJException success) {
1000 >                    checkCompletedAbnormally(f, success);
1001                  }
1002 <            };
1003 <        mainPool.invoke(a);
1002 >            }};
1003 >        testInvokeOnPool(mainPool(), a);
1004      }
1005  
1006 <    /**
1006 >    /**
1007       * tryUnfork returns true for most recent unexecuted task,
1008       * and suppresses execution
1009       */
1010      public void testTryUnfork() {
1011 <        RecursiveAction a = new RecursiveAction() {
1012 <                public void compute() {
1013 <                    FibAction g = new FibAction(9);
1014 <                    g.fork();
1015 <                    FibAction f = new FibAction(8);
1016 <                    f.fork();
1017 <                    threadAssertTrue(f.tryUnfork());
1018 <                    helpQuiesce();
1019 <                    threadAssertFalse(f.isDone());
1020 <                    threadAssertTrue(g.isDone());
1021 <                }
1022 <            };
867 <        singletonPool.invoke(a);
1011 >        RecursiveAction a = new CheckedRecursiveAction() {
1012 >            public void realCompute() {
1013 >                FibAction g = new FibAction(9);
1014 >                assertSame(g, g.fork());
1015 >                FibAction f = new FibAction(8);
1016 >                assertSame(f, f.fork());
1017 >                assertTrue(f.tryUnfork());
1018 >                helpQuiesce();
1019 >                checkNotDone(f);
1020 >                checkCompletedNormally(g);
1021 >            }};
1022 >        testInvokeOnPool(singletonPool(), a);
1023      }
1024  
1025 <    /**
1025 >    /**
1026       * getSurplusQueuedTaskCount returns > 0 when
1027       * there are more tasks than threads
1028       */
1029      public void testGetSurplusQueuedTaskCount() {
1030 <        RecursiveAction a = new RecursiveAction() {
1031 <                public void compute() {
1032 <                    FibAction h = new FibAction(7);
1033 <                    h.fork();
1034 <                    FibAction g = new FibAction(9);
1035 <                    g.fork();
1036 <                    FibAction f = new FibAction(8);
1037 <                    f.fork();
1038 <                    threadAssertTrue(getSurplusQueuedTaskCount() > 0);
1039 <                    helpQuiesce();
1040 <                }
1041 <            };
1042 <        singletonPool.invoke(a);
1030 >        RecursiveAction a = new CheckedRecursiveAction() {
1031 >            public void realCompute() {
1032 >                FibAction h = new FibAction(7);
1033 >                assertSame(h, h.fork());
1034 >                FibAction g = new FibAction(9);
1035 >                assertSame(g, g.fork());
1036 >                FibAction f = new FibAction(8);
1037 >                assertSame(f, f.fork());
1038 >                assertTrue(getSurplusQueuedTaskCount() > 0);
1039 >                helpQuiesce();
1040 >                assertEquals(0, getSurplusQueuedTaskCount());
1041 >                checkCompletedNormally(f);
1042 >                checkCompletedNormally(g);
1043 >                checkCompletedNormally(h);
1044 >            }};
1045 >        testInvokeOnPool(singletonPool(), a);
1046      }
1047  
1048 <    /**
1048 >    /**
1049       * peekNextLocalTask returns most recent unexecuted task.
1050       */
1051      public void testPeekNextLocalTask() {
1052 <        RecursiveAction a = new RecursiveAction() {
1053 <                public void compute() {
1054 <                    FibAction g = new FibAction(9);
1055 <                    g.fork();
1056 <                    FibAction f = new FibAction(8);
1057 <                    f.fork();
1058 <                    threadAssertTrue(peekNextLocalTask() == f);
1059 <                    f.join();
1060 <                    threadAssertTrue(f.isDone());
1061 <                    helpQuiesce();
1062 <                }
1063 <            };
1064 <        singletonPool.invoke(a);
1052 >        RecursiveAction a = new CheckedRecursiveAction() {
1053 >            public void realCompute() {
1054 >                FibAction g = new FibAction(9);
1055 >                assertSame(g, g.fork());
1056 >                FibAction f = new FibAction(8);
1057 >                assertSame(f, f.fork());
1058 >                assertSame(f, peekNextLocalTask());
1059 >                assertNull(f.join());
1060 >                checkCompletedNormally(f);
1061 >                helpQuiesce();
1062 >                checkCompletedNormally(f);
1063 >                checkCompletedNormally(g);
1064 >            }};
1065 >        testInvokeOnPool(singletonPool(), a);
1066      }
1067  
1068 <    /**
1068 >    /**
1069       * pollNextLocalTask returns most recent unexecuted task
1070       * without executing it
1071       */
1072      public void testPollNextLocalTask() {
1073 <        RecursiveAction a = new RecursiveAction() {
1074 <                public void compute() {
1075 <                    FibAction g = new FibAction(9);
1076 <                    g.fork();
1077 <                    FibAction f = new FibAction(8);
1078 <                    f.fork();
1079 <                    threadAssertTrue(pollNextLocalTask() == f);
1080 <                    helpQuiesce();
1081 <                    threadAssertFalse(f.isDone());
1082 <                }
1083 <            };
1084 <        singletonPool.invoke(a);
1073 >        RecursiveAction a = new CheckedRecursiveAction() {
1074 >            public void realCompute() {
1075 >                FibAction g = new FibAction(9);
1076 >                assertSame(g, g.fork());
1077 >                FibAction f = new FibAction(8);
1078 >                assertSame(f, f.fork());
1079 >                assertSame(f, pollNextLocalTask());
1080 >                helpQuiesce();
1081 >                checkNotDone(f);
1082 >                checkCompletedNormally(g);
1083 >            }};
1084 >        testInvokeOnPool(singletonPool(), a);
1085      }
1086  
1087 <    /**
1088 <     * pollTask returns an unexecuted task
930 <     * without executing it
1087 >    /**
1088 >     * pollTask returns an unexecuted task without executing it
1089       */
1090      public void testPollTask() {
1091 <        RecursiveAction a = new RecursiveAction() {
1092 <                public void compute() {
1093 <                    FibAction g = new FibAction(9);
1094 <                    g.fork();
1095 <                    FibAction f = new FibAction(8);
1096 <                    f.fork();
1097 <                    threadAssertTrue(pollTask() == f);
1098 <                    helpQuiesce();
1099 <                    threadAssertFalse(f.isDone());
1100 <                    threadAssertTrue(g.isDone());
1101 <                }
1102 <            };
945 <        singletonPool.invoke(a);
1091 >        RecursiveAction a = new CheckedRecursiveAction() {
1092 >            public void realCompute() {
1093 >                FibAction g = new FibAction(9);
1094 >                assertSame(g, g.fork());
1095 >                FibAction f = new FibAction(8);
1096 >                assertSame(f, f.fork());
1097 >                assertSame(f, pollTask());
1098 >                helpQuiesce();
1099 >                checkNotDone(f);
1100 >                checkCompletedNormally(g);
1101 >            }};
1102 >        testInvokeOnPool(singletonPool(), a);
1103      }
1104  
1105 <    /**
1105 >    /**
1106       * peekNextLocalTask returns least recent unexecuted task in async mode
1107       */
1108      public void testPeekNextLocalTaskAsync() {
1109 <        RecursiveAction a = new RecursiveAction() {
1110 <                public void compute() {
1111 <                    FibAction g = new FibAction(9);
1112 <                    g.fork();
1113 <                    FibAction f = new FibAction(8);
1114 <                    f.fork();
1115 <                    threadAssertTrue(peekNextLocalTask() == g);
1116 <                    f.join();
1117 <                    helpQuiesce();
1118 <                    threadAssertTrue(f.isDone());
1119 <                }
1120 <            };
1121 <        asyncSingletonPool.invoke(a);
1109 >        RecursiveAction a = new CheckedRecursiveAction() {
1110 >            public void realCompute() {
1111 >                FibAction g = new FibAction(9);
1112 >                assertSame(g, g.fork());
1113 >                FibAction f = new FibAction(8);
1114 >                assertSame(f, f.fork());
1115 >                assertSame(g, peekNextLocalTask());
1116 >                assertNull(f.join());
1117 >                helpQuiesce();
1118 >                checkCompletedNormally(f);
1119 >                checkCompletedNormally(g);
1120 >            }};
1121 >        testInvokeOnPool(asyncSingletonPool(), a);
1122      }
1123  
1124 <    /**
1125 <     * pollNextLocalTask returns least recent unexecuted task
1126 <     * without executing it, in async mode
1124 >    /**
1125 >     * pollNextLocalTask returns least recent unexecuted task without
1126 >     * executing it, in async mode
1127       */
1128      public void testPollNextLocalTaskAsync() {
1129 <        RecursiveAction a = new RecursiveAction() {
1130 <                public void compute() {
1131 <                    FibAction g = new FibAction(9);
1132 <                    g.fork();
1133 <                    FibAction f = new FibAction(8);
1134 <                    f.fork();
1135 <                    threadAssertTrue(pollNextLocalTask() == g);
1136 <                    helpQuiesce();
1137 <                    threadAssertTrue(f.isDone());
1138 <                    threadAssertFalse(g.isDone());
1139 <                }
1140 <            };
1141 <        asyncSingletonPool.invoke(a);
1142 <    }
1143 <
1144 <    /**
1145 <     * pollTask returns an unexecuted task
989 <     * without executing it, in async mode
1129 >        RecursiveAction a = new CheckedRecursiveAction() {
1130 >            public void realCompute() {
1131 >                FibAction g = new FibAction(9);
1132 >                assertSame(g, g.fork());
1133 >                FibAction f = new FibAction(8);
1134 >                assertSame(f, f.fork());
1135 >                assertSame(g, pollNextLocalTask());
1136 >                helpQuiesce();
1137 >                checkCompletedNormally(f);
1138 >                checkNotDone(g);
1139 >            }};
1140 >        testInvokeOnPool(asyncSingletonPool(), a);
1141 >    }
1142 >
1143 >    /**
1144 >     * pollTask returns an unexecuted task without executing it, in
1145 >     * async mode
1146       */
1147      public void testPollTaskAsync() {
1148 <        RecursiveAction a = new RecursiveAction() {
1149 <                public void compute() {
1150 <                    FibAction g = new FibAction(9);
1151 <                    g.fork();
1152 <                    FibAction f = new FibAction(8);
1153 <                    f.fork();
1154 <                    threadAssertTrue(pollTask() == g);
1155 <                    helpQuiesce();
1156 <                    threadAssertTrue(f.isDone());
1157 <                    threadAssertFalse(g.isDone());
1158 <                }
1159 <            };
1004 <        asyncSingletonPool.invoke(a);
1148 >        RecursiveAction a = new CheckedRecursiveAction() {
1149 >            public void realCompute() {
1150 >                FibAction g = new FibAction(9);
1151 >                assertSame(g, g.fork());
1152 >                FibAction f = new FibAction(8);
1153 >                assertSame(f, f.fork());
1154 >                assertSame(g, pollTask());
1155 >                helpQuiesce();
1156 >                checkCompletedNormally(f);
1157 >                checkNotDone(g);
1158 >            }};
1159 >        testInvokeOnPool(asyncSingletonPool(), a);
1160      }
1161  
1162   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines