ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountedCompleterTest.java
Revision: 1.32
Committed: Sat Mar 11 17:33:32 2017 UTC (7 years, 2 months ago) by jsr166
Branch: MAIN
Changes since 1.31: +0 -1 lines
Log Message:
fix unused imports reported by errorprone [RemoveUnusedImports]

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