ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountedCompleterTest.java
Revision: 1.10
Committed: Thu Jun 6 00:40:13 2013 UTC (10 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.9: +11 -5 lines
Log Message:
clarify and test that complete() ignores pending count

File Contents

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