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.1 by dl, Fri Jul 31 23:02:50 2009 UTC vs.
Revision 1.36 by jsr166, Mon May 29 19:15:02 2017 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines