ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPool8Test.java
Revision: 1.34
Committed: Tue Aug 16 23:17:13 2016 UTC (7 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.33: +0 -6 lines
Log Message:
stop checking that Thread.interrupt() works

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