ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinTaskTest.java
Revision: 1.45
Committed: Wed Oct 7 22:39:31 2015 UTC (8 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.44: +23 -14 lines
Log Message:
shuffle tasks in AbnormalInvokeAll tests

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