ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinTaskTest.java
Revision: 1.59
Committed: Fri Dec 4 20:29:22 2020 UTC (3 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.58: +15 -12 lines
Log Message:
JDK-8246587: adaptInterruptible deferred to its own independent change

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 dl 1.57 while (!f.isDone()) // wait out race
503     ;
504 jsr166 1.17 assertEquals(21, f.number);
505     assertEquals(0, getQueuedTaskCount());
506 jsr166 1.24 checkCompletedNormally(f);
507 jsr166 1.12 }};
508 jsr166 1.14 testInvokeOnPool(mainPool(), a);
509 dl 1.1 }
510    
511 jsr166 1.2 /**
512 dl 1.1 * invoke task throws exception when task completes abnormally
513     */
514     public void testAbnormalInvoke() {
515 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
516 jsr166 1.34 protected void realCompute() {
517 jsr166 1.17 FailingAsyncFib f = new FailingAsyncFib(8);
518 jsr166 1.12 try {
519     f.invoke();
520     shouldThrow();
521 jsr166 1.24 } catch (FJException success) {
522     checkCompletedAbnormally(f, success);
523     }
524 jsr166 1.12 }};
525 jsr166 1.14 testInvokeOnPool(mainPool(), a);
526 dl 1.1 }
527    
528 jsr166 1.2 /**
529 jsr166 1.3 * quietlyInvoke task returns when task completes abnormally
530 dl 1.1 */
531     public void testAbnormalQuietlyInvoke() {
532 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
533 jsr166 1.34 protected void realCompute() {
534 jsr166 1.12 FailingAsyncFib f = new FailingAsyncFib(8);
535     f.quietlyInvoke();
536 jsr166 1.24 assertTrue(f.getException() instanceof FJException);
537     checkCompletedAbnormally(f, f.getException());
538 jsr166 1.12 }};
539 jsr166 1.14 testInvokeOnPool(mainPool(), a);
540 dl 1.1 }
541    
542 jsr166 1.2 /**
543 dl 1.1 * join of a forked task throws exception when task completes abnormally
544     */
545     public void testAbnormalForkJoin() {
546 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
547 jsr166 1.34 protected void realCompute() {
548 jsr166 1.17 FailingAsyncFib f = new FailingAsyncFib(8);
549     assertSame(f, f.fork());
550 jsr166 1.12 try {
551     f.join();
552     shouldThrow();
553 jsr166 1.24 } catch (FJException success) {
554     checkCompletedAbnormally(f, success);
555     }
556 jsr166 1.12 }};
557 jsr166 1.14 testInvokeOnPool(mainPool(), a);
558 dl 1.1 }
559    
560 jsr166 1.2 /**
561 dl 1.1 * get of a forked task throws exception when task completes abnormally
562     */
563     public void testAbnormalForkGet() {
564 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
565 jsr166 1.34 protected void realCompute() throws Exception {
566 jsr166 1.17 FailingAsyncFib f = new FailingAsyncFib(8);
567     assertSame(f, f.fork());
568 jsr166 1.12 try {
569     f.get();
570     shouldThrow();
571 jsr166 1.18 } catch (ExecutionException success) {
572     Throwable cause = success.getCause();
573     assertTrue(cause instanceof FJException);
574 jsr166 1.24 checkCompletedAbnormally(f, cause);
575 jsr166 1.18 }
576 jsr166 1.12 }};
577 jsr166 1.14 testInvokeOnPool(mainPool(), a);
578 dl 1.1 }
579    
580 jsr166 1.2 /**
581 dl 1.1 * timed get of a forked task throws exception when task completes abnormally
582     */
583     public void testAbnormalForkTimedGet() {
584 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
585 jsr166 1.34 protected void realCompute() throws Exception {
586 jsr166 1.17 FailingAsyncFib f = new FailingAsyncFib(8);
587     assertSame(f, f.fork());
588 jsr166 1.12 try {
589 jsr166 1.18 f.get(LONG_DELAY_MS, MILLISECONDS);
590 jsr166 1.12 shouldThrow();
591 jsr166 1.18 } catch (ExecutionException success) {
592     Throwable cause = success.getCause();
593     assertTrue(cause instanceof FJException);
594 jsr166 1.24 checkCompletedAbnormally(f, cause);
595 jsr166 1.18 }
596 jsr166 1.12 }};
597 jsr166 1.14 testInvokeOnPool(mainPool(), a);
598 dl 1.1 }
599    
600 jsr166 1.2 /**
601 dl 1.1 * quietlyJoin of a forked task returns when task completes abnormally
602     */
603     public void testAbnormalForkQuietlyJoin() {
604 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
605 jsr166 1.34 protected void realCompute() {
606 jsr166 1.12 FailingAsyncFib f = new FailingAsyncFib(8);
607 jsr166 1.17 assertSame(f, f.fork());
608 jsr166 1.12 f.quietlyJoin();
609 jsr166 1.17 assertTrue(f.getException() instanceof FJException);
610 jsr166 1.24 checkCompletedAbnormally(f, f.getException());
611 jsr166 1.12 }};
612 jsr166 1.14 testInvokeOnPool(mainPool(), a);
613 dl 1.1 }
614    
615 jsr166 1.2 /**
616 dl 1.1 * invoke task throws exception when task cancelled
617     */
618     public void testCancelledInvoke() {
619 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
620 jsr166 1.34 protected void realCompute() {
621 jsr166 1.17 AsyncFib f = new AsyncFib(8);
622     assertTrue(f.cancel(true));
623 jsr166 1.12 try {
624     f.invoke();
625     shouldThrow();
626 jsr166 1.18 } catch (CancellationException success) {
627 jsr166 1.24 checkCancelled(f);
628 jsr166 1.18 }
629 jsr166 1.12 }};
630 jsr166 1.14 testInvokeOnPool(mainPool(), a);
631 dl 1.1 }
632    
633 jsr166 1.2 /**
634 dl 1.1 * join of a forked task throws exception when task cancelled
635     */
636     public void testCancelledForkJoin() {
637 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
638 jsr166 1.34 protected void realCompute() {
639 jsr166 1.17 AsyncFib f = new AsyncFib(8);
640     assertTrue(f.cancel(true));
641     assertSame(f, f.fork());
642 jsr166 1.12 try {
643     f.join();
644     shouldThrow();
645 jsr166 1.18 } catch (CancellationException success) {
646 jsr166 1.24 checkCancelled(f);
647 jsr166 1.18 }
648 jsr166 1.12 }};
649 jsr166 1.14 testInvokeOnPool(mainPool(), a);
650 dl 1.1 }
651    
652 jsr166 1.2 /**
653 dl 1.1 * get of a forked task throws exception when task cancelled
654     */
655     public void testCancelledForkGet() {
656 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
657 jsr166 1.34 protected void realCompute() throws Exception {
658 jsr166 1.17 AsyncFib f = new AsyncFib(8);
659     assertTrue(f.cancel(true));
660     assertSame(f, f.fork());
661 jsr166 1.12 try {
662     f.get();
663     shouldThrow();
664 jsr166 1.18 } catch (CancellationException success) {
665 jsr166 1.24 checkCancelled(f);
666 jsr166 1.18 }
667 jsr166 1.12 }};
668 jsr166 1.14 testInvokeOnPool(mainPool(), a);
669 dl 1.1 }
670    
671 jsr166 1.2 /**
672 dl 1.1 * timed get of a forked task throws exception when task cancelled
673     */
674 jsr166 1.17 public void testCancelledForkTimedGet() throws Exception {
675     RecursiveAction a = new CheckedRecursiveAction() {
676 jsr166 1.34 protected void realCompute() throws Exception {
677 jsr166 1.17 AsyncFib f = new AsyncFib(8);
678     assertTrue(f.cancel(true));
679     assertSame(f, f.fork());
680     try {
681 jsr166 1.18 f.get(LONG_DELAY_MS, MILLISECONDS);
682 jsr166 1.12 shouldThrow();
683 jsr166 1.18 } catch (CancellationException success) {
684 jsr166 1.24 checkCancelled(f);
685 jsr166 1.18 }
686 jsr166 1.12 }};
687 jsr166 1.14 testInvokeOnPool(mainPool(), a);
688 dl 1.1 }
689    
690 jsr166 1.2 /**
691 dl 1.1 * quietlyJoin of a forked task returns when task cancelled
692     */
693     public void testCancelledForkQuietlyJoin() {
694 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
695 jsr166 1.34 protected void realCompute() {
696 jsr166 1.12 AsyncFib f = new AsyncFib(8);
697 jsr166 1.17 assertTrue(f.cancel(true));
698     assertSame(f, f.fork());
699 jsr166 1.12 f.quietlyJoin();
700 jsr166 1.24 checkCancelled(f);
701 jsr166 1.12 }};
702 jsr166 1.14 testInvokeOnPool(mainPool(), a);
703 dl 1.1 }
704    
705     /**
706     * getPool of executing task returns its pool
707     */
708     public void testGetPool() {
709 jsr166 1.14 final ForkJoinPool mainPool = mainPool();
710 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
711 jsr166 1.34 protected void realCompute() {
712 jsr166 1.17 assertSame(mainPool, getPool());
713 jsr166 1.12 }};
714 jsr166 1.14 testInvokeOnPool(mainPool, a);
715 dl 1.1 }
716    
717     /**
718     * getPool of non-FJ task returns null
719     */
720     public void testGetPool2() {
721 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
722 jsr166 1.34 protected void realCompute() {
723 jsr166 1.17 assertNull(getPool());
724 jsr166 1.12 }};
725 jsr166 1.16 assertNull(a.invoke());
726 dl 1.1 }
727    
728     /**
729     * inForkJoinPool of executing task returns true
730     */
731     public void testInForkJoinPool() {
732 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
733 jsr166 1.34 protected void realCompute() {
734 jsr166 1.17 assertTrue(inForkJoinPool());
735 jsr166 1.12 }};
736 jsr166 1.14 testInvokeOnPool(mainPool(), a);
737 dl 1.1 }
738    
739     /**
740     * inForkJoinPool of non-FJ task returns false
741     */
742     public void testInForkJoinPool2() {
743 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
744 jsr166 1.34 protected void realCompute() {
745 jsr166 1.24 assertFalse(inForkJoinPool());
746 jsr166 1.12 }};
747 jsr166 1.16 assertNull(a.invoke());
748 dl 1.1 }
749    
750     /**
751     * setRawResult(null) succeeds
752     */
753     public void testSetRawResult() {
754 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
755 jsr166 1.34 protected void realCompute() {
756 jsr166 1.12 setRawResult(null);
757 jsr166 1.27 assertNull(getRawResult());
758 jsr166 1.12 }};
759 jsr166 1.16 assertNull(a.invoke());
760 dl 1.1 }
761    
762 jsr166 1.2 /**
763 dl 1.1 * invoke task throws exception after invoking completeExceptionally
764     */
765     public void testCompleteExceptionally() {
766 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
767 jsr166 1.34 protected void realCompute() {
768 jsr166 1.17 AsyncFib f = new AsyncFib(8);
769     f.completeExceptionally(new FJException());
770 jsr166 1.12 try {
771     f.invoke();
772     shouldThrow();
773 jsr166 1.24 } catch (FJException success) {
774     checkCompletedAbnormally(f, success);
775     }
776 jsr166 1.12 }};
777 jsr166 1.14 testInvokeOnPool(mainPool(), a);
778 dl 1.1 }
779    
780 jsr166 1.2 /**
781 jsr166 1.49 * completeExceptionally(null) surprisingly has the same effect as
782     * completeExceptionally(new RuntimeException())
783     */
784     public void testCompleteExceptionally_null() {
785     RecursiveAction a = new CheckedRecursiveAction() {
786     protected void realCompute() {
787     AsyncFib f = new AsyncFib(8);
788     f.completeExceptionally(null);
789     try {
790     f.invoke();
791     shouldThrow();
792     } catch (RuntimeException success) {
793     assertSame(success.getClass(), RuntimeException.class);
794     assertNull(success.getCause());
795     checkCompletedAbnormally(f, success);
796     }
797     }};
798     testInvokeOnPool(mainPool(), a);
799     }
800    
801     /**
802 dl 1.1 * invokeAll(t1, t2) invokes all task arguments
803     */
804     public void testInvokeAll2() {
805 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
806 jsr166 1.34 protected void realCompute() {
807 jsr166 1.12 AsyncFib f = new AsyncFib(8);
808     AsyncFib g = new AsyncFib(9);
809     invokeAll(f, g);
810 jsr166 1.17 assertEquals(21, f.number);
811     assertEquals(34, g.number);
812 jsr166 1.24 checkCompletedNormally(f);
813     checkCompletedNormally(g);
814 jsr166 1.12 }};
815 jsr166 1.14 testInvokeOnPool(mainPool(), a);
816 dl 1.1 }
817    
818 jsr166 1.2 /**
819 dl 1.1 * invokeAll(tasks) with 1 argument invokes task
820     */
821     public void testInvokeAll1() {
822 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
823 jsr166 1.34 protected void realCompute() {
824 jsr166 1.12 AsyncFib f = new AsyncFib(8);
825     invokeAll(f);
826 jsr166 1.24 checkCompletedNormally(f);
827 jsr166 1.17 assertEquals(21, f.number);
828 jsr166 1.12 }};
829 jsr166 1.14 testInvokeOnPool(mainPool(), a);
830 dl 1.1 }
831    
832 jsr166 1.2 /**
833 dl 1.1 * invokeAll(tasks) with > 2 argument invokes tasks
834     */
835     public void testInvokeAll3() {
836 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
837 jsr166 1.34 protected void realCompute() {
838 jsr166 1.12 AsyncFib f = new AsyncFib(8);
839     AsyncFib g = new AsyncFib(9);
840     AsyncFib h = new AsyncFib(7);
841     invokeAll(f, g, h);
842 jsr166 1.17 assertEquals(21, f.number);
843     assertEquals(34, g.number);
844     assertEquals(13, h.number);
845 jsr166 1.24 checkCompletedNormally(f);
846     checkCompletedNormally(g);
847     checkCompletedNormally(h);
848 jsr166 1.12 }};
849 jsr166 1.14 testInvokeOnPool(mainPool(), a);
850 dl 1.1 }
851    
852 jsr166 1.2 /**
853 dl 1.1 * invokeAll(collection) invokes all tasks in the collection
854     */
855     public void testInvokeAllCollection() {
856 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
857 jsr166 1.34 protected void realCompute() {
858 jsr166 1.12 AsyncFib f = new AsyncFib(8);
859     AsyncFib g = new AsyncFib(9);
860     AsyncFib h = new AsyncFib(7);
861     HashSet set = new HashSet();
862     set.add(f);
863     set.add(g);
864     set.add(h);
865     invokeAll(set);
866 jsr166 1.17 assertEquals(21, f.number);
867     assertEquals(34, g.number);
868     assertEquals(13, h.number);
869 jsr166 1.24 checkCompletedNormally(f);
870     checkCompletedNormally(g);
871     checkCompletedNormally(h);
872 jsr166 1.12 }};
873 jsr166 1.14 testInvokeOnPool(mainPool(), a);
874 dl 1.1 }
875    
876 jsr166 1.2 /**
877 dl 1.1 * invokeAll(tasks) with any null task throws NPE
878     */
879     public void testInvokeAllNPE() {
880 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
881 jsr166 1.34 protected void realCompute() {
882 jsr166 1.17 AsyncFib f = new AsyncFib(8);
883     AsyncFib g = new AsyncFib(9);
884     AsyncFib h = null;
885 jsr166 1.12 try {
886     invokeAll(f, g, h);
887     shouldThrow();
888 jsr166 1.17 } catch (NullPointerException success) {}
889 jsr166 1.12 }};
890 jsr166 1.14 testInvokeOnPool(mainPool(), a);
891 dl 1.1 }
892    
893 jsr166 1.2 /**
894 dl 1.1 * invokeAll(t1, t2) throw exception if any task does
895     */
896     public void testAbnormalInvokeAll2() {
897 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
898 jsr166 1.34 protected void realCompute() {
899 jsr166 1.17 AsyncFib f = new AsyncFib(8);
900     FailingAsyncFib g = new FailingAsyncFib(9);
901 jsr166 1.45 ForkJoinTask[] tasks = { f, g };
902 jsr166 1.50 shuffle(tasks);
903 jsr166 1.12 try {
904 jsr166 1.45 invokeAll(tasks);
905 jsr166 1.12 shouldThrow();
906 jsr166 1.24 } catch (FJException success) {
907     checkCompletedAbnormally(g, success);
908     }
909 jsr166 1.12 }};
910 jsr166 1.14 testInvokeOnPool(mainPool(), a);
911 dl 1.1 }
912    
913 jsr166 1.2 /**
914 dl 1.1 * invokeAll(tasks) with 1 argument throws exception if task does
915     */
916     public void testAbnormalInvokeAll1() {
917 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
918 jsr166 1.34 protected void realCompute() {
919 jsr166 1.17 FailingAsyncFib g = new FailingAsyncFib(9);
920 jsr166 1.12 try {
921     invokeAll(g);
922     shouldThrow();
923 jsr166 1.24 } catch (FJException success) {
924     checkCompletedAbnormally(g, success);
925     }
926 jsr166 1.12 }};
927 jsr166 1.14 testInvokeOnPool(mainPool(), a);
928 dl 1.1 }
929    
930 jsr166 1.2 /**
931 dl 1.1 * invokeAll(tasks) with > 2 argument throws exception if any task does
932     */
933     public void testAbnormalInvokeAll3() {
934 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
935 jsr166 1.34 protected void realCompute() {
936 jsr166 1.17 AsyncFib f = new AsyncFib(8);
937     FailingAsyncFib g = new FailingAsyncFib(9);
938     AsyncFib h = new AsyncFib(7);
939 jsr166 1.45 ForkJoinTask[] tasks = { f, g, h };
940 jsr166 1.50 shuffle(tasks);
941 jsr166 1.12 try {
942 jsr166 1.45 invokeAll(tasks);
943 jsr166 1.12 shouldThrow();
944 jsr166 1.24 } catch (FJException success) {
945     checkCompletedAbnormally(g, success);
946     }
947 jsr166 1.12 }};
948 jsr166 1.14 testInvokeOnPool(mainPool(), a);
949 dl 1.1 }
950    
951 jsr166 1.2 /**
952 jsr166 1.37 * invokeAll(collection) throws exception if any task does
953 dl 1.1 */
954     public void testAbnormalInvokeAllCollection() {
955 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
956 jsr166 1.34 protected void realCompute() {
957 jsr166 1.17 FailingAsyncFib f = new FailingAsyncFib(8);
958     AsyncFib g = new AsyncFib(9);
959     AsyncFib h = new AsyncFib(7);
960 jsr166 1.45 ForkJoinTask[] tasks = { f, g, h };
961 jsr166 1.50 shuffle(tasks);
962 jsr166 1.12 try {
963 jsr166 1.50 invokeAll(Arrays.asList(tasks));
964 jsr166 1.12 shouldThrow();
965 jsr166 1.24 } catch (FJException success) {
966     checkCompletedAbnormally(f, success);
967     }
968 jsr166 1.12 }};
969 jsr166 1.14 testInvokeOnPool(mainPool(), a);
970 dl 1.1 }
971    
972 jsr166 1.2 /**
973 dl 1.1 * tryUnfork returns true for most recent unexecuted task,
974     * and suppresses execution
975     */
976     public void testTryUnfork() {
977 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
978 jsr166 1.34 protected void realCompute() {
979 jsr166 1.12 AsyncFib g = new AsyncFib(9);
980 jsr166 1.17 assertSame(g, g.fork());
981 jsr166 1.12 AsyncFib f = new AsyncFib(8);
982 jsr166 1.17 assertSame(f, f.fork());
983     assertTrue(f.tryUnfork());
984 jsr166 1.12 helpQuiesce();
985 jsr166 1.24 checkNotDone(f);
986     checkCompletedNormally(g);
987 jsr166 1.12 }};
988 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
989 dl 1.1 }
990    
991 jsr166 1.2 /**
992 dl 1.1 * getSurplusQueuedTaskCount returns > 0 when
993     * there are more tasks than threads
994     */
995     public void testGetSurplusQueuedTaskCount() {
996 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
997 jsr166 1.34 protected void realCompute() {
998 jsr166 1.12 AsyncFib h = new AsyncFib(7);
999 jsr166 1.17 assertSame(h, h.fork());
1000 jsr166 1.12 AsyncFib g = new AsyncFib(9);
1001 jsr166 1.17 assertSame(g, g.fork());
1002 jsr166 1.12 AsyncFib f = new AsyncFib(8);
1003 jsr166 1.17 assertSame(f, f.fork());
1004     assertTrue(getSurplusQueuedTaskCount() > 0);
1005 jsr166 1.12 helpQuiesce();
1006 jsr166 1.24 assertEquals(0, getSurplusQueuedTaskCount());
1007     checkCompletedNormally(f);
1008     checkCompletedNormally(g);
1009     checkCompletedNormally(h);
1010 jsr166 1.12 }};
1011 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1012 dl 1.1 }
1013    
1014 jsr166 1.2 /**
1015 dl 1.1 * peekNextLocalTask returns most recent unexecuted task.
1016     */
1017     public void testPeekNextLocalTask() {
1018 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
1019 jsr166 1.34 protected void realCompute() {
1020 jsr166 1.12 AsyncFib g = new AsyncFib(9);
1021 jsr166 1.17 assertSame(g, g.fork());
1022 jsr166 1.12 AsyncFib f = new AsyncFib(8);
1023 jsr166 1.17 assertSame(f, f.fork());
1024     assertSame(f, peekNextLocalTask());
1025     assertNull(f.join());
1026 jsr166 1.24 checkCompletedNormally(f);
1027 jsr166 1.12 helpQuiesce();
1028 jsr166 1.24 checkCompletedNormally(g);
1029 jsr166 1.12 }};
1030 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1031 dl 1.1 }
1032    
1033 jsr166 1.2 /**
1034 jsr166 1.24 * pollNextLocalTask returns most recent unexecuted task without
1035     * executing it
1036 dl 1.1 */
1037     public void testPollNextLocalTask() {
1038 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
1039 jsr166 1.34 protected void realCompute() {
1040 jsr166 1.12 AsyncFib g = new AsyncFib(9);
1041 jsr166 1.17 assertSame(g, g.fork());
1042 jsr166 1.12 AsyncFib f = new AsyncFib(8);
1043 jsr166 1.17 assertSame(f, f.fork());
1044     assertSame(f, pollNextLocalTask());
1045 jsr166 1.12 helpQuiesce();
1046 jsr166 1.24 checkNotDone(f);
1047     assertEquals(34, g.number);
1048     checkCompletedNormally(g);
1049 jsr166 1.12 }};
1050 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1051 dl 1.1 }
1052    
1053 jsr166 1.2 /**
1054 jsr166 1.24 * pollTask returns an unexecuted task without executing it
1055 dl 1.1 */
1056     public void testPollTask() {
1057 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
1058 jsr166 1.34 protected void realCompute() {
1059 jsr166 1.12 AsyncFib g = new AsyncFib(9);
1060 jsr166 1.17 assertSame(g, g.fork());
1061 jsr166 1.12 AsyncFib f = new AsyncFib(8);
1062 jsr166 1.17 assertSame(f, f.fork());
1063     assertSame(f, pollTask());
1064 jsr166 1.12 helpQuiesce();
1065 jsr166 1.24 checkNotDone(f);
1066     checkCompletedNormally(g);
1067 jsr166 1.12 }};
1068 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1069 dl 1.1 }
1070    
1071 jsr166 1.2 /**
1072 dl 1.1 * peekNextLocalTask returns least recent unexecuted task in async mode
1073     */
1074     public void testPeekNextLocalTaskAsync() {
1075 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
1076 jsr166 1.34 protected void realCompute() {
1077 jsr166 1.12 AsyncFib g = new AsyncFib(9);
1078 jsr166 1.17 assertSame(g, g.fork());
1079 jsr166 1.12 AsyncFib f = new AsyncFib(8);
1080 jsr166 1.17 assertSame(f, f.fork());
1081     assertSame(g, peekNextLocalTask());
1082     assertNull(f.join());
1083 jsr166 1.12 helpQuiesce();
1084 jsr166 1.24 checkCompletedNormally(f);
1085     assertEquals(34, g.number);
1086     checkCompletedNormally(g);
1087 jsr166 1.12 }};
1088 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
1089 dl 1.1 }
1090    
1091 jsr166 1.2 /**
1092 jsr166 1.24 * pollNextLocalTask returns least recent unexecuted task without
1093     * executing it, in async mode
1094 dl 1.1 */
1095     public void testPollNextLocalTaskAsync() {
1096 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
1097 jsr166 1.34 protected void realCompute() {
1098 jsr166 1.12 AsyncFib g = new AsyncFib(9);
1099 jsr166 1.17 assertSame(g, g.fork());
1100 jsr166 1.12 AsyncFib f = new AsyncFib(8);
1101 jsr166 1.17 assertSame(f, f.fork());
1102     assertSame(g, pollNextLocalTask());
1103 jsr166 1.12 helpQuiesce();
1104 jsr166 1.24 assertEquals(21, f.number);
1105     checkCompletedNormally(f);
1106     checkNotDone(g);
1107 jsr166 1.12 }};
1108 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
1109 dl 1.1 }
1110    
1111 jsr166 1.2 /**
1112 jsr166 1.24 * pollTask returns an unexecuted task without executing it, in
1113     * async mode
1114 dl 1.1 */
1115     public void testPollTaskAsync() {
1116 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
1117 jsr166 1.34 protected void realCompute() {
1118 jsr166 1.12 AsyncFib g = new AsyncFib(9);
1119 jsr166 1.17 assertSame(g, g.fork());
1120 jsr166 1.12 AsyncFib f = new AsyncFib(8);
1121 jsr166 1.17 assertSame(f, f.fork());
1122     assertSame(g, pollTask());
1123 jsr166 1.12 helpQuiesce();
1124 jsr166 1.24 assertEquals(21, f.number);
1125     checkCompletedNormally(f);
1126     checkNotDone(g);
1127 jsr166 1.12 }};
1128 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
1129 dl 1.1 }
1130 dl 1.20
1131     // versions for singleton pools
1132    
1133     /**
1134     * invoke returns when task completes normally.
1135     * isCompletedAbnormally and isCancelled return false for normally
1136 jsr166 1.26 * completed tasks; getRawResult returns null.
1137 dl 1.20 */
1138     public void testInvokeSingleton() {
1139     RecursiveAction a = new CheckedRecursiveAction() {
1140 jsr166 1.34 protected void realCompute() {
1141 dl 1.20 AsyncFib f = new AsyncFib(8);
1142     assertNull(f.invoke());
1143     assertEquals(21, f.number);
1144 jsr166 1.24 checkCompletedNormally(f);
1145 dl 1.20 }};
1146     testInvokeOnPool(singletonPool(), a);
1147     }
1148    
1149     /**
1150     * quietlyInvoke task returns when task completes normally.
1151     * isCompletedAbnormally and isCancelled return false for normally
1152     * completed tasks
1153     */
1154     public void testQuietlyInvokeSingleton() {
1155     RecursiveAction a = new CheckedRecursiveAction() {
1156 jsr166 1.34 protected void realCompute() {
1157 dl 1.20 AsyncFib f = new AsyncFib(8);
1158     f.quietlyInvoke();
1159     assertEquals(21, f.number);
1160 jsr166 1.24 checkCompletedNormally(f);
1161 dl 1.20 }};
1162     testInvokeOnPool(singletonPool(), a);
1163     }
1164    
1165     /**
1166     * join of a forked task returns when task completes
1167     */
1168     public void testForkJoinSingleton() {
1169     RecursiveAction a = new CheckedRecursiveAction() {
1170 jsr166 1.34 protected void realCompute() {
1171 dl 1.20 AsyncFib f = new AsyncFib(8);
1172     assertSame(f, f.fork());
1173     assertNull(f.join());
1174     assertEquals(21, f.number);
1175 jsr166 1.24 checkCompletedNormally(f);
1176 dl 1.20 }};
1177     testInvokeOnPool(singletonPool(), a);
1178     }
1179    
1180     /**
1181     * get of a forked task returns when task completes
1182     */
1183     public void testForkGetSingleton() {
1184     RecursiveAction a = new CheckedRecursiveAction() {
1185 jsr166 1.34 protected void realCompute() throws Exception {
1186 dl 1.20 AsyncFib f = new AsyncFib(8);
1187     assertSame(f, f.fork());
1188     assertNull(f.get());
1189     assertEquals(21, f.number);
1190 jsr166 1.24 checkCompletedNormally(f);
1191 dl 1.20 }};
1192     testInvokeOnPool(singletonPool(), a);
1193     }
1194    
1195     /**
1196     * timed get of a forked task returns when task completes
1197     */
1198     public void testForkTimedGetSingleton() {
1199     RecursiveAction a = new CheckedRecursiveAction() {
1200 jsr166 1.34 protected void realCompute() throws Exception {
1201 dl 1.20 AsyncFib f = new AsyncFib(8);
1202     assertSame(f, f.fork());
1203     assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1204     assertEquals(21, f.number);
1205 jsr166 1.24 checkCompletedNormally(f);
1206 dl 1.20 }};
1207     testInvokeOnPool(singletonPool(), a);
1208     }
1209    
1210     /**
1211     * timed get with null time unit throws NPE
1212     */
1213     public void testForkTimedGetNPESingleton() {
1214     RecursiveAction a = new CheckedRecursiveAction() {
1215 jsr166 1.34 protected void realCompute() throws Exception {
1216 dl 1.20 AsyncFib f = new AsyncFib(8);
1217     assertSame(f, f.fork());
1218     try {
1219 jsr166 1.52 f.get(randomTimeout(), null);
1220 dl 1.20 shouldThrow();
1221     } catch (NullPointerException success) {}
1222     }};
1223     testInvokeOnPool(singletonPool(), a);
1224     }
1225    
1226     /**
1227     * quietlyJoin of a forked task returns when task completes
1228     */
1229     public void testForkQuietlyJoinSingleton() {
1230     RecursiveAction a = new CheckedRecursiveAction() {
1231 jsr166 1.34 protected void realCompute() {
1232 dl 1.20 AsyncFib f = new AsyncFib(8);
1233     assertSame(f, f.fork());
1234     f.quietlyJoin();
1235     assertEquals(21, f.number);
1236 jsr166 1.24 checkCompletedNormally(f);
1237 dl 1.20 }};
1238     testInvokeOnPool(singletonPool(), a);
1239     }
1240    
1241     /**
1242     * helpQuiesce returns when tasks are complete.
1243     * getQueuedTaskCount returns 0 when quiescent
1244     */
1245     public void testForkHelpQuiesceSingleton() {
1246     RecursiveAction a = new CheckedRecursiveAction() {
1247 jsr166 1.34 protected void realCompute() {
1248 dl 1.20 AsyncFib f = new AsyncFib(8);
1249     assertSame(f, f.fork());
1250 jsr166 1.32 helpQuiesce();
1251 jsr166 1.24 assertEquals(0, getQueuedTaskCount());
1252 dl 1.20 assertEquals(21, f.number);
1253 jsr166 1.24 checkCompletedNormally(f);
1254 dl 1.20 }};
1255     testInvokeOnPool(singletonPool(), a);
1256     }
1257    
1258     /**
1259     * invoke task throws exception when task completes abnormally
1260     */
1261     public void testAbnormalInvokeSingleton() {
1262     RecursiveAction a = new CheckedRecursiveAction() {
1263 jsr166 1.34 protected void realCompute() {
1264 dl 1.20 FailingAsyncFib f = new FailingAsyncFib(8);
1265     try {
1266     f.invoke();
1267     shouldThrow();
1268 jsr166 1.24 } catch (FJException success) {
1269     checkCompletedAbnormally(f, success);
1270     }
1271 dl 1.20 }};
1272     testInvokeOnPool(singletonPool(), a);
1273     }
1274    
1275     /**
1276     * quietlyInvoke task returns when task completes abnormally
1277     */
1278     public void testAbnormalQuietlyInvokeSingleton() {
1279     RecursiveAction a = new CheckedRecursiveAction() {
1280 jsr166 1.34 protected void realCompute() {
1281 dl 1.20 FailingAsyncFib f = new FailingAsyncFib(8);
1282     f.quietlyInvoke();
1283 jsr166 1.24 assertTrue(f.getException() instanceof FJException);
1284     checkCompletedAbnormally(f, f.getException());
1285 dl 1.20 }};
1286     testInvokeOnPool(singletonPool(), a);
1287     }
1288    
1289     /**
1290     * join of a forked task throws exception when task completes abnormally
1291     */
1292     public void testAbnormalForkJoinSingleton() {
1293     RecursiveAction a = new CheckedRecursiveAction() {
1294 jsr166 1.34 protected void realCompute() {
1295 dl 1.20 FailingAsyncFib f = new FailingAsyncFib(8);
1296     assertSame(f, f.fork());
1297     try {
1298     f.join();
1299     shouldThrow();
1300 jsr166 1.24 } catch (FJException success) {
1301     checkCompletedAbnormally(f, success);
1302     }
1303 dl 1.20 }};
1304     testInvokeOnPool(singletonPool(), a);
1305     }
1306    
1307     /**
1308     * get of a forked task throws exception when task completes abnormally
1309     */
1310     public void testAbnormalForkGetSingleton() {
1311     RecursiveAction a = new CheckedRecursiveAction() {
1312 jsr166 1.34 protected void realCompute() throws Exception {
1313 dl 1.20 FailingAsyncFib f = new FailingAsyncFib(8);
1314     assertSame(f, f.fork());
1315     try {
1316     f.get();
1317     shouldThrow();
1318     } catch (ExecutionException success) {
1319     Throwable cause = success.getCause();
1320     assertTrue(cause instanceof FJException);
1321 jsr166 1.24 checkCompletedAbnormally(f, cause);
1322 dl 1.20 }
1323     }};
1324     testInvokeOnPool(singletonPool(), a);
1325     }
1326    
1327     /**
1328     * timed get of a forked task throws exception when task completes abnormally
1329     */
1330     public void testAbnormalForkTimedGetSingleton() {
1331     RecursiveAction a = new CheckedRecursiveAction() {
1332 jsr166 1.34 protected void realCompute() throws Exception {
1333 dl 1.20 FailingAsyncFib f = new FailingAsyncFib(8);
1334     assertSame(f, f.fork());
1335     try {
1336     f.get(LONG_DELAY_MS, MILLISECONDS);
1337     shouldThrow();
1338     } catch (ExecutionException success) {
1339     Throwable cause = success.getCause();
1340     assertTrue(cause instanceof FJException);
1341 jsr166 1.24 checkCompletedAbnormally(f, cause);
1342 dl 1.20 }
1343     }};
1344     testInvokeOnPool(singletonPool(), a);
1345     }
1346    
1347     /**
1348     * quietlyJoin of a forked task returns when task completes abnormally
1349     */
1350     public void testAbnormalForkQuietlyJoinSingleton() {
1351     RecursiveAction a = new CheckedRecursiveAction() {
1352 jsr166 1.34 protected void realCompute() {
1353 dl 1.20 FailingAsyncFib f = new FailingAsyncFib(8);
1354     assertSame(f, f.fork());
1355     f.quietlyJoin();
1356     assertTrue(f.getException() instanceof FJException);
1357 jsr166 1.24 checkCompletedAbnormally(f, f.getException());
1358 dl 1.20 }};
1359     testInvokeOnPool(singletonPool(), a);
1360     }
1361    
1362     /**
1363     * invoke task throws exception when task cancelled
1364     */
1365     public void testCancelledInvokeSingleton() {
1366     RecursiveAction a = new CheckedRecursiveAction() {
1367 jsr166 1.34 protected void realCompute() {
1368 dl 1.20 AsyncFib f = new AsyncFib(8);
1369     assertTrue(f.cancel(true));
1370     try {
1371     f.invoke();
1372     shouldThrow();
1373     } catch (CancellationException success) {
1374 jsr166 1.24 checkCancelled(f);
1375 dl 1.20 }
1376     }};
1377     testInvokeOnPool(singletonPool(), a);
1378     }
1379    
1380     /**
1381     * join of a forked task throws exception when task cancelled
1382     */
1383     public void testCancelledForkJoinSingleton() {
1384     RecursiveAction a = new CheckedRecursiveAction() {
1385 jsr166 1.34 protected void realCompute() {
1386 dl 1.20 AsyncFib f = new AsyncFib(8);
1387     assertTrue(f.cancel(true));
1388     assertSame(f, f.fork());
1389     try {
1390     f.join();
1391     shouldThrow();
1392     } catch (CancellationException success) {
1393 jsr166 1.24 checkCancelled(f);
1394 dl 1.20 }
1395     }};
1396     testInvokeOnPool(singletonPool(), a);
1397     }
1398    
1399     /**
1400     * get of a forked task throws exception when task cancelled
1401     */
1402     public void testCancelledForkGetSingleton() {
1403     RecursiveAction a = new CheckedRecursiveAction() {
1404 jsr166 1.34 protected void realCompute() throws Exception {
1405 dl 1.20 AsyncFib f = new AsyncFib(8);
1406     assertTrue(f.cancel(true));
1407     assertSame(f, f.fork());
1408     try {
1409     f.get();
1410     shouldThrow();
1411     } catch (CancellationException success) {
1412 jsr166 1.24 checkCancelled(f);
1413 dl 1.20 }
1414     }};
1415     testInvokeOnPool(singletonPool(), a);
1416     }
1417    
1418     /**
1419     * timed get of a forked task throws exception when task cancelled
1420     */
1421     public void testCancelledForkTimedGetSingleton() throws Exception {
1422     RecursiveAction a = new CheckedRecursiveAction() {
1423 jsr166 1.34 protected void realCompute() throws Exception {
1424 dl 1.20 AsyncFib f = new AsyncFib(8);
1425     assertTrue(f.cancel(true));
1426     assertSame(f, f.fork());
1427     try {
1428     f.get(LONG_DELAY_MS, MILLISECONDS);
1429     shouldThrow();
1430     } catch (CancellationException success) {
1431 jsr166 1.24 checkCancelled(f);
1432 dl 1.20 }
1433     }};
1434     testInvokeOnPool(singletonPool(), a);
1435     }
1436    
1437     /**
1438     * quietlyJoin of a forked task returns when task cancelled
1439     */
1440     public void testCancelledForkQuietlyJoinSingleton() {
1441     RecursiveAction a = new CheckedRecursiveAction() {
1442 jsr166 1.34 protected void realCompute() {
1443 dl 1.20 AsyncFib f = new AsyncFib(8);
1444     assertTrue(f.cancel(true));
1445     assertSame(f, f.fork());
1446     f.quietlyJoin();
1447 jsr166 1.24 checkCancelled(f);
1448 dl 1.20 }};
1449     testInvokeOnPool(singletonPool(), a);
1450     }
1451    
1452     /**
1453     * invoke task throws exception after invoking completeExceptionally
1454     */
1455     public void testCompleteExceptionallySingleton() {
1456     RecursiveAction a = new CheckedRecursiveAction() {
1457 jsr166 1.34 protected void realCompute() {
1458 dl 1.20 AsyncFib f = new AsyncFib(8);
1459     f.completeExceptionally(new FJException());
1460     try {
1461     f.invoke();
1462     shouldThrow();
1463 jsr166 1.24 } catch (FJException success) {
1464     checkCompletedAbnormally(f, success);
1465     }
1466 dl 1.20 }};
1467     testInvokeOnPool(singletonPool(), a);
1468     }
1469    
1470     /**
1471     * invokeAll(t1, t2) invokes all task arguments
1472     */
1473     public void testInvokeAll2Singleton() {
1474     RecursiveAction a = new CheckedRecursiveAction() {
1475 jsr166 1.34 protected void realCompute() {
1476 dl 1.20 AsyncFib f = new AsyncFib(8);
1477     AsyncFib g = new AsyncFib(9);
1478     invokeAll(f, g);
1479     assertEquals(21, f.number);
1480     assertEquals(34, g.number);
1481 jsr166 1.24 checkCompletedNormally(f);
1482     checkCompletedNormally(g);
1483 dl 1.20 }};
1484     testInvokeOnPool(singletonPool(), a);
1485     }
1486    
1487     /**
1488     * invokeAll(tasks) with 1 argument invokes task
1489     */
1490     public void testInvokeAll1Singleton() {
1491     RecursiveAction a = new CheckedRecursiveAction() {
1492 jsr166 1.34 protected void realCompute() {
1493 dl 1.20 AsyncFib f = new AsyncFib(8);
1494     invokeAll(f);
1495 jsr166 1.24 checkCompletedNormally(f);
1496 dl 1.20 assertEquals(21, f.number);
1497     }};
1498     testInvokeOnPool(singletonPool(), a);
1499     }
1500    
1501     /**
1502     * invokeAll(tasks) with > 2 argument invokes tasks
1503     */
1504     public void testInvokeAll3Singleton() {
1505     RecursiveAction a = new CheckedRecursiveAction() {
1506 jsr166 1.34 protected void realCompute() {
1507 dl 1.20 AsyncFib f = new AsyncFib(8);
1508     AsyncFib g = new AsyncFib(9);
1509     AsyncFib h = new AsyncFib(7);
1510     invokeAll(f, g, h);
1511     assertEquals(21, f.number);
1512     assertEquals(34, g.number);
1513     assertEquals(13, h.number);
1514 jsr166 1.24 checkCompletedNormally(f);
1515     checkCompletedNormally(g);
1516     checkCompletedNormally(h);
1517 dl 1.20 }};
1518     testInvokeOnPool(singletonPool(), a);
1519     }
1520    
1521     /**
1522     * invokeAll(collection) invokes all tasks in the collection
1523     */
1524     public void testInvokeAllCollectionSingleton() {
1525     RecursiveAction a = new CheckedRecursiveAction() {
1526 jsr166 1.34 protected void realCompute() {
1527 dl 1.20 AsyncFib f = new AsyncFib(8);
1528     AsyncFib g = new AsyncFib(9);
1529     AsyncFib h = new AsyncFib(7);
1530     HashSet set = new HashSet();
1531     set.add(f);
1532     set.add(g);
1533     set.add(h);
1534     invokeAll(set);
1535     assertEquals(21, f.number);
1536     assertEquals(34, g.number);
1537     assertEquals(13, h.number);
1538 jsr166 1.24 checkCompletedNormally(f);
1539     checkCompletedNormally(g);
1540     checkCompletedNormally(h);
1541 dl 1.20 }};
1542     testInvokeOnPool(singletonPool(), a);
1543     }
1544    
1545     /**
1546     * invokeAll(tasks) with any null task throws NPE
1547     */
1548     public void testInvokeAllNPESingleton() {
1549     RecursiveAction a = new CheckedRecursiveAction() {
1550 jsr166 1.34 protected void realCompute() {
1551 dl 1.20 AsyncFib f = new AsyncFib(8);
1552     AsyncFib g = new AsyncFib(9);
1553     AsyncFib h = null;
1554     try {
1555     invokeAll(f, g, h);
1556     shouldThrow();
1557     } catch (NullPointerException success) {}
1558     }};
1559     testInvokeOnPool(singletonPool(), a);
1560     }
1561    
1562     /**
1563     * invokeAll(t1, t2) throw exception if any task does
1564     */
1565     public void testAbnormalInvokeAll2Singleton() {
1566     RecursiveAction a = new CheckedRecursiveAction() {
1567 jsr166 1.34 protected void realCompute() {
1568 dl 1.20 AsyncFib f = new AsyncFib(8);
1569     FailingAsyncFib g = new FailingAsyncFib(9);
1570 jsr166 1.45 ForkJoinTask[] tasks = { f, g };
1571 jsr166 1.50 shuffle(tasks);
1572 dl 1.20 try {
1573 jsr166 1.45 invokeAll(tasks);
1574 dl 1.20 shouldThrow();
1575 jsr166 1.24 } catch (FJException success) {
1576     checkCompletedAbnormally(g, success);
1577     }
1578 dl 1.20 }};
1579     testInvokeOnPool(singletonPool(), a);
1580     }
1581    
1582     /**
1583     * invokeAll(tasks) with 1 argument throws exception if task does
1584     */
1585     public void testAbnormalInvokeAll1Singleton() {
1586     RecursiveAction a = new CheckedRecursiveAction() {
1587 jsr166 1.34 protected void realCompute() {
1588 dl 1.20 FailingAsyncFib g = new FailingAsyncFib(9);
1589     try {
1590     invokeAll(g);
1591     shouldThrow();
1592 jsr166 1.24 } catch (FJException success) {
1593     checkCompletedAbnormally(g, success);
1594     }
1595 dl 1.20 }};
1596     testInvokeOnPool(singletonPool(), a);
1597     }
1598    
1599     /**
1600     * invokeAll(tasks) with > 2 argument throws exception if any task does
1601     */
1602     public void testAbnormalInvokeAll3Singleton() {
1603     RecursiveAction a = new CheckedRecursiveAction() {
1604 jsr166 1.34 protected void realCompute() {
1605 dl 1.20 AsyncFib f = new AsyncFib(8);
1606     FailingAsyncFib g = new FailingAsyncFib(9);
1607     AsyncFib h = new AsyncFib(7);
1608 jsr166 1.45 ForkJoinTask[] tasks = { f, g, h };
1609 jsr166 1.50 shuffle(tasks);
1610 dl 1.20 try {
1611 jsr166 1.45 invokeAll(tasks);
1612 dl 1.20 shouldThrow();
1613 jsr166 1.24 } catch (FJException success) {
1614     checkCompletedAbnormally(g, success);
1615     }
1616 dl 1.20 }};
1617     testInvokeOnPool(singletonPool(), a);
1618     }
1619    
1620     /**
1621 jsr166 1.37 * invokeAll(collection) throws exception if any task does
1622 dl 1.20 */
1623     public void testAbnormalInvokeAllCollectionSingleton() {
1624     RecursiveAction a = new CheckedRecursiveAction() {
1625 jsr166 1.34 protected void realCompute() {
1626 dl 1.20 FailingAsyncFib f = new FailingAsyncFib(8);
1627     AsyncFib g = new AsyncFib(9);
1628     AsyncFib h = new AsyncFib(7);
1629 jsr166 1.45 ForkJoinTask[] tasks = { f, g, h };
1630 jsr166 1.50 shuffle(tasks);
1631 dl 1.20 try {
1632 jsr166 1.50 invokeAll(Arrays.asList(tasks));
1633 dl 1.20 shouldThrow();
1634 jsr166 1.24 } catch (FJException success) {
1635     checkCompletedAbnormally(f, success);
1636     }
1637 dl 1.20 }};
1638     testInvokeOnPool(singletonPool(), a);
1639     }
1640    
1641 dl 1.35 /**
1642     * ForkJoinTask.quietlyComplete returns when task completes
1643     * normally without setting a value. The most recent value
1644     * established by setRawResult(V) (or null by default) is returned
1645     * from invoke.
1646     */
1647     public void testQuietlyComplete() {
1648     RecursiveAction a = new CheckedRecursiveAction() {
1649     protected void realCompute() {
1650     AsyncFib f = new AsyncFib(8);
1651     f.quietlyComplete();
1652     assertEquals(8, f.number);
1653     checkCompletedNormally(f);
1654     }};
1655     testInvokeOnPool(mainPool(), a);
1656     }
1657    
1658 jsr166 1.53 /**
1659     * adapt(runnable).toString() contains toString of wrapped task
1660     */
1661     public void testAdapt_Runnable_toString() {
1662     if (testImplementationDetails) {
1663     Runnable r = () -> {};
1664     ForkJoinTask<?> task = ForkJoinTask.adapt(r);
1665     assertEquals(
1666     identityString(task) + "[Wrapped task = " + r.toString() + "]",
1667     task.toString());
1668     }
1669     }
1670    
1671     /**
1672     * adapt(runnable, x).toString() contains toString of wrapped task
1673     */
1674     public void testAdapt_Runnable_withResult_toString() {
1675     if (testImplementationDetails) {
1676     Runnable r = () -> {};
1677     ForkJoinTask<String> task = ForkJoinTask.adapt(r, "");
1678     assertEquals(
1679     identityString(task) + "[Wrapped task = " + r.toString() + "]",
1680     task.toString());
1681     }
1682     }
1683    
1684     /**
1685     * adapt(callable).toString() contains toString of wrapped task
1686     */
1687     public void testAdapt_Callable_toString() {
1688     if (testImplementationDetails) {
1689     Callable<String> c = () -> "";
1690     ForkJoinTask<String> task = ForkJoinTask.adapt(c);
1691     assertEquals(
1692     identityString(task) + "[Wrapped task = " + c.toString() + "]",
1693     task.toString());
1694     }
1695     }
1696 jsr166 1.58
1697 jsr166 1.59 // adaptInterruptible deferred to its own independent change
1698     // https://bugs.openjdk.java.net/browse/JDK-8246587
1699    
1700     // /**
1701     // * adaptInterruptible(callable).toString() contains toString of wrapped task
1702     // */
1703     // public void testAdaptInterruptible_Callable_toString() {
1704     // if (testImplementationDetails) {
1705     // Callable<String> c = () -> "";
1706     // ForkJoinTask<String> task = ForkJoinTask.adaptInterruptible(c);
1707     // assertEquals(
1708     // identityString(task) + "[Wrapped task = " + c.toString() + "]",
1709     // task.toString());
1710     // }
1711     // }
1712 dl 1.1 }