ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinTaskTest.java
Revision: 1.46
Committed: Sun Oct 11 13:30:30 2015 UTC (8 years, 6 months ago) by dl
Branch: MAIN
Changes since 1.45: +12 -5 lines
Log Message:
fix cancellation in test class

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