ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinTaskTest.java
Revision: 1.51
Committed: Wed Aug 24 22:22:39 2016 UTC (7 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.50: +0 -2 lines
Log Message:
fix imports

File Contents

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