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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines