ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountedCompleterTest.java
Revision: 1.2
Committed: Thu Mar 21 16:26:43 2013 UTC (11 years, 2 months ago) by dl
Branch: MAIN
Changes since 1.1: +108 -102 lines
Log Message:
CompletableFuture coverage

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/publicdomain/zero/1.0/
5     */
6     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.CountedCompleter;
11     import java.util.concurrent.ForkJoinWorkerThread;
12     import java.util.concurrent.RecursiveAction;
13     import java.util.concurrent.TimeUnit;
14     import java.util.concurrent.TimeoutException;
15     import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
16     import static java.util.concurrent.TimeUnit.MILLISECONDS;
17     import static java.util.concurrent.TimeUnit.SECONDS;
18     import java.util.HashSet;
19     import junit.framework.*;
20    
21     public class CountedCompleterTest extends JSR166TestCase {
22    
23     public static void main(String[] args) {
24     junit.textui.TestRunner.run(suite());
25     }
26    
27     public static Test suite() {
28     return new TestSuite(CountedCompleterTest.class);
29     }
30    
31     // Runs with "mainPool" use > 1 thread. singletonPool tests use 1
32     static final int mainPoolSize =
33     Math.max(2, Runtime.getRuntime().availableProcessors());
34    
35     /**
36 dl 1.2 * Analog of CheckedRunnable for ForkJoinTasks
37 dl 1.1 */
38     public abstract class CheckedFJTask extends RecursiveAction {
39     protected abstract void realCompute() throws Throwable;
40    
41     public final void compute() {
42     try {
43     realCompute();
44     } catch (Throwable t) {
45     threadUnexpectedException(t);
46     }
47     }
48     }
49    
50     private static ForkJoinPool mainPool() {
51     return new ForkJoinPool(mainPoolSize);
52     }
53    
54     private static ForkJoinPool singletonPool() {
55     return new ForkJoinPool(1);
56     }
57    
58     private static ForkJoinPool asyncSingletonPool() {
59     return new ForkJoinPool(1,
60     ForkJoinPool.defaultForkJoinWorkerThreadFactory,
61     null, true);
62     }
63    
64     private void testInvokeOnPool(ForkJoinPool pool, ForkJoinTask a) {
65     try {
66     assertFalse(a.isDone());
67     assertFalse(a.isCompletedNormally());
68     assertFalse(a.isCompletedAbnormally());
69     assertFalse(a.isCancelled());
70     assertNull(a.getException());
71     assertNull(a.getRawResult());
72    
73     assertNull(pool.invoke(a));
74    
75     assertTrue(a.isDone());
76     assertTrue(a.isCompletedNormally());
77     assertFalse(a.isCompletedAbnormally());
78     assertFalse(a.isCancelled());
79     assertNull(a.getException());
80     assertNull(a.getRawResult());
81     } finally {
82     joinPool(pool);
83     }
84     }
85    
86     void checkNotDone(CountedCompleter a) {
87     assertFalse(a.isDone());
88     assertFalse(a.isCompletedNormally());
89     assertFalse(a.isCompletedAbnormally());
90     assertFalse(a.isCancelled());
91     assertNull(a.getException());
92     assertNull(a.getRawResult());
93    
94     try {
95     a.get(0L, SECONDS);
96     shouldThrow();
97     } catch (TimeoutException success) {
98     } catch (Throwable fail) { threadUnexpectedException(fail); }
99     }
100    
101     <T> void checkCompletedNormally(CountedCompleter<T> a) {
102     checkCompletedNormally(a, null);
103     }
104    
105     <T> void checkCompletedNormally(CountedCompleter<T> a, T expected) {
106     assertTrue(a.isDone());
107     assertFalse(a.isCancelled());
108     assertTrue(a.isCompletedNormally());
109     assertFalse(a.isCompletedAbnormally());
110     assertNull(a.getException());
111     assertSame(expected, a.getRawResult());
112    
113     {
114     Thread.currentThread().interrupt();
115     long t0 = System.nanoTime();
116     assertSame(expected, a.join());
117     assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
118     Thread.interrupted();
119     }
120    
121     {
122     Thread.currentThread().interrupt();
123     long t0 = System.nanoTime();
124     a.quietlyJoin(); // should be no-op
125     assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
126     Thread.interrupted();
127     }
128    
129     assertFalse(a.cancel(false));
130     assertFalse(a.cancel(true));
131     try {
132     assertSame(expected, a.get());
133     } catch (Throwable fail) { threadUnexpectedException(fail); }
134     try {
135     assertSame(expected, a.get(5L, SECONDS));
136     } catch (Throwable fail) { threadUnexpectedException(fail); }
137     }
138    
139     void checkCancelled(CountedCompleter a) {
140     assertTrue(a.isDone());
141     assertTrue(a.isCancelled());
142     assertFalse(a.isCompletedNormally());
143     assertTrue(a.isCompletedAbnormally());
144     assertTrue(a.getException() instanceof CancellationException);
145     assertNull(a.getRawResult());
146     assertTrue(a.cancel(false));
147     assertTrue(a.cancel(true));
148    
149     try {
150     Thread.currentThread().interrupt();
151     a.join();
152     shouldThrow();
153     } catch (CancellationException success) {
154     } catch (Throwable fail) { threadUnexpectedException(fail); }
155     Thread.interrupted();
156    
157     {
158     long t0 = System.nanoTime();
159     a.quietlyJoin(); // should be no-op
160     assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
161     }
162    
163     try {
164     a.get();
165     shouldThrow();
166     } catch (CancellationException success) {
167     } catch (Throwable fail) { threadUnexpectedException(fail); }
168    
169     try {
170     a.get(5L, SECONDS);
171     shouldThrow();
172     } catch (CancellationException success) {
173     } catch (Throwable fail) { threadUnexpectedException(fail); }
174     }
175    
176     void checkCompletedAbnormally(CountedCompleter a, Throwable t) {
177     assertTrue(a.isDone());
178     assertFalse(a.isCancelled());
179     assertFalse(a.isCompletedNormally());
180     assertTrue(a.isCompletedAbnormally());
181     assertSame(t.getClass(), a.getException().getClass());
182     assertNull(a.getRawResult());
183     assertFalse(a.cancel(false));
184     assertFalse(a.cancel(true));
185    
186     try {
187     Thread.currentThread().interrupt();
188     a.join();
189     shouldThrow();
190     } catch (Throwable expected) {
191     assertSame(t.getClass(), expected.getClass());
192     }
193     Thread.interrupted();
194    
195     {
196     long t0 = System.nanoTime();
197     a.quietlyJoin(); // should be no-op
198     assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
199     }
200    
201     try {
202     a.get();
203     shouldThrow();
204     } catch (ExecutionException success) {
205     assertSame(t.getClass(), success.getCause().getClass());
206     } catch (Throwable fail) { threadUnexpectedException(fail); }
207    
208     try {
209     a.get(5L, SECONDS);
210     shouldThrow();
211     } catch (ExecutionException success) {
212     assertSame(t.getClass(), success.getCause().getClass());
213     } catch (Throwable fail) { threadUnexpectedException(fail); }
214     }
215    
216     public static final class FJException extends RuntimeException {
217     FJException() { super(); }
218     }
219    
220     static final class NoopCountedCompleter extends CountedCompleter {
221     boolean post; // set true if onCompletion called
222     NoopCountedCompleter() { super(); }
223     NoopCountedCompleter(CountedCompleter p) { super(p); }
224     public void compute() {}
225     public final void onCompletion(CountedCompleter caller) {
226     post = true;
227     }
228     }
229    
230     /**
231     * A newly constructed CountedCompleter is not completed;
232     * complete() causes completion.
233     */
234     public void testComplete() {
235     NoopCountedCompleter a = new NoopCountedCompleter();
236     assertFalse(a.isDone());
237     assertFalse(a.isCompletedNormally());
238     assertFalse(a.isCompletedAbnormally());
239     assertFalse(a.isCancelled());
240     assertNull(a.getException());
241     assertNull(a.getRawResult());
242     assertFalse(a.post);
243     a.complete(null);
244     assertTrue(a.post);
245     assertTrue(a.isDone());
246     assertTrue(a.isCompletedNormally());
247     assertFalse(a.isCompletedAbnormally());
248     assertFalse(a.isCancelled());
249     assertNull(a.getException());
250     assertNull(a.getRawResult());
251     }
252    
253     /**
254     * completeExceptionally completes exceptionally
255     */
256     public void testCompleteExceptionally() {
257     NoopCountedCompleter a = new NoopCountedCompleter();
258     assertFalse(a.isDone());
259     assertFalse(a.isCompletedNormally());
260     assertFalse(a.isCompletedAbnormally());
261     assertFalse(a.isCancelled());
262     assertNull(a.getException());
263     assertNull(a.getRawResult());
264     assertFalse(a.post);
265     a.completeExceptionally(new FJException());
266     assertFalse(a.post);
267     assertTrue(a.isDone());
268     assertFalse(a.isCompletedNormally());
269     assertTrue(a.isCompletedAbnormally());
270     assertFalse(a.isCancelled());
271     assertTrue(a.getException() instanceof FJException);
272     assertNull(a.getRawResult());
273     }
274    
275     /**
276     * setPendingCount sets the reported pending count
277     */
278     public void testSetPendingCount() {
279     NoopCountedCompleter a = new NoopCountedCompleter();
280     assertEquals(0, a.getPendingCount());
281     a.setPendingCount(1);
282     assertEquals(1, a.getPendingCount());
283     a.setPendingCount(27);
284     assertEquals(27, a.getPendingCount());
285     }
286    
287     /**
288     * addToPendingCount adds to the reported pending count
289     */
290     public void testAddToPendingCount() {
291     NoopCountedCompleter a = new NoopCountedCompleter();
292     assertEquals(0, a.getPendingCount());
293     a.addToPendingCount(1);
294     assertEquals(1, a.getPendingCount());
295     a.addToPendingCount(27);
296     assertEquals(28, a.getPendingCount());
297     }
298    
299     /**
300     * decrementPendingCountUnlessZero decrements reported pending
301     * count unless zero
302     */
303     public void testDecrementPendingCount() {
304     NoopCountedCompleter a = new NoopCountedCompleter();
305     assertEquals(0, a.getPendingCount());
306     a.addToPendingCount(1);
307     assertEquals(1, a.getPendingCount());
308     a.decrementPendingCountUnlessZero();
309     assertEquals(0, a.getPendingCount());
310     a.decrementPendingCountUnlessZero();
311     assertEquals(0, a.getPendingCount());
312     }
313    
314     /**
315     * getCompleter returns parent or null if at root
316     */
317     public void testGetCompleter() {
318     NoopCountedCompleter a = new NoopCountedCompleter();
319     assertNull(a.getCompleter());
320     CountedCompleter b = new NoopCountedCompleter(a);
321     assertEquals(a, b.getCompleter());
322     }
323    
324     /**
325     * getRoot returns self if no parent, else parent's root
326     */
327     public void testGetRoot() {
328     NoopCountedCompleter a = new NoopCountedCompleter();
329     NoopCountedCompleter b = new NoopCountedCompleter(a);
330     assertEquals(a, a.getRoot());
331     assertEquals(a, b.getRoot());
332     }
333    
334     /**
335     * tryComplete causes completion if pending count is zero
336     */
337     public void testTryComplete1() {
338     NoopCountedCompleter a = new NoopCountedCompleter();
339     assertEquals(0, a.getPendingCount());
340     a.tryComplete();
341     assertTrue(a.post);
342     assertTrue(a.isDone());
343     }
344    
345     /**
346     * propagateCompletion causes completion without invokein
347     * onCompletion if pending count is zero
348     */
349     public void testPropagateCompletion() {
350     NoopCountedCompleter a = new NoopCountedCompleter();
351     assertEquals(0, a.getPendingCount());
352     a.propagateCompletion();
353     assertFalse(a.post);
354     assertTrue(a.isDone());
355     }
356    
357     /**
358     * tryComplete decrments pending count unless zero
359     */
360     public void testTryComplete2() {
361     NoopCountedCompleter a = new NoopCountedCompleter();
362     assertEquals(0, a.getPendingCount());
363     a.setPendingCount(1);
364     a.tryComplete();
365     assertFalse(a.post);
366     assertFalse(a.isDone());
367     assertEquals(0, a.getPendingCount());
368     a.tryComplete();
369     assertTrue(a.post);
370     assertTrue(a.isDone());
371     }
372    
373     /**
374     * firstComplete returns this if pending count is zero else null
375     */
376     public void testFirstComplete() {
377     NoopCountedCompleter a = new NoopCountedCompleter();
378     a.setPendingCount(1);
379     assertNull(a.firstComplete());
380     assertEquals(a, a.firstComplete());
381     }
382    
383     /**
384     * firstComplete.nextComplete returns parent if pending count is
385     * zero else null
386     */
387     public void testNextComplete() {
388     NoopCountedCompleter a = new NoopCountedCompleter();
389     NoopCountedCompleter b = new NoopCountedCompleter(a);
390     a.setPendingCount(1);
391     b.setPendingCount(1);
392     assertNull(b.firstComplete());
393     CountedCompleter c = b.firstComplete();
394     assertEquals(b, c);
395     CountedCompleter d = c.nextComplete();
396     assertNull(d);
397     CountedCompleter e = c.nextComplete();
398     assertEquals(a, e);
399     }
400    
401     /**
402     * quietlyCompleteRoot completes root task
403     */
404     public void testQuietlyCompleteRoot() {
405     NoopCountedCompleter a = new NoopCountedCompleter();
406     NoopCountedCompleter b = new NoopCountedCompleter(a);
407     a.setPendingCount(1);
408     b.setPendingCount(1);
409     b.quietlyCompleteRoot();
410     assertTrue(a.isDone());
411     assertFalse(b.isDone());
412     }
413 dl 1.2
414     // Invocation tests use some interdependent task classes
415     // to better test propagation etc
416    
417    
418     // Version of Fibonacci with different classes for left vs right forks
419     static abstract class CCF extends CountedCompleter {
420     int number;
421     int rnumber;
422    
423     public CCF(CountedCompleter parent, int n) {
424     super(parent, 1);
425     this.number = n;
426     }
427    
428     public final void compute() {
429     CountedCompleter p;
430     CCF f = this;
431     int n = number;
432     while (n >= 2) {
433     new RCCF(f, n - 2).fork();
434     f = new LCCF(f, --n);
435     }
436     f.number = n;
437     f.onCompletion(f);
438     if ((p = f.getCompleter()) != null)
439     p.tryComplete();
440     else
441     f.quietlyComplete();
442     }
443     }
444    
445     static final class LCCF extends CCF {
446     public LCCF(CountedCompleter parent, int n) {
447     super(parent, n);
448     }
449     public final void onCompletion(CountedCompleter caller) {
450     CCF p = (CCF)getCompleter();
451     int n = number + rnumber;
452     if (p != null)
453     p.number = n;
454     else
455     number = n;
456     }
457     }
458     static final class RCCF extends CCF {
459     public RCCF(CountedCompleter parent, int n) {
460     super(parent, n);
461     }
462     public final void onCompletion(CountedCompleter caller) {
463     CCF p = (CCF)getCompleter();
464     int n = number + rnumber;
465     if (p != null)
466     p.rnumber = n;
467     else
468     number = n;
469     }
470     }
471    
472     // Version of CCF with forced failure in left completions
473     static abstract class FailingCCF extends CountedCompleter {
474     int number;
475     int rnumber;
476    
477     public FailingCCF(CountedCompleter parent, int n) {
478     super(parent, 1);
479     this.number = n;
480     }
481    
482     public final void compute() {
483     CountedCompleter p;
484     FailingCCF f = this;
485     int n = number;
486     while (n >= 2) {
487     new RFCCF(f, n - 2).fork();
488     f = new LFCCF(f, --n);
489     }
490     f.number = n;
491     f.onCompletion(f);
492     if ((p = f.getCompleter()) != null)
493     p.tryComplete();
494     else
495     f.quietlyComplete();
496     }
497     }
498    
499     static final class LFCCF extends FailingCCF {
500     public LFCCF(CountedCompleter parent, int n) {
501     super(parent, n);
502     }
503     public final void onCompletion(CountedCompleter caller) {
504     FailingCCF p = (FailingCCF)getCompleter();
505     int n = number + rnumber;
506     if (p != null)
507     p.number = n;
508     else
509     number = n;
510     }
511     }
512     static final class RFCCF extends FailingCCF {
513     public RFCCF(CountedCompleter parent, int n) {
514     super(parent, n);
515     }
516     public final void onCompletion(CountedCompleter caller) {
517     completeExceptionally(new FJException());
518     }
519     }
520 dl 1.1
521     /**
522     * invoke returns when task completes normally.
523     * isCompletedAbnormally and isCancelled return false for normally
524     * completed tasks; getRawResult returns null.
525     */
526     public void testInvoke() {
527     ForkJoinTask a = new CheckedFJTask() {
528     public void realCompute() {
529     CCF f = new LCCF(null, 8);
530     assertNull(f.invoke());
531     assertEquals(21, f.number);
532     checkCompletedNormally(f);
533     }};
534     testInvokeOnPool(mainPool(), a);
535     }
536    
537     /**
538     * quietlyInvoke task returns when task completes normally.
539     * isCompletedAbnormally and isCancelled return false for normally
540     * completed tasks
541     */
542     public void testQuietlyInvoke() {
543     ForkJoinTask a = new CheckedFJTask() {
544     public void realCompute() {
545     CCF f = new LCCF(null, 8);
546     f.quietlyInvoke();
547     assertEquals(21, f.number);
548     checkCompletedNormally(f);
549     }};
550     testInvokeOnPool(mainPool(), a);
551     }
552    
553     /**
554     * join of a forked task returns when task completes
555     */
556     public void testForkJoin() {
557     ForkJoinTask a = new CheckedFJTask() {
558     public void realCompute() {
559     CCF f = new LCCF(null, 8);
560     assertSame(f, f.fork());
561     assertNull(f.join());
562     assertEquals(21, f.number);
563     checkCompletedNormally(f);
564     }};
565     testInvokeOnPool(mainPool(), a);
566     }
567    
568     /**
569     * get of a forked task returns when task completes
570     */
571     public void testForkGet() {
572     ForkJoinTask a = new CheckedFJTask() {
573     public void realCompute() throws Exception {
574     CCF f = new LCCF(null, 8);
575     assertSame(f, f.fork());
576     assertNull(f.get());
577     assertEquals(21, f.number);
578     checkCompletedNormally(f);
579     }};
580     testInvokeOnPool(mainPool(), a);
581     }
582    
583     /**
584     * timed get of a forked task returns when task completes
585     */
586     public void testForkTimedGet() {
587     ForkJoinTask a = new CheckedFJTask() {
588     public void realCompute() throws Exception {
589     CCF f = new LCCF(null, 8);
590     assertSame(f, f.fork());
591     assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
592     assertEquals(21, f.number);
593     checkCompletedNormally(f);
594     }};
595     testInvokeOnPool(mainPool(), a);
596     }
597    
598     /**
599     * timed get with null time unit throws NPE
600     */
601     public void testForkTimedGetNPE() {
602     ForkJoinTask a = new CheckedFJTask() {
603     public void realCompute() throws Exception {
604     CCF f = new LCCF(null, 8);
605     assertSame(f, f.fork());
606     try {
607     f.get(5L, null);
608     shouldThrow();
609     } catch (NullPointerException success) {}
610     }};
611     testInvokeOnPool(mainPool(), a);
612     }
613    
614     /**
615     * quietlyJoin of a forked task returns when task completes
616     */
617     public void testForkQuietlyJoin() {
618     ForkJoinTask a = new CheckedFJTask() {
619     public void realCompute() {
620     CCF f = new LCCF(null, 8);
621     assertSame(f, f.fork());
622     f.quietlyJoin();
623     assertEquals(21, f.number);
624     checkCompletedNormally(f);
625     }};
626     testInvokeOnPool(mainPool(), a);
627     }
628    
629     /**
630     * helpQuiesce returns when tasks are complete.
631     * getQueuedTaskCount returns 0 when quiescent
632     */
633     public void testForkHelpQuiesce() {
634     ForkJoinTask a = new CheckedFJTask() {
635     public void realCompute() {
636     CCF f = new LCCF(null, 8);
637     assertSame(f, f.fork());
638     helpQuiesce();
639     assertEquals(21, f.number);
640     assertEquals(0, getQueuedTaskCount());
641     checkCompletedNormally(f);
642     }};
643     testInvokeOnPool(mainPool(), a);
644     }
645    
646     /**
647     * invoke task throws exception when task completes abnormally
648     */
649     public void testAbnormalInvoke() {
650     ForkJoinTask a = new CheckedFJTask() {
651     public void realCompute() {
652     FailingCCF f = new LFCCF(null, 8);
653     try {
654     f.invoke();
655     shouldThrow();
656     } catch (FJException success) {
657     checkCompletedAbnormally(f, success);
658     }
659     }};
660     testInvokeOnPool(mainPool(), a);
661     }
662    
663     /**
664     * quietlyInvoke task returns when task completes abnormally
665     */
666     public void testAbnormalQuietlyInvoke() {
667     ForkJoinTask a = new CheckedFJTask() {
668     public void realCompute() {
669     FailingCCF f = new LFCCF(null, 8);
670     f.quietlyInvoke();
671     assertTrue(f.getException() instanceof FJException);
672     checkCompletedAbnormally(f, f.getException());
673     }};
674     testInvokeOnPool(mainPool(), a);
675     }
676    
677     /**
678     * join of a forked task throws exception when task completes abnormally
679     */
680     public void testAbnormalForkJoin() {
681     ForkJoinTask a = new CheckedFJTask() {
682     public void realCompute() {
683     FailingCCF f = new LFCCF(null, 8);
684     assertSame(f, f.fork());
685     try {
686     f.join();
687     shouldThrow();
688     } catch (FJException success) {
689     checkCompletedAbnormally(f, success);
690     }
691     }};
692     testInvokeOnPool(mainPool(), a);
693     }
694    
695     /**
696     * get of a forked task throws exception when task completes abnormally
697     */
698     public void testAbnormalForkGet() {
699     ForkJoinTask a = new CheckedFJTask() {
700     public void realCompute() throws Exception {
701     FailingCCF f = new LFCCF(null, 8);
702     assertSame(f, f.fork());
703     try {
704     f.get();
705     shouldThrow();
706     } catch (ExecutionException success) {
707     Throwable cause = success.getCause();
708     assertTrue(cause instanceof FJException);
709     checkCompletedAbnormally(f, cause);
710     }
711     }};
712     testInvokeOnPool(mainPool(), a);
713     }
714    
715     /**
716     * timed get of a forked task throws exception when task completes abnormally
717     */
718     public void testAbnormalForkTimedGet() {
719     ForkJoinTask a = new CheckedFJTask() {
720     public void realCompute() throws Exception {
721     FailingCCF f = new LFCCF(null, 8);
722     assertSame(f, f.fork());
723     try {
724     f.get(LONG_DELAY_MS, MILLISECONDS);
725     shouldThrow();
726     } catch (ExecutionException success) {
727     Throwable cause = success.getCause();
728     assertTrue(cause instanceof FJException);
729     checkCompletedAbnormally(f, cause);
730     }
731     }};
732     testInvokeOnPool(mainPool(), a);
733     }
734    
735     /**
736     * quietlyJoin of a forked task returns when task completes abnormally
737     */
738     public void testAbnormalForkQuietlyJoin() {
739     ForkJoinTask a = new CheckedFJTask() {
740     public void realCompute() {
741     FailingCCF f = new LFCCF(null, 8);
742     assertSame(f, f.fork());
743     f.quietlyJoin();
744     assertTrue(f.getException() instanceof FJException);
745     checkCompletedAbnormally(f, f.getException());
746     }};
747     testInvokeOnPool(mainPool(), a);
748     }
749    
750     /**
751     * invoke task throws exception when task cancelled
752     */
753     public void testCancelledInvoke() {
754     ForkJoinTask a = new CheckedFJTask() {
755     public void realCompute() {
756     CCF f = new LCCF(null, 8);
757     assertTrue(f.cancel(true));
758     try {
759     f.invoke();
760     shouldThrow();
761     } catch (CancellationException success) {
762     checkCancelled(f);
763     }
764     }};
765     testInvokeOnPool(mainPool(), a);
766     }
767    
768     /**
769     * join of a forked task throws exception when task cancelled
770     */
771     public void testCancelledForkJoin() {
772     ForkJoinTask a = new CheckedFJTask() {
773     public void realCompute() {
774     CCF f = new LCCF(null, 8);
775     assertTrue(f.cancel(true));
776     assertSame(f, f.fork());
777     try {
778     f.join();
779     shouldThrow();
780     } catch (CancellationException success) {
781     checkCancelled(f);
782     }
783     }};
784     testInvokeOnPool(mainPool(), a);
785     }
786    
787     /**
788     * get of a forked task throws exception when task cancelled
789     */
790     public void testCancelledForkGet() {
791     ForkJoinTask a = new CheckedFJTask() {
792     public void realCompute() throws Exception {
793     CCF f = new LCCF(null, 8);
794     assertTrue(f.cancel(true));
795     assertSame(f, f.fork());
796     try {
797     f.get();
798     shouldThrow();
799     } catch (CancellationException success) {
800     checkCancelled(f);
801     }
802     }};
803     testInvokeOnPool(mainPool(), a);
804     }
805    
806     /**
807     * timed get of a forked task throws exception when task cancelled
808     */
809     public void testCancelledForkTimedGet() throws Exception {
810     ForkJoinTask a = new CheckedFJTask() {
811     public void realCompute() throws Exception {
812     CCF f = new LCCF(null, 8);
813     assertTrue(f.cancel(true));
814     assertSame(f, f.fork());
815     try {
816     f.get(LONG_DELAY_MS, MILLISECONDS);
817     shouldThrow();
818     } catch (CancellationException success) {
819     checkCancelled(f);
820     }
821     }};
822     testInvokeOnPool(mainPool(), a);
823     }
824    
825     /**
826     * quietlyJoin of a forked task returns when task cancelled
827     */
828     public void testCancelledForkQuietlyJoin() {
829     ForkJoinTask a = new CheckedFJTask() {
830     public void realCompute() {
831     CCF f = new LCCF(null, 8);
832     assertTrue(f.cancel(true));
833     assertSame(f, f.fork());
834     f.quietlyJoin();
835     checkCancelled(f);
836     }};
837     testInvokeOnPool(mainPool(), a);
838     }
839    
840     /**
841     * getPool of executing task returns its pool
842     */
843     public void testGetPool() {
844     final ForkJoinPool mainPool = mainPool();
845     ForkJoinTask a = new CheckedFJTask() {
846     public void realCompute() {
847     assertSame(mainPool, getPool());
848     }};
849     testInvokeOnPool(mainPool, a);
850     }
851    
852     /**
853     * getPool of non-FJ task returns null
854     */
855     public void testGetPool2() {
856     ForkJoinTask a = new CheckedFJTask() {
857     public void realCompute() {
858     assertNull(getPool());
859     }};
860     assertNull(a.invoke());
861     }
862    
863     /**
864     * inForkJoinPool of executing task returns true
865     */
866     public void testInForkJoinPool() {
867     ForkJoinTask a = new CheckedFJTask() {
868     public void realCompute() {
869     assertTrue(inForkJoinPool());
870     }};
871     testInvokeOnPool(mainPool(), a);
872     }
873    
874     /**
875     * inForkJoinPool of non-FJ task returns false
876     */
877     public void testInForkJoinPool2() {
878     ForkJoinTask a = new CheckedFJTask() {
879     public void realCompute() {
880     assertFalse(inForkJoinPool());
881     }};
882     assertNull(a.invoke());
883     }
884    
885     /**
886     * setRawResult(null) succeeds
887     */
888     public void testSetRawResult() {
889     ForkJoinTask a = new CheckedFJTask() {
890     public void realCompute() {
891     setRawResult(null);
892     assertNull(getRawResult());
893     }};
894     assertNull(a.invoke());
895     }
896    
897     /**
898     * invoke task throws exception after invoking completeExceptionally
899     */
900     public void testCompleteExceptionally2() {
901     ForkJoinTask a = new CheckedFJTask() {
902     public void realCompute() {
903     CCF f = new LCCF(null, 8);
904     f.completeExceptionally(new FJException());
905     try {
906     f.invoke();
907     shouldThrow();
908     } catch (FJException success) {
909     checkCompletedAbnormally(f, success);
910     }
911     }};
912     testInvokeOnPool(mainPool(), a);
913     }
914    
915     /**
916     * invokeAll(t1, t2) invokes all task arguments
917     */
918     public void testInvokeAll2() {
919     ForkJoinTask a = new CheckedFJTask() {
920     public void realCompute() {
921     CCF f = new LCCF(null, 8);
922     CCF g = new LCCF(null, 9);
923     invokeAll(f, g);
924     assertEquals(21, f.number);
925     assertEquals(34, g.number);
926     checkCompletedNormally(f);
927     checkCompletedNormally(g);
928     }};
929     testInvokeOnPool(mainPool(), a);
930     }
931    
932     /**
933     * invokeAll(tasks) with 1 argument invokes task
934     */
935     public void testInvokeAll1() {
936     ForkJoinTask a = new CheckedFJTask() {
937     public void realCompute() {
938     CCF f = new LCCF(null, 8);
939     invokeAll(f);
940     checkCompletedNormally(f);
941     assertEquals(21, f.number);
942     }};
943     testInvokeOnPool(mainPool(), a);
944     }
945    
946     /**
947     * invokeAll(tasks) with > 2 argument invokes tasks
948     */
949     public void testInvokeAll3() {
950     ForkJoinTask a = new CheckedFJTask() {
951     public void realCompute() {
952     CCF f = new LCCF(null, 8);
953     CCF g = new LCCF(null, 9);
954     CCF h = new LCCF(null, 7);
955     invokeAll(f, g, h);
956     assertEquals(21, f.number);
957     assertEquals(34, g.number);
958     assertEquals(13, h.number);
959     checkCompletedNormally(f);
960     checkCompletedNormally(g);
961     checkCompletedNormally(h);
962     }};
963     testInvokeOnPool(mainPool(), a);
964     }
965    
966     /**
967     * invokeAll(collection) invokes all tasks in the collection
968     */
969     public void testInvokeAllCollection() {
970     ForkJoinTask a = new CheckedFJTask() {
971     public void realCompute() {
972     CCF f = new LCCF(null, 8);
973     CCF g = new LCCF(null, 9);
974     CCF h = new LCCF(null, 7);
975     HashSet set = new HashSet();
976     set.add(f);
977     set.add(g);
978     set.add(h);
979     invokeAll(set);
980     assertEquals(21, f.number);
981     assertEquals(34, g.number);
982     assertEquals(13, h.number);
983     checkCompletedNormally(f);
984     checkCompletedNormally(g);
985     checkCompletedNormally(h);
986     }};
987     testInvokeOnPool(mainPool(), a);
988     }
989    
990     /**
991     * invokeAll(tasks) with any null task throws NPE
992     */
993     public void testInvokeAllNPE() {
994     ForkJoinTask a = new CheckedFJTask() {
995     public void realCompute() {
996     CCF f = new LCCF(null, 8);
997     CCF g = new LCCF(null, 9);
998     CCF h = null;
999     try {
1000     invokeAll(f, g, h);
1001     shouldThrow();
1002     } catch (NullPointerException success) {}
1003     }};
1004     testInvokeOnPool(mainPool(), a);
1005     }
1006    
1007     /**
1008     * invokeAll(t1, t2) throw exception if any task does
1009     */
1010     public void testAbnormalInvokeAll2() {
1011     ForkJoinTask a = new CheckedFJTask() {
1012     public void realCompute() {
1013     CCF f = new LCCF(null, 8);
1014     FailingCCF g = new LFCCF(null, 9);
1015     try {
1016     invokeAll(f, g);
1017     shouldThrow();
1018     } catch (FJException success) {
1019     checkCompletedAbnormally(g, success);
1020     }
1021     }};
1022     testInvokeOnPool(mainPool(), a);
1023     }
1024    
1025     /**
1026     * invokeAll(tasks) with 1 argument throws exception if task does
1027     */
1028     public void testAbnormalInvokeAll1() {
1029     ForkJoinTask a = new CheckedFJTask() {
1030     public void realCompute() {
1031     FailingCCF g = new LFCCF(null, 9);
1032     try {
1033     invokeAll(g);
1034     shouldThrow();
1035     } catch (FJException success) {
1036     checkCompletedAbnormally(g, success);
1037     }
1038     }};
1039     testInvokeOnPool(mainPool(), a);
1040     }
1041    
1042     /**
1043     * invokeAll(tasks) with > 2 argument throws exception if any task does
1044     */
1045     public void testAbnormalInvokeAll3() {
1046     ForkJoinTask a = new CheckedFJTask() {
1047     public void realCompute() {
1048     CCF f = new LCCF(null, 8);
1049     FailingCCF g = new LFCCF(null, 9);
1050     CCF h = new LCCF(null, 7);
1051     try {
1052     invokeAll(f, g, h);
1053     shouldThrow();
1054     } catch (FJException success) {
1055     checkCompletedAbnormally(g, success);
1056     }
1057     }};
1058     testInvokeOnPool(mainPool(), a);
1059     }
1060    
1061     /**
1062     * invokeAll(collection) throws exception if any task does
1063     */
1064     public void testAbnormalInvokeAllCollection() {
1065     ForkJoinTask a = new CheckedFJTask() {
1066     public void realCompute() {
1067     FailingCCF f = new LFCCF(null, 8);
1068     CCF g = new LCCF(null, 9);
1069     CCF h = new LCCF(null, 7);
1070     HashSet set = new HashSet();
1071     set.add(f);
1072     set.add(g);
1073     set.add(h);
1074     try {
1075     invokeAll(set);
1076     shouldThrow();
1077     } catch (FJException success) {
1078     checkCompletedAbnormally(f, success);
1079     }
1080     }};
1081     testInvokeOnPool(mainPool(), a);
1082     }
1083    
1084     /**
1085     * tryUnfork returns true for most recent unexecuted task,
1086     * and suppresses execution
1087     */
1088     public void testTryUnfork() {
1089     ForkJoinTask a = new CheckedFJTask() {
1090     public void realCompute() {
1091     CCF g = new LCCF(null, 9);
1092     assertSame(g, g.fork());
1093     CCF f = new LCCF(null, 8);
1094     assertSame(f, f.fork());
1095     assertTrue(f.tryUnfork());
1096     helpQuiesce();
1097     checkNotDone(f);
1098     checkCompletedNormally(g);
1099     }};
1100     testInvokeOnPool(singletonPool(), a);
1101     }
1102    
1103     /**
1104     * getSurplusQueuedTaskCount returns > 0 when
1105     * there are more tasks than threads
1106     */
1107     public void testGetSurplusQueuedTaskCount() {
1108     ForkJoinTask a = new CheckedFJTask() {
1109     public void realCompute() {
1110     CCF h = new LCCF(null, 7);
1111     assertSame(h, h.fork());
1112     CCF g = new LCCF(null, 9);
1113     assertSame(g, g.fork());
1114     CCF f = new LCCF(null, 8);
1115     assertSame(f, f.fork());
1116     assertTrue(getSurplusQueuedTaskCount() > 0);
1117     helpQuiesce();
1118     assertEquals(0, getSurplusQueuedTaskCount());
1119     checkCompletedNormally(f);
1120     checkCompletedNormally(g);
1121     checkCompletedNormally(h);
1122     }};
1123     testInvokeOnPool(singletonPool(), a);
1124     }
1125    
1126     /**
1127     * peekNextLocalTask returns most recent unexecuted task.
1128     */
1129     public void testPeekNextLocalTask() {
1130     ForkJoinTask a = new CheckedFJTask() {
1131     public void realCompute() {
1132     CCF g = new LCCF(null, 9);
1133     assertSame(g, g.fork());
1134     CCF f = new LCCF(null, 8);
1135     assertSame(f, f.fork());
1136     assertSame(f, peekNextLocalTask());
1137     assertNull(f.join());
1138     checkCompletedNormally(f);
1139     helpQuiesce();
1140     checkCompletedNormally(g);
1141     }};
1142     testInvokeOnPool(singletonPool(), a);
1143     }
1144    
1145     /**
1146     * pollNextLocalTask returns most recent unexecuted task without
1147     * executing it
1148     */
1149     public void testPollNextLocalTask() {
1150     ForkJoinTask a = new CheckedFJTask() {
1151     public void realCompute() {
1152     CCF g = new LCCF(null, 9);
1153     assertSame(g, g.fork());
1154     CCF f = new LCCF(null, 8);
1155     assertSame(f, f.fork());
1156     assertSame(f, pollNextLocalTask());
1157     helpQuiesce();
1158     checkNotDone(f);
1159     assertEquals(34, g.number);
1160     checkCompletedNormally(g);
1161     }};
1162     testInvokeOnPool(singletonPool(), a);
1163     }
1164    
1165     /**
1166     * pollTask returns an unexecuted task without executing it
1167     */
1168     public void testPollTask() {
1169     ForkJoinTask a = new CheckedFJTask() {
1170     public void realCompute() {
1171     CCF g = new LCCF(null, 9);
1172     assertSame(g, g.fork());
1173     CCF f = new LCCF(null, 8);
1174     assertSame(f, f.fork());
1175     assertSame(f, pollTask());
1176     helpQuiesce();
1177     checkNotDone(f);
1178     checkCompletedNormally(g);
1179     }};
1180     testInvokeOnPool(singletonPool(), a);
1181     }
1182    
1183     /**
1184     * peekNextLocalTask returns least recent unexecuted task in async mode
1185     */
1186     public void testPeekNextLocalTaskAsync() {
1187     ForkJoinTask a = new CheckedFJTask() {
1188     public void realCompute() {
1189     CCF g = new LCCF(null, 9);
1190     assertSame(g, g.fork());
1191     CCF f = new LCCF(null, 8);
1192     assertSame(f, f.fork());
1193     assertSame(g, peekNextLocalTask());
1194     assertNull(f.join());
1195     helpQuiesce();
1196     checkCompletedNormally(f);
1197     assertEquals(34, g.number);
1198     checkCompletedNormally(g);
1199     }};
1200     testInvokeOnPool(asyncSingletonPool(), a);
1201     }
1202    
1203     /**
1204     * pollNextLocalTask returns least recent unexecuted task without
1205     * executing it, in async mode
1206     */
1207     public void testPollNextLocalTaskAsync() {
1208     ForkJoinTask a = new CheckedFJTask() {
1209     public void realCompute() {
1210     CCF g = new LCCF(null, 9);
1211     assertSame(g, g.fork());
1212     CCF f = new LCCF(null, 8);
1213     assertSame(f, f.fork());
1214     assertSame(g, pollNextLocalTask());
1215     helpQuiesce();
1216     assertEquals(21, f.number);
1217     checkCompletedNormally(f);
1218     checkNotDone(g);
1219     }};
1220     testInvokeOnPool(asyncSingletonPool(), a);
1221     }
1222    
1223     /**
1224     * pollTask returns an unexecuted task without executing it, in
1225     * async mode
1226     */
1227     public void testPollTaskAsync() {
1228     ForkJoinTask a = new CheckedFJTask() {
1229     public void realCompute() {
1230     CCF g = new LCCF(null, 9);
1231     assertSame(g, g.fork());
1232     CCF f = new LCCF(null, 8);
1233     assertSame(f, f.fork());
1234     assertSame(g, pollTask());
1235     helpQuiesce();
1236     assertEquals(21, f.number);
1237     checkCompletedNormally(f);
1238     checkNotDone(g);
1239     }};
1240     testInvokeOnPool(asyncSingletonPool(), a);
1241     }
1242    
1243     // versions for singleton pools
1244    
1245     /**
1246     * invoke returns when task completes normally.
1247     * isCompletedAbnormally and isCancelled return false for normally
1248     * completed tasks; getRawResult returns null.
1249     */
1250     public void testInvokeSingleton() {
1251     ForkJoinTask a = new CheckedFJTask() {
1252     public void realCompute() {
1253     CCF f = new LCCF(null, 8);
1254     assertNull(f.invoke());
1255     assertEquals(21, f.number);
1256     checkCompletedNormally(f);
1257     }};
1258     testInvokeOnPool(singletonPool(), a);
1259     }
1260    
1261     /**
1262     * quietlyInvoke task returns when task completes normally.
1263     * isCompletedAbnormally and isCancelled return false for normally
1264     * completed tasks
1265     */
1266     public void testQuietlyInvokeSingleton() {
1267     ForkJoinTask a = new CheckedFJTask() {
1268     public void realCompute() {
1269     CCF f = new LCCF(null, 8);
1270     f.quietlyInvoke();
1271     assertEquals(21, f.number);
1272     checkCompletedNormally(f);
1273     }};
1274     testInvokeOnPool(singletonPool(), a);
1275     }
1276    
1277     /**
1278     * join of a forked task returns when task completes
1279     */
1280     public void testForkJoinSingleton() {
1281     ForkJoinTask a = new CheckedFJTask() {
1282     public void realCompute() {
1283     CCF f = new LCCF(null, 8);
1284     assertSame(f, f.fork());
1285     assertNull(f.join());
1286     assertEquals(21, f.number);
1287     checkCompletedNormally(f);
1288     }};
1289     testInvokeOnPool(singletonPool(), a);
1290     }
1291    
1292     /**
1293     * get of a forked task returns when task completes
1294     */
1295     public void testForkGetSingleton() {
1296     ForkJoinTask a = new CheckedFJTask() {
1297     public void realCompute() throws Exception {
1298     CCF f = new LCCF(null, 8);
1299     assertSame(f, f.fork());
1300     assertNull(f.get());
1301     assertEquals(21, f.number);
1302     checkCompletedNormally(f);
1303     }};
1304     testInvokeOnPool(singletonPool(), a);
1305     }
1306    
1307     /**
1308     * timed get of a forked task returns when task completes
1309     */
1310     public void testForkTimedGetSingleton() {
1311     ForkJoinTask a = new CheckedFJTask() {
1312     public void realCompute() throws Exception {
1313     CCF f = new LCCF(null, 8);
1314     assertSame(f, f.fork());
1315     assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1316     assertEquals(21, f.number);
1317     checkCompletedNormally(f);
1318     }};
1319     testInvokeOnPool(singletonPool(), a);
1320     }
1321    
1322     /**
1323     * timed get with null time unit throws NPE
1324     */
1325     public void testForkTimedGetNPESingleton() {
1326     ForkJoinTask a = new CheckedFJTask() {
1327     public void realCompute() throws Exception {
1328     CCF f = new LCCF(null, 8);
1329     assertSame(f, f.fork());
1330     try {
1331     f.get(5L, null);
1332     shouldThrow();
1333     } catch (NullPointerException success) {}
1334     }};
1335     testInvokeOnPool(singletonPool(), a);
1336     }
1337    
1338     /**
1339     * quietlyJoin of a forked task returns when task completes
1340     */
1341     public void testForkQuietlyJoinSingleton() {
1342     ForkJoinTask a = new CheckedFJTask() {
1343     public void realCompute() {
1344     CCF f = new LCCF(null, 8);
1345     assertSame(f, f.fork());
1346     f.quietlyJoin();
1347     assertEquals(21, f.number);
1348     checkCompletedNormally(f);
1349     }};
1350     testInvokeOnPool(singletonPool(), a);
1351     }
1352    
1353     /**
1354     * helpQuiesce returns when tasks are complete.
1355     * getQueuedTaskCount returns 0 when quiescent
1356     */
1357     public void testForkHelpQuiesceSingleton() {
1358     ForkJoinTask a = new CheckedFJTask() {
1359     public void realCompute() {
1360     CCF f = new LCCF(null, 8);
1361     assertSame(f, f.fork());
1362     helpQuiesce();
1363     assertEquals(0, getQueuedTaskCount());
1364     assertEquals(21, f.number);
1365     checkCompletedNormally(f);
1366     }};
1367     testInvokeOnPool(singletonPool(), a);
1368     }
1369    
1370     /**
1371     * invoke task throws exception when task completes abnormally
1372     */
1373     public void testAbnormalInvokeSingleton() {
1374     ForkJoinTask a = new CheckedFJTask() {
1375     public void realCompute() {
1376     FailingCCF f = new LFCCF(null, 8);
1377     try {
1378     f.invoke();
1379     shouldThrow();
1380     } catch (FJException success) {
1381     checkCompletedAbnormally(f, success);
1382     }
1383     }};
1384     testInvokeOnPool(singletonPool(), a);
1385     }
1386    
1387     /**
1388     * quietlyInvoke task returns when task completes abnormally
1389     */
1390     public void testAbnormalQuietlyInvokeSingleton() {
1391     ForkJoinTask a = new CheckedFJTask() {
1392     public void realCompute() {
1393     FailingCCF f = new LFCCF(null, 8);
1394     f.quietlyInvoke();
1395     assertTrue(f.getException() instanceof FJException);
1396     checkCompletedAbnormally(f, f.getException());
1397     }};
1398     testInvokeOnPool(singletonPool(), a);
1399     }
1400    
1401     /**
1402     * join of a forked task throws exception when task completes abnormally
1403     */
1404     public void testAbnormalForkJoinSingleton() {
1405     ForkJoinTask a = new CheckedFJTask() {
1406     public void realCompute() {
1407     FailingCCF f = new LFCCF(null, 8);
1408     assertSame(f, f.fork());
1409     try {
1410     f.join();
1411     shouldThrow();
1412     } catch (FJException success) {
1413     checkCompletedAbnormally(f, success);
1414     }
1415     }};
1416     testInvokeOnPool(singletonPool(), a);
1417     }
1418    
1419     /**
1420     * get of a forked task throws exception when task completes abnormally
1421     */
1422     public void testAbnormalForkGetSingleton() {
1423     ForkJoinTask a = new CheckedFJTask() {
1424     public void realCompute() throws Exception {
1425     FailingCCF f = new LFCCF(null, 8);
1426     assertSame(f, f.fork());
1427     try {
1428     f.get();
1429     shouldThrow();
1430     } catch (ExecutionException success) {
1431     Throwable cause = success.getCause();
1432     assertTrue(cause instanceof FJException);
1433     checkCompletedAbnormally(f, cause);
1434     }
1435     }};
1436     testInvokeOnPool(singletonPool(), a);
1437     }
1438    
1439     /**
1440     * timed get of a forked task throws exception when task completes abnormally
1441     */
1442     public void testAbnormalForkTimedGetSingleton() {
1443     ForkJoinTask a = new CheckedFJTask() {
1444     public void realCompute() throws Exception {
1445     FailingCCF f = new LFCCF(null, 8);
1446     assertSame(f, f.fork());
1447     try {
1448     f.get(LONG_DELAY_MS, MILLISECONDS);
1449     shouldThrow();
1450     } catch (ExecutionException success) {
1451     Throwable cause = success.getCause();
1452     assertTrue(cause instanceof FJException);
1453     checkCompletedAbnormally(f, cause);
1454     }
1455     }};
1456     testInvokeOnPool(singletonPool(), a);
1457     }
1458    
1459     /**
1460     * quietlyJoin of a forked task returns when task completes abnormally
1461     */
1462     public void testAbnormalForkQuietlyJoinSingleton() {
1463     ForkJoinTask a = new CheckedFJTask() {
1464     public void realCompute() {
1465     FailingCCF f = new LFCCF(null, 8);
1466     assertSame(f, f.fork());
1467     f.quietlyJoin();
1468     assertTrue(f.getException() instanceof FJException);
1469     checkCompletedAbnormally(f, f.getException());
1470     }};
1471     testInvokeOnPool(singletonPool(), a);
1472     }
1473    
1474     /**
1475     * invoke task throws exception when task cancelled
1476     */
1477     public void testCancelledInvokeSingleton() {
1478     ForkJoinTask a = new CheckedFJTask() {
1479     public void realCompute() {
1480     CCF f = new LCCF(null, 8);
1481     assertTrue(f.cancel(true));
1482     try {
1483     f.invoke();
1484     shouldThrow();
1485     } catch (CancellationException success) {
1486     checkCancelled(f);
1487     }
1488     }};
1489     testInvokeOnPool(singletonPool(), a);
1490     }
1491    
1492     /**
1493     * join of a forked task throws exception when task cancelled
1494     */
1495     public void testCancelledForkJoinSingleton() {
1496     ForkJoinTask a = new CheckedFJTask() {
1497     public void realCompute() {
1498     CCF f = new LCCF(null, 8);
1499     assertTrue(f.cancel(true));
1500     assertSame(f, f.fork());
1501     try {
1502     f.join();
1503     shouldThrow();
1504     } catch (CancellationException success) {
1505     checkCancelled(f);
1506     }
1507     }};
1508     testInvokeOnPool(singletonPool(), a);
1509     }
1510    
1511     /**
1512     * get of a forked task throws exception when task cancelled
1513     */
1514     public void testCancelledForkGetSingleton() {
1515     ForkJoinTask a = new CheckedFJTask() {
1516     public void realCompute() throws Exception {
1517     CCF f = new LCCF(null, 8);
1518     assertTrue(f.cancel(true));
1519     assertSame(f, f.fork());
1520     try {
1521     f.get();
1522     shouldThrow();
1523     } catch (CancellationException success) {
1524     checkCancelled(f);
1525     }
1526     }};
1527     testInvokeOnPool(singletonPool(), a);
1528     }
1529    
1530     /**
1531     * timed get of a forked task throws exception when task cancelled
1532     */
1533     public void testCancelledForkTimedGetSingleton() throws Exception {
1534     ForkJoinTask a = new CheckedFJTask() {
1535     public void realCompute() throws Exception {
1536     CCF f = new LCCF(null, 8);
1537     assertTrue(f.cancel(true));
1538     assertSame(f, f.fork());
1539     try {
1540     f.get(LONG_DELAY_MS, MILLISECONDS);
1541     shouldThrow();
1542     } catch (CancellationException success) {
1543     checkCancelled(f);
1544     }
1545     }};
1546     testInvokeOnPool(singletonPool(), a);
1547     }
1548    
1549     /**
1550     * quietlyJoin of a forked task returns when task cancelled
1551     */
1552     public void testCancelledForkQuietlyJoinSingleton() {
1553     ForkJoinTask a = new CheckedFJTask() {
1554     public void realCompute() {
1555     CCF f = new LCCF(null, 8);
1556     assertTrue(f.cancel(true));
1557     assertSame(f, f.fork());
1558     f.quietlyJoin();
1559     checkCancelled(f);
1560     }};
1561     testInvokeOnPool(singletonPool(), a);
1562     }
1563    
1564     /**
1565     * invoke task throws exception after invoking completeExceptionally
1566     */
1567     public void testCompleteExceptionallySingleton() {
1568     ForkJoinTask a = new CheckedFJTask() {
1569     public void realCompute() {
1570     CCF f = new LCCF(null, 8);
1571     f.completeExceptionally(new FJException());
1572     try {
1573     f.invoke();
1574     shouldThrow();
1575     } catch (FJException success) {
1576     checkCompletedAbnormally(f, success);
1577     }
1578     }};
1579     testInvokeOnPool(singletonPool(), a);
1580     }
1581    
1582     /**
1583     * invokeAll(t1, t2) invokes all task arguments
1584     */
1585     public void testInvokeAll2Singleton() {
1586     ForkJoinTask a = new CheckedFJTask() {
1587     public void realCompute() {
1588     CCF f = new LCCF(null, 8);
1589     CCF g = new LCCF(null, 9);
1590     invokeAll(f, g);
1591     assertEquals(21, f.number);
1592     assertEquals(34, g.number);
1593     checkCompletedNormally(f);
1594     checkCompletedNormally(g);
1595     }};
1596     testInvokeOnPool(singletonPool(), a);
1597     }
1598    
1599     /**
1600     * invokeAll(tasks) with 1 argument invokes task
1601     */
1602     public void testInvokeAll1Singleton() {
1603     ForkJoinTask a = new CheckedFJTask() {
1604     public void realCompute() {
1605     CCF f = new LCCF(null, 8);
1606     invokeAll(f);
1607     checkCompletedNormally(f);
1608     assertEquals(21, f.number);
1609     }};
1610     testInvokeOnPool(singletonPool(), a);
1611     }
1612    
1613     /**
1614     * invokeAll(tasks) with > 2 argument invokes tasks
1615     */
1616     public void testInvokeAll3Singleton() {
1617     ForkJoinTask a = new CheckedFJTask() {
1618     public void realCompute() {
1619     CCF f = new LCCF(null, 8);
1620     CCF g = new LCCF(null, 9);
1621     CCF h = new LCCF(null, 7);
1622     invokeAll(f, g, h);
1623     assertEquals(21, f.number);
1624     assertEquals(34, g.number);
1625     assertEquals(13, h.number);
1626     checkCompletedNormally(f);
1627     checkCompletedNormally(g);
1628     checkCompletedNormally(h);
1629     }};
1630     testInvokeOnPool(singletonPool(), a);
1631     }
1632    
1633     /**
1634     * invokeAll(collection) invokes all tasks in the collection
1635     */
1636     public void testInvokeAllCollectionSingleton() {
1637     ForkJoinTask a = new CheckedFJTask() {
1638     public void realCompute() {
1639     CCF f = new LCCF(null, 8);
1640     CCF g = new LCCF(null, 9);
1641     CCF h = new LCCF(null, 7);
1642     HashSet set = new HashSet();
1643     set.add(f);
1644     set.add(g);
1645     set.add(h);
1646     invokeAll(set);
1647     assertEquals(21, f.number);
1648     assertEquals(34, g.number);
1649     assertEquals(13, h.number);
1650     checkCompletedNormally(f);
1651     checkCompletedNormally(g);
1652     checkCompletedNormally(h);
1653     }};
1654     testInvokeOnPool(singletonPool(), a);
1655     }
1656    
1657     /**
1658     * invokeAll(tasks) with any null task throws NPE
1659     */
1660     public void testInvokeAllNPESingleton() {
1661     ForkJoinTask a = new CheckedFJTask() {
1662     public void realCompute() {
1663     CCF f = new LCCF(null, 8);
1664     CCF g = new LCCF(null, 9);
1665     CCF h = null;
1666     try {
1667     invokeAll(f, g, h);
1668     shouldThrow();
1669     } catch (NullPointerException success) {}
1670     }};
1671     testInvokeOnPool(singletonPool(), a);
1672     }
1673    
1674     /**
1675     * invokeAll(t1, t2) throw exception if any task does
1676     */
1677     public void testAbnormalInvokeAll2Singleton() {
1678     ForkJoinTask a = new CheckedFJTask() {
1679     public void realCompute() {
1680     CCF f = new LCCF(null, 8);
1681     FailingCCF g = new LFCCF(null, 9);
1682     try {
1683     invokeAll(f, g);
1684     shouldThrow();
1685     } catch (FJException success) {
1686     checkCompletedAbnormally(g, success);
1687     }
1688     }};
1689     testInvokeOnPool(singletonPool(), a);
1690     }
1691    
1692     /**
1693     * invokeAll(tasks) with 1 argument throws exception if task does
1694     */
1695     public void testAbnormalInvokeAll1Singleton() {
1696     ForkJoinTask a = new CheckedFJTask() {
1697     public void realCompute() {
1698     FailingCCF g = new LFCCF(null, 9);
1699     try {
1700     invokeAll(g);
1701     shouldThrow();
1702     } catch (FJException success) {
1703     checkCompletedAbnormally(g, success);
1704     }
1705     }};
1706     testInvokeOnPool(singletonPool(), a);
1707     }
1708    
1709     /**
1710     * invokeAll(tasks) with > 2 argument throws exception if any task does
1711     */
1712     public void testAbnormalInvokeAll3Singleton() {
1713     ForkJoinTask a = new CheckedFJTask() {
1714     public void realCompute() {
1715     CCF f = new LCCF(null, 8);
1716     FailingCCF g = new LFCCF(null, 9);
1717     CCF h = new LCCF(null, 7);
1718     try {
1719     invokeAll(f, g, h);
1720     shouldThrow();
1721     } catch (FJException success) {
1722     checkCompletedAbnormally(g, success);
1723     }
1724     }};
1725     testInvokeOnPool(singletonPool(), a);
1726     }
1727    
1728     /**
1729     * invokeAll(collection) throws exception if any task does
1730     */
1731     public void testAbnormalInvokeAllCollectionSingleton() {
1732     ForkJoinTask a = new CheckedFJTask() {
1733     public void realCompute() {
1734     FailingCCF f = new LFCCF(null, 8);
1735     CCF g = new LCCF(null, 9);
1736     CCF h = new LCCF(null, 7);
1737     HashSet set = new HashSet();
1738     set.add(f);
1739     set.add(g);
1740     set.add(h);
1741     try {
1742     invokeAll(set);
1743     shouldThrow();
1744     } catch (FJException success) {
1745     checkCompletedAbnormally(f, success);
1746     }
1747     }};
1748     testInvokeOnPool(singletonPool(), a);
1749     }
1750    
1751     }