ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveTaskTest.java
Revision: 1.13
Committed: Mon Sep 13 07:51:18 2010 UTC (13 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.12: +64 -48 lines
Log Message:
avoid static ForkJoinPools; create a new pool for each test instead

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