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

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