ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinTaskTest.java
Revision: 1.27
Committed: Mon Nov 22 07:50:50 2010 UTC (13 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.26: +1 -0 lines
Log Message:
small improvement to testSetRawResult

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/licenses/publicdomain
5     */
6 dl 1.8 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.ForkJoinWorkerThread;
11     import java.util.concurrent.RecursiveAction;
12     import java.util.concurrent.TimeUnit;
13 jsr166 1.24 import java.util.concurrent.TimeoutException;
14 jsr166 1.15 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
15 jsr166 1.18 import static java.util.concurrent.TimeUnit.MILLISECONDS;
16 jsr166 1.24 import static java.util.concurrent.TimeUnit.SECONDS;
17 jsr166 1.15 import java.util.HashSet;
18 jsr166 1.13 import junit.framework.*;
19 dl 1.1
20     public class ForkJoinTaskTest extends JSR166TestCase {
21    
22     public static void main(String[] args) {
23 jsr166 1.9 junit.textui.TestRunner.run(suite());
24 dl 1.1 }
25 jsr166 1.12
26 dl 1.1 public static Test suite() {
27 jsr166 1.5 return new TestSuite(ForkJoinTaskTest.class);
28 dl 1.1 }
29    
30 dl 1.20 // Runs with "mainPool" use > 1 thread. singletonPool tests use 1
31 jsr166 1.21 static final int mainPoolSize =
32 dl 1.20 Math.max(2, Runtime.getRuntime().availableProcessors());
33    
34 jsr166 1.14 private static ForkJoinPool mainPool() {
35 dl 1.20 return new ForkJoinPool(mainPoolSize);
36 jsr166 1.14 }
37    
38     private static ForkJoinPool singletonPool() {
39     return new ForkJoinPool(1);
40     }
41    
42     private static ForkJoinPool asyncSingletonPool() {
43     return new ForkJoinPool(1,
44     ForkJoinPool.defaultForkJoinWorkerThreadFactory,
45     null, true);
46     }
47    
48     private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
49     try {
50 jsr166 1.17 assertFalse(a.isDone());
51     assertFalse(a.isCompletedNormally());
52     assertFalse(a.isCompletedAbnormally());
53     assertFalse(a.isCancelled());
54     assertNull(a.getException());
55 jsr166 1.24 assertNull(a.getRawResult());
56 jsr166 1.17
57     assertNull(pool.invoke(a));
58    
59     assertTrue(a.isDone());
60     assertTrue(a.isCompletedNormally());
61     assertFalse(a.isCompletedAbnormally());
62     assertFalse(a.isCancelled());
63     assertNull(a.getException());
64 jsr166 1.24 assertNull(a.getRawResult());
65 jsr166 1.14 } finally {
66     joinPool(pool);
67     }
68     }
69    
70 jsr166 1.24 void checkNotDone(ForkJoinTask a) {
71     assertFalse(a.isDone());
72     assertFalse(a.isCompletedNormally());
73     assertFalse(a.isCompletedAbnormally());
74     assertFalse(a.isCancelled());
75     assertNull(a.getException());
76     assertNull(a.getRawResult());
77    
78     try {
79     a.get(0L, SECONDS);
80     shouldThrow();
81     } catch (TimeoutException success) {
82     } catch (Throwable fail) { threadUnexpectedException(fail); }
83     }
84    
85     <T> void checkCompletedNormally(ForkJoinTask<T> a) {
86     checkCompletedNormally(a, null);
87     }
88    
89     <T> void checkCompletedNormally(ForkJoinTask<T> a, T expected) {
90     assertTrue(a.isDone());
91     assertFalse(a.isCancelled());
92     assertTrue(a.isCompletedNormally());
93     assertFalse(a.isCompletedAbnormally());
94     assertNull(a.getException());
95     assertSame(expected, a.getRawResult());
96     assertSame(expected, a.join());
97 jsr166 1.25 assertFalse(a.cancel(false));
98     assertFalse(a.cancel(true));
99 jsr166 1.24 try {
100     assertSame(expected, a.get());
101     } catch (Throwable fail) { threadUnexpectedException(fail); }
102     try {
103     assertSame(expected, a.get(5L, SECONDS));
104     } catch (Throwable fail) { threadUnexpectedException(fail); }
105     }
106    
107     void checkCancelled(ForkJoinTask a) {
108     assertTrue(a.isDone());
109     assertTrue(a.isCancelled());
110     assertFalse(a.isCompletedNormally());
111     assertTrue(a.isCompletedAbnormally());
112     assertTrue(a.getException() instanceof CancellationException);
113     assertNull(a.getRawResult());
114 jsr166 1.25 assertTrue(a.cancel(false));
115     assertTrue(a.cancel(true));
116 jsr166 1.24
117     try {
118     a.join();
119     shouldThrow();
120     } catch (CancellationException success) {
121     } catch (Throwable fail) { threadUnexpectedException(fail); }
122    
123     try {
124     a.get();
125     shouldThrow();
126     } catch (CancellationException success) {
127     } catch (Throwable fail) { threadUnexpectedException(fail); }
128    
129     try {
130     a.get(5L, SECONDS);
131     shouldThrow();
132     } catch (CancellationException success) {
133     } catch (Throwable fail) { threadUnexpectedException(fail); }
134     }
135    
136     void checkCompletedAbnormally(ForkJoinTask a, Throwable t) {
137     assertTrue(a.isDone());
138     assertFalse(a.isCancelled());
139     assertFalse(a.isCompletedNormally());
140     assertTrue(a.isCompletedAbnormally());
141     assertSame(t, a.getException());
142     assertNull(a.getRawResult());
143 jsr166 1.25 assertFalse(a.cancel(false));
144     assertFalse(a.cancel(true));
145 jsr166 1.24
146     try {
147     a.join();
148     shouldThrow();
149     } catch (Throwable expected) {
150     assertSame(t, expected);
151     }
152    
153     try {
154     a.get();
155     shouldThrow();
156     } catch (ExecutionException success) {
157     assertSame(t, success.getCause());
158     } catch (Throwable fail) { threadUnexpectedException(fail); }
159    
160     try {
161     a.get(5L, SECONDS);
162     shouldThrow();
163     } catch (ExecutionException success) {
164     assertSame(t, success.getCause());
165     } catch (Throwable fail) { threadUnexpectedException(fail); }
166     }
167    
168 jsr166 1.14 /*
169 dl 1.1 * Testing coverage notes:
170 jsr166 1.2 *
171 dl 1.1 * To test extension methods and overrides, most tests use
172     * BinaryAsyncAction extension class that processes joins
173     * differently than supplied Recursive forms.
174 jsr166 1.2 */
175 dl 1.1
176     static final class FJException extends RuntimeException {
177     FJException() { super(); }
178     }
179    
180 jsr166 1.19 abstract static class BinaryAsyncAction extends ForkJoinTask<Void> {
181 dl 1.1 private volatile int controlState;
182    
183     static final AtomicIntegerFieldUpdater<BinaryAsyncAction> controlStateUpdater =
184 jsr166 1.2 AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class,
185 dl 1.1 "controlState");
186    
187     private BinaryAsyncAction parent;
188    
189     private BinaryAsyncAction sibling;
190    
191     protected BinaryAsyncAction() {
192     }
193    
194     public final Void getRawResult() { return null; }
195     protected final void setRawResult(Void mustBeNull) { }
196    
197     public final void linkSubtasks(BinaryAsyncAction x, BinaryAsyncAction y) {
198     x.parent = y.parent = this;
199     x.sibling = y;
200     y.sibling = x;
201     }
202    
203     protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) {
204     }
205    
206     protected boolean onException() {
207     return true;
208     }
209    
210     public void linkAndForkSubtasks(BinaryAsyncAction x, BinaryAsyncAction y) {
211     linkSubtasks(x, y);
212     y.fork();
213     x.fork();
214     }
215    
216     private void completeThis() {
217     super.complete(null);
218     }
219    
220     private void completeThisExceptionally(Throwable ex) {
221     super.completeExceptionally(ex);
222     }
223    
224     public final void complete() {
225     BinaryAsyncAction a = this;
226     for (;;) {
227     BinaryAsyncAction s = a.sibling;
228     BinaryAsyncAction p = a.parent;
229     a.sibling = null;
230     a.parent = null;
231     a.completeThis();
232     if (p == null || p.compareAndSetControlState(0, 1))
233     break;
234     try {
235     p.onComplete(a, s);
236 jsr166 1.2 } catch (Throwable rex) {
237 dl 1.1 p.completeExceptionally(rex);
238     return;
239     }
240     a = p;
241     }
242     }
243    
244     public final void completeExceptionally(Throwable ex) {
245     BinaryAsyncAction a = this;
246     while (!a.isCompletedAbnormally()) {
247     a.completeThisExceptionally(ex);
248     BinaryAsyncAction s = a.sibling;
249     if (s != null)
250     s.cancel(false);
251     if (!a.onException() || (a = a.parent) == null)
252     break;
253     }
254     }
255    
256     public final BinaryAsyncAction getParent() {
257     return parent;
258     }
259    
260     public BinaryAsyncAction getSibling() {
261     return sibling;
262     }
263    
264     public void reinitialize() {
265     parent = sibling = null;
266     super.reinitialize();
267     }
268    
269     protected final int getControlState() {
270     return controlState;
271     }
272    
273 jsr166 1.2 protected final boolean compareAndSetControlState(int expect,
274 dl 1.1 int update) {
275     return controlStateUpdater.compareAndSet(this, expect, update);
276     }
277    
278     protected final void setControlState(int value) {
279     controlState = value;
280     }
281    
282     protected final void incrementControlState() {
283     controlStateUpdater.incrementAndGet(this);
284     }
285    
286     protected final void decrementControlState() {
287     controlStateUpdater.decrementAndGet(this);
288     }
289    
290     }
291    
292 jsr166 1.6 static final class AsyncFib extends BinaryAsyncAction {
293 dl 1.1 int number;
294 jsr166 1.2 public AsyncFib(int n) {
295 dl 1.1 this.number = n;
296     }
297 jsr166 1.2
298 dl 1.1 public final boolean exec() {
299     AsyncFib f = this;
300     int n = f.number;
301     if (n > 1) {
302     while (n > 1) {
303     AsyncFib p = f;
304     AsyncFib r = new AsyncFib(n - 2);
305     f = new AsyncFib(--n);
306     p.linkSubtasks(r, f);
307     r.fork();
308     }
309     f.number = n;
310     }
311     f.complete();
312     return false;
313     }
314    
315     protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) {
316     number = ((AsyncFib)x).number + ((AsyncFib)y).number;
317     }
318     }
319    
320    
321 jsr166 1.6 static final class FailingAsyncFib extends BinaryAsyncAction {
322 dl 1.1 int number;
323 jsr166 1.2 public FailingAsyncFib(int n) {
324 dl 1.1 this.number = n;
325     }
326 jsr166 1.2
327 dl 1.1 public final boolean exec() {
328     FailingAsyncFib f = this;
329     int n = f.number;
330     if (n > 1) {
331     while (n > 1) {
332     FailingAsyncFib p = f;
333     FailingAsyncFib r = new FailingAsyncFib(n - 2);
334     f = new FailingAsyncFib(--n);
335     p.linkSubtasks(r, f);
336     r.fork();
337     }
338     f.number = n;
339     }
340     f.complete();
341     return false;
342     }
343    
344     protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) {
345     completeExceptionally(new FJException());
346     }
347     }
348    
349 jsr166 1.2 /**
350 dl 1.1 * invoke returns when task completes normally.
351     * isCompletedAbnormally and isCancelled return false for normally
352 jsr166 1.26 * completed tasks; getRawResult returns null.
353 dl 1.1 */
354     public void testInvoke() {
355 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
356     public void realCompute() {
357 jsr166 1.12 AsyncFib f = new AsyncFib(8);
358 jsr166 1.17 assertNull(f.invoke());
359     assertEquals(21, f.number);
360 jsr166 1.24 checkCompletedNormally(f);
361 jsr166 1.12 }};
362 jsr166 1.14 testInvokeOnPool(mainPool(), a);
363 dl 1.1 }
364    
365 jsr166 1.2 /**
366 dl 1.1 * quietlyInvoke task returns when task completes normally.
367     * isCompletedAbnormally and isCancelled return false for normally
368     * completed tasks
369     */
370     public void testQuietlyInvoke() {
371 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
372     public void realCompute() {
373 jsr166 1.12 AsyncFib f = new AsyncFib(8);
374     f.quietlyInvoke();
375 jsr166 1.17 assertEquals(21, f.number);
376 jsr166 1.24 checkCompletedNormally(f);
377 jsr166 1.12 }};
378 jsr166 1.14 testInvokeOnPool(mainPool(), a);
379 dl 1.1 }
380    
381 jsr166 1.2 /**
382 dl 1.1 * join of a forked task returns when task completes
383     */
384     public void testForkJoin() {
385 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
386     public void realCompute() {
387 jsr166 1.12 AsyncFib f = new AsyncFib(8);
388 jsr166 1.17 assertSame(f, f.fork());
389     assertNull(f.join());
390     assertEquals(21, f.number);
391 jsr166 1.24 checkCompletedNormally(f);
392 jsr166 1.12 }};
393 jsr166 1.14 testInvokeOnPool(mainPool(), a);
394 dl 1.1 }
395    
396 jsr166 1.2 /**
397 dl 1.1 * get of a forked task returns when task completes
398     */
399     public void testForkGet() {
400 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
401     public void realCompute() throws Exception {
402     AsyncFib f = new AsyncFib(8);
403     assertSame(f, f.fork());
404     assertNull(f.get());
405     assertEquals(21, f.number);
406 jsr166 1.24 checkCompletedNormally(f);
407 jsr166 1.12 }};
408 jsr166 1.14 testInvokeOnPool(mainPool(), a);
409 dl 1.1 }
410    
411 jsr166 1.2 /**
412 dl 1.1 * timed get of a forked task returns when task completes
413     */
414     public void testForkTimedGet() {
415 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
416     public void realCompute() throws Exception {
417     AsyncFib f = new AsyncFib(8);
418     assertSame(f, f.fork());
419 jsr166 1.18 assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
420 jsr166 1.17 assertEquals(21, f.number);
421 jsr166 1.24 checkCompletedNormally(f);
422 jsr166 1.12 }};
423 jsr166 1.14 testInvokeOnPool(mainPool(), a);
424 dl 1.1 }
425    
426 jsr166 1.2 /**
427 dl 1.1 * timed get with null time unit throws NPE
428     */
429     public void testForkTimedGetNPE() {
430 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
431     public void realCompute() throws Exception {
432     AsyncFib f = new AsyncFib(8);
433     assertSame(f, f.fork());
434 jsr166 1.12 try {
435     f.get(5L, null);
436     shouldThrow();
437 jsr166 1.17 } catch (NullPointerException success) {}
438 jsr166 1.12 }};
439 jsr166 1.14 testInvokeOnPool(mainPool(), a);
440 dl 1.1 }
441    
442 jsr166 1.2 /**
443 dl 1.1 * quietlyJoin of a forked task returns when task completes
444     */
445     public void testForkQuietlyJoin() {
446 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
447     public void realCompute() {
448 jsr166 1.12 AsyncFib f = new AsyncFib(8);
449 jsr166 1.17 assertSame(f, f.fork());
450 jsr166 1.12 f.quietlyJoin();
451 jsr166 1.17 assertEquals(21, f.number);
452 jsr166 1.24 checkCompletedNormally(f);
453 jsr166 1.12 }};
454 jsr166 1.14 testInvokeOnPool(mainPool(), a);
455 dl 1.1 }
456    
457    
458 jsr166 1.2 /**
459 dl 1.1 * helpQuiesce returns when tasks are complete.
460     * getQueuedTaskCount returns 0 when quiescent
461     */
462     public void testForkHelpQuiesce() {
463 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
464     public void realCompute() {
465 jsr166 1.12 AsyncFib f = new AsyncFib(8);
466 jsr166 1.17 assertSame(f, f.fork());
467 jsr166 1.12 f.helpQuiesce();
468 jsr166 1.17 assertEquals(21, f.number);
469     assertEquals(0, getQueuedTaskCount());
470 jsr166 1.24 checkCompletedNormally(f);
471 jsr166 1.12 }};
472 jsr166 1.14 testInvokeOnPool(mainPool(), a);
473 dl 1.1 }
474    
475    
476 jsr166 1.2 /**
477 dl 1.1 * invoke task throws exception when task completes abnormally
478     */
479     public void testAbnormalInvoke() {
480 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
481     public void realCompute() {
482     FailingAsyncFib f = new FailingAsyncFib(8);
483 jsr166 1.12 try {
484     f.invoke();
485     shouldThrow();
486 jsr166 1.24 } catch (FJException success) {
487     checkCompletedAbnormally(f, success);
488     }
489 jsr166 1.12 }};
490 jsr166 1.14 testInvokeOnPool(mainPool(), a);
491 dl 1.1 }
492    
493 jsr166 1.2 /**
494 jsr166 1.3 * quietlyInvoke task returns when task completes abnormally
495 dl 1.1 */
496     public void testAbnormalQuietlyInvoke() {
497 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
498     public void realCompute() {
499 jsr166 1.12 FailingAsyncFib f = new FailingAsyncFib(8);
500     f.quietlyInvoke();
501 jsr166 1.24 assertTrue(f.getException() instanceof FJException);
502     checkCompletedAbnormally(f, f.getException());
503 jsr166 1.12 }};
504 jsr166 1.14 testInvokeOnPool(mainPool(), a);
505 dl 1.1 }
506    
507 jsr166 1.2 /**
508 dl 1.1 * join of a forked task throws exception when task completes abnormally
509     */
510     public void testAbnormalForkJoin() {
511 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
512     public void realCompute() {
513     FailingAsyncFib f = new FailingAsyncFib(8);
514     assertSame(f, f.fork());
515 jsr166 1.12 try {
516     f.join();
517     shouldThrow();
518 jsr166 1.24 } catch (FJException success) {
519     checkCompletedAbnormally(f, success);
520     }
521 jsr166 1.12 }};
522 jsr166 1.14 testInvokeOnPool(mainPool(), a);
523 dl 1.1 }
524    
525 jsr166 1.2 /**
526 dl 1.1 * get of a forked task throws exception when task completes abnormally
527     */
528     public void testAbnormalForkGet() {
529 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
530     public void realCompute() throws Exception {
531     FailingAsyncFib f = new FailingAsyncFib(8);
532     assertSame(f, f.fork());
533 jsr166 1.12 try {
534     f.get();
535     shouldThrow();
536 jsr166 1.18 } catch (ExecutionException success) {
537     Throwable cause = success.getCause();
538     assertTrue(cause instanceof FJException);
539 jsr166 1.24 checkCompletedAbnormally(f, cause);
540 jsr166 1.18 }
541 jsr166 1.12 }};
542 jsr166 1.14 testInvokeOnPool(mainPool(), a);
543 dl 1.1 }
544    
545 jsr166 1.2 /**
546 dl 1.1 * timed get of a forked task throws exception when task completes abnormally
547     */
548     public void testAbnormalForkTimedGet() {
549 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
550     public void realCompute() throws Exception {
551     FailingAsyncFib f = new FailingAsyncFib(8);
552     assertSame(f, f.fork());
553 jsr166 1.12 try {
554 jsr166 1.18 f.get(LONG_DELAY_MS, MILLISECONDS);
555 jsr166 1.12 shouldThrow();
556 jsr166 1.18 } catch (ExecutionException success) {
557     Throwable cause = success.getCause();
558     assertTrue(cause instanceof FJException);
559 jsr166 1.24 checkCompletedAbnormally(f, cause);
560 jsr166 1.18 }
561 jsr166 1.12 }};
562 jsr166 1.14 testInvokeOnPool(mainPool(), a);
563 dl 1.1 }
564    
565 jsr166 1.2 /**
566 dl 1.1 * quietlyJoin of a forked task returns when task completes abnormally
567     */
568     public void testAbnormalForkQuietlyJoin() {
569 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
570     public void realCompute() {
571 jsr166 1.12 FailingAsyncFib f = new FailingAsyncFib(8);
572 jsr166 1.17 assertSame(f, f.fork());
573 jsr166 1.12 f.quietlyJoin();
574 jsr166 1.17 assertTrue(f.getException() instanceof FJException);
575 jsr166 1.24 checkCompletedAbnormally(f, f.getException());
576 jsr166 1.12 }};
577 jsr166 1.14 testInvokeOnPool(mainPool(), a);
578 dl 1.1 }
579    
580 jsr166 1.2 /**
581 dl 1.1 * invoke task throws exception when task cancelled
582     */
583     public void testCancelledInvoke() {
584 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
585     public void realCompute() {
586     AsyncFib f = new AsyncFib(8);
587     assertTrue(f.cancel(true));
588 jsr166 1.12 try {
589     f.invoke();
590     shouldThrow();
591 jsr166 1.18 } catch (CancellationException success) {
592 jsr166 1.24 checkCancelled(f);
593 jsr166 1.18 }
594 jsr166 1.12 }};
595 jsr166 1.14 testInvokeOnPool(mainPool(), a);
596 dl 1.1 }
597    
598 jsr166 1.2 /**
599 dl 1.1 * join of a forked task throws exception when task cancelled
600     */
601     public void testCancelledForkJoin() {
602 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
603     public void realCompute() {
604     AsyncFib f = new AsyncFib(8);
605     assertTrue(f.cancel(true));
606     assertSame(f, f.fork());
607 jsr166 1.12 try {
608     f.join();
609     shouldThrow();
610 jsr166 1.18 } catch (CancellationException success) {
611 jsr166 1.24 checkCancelled(f);
612 jsr166 1.18 }
613 jsr166 1.12 }};
614 jsr166 1.14 testInvokeOnPool(mainPool(), a);
615 dl 1.1 }
616    
617 jsr166 1.2 /**
618 dl 1.1 * get of a forked task throws exception when task cancelled
619     */
620     public void testCancelledForkGet() {
621 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
622     public void realCompute() throws Exception {
623     AsyncFib f = new AsyncFib(8);
624     assertTrue(f.cancel(true));
625     assertSame(f, f.fork());
626 jsr166 1.12 try {
627     f.get();
628     shouldThrow();
629 jsr166 1.18 } catch (CancellationException success) {
630 jsr166 1.24 checkCancelled(f);
631 jsr166 1.18 }
632 jsr166 1.12 }};
633 jsr166 1.14 testInvokeOnPool(mainPool(), a);
634 dl 1.1 }
635    
636 jsr166 1.2 /**
637 dl 1.1 * timed get of a forked task throws exception when task cancelled
638     */
639 jsr166 1.17 public void testCancelledForkTimedGet() throws Exception {
640     RecursiveAction a = new CheckedRecursiveAction() {
641     public void realCompute() throws Exception {
642     AsyncFib f = new AsyncFib(8);
643     assertTrue(f.cancel(true));
644     assertSame(f, f.fork());
645     try {
646 jsr166 1.18 f.get(LONG_DELAY_MS, MILLISECONDS);
647 jsr166 1.12 shouldThrow();
648 jsr166 1.18 } catch (CancellationException success) {
649 jsr166 1.24 checkCancelled(f);
650 jsr166 1.18 }
651 jsr166 1.12 }};
652 jsr166 1.14 testInvokeOnPool(mainPool(), a);
653 dl 1.1 }
654    
655 jsr166 1.2 /**
656 dl 1.1 * quietlyJoin of a forked task returns when task cancelled
657     */
658     public void testCancelledForkQuietlyJoin() {
659 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
660     public void realCompute() {
661 jsr166 1.12 AsyncFib f = new AsyncFib(8);
662 jsr166 1.17 assertTrue(f.cancel(true));
663     assertSame(f, f.fork());
664 jsr166 1.12 f.quietlyJoin();
665 jsr166 1.24 checkCancelled(f);
666 jsr166 1.12 }};
667 jsr166 1.14 testInvokeOnPool(mainPool(), a);
668 dl 1.1 }
669    
670     /**
671     * getPool of executing task returns its pool
672     */
673     public void testGetPool() {
674 jsr166 1.14 final ForkJoinPool mainPool = mainPool();
675 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
676     public void realCompute() {
677     assertSame(mainPool, getPool());
678 jsr166 1.12 }};
679 jsr166 1.14 testInvokeOnPool(mainPool, a);
680 dl 1.1 }
681    
682     /**
683     * getPool of non-FJ task returns null
684     */
685     public void testGetPool2() {
686 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
687     public void realCompute() {
688     assertNull(getPool());
689 jsr166 1.12 }};
690 jsr166 1.16 assertNull(a.invoke());
691 dl 1.1 }
692    
693     /**
694     * inForkJoinPool of executing task returns true
695     */
696     public void testInForkJoinPool() {
697 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
698     public void realCompute() {
699     assertTrue(inForkJoinPool());
700 jsr166 1.12 }};
701 jsr166 1.14 testInvokeOnPool(mainPool(), a);
702 dl 1.1 }
703    
704     /**
705     * inForkJoinPool of non-FJ task returns false
706     */
707     public void testInForkJoinPool2() {
708 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
709     public void realCompute() {
710 jsr166 1.24 assertFalse(inForkJoinPool());
711 jsr166 1.12 }};
712 jsr166 1.16 assertNull(a.invoke());
713 dl 1.1 }
714    
715     /**
716     * setRawResult(null) succeeds
717     */
718     public void testSetRawResult() {
719 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
720     public void realCompute() {
721 jsr166 1.12 setRawResult(null);
722 jsr166 1.27 assertNull(getRawResult());
723 jsr166 1.12 }};
724 jsr166 1.16 assertNull(a.invoke());
725 dl 1.1 }
726    
727 jsr166 1.2 /**
728 dl 1.1 * invoke task throws exception after invoking completeExceptionally
729     */
730     public void testCompleteExceptionally() {
731 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
732     public void realCompute() {
733     AsyncFib f = new AsyncFib(8);
734     f.completeExceptionally(new FJException());
735 jsr166 1.12 try {
736     f.invoke();
737     shouldThrow();
738 jsr166 1.24 } catch (FJException success) {
739     checkCompletedAbnormally(f, success);
740     }
741 jsr166 1.12 }};
742 jsr166 1.14 testInvokeOnPool(mainPool(), a);
743 dl 1.1 }
744    
745 jsr166 1.2 /**
746 dl 1.1 * invokeAll(t1, t2) invokes all task arguments
747     */
748     public void testInvokeAll2() {
749 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
750     public void realCompute() {
751 jsr166 1.12 AsyncFib f = new AsyncFib(8);
752     AsyncFib g = new AsyncFib(9);
753     invokeAll(f, g);
754 jsr166 1.17 assertEquals(21, f.number);
755     assertEquals(34, g.number);
756 jsr166 1.24 checkCompletedNormally(f);
757     checkCompletedNormally(g);
758 jsr166 1.12 }};
759 jsr166 1.14 testInvokeOnPool(mainPool(), a);
760 dl 1.1 }
761    
762 jsr166 1.2 /**
763 dl 1.1 * invokeAll(tasks) with 1 argument invokes task
764     */
765     public void testInvokeAll1() {
766 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
767     public void realCompute() {
768 jsr166 1.12 AsyncFib f = new AsyncFib(8);
769     invokeAll(f);
770 jsr166 1.24 checkCompletedNormally(f);
771 jsr166 1.17 assertEquals(21, f.number);
772 jsr166 1.12 }};
773 jsr166 1.14 testInvokeOnPool(mainPool(), a);
774 dl 1.1 }
775    
776 jsr166 1.2 /**
777 dl 1.1 * invokeAll(tasks) with > 2 argument invokes tasks
778     */
779     public void testInvokeAll3() {
780 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
781     public void realCompute() {
782 jsr166 1.12 AsyncFib f = new AsyncFib(8);
783     AsyncFib g = new AsyncFib(9);
784     AsyncFib h = new AsyncFib(7);
785     invokeAll(f, g, h);
786 jsr166 1.17 assertEquals(21, f.number);
787     assertEquals(34, g.number);
788     assertEquals(13, h.number);
789 jsr166 1.24 checkCompletedNormally(f);
790     checkCompletedNormally(g);
791     checkCompletedNormally(h);
792 jsr166 1.12 }};
793 jsr166 1.14 testInvokeOnPool(mainPool(), a);
794 dl 1.1 }
795    
796 jsr166 1.2 /**
797 dl 1.1 * invokeAll(collection) invokes all tasks in the collection
798     */
799     public void testInvokeAllCollection() {
800 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
801     public void realCompute() {
802 jsr166 1.12 AsyncFib f = new AsyncFib(8);
803     AsyncFib g = new AsyncFib(9);
804     AsyncFib h = new AsyncFib(7);
805     HashSet set = new HashSet();
806     set.add(f);
807     set.add(g);
808     set.add(h);
809     invokeAll(set);
810 jsr166 1.17 assertEquals(21, f.number);
811     assertEquals(34, g.number);
812     assertEquals(13, h.number);
813 jsr166 1.24 checkCompletedNormally(f);
814     checkCompletedNormally(g);
815     checkCompletedNormally(h);
816 jsr166 1.12 }};
817 jsr166 1.14 testInvokeOnPool(mainPool(), a);
818 dl 1.1 }
819    
820    
821 jsr166 1.2 /**
822 dl 1.1 * invokeAll(tasks) with any null task throws NPE
823     */
824     public void testInvokeAllNPE() {
825 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
826     public void realCompute() {
827     AsyncFib f = new AsyncFib(8);
828     AsyncFib g = new AsyncFib(9);
829     AsyncFib h = null;
830 jsr166 1.12 try {
831     invokeAll(f, g, h);
832     shouldThrow();
833 jsr166 1.17 } catch (NullPointerException success) {}
834 jsr166 1.12 }};
835 jsr166 1.14 testInvokeOnPool(mainPool(), a);
836 dl 1.1 }
837    
838 jsr166 1.2 /**
839 dl 1.1 * invokeAll(t1, t2) throw exception if any task does
840     */
841     public void testAbnormalInvokeAll2() {
842 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
843     public void realCompute() {
844     AsyncFib f = new AsyncFib(8);
845     FailingAsyncFib g = new FailingAsyncFib(9);
846 jsr166 1.12 try {
847     invokeAll(f, g);
848     shouldThrow();
849 jsr166 1.24 } catch (FJException success) {
850     checkCompletedAbnormally(g, success);
851     }
852 jsr166 1.12 }};
853 jsr166 1.14 testInvokeOnPool(mainPool(), a);
854 dl 1.1 }
855    
856 jsr166 1.2 /**
857 dl 1.1 * invokeAll(tasks) with 1 argument throws exception if task does
858     */
859     public void testAbnormalInvokeAll1() {
860 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
861     public void realCompute() {
862     FailingAsyncFib g = new FailingAsyncFib(9);
863 jsr166 1.12 try {
864     invokeAll(g);
865     shouldThrow();
866 jsr166 1.24 } catch (FJException success) {
867     checkCompletedAbnormally(g, success);
868     }
869 jsr166 1.12 }};
870 jsr166 1.14 testInvokeOnPool(mainPool(), a);
871 dl 1.1 }
872    
873 jsr166 1.2 /**
874 dl 1.1 * invokeAll(tasks) with > 2 argument throws exception if any task does
875     */
876     public void testAbnormalInvokeAll3() {
877 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
878     public void realCompute() {
879     AsyncFib f = new AsyncFib(8);
880     FailingAsyncFib g = new FailingAsyncFib(9);
881     AsyncFib h = new AsyncFib(7);
882 jsr166 1.12 try {
883     invokeAll(f, g, h);
884     shouldThrow();
885 jsr166 1.24 } catch (FJException success) {
886     checkCompletedAbnormally(g, success);
887     }
888 jsr166 1.12 }};
889 jsr166 1.14 testInvokeOnPool(mainPool(), a);
890 dl 1.1 }
891    
892 jsr166 1.2 /**
893 dl 1.1 * invokeAll(collection) throws exception if any task does
894     */
895     public void testAbnormalInvokeAllCollection() {
896 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
897     public void realCompute() {
898     FailingAsyncFib f = new FailingAsyncFib(8);
899     AsyncFib g = new AsyncFib(9);
900     AsyncFib h = new AsyncFib(7);
901     HashSet set = new HashSet();
902     set.add(f);
903     set.add(g);
904     set.add(h);
905 jsr166 1.12 try {
906     invokeAll(set);
907     shouldThrow();
908 jsr166 1.24 } catch (FJException success) {
909     checkCompletedAbnormally(f, success);
910     }
911 jsr166 1.12 }};
912 jsr166 1.14 testInvokeOnPool(mainPool(), a);
913 dl 1.1 }
914    
915 jsr166 1.2 /**
916 dl 1.1 * tryUnfork returns true for most recent unexecuted task,
917     * and suppresses execution
918     */
919     public void testTryUnfork() {
920 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
921     public void realCompute() {
922 jsr166 1.12 AsyncFib g = new AsyncFib(9);
923 jsr166 1.17 assertSame(g, g.fork());
924 jsr166 1.12 AsyncFib f = new AsyncFib(8);
925 jsr166 1.17 assertSame(f, f.fork());
926     assertTrue(f.tryUnfork());
927 jsr166 1.12 helpQuiesce();
928 jsr166 1.24 checkNotDone(f);
929     checkCompletedNormally(g);
930 jsr166 1.12 }};
931 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
932 dl 1.1 }
933    
934 jsr166 1.2 /**
935 dl 1.1 * getSurplusQueuedTaskCount returns > 0 when
936     * there are more tasks than threads
937     */
938     public void testGetSurplusQueuedTaskCount() {
939 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
940     public void realCompute() {
941 jsr166 1.12 AsyncFib h = new AsyncFib(7);
942 jsr166 1.17 assertSame(h, h.fork());
943 jsr166 1.12 AsyncFib g = new AsyncFib(9);
944 jsr166 1.17 assertSame(g, g.fork());
945 jsr166 1.12 AsyncFib f = new AsyncFib(8);
946 jsr166 1.17 assertSame(f, f.fork());
947     assertTrue(getSurplusQueuedTaskCount() > 0);
948 jsr166 1.12 helpQuiesce();
949 jsr166 1.24 assertEquals(0, getSurplusQueuedTaskCount());
950     checkCompletedNormally(f);
951     checkCompletedNormally(g);
952     checkCompletedNormally(h);
953 jsr166 1.12 }};
954 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
955 dl 1.1 }
956    
957 jsr166 1.2 /**
958 dl 1.1 * peekNextLocalTask returns most recent unexecuted task.
959     */
960     public void testPeekNextLocalTask() {
961 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
962     public void realCompute() {
963 jsr166 1.12 AsyncFib g = new AsyncFib(9);
964 jsr166 1.17 assertSame(g, g.fork());
965 jsr166 1.12 AsyncFib f = new AsyncFib(8);
966 jsr166 1.17 assertSame(f, f.fork());
967     assertSame(f, peekNextLocalTask());
968     assertNull(f.join());
969 jsr166 1.24 checkCompletedNormally(f);
970 jsr166 1.12 helpQuiesce();
971 jsr166 1.24 checkCompletedNormally(g);
972 jsr166 1.12 }};
973 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
974 dl 1.1 }
975    
976 jsr166 1.2 /**
977 jsr166 1.24 * pollNextLocalTask returns most recent unexecuted task without
978     * executing it
979 dl 1.1 */
980     public void testPollNextLocalTask() {
981 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
982     public void realCompute() {
983 jsr166 1.12 AsyncFib g = new AsyncFib(9);
984 jsr166 1.17 assertSame(g, g.fork());
985 jsr166 1.12 AsyncFib f = new AsyncFib(8);
986 jsr166 1.17 assertSame(f, f.fork());
987     assertSame(f, pollNextLocalTask());
988 jsr166 1.12 helpQuiesce();
989 jsr166 1.24 checkNotDone(f);
990     assertEquals(34, g.number);
991     checkCompletedNormally(g);
992 jsr166 1.12 }};
993 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
994 dl 1.1 }
995    
996 jsr166 1.2 /**
997 jsr166 1.24 * pollTask returns an unexecuted task without executing it
998 dl 1.1 */
999     public void testPollTask() {
1000 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
1001     public void realCompute() {
1002 jsr166 1.12 AsyncFib g = new AsyncFib(9);
1003 jsr166 1.17 assertSame(g, g.fork());
1004 jsr166 1.12 AsyncFib f = new AsyncFib(8);
1005 jsr166 1.17 assertSame(f, f.fork());
1006     assertSame(f, pollTask());
1007 jsr166 1.12 helpQuiesce();
1008 jsr166 1.24 checkNotDone(f);
1009     checkCompletedNormally(g);
1010 jsr166 1.12 }};
1011 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1012 dl 1.1 }
1013    
1014 jsr166 1.2 /**
1015 dl 1.1 * peekNextLocalTask returns least recent unexecuted task in async mode
1016     */
1017     public void testPeekNextLocalTaskAsync() {
1018 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
1019     public void realCompute() {
1020 jsr166 1.12 AsyncFib g = new AsyncFib(9);
1021 jsr166 1.17 assertSame(g, g.fork());
1022 jsr166 1.12 AsyncFib f = new AsyncFib(8);
1023 jsr166 1.17 assertSame(f, f.fork());
1024     assertSame(g, peekNextLocalTask());
1025     assertNull(f.join());
1026 jsr166 1.12 helpQuiesce();
1027 jsr166 1.24 checkCompletedNormally(f);
1028     assertEquals(34, g.number);
1029     checkCompletedNormally(g);
1030 jsr166 1.12 }};
1031 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
1032 dl 1.1 }
1033    
1034 jsr166 1.2 /**
1035 jsr166 1.24 * pollNextLocalTask returns least recent unexecuted task without
1036     * executing it, in async mode
1037 dl 1.1 */
1038     public void testPollNextLocalTaskAsync() {
1039 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
1040     public void realCompute() {
1041 jsr166 1.12 AsyncFib g = new AsyncFib(9);
1042 jsr166 1.17 assertSame(g, g.fork());
1043 jsr166 1.12 AsyncFib f = new AsyncFib(8);
1044 jsr166 1.17 assertSame(f, f.fork());
1045     assertSame(g, pollNextLocalTask());
1046 jsr166 1.12 helpQuiesce();
1047 jsr166 1.24 assertEquals(21, f.number);
1048     checkCompletedNormally(f);
1049     checkNotDone(g);
1050 jsr166 1.12 }};
1051 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
1052 dl 1.1 }
1053    
1054 jsr166 1.2 /**
1055 jsr166 1.24 * pollTask returns an unexecuted task without executing it, in
1056     * async mode
1057 dl 1.1 */
1058     public void testPollTaskAsync() {
1059 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
1060     public void realCompute() {
1061 jsr166 1.12 AsyncFib g = new AsyncFib(9);
1062 jsr166 1.17 assertSame(g, g.fork());
1063 jsr166 1.12 AsyncFib f = new AsyncFib(8);
1064 jsr166 1.17 assertSame(f, f.fork());
1065     assertSame(g, pollTask());
1066 jsr166 1.12 helpQuiesce();
1067 jsr166 1.24 assertEquals(21, f.number);
1068     checkCompletedNormally(f);
1069     checkNotDone(g);
1070 jsr166 1.12 }};
1071 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
1072 dl 1.1 }
1073 dl 1.20
1074     // versions for singleton pools
1075    
1076     /**
1077     * invoke returns when task completes normally.
1078     * isCompletedAbnormally and isCancelled return false for normally
1079 jsr166 1.26 * completed tasks; getRawResult returns null.
1080 dl 1.20 */
1081     public void testInvokeSingleton() {
1082     RecursiveAction a = new CheckedRecursiveAction() {
1083     public void realCompute() {
1084     AsyncFib f = new AsyncFib(8);
1085     assertNull(f.invoke());
1086     assertEquals(21, f.number);
1087 jsr166 1.24 checkCompletedNormally(f);
1088 dl 1.20 }};
1089     testInvokeOnPool(singletonPool(), a);
1090     }
1091    
1092     /**
1093     * quietlyInvoke task returns when task completes normally.
1094     * isCompletedAbnormally and isCancelled return false for normally
1095     * completed tasks
1096     */
1097     public void testQuietlyInvokeSingleton() {
1098     RecursiveAction a = new CheckedRecursiveAction() {
1099     public void realCompute() {
1100     AsyncFib f = new AsyncFib(8);
1101     f.quietlyInvoke();
1102     assertEquals(21, f.number);
1103 jsr166 1.24 checkCompletedNormally(f);
1104 dl 1.20 }};
1105     testInvokeOnPool(singletonPool(), a);
1106     }
1107    
1108     /**
1109     * join of a forked task returns when task completes
1110     */
1111     public void testForkJoinSingleton() {
1112     RecursiveAction a = new CheckedRecursiveAction() {
1113     public void realCompute() {
1114     AsyncFib f = new AsyncFib(8);
1115     assertSame(f, f.fork());
1116     assertNull(f.join());
1117     assertEquals(21, f.number);
1118 jsr166 1.24 checkCompletedNormally(f);
1119 dl 1.20 }};
1120     testInvokeOnPool(singletonPool(), a);
1121     }
1122    
1123     /**
1124     * get of a forked task returns when task completes
1125     */
1126     public void testForkGetSingleton() {
1127     RecursiveAction a = new CheckedRecursiveAction() {
1128     public void realCompute() throws Exception {
1129     AsyncFib f = new AsyncFib(8);
1130     assertSame(f, f.fork());
1131     assertNull(f.get());
1132     assertEquals(21, f.number);
1133 jsr166 1.24 checkCompletedNormally(f);
1134 dl 1.20 }};
1135     testInvokeOnPool(singletonPool(), a);
1136     }
1137    
1138     /**
1139     * timed get of a forked task returns when task completes
1140     */
1141     public void testForkTimedGetSingleton() {
1142     RecursiveAction a = new CheckedRecursiveAction() {
1143     public void realCompute() throws Exception {
1144     AsyncFib f = new AsyncFib(8);
1145     assertSame(f, f.fork());
1146     assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1147     assertEquals(21, f.number);
1148 jsr166 1.24 checkCompletedNormally(f);
1149 dl 1.20 }};
1150     testInvokeOnPool(singletonPool(), a);
1151     }
1152    
1153     /**
1154     * timed get with null time unit throws NPE
1155     */
1156     public void testForkTimedGetNPESingleton() {
1157     RecursiveAction a = new CheckedRecursiveAction() {
1158     public void realCompute() throws Exception {
1159     AsyncFib f = new AsyncFib(8);
1160     assertSame(f, f.fork());
1161     try {
1162     f.get(5L, null);
1163     shouldThrow();
1164     } catch (NullPointerException success) {}
1165     }};
1166     testInvokeOnPool(singletonPool(), a);
1167     }
1168    
1169     /**
1170     * quietlyJoin of a forked task returns when task completes
1171     */
1172     public void testForkQuietlyJoinSingleton() {
1173     RecursiveAction a = new CheckedRecursiveAction() {
1174     public void realCompute() {
1175     AsyncFib f = new AsyncFib(8);
1176     assertSame(f, f.fork());
1177     f.quietlyJoin();
1178     assertEquals(21, f.number);
1179 jsr166 1.24 checkCompletedNormally(f);
1180 dl 1.20 }};
1181     testInvokeOnPool(singletonPool(), a);
1182     }
1183    
1184    
1185     /**
1186     * helpQuiesce returns when tasks are complete.
1187     * getQueuedTaskCount returns 0 when quiescent
1188     */
1189     public void testForkHelpQuiesceSingleton() {
1190     RecursiveAction a = new CheckedRecursiveAction() {
1191     public void realCompute() {
1192     AsyncFib f = new AsyncFib(8);
1193     assertSame(f, f.fork());
1194     f.helpQuiesce();
1195 jsr166 1.24 assertEquals(0, getQueuedTaskCount());
1196 dl 1.20 assertEquals(21, f.number);
1197 jsr166 1.24 checkCompletedNormally(f);
1198 dl 1.20 }};
1199     testInvokeOnPool(singletonPool(), a);
1200     }
1201    
1202    
1203     /**
1204     * invoke task throws exception when task completes abnormally
1205     */
1206     public void testAbnormalInvokeSingleton() {
1207     RecursiveAction a = new CheckedRecursiveAction() {
1208     public void realCompute() {
1209     FailingAsyncFib f = new FailingAsyncFib(8);
1210     try {
1211     f.invoke();
1212     shouldThrow();
1213 jsr166 1.24 } catch (FJException success) {
1214     checkCompletedAbnormally(f, success);
1215     }
1216 dl 1.20 }};
1217     testInvokeOnPool(singletonPool(), a);
1218     }
1219    
1220     /**
1221     * quietlyInvoke task returns when task completes abnormally
1222     */
1223     public void testAbnormalQuietlyInvokeSingleton() {
1224     RecursiveAction a = new CheckedRecursiveAction() {
1225     public void realCompute() {
1226     FailingAsyncFib f = new FailingAsyncFib(8);
1227     f.quietlyInvoke();
1228 jsr166 1.24 assertTrue(f.getException() instanceof FJException);
1229     checkCompletedAbnormally(f, f.getException());
1230 dl 1.20 }};
1231     testInvokeOnPool(singletonPool(), a);
1232     }
1233    
1234     /**
1235     * join of a forked task throws exception when task completes abnormally
1236     */
1237     public void testAbnormalForkJoinSingleton() {
1238     RecursiveAction a = new CheckedRecursiveAction() {
1239     public void realCompute() {
1240     FailingAsyncFib f = new FailingAsyncFib(8);
1241     assertSame(f, f.fork());
1242     try {
1243     f.join();
1244     shouldThrow();
1245 jsr166 1.24 } catch (FJException success) {
1246     checkCompletedAbnormally(f, success);
1247     }
1248 dl 1.20 }};
1249     testInvokeOnPool(singletonPool(), a);
1250     }
1251    
1252     /**
1253     * get of a forked task throws exception when task completes abnormally
1254     */
1255     public void testAbnormalForkGetSingleton() {
1256     RecursiveAction a = new CheckedRecursiveAction() {
1257     public void realCompute() throws Exception {
1258     FailingAsyncFib f = new FailingAsyncFib(8);
1259     assertSame(f, f.fork());
1260     try {
1261     f.get();
1262     shouldThrow();
1263     } catch (ExecutionException success) {
1264     Throwable cause = success.getCause();
1265     assertTrue(cause instanceof FJException);
1266 jsr166 1.24 checkCompletedAbnormally(f, cause);
1267 dl 1.20 }
1268     }};
1269     testInvokeOnPool(singletonPool(), a);
1270     }
1271    
1272     /**
1273     * timed get of a forked task throws exception when task completes abnormally
1274     */
1275     public void testAbnormalForkTimedGetSingleton() {
1276     RecursiveAction a = new CheckedRecursiveAction() {
1277     public void realCompute() throws Exception {
1278     FailingAsyncFib f = new FailingAsyncFib(8);
1279     assertSame(f, f.fork());
1280     try {
1281     f.get(LONG_DELAY_MS, MILLISECONDS);
1282     shouldThrow();
1283     } catch (ExecutionException success) {
1284     Throwable cause = success.getCause();
1285     assertTrue(cause instanceof FJException);
1286 jsr166 1.24 checkCompletedAbnormally(f, cause);
1287 dl 1.20 }
1288     }};
1289     testInvokeOnPool(singletonPool(), a);
1290     }
1291    
1292     /**
1293     * quietlyJoin of a forked task returns when task completes abnormally
1294     */
1295     public void testAbnormalForkQuietlyJoinSingleton() {
1296     RecursiveAction a = new CheckedRecursiveAction() {
1297     public void realCompute() {
1298     FailingAsyncFib f = new FailingAsyncFib(8);
1299     assertSame(f, f.fork());
1300     f.quietlyJoin();
1301     assertTrue(f.getException() instanceof FJException);
1302 jsr166 1.24 checkCompletedAbnormally(f, f.getException());
1303 dl 1.20 }};
1304     testInvokeOnPool(singletonPool(), a);
1305     }
1306    
1307     /**
1308     * invoke task throws exception when task cancelled
1309     */
1310     public void testCancelledInvokeSingleton() {
1311     RecursiveAction a = new CheckedRecursiveAction() {
1312     public void realCompute() {
1313     AsyncFib f = new AsyncFib(8);
1314     assertTrue(f.cancel(true));
1315     try {
1316     f.invoke();
1317     shouldThrow();
1318     } catch (CancellationException success) {
1319 jsr166 1.24 checkCancelled(f);
1320 dl 1.20 }
1321     }};
1322     testInvokeOnPool(singletonPool(), a);
1323     }
1324    
1325     /**
1326     * join of a forked task throws exception when task cancelled
1327     */
1328     public void testCancelledForkJoinSingleton() {
1329     RecursiveAction a = new CheckedRecursiveAction() {
1330     public void realCompute() {
1331     AsyncFib f = new AsyncFib(8);
1332     assertTrue(f.cancel(true));
1333     assertSame(f, f.fork());
1334     try {
1335     f.join();
1336     shouldThrow();
1337     } catch (CancellationException success) {
1338 jsr166 1.24 checkCancelled(f);
1339 dl 1.20 }
1340     }};
1341     testInvokeOnPool(singletonPool(), a);
1342     }
1343    
1344     /**
1345     * get of a forked task throws exception when task cancelled
1346     */
1347     public void testCancelledForkGetSingleton() {
1348     RecursiveAction a = new CheckedRecursiveAction() {
1349     public void realCompute() throws Exception {
1350     AsyncFib f = new AsyncFib(8);
1351     assertTrue(f.cancel(true));
1352     assertSame(f, f.fork());
1353     try {
1354     f.get();
1355     shouldThrow();
1356     } catch (CancellationException success) {
1357 jsr166 1.24 checkCancelled(f);
1358 dl 1.20 }
1359     }};
1360     testInvokeOnPool(singletonPool(), a);
1361     }
1362    
1363     /**
1364     * timed get of a forked task throws exception when task cancelled
1365     */
1366     public void testCancelledForkTimedGetSingleton() throws Exception {
1367     RecursiveAction a = new CheckedRecursiveAction() {
1368     public void realCompute() throws Exception {
1369     AsyncFib f = new AsyncFib(8);
1370     assertTrue(f.cancel(true));
1371     assertSame(f, f.fork());
1372     try {
1373     f.get(LONG_DELAY_MS, MILLISECONDS);
1374     shouldThrow();
1375     } catch (CancellationException success) {
1376 jsr166 1.24 checkCancelled(f);
1377 dl 1.20 }
1378     }};
1379     testInvokeOnPool(singletonPool(), a);
1380     }
1381    
1382     /**
1383     * quietlyJoin of a forked task returns when task cancelled
1384     */
1385     public void testCancelledForkQuietlyJoinSingleton() {
1386     RecursiveAction a = new CheckedRecursiveAction() {
1387     public void realCompute() {
1388     AsyncFib f = new AsyncFib(8);
1389     assertTrue(f.cancel(true));
1390     assertSame(f, f.fork());
1391     f.quietlyJoin();
1392 jsr166 1.24 checkCancelled(f);
1393 dl 1.20 }};
1394     testInvokeOnPool(singletonPool(), a);
1395     }
1396    
1397     /**
1398     * invoke task throws exception after invoking completeExceptionally
1399     */
1400     public void testCompleteExceptionallySingleton() {
1401     RecursiveAction a = new CheckedRecursiveAction() {
1402     public void realCompute() {
1403     AsyncFib f = new AsyncFib(8);
1404     f.completeExceptionally(new FJException());
1405     try {
1406     f.invoke();
1407     shouldThrow();
1408 jsr166 1.24 } catch (FJException success) {
1409     checkCompletedAbnormally(f, success);
1410     }
1411 dl 1.20 }};
1412     testInvokeOnPool(singletonPool(), a);
1413     }
1414    
1415     /**
1416     * invokeAll(t1, t2) invokes all task arguments
1417     */
1418     public void testInvokeAll2Singleton() {
1419     RecursiveAction a = new CheckedRecursiveAction() {
1420     public void realCompute() {
1421     AsyncFib f = new AsyncFib(8);
1422     AsyncFib g = new AsyncFib(9);
1423     invokeAll(f, g);
1424     assertEquals(21, f.number);
1425     assertEquals(34, g.number);
1426 jsr166 1.24 checkCompletedNormally(f);
1427     checkCompletedNormally(g);
1428 dl 1.20 }};
1429     testInvokeOnPool(singletonPool(), a);
1430     }
1431    
1432     /**
1433     * invokeAll(tasks) with 1 argument invokes task
1434     */
1435     public void testInvokeAll1Singleton() {
1436     RecursiveAction a = new CheckedRecursiveAction() {
1437     public void realCompute() {
1438     AsyncFib f = new AsyncFib(8);
1439     invokeAll(f);
1440 jsr166 1.24 checkCompletedNormally(f);
1441 dl 1.20 assertEquals(21, f.number);
1442     }};
1443     testInvokeOnPool(singletonPool(), a);
1444     }
1445    
1446     /**
1447     * invokeAll(tasks) with > 2 argument invokes tasks
1448     */
1449     public void testInvokeAll3Singleton() {
1450     RecursiveAction a = new CheckedRecursiveAction() {
1451     public void realCompute() {
1452     AsyncFib f = new AsyncFib(8);
1453     AsyncFib g = new AsyncFib(9);
1454     AsyncFib h = new AsyncFib(7);
1455     invokeAll(f, g, h);
1456     assertEquals(21, f.number);
1457     assertEquals(34, g.number);
1458     assertEquals(13, h.number);
1459 jsr166 1.24 checkCompletedNormally(f);
1460     checkCompletedNormally(g);
1461     checkCompletedNormally(h);
1462 dl 1.20 }};
1463     testInvokeOnPool(singletonPool(), a);
1464     }
1465    
1466     /**
1467     * invokeAll(collection) invokes all tasks in the collection
1468     */
1469     public void testInvokeAllCollectionSingleton() {
1470     RecursiveAction a = new CheckedRecursiveAction() {
1471     public void realCompute() {
1472     AsyncFib f = new AsyncFib(8);
1473     AsyncFib g = new AsyncFib(9);
1474     AsyncFib h = new AsyncFib(7);
1475     HashSet set = new HashSet();
1476     set.add(f);
1477     set.add(g);
1478     set.add(h);
1479     invokeAll(set);
1480     assertEquals(21, f.number);
1481     assertEquals(34, g.number);
1482     assertEquals(13, h.number);
1483 jsr166 1.24 checkCompletedNormally(f);
1484     checkCompletedNormally(g);
1485     checkCompletedNormally(h);
1486 dl 1.20 }};
1487     testInvokeOnPool(singletonPool(), a);
1488     }
1489    
1490    
1491     /**
1492     * invokeAll(tasks) with any null task throws NPE
1493     */
1494     public void testInvokeAllNPESingleton() {
1495     RecursiveAction a = new CheckedRecursiveAction() {
1496     public void realCompute() {
1497     AsyncFib f = new AsyncFib(8);
1498     AsyncFib g = new AsyncFib(9);
1499     AsyncFib h = null;
1500     try {
1501     invokeAll(f, g, h);
1502     shouldThrow();
1503     } catch (NullPointerException success) {}
1504     }};
1505     testInvokeOnPool(singletonPool(), a);
1506     }
1507    
1508     /**
1509     * invokeAll(t1, t2) throw exception if any task does
1510     */
1511     public void testAbnormalInvokeAll2Singleton() {
1512     RecursiveAction a = new CheckedRecursiveAction() {
1513     public void realCompute() {
1514     AsyncFib f = new AsyncFib(8);
1515     FailingAsyncFib g = new FailingAsyncFib(9);
1516     try {
1517     invokeAll(f, g);
1518     shouldThrow();
1519 jsr166 1.24 } catch (FJException success) {
1520     checkCompletedAbnormally(g, success);
1521     }
1522 dl 1.20 }};
1523     testInvokeOnPool(singletonPool(), a);
1524     }
1525    
1526     /**
1527     * invokeAll(tasks) with 1 argument throws exception if task does
1528     */
1529     public void testAbnormalInvokeAll1Singleton() {
1530     RecursiveAction a = new CheckedRecursiveAction() {
1531     public void realCompute() {
1532     FailingAsyncFib g = new FailingAsyncFib(9);
1533     try {
1534     invokeAll(g);
1535     shouldThrow();
1536 jsr166 1.24 } catch (FJException success) {
1537     checkCompletedAbnormally(g, success);
1538     }
1539 dl 1.20 }};
1540     testInvokeOnPool(singletonPool(), a);
1541     }
1542    
1543     /**
1544     * invokeAll(tasks) with > 2 argument throws exception if any task does
1545     */
1546     public void testAbnormalInvokeAll3Singleton() {
1547     RecursiveAction a = new CheckedRecursiveAction() {
1548     public void realCompute() {
1549     AsyncFib f = new AsyncFib(8);
1550     FailingAsyncFib g = new FailingAsyncFib(9);
1551     AsyncFib h = new AsyncFib(7);
1552     try {
1553     invokeAll(f, g, h);
1554     shouldThrow();
1555 jsr166 1.24 } catch (FJException success) {
1556     checkCompletedAbnormally(g, success);
1557     }
1558 dl 1.20 }};
1559     testInvokeOnPool(singletonPool(), a);
1560     }
1561    
1562     /**
1563     * invokeAll(collection) throws exception if any task does
1564     */
1565     public void testAbnormalInvokeAllCollectionSingleton() {
1566     RecursiveAction a = new CheckedRecursiveAction() {
1567     public void realCompute() {
1568     FailingAsyncFib f = new FailingAsyncFib(8);
1569     AsyncFib g = new AsyncFib(9);
1570     AsyncFib h = new AsyncFib(7);
1571     HashSet set = new HashSet();
1572     set.add(f);
1573     set.add(g);
1574     set.add(h);
1575     try {
1576     invokeAll(set);
1577     shouldThrow();
1578 jsr166 1.24 } catch (FJException success) {
1579     checkCompletedAbnormally(f, success);
1580     }
1581 dl 1.20 }};
1582     testInvokeOnPool(singletonPool(), a);
1583     }
1584    
1585 dl 1.1 }