ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountedCompleterTest.java
Revision: 1.34
Committed: Sat Oct 21 06:53:36 2017 UTC (6 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.33: +1 -1 lines
Log Message:
better exception handling

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