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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines