ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinTaskTest.java
Revision: 1.24
Committed: Sun Nov 21 19:06:53 2010 UTC (13 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.23: +221 -139 lines
Log Message:
miscellaneous test improvements

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