ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountedCompleterTest.java
Revision: 1.9
Committed: Wed Jun 5 05:48:26 2013 UTC (10 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.8: +14 -7 lines
Log Message:
improve firstComplete/nextComplete tests

File Contents

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