ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinTaskTest.java
Revision: 1.21
Committed: Sun Oct 24 17:52:23 2010 UTC (13 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.20: +1 -1 lines
Log Message:
whitespace

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