ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinTaskTest.java
Revision: 1.2
Committed: Fri Jul 31 23:37:31 2009 UTC (14 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.1: +73 -73 lines
Log Message:
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.concurrent.atomic.*;
9     import java.util.*;
10    
11     public class ForkJoinTaskTest extends JSR166TestCase {
12    
13     public static void main(String[] args) {
14 jsr166 1.2 junit.textui.TestRunner.run (suite());
15 dl 1.1 }
16     public static Test suite() {
17     return new TestSuite(ForkJoinTaskTest.class);
18     }
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     static final class AsyncFib extends BinaryAsyncAction {
152     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     static final class FailingAsyncFib extends BinaryAsyncAction {
181     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 dl 1.1 unexpectedException();
280     }
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     f.get(5L, TimeUnit.SECONDS);
296     threadAssertTrue(f.number == 21);
297     threadAssertTrue(f.isDone());
298 jsr166 1.2 } catch (Exception ex) {
299 dl 1.1 unexpectedException();
300     }
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.2 } catch (NullPointerException success) {
317     } catch (Exception ex) {
318 dl 1.1 unexpectedException();
319     }
320     }
321     };
322     mainPool.invoke(a);
323     }
324    
325 jsr166 1.2 /**
326 dl 1.1 * helpJoin of a forked task returns when task completes
327     */
328     public void testForkHelpJoin() {
329     RecursiveAction a = new RecursiveAction() {
330     public void compute() {
331     AsyncFib f = new AsyncFib(8);
332     f.fork();
333     f.helpJoin();
334     threadAssertTrue(f.number == 21);
335     threadAssertTrue(f.isDone());
336     }
337     };
338     mainPool.invoke(a);
339     }
340    
341 jsr166 1.2 /**
342 dl 1.1 * quietlyJoin of a forked task returns when task completes
343     */
344     public void testForkQuietlyJoin() {
345     RecursiveAction a = new RecursiveAction() {
346     public void compute() {
347     AsyncFib f = new AsyncFib(8);
348     f.fork();
349     f.quietlyJoin();
350     threadAssertTrue(f.number == 21);
351     threadAssertTrue(f.isDone());
352     }
353     };
354     mainPool.invoke(a);
355     }
356    
357    
358 jsr166 1.2 /**
359 dl 1.1 * quietlyHelpJoin of a forked task returns when task completes
360     */
361     public void testForkQuietlyHelpJoin() {
362     RecursiveAction a = new RecursiveAction() {
363     public void compute() {
364     AsyncFib f = new AsyncFib(8);
365     f.fork();
366     f.quietlyHelpJoin();
367     threadAssertTrue(f.number == 21);
368     threadAssertTrue(f.isDone());
369     }
370     };
371     mainPool.invoke(a);
372     }
373    
374    
375 jsr166 1.2 /**
376 dl 1.1 * helpQuiesce returns when tasks are complete.
377     * getQueuedTaskCount returns 0 when quiescent
378     */
379     public void testForkHelpQuiesce() {
380     RecursiveAction a = new RecursiveAction() {
381     public void compute() {
382     AsyncFib f = new AsyncFib(8);
383     f.fork();
384     f.helpQuiesce();
385     threadAssertTrue(f.number == 21);
386     threadAssertTrue(f.isDone());
387     threadAssertTrue(getQueuedTaskCount() == 0);
388     }
389     };
390     mainPool.invoke(a);
391     }
392    
393    
394 jsr166 1.2 /**
395 dl 1.1 * invoke task throws exception when task completes abnormally
396     */
397     public void testAbnormalInvoke() {
398     RecursiveAction a = new RecursiveAction() {
399     public void compute() {
400     try {
401     FailingAsyncFib f = new FailingAsyncFib(8);
402     f.invoke();
403     shouldThrow();
404 jsr166 1.2 } catch (FJException success) {
405 dl 1.1 }
406     }
407     };
408     mainPool.invoke(a);
409     }
410    
411 jsr166 1.2 /**
412 dl 1.1 * quietelyInvoke task returns when task completes abnormally
413     */
414     public void testAbnormalQuietlyInvoke() {
415     RecursiveAction a = new RecursiveAction() {
416     public void compute() {
417     FailingAsyncFib f = new FailingAsyncFib(8);
418     f.quietlyInvoke();
419     threadAssertTrue(f.isDone());
420     }
421     };
422     mainPool.invoke(a);
423     }
424    
425 jsr166 1.2 /**
426 dl 1.1 * join of a forked task throws exception when task completes abnormally
427     */
428     public void testAbnormalForkJoin() {
429     RecursiveAction a = new RecursiveAction() {
430     public void compute() {
431     try {
432     FailingAsyncFib f = new FailingAsyncFib(8);
433     f.fork();
434     f.join();
435     shouldThrow();
436 jsr166 1.2 } catch (FJException success) {
437 dl 1.1 }
438     }
439     };
440     mainPool.invoke(a);
441     }
442    
443 jsr166 1.2 /**
444 dl 1.1 * get of a forked task throws exception when task completes abnormally
445     */
446     public void testAbnormalForkGet() {
447     RecursiveAction a = new RecursiveAction() {
448     public void compute() {
449     try {
450     FailingAsyncFib f = new FailingAsyncFib(8);
451     f.fork();
452     f.get();
453     shouldThrow();
454 jsr166 1.2 } catch (Exception success) {
455 dl 1.1 }
456     }
457     };
458     mainPool.invoke(a);
459     }
460    
461 jsr166 1.2 /**
462 dl 1.1 * timed get of a forked task throws exception when task completes abnormally
463     */
464     public void testAbnormalForkTimedGet() {
465     RecursiveAction a = new RecursiveAction() {
466     public void compute() {
467     try {
468     FailingAsyncFib f = new FailingAsyncFib(8);
469     f.fork();
470     f.get(5L, TimeUnit.SECONDS);
471     shouldThrow();
472 jsr166 1.2 } catch (Exception success) {
473 dl 1.1 }
474     }
475     };
476     mainPool.invoke(a);
477     }
478    
479 jsr166 1.2 /**
480 dl 1.1 * join of a forked task throws exception when task completes abnormally
481     */
482     public void testAbnormalForkHelpJoin() {
483     RecursiveAction a = new RecursiveAction() {
484     public void compute() {
485     try {
486     FailingAsyncFib f = new FailingAsyncFib(8);
487     f.fork();
488     f.helpJoin();
489     shouldThrow();
490 jsr166 1.2 } catch (FJException success) {
491 dl 1.1 }
492     }
493     };
494     mainPool.invoke(a);
495     }
496    
497 jsr166 1.2 /**
498 dl 1.1 * quietlyHelpJoin of a forked task returns when task completes abnormally.
499     * getException of failed task returns its exception.
500     * isCompletedAbnormally of a failed task returns true.
501     * isCancelled of a failed uncancelled task returns false
502     */
503     public void testAbnormalForkQuietlyHelpJoin() {
504     RecursiveAction a = new RecursiveAction() {
505     public void compute() {
506     FailingAsyncFib f = new FailingAsyncFib(8);
507     f.fork();
508     f.quietlyHelpJoin();
509     threadAssertTrue(f.isDone());
510     threadAssertTrue(f.isCompletedAbnormally());
511     threadAssertFalse(f.isCancelled());
512     threadAssertTrue(f.getException() instanceof FJException);
513     }
514     };
515     mainPool.invoke(a);
516     }
517    
518 jsr166 1.2 /**
519 dl 1.1 * quietlyJoin of a forked task returns when task completes abnormally
520     */
521     public void testAbnormalForkQuietlyJoin() {
522     RecursiveAction a = new RecursiveAction() {
523     public void compute() {
524     FailingAsyncFib f = new FailingAsyncFib(8);
525     f.fork();
526     f.quietlyJoin();
527     threadAssertTrue(f.isDone());
528     threadAssertTrue(f.isCompletedAbnormally());
529     threadAssertTrue(f.getException() instanceof FJException);
530     }
531     };
532     mainPool.invoke(a);
533     }
534    
535 jsr166 1.2 /**
536 dl 1.1 * invoke task throws exception when task cancelled
537     */
538     public void testCancelledInvoke() {
539     RecursiveAction a = new RecursiveAction() {
540     public void compute() {
541     try {
542     AsyncFib f = new AsyncFib(8);
543     f.cancel(true);
544     f.invoke();
545     shouldThrow();
546 jsr166 1.2 } catch (CancellationException success) {
547 dl 1.1 }
548     }
549     };
550     mainPool.invoke(a);
551     }
552    
553 jsr166 1.2 /**
554 dl 1.1 * join of a forked task throws exception when task cancelled
555     */
556     public void testCancelledForkJoin() {
557     RecursiveAction a = new RecursiveAction() {
558     public void compute() {
559     try {
560     AsyncFib f = new AsyncFib(8);
561     f.cancel(true);
562     f.fork();
563     f.join();
564     shouldThrow();
565 jsr166 1.2 } catch (CancellationException success) {
566 dl 1.1 }
567     }
568     };
569     mainPool.invoke(a);
570     }
571    
572 jsr166 1.2 /**
573 dl 1.1 * get of a forked task throws exception when task cancelled
574     */
575     public void testCancelledForkGet() {
576     RecursiveAction a = new RecursiveAction() {
577     public void compute() {
578     try {
579     AsyncFib f = new AsyncFib(8);
580     f.cancel(true);
581     f.fork();
582     f.get();
583     shouldThrow();
584 jsr166 1.2 } catch (Exception success) {
585 dl 1.1 }
586     }
587     };
588     mainPool.invoke(a);
589     }
590    
591 jsr166 1.2 /**
592 dl 1.1 * timed get of a forked task throws exception when task cancelled
593     */
594     public void testCancelledForkTimedGet() {
595     RecursiveAction a = new RecursiveAction() {
596     public void compute() {
597     try {
598     AsyncFib f = new AsyncFib(8);
599     f.cancel(true);
600     f.fork();
601     f.get(5L, TimeUnit.SECONDS);
602     shouldThrow();
603 jsr166 1.2 } catch (Exception success) {
604 dl 1.1 }
605     }
606     };
607     mainPool.invoke(a);
608     }
609    
610 jsr166 1.2 /**
611 dl 1.1 * join of a forked task throws exception when task cancelled
612     */
613     public void testCancelledForkHelpJoin() {
614     RecursiveAction a = new RecursiveAction() {
615     public void compute() {
616     try {
617     AsyncFib f = new AsyncFib(8);
618     f.cancel(true);
619     f.fork();
620     f.helpJoin();
621     shouldThrow();
622 jsr166 1.2 } catch (CancellationException success) {
623 dl 1.1 }
624     }
625     };
626     mainPool.invoke(a);
627     }
628    
629 jsr166 1.2 /**
630 dl 1.1 * quietlyHelpJoin of a forked task returns when task cancelled.
631     * getException of cancelled task returns its exception.
632     * isCompletedAbnormally of a cancelled task returns true.
633     * isCancelled of a cancelled task returns true
634     */
635     public void testCancelledForkQuietlyHelpJoin() {
636     RecursiveAction a = new RecursiveAction() {
637     public void compute() {
638     AsyncFib f = new AsyncFib(8);
639     f.cancel(true);
640     f.fork();
641     f.quietlyHelpJoin();
642     threadAssertTrue(f.isDone());
643     threadAssertTrue(f.isCompletedAbnormally());
644     threadAssertTrue(f.isCancelled());
645     threadAssertTrue(f.getException() instanceof CancellationException);
646     }
647     };
648     mainPool.invoke(a);
649     }
650    
651 jsr166 1.2 /**
652 dl 1.1 * quietlyJoin of a forked task returns when task cancelled
653     */
654     public void testCancelledForkQuietlyJoin() {
655     RecursiveAction a = new RecursiveAction() {
656     public void compute() {
657     AsyncFib f = new AsyncFib(8);
658     f.cancel(true);
659     f.fork();
660     f.quietlyJoin();
661     threadAssertTrue(f.isDone());
662     threadAssertTrue(f.isCompletedAbnormally());
663     threadAssertTrue(f.getException() instanceof CancellationException);
664     }
665     };
666     mainPool.invoke(a);
667     }
668    
669     /**
670     * getPool of executing task returns its pool
671     */
672     public void testGetPool() {
673     RecursiveAction a = new RecursiveAction() {
674     public void compute() {
675     threadAssertTrue(getPool() == mainPool);
676     }
677     };
678     mainPool.invoke(a);
679     }
680    
681     /**
682     * getPool of non-FJ task returns null
683     */
684     public void testGetPool2() {
685     RecursiveAction a = new RecursiveAction() {
686     public void compute() {
687     threadAssertTrue(getPool() == null);
688     }
689     };
690     a.invoke();
691     }
692    
693     /**
694     * inForkJoinPool of executing task returns true
695     */
696     public void testInForkJoinPool() {
697     RecursiveAction a = new RecursiveAction() {
698     public void compute() {
699     threadAssertTrue(inForkJoinPool());
700     }
701     };
702     mainPool.invoke(a);
703     }
704    
705     /**
706     * inForkJoinPool of non-FJ task returns false
707     */
708     public void testInForkJoinPool2() {
709     RecursiveAction a = new RecursiveAction() {
710     public void compute() {
711     threadAssertTrue(!inForkJoinPool());
712     }
713     };
714     a.invoke();
715     }
716    
717     /**
718     * setRawResult(null) succeeds
719     */
720     public void testSetRawResult() {
721     RecursiveAction a = new RecursiveAction() {
722     public void compute() {
723     setRawResult(null);
724     }
725     };
726     a.invoke();
727     }
728    
729 jsr166 1.2 /**
730 dl 1.1 * invoke task throws exception after invoking completeExceptionally
731     */
732     public void testCompleteExceptionally() {
733     RecursiveAction a = new RecursiveAction() {
734     public void compute() {
735     try {
736     AsyncFib f = new AsyncFib(8);
737     f.completeExceptionally(new FJException());
738     f.invoke();
739     shouldThrow();
740 jsr166 1.2 } catch (FJException success) {
741 dl 1.1 }
742     }
743     };
744     mainPool.invoke(a);
745     }
746    
747 jsr166 1.2 /**
748 dl 1.1 * invokeAll(t1, t2) invokes all task arguments
749     */
750     public void testInvokeAll2() {
751     RecursiveAction a = new RecursiveAction() {
752     public void compute() {
753     AsyncFib f = new AsyncFib(8);
754     AsyncFib g = new AsyncFib(9);
755     invokeAll(f, g);
756     threadAssertTrue(f.isDone());
757     threadAssertTrue(f.number == 21);
758     threadAssertTrue(g.isDone());
759     threadAssertTrue(g.number == 34);
760     }
761     };
762     mainPool.invoke(a);
763     }
764    
765 jsr166 1.2 /**
766 dl 1.1 * invokeAll(tasks) with 1 argument invokes task
767     */
768     public void testInvokeAll1() {
769     RecursiveAction a = new RecursiveAction() {
770     public void compute() {
771     AsyncFib f = new AsyncFib(8);
772     invokeAll(f);
773     threadAssertTrue(f.isDone());
774     threadAssertTrue(f.number == 21);
775     }
776     };
777     mainPool.invoke(a);
778     }
779    
780 jsr166 1.2 /**
781 dl 1.1 * invokeAll(tasks) with > 2 argument invokes tasks
782     */
783     public void testInvokeAll3() {
784     RecursiveAction a = new RecursiveAction() {
785     public void compute() {
786     AsyncFib f = new AsyncFib(8);
787     AsyncFib g = new AsyncFib(9);
788     AsyncFib h = new AsyncFib(7);
789     invokeAll(f, g, h);
790     threadAssertTrue(f.isDone());
791     threadAssertTrue(f.number == 21);
792     threadAssertTrue(g.isDone());
793     threadAssertTrue(g.number == 34);
794     threadAssertTrue(h.isDone());
795     threadAssertTrue(h.number == 13);
796     }
797     };
798     mainPool.invoke(a);
799     }
800    
801 jsr166 1.2 /**
802 dl 1.1 * invokeAll(collection) invokes all tasks in the collection
803     */
804     public void testInvokeAllCollection() {
805     RecursiveAction a = new RecursiveAction() {
806     public void compute() {
807     AsyncFib f = new AsyncFib(8);
808     AsyncFib g = new AsyncFib(9);
809     AsyncFib h = new AsyncFib(7);
810     HashSet set = new HashSet();
811     set.add(f);
812     set.add(g);
813     set.add(h);
814     invokeAll(set);
815     threadAssertTrue(f.isDone());
816     threadAssertTrue(f.number == 21);
817     threadAssertTrue(g.isDone());
818     threadAssertTrue(g.number == 34);
819     threadAssertTrue(h.isDone());
820     threadAssertTrue(h.number == 13);
821     }
822     };
823     mainPool.invoke(a);
824     }
825    
826    
827 jsr166 1.2 /**
828 dl 1.1 * invokeAll(tasks) with any null task throws NPE
829     */
830     public void testInvokeAllNPE() {
831     RecursiveAction a = new RecursiveAction() {
832     public void compute() {
833     try {
834     AsyncFib f = new AsyncFib(8);
835     AsyncFib g = new AsyncFib(9);
836     AsyncFib h = null;
837     invokeAll(f, g, h);
838     shouldThrow();
839     } catch (NullPointerException success) {
840     }
841     }
842     };
843     mainPool.invoke(a);
844     }
845    
846 jsr166 1.2 /**
847 dl 1.1 * invokeAll(t1, t2) throw exception if any task does
848     */
849     public void testAbnormalInvokeAll2() {
850     RecursiveAction a = new RecursiveAction() {
851     public void compute() {
852     try {
853     AsyncFib f = new AsyncFib(8);
854     FailingAsyncFib g = new FailingAsyncFib(9);
855     invokeAll(f, g);
856     shouldThrow();
857 jsr166 1.2 } catch (FJException success) {
858 dl 1.1 }
859     }
860     };
861     mainPool.invoke(a);
862     }
863    
864 jsr166 1.2 /**
865 dl 1.1 * invokeAll(tasks) with 1 argument throws exception if task does
866     */
867     public void testAbnormalInvokeAll1() {
868     RecursiveAction a = new RecursiveAction() {
869     public void compute() {
870     try {
871     FailingAsyncFib g = new FailingAsyncFib(9);
872     invokeAll(g);
873     shouldThrow();
874 jsr166 1.2 } catch (FJException success) {
875 dl 1.1 }
876     }
877     };
878     mainPool.invoke(a);
879     }
880    
881 jsr166 1.2 /**
882 dl 1.1 * invokeAll(tasks) with > 2 argument throws exception if any task does
883     */
884     public void testAbnormalInvokeAll3() {
885     RecursiveAction a = new RecursiveAction() {
886     public void compute() {
887     try {
888     AsyncFib f = new AsyncFib(8);
889     FailingAsyncFib g = new FailingAsyncFib(9);
890     AsyncFib h = new AsyncFib(7);
891     invokeAll(f, g, h);
892     shouldThrow();
893 jsr166 1.2 } catch (FJException success) {
894 dl 1.1 }
895     }
896     };
897     mainPool.invoke(a);
898     }
899    
900 jsr166 1.2 /**
901 dl 1.1 * invokeAll(collection) throws exception if any task does
902     */
903     public void testAbnormalInvokeAllCollection() {
904     RecursiveAction a = new RecursiveAction() {
905     public void compute() {
906     try {
907     FailingAsyncFib f = new FailingAsyncFib(8);
908     AsyncFib g = new AsyncFib(9);
909     AsyncFib h = new AsyncFib(7);
910     HashSet set = new HashSet();
911     set.add(f);
912     set.add(g);
913     set.add(h);
914     invokeAll(set);
915     shouldThrow();
916 jsr166 1.2 } catch (FJException success) {
917 dl 1.1 }
918     }
919     };
920     mainPool.invoke(a);
921     }
922    
923 jsr166 1.2 /**
924 dl 1.1 * tryUnfork returns true for most recent unexecuted task,
925     * and suppresses execution
926     */
927     public void testTryUnfork() {
928     RecursiveAction a = new RecursiveAction() {
929     public void compute() {
930     AsyncFib g = new AsyncFib(9);
931     g.fork();
932     AsyncFib f = new AsyncFib(8);
933     f.fork();
934     threadAssertTrue(f.tryUnfork());
935     helpQuiesce();
936     threadAssertFalse(f.isDone());
937     threadAssertTrue(g.isDone());
938     }
939     };
940     singletonPool.invoke(a);
941     }
942    
943 jsr166 1.2 /**
944 dl 1.1 * getSurplusQueuedTaskCount returns > 0 when
945     * there are more tasks than threads
946     */
947     public void testGetSurplusQueuedTaskCount() {
948     RecursiveAction a = new RecursiveAction() {
949     public void compute() {
950     AsyncFib h = new AsyncFib(7);
951     h.fork();
952     AsyncFib g = new AsyncFib(9);
953     g.fork();
954     AsyncFib f = new AsyncFib(8);
955     f.fork();
956     threadAssertTrue(getSurplusQueuedTaskCount() > 0);
957     helpQuiesce();
958     }
959     };
960     singletonPool.invoke(a);
961     }
962    
963 jsr166 1.2 /**
964 dl 1.1 * peekNextLocalTask returns most recent unexecuted task.
965     */
966     public void testPeekNextLocalTask() {
967     RecursiveAction a = new RecursiveAction() {
968     public void compute() {
969     AsyncFib g = new AsyncFib(9);
970     g.fork();
971     AsyncFib f = new AsyncFib(8);
972     f.fork();
973     threadAssertTrue(peekNextLocalTask() == f);
974     f.join();
975     threadAssertTrue(f.isDone());
976     helpQuiesce();
977     }
978     };
979     singletonPool.invoke(a);
980     }
981    
982 jsr166 1.2 /**
983 dl 1.1 * pollNextLocalTask returns most recent unexecuted task
984     * without executing it
985     */
986     public void testPollNextLocalTask() {
987     RecursiveAction a = new RecursiveAction() {
988     public void compute() {
989     AsyncFib g = new AsyncFib(9);
990     g.fork();
991     AsyncFib f = new AsyncFib(8);
992     f.fork();
993     threadAssertTrue(pollNextLocalTask() == f);
994     helpQuiesce();
995     threadAssertFalse(f.isDone());
996     }
997     };
998     singletonPool.invoke(a);
999     }
1000    
1001 jsr166 1.2 /**
1002 dl 1.1 * pollTask returns an unexecuted task
1003     * without executing it
1004     */
1005     public void testPollTask() {
1006     RecursiveAction a = new RecursiveAction() {
1007     public void compute() {
1008     AsyncFib g = new AsyncFib(9);
1009     g.fork();
1010     AsyncFib f = new AsyncFib(8);
1011     f.fork();
1012     threadAssertTrue(pollTask() == f);
1013     helpQuiesce();
1014     threadAssertFalse(f.isDone());
1015     threadAssertTrue(g.isDone());
1016     }
1017     };
1018     singletonPool.invoke(a);
1019     }
1020    
1021 jsr166 1.2 /**
1022 dl 1.1 * peekNextLocalTask returns least recent unexecuted task in async mode
1023     */
1024     public void testPeekNextLocalTaskAsync() {
1025     RecursiveAction a = new RecursiveAction() {
1026     public void compute() {
1027     AsyncFib g = new AsyncFib(9);
1028     g.fork();
1029     AsyncFib f = new AsyncFib(8);
1030     f.fork();
1031     threadAssertTrue(peekNextLocalTask() == g);
1032     f.join();
1033     helpQuiesce();
1034     threadAssertTrue(f.isDone());
1035     }
1036     };
1037     asyncSingletonPool.invoke(a);
1038     }
1039    
1040 jsr166 1.2 /**
1041 dl 1.1 * pollNextLocalTask returns least recent unexecuted task
1042     * without executing it, in async mode
1043     */
1044     public void testPollNextLocalTaskAsync() {
1045     RecursiveAction a = new RecursiveAction() {
1046     public void compute() {
1047     AsyncFib g = new AsyncFib(9);
1048     g.fork();
1049     AsyncFib f = new AsyncFib(8);
1050     f.fork();
1051     threadAssertTrue(pollNextLocalTask() == g);
1052     helpQuiesce();
1053     threadAssertTrue(f.isDone());
1054     threadAssertFalse(g.isDone());
1055     }
1056     };
1057     asyncSingletonPool.invoke(a);
1058     }
1059    
1060 jsr166 1.2 /**
1061 dl 1.1 * pollTask returns an unexecuted task
1062     * without executing it, in async mode
1063     */
1064     public void testPollTaskAsync() {
1065     RecursiveAction a = new RecursiveAction() {
1066     public void compute() {
1067     AsyncFib g = new AsyncFib(9);
1068     g.fork();
1069     AsyncFib f = new AsyncFib(8);
1070     f.fork();
1071     threadAssertTrue(pollTask() == g);
1072     helpQuiesce();
1073     threadAssertTrue(f.isDone());
1074     threadAssertFalse(g.isDone());
1075     }
1076     };
1077     asyncSingletonPool.invoke(a);
1078     }
1079     }