ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountedCompleterTest.java
Revision: 1.21
Committed: Sun Oct 18 16:43:53 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.20: +19 -10 lines
Log Message:
improve testDecrementPendingCount; fix testCompleteExceptionally_null

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