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