ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveTaskTest.java
Revision: 1.19
Committed: Thu Sep 16 00:52:49 2010 UTC (13 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.18: +297 -356 lines
Log Message:
testcase hygiene: introduce CheckedRecursiveAction and CheckedRecursiveTask; eliminate almost all threadAssertXXX; use preferred junit conventions;narrow the scope of exception checking code; make sure test failures in non-junit threads produce proper stacktraces

File Contents

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