ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinTaskTest.java
Revision: 1.56
Committed: Sun Jul 22 20:47:22 2018 UTC (5 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.55: +9 -5 lines
Log Message:
Fix errorprone warning [AssertionFailureIgnored]

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