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.7 by jsr166, Wed Aug 5 01:17:30 2009 UTC vs.
Revision 1.26 by jsr166, Tue Nov 23 07:38:40 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines