ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinTaskTest.java
Revision: 1.38
Committed: Wed Dec 31 16:44:02 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.37: +0 -2 lines
Log Message:
remove unused imports

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