ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountedCompleterTest.java
Revision: 1.31
Committed: Wed Jan 4 06:09:58 2017 UTC (7 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.30: +1 -1 lines
Log Message:
convert to Diamond

File Contents

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