ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPool8Test.java
Revision: 1.6
Committed: Fri Mar 22 19:02:27 2013 UTC (11 years, 1 month ago) by dl
Branch: MAIN
Changes since 1.5: +0 -34 lines
Log Message:
Remove inapplicable tests

File Contents

# User Rev Content
1 jsr166 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    
7     import junit.framework.*;
8 dl 1.4 import java.util.concurrent.CancellationException;
9     import java.util.concurrent.ExecutionException;
10 jsr166 1.1 import java.util.concurrent.ForkJoinPool;
11     import java.util.concurrent.ForkJoinTask;
12     import java.util.concurrent.ForkJoinWorkerThread;
13 dl 1.4 import java.util.concurrent.RecursiveAction;
14     import java.util.concurrent.CountedCompleter;
15     import java.util.concurrent.ThreadLocalRandom;
16     import java.util.concurrent.TimeUnit;
17     import java.util.concurrent.TimeoutException;
18     import static java.util.concurrent.TimeUnit.SECONDS;
19     import static java.util.concurrent.TimeUnit.MILLISECONDS;
20     import java.util.Arrays;
21     import java.util.HashSet;
22 jsr166 1.1
23     public class ForkJoinPool8Test extends JSR166TestCase {
24     public static void main(String[] args) {
25     junit.textui.TestRunner.run(suite());
26     }
27    
28     public static Test suite() {
29     return new TestSuite(ForkJoinPool8Test.class);
30     }
31    
32     /**
33     * Common pool exists and has expected parallelism.
34     */
35     public void testCommonPoolParallelism() {
36     assertEquals(ForkJoinPool.getCommonPoolParallelism(),
37     ForkJoinPool.commonPool().getParallelism());
38     }
39 dl 1.4
40     /**
41     * Common pool cannot be shut down
42     */
43     public void testCommonPoolShutDown() {
44     assertFalse(ForkJoinPool.commonPool().isShutdown());
45     assertFalse(ForkJoinPool.commonPool().isTerminating());
46     assertFalse(ForkJoinPool.commonPool().isTerminated());
47     ForkJoinPool.commonPool().shutdown();
48     assertFalse(ForkJoinPool.commonPool().isShutdown());
49     assertFalse(ForkJoinPool.commonPool().isTerminating());
50     assertFalse(ForkJoinPool.commonPool().isTerminated());
51     ForkJoinPool.commonPool().shutdownNow();
52     assertFalse(ForkJoinPool.commonPool().isShutdown());
53     assertFalse(ForkJoinPool.commonPool().isTerminating());
54     assertFalse(ForkJoinPool.commonPool().isTerminated());
55     }
56    
57     /*
58     * All of the following test methods are adaptations of those for
59     * RecursiveAction and CountedCompleter, but with all actions
60     * executed in the common pool, generally implicitly via
61     * checkInvoke.
62 jsr166 1.5 */
63 dl 1.4
64     private void checkInvoke(ForkJoinTask a) {
65     checkNotDone(a);
66     assertNull(a.invoke());
67     checkCompletedNormally(a);
68     }
69    
70     void checkNotDone(ForkJoinTask a) {
71     assertFalse(a.isDone());
72     assertFalse(a.isCompletedNormally());
73     assertFalse(a.isCompletedAbnormally());
74     assertFalse(a.isCancelled());
75     assertNull(a.getException());
76     assertNull(a.getRawResult());
77    
78     if (! ForkJoinTask.inForkJoinPool()) {
79     Thread.currentThread().interrupt();
80     try {
81     a.get();
82     shouldThrow();
83     } catch (InterruptedException success) {
84     } catch (Throwable fail) { threadUnexpectedException(fail); }
85    
86     Thread.currentThread().interrupt();
87     try {
88     a.get(5L, SECONDS);
89     shouldThrow();
90     } catch (InterruptedException success) {
91     } catch (Throwable fail) { threadUnexpectedException(fail); }
92     }
93    
94     try {
95     a.get(0L, SECONDS);
96     shouldThrow();
97     } catch (TimeoutException success) {
98     } catch (Throwable fail) { threadUnexpectedException(fail); }
99     }
100    
101     void checkCompletedNormally(ForkJoinTask a) {
102     assertTrue(a.isDone());
103     assertFalse(a.isCancelled());
104     assertTrue(a.isCompletedNormally());
105     assertFalse(a.isCompletedAbnormally());
106     assertNull(a.getException());
107     assertNull(a.getRawResult());
108     assertNull(a.join());
109     assertFalse(a.cancel(false));
110     assertFalse(a.cancel(true));
111     try {
112     assertNull(a.get());
113     } catch (Throwable fail) { threadUnexpectedException(fail); }
114     try {
115     assertNull(a.get(5L, SECONDS));
116     } catch (Throwable fail) { threadUnexpectedException(fail); }
117     }
118    
119     void checkCancelled(ForkJoinTask a) {
120     assertTrue(a.isDone());
121     assertTrue(a.isCancelled());
122     assertFalse(a.isCompletedNormally());
123     assertTrue(a.isCompletedAbnormally());
124     assertTrue(a.getException() instanceof CancellationException);
125     assertNull(a.getRawResult());
126    
127     try {
128     a.join();
129     shouldThrow();
130     } catch (CancellationException success) {
131     } catch (Throwable fail) { threadUnexpectedException(fail); }
132    
133     try {
134     a.get();
135     shouldThrow();
136     } catch (CancellationException success) {
137     } catch (Throwable fail) { threadUnexpectedException(fail); }
138    
139     try {
140     a.get(5L, SECONDS);
141     shouldThrow();
142     } catch (CancellationException success) {
143     } catch (Throwable fail) { threadUnexpectedException(fail); }
144     }
145    
146     void checkCompletedAbnormally(ForkJoinTask a, Throwable t) {
147     assertTrue(a.isDone());
148     assertFalse(a.isCancelled());
149     assertFalse(a.isCompletedNormally());
150     assertTrue(a.isCompletedAbnormally());
151     assertSame(t.getClass(), a.getException().getClass());
152     assertNull(a.getRawResult());
153     assertFalse(a.cancel(false));
154     assertFalse(a.cancel(true));
155    
156     try {
157     a.join();
158     shouldThrow();
159     } catch (Throwable expected) {
160     assertSame(expected.getClass(), t.getClass());
161     }
162    
163     try {
164     a.get();
165     shouldThrow();
166     } catch (ExecutionException success) {
167     assertSame(t.getClass(), success.getCause().getClass());
168     } catch (Throwable fail) { threadUnexpectedException(fail); }
169    
170     try {
171     a.get(5L, SECONDS);
172     shouldThrow();
173     } catch (ExecutionException success) {
174     assertSame(t.getClass(), success.getCause().getClass());
175     } catch (Throwable fail) { threadUnexpectedException(fail); }
176     }
177    
178     public static final class FJException extends RuntimeException {
179     public FJException() { super(); }
180     public FJException(Throwable cause) { super(cause); }
181     }
182    
183     // A simple recursive action for testing
184     final class FibAction extends CheckedRecursiveAction {
185     final int number;
186     int result;
187     FibAction(int n) { number = n; }
188     public void realCompute() {
189     int n = number;
190     if (n <= 1)
191     result = n;
192     else {
193     FibAction f1 = new FibAction(n - 1);
194     FibAction f2 = new FibAction(n - 2);
195     invokeAll(f1, f2);
196     result = f1.result + f2.result;
197     }
198     }
199     }
200    
201     // A recursive action failing in base case
202     static final class FailingFibAction extends RecursiveAction {
203     final int number;
204     int result;
205     FailingFibAction(int n) { number = n; }
206     public void compute() {
207     int n = number;
208     if (n <= 1)
209     throw new FJException();
210     else {
211     FailingFibAction f1 = new FailingFibAction(n - 1);
212     FailingFibAction f2 = new FailingFibAction(n - 2);
213     invokeAll(f1, f2);
214     result = f1.result + f2.result;
215     }
216     }
217     }
218    
219     /**
220     * invoke returns when task completes normally.
221     * isCompletedAbnormally and isCancelled return false for normally
222     * completed tasks. getRawResult of a RecursiveAction returns null;
223     */
224     public void testInvoke() {
225     RecursiveAction a = new CheckedRecursiveAction() {
226     public void realCompute() {
227     FibAction f = new FibAction(8);
228     assertNull(f.invoke());
229     assertEquals(21, f.result);
230     checkCompletedNormally(f);
231     }};
232     checkInvoke(a);
233     }
234    
235     /**
236     * quietlyInvoke task returns when task completes normally.
237     * isCompletedAbnormally and isCancelled return false for normally
238     * completed tasks
239     */
240     public void testQuietlyInvoke() {
241     RecursiveAction a = new CheckedRecursiveAction() {
242     public void realCompute() {
243     FibAction f = new FibAction(8);
244     f.quietlyInvoke();
245     assertEquals(21, f.result);
246     checkCompletedNormally(f);
247     }};
248     checkInvoke(a);
249     }
250    
251     /**
252     * join of a forked task returns when task completes
253     */
254     public void testForkJoin() {
255     RecursiveAction a = new CheckedRecursiveAction() {
256     public void realCompute() {
257     FibAction f = new FibAction(8);
258     assertSame(f, f.fork());
259     assertNull(f.join());
260     assertEquals(21, f.result);
261     checkCompletedNormally(f);
262     }};
263     checkInvoke(a);
264     }
265    
266     /**
267     * join/quietlyJoin of a forked task succeeds in the presence of interrupts
268     */
269     public void testJoinIgnoresInterrupts() {
270     RecursiveAction a = new CheckedRecursiveAction() {
271     public void realCompute() {
272     FibAction f = new FibAction(8);
273     final Thread myself = Thread.currentThread();
274    
275     // test join()
276     assertSame(f, f.fork());
277     myself.interrupt();
278     assertTrue(myself.isInterrupted());
279     assertNull(f.join());
280     Thread.interrupted();
281     assertEquals(21, f.result);
282     checkCompletedNormally(f);
283    
284     f = new FibAction(8);
285     f.cancel(true);
286     assertSame(f, f.fork());
287     myself.interrupt();
288     assertTrue(myself.isInterrupted());
289     try {
290     f.join();
291     shouldThrow();
292     } catch (CancellationException success) {
293     Thread.interrupted();
294     checkCancelled(f);
295     }
296    
297     f = new FibAction(8);
298     f.completeExceptionally(new FJException());
299     assertSame(f, f.fork());
300     myself.interrupt();
301     assertTrue(myself.isInterrupted());
302     try {
303     f.join();
304     shouldThrow();
305     } catch (FJException success) {
306     Thread.interrupted();
307     checkCompletedAbnormally(f, success);
308     }
309    
310     // test quietlyJoin()
311     f = new FibAction(8);
312     assertSame(f, f.fork());
313     myself.interrupt();
314     assertTrue(myself.isInterrupted());
315     f.quietlyJoin();
316     Thread.interrupted();
317     assertEquals(21, f.result);
318     checkCompletedNormally(f);
319    
320     f = new FibAction(8);
321     f.cancel(true);
322     assertSame(f, f.fork());
323     myself.interrupt();
324     assertTrue(myself.isInterrupted());
325     f.quietlyJoin();
326     Thread.interrupted();
327     checkCancelled(f);
328    
329     f = new FibAction(8);
330     f.completeExceptionally(new FJException());
331     assertSame(f, f.fork());
332     myself.interrupt();
333     assertTrue(myself.isInterrupted());
334     f.quietlyJoin();
335     Thread.interrupted();
336     checkCompletedAbnormally(f, f.getException());
337     }};
338     checkInvoke(a);
339     a.reinitialize();
340     checkInvoke(a);
341     }
342    
343    
344     /**
345     * get of a forked task returns when task completes
346     */
347     public void testForkGet() {
348     RecursiveAction a = new CheckedRecursiveAction() {
349     public void realCompute() throws Exception {
350     FibAction f = new FibAction(8);
351     assertSame(f, f.fork());
352     assertNull(f.get());
353     assertEquals(21, f.result);
354     checkCompletedNormally(f);
355     }};
356     checkInvoke(a);
357     }
358    
359     /**
360     * timed get of a forked task returns when task completes
361     */
362     public void testForkTimedGet() {
363     RecursiveAction a = new CheckedRecursiveAction() {
364     public void realCompute() throws Exception {
365     FibAction f = new FibAction(8);
366     assertSame(f, f.fork());
367     assertNull(f.get(5L, SECONDS));
368     assertEquals(21, f.result);
369     checkCompletedNormally(f);
370     }};
371     checkInvoke(a);
372     }
373    
374     /**
375     * timed get with null time unit throws NPE
376     */
377     public void testForkTimedGetNPE() {
378     RecursiveAction a = new CheckedRecursiveAction() {
379     public void realCompute() throws Exception {
380     FibAction f = new FibAction(8);
381     assertSame(f, f.fork());
382     try {
383     f.get(5L, null);
384     shouldThrow();
385     } catch (NullPointerException success) {}
386     }};
387     checkInvoke(a);
388     }
389    
390     /**
391     * quietlyJoin of a forked task returns when task completes
392     */
393     public void testForkQuietlyJoin() {
394     RecursiveAction a = new CheckedRecursiveAction() {
395     public void realCompute() {
396     FibAction f = new FibAction(8);
397     assertSame(f, f.fork());
398     f.quietlyJoin();
399     assertEquals(21, f.result);
400     checkCompletedNormally(f);
401     }};
402     checkInvoke(a);
403     }
404    
405     /**
406     * invoke task throws exception when task completes abnormally
407     */
408     public void testAbnormalInvoke() {
409     RecursiveAction a = new CheckedRecursiveAction() {
410     public void realCompute() {
411     FailingFibAction f = new FailingFibAction(8);
412     try {
413     f.invoke();
414     shouldThrow();
415     } catch (FJException success) {
416     checkCompletedAbnormally(f, success);
417     }
418     }};
419     checkInvoke(a);
420     }
421    
422     /**
423     * quietlyInvoke task returns when task completes abnormally
424     */
425     public void testAbnormalQuietlyInvoke() {
426     RecursiveAction a = new CheckedRecursiveAction() {
427     public void realCompute() {
428     FailingFibAction f = new FailingFibAction(8);
429     f.quietlyInvoke();
430     assertTrue(f.getException() instanceof FJException);
431     checkCompletedAbnormally(f, f.getException());
432     }};
433     checkInvoke(a);
434     }
435    
436     /**
437     * join of a forked task throws exception when task completes abnormally
438     */
439     public void testAbnormalForkJoin() {
440     RecursiveAction a = new CheckedRecursiveAction() {
441     public void realCompute() {
442     FailingFibAction f = new FailingFibAction(8);
443     assertSame(f, f.fork());
444     try {
445     f.join();
446     shouldThrow();
447     } catch (FJException success) {
448     checkCompletedAbnormally(f, success);
449     }
450     }};
451     checkInvoke(a);
452     }
453    
454     /**
455     * get of a forked task throws exception when task completes abnormally
456     */
457     public void testAbnormalForkGet() {
458     RecursiveAction a = new CheckedRecursiveAction() {
459     public void realCompute() throws Exception {
460     FailingFibAction f = new FailingFibAction(8);
461     assertSame(f, f.fork());
462     try {
463     f.get();
464     shouldThrow();
465     } catch (ExecutionException success) {
466     Throwable cause = success.getCause();
467     assertTrue(cause instanceof FJException);
468     checkCompletedAbnormally(f, cause);
469     }
470     }};
471     checkInvoke(a);
472     }
473    
474     /**
475     * timed get of a forked task throws exception when task completes abnormally
476     */
477     public void testAbnormalForkTimedGet() {
478     RecursiveAction a = new CheckedRecursiveAction() {
479     public void realCompute() throws Exception {
480     FailingFibAction f = new FailingFibAction(8);
481     assertSame(f, f.fork());
482     try {
483     f.get(5L, TimeUnit.SECONDS);
484     shouldThrow();
485     } catch (ExecutionException success) {
486     Throwable cause = success.getCause();
487     assertTrue(cause instanceof FJException);
488     checkCompletedAbnormally(f, cause);
489     }
490     }};
491     checkInvoke(a);
492     }
493    
494     /**
495     * quietlyJoin of a forked task returns when task completes abnormally
496     */
497     public void testAbnormalForkQuietlyJoin() {
498     RecursiveAction a = new CheckedRecursiveAction() {
499     public void realCompute() {
500     FailingFibAction f = new FailingFibAction(8);
501     assertSame(f, f.fork());
502     f.quietlyJoin();
503     assertTrue(f.getException() instanceof FJException);
504     checkCompletedAbnormally(f, f.getException());
505     }};
506     checkInvoke(a);
507     }
508    
509     /**
510     * invoke task throws exception when task cancelled
511     */
512     public void testCancelledInvoke() {
513     RecursiveAction a = new CheckedRecursiveAction() {
514     public void realCompute() {
515     FibAction f = new FibAction(8);
516     assertTrue(f.cancel(true));
517     try {
518     f.invoke();
519     shouldThrow();
520     } catch (CancellationException success) {
521     checkCancelled(f);
522     }
523     }};
524     checkInvoke(a);
525     }
526    
527     /**
528     * join of a forked task throws exception when task cancelled
529     */
530     public void testCancelledForkJoin() {
531     RecursiveAction a = new CheckedRecursiveAction() {
532     public void realCompute() {
533     FibAction f = new FibAction(8);
534     assertTrue(f.cancel(true));
535     assertSame(f, f.fork());
536     try {
537     f.join();
538     shouldThrow();
539     } catch (CancellationException success) {
540     checkCancelled(f);
541     }
542     }};
543     checkInvoke(a);
544     }
545    
546     /**
547     * get of a forked task throws exception when task cancelled
548     */
549     public void testCancelledForkGet() {
550     RecursiveAction a = new CheckedRecursiveAction() {
551     public void realCompute() throws Exception {
552     FibAction f = new FibAction(8);
553     assertTrue(f.cancel(true));
554     assertSame(f, f.fork());
555     try {
556     f.get();
557     shouldThrow();
558     } catch (CancellationException success) {
559     checkCancelled(f);
560     }
561     }};
562     checkInvoke(a);
563     }
564    
565     /**
566     * timed get of a forked task throws exception when task cancelled
567     */
568     public void testCancelledForkTimedGet() {
569     RecursiveAction a = new CheckedRecursiveAction() {
570     public void realCompute() throws Exception {
571     FibAction f = new FibAction(8);
572     assertTrue(f.cancel(true));
573     assertSame(f, f.fork());
574     try {
575     f.get(5L, SECONDS);
576     shouldThrow();
577     } catch (CancellationException success) {
578     checkCancelled(f);
579     }
580     }};
581     checkInvoke(a);
582     }
583    
584     /**
585     * quietlyJoin of a forked task returns when task cancelled
586     */
587     public void testCancelledForkQuietlyJoin() {
588     RecursiveAction a = new CheckedRecursiveAction() {
589     public void realCompute() {
590     FibAction f = new FibAction(8);
591     assertTrue(f.cancel(true));
592     assertSame(f, f.fork());
593     f.quietlyJoin();
594     checkCancelled(f);
595     }};
596     checkInvoke(a);
597     }
598    
599     /**
600     * inForkJoinPool of non-FJ task returns false
601     */
602     public void testInForkJoinPool2() {
603     RecursiveAction a = new CheckedRecursiveAction() {
604     public void realCompute() {
605     assertFalse(inForkJoinPool());
606     }};
607     assertNull(a.invoke());
608     }
609    
610     /**
611     * A reinitialized normally completed task may be re-invoked
612     */
613     public void testReinitialize() {
614     RecursiveAction a = new CheckedRecursiveAction() {
615     public void realCompute() {
616     FibAction f = new FibAction(8);
617     checkNotDone(f);
618    
619     for (int i = 0; i < 3; i++) {
620     assertNull(f.invoke());
621     assertEquals(21, f.result);
622     checkCompletedNormally(f);
623     f.reinitialize();
624     checkNotDone(f);
625     }
626     }};
627     checkInvoke(a);
628     }
629    
630     /**
631     * A reinitialized abnormally completed task may be re-invoked
632     */
633     public void testReinitializeAbnormal() {
634     RecursiveAction a = new CheckedRecursiveAction() {
635     public void realCompute() {
636     FailingFibAction f = new FailingFibAction(8);
637     checkNotDone(f);
638    
639     for (int i = 0; i < 3; i++) {
640     try {
641     f.invoke();
642     shouldThrow();
643     } catch (FJException success) {
644     checkCompletedAbnormally(f, success);
645     }
646     f.reinitialize();
647     checkNotDone(f);
648     }
649     }};
650     checkInvoke(a);
651     }
652    
653     /**
654     * invoke task throws exception after invoking completeExceptionally
655     */
656     public void testCompleteExceptionally() {
657     RecursiveAction a = new CheckedRecursiveAction() {
658     public void realCompute() {
659     FibAction f = new FibAction(8);
660     f.completeExceptionally(new FJException());
661     try {
662     f.invoke();
663     shouldThrow();
664     } catch (FJException success) {
665     checkCompletedAbnormally(f, success);
666     }
667     }};
668     checkInvoke(a);
669     }
670    
671     /**
672     * invoke task suppresses execution invoking complete
673     */
674     public void testComplete() {
675     RecursiveAction a = new CheckedRecursiveAction() {
676     public void realCompute() {
677     FibAction f = new FibAction(8);
678     f.complete(null);
679     assertNull(f.invoke());
680     assertEquals(0, f.result);
681     checkCompletedNormally(f);
682     }};
683     checkInvoke(a);
684     }
685    
686     /**
687     * invokeAll(t1, t2) invokes all task arguments
688     */
689     public void testInvokeAll2() {
690     RecursiveAction a = new CheckedRecursiveAction() {
691     public void realCompute() {
692     FibAction f = new FibAction(8);
693     FibAction g = new FibAction(9);
694     invokeAll(f, g);
695     checkCompletedNormally(f);
696     assertEquals(21, f.result);
697     checkCompletedNormally(g);
698     assertEquals(34, g.result);
699     }};
700     checkInvoke(a);
701     }
702    
703     /**
704     * invokeAll(tasks) with 1 argument invokes task
705     */
706     public void testInvokeAll1() {
707     RecursiveAction a = new CheckedRecursiveAction() {
708     public void realCompute() {
709     FibAction f = new FibAction(8);
710     invokeAll(f);
711     checkCompletedNormally(f);
712     assertEquals(21, f.result);
713     }};
714     checkInvoke(a);
715     }
716    
717     /**
718     * invokeAll(tasks) with > 2 argument invokes tasks
719     */
720     public void testInvokeAll3() {
721     RecursiveAction a = new CheckedRecursiveAction() {
722     public void realCompute() {
723     FibAction f = new FibAction(8);
724     FibAction g = new FibAction(9);
725     FibAction h = new FibAction(7);
726     invokeAll(f, g, h);
727     assertTrue(f.isDone());
728     assertTrue(g.isDone());
729     assertTrue(h.isDone());
730     checkCompletedNormally(f);
731     assertEquals(21, f.result);
732     checkCompletedNormally(g);
733     assertEquals(34, g.result);
734     checkCompletedNormally(g);
735     assertEquals(13, h.result);
736     }};
737     checkInvoke(a);
738     }
739    
740     /**
741     * invokeAll(collection) invokes all tasks in the collection
742     */
743     public void testInvokeAllCollection() {
744     RecursiveAction a = new CheckedRecursiveAction() {
745     public void realCompute() {
746     FibAction f = new FibAction(8);
747     FibAction g = new FibAction(9);
748     FibAction h = new FibAction(7);
749     HashSet set = new HashSet();
750     set.add(f);
751     set.add(g);
752     set.add(h);
753     invokeAll(set);
754     assertTrue(f.isDone());
755     assertTrue(g.isDone());
756     assertTrue(h.isDone());
757     checkCompletedNormally(f);
758     assertEquals(21, f.result);
759     checkCompletedNormally(g);
760     assertEquals(34, g.result);
761     checkCompletedNormally(g);
762     assertEquals(13, h.result);
763     }};
764     checkInvoke(a);
765     }
766    
767     /**
768     * invokeAll(tasks) with any null task throws NPE
769     */
770     public void testInvokeAllNPE() {
771     RecursiveAction a = new CheckedRecursiveAction() {
772     public void realCompute() {
773     FibAction f = new FibAction(8);
774     FibAction g = new FibAction(9);
775     FibAction h = null;
776     try {
777     invokeAll(f, g, h);
778     shouldThrow();
779     } catch (NullPointerException success) {}
780     }};
781     checkInvoke(a);
782     }
783    
784     /**
785     * invokeAll(t1, t2) throw exception if any task does
786     */
787     public void testAbnormalInvokeAll2() {
788     RecursiveAction a = new CheckedRecursiveAction() {
789     public void realCompute() {
790     FibAction f = new FibAction(8);
791     FailingFibAction g = new FailingFibAction(9);
792     try {
793     invokeAll(f, g);
794     shouldThrow();
795     } catch (FJException success) {
796     checkCompletedAbnormally(g, success);
797     }
798     }};
799     checkInvoke(a);
800     }
801    
802     /**
803     * invokeAll(tasks) with 1 argument throws exception if task does
804     */
805     public void testAbnormalInvokeAll1() {
806     RecursiveAction a = new CheckedRecursiveAction() {
807     public void realCompute() {
808     FailingFibAction g = new FailingFibAction(9);
809     try {
810     invokeAll(g);
811     shouldThrow();
812     } catch (FJException success) {
813     checkCompletedAbnormally(g, success);
814     }
815     }};
816     checkInvoke(a);
817     }
818    
819     /**
820     * invokeAll(tasks) with > 2 argument throws exception if any task does
821     */
822     public void testAbnormalInvokeAll3() {
823     RecursiveAction a = new CheckedRecursiveAction() {
824     public void realCompute() {
825     FibAction f = new FibAction(8);
826     FailingFibAction g = new FailingFibAction(9);
827     FibAction h = new FibAction(7);
828     try {
829     invokeAll(f, g, h);
830     shouldThrow();
831     } catch (FJException success) {
832     checkCompletedAbnormally(g, success);
833     }
834     }};
835     checkInvoke(a);
836     }
837    
838     /**
839     * invokeAll(collection) throws exception if any task does
840     */
841     public void testAbnormalInvokeAllCollection() {
842     RecursiveAction a = new CheckedRecursiveAction() {
843     public void realCompute() {
844     FailingFibAction f = new FailingFibAction(8);
845     FibAction g = new FibAction(9);
846     FibAction h = new FibAction(7);
847     HashSet set = new HashSet();
848     set.add(f);
849     set.add(g);
850     set.add(h);
851     try {
852     invokeAll(set);
853     shouldThrow();
854     } catch (FJException success) {
855     checkCompletedAbnormally(f, success);
856     }
857     }};
858     checkInvoke(a);
859     }
860    
861     // CountedCompleter versions
862    
863     public abstract class CheckedFJTask extends RecursiveAction {
864     protected abstract void realCompute() throws Throwable;
865    
866     public final void compute() {
867     try {
868     realCompute();
869     } catch (Throwable t) {
870     threadUnexpectedException(t);
871     }
872     }
873     }
874    
875 jsr166 1.5 abstract static class CCF extends CountedCompleter {
876 dl 1.4 int number;
877     int rnumber;
878    
879     public CCF(CountedCompleter parent, int n) {
880     super(parent, 1);
881     this.number = n;
882     }
883    
884     public final void compute() {
885     CountedCompleter p;
886     CCF f = this;
887     int n = number;
888     while (n >= 2) {
889     new RCCF(f, n - 2).fork();
890     f = new LCCF(f, --n);
891     }
892     f.number = n;
893     f.onCompletion(f);
894     if ((p = f.getCompleter()) != null)
895     p.tryComplete();
896 jsr166 1.5 else
897     f.quietlyComplete();
898 dl 1.4 }
899     }
900    
901     static final class LCCF extends CCF {
902     public LCCF(CountedCompleter parent, int n) {
903     super(parent, n);
904     }
905     public final void onCompletion(CountedCompleter caller) {
906     CCF p = (CCF)getCompleter();
907     int n = number + rnumber;
908     if (p != null)
909     p.number = n;
910     else
911     number = n;
912     }
913     }
914     static final class RCCF extends CCF {
915     public RCCF(CountedCompleter parent, int n) {
916     super(parent, n);
917     }
918     public final void onCompletion(CountedCompleter caller) {
919     CCF p = (CCF)getCompleter();
920     int n = number + rnumber;
921     if (p != null)
922     p.rnumber = n;
923     else
924     number = n;
925     }
926     }
927    
928     // Version of CCF with forced failure in left completions
929 jsr166 1.5 abstract static class FailingCCF extends CountedCompleter {
930 dl 1.4 int number;
931     int rnumber;
932    
933     public FailingCCF(CountedCompleter parent, int n) {
934     super(parent, 1);
935     this.number = n;
936     }
937    
938     public final void compute() {
939     CountedCompleter p;
940     FailingCCF f = this;
941     int n = number;
942     while (n >= 2) {
943     new RFCCF(f, n - 2).fork();
944     f = new LFCCF(f, --n);
945     }
946     f.number = n;
947     f.onCompletion(f);
948     if ((p = f.getCompleter()) != null)
949     p.tryComplete();
950 jsr166 1.5 else
951     f.quietlyComplete();
952 dl 1.4 }
953     }
954    
955     static final class LFCCF extends FailingCCF {
956     public LFCCF(CountedCompleter parent, int n) {
957     super(parent, n);
958     }
959     public final void onCompletion(CountedCompleter caller) {
960     FailingCCF p = (FailingCCF)getCompleter();
961     int n = number + rnumber;
962     if (p != null)
963     p.number = n;
964     else
965     number = n;
966     }
967     }
968     static final class RFCCF extends FailingCCF {
969     public RFCCF(CountedCompleter parent, int n) {
970     super(parent, n);
971     }
972     public final void onCompletion(CountedCompleter caller) {
973     completeExceptionally(new FJException());
974     }
975     }
976 jsr166 1.5
977 dl 1.4 /**
978     * invoke returns when task completes normally.
979     * isCompletedAbnormally and isCancelled return false for normally
980     * completed tasks; getRawResult returns null.
981     */
982     public void testInvokeCC() {
983     ForkJoinTask a = new CheckedFJTask() {
984     public void realCompute() {
985     CCF f = new LCCF(null, 8);
986     assertNull(f.invoke());
987     assertEquals(21, f.number);
988     checkCompletedNormally(f);
989     }};
990     checkInvoke(a);
991     }
992    
993     /**
994     * quietlyInvoke task returns when task completes normally.
995     * isCompletedAbnormally and isCancelled return false for normally
996     * completed tasks
997     */
998     public void testQuietlyInvokeCC() {
999     ForkJoinTask a = new CheckedFJTask() {
1000     public void realCompute() {
1001     CCF f = new LCCF(null, 8);
1002     f.quietlyInvoke();
1003     assertEquals(21, f.number);
1004     checkCompletedNormally(f);
1005     }};
1006     checkInvoke(a);
1007     }
1008    
1009     /**
1010     * join of a forked task returns when task completes
1011     */
1012     public void testForkJoinCC() {
1013     ForkJoinTask a = new CheckedFJTask() {
1014     public void realCompute() {
1015     CCF f = new LCCF(null, 8);
1016     assertSame(f, f.fork());
1017     assertNull(f.join());
1018     assertEquals(21, f.number);
1019     checkCompletedNormally(f);
1020     }};
1021     checkInvoke(a);
1022     }
1023    
1024     /**
1025     * get of a forked task returns when task completes
1026     */
1027     public void testForkGetCC() {
1028     ForkJoinTask a = new CheckedFJTask() {
1029     public void realCompute() throws Exception {
1030     CCF f = new LCCF(null, 8);
1031     assertSame(f, f.fork());
1032     assertNull(f.get());
1033     assertEquals(21, f.number);
1034     checkCompletedNormally(f);
1035     }};
1036     checkInvoke(a);
1037     }
1038    
1039     /**
1040     * timed get of a forked task returns when task completes
1041     */
1042     public void testForkTimedGetCC() {
1043     ForkJoinTask a = new CheckedFJTask() {
1044     public void realCompute() throws Exception {
1045     CCF f = new LCCF(null, 8);
1046     assertSame(f, f.fork());
1047     assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1048     assertEquals(21, f.number);
1049     checkCompletedNormally(f);
1050     }};
1051     checkInvoke(a);
1052     }
1053    
1054     /**
1055     * timed get with null time unit throws NPE
1056     */
1057     public void testForkTimedGetNPECC() {
1058     ForkJoinTask a = new CheckedFJTask() {
1059     public void realCompute() throws Exception {
1060     CCF f = new LCCF(null, 8);
1061     assertSame(f, f.fork());
1062     try {
1063     f.get(5L, null);
1064     shouldThrow();
1065     } catch (NullPointerException success) {}
1066     }};
1067     checkInvoke(a);
1068     }
1069    
1070     /**
1071     * quietlyJoin of a forked task returns when task completes
1072     */
1073     public void testForkQuietlyJoinCC() {
1074     ForkJoinTask a = new CheckedFJTask() {
1075     public void realCompute() {
1076     CCF f = new LCCF(null, 8);
1077     assertSame(f, f.fork());
1078     f.quietlyJoin();
1079     assertEquals(21, f.number);
1080     checkCompletedNormally(f);
1081     }};
1082     checkInvoke(a);
1083     }
1084    
1085     /**
1086     * invoke task throws exception when task completes abnormally
1087     */
1088     public void testAbnormalInvokeCC() {
1089     ForkJoinTask a = new CheckedFJTask() {
1090     public void realCompute() {
1091     FailingCCF f = new LFCCF(null, 8);
1092     try {
1093     f.invoke();
1094     shouldThrow();
1095     } catch (FJException success) {
1096     checkCompletedAbnormally(f, success);
1097     }
1098     }};
1099     checkInvoke(a);
1100     }
1101    
1102     /**
1103     * quietlyInvoke task returns when task completes abnormally
1104     */
1105     public void testAbnormalQuietlyInvokeCC() {
1106     ForkJoinTask a = new CheckedFJTask() {
1107     public void realCompute() {
1108     FailingCCF f = new LFCCF(null, 8);
1109     f.quietlyInvoke();
1110     assertTrue(f.getException() instanceof FJException);
1111     checkCompletedAbnormally(f, f.getException());
1112     }};
1113     checkInvoke(a);
1114     }
1115    
1116     /**
1117     * join of a forked task throws exception when task completes abnormally
1118     */
1119     public void testAbnormalForkJoinCC() {
1120     ForkJoinTask a = new CheckedFJTask() {
1121     public void realCompute() {
1122     FailingCCF f = new LFCCF(null, 8);
1123     assertSame(f, f.fork());
1124     try {
1125     f.join();
1126     shouldThrow();
1127     } catch (FJException success) {
1128     checkCompletedAbnormally(f, success);
1129     }
1130     }};
1131     checkInvoke(a);
1132     }
1133    
1134     /**
1135     * get of a forked task throws exception when task completes abnormally
1136     */
1137     public void testAbnormalForkGetCC() {
1138     ForkJoinTask a = new CheckedFJTask() {
1139     public void realCompute() throws Exception {
1140     FailingCCF f = new LFCCF(null, 8);
1141     assertSame(f, f.fork());
1142     try {
1143     f.get();
1144     shouldThrow();
1145     } catch (ExecutionException success) {
1146     Throwable cause = success.getCause();
1147     assertTrue(cause instanceof FJException);
1148     checkCompletedAbnormally(f, cause);
1149     }
1150     }};
1151     checkInvoke(a);
1152     }
1153    
1154     /**
1155     * timed get of a forked task throws exception when task completes abnormally
1156     */
1157     public void testAbnormalForkTimedGetCC() {
1158     ForkJoinTask a = new CheckedFJTask() {
1159     public void realCompute() throws Exception {
1160     FailingCCF f = new LFCCF(null, 8);
1161     assertSame(f, f.fork());
1162     try {
1163     f.get(LONG_DELAY_MS, MILLISECONDS);
1164     shouldThrow();
1165     } catch (ExecutionException success) {
1166     Throwable cause = success.getCause();
1167     assertTrue(cause instanceof FJException);
1168     checkCompletedAbnormally(f, cause);
1169     }
1170     }};
1171     checkInvoke(a);
1172     }
1173    
1174     /**
1175     * quietlyJoin of a forked task returns when task completes abnormally
1176     */
1177     public void testAbnormalForkQuietlyJoinCC() {
1178     ForkJoinTask a = new CheckedFJTask() {
1179     public void realCompute() {
1180     FailingCCF f = new LFCCF(null, 8);
1181     assertSame(f, f.fork());
1182     f.quietlyJoin();
1183     assertTrue(f.getException() instanceof FJException);
1184     checkCompletedAbnormally(f, f.getException());
1185     }};
1186     checkInvoke(a);
1187     }
1188    
1189     /**
1190     * invoke task throws exception when task cancelled
1191     */
1192     public void testCancelledInvokeCC() {
1193     ForkJoinTask a = new CheckedFJTask() {
1194     public void realCompute() {
1195     CCF f = new LCCF(null, 8);
1196     assertTrue(f.cancel(true));
1197     try {
1198     f.invoke();
1199     shouldThrow();
1200     } catch (CancellationException success) {
1201     checkCancelled(f);
1202     }
1203     }};
1204     checkInvoke(a);
1205     }
1206    
1207     /**
1208     * join of a forked task throws exception when task cancelled
1209     */
1210     public void testCancelledForkJoinCC() {
1211     ForkJoinTask a = new CheckedFJTask() {
1212     public void realCompute() {
1213     CCF f = new LCCF(null, 8);
1214     assertTrue(f.cancel(true));
1215     assertSame(f, f.fork());
1216     try {
1217     f.join();
1218     shouldThrow();
1219     } catch (CancellationException success) {
1220     checkCancelled(f);
1221     }
1222     }};
1223     checkInvoke(a);
1224     }
1225    
1226     /**
1227     * get of a forked task throws exception when task cancelled
1228     */
1229     public void testCancelledForkGetCC() {
1230     ForkJoinTask a = new CheckedFJTask() {
1231     public void realCompute() throws Exception {
1232     CCF f = new LCCF(null, 8);
1233     assertTrue(f.cancel(true));
1234     assertSame(f, f.fork());
1235     try {
1236     f.get();
1237     shouldThrow();
1238     } catch (CancellationException success) {
1239     checkCancelled(f);
1240     }
1241     }};
1242     checkInvoke(a);
1243     }
1244    
1245     /**
1246     * timed get of a forked task throws exception when task cancelled
1247     */
1248     public void testCancelledForkTimedGetCC() throws Exception {
1249     ForkJoinTask a = new CheckedFJTask() {
1250     public void realCompute() throws Exception {
1251     CCF f = new LCCF(null, 8);
1252     assertTrue(f.cancel(true));
1253     assertSame(f, f.fork());
1254     try {
1255     f.get(LONG_DELAY_MS, MILLISECONDS);
1256     shouldThrow();
1257     } catch (CancellationException success) {
1258     checkCancelled(f);
1259     }
1260     }};
1261     checkInvoke(a);
1262     }
1263    
1264     /**
1265     * quietlyJoin of a forked task returns when task cancelled
1266     */
1267     public void testCancelledForkQuietlyJoinCC() {
1268     ForkJoinTask a = new CheckedFJTask() {
1269     public void realCompute() {
1270     CCF f = new LCCF(null, 8);
1271     assertTrue(f.cancel(true));
1272     assertSame(f, f.fork());
1273     f.quietlyJoin();
1274     checkCancelled(f);
1275     }};
1276     checkInvoke(a);
1277     }
1278    
1279     /**
1280     * getPool of non-FJ task returns null
1281     */
1282     public void testGetPool2CC() {
1283     ForkJoinTask a = new CheckedFJTask() {
1284     public void realCompute() {
1285     assertNull(getPool());
1286     }};
1287     assertNull(a.invoke());
1288     }
1289    
1290     /**
1291     * inForkJoinPool of non-FJ task returns false
1292     */
1293     public void testInForkJoinPool2CC() {
1294     ForkJoinTask a = new CheckedFJTask() {
1295     public void realCompute() {
1296     assertFalse(inForkJoinPool());
1297     }};
1298     assertNull(a.invoke());
1299     }
1300    
1301     /**
1302     * setRawResult(null) succeeds
1303     */
1304     public void testSetRawResultCC() {
1305     ForkJoinTask a = new CheckedFJTask() {
1306     public void realCompute() {
1307     setRawResult(null);
1308     assertNull(getRawResult());
1309     }};
1310     assertNull(a.invoke());
1311     }
1312    
1313     /**
1314     * invoke task throws exception after invoking completeExceptionally
1315     */
1316     public void testCompleteExceptionally2CC() {
1317     ForkJoinTask a = new CheckedFJTask() {
1318     public void realCompute() {
1319     CCF f = new LCCF(null, 8);
1320     f.completeExceptionally(new FJException());
1321     try {
1322     f.invoke();
1323     shouldThrow();
1324     } catch (FJException success) {
1325     checkCompletedAbnormally(f, success);
1326     }
1327     }};
1328     checkInvoke(a);
1329     }
1330    
1331     /**
1332     * invokeAll(t1, t2) invokes all task arguments
1333     */
1334     public void testInvokeAll2CC() {
1335     ForkJoinTask a = new CheckedFJTask() {
1336     public void realCompute() {
1337     CCF f = new LCCF(null, 8);
1338     CCF g = new LCCF(null, 9);
1339     invokeAll(f, g);
1340     assertEquals(21, f.number);
1341     assertEquals(34, g.number);
1342     checkCompletedNormally(f);
1343     checkCompletedNormally(g);
1344     }};
1345     checkInvoke(a);
1346     }
1347    
1348     /**
1349     * invokeAll(tasks) with 1 argument invokes task
1350     */
1351     public void testInvokeAll1CC() {
1352     ForkJoinTask a = new CheckedFJTask() {
1353     public void realCompute() {
1354     CCF f = new LCCF(null, 8);
1355     invokeAll(f);
1356     checkCompletedNormally(f);
1357     assertEquals(21, f.number);
1358     }};
1359     checkInvoke(a);
1360     }
1361    
1362     /**
1363     * invokeAll(tasks) with > 2 argument invokes tasks
1364     */
1365     public void testInvokeAll3CC() {
1366     ForkJoinTask a = new CheckedFJTask() {
1367     public void realCompute() {
1368     CCF f = new LCCF(null, 8);
1369     CCF g = new LCCF(null, 9);
1370     CCF h = new LCCF(null, 7);
1371     invokeAll(f, g, h);
1372     assertEquals(21, f.number);
1373     assertEquals(34, g.number);
1374     assertEquals(13, h.number);
1375     checkCompletedNormally(f);
1376     checkCompletedNormally(g);
1377     checkCompletedNormally(h);
1378     }};
1379     checkInvoke(a);
1380     }
1381    
1382     /**
1383     * invokeAll(collection) invokes all tasks in the collection
1384     */
1385     public void testInvokeAllCollectionCC() {
1386     ForkJoinTask a = new CheckedFJTask() {
1387     public void realCompute() {
1388     CCF f = new LCCF(null, 8);
1389     CCF g = new LCCF(null, 9);
1390     CCF h = new LCCF(null, 7);
1391     HashSet set = new HashSet();
1392     set.add(f);
1393     set.add(g);
1394     set.add(h);
1395     invokeAll(set);
1396     assertEquals(21, f.number);
1397     assertEquals(34, g.number);
1398     assertEquals(13, h.number);
1399     checkCompletedNormally(f);
1400     checkCompletedNormally(g);
1401     checkCompletedNormally(h);
1402     }};
1403     checkInvoke(a);
1404     }
1405    
1406     /**
1407     * invokeAll(tasks) with any null task throws NPE
1408     */
1409     public void testInvokeAllNPECC() {
1410     ForkJoinTask a = new CheckedFJTask() {
1411     public void realCompute() {
1412     CCF f = new LCCF(null, 8);
1413     CCF g = new LCCF(null, 9);
1414     CCF h = null;
1415     try {
1416     invokeAll(f, g, h);
1417     shouldThrow();
1418     } catch (NullPointerException success) {}
1419     }};
1420     checkInvoke(a);
1421     }
1422    
1423     /**
1424     * invokeAll(t1, t2) throw exception if any task does
1425     */
1426     public void testAbnormalInvokeAll2CC() {
1427     ForkJoinTask a = new CheckedFJTask() {
1428     public void realCompute() {
1429     CCF f = new LCCF(null, 8);
1430     FailingCCF g = new LFCCF(null, 9);
1431     try {
1432     invokeAll(f, g);
1433     shouldThrow();
1434     } catch (FJException success) {
1435     checkCompletedAbnormally(g, success);
1436     }
1437     }};
1438     checkInvoke(a);
1439     }
1440    
1441     /**
1442     * invokeAll(tasks) with 1 argument throws exception if task does
1443     */
1444     public void testAbnormalInvokeAll1CC() {
1445     ForkJoinTask a = new CheckedFJTask() {
1446     public void realCompute() {
1447     FailingCCF g = new LFCCF(null, 9);
1448     try {
1449     invokeAll(g);
1450     shouldThrow();
1451     } catch (FJException success) {
1452     checkCompletedAbnormally(g, success);
1453     }
1454     }};
1455     checkInvoke(a);
1456     }
1457    
1458     /**
1459     * invokeAll(tasks) with > 2 argument throws exception if any task does
1460     */
1461     public void testAbnormalInvokeAll3CC() {
1462     ForkJoinTask a = new CheckedFJTask() {
1463     public void realCompute() {
1464     CCF f = new LCCF(null, 8);
1465     FailingCCF g = new LFCCF(null, 9);
1466     CCF h = new LCCF(null, 7);
1467     try {
1468     invokeAll(f, g, h);
1469     shouldThrow();
1470     } catch (FJException success) {
1471     checkCompletedAbnormally(g, success);
1472     }
1473     }};
1474     checkInvoke(a);
1475     }
1476    
1477     /**
1478     * invokeAll(collection) throws exception if any task does
1479     */
1480     public void testAbnormalInvokeAllCollectionCC() {
1481     ForkJoinTask a = new CheckedFJTask() {
1482     public void realCompute() {
1483     FailingCCF f = new LFCCF(null, 8);
1484     CCF g = new LCCF(null, 9);
1485     CCF h = new LCCF(null, 7);
1486     HashSet set = new HashSet();
1487     set.add(f);
1488     set.add(g);
1489     set.add(h);
1490     try {
1491     invokeAll(set);
1492     shouldThrow();
1493     } catch (FJException success) {
1494     checkCompletedAbnormally(f, success);
1495     }
1496     }};
1497     checkInvoke(a);
1498     }
1499    
1500 jsr166 1.1 }