ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveTaskTest.java
Revision: 1.11
Committed: Wed Sep 1 06:41:55 2010 UTC (13 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.10: +2 -2 lines
Log Message:
trailing whitespace

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