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

File Contents

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