ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinTaskTest.java
Revision: 1.7
Committed: Tue Dec 1 23:01:44 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.6: +3 -3 lines
Log Message:
replace 5 seconds absolute timeout with LONG_DELAY_MS

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.concurrent.atomic.*;
9     import java.util.*;
10    
11     public class ForkJoinTaskTest extends JSR166TestCase {
12    
13     public static void main(String[] args) {
14 jsr166 1.5 junit.textui.TestRunner.run (suite());
15 dl 1.1 }
16     public static Test suite() {
17 jsr166 1.5 return new TestSuite(ForkJoinTaskTest.class);
18 dl 1.1 }
19    
20     /**
21     * Testing coverage notes:
22 jsr166 1.2 *
23 dl 1.1 * To test extension methods and overrides, most tests use
24     * BinaryAsyncAction extension class that processes joins
25     * differently than supplied Recursive forms.
26 jsr166 1.2 */
27 dl 1.1
28     static final ForkJoinPool mainPool = new ForkJoinPool();
29     static final ForkJoinPool singletonPool = new ForkJoinPool(1);
30     static final ForkJoinPool asyncSingletonPool = new ForkJoinPool(1);
31     static {
32     asyncSingletonPool.setAsyncMode(true);
33     }
34    
35     static final class FJException extends RuntimeException {
36     FJException() { super(); }
37     }
38    
39     static abstract class BinaryAsyncAction extends ForkJoinTask<Void> {
40     private volatile int controlState;
41    
42     static final AtomicIntegerFieldUpdater<BinaryAsyncAction> controlStateUpdater =
43 jsr166 1.2 AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class,
44 dl 1.1 "controlState");
45    
46     private BinaryAsyncAction parent;
47    
48     private BinaryAsyncAction sibling;
49    
50     protected BinaryAsyncAction() {
51     }
52    
53     public final Void getRawResult() { return null; }
54     protected final void setRawResult(Void mustBeNull) { }
55    
56     public final void linkSubtasks(BinaryAsyncAction x, BinaryAsyncAction y) {
57     x.parent = y.parent = this;
58     x.sibling = y;
59     y.sibling = x;
60     }
61    
62     protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) {
63     }
64    
65     protected boolean onException() {
66     return true;
67     }
68    
69     public void linkAndForkSubtasks(BinaryAsyncAction x, BinaryAsyncAction y) {
70     linkSubtasks(x, y);
71     y.fork();
72     x.fork();
73     }
74    
75     private void completeThis() {
76     super.complete(null);
77     }
78    
79     private void completeThisExceptionally(Throwable ex) {
80     super.completeExceptionally(ex);
81     }
82    
83     public final void complete() {
84     BinaryAsyncAction a = this;
85     for (;;) {
86     BinaryAsyncAction s = a.sibling;
87     BinaryAsyncAction p = a.parent;
88     a.sibling = null;
89     a.parent = null;
90     a.completeThis();
91     if (p == null || p.compareAndSetControlState(0, 1))
92     break;
93     try {
94     p.onComplete(a, s);
95 jsr166 1.2 } catch (Throwable rex) {
96 dl 1.1 p.completeExceptionally(rex);
97     return;
98     }
99     a = p;
100     }
101     }
102    
103     public final void completeExceptionally(Throwable ex) {
104     BinaryAsyncAction a = this;
105     while (!a.isCompletedAbnormally()) {
106     a.completeThisExceptionally(ex);
107     BinaryAsyncAction s = a.sibling;
108     if (s != null)
109     s.cancel(false);
110     if (!a.onException() || (a = a.parent) == null)
111     break;
112     }
113     }
114    
115     public final BinaryAsyncAction getParent() {
116     return parent;
117     }
118    
119     public BinaryAsyncAction getSibling() {
120     return sibling;
121     }
122    
123     public void reinitialize() {
124     parent = sibling = null;
125     super.reinitialize();
126     }
127    
128     protected final int getControlState() {
129     return controlState;
130     }
131    
132 jsr166 1.2 protected final boolean compareAndSetControlState(int expect,
133 dl 1.1 int update) {
134     return controlStateUpdater.compareAndSet(this, expect, update);
135     }
136    
137     protected final void setControlState(int value) {
138     controlState = value;
139     }
140    
141     protected final void incrementControlState() {
142     controlStateUpdater.incrementAndGet(this);
143     }
144    
145     protected final void decrementControlState() {
146     controlStateUpdater.decrementAndGet(this);
147     }
148    
149     }
150    
151 jsr166 1.6 static final class AsyncFib extends BinaryAsyncAction {
152 dl 1.1 int number;
153 jsr166 1.2 public AsyncFib(int n) {
154 dl 1.1 this.number = n;
155     }
156 jsr166 1.2
157 dl 1.1 public final boolean exec() {
158     AsyncFib f = this;
159     int n = f.number;
160     if (n > 1) {
161     while (n > 1) {
162     AsyncFib p = f;
163     AsyncFib r = new AsyncFib(n - 2);
164     f = new AsyncFib(--n);
165     p.linkSubtasks(r, f);
166     r.fork();
167     }
168     f.number = n;
169     }
170     f.complete();
171     return false;
172     }
173    
174     protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) {
175     number = ((AsyncFib)x).number + ((AsyncFib)y).number;
176     }
177     }
178    
179    
180 jsr166 1.6 static final class FailingAsyncFib extends BinaryAsyncAction {
181 dl 1.1 int number;
182 jsr166 1.2 public FailingAsyncFib(int n) {
183 dl 1.1 this.number = n;
184     }
185 jsr166 1.2
186 dl 1.1 public final boolean exec() {
187     FailingAsyncFib f = this;
188     int n = f.number;
189     if (n > 1) {
190     while (n > 1) {
191     FailingAsyncFib p = f;
192     FailingAsyncFib r = new FailingAsyncFib(n - 2);
193     f = new FailingAsyncFib(--n);
194     p.linkSubtasks(r, f);
195     r.fork();
196     }
197     f.number = n;
198     }
199     f.complete();
200     return false;
201     }
202    
203     protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) {
204     completeExceptionally(new FJException());
205     }
206     }
207    
208 jsr166 1.2 /**
209 dl 1.1 * invoke returns when task completes normally.
210     * isCompletedAbnormally and isCancelled return false for normally
211     * completed tasks. getRawResult of a RecursiveAction returns null;
212 jsr166 1.2 *
213 dl 1.1 */
214     public void testInvoke() {
215     RecursiveAction a = new RecursiveAction() {
216     public void compute() {
217     AsyncFib f = new AsyncFib(8);
218     f.invoke();
219     threadAssertTrue(f.number == 21);
220     threadAssertTrue(f.isDone());
221     threadAssertFalse(f.isCancelled());
222     threadAssertFalse(f.isCompletedAbnormally());
223     threadAssertTrue(f.getRawResult() == null);
224     }
225     };
226     mainPool.invoke(a);
227     }
228    
229 jsr166 1.2 /**
230 dl 1.1 * quietlyInvoke task returns when task completes normally.
231     * isCompletedAbnormally and isCancelled return false for normally
232     * completed tasks
233     */
234     public void testQuietlyInvoke() {
235     RecursiveAction a = new RecursiveAction() {
236     public void compute() {
237     AsyncFib f = new AsyncFib(8);
238     f.quietlyInvoke();
239     threadAssertTrue(f.number == 21);
240     threadAssertTrue(f.isDone());
241     threadAssertFalse(f.isCancelled());
242     threadAssertFalse(f.isCompletedAbnormally());
243     threadAssertTrue(f.getRawResult() == null);
244     }
245     };
246     mainPool.invoke(a);
247     }
248    
249 jsr166 1.2 /**
250 dl 1.1 * join of a forked task returns when task completes
251     */
252     public void testForkJoin() {
253     RecursiveAction a = new RecursiveAction() {
254     public void compute() {
255     AsyncFib f = new AsyncFib(8);
256     f.fork();
257     f.join();
258     threadAssertTrue(f.number == 21);
259     threadAssertTrue(f.isDone());
260     threadAssertTrue(f.getRawResult() == null);
261     }
262     };
263     mainPool.invoke(a);
264     }
265    
266 jsr166 1.2 /**
267 dl 1.1 * get of a forked task returns when task completes
268     */
269     public void testForkGet() {
270     RecursiveAction a = new RecursiveAction() {
271     public void compute() {
272     try {
273     AsyncFib f = new AsyncFib(8);
274     f.fork();
275     f.get();
276     threadAssertTrue(f.number == 21);
277     threadAssertTrue(f.isDone());
278 jsr166 1.2 } catch (Exception ex) {
279 jsr166 1.4 unexpectedException(ex);
280 dl 1.1 }
281     }
282     };
283     mainPool.invoke(a);
284     }
285    
286 jsr166 1.2 /**
287 dl 1.1 * timed get of a forked task returns when task completes
288     */
289     public void testForkTimedGet() {
290     RecursiveAction a = new RecursiveAction() {
291     public void compute() {
292     try {
293     AsyncFib f = new AsyncFib(8);
294     f.fork();
295 jsr166 1.7 f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
296 dl 1.1 threadAssertTrue(f.number == 21);
297     threadAssertTrue(f.isDone());
298 jsr166 1.2 } catch (Exception ex) {
299 jsr166 1.4 unexpectedException(ex);
300 dl 1.1 }
301     }
302     };
303     mainPool.invoke(a);
304     }
305    
306 jsr166 1.2 /**
307 dl 1.1 * timed get with null time unit throws NPE
308     */
309     public void testForkTimedGetNPE() {
310     RecursiveAction a = new RecursiveAction() {
311     public void compute() {
312     try {
313     AsyncFib f = new AsyncFib(8);
314     f.fork();
315     f.get(5L, null);
316 jsr166 1.4 shouldThrow();
317 jsr166 1.2 } catch (NullPointerException success) {
318     } catch (Exception ex) {
319 jsr166 1.4 unexpectedException(ex);
320 dl 1.1 }
321     }
322     };
323     mainPool.invoke(a);
324     }
325    
326 jsr166 1.2 /**
327 dl 1.1 * helpJoin of a forked task returns when task completes
328     */
329     public void testForkHelpJoin() {
330     RecursiveAction a = new RecursiveAction() {
331     public void compute() {
332     AsyncFib f = new AsyncFib(8);
333     f.fork();
334     f.helpJoin();
335     threadAssertTrue(f.number == 21);
336     threadAssertTrue(f.isDone());
337     }
338     };
339     mainPool.invoke(a);
340     }
341    
342 jsr166 1.2 /**
343 dl 1.1 * quietlyJoin of a forked task returns when task completes
344     */
345     public void testForkQuietlyJoin() {
346     RecursiveAction a = new RecursiveAction() {
347     public void compute() {
348     AsyncFib f = new AsyncFib(8);
349     f.fork();
350     f.quietlyJoin();
351     threadAssertTrue(f.number == 21);
352     threadAssertTrue(f.isDone());
353     }
354     };
355     mainPool.invoke(a);
356     }
357    
358    
359 jsr166 1.2 /**
360 dl 1.1 * quietlyHelpJoin of a forked task returns when task completes
361     */
362     public void testForkQuietlyHelpJoin() {
363     RecursiveAction a = new RecursiveAction() {
364     public void compute() {
365     AsyncFib f = new AsyncFib(8);
366     f.fork();
367     f.quietlyHelpJoin();
368     threadAssertTrue(f.number == 21);
369     threadAssertTrue(f.isDone());
370     }
371     };
372     mainPool.invoke(a);
373     }
374    
375    
376 jsr166 1.2 /**
377 dl 1.1 * helpQuiesce returns when tasks are complete.
378     * getQueuedTaskCount returns 0 when quiescent
379     */
380     public void testForkHelpQuiesce() {
381     RecursiveAction a = new RecursiveAction() {
382     public void compute() {
383     AsyncFib f = new AsyncFib(8);
384     f.fork();
385     f.helpQuiesce();
386     threadAssertTrue(f.number == 21);
387     threadAssertTrue(f.isDone());
388     threadAssertTrue(getQueuedTaskCount() == 0);
389     }
390     };
391     mainPool.invoke(a);
392     }
393    
394    
395 jsr166 1.2 /**
396 dl 1.1 * invoke task throws exception when task completes abnormally
397     */
398     public void testAbnormalInvoke() {
399     RecursiveAction a = new RecursiveAction() {
400     public void compute() {
401     try {
402     FailingAsyncFib f = new FailingAsyncFib(8);
403     f.invoke();
404     shouldThrow();
405 jsr166 1.2 } catch (FJException success) {
406 dl 1.1 }
407     }
408     };
409     mainPool.invoke(a);
410     }
411    
412 jsr166 1.2 /**
413 jsr166 1.3 * quietlyInvoke task returns when task completes abnormally
414 dl 1.1 */
415     public void testAbnormalQuietlyInvoke() {
416     RecursiveAction a = new RecursiveAction() {
417     public void compute() {
418     FailingAsyncFib f = new FailingAsyncFib(8);
419     f.quietlyInvoke();
420     threadAssertTrue(f.isDone());
421     }
422     };
423     mainPool.invoke(a);
424     }
425    
426 jsr166 1.2 /**
427 dl 1.1 * join of a forked task throws exception when task completes abnormally
428     */
429     public void testAbnormalForkJoin() {
430     RecursiveAction a = new RecursiveAction() {
431     public void compute() {
432     try {
433     FailingAsyncFib f = new FailingAsyncFib(8);
434     f.fork();
435     f.join();
436     shouldThrow();
437 jsr166 1.2 } catch (FJException success) {
438 dl 1.1 }
439     }
440     };
441     mainPool.invoke(a);
442     }
443    
444 jsr166 1.2 /**
445 dl 1.1 * get of a forked task throws exception when task completes abnormally
446     */
447     public void testAbnormalForkGet() {
448     RecursiveAction a = new RecursiveAction() {
449     public void compute() {
450     try {
451     FailingAsyncFib f = new FailingAsyncFib(8);
452     f.fork();
453     f.get();
454     shouldThrow();
455 jsr166 1.4 } catch (ExecutionException success) {
456     } catch (Exception ex) {
457     unexpectedException(ex);
458 dl 1.1 }
459     }
460     };
461     mainPool.invoke(a);
462     }
463    
464 jsr166 1.2 /**
465 dl 1.1 * timed get of a forked task throws exception when task completes abnormally
466     */
467     public void testAbnormalForkTimedGet() {
468     RecursiveAction a = new RecursiveAction() {
469     public void compute() {
470     try {
471     FailingAsyncFib f = new FailingAsyncFib(8);
472     f.fork();
473 jsr166 1.7 f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
474 dl 1.1 shouldThrow();
475 jsr166 1.4 } catch (ExecutionException success) {
476     } catch (Exception ex) {
477     unexpectedException(ex);
478 dl 1.1 }
479     }
480     };
481     mainPool.invoke(a);
482     }
483    
484 jsr166 1.2 /**
485 dl 1.1 * join of a forked task throws exception when task completes abnormally
486     */
487     public void testAbnormalForkHelpJoin() {
488     RecursiveAction a = new RecursiveAction() {
489     public void compute() {
490     try {
491     FailingAsyncFib f = new FailingAsyncFib(8);
492     f.fork();
493     f.helpJoin();
494     shouldThrow();
495 jsr166 1.2 } catch (FJException success) {
496 dl 1.1 }
497     }
498     };
499     mainPool.invoke(a);
500     }
501    
502 jsr166 1.2 /**
503 dl 1.1 * quietlyHelpJoin of a forked task returns when task completes abnormally.
504     * getException of failed task returns its exception.
505     * isCompletedAbnormally of a failed task returns true.
506     * isCancelled of a failed uncancelled task returns false
507     */
508     public void testAbnormalForkQuietlyHelpJoin() {
509     RecursiveAction a = new RecursiveAction() {
510     public void compute() {
511     FailingAsyncFib f = new FailingAsyncFib(8);
512     f.fork();
513     f.quietlyHelpJoin();
514     threadAssertTrue(f.isDone());
515     threadAssertTrue(f.isCompletedAbnormally());
516     threadAssertFalse(f.isCancelled());
517     threadAssertTrue(f.getException() instanceof FJException);
518     }
519     };
520     mainPool.invoke(a);
521     }
522    
523 jsr166 1.2 /**
524 dl 1.1 * quietlyJoin of a forked task returns when task completes abnormally
525     */
526     public void testAbnormalForkQuietlyJoin() {
527     RecursiveAction a = new RecursiveAction() {
528     public void compute() {
529     FailingAsyncFib f = new FailingAsyncFib(8);
530     f.fork();
531     f.quietlyJoin();
532     threadAssertTrue(f.isDone());
533     threadAssertTrue(f.isCompletedAbnormally());
534     threadAssertTrue(f.getException() instanceof FJException);
535     }
536     };
537     mainPool.invoke(a);
538     }
539    
540 jsr166 1.2 /**
541 dl 1.1 * invoke task throws exception when task cancelled
542     */
543     public void testCancelledInvoke() {
544     RecursiveAction a = new RecursiveAction() {
545     public void compute() {
546     try {
547     AsyncFib f = new AsyncFib(8);
548     f.cancel(true);
549     f.invoke();
550     shouldThrow();
551 jsr166 1.2 } catch (CancellationException success) {
552 dl 1.1 }
553     }
554     };
555     mainPool.invoke(a);
556     }
557    
558 jsr166 1.2 /**
559 dl 1.1 * join of a forked task throws exception when task cancelled
560     */
561     public void testCancelledForkJoin() {
562     RecursiveAction a = new RecursiveAction() {
563     public void compute() {
564     try {
565     AsyncFib f = new AsyncFib(8);
566     f.cancel(true);
567     f.fork();
568     f.join();
569     shouldThrow();
570 jsr166 1.2 } catch (CancellationException success) {
571 dl 1.1 }
572     }
573     };
574     mainPool.invoke(a);
575     }
576    
577 jsr166 1.2 /**
578 dl 1.1 * get of a forked task throws exception when task cancelled
579     */
580     public void testCancelledForkGet() {
581     RecursiveAction a = new RecursiveAction() {
582     public void compute() {
583     try {
584     AsyncFib f = new AsyncFib(8);
585     f.cancel(true);
586     f.fork();
587     f.get();
588     shouldThrow();
589 jsr166 1.4 } catch (CancellationException success) {
590     } catch (Exception ex) {
591     unexpectedException(ex);
592 dl 1.1 }
593     }
594     };
595     mainPool.invoke(a);
596     }
597    
598 jsr166 1.2 /**
599 dl 1.1 * timed get of a forked task throws exception when task cancelled
600     */
601     public void testCancelledForkTimedGet() {
602     RecursiveAction a = new RecursiveAction() {
603     public void compute() {
604     try {
605     AsyncFib f = new AsyncFib(8);
606     f.cancel(true);
607     f.fork();
608 jsr166 1.7 f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
609 dl 1.1 shouldThrow();
610 jsr166 1.4 } catch (CancellationException success) {
611     } catch (Exception ex) {
612     unexpectedException(ex);
613 dl 1.1 }
614     }
615     };
616     mainPool.invoke(a);
617     }
618    
619 jsr166 1.2 /**
620 dl 1.1 * join of a forked task throws exception when task cancelled
621     */
622     public void testCancelledForkHelpJoin() {
623     RecursiveAction a = new RecursiveAction() {
624     public void compute() {
625     try {
626     AsyncFib f = new AsyncFib(8);
627     f.cancel(true);
628     f.fork();
629     f.helpJoin();
630     shouldThrow();
631 jsr166 1.2 } catch (CancellationException success) {
632 dl 1.1 }
633     }
634     };
635     mainPool.invoke(a);
636     }
637    
638 jsr166 1.2 /**
639 dl 1.1 * quietlyHelpJoin of a forked task returns when task cancelled.
640     * getException of cancelled task returns its exception.
641     * isCompletedAbnormally of a cancelled task returns true.
642     * isCancelled of a cancelled task returns true
643     */
644     public void testCancelledForkQuietlyHelpJoin() {
645     RecursiveAction a = new RecursiveAction() {
646     public void compute() {
647     AsyncFib f = new AsyncFib(8);
648     f.cancel(true);
649     f.fork();
650     f.quietlyHelpJoin();
651     threadAssertTrue(f.isDone());
652     threadAssertTrue(f.isCompletedAbnormally());
653     threadAssertTrue(f.isCancelled());
654     threadAssertTrue(f.getException() instanceof CancellationException);
655     }
656     };
657     mainPool.invoke(a);
658     }
659    
660 jsr166 1.2 /**
661 dl 1.1 * quietlyJoin of a forked task returns when task cancelled
662     */
663     public void testCancelledForkQuietlyJoin() {
664     RecursiveAction a = new RecursiveAction() {
665     public void compute() {
666     AsyncFib f = new AsyncFib(8);
667     f.cancel(true);
668     f.fork();
669     f.quietlyJoin();
670     threadAssertTrue(f.isDone());
671     threadAssertTrue(f.isCompletedAbnormally());
672     threadAssertTrue(f.getException() instanceof CancellationException);
673     }
674     };
675     mainPool.invoke(a);
676     }
677    
678     /**
679     * getPool of executing task returns its pool
680     */
681     public void testGetPool() {
682     RecursiveAction a = new RecursiveAction() {
683     public void compute() {
684     threadAssertTrue(getPool() == mainPool);
685     }
686     };
687     mainPool.invoke(a);
688     }
689    
690     /**
691     * getPool of non-FJ task returns null
692     */
693     public void testGetPool2() {
694     RecursiveAction a = new RecursiveAction() {
695     public void compute() {
696     threadAssertTrue(getPool() == null);
697     }
698     };
699     a.invoke();
700     }
701    
702     /**
703     * inForkJoinPool of executing task returns true
704     */
705     public void testInForkJoinPool() {
706     RecursiveAction a = new RecursiveAction() {
707     public void compute() {
708     threadAssertTrue(inForkJoinPool());
709     }
710     };
711     mainPool.invoke(a);
712     }
713    
714     /**
715     * inForkJoinPool of non-FJ task returns false
716     */
717     public void testInForkJoinPool2() {
718     RecursiveAction a = new RecursiveAction() {
719     public void compute() {
720     threadAssertTrue(!inForkJoinPool());
721     }
722     };
723     a.invoke();
724     }
725    
726     /**
727     * setRawResult(null) succeeds
728     */
729     public void testSetRawResult() {
730     RecursiveAction a = new RecursiveAction() {
731     public void compute() {
732     setRawResult(null);
733     }
734     };
735     a.invoke();
736     }
737    
738 jsr166 1.2 /**
739 dl 1.1 * invoke task throws exception after invoking completeExceptionally
740     */
741     public void testCompleteExceptionally() {
742     RecursiveAction a = new RecursiveAction() {
743     public void compute() {
744     try {
745     AsyncFib f = new AsyncFib(8);
746     f.completeExceptionally(new FJException());
747     f.invoke();
748     shouldThrow();
749 jsr166 1.2 } catch (FJException success) {
750 dl 1.1 }
751     }
752     };
753     mainPool.invoke(a);
754     }
755    
756 jsr166 1.2 /**
757 dl 1.1 * invokeAll(t1, t2) invokes all task arguments
758     */
759     public void testInvokeAll2() {
760     RecursiveAction a = new RecursiveAction() {
761     public void compute() {
762     AsyncFib f = new AsyncFib(8);
763     AsyncFib g = new AsyncFib(9);
764     invokeAll(f, g);
765     threadAssertTrue(f.isDone());
766     threadAssertTrue(f.number == 21);
767     threadAssertTrue(g.isDone());
768     threadAssertTrue(g.number == 34);
769     }
770     };
771     mainPool.invoke(a);
772     }
773    
774 jsr166 1.2 /**
775 dl 1.1 * invokeAll(tasks) with 1 argument invokes task
776     */
777     public void testInvokeAll1() {
778     RecursiveAction a = new RecursiveAction() {
779     public void compute() {
780     AsyncFib f = new AsyncFib(8);
781     invokeAll(f);
782     threadAssertTrue(f.isDone());
783     threadAssertTrue(f.number == 21);
784     }
785     };
786     mainPool.invoke(a);
787     }
788    
789 jsr166 1.2 /**
790 dl 1.1 * invokeAll(tasks) with > 2 argument invokes tasks
791     */
792     public void testInvokeAll3() {
793     RecursiveAction a = new RecursiveAction() {
794     public void compute() {
795     AsyncFib f = new AsyncFib(8);
796     AsyncFib g = new AsyncFib(9);
797     AsyncFib h = new AsyncFib(7);
798     invokeAll(f, g, h);
799     threadAssertTrue(f.isDone());
800     threadAssertTrue(f.number == 21);
801     threadAssertTrue(g.isDone());
802     threadAssertTrue(g.number == 34);
803     threadAssertTrue(h.isDone());
804     threadAssertTrue(h.number == 13);
805     }
806     };
807     mainPool.invoke(a);
808     }
809    
810 jsr166 1.2 /**
811 dl 1.1 * invokeAll(collection) invokes all tasks in the collection
812     */
813     public void testInvokeAllCollection() {
814     RecursiveAction a = new RecursiveAction() {
815     public void compute() {
816     AsyncFib f = new AsyncFib(8);
817     AsyncFib g = new AsyncFib(9);
818     AsyncFib h = new AsyncFib(7);
819     HashSet set = new HashSet();
820     set.add(f);
821     set.add(g);
822     set.add(h);
823     invokeAll(set);
824     threadAssertTrue(f.isDone());
825     threadAssertTrue(f.number == 21);
826     threadAssertTrue(g.isDone());
827     threadAssertTrue(g.number == 34);
828     threadAssertTrue(h.isDone());
829     threadAssertTrue(h.number == 13);
830     }
831     };
832     mainPool.invoke(a);
833     }
834    
835    
836 jsr166 1.2 /**
837 dl 1.1 * invokeAll(tasks) with any null task throws NPE
838     */
839     public void testInvokeAllNPE() {
840     RecursiveAction a = new RecursiveAction() {
841     public void compute() {
842     try {
843     AsyncFib f = new AsyncFib(8);
844     AsyncFib g = new AsyncFib(9);
845     AsyncFib h = null;
846     invokeAll(f, g, h);
847     shouldThrow();
848     } catch (NullPointerException success) {
849     }
850     }
851     };
852     mainPool.invoke(a);
853     }
854    
855 jsr166 1.2 /**
856 dl 1.1 * invokeAll(t1, t2) throw exception if any task does
857     */
858     public void testAbnormalInvokeAll2() {
859     RecursiveAction a = new RecursiveAction() {
860     public void compute() {
861     try {
862     AsyncFib f = new AsyncFib(8);
863     FailingAsyncFib g = new FailingAsyncFib(9);
864     invokeAll(f, g);
865     shouldThrow();
866 jsr166 1.2 } catch (FJException success) {
867 dl 1.1 }
868     }
869     };
870     mainPool.invoke(a);
871     }
872    
873 jsr166 1.2 /**
874 dl 1.1 * invokeAll(tasks) with 1 argument throws exception if task does
875     */
876     public void testAbnormalInvokeAll1() {
877     RecursiveAction a = new RecursiveAction() {
878     public void compute() {
879     try {
880     FailingAsyncFib g = new FailingAsyncFib(9);
881     invokeAll(g);
882     shouldThrow();
883 jsr166 1.2 } catch (FJException success) {
884 dl 1.1 }
885     }
886     };
887     mainPool.invoke(a);
888     }
889    
890 jsr166 1.2 /**
891 dl 1.1 * invokeAll(tasks) with > 2 argument throws exception if any task does
892     */
893     public void testAbnormalInvokeAll3() {
894     RecursiveAction a = new RecursiveAction() {
895     public void compute() {
896     try {
897     AsyncFib f = new AsyncFib(8);
898     FailingAsyncFib g = new FailingAsyncFib(9);
899     AsyncFib h = new AsyncFib(7);
900     invokeAll(f, g, h);
901     shouldThrow();
902 jsr166 1.2 } catch (FJException success) {
903 dl 1.1 }
904     }
905     };
906     mainPool.invoke(a);
907     }
908    
909 jsr166 1.2 /**
910 dl 1.1 * invokeAll(collection) throws exception if any task does
911     */
912     public void testAbnormalInvokeAllCollection() {
913     RecursiveAction a = new RecursiveAction() {
914     public void compute() {
915     try {
916     FailingAsyncFib f = new FailingAsyncFib(8);
917     AsyncFib g = new AsyncFib(9);
918     AsyncFib h = new AsyncFib(7);
919     HashSet set = new HashSet();
920     set.add(f);
921     set.add(g);
922     set.add(h);
923     invokeAll(set);
924     shouldThrow();
925 jsr166 1.2 } catch (FJException success) {
926 dl 1.1 }
927     }
928     };
929     mainPool.invoke(a);
930     }
931    
932 jsr166 1.2 /**
933 dl 1.1 * tryUnfork returns true for most recent unexecuted task,
934     * and suppresses execution
935     */
936     public void testTryUnfork() {
937     RecursiveAction a = new RecursiveAction() {
938     public void compute() {
939     AsyncFib g = new AsyncFib(9);
940     g.fork();
941     AsyncFib f = new AsyncFib(8);
942     f.fork();
943     threadAssertTrue(f.tryUnfork());
944     helpQuiesce();
945     threadAssertFalse(f.isDone());
946     threadAssertTrue(g.isDone());
947     }
948     };
949     singletonPool.invoke(a);
950     }
951    
952 jsr166 1.2 /**
953 dl 1.1 * getSurplusQueuedTaskCount returns > 0 when
954     * there are more tasks than threads
955     */
956     public void testGetSurplusQueuedTaskCount() {
957     RecursiveAction a = new RecursiveAction() {
958     public void compute() {
959     AsyncFib h = new AsyncFib(7);
960     h.fork();
961     AsyncFib g = new AsyncFib(9);
962     g.fork();
963     AsyncFib f = new AsyncFib(8);
964     f.fork();
965     threadAssertTrue(getSurplusQueuedTaskCount() > 0);
966     helpQuiesce();
967     }
968     };
969     singletonPool.invoke(a);
970     }
971    
972 jsr166 1.2 /**
973 dl 1.1 * peekNextLocalTask returns most recent unexecuted task.
974     */
975     public void testPeekNextLocalTask() {
976     RecursiveAction a = new RecursiveAction() {
977     public void compute() {
978     AsyncFib g = new AsyncFib(9);
979     g.fork();
980     AsyncFib f = new AsyncFib(8);
981     f.fork();
982     threadAssertTrue(peekNextLocalTask() == f);
983     f.join();
984     threadAssertTrue(f.isDone());
985     helpQuiesce();
986     }
987     };
988     singletonPool.invoke(a);
989     }
990    
991 jsr166 1.2 /**
992 dl 1.1 * pollNextLocalTask returns most recent unexecuted task
993     * without executing it
994     */
995     public void testPollNextLocalTask() {
996     RecursiveAction a = new RecursiveAction() {
997     public void compute() {
998     AsyncFib g = new AsyncFib(9);
999     g.fork();
1000     AsyncFib f = new AsyncFib(8);
1001     f.fork();
1002     threadAssertTrue(pollNextLocalTask() == f);
1003     helpQuiesce();
1004     threadAssertFalse(f.isDone());
1005     }
1006     };
1007     singletonPool.invoke(a);
1008     }
1009    
1010 jsr166 1.2 /**
1011 dl 1.1 * pollTask returns an unexecuted task
1012     * without executing it
1013     */
1014     public void testPollTask() {
1015     RecursiveAction a = new RecursiveAction() {
1016     public void compute() {
1017     AsyncFib g = new AsyncFib(9);
1018     g.fork();
1019     AsyncFib f = new AsyncFib(8);
1020     f.fork();
1021     threadAssertTrue(pollTask() == f);
1022     helpQuiesce();
1023     threadAssertFalse(f.isDone());
1024     threadAssertTrue(g.isDone());
1025     }
1026     };
1027     singletonPool.invoke(a);
1028     }
1029    
1030 jsr166 1.2 /**
1031 dl 1.1 * peekNextLocalTask returns least recent unexecuted task in async mode
1032     */
1033     public void testPeekNextLocalTaskAsync() {
1034     RecursiveAction a = new RecursiveAction() {
1035     public void compute() {
1036     AsyncFib g = new AsyncFib(9);
1037     g.fork();
1038     AsyncFib f = new AsyncFib(8);
1039     f.fork();
1040     threadAssertTrue(peekNextLocalTask() == g);
1041     f.join();
1042     helpQuiesce();
1043     threadAssertTrue(f.isDone());
1044     }
1045     };
1046     asyncSingletonPool.invoke(a);
1047     }
1048    
1049 jsr166 1.2 /**
1050 dl 1.1 * pollNextLocalTask returns least recent unexecuted task
1051     * without executing it, in async mode
1052     */
1053     public void testPollNextLocalTaskAsync() {
1054     RecursiveAction a = new RecursiveAction() {
1055     public void compute() {
1056     AsyncFib g = new AsyncFib(9);
1057     g.fork();
1058     AsyncFib f = new AsyncFib(8);
1059     f.fork();
1060     threadAssertTrue(pollNextLocalTask() == g);
1061     helpQuiesce();
1062     threadAssertTrue(f.isDone());
1063     threadAssertFalse(g.isDone());
1064     }
1065     };
1066     asyncSingletonPool.invoke(a);
1067     }
1068    
1069 jsr166 1.2 /**
1070 dl 1.1 * pollTask returns an unexecuted task
1071     * without executing it, in async mode
1072     */
1073     public void testPollTaskAsync() {
1074     RecursiveAction a = new RecursiveAction() {
1075     public void compute() {
1076     AsyncFib g = new AsyncFib(9);
1077     g.fork();
1078     AsyncFib f = new AsyncFib(8);
1079     f.fork();
1080     threadAssertTrue(pollTask() == g);
1081     helpQuiesce();
1082     threadAssertTrue(f.isDone());
1083     threadAssertFalse(g.isDone());
1084     }
1085     };
1086     asyncSingletonPool.invoke(a);
1087     }
1088     }