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.12 by jsr166, Sat Sep 11 07:31:52 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines