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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines