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

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     }};
723 jsr166 1.16 assertNull(a.invoke());
724 dl 1.1 }
725    
726 jsr166 1.2 /**
727 dl 1.1 * invoke task throws exception after invoking completeExceptionally
728     */
729     public void testCompleteExceptionally() {
730 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
731     public void realCompute() {
732     AsyncFib f = new AsyncFib(8);
733     f.completeExceptionally(new FJException());
734 jsr166 1.12 try {
735     f.invoke();
736     shouldThrow();
737 jsr166 1.24 } catch (FJException success) {
738     checkCompletedAbnormally(f, success);
739     }
740 jsr166 1.12 }};
741 jsr166 1.14 testInvokeOnPool(mainPool(), a);
742 dl 1.1 }
743    
744 jsr166 1.2 /**
745 dl 1.1 * invokeAll(t1, t2) invokes all task arguments
746     */
747     public void testInvokeAll2() {
748 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
749     public void realCompute() {
750 jsr166 1.12 AsyncFib f = new AsyncFib(8);
751     AsyncFib g = new AsyncFib(9);
752     invokeAll(f, g);
753 jsr166 1.17 assertEquals(21, f.number);
754     assertEquals(34, g.number);
755 jsr166 1.24 checkCompletedNormally(f);
756     checkCompletedNormally(g);
757 jsr166 1.12 }};
758 jsr166 1.14 testInvokeOnPool(mainPool(), a);
759 dl 1.1 }
760    
761 jsr166 1.2 /**
762 dl 1.1 * invokeAll(tasks) with 1 argument invokes task
763     */
764     public void testInvokeAll1() {
765 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
766     public void realCompute() {
767 jsr166 1.12 AsyncFib f = new AsyncFib(8);
768     invokeAll(f);
769 jsr166 1.24 checkCompletedNormally(f);
770 jsr166 1.17 assertEquals(21, f.number);
771 jsr166 1.12 }};
772 jsr166 1.14 testInvokeOnPool(mainPool(), a);
773 dl 1.1 }
774    
775 jsr166 1.2 /**
776 dl 1.1 * invokeAll(tasks) with > 2 argument invokes tasks
777     */
778     public void testInvokeAll3() {
779 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
780     public void realCompute() {
781 jsr166 1.12 AsyncFib f = new AsyncFib(8);
782     AsyncFib g = new AsyncFib(9);
783     AsyncFib h = new AsyncFib(7);
784     invokeAll(f, g, h);
785 jsr166 1.17 assertEquals(21, f.number);
786     assertEquals(34, g.number);
787     assertEquals(13, h.number);
788 jsr166 1.24 checkCompletedNormally(f);
789     checkCompletedNormally(g);
790     checkCompletedNormally(h);
791 jsr166 1.12 }};
792 jsr166 1.14 testInvokeOnPool(mainPool(), a);
793 dl 1.1 }
794    
795 jsr166 1.2 /**
796 dl 1.1 * invokeAll(collection) invokes all tasks in the collection
797     */
798     public void testInvokeAllCollection() {
799 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
800     public void realCompute() {
801 jsr166 1.12 AsyncFib f = new AsyncFib(8);
802     AsyncFib g = new AsyncFib(9);
803     AsyncFib h = new AsyncFib(7);
804     HashSet set = new HashSet();
805     set.add(f);
806     set.add(g);
807     set.add(h);
808     invokeAll(set);
809 jsr166 1.17 assertEquals(21, f.number);
810     assertEquals(34, g.number);
811     assertEquals(13, h.number);
812 jsr166 1.24 checkCompletedNormally(f);
813     checkCompletedNormally(g);
814     checkCompletedNormally(h);
815 jsr166 1.12 }};
816 jsr166 1.14 testInvokeOnPool(mainPool(), a);
817 dl 1.1 }
818    
819    
820 jsr166 1.2 /**
821 dl 1.1 * invokeAll(tasks) with any null task throws NPE
822     */
823     public void testInvokeAllNPE() {
824 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
825     public void realCompute() {
826     AsyncFib f = new AsyncFib(8);
827     AsyncFib g = new AsyncFib(9);
828     AsyncFib h = null;
829 jsr166 1.12 try {
830     invokeAll(f, g, h);
831     shouldThrow();
832 jsr166 1.17 } catch (NullPointerException success) {}
833 jsr166 1.12 }};
834 jsr166 1.14 testInvokeOnPool(mainPool(), a);
835 dl 1.1 }
836    
837 jsr166 1.2 /**
838 dl 1.1 * invokeAll(t1, t2) throw exception if any task does
839     */
840     public void testAbnormalInvokeAll2() {
841 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
842     public void realCompute() {
843     AsyncFib f = new AsyncFib(8);
844     FailingAsyncFib g = new FailingAsyncFib(9);
845 jsr166 1.12 try {
846     invokeAll(f, g);
847     shouldThrow();
848 jsr166 1.24 } catch (FJException success) {
849     checkCompletedAbnormally(g, success);
850     }
851 jsr166 1.12 }};
852 jsr166 1.14 testInvokeOnPool(mainPool(), a);
853 dl 1.1 }
854    
855 jsr166 1.2 /**
856 dl 1.1 * invokeAll(tasks) with 1 argument throws exception if task does
857     */
858     public void testAbnormalInvokeAll1() {
859 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
860     public void realCompute() {
861     FailingAsyncFib g = new FailingAsyncFib(9);
862 jsr166 1.12 try {
863     invokeAll(g);
864     shouldThrow();
865 jsr166 1.24 } catch (FJException success) {
866     checkCompletedAbnormally(g, success);
867     }
868 jsr166 1.12 }};
869 jsr166 1.14 testInvokeOnPool(mainPool(), a);
870 dl 1.1 }
871    
872 jsr166 1.2 /**
873 dl 1.1 * invokeAll(tasks) with > 2 argument throws exception if any task does
874     */
875     public void testAbnormalInvokeAll3() {
876 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
877     public void realCompute() {
878     AsyncFib f = new AsyncFib(8);
879     FailingAsyncFib g = new FailingAsyncFib(9);
880     AsyncFib h = new AsyncFib(7);
881 jsr166 1.12 try {
882     invokeAll(f, g, h);
883     shouldThrow();
884 jsr166 1.24 } catch (FJException success) {
885     checkCompletedAbnormally(g, success);
886     }
887 jsr166 1.12 }};
888 jsr166 1.14 testInvokeOnPool(mainPool(), a);
889 dl 1.1 }
890    
891 jsr166 1.2 /**
892 dl 1.1 * invokeAll(collection) throws exception if any task does
893     */
894     public void testAbnormalInvokeAllCollection() {
895 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
896     public void realCompute() {
897     FailingAsyncFib f = new FailingAsyncFib(8);
898     AsyncFib g = new AsyncFib(9);
899     AsyncFib h = new AsyncFib(7);
900     HashSet set = new HashSet();
901     set.add(f);
902     set.add(g);
903     set.add(h);
904 jsr166 1.12 try {
905     invokeAll(set);
906     shouldThrow();
907 jsr166 1.24 } catch (FJException success) {
908     checkCompletedAbnormally(f, success);
909     }
910 jsr166 1.12 }};
911 jsr166 1.14 testInvokeOnPool(mainPool(), a);
912 dl 1.1 }
913    
914 jsr166 1.2 /**
915 dl 1.1 * tryUnfork returns true for most recent unexecuted task,
916     * and suppresses execution
917     */
918     public void testTryUnfork() {
919 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
920     public void realCompute() {
921 jsr166 1.12 AsyncFib g = new AsyncFib(9);
922 jsr166 1.17 assertSame(g, g.fork());
923 jsr166 1.12 AsyncFib f = new AsyncFib(8);
924 jsr166 1.17 assertSame(f, f.fork());
925     assertTrue(f.tryUnfork());
926 jsr166 1.12 helpQuiesce();
927 jsr166 1.24 checkNotDone(f);
928     checkCompletedNormally(g);
929 jsr166 1.12 }};
930 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
931 dl 1.1 }
932    
933 jsr166 1.2 /**
934 dl 1.1 * getSurplusQueuedTaskCount returns > 0 when
935     * there are more tasks than threads
936     */
937     public void testGetSurplusQueuedTaskCount() {
938 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
939     public void realCompute() {
940 jsr166 1.12 AsyncFib h = new AsyncFib(7);
941 jsr166 1.17 assertSame(h, h.fork());
942 jsr166 1.12 AsyncFib g = new AsyncFib(9);
943 jsr166 1.17 assertSame(g, g.fork());
944 jsr166 1.12 AsyncFib f = new AsyncFib(8);
945 jsr166 1.17 assertSame(f, f.fork());
946     assertTrue(getSurplusQueuedTaskCount() > 0);
947 jsr166 1.12 helpQuiesce();
948 jsr166 1.24 assertEquals(0, getSurplusQueuedTaskCount());
949     checkCompletedNormally(f);
950     checkCompletedNormally(g);
951     checkCompletedNormally(h);
952 jsr166 1.12 }};
953 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
954 dl 1.1 }
955    
956 jsr166 1.2 /**
957 dl 1.1 * peekNextLocalTask returns most recent unexecuted task.
958     */
959     public void testPeekNextLocalTask() {
960 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
961     public void realCompute() {
962 jsr166 1.12 AsyncFib g = new AsyncFib(9);
963 jsr166 1.17 assertSame(g, g.fork());
964 jsr166 1.12 AsyncFib f = new AsyncFib(8);
965 jsr166 1.17 assertSame(f, f.fork());
966     assertSame(f, peekNextLocalTask());
967     assertNull(f.join());
968 jsr166 1.24 checkCompletedNormally(f);
969 jsr166 1.12 helpQuiesce();
970 jsr166 1.24 checkCompletedNormally(g);
971 jsr166 1.12 }};
972 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
973 dl 1.1 }
974    
975 jsr166 1.2 /**
976 jsr166 1.24 * pollNextLocalTask returns most recent unexecuted task without
977     * executing it
978 dl 1.1 */
979     public void testPollNextLocalTask() {
980 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
981     public void realCompute() {
982 jsr166 1.12 AsyncFib g = new AsyncFib(9);
983 jsr166 1.17 assertSame(g, g.fork());
984 jsr166 1.12 AsyncFib f = new AsyncFib(8);
985 jsr166 1.17 assertSame(f, f.fork());
986     assertSame(f, pollNextLocalTask());
987 jsr166 1.12 helpQuiesce();
988 jsr166 1.24 checkNotDone(f);
989     assertEquals(34, g.number);
990     checkCompletedNormally(g);
991 jsr166 1.12 }};
992 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
993 dl 1.1 }
994    
995 jsr166 1.2 /**
996 jsr166 1.24 * pollTask returns an unexecuted task without executing it
997 dl 1.1 */
998     public void testPollTask() {
999 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
1000     public void realCompute() {
1001 jsr166 1.12 AsyncFib g = new AsyncFib(9);
1002 jsr166 1.17 assertSame(g, g.fork());
1003 jsr166 1.12 AsyncFib f = new AsyncFib(8);
1004 jsr166 1.17 assertSame(f, f.fork());
1005     assertSame(f, pollTask());
1006 jsr166 1.12 helpQuiesce();
1007 jsr166 1.24 checkNotDone(f);
1008     checkCompletedNormally(g);
1009 jsr166 1.12 }};
1010 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1011 dl 1.1 }
1012    
1013 jsr166 1.2 /**
1014 dl 1.1 * peekNextLocalTask returns least recent unexecuted task in async mode
1015     */
1016     public void testPeekNextLocalTaskAsync() {
1017 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
1018     public void realCompute() {
1019 jsr166 1.12 AsyncFib g = new AsyncFib(9);
1020 jsr166 1.17 assertSame(g, g.fork());
1021 jsr166 1.12 AsyncFib f = new AsyncFib(8);
1022 jsr166 1.17 assertSame(f, f.fork());
1023     assertSame(g, peekNextLocalTask());
1024     assertNull(f.join());
1025 jsr166 1.12 helpQuiesce();
1026 jsr166 1.24 checkCompletedNormally(f);
1027     assertEquals(34, g.number);
1028     checkCompletedNormally(g);
1029 jsr166 1.12 }};
1030 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
1031 dl 1.1 }
1032    
1033 jsr166 1.2 /**
1034 jsr166 1.24 * pollNextLocalTask returns least recent unexecuted task without
1035     * executing it, in async mode
1036 dl 1.1 */
1037     public void testPollNextLocalTaskAsync() {
1038 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
1039     public void realCompute() {
1040 jsr166 1.12 AsyncFib g = new AsyncFib(9);
1041 jsr166 1.17 assertSame(g, g.fork());
1042 jsr166 1.12 AsyncFib f = new AsyncFib(8);
1043 jsr166 1.17 assertSame(f, f.fork());
1044     assertSame(g, pollNextLocalTask());
1045 jsr166 1.12 helpQuiesce();
1046 jsr166 1.24 assertEquals(21, f.number);
1047     checkCompletedNormally(f);
1048     checkNotDone(g);
1049 jsr166 1.12 }};
1050 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
1051 dl 1.1 }
1052    
1053 jsr166 1.2 /**
1054 jsr166 1.24 * pollTask returns an unexecuted task without executing it, in
1055     * async mode
1056 dl 1.1 */
1057     public void testPollTaskAsync() {
1058 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
1059     public void realCompute() {
1060 jsr166 1.12 AsyncFib g = new AsyncFib(9);
1061 jsr166 1.17 assertSame(g, g.fork());
1062 jsr166 1.12 AsyncFib f = new AsyncFib(8);
1063 jsr166 1.17 assertSame(f, f.fork());
1064     assertSame(g, pollTask());
1065 jsr166 1.12 helpQuiesce();
1066 jsr166 1.24 assertEquals(21, f.number);
1067     checkCompletedNormally(f);
1068     checkNotDone(g);
1069 jsr166 1.12 }};
1070 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
1071 dl 1.1 }
1072 dl 1.20
1073     // versions for singleton pools
1074    
1075     /**
1076     * invoke returns when task completes normally.
1077     * isCompletedAbnormally and isCancelled return false for normally
1078 jsr166 1.26 * completed tasks; getRawResult returns null.
1079 dl 1.20 */
1080     public void testInvokeSingleton() {
1081     RecursiveAction a = new CheckedRecursiveAction() {
1082     public void realCompute() {
1083     AsyncFib f = new AsyncFib(8);
1084     assertNull(f.invoke());
1085     assertEquals(21, f.number);
1086 jsr166 1.24 checkCompletedNormally(f);
1087 dl 1.20 }};
1088     testInvokeOnPool(singletonPool(), a);
1089     }
1090    
1091     /**
1092     * quietlyInvoke task returns when task completes normally.
1093     * isCompletedAbnormally and isCancelled return false for normally
1094     * completed tasks
1095     */
1096     public void testQuietlyInvokeSingleton() {
1097     RecursiveAction a = new CheckedRecursiveAction() {
1098     public void realCompute() {
1099     AsyncFib f = new AsyncFib(8);
1100     f.quietlyInvoke();
1101     assertEquals(21, f.number);
1102 jsr166 1.24 checkCompletedNormally(f);
1103 dl 1.20 }};
1104     testInvokeOnPool(singletonPool(), a);
1105     }
1106    
1107     /**
1108     * join of a forked task returns when task completes
1109     */
1110     public void testForkJoinSingleton() {
1111     RecursiveAction a = new CheckedRecursiveAction() {
1112     public void realCompute() {
1113     AsyncFib f = new AsyncFib(8);
1114     assertSame(f, f.fork());
1115     assertNull(f.join());
1116     assertEquals(21, f.number);
1117 jsr166 1.24 checkCompletedNormally(f);
1118 dl 1.20 }};
1119     testInvokeOnPool(singletonPool(), a);
1120     }
1121    
1122     /**
1123     * get of a forked task returns when task completes
1124     */
1125     public void testForkGetSingleton() {
1126     RecursiveAction a = new CheckedRecursiveAction() {
1127     public void realCompute() throws Exception {
1128     AsyncFib f = new AsyncFib(8);
1129     assertSame(f, f.fork());
1130     assertNull(f.get());
1131     assertEquals(21, f.number);
1132 jsr166 1.24 checkCompletedNormally(f);
1133 dl 1.20 }};
1134     testInvokeOnPool(singletonPool(), a);
1135     }
1136    
1137     /**
1138     * timed get of a forked task returns when task completes
1139     */
1140     public void testForkTimedGetSingleton() {
1141     RecursiveAction a = new CheckedRecursiveAction() {
1142     public void realCompute() throws Exception {
1143     AsyncFib f = new AsyncFib(8);
1144     assertSame(f, f.fork());
1145     assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1146     assertEquals(21, f.number);
1147 jsr166 1.24 checkCompletedNormally(f);
1148 dl 1.20 }};
1149     testInvokeOnPool(singletonPool(), a);
1150     }
1151    
1152     /**
1153     * timed get with null time unit throws NPE
1154     */
1155     public void testForkTimedGetNPESingleton() {
1156     RecursiveAction a = new CheckedRecursiveAction() {
1157     public void realCompute() throws Exception {
1158     AsyncFib f = new AsyncFib(8);
1159     assertSame(f, f.fork());
1160     try {
1161     f.get(5L, null);
1162     shouldThrow();
1163     } catch (NullPointerException success) {}
1164     }};
1165     testInvokeOnPool(singletonPool(), a);
1166     }
1167    
1168     /**
1169     * quietlyJoin of a forked task returns when task completes
1170     */
1171     public void testForkQuietlyJoinSingleton() {
1172     RecursiveAction a = new CheckedRecursiveAction() {
1173     public void realCompute() {
1174     AsyncFib f = new AsyncFib(8);
1175     assertSame(f, f.fork());
1176     f.quietlyJoin();
1177     assertEquals(21, f.number);
1178 jsr166 1.24 checkCompletedNormally(f);
1179 dl 1.20 }};
1180     testInvokeOnPool(singletonPool(), a);
1181     }
1182    
1183    
1184     /**
1185     * helpQuiesce returns when tasks are complete.
1186     * getQueuedTaskCount returns 0 when quiescent
1187     */
1188     public void testForkHelpQuiesceSingleton() {
1189     RecursiveAction a = new CheckedRecursiveAction() {
1190     public void realCompute() {
1191     AsyncFib f = new AsyncFib(8);
1192     assertSame(f, f.fork());
1193     f.helpQuiesce();
1194 jsr166 1.24 assertEquals(0, getQueuedTaskCount());
1195 dl 1.20 assertEquals(21, f.number);
1196 jsr166 1.24 checkCompletedNormally(f);
1197 dl 1.20 }};
1198     testInvokeOnPool(singletonPool(), a);
1199     }
1200    
1201    
1202     /**
1203     * invoke task throws exception when task completes abnormally
1204     */
1205     public void testAbnormalInvokeSingleton() {
1206     RecursiveAction a = new CheckedRecursiveAction() {
1207     public void realCompute() {
1208     FailingAsyncFib f = new FailingAsyncFib(8);
1209     try {
1210     f.invoke();
1211     shouldThrow();
1212 jsr166 1.24 } catch (FJException success) {
1213     checkCompletedAbnormally(f, success);
1214     }
1215 dl 1.20 }};
1216     testInvokeOnPool(singletonPool(), a);
1217     }
1218    
1219     /**
1220     * quietlyInvoke task returns when task completes abnormally
1221     */
1222     public void testAbnormalQuietlyInvokeSingleton() {
1223     RecursiveAction a = new CheckedRecursiveAction() {
1224     public void realCompute() {
1225     FailingAsyncFib f = new FailingAsyncFib(8);
1226     f.quietlyInvoke();
1227 jsr166 1.24 assertTrue(f.getException() instanceof FJException);
1228     checkCompletedAbnormally(f, f.getException());
1229 dl 1.20 }};
1230     testInvokeOnPool(singletonPool(), a);
1231     }
1232    
1233     /**
1234     * join of a forked task throws exception when task completes abnormally
1235     */
1236     public void testAbnormalForkJoinSingleton() {
1237     RecursiveAction a = new CheckedRecursiveAction() {
1238     public void realCompute() {
1239     FailingAsyncFib f = new FailingAsyncFib(8);
1240     assertSame(f, f.fork());
1241     try {
1242     f.join();
1243     shouldThrow();
1244 jsr166 1.24 } catch (FJException success) {
1245     checkCompletedAbnormally(f, success);
1246     }
1247 dl 1.20 }};
1248     testInvokeOnPool(singletonPool(), a);
1249     }
1250    
1251     /**
1252     * get of a forked task throws exception when task completes abnormally
1253     */
1254     public void testAbnormalForkGetSingleton() {
1255     RecursiveAction a = new CheckedRecursiveAction() {
1256     public void realCompute() throws Exception {
1257     FailingAsyncFib f = new FailingAsyncFib(8);
1258     assertSame(f, f.fork());
1259     try {
1260     f.get();
1261     shouldThrow();
1262     } catch (ExecutionException success) {
1263     Throwable cause = success.getCause();
1264     assertTrue(cause instanceof FJException);
1265 jsr166 1.24 checkCompletedAbnormally(f, cause);
1266 dl 1.20 }
1267     }};
1268     testInvokeOnPool(singletonPool(), a);
1269     }
1270    
1271     /**
1272     * timed get of a forked task throws exception when task completes abnormally
1273     */
1274     public void testAbnormalForkTimedGetSingleton() {
1275     RecursiveAction a = new CheckedRecursiveAction() {
1276     public void realCompute() throws Exception {
1277     FailingAsyncFib f = new FailingAsyncFib(8);
1278     assertSame(f, f.fork());
1279     try {
1280     f.get(LONG_DELAY_MS, MILLISECONDS);
1281     shouldThrow();
1282     } catch (ExecutionException success) {
1283     Throwable cause = success.getCause();
1284     assertTrue(cause instanceof FJException);
1285 jsr166 1.24 checkCompletedAbnormally(f, cause);
1286 dl 1.20 }
1287     }};
1288     testInvokeOnPool(singletonPool(), a);
1289     }
1290    
1291     /**
1292     * quietlyJoin of a forked task returns when task completes abnormally
1293     */
1294     public void testAbnormalForkQuietlyJoinSingleton() {
1295     RecursiveAction a = new CheckedRecursiveAction() {
1296     public void realCompute() {
1297     FailingAsyncFib f = new FailingAsyncFib(8);
1298     assertSame(f, f.fork());
1299     f.quietlyJoin();
1300     assertTrue(f.getException() instanceof FJException);
1301 jsr166 1.24 checkCompletedAbnormally(f, f.getException());
1302 dl 1.20 }};
1303     testInvokeOnPool(singletonPool(), a);
1304     }
1305    
1306     /**
1307     * invoke task throws exception when task cancelled
1308     */
1309     public void testCancelledInvokeSingleton() {
1310     RecursiveAction a = new CheckedRecursiveAction() {
1311     public void realCompute() {
1312     AsyncFib f = new AsyncFib(8);
1313     assertTrue(f.cancel(true));
1314     try {
1315     f.invoke();
1316     shouldThrow();
1317     } catch (CancellationException success) {
1318 jsr166 1.24 checkCancelled(f);
1319 dl 1.20 }
1320     }};
1321     testInvokeOnPool(singletonPool(), a);
1322     }
1323    
1324     /**
1325     * join of a forked task throws exception when task cancelled
1326     */
1327     public void testCancelledForkJoinSingleton() {
1328     RecursiveAction a = new CheckedRecursiveAction() {
1329     public void realCompute() {
1330     AsyncFib f = new AsyncFib(8);
1331     assertTrue(f.cancel(true));
1332     assertSame(f, f.fork());
1333     try {
1334     f.join();
1335     shouldThrow();
1336     } catch (CancellationException success) {
1337 jsr166 1.24 checkCancelled(f);
1338 dl 1.20 }
1339     }};
1340     testInvokeOnPool(singletonPool(), a);
1341     }
1342    
1343     /**
1344     * get of a forked task throws exception when task cancelled
1345     */
1346     public void testCancelledForkGetSingleton() {
1347     RecursiveAction a = new CheckedRecursiveAction() {
1348     public void realCompute() throws Exception {
1349     AsyncFib f = new AsyncFib(8);
1350     assertTrue(f.cancel(true));
1351     assertSame(f, f.fork());
1352     try {
1353     f.get();
1354     shouldThrow();
1355     } catch (CancellationException success) {
1356 jsr166 1.24 checkCancelled(f);
1357 dl 1.20 }
1358     }};
1359     testInvokeOnPool(singletonPool(), a);
1360     }
1361    
1362     /**
1363     * timed get of a forked task throws exception when task cancelled
1364     */
1365     public void testCancelledForkTimedGetSingleton() throws Exception {
1366     RecursiveAction a = new CheckedRecursiveAction() {
1367     public void realCompute() throws Exception {
1368     AsyncFib f = new AsyncFib(8);
1369     assertTrue(f.cancel(true));
1370     assertSame(f, f.fork());
1371     try {
1372     f.get(LONG_DELAY_MS, MILLISECONDS);
1373     shouldThrow();
1374     } catch (CancellationException success) {
1375 jsr166 1.24 checkCancelled(f);
1376 dl 1.20 }
1377     }};
1378     testInvokeOnPool(singletonPool(), a);
1379     }
1380    
1381     /**
1382     * quietlyJoin of a forked task returns when task cancelled
1383     */
1384     public void testCancelledForkQuietlyJoinSingleton() {
1385     RecursiveAction a = new CheckedRecursiveAction() {
1386     public void realCompute() {
1387     AsyncFib f = new AsyncFib(8);
1388     assertTrue(f.cancel(true));
1389     assertSame(f, f.fork());
1390     f.quietlyJoin();
1391 jsr166 1.24 checkCancelled(f);
1392 dl 1.20 }};
1393     testInvokeOnPool(singletonPool(), a);
1394     }
1395    
1396     /**
1397     * invoke task throws exception after invoking completeExceptionally
1398     */
1399     public void testCompleteExceptionallySingleton() {
1400     RecursiveAction a = new CheckedRecursiveAction() {
1401     public void realCompute() {
1402     AsyncFib f = new AsyncFib(8);
1403     f.completeExceptionally(new FJException());
1404     try {
1405     f.invoke();
1406     shouldThrow();
1407 jsr166 1.24 } catch (FJException success) {
1408     checkCompletedAbnormally(f, success);
1409     }
1410 dl 1.20 }};
1411     testInvokeOnPool(singletonPool(), a);
1412     }
1413    
1414     /**
1415     * invokeAll(t1, t2) invokes all task arguments
1416     */
1417     public void testInvokeAll2Singleton() {
1418     RecursiveAction a = new CheckedRecursiveAction() {
1419     public void realCompute() {
1420     AsyncFib f = new AsyncFib(8);
1421     AsyncFib g = new AsyncFib(9);
1422     invokeAll(f, g);
1423     assertEquals(21, f.number);
1424     assertEquals(34, g.number);
1425 jsr166 1.24 checkCompletedNormally(f);
1426     checkCompletedNormally(g);
1427 dl 1.20 }};
1428     testInvokeOnPool(singletonPool(), a);
1429     }
1430    
1431     /**
1432     * invokeAll(tasks) with 1 argument invokes task
1433     */
1434     public void testInvokeAll1Singleton() {
1435     RecursiveAction a = new CheckedRecursiveAction() {
1436     public void realCompute() {
1437     AsyncFib f = new AsyncFib(8);
1438     invokeAll(f);
1439 jsr166 1.24 checkCompletedNormally(f);
1440 dl 1.20 assertEquals(21, f.number);
1441     }};
1442     testInvokeOnPool(singletonPool(), a);
1443     }
1444    
1445     /**
1446     * invokeAll(tasks) with > 2 argument invokes tasks
1447     */
1448     public void testInvokeAll3Singleton() {
1449     RecursiveAction a = new CheckedRecursiveAction() {
1450     public void realCompute() {
1451     AsyncFib f = new AsyncFib(8);
1452     AsyncFib g = new AsyncFib(9);
1453     AsyncFib h = new AsyncFib(7);
1454     invokeAll(f, g, h);
1455     assertEquals(21, f.number);
1456     assertEquals(34, g.number);
1457     assertEquals(13, h.number);
1458 jsr166 1.24 checkCompletedNormally(f);
1459     checkCompletedNormally(g);
1460     checkCompletedNormally(h);
1461 dl 1.20 }};
1462     testInvokeOnPool(singletonPool(), a);
1463     }
1464    
1465     /**
1466     * invokeAll(collection) invokes all tasks in the collection
1467     */
1468     public void testInvokeAllCollectionSingleton() {
1469     RecursiveAction a = new CheckedRecursiveAction() {
1470     public void realCompute() {
1471     AsyncFib f = new AsyncFib(8);
1472     AsyncFib g = new AsyncFib(9);
1473     AsyncFib h = new AsyncFib(7);
1474     HashSet set = new HashSet();
1475     set.add(f);
1476     set.add(g);
1477     set.add(h);
1478     invokeAll(set);
1479     assertEquals(21, f.number);
1480     assertEquals(34, g.number);
1481     assertEquals(13, h.number);
1482 jsr166 1.24 checkCompletedNormally(f);
1483     checkCompletedNormally(g);
1484     checkCompletedNormally(h);
1485 dl 1.20 }};
1486     testInvokeOnPool(singletonPool(), a);
1487     }
1488    
1489    
1490     /**
1491     * invokeAll(tasks) with any null task throws NPE
1492     */
1493     public void testInvokeAllNPESingleton() {
1494     RecursiveAction a = new CheckedRecursiveAction() {
1495     public void realCompute() {
1496     AsyncFib f = new AsyncFib(8);
1497     AsyncFib g = new AsyncFib(9);
1498     AsyncFib h = null;
1499     try {
1500     invokeAll(f, g, h);
1501     shouldThrow();
1502     } catch (NullPointerException success) {}
1503     }};
1504     testInvokeOnPool(singletonPool(), a);
1505     }
1506    
1507     /**
1508     * invokeAll(t1, t2) throw exception if any task does
1509     */
1510     public void testAbnormalInvokeAll2Singleton() {
1511     RecursiveAction a = new CheckedRecursiveAction() {
1512     public void realCompute() {
1513     AsyncFib f = new AsyncFib(8);
1514     FailingAsyncFib g = new FailingAsyncFib(9);
1515     try {
1516     invokeAll(f, g);
1517     shouldThrow();
1518 jsr166 1.24 } catch (FJException success) {
1519     checkCompletedAbnormally(g, success);
1520     }
1521 dl 1.20 }};
1522     testInvokeOnPool(singletonPool(), a);
1523     }
1524    
1525     /**
1526     * invokeAll(tasks) with 1 argument throws exception if task does
1527     */
1528     public void testAbnormalInvokeAll1Singleton() {
1529     RecursiveAction a = new CheckedRecursiveAction() {
1530     public void realCompute() {
1531     FailingAsyncFib g = new FailingAsyncFib(9);
1532     try {
1533     invokeAll(g);
1534     shouldThrow();
1535 jsr166 1.24 } catch (FJException success) {
1536     checkCompletedAbnormally(g, success);
1537     }
1538 dl 1.20 }};
1539     testInvokeOnPool(singletonPool(), a);
1540     }
1541    
1542     /**
1543     * invokeAll(tasks) with > 2 argument throws exception if any task does
1544     */
1545     public void testAbnormalInvokeAll3Singleton() {
1546     RecursiveAction a = new CheckedRecursiveAction() {
1547     public void realCompute() {
1548     AsyncFib f = new AsyncFib(8);
1549     FailingAsyncFib g = new FailingAsyncFib(9);
1550     AsyncFib h = new AsyncFib(7);
1551     try {
1552     invokeAll(f, g, h);
1553     shouldThrow();
1554 jsr166 1.24 } catch (FJException success) {
1555     checkCompletedAbnormally(g, success);
1556     }
1557 dl 1.20 }};
1558     testInvokeOnPool(singletonPool(), a);
1559     }
1560    
1561     /**
1562     * invokeAll(collection) throws exception if any task does
1563     */
1564     public void testAbnormalInvokeAllCollectionSingleton() {
1565     RecursiveAction a = new CheckedRecursiveAction() {
1566     public void realCompute() {
1567     FailingAsyncFib f = new FailingAsyncFib(8);
1568     AsyncFib g = new AsyncFib(9);
1569     AsyncFib h = new AsyncFib(7);
1570     HashSet set = new HashSet();
1571     set.add(f);
1572     set.add(g);
1573     set.add(h);
1574     try {
1575     invokeAll(set);
1576     shouldThrow();
1577 jsr166 1.24 } catch (FJException success) {
1578     checkCompletedAbnormally(f, success);
1579     }
1580 dl 1.20 }};
1581     testInvokeOnPool(singletonPool(), a);
1582     }
1583    
1584 dl 1.1 }