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