ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountedCompleterTest.java
Revision: 1.22
Committed: Sun Oct 18 18:54:49 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.21: +1 -0 lines
Log Message:
improve testComplete

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