ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinTaskTest.java
Revision: 1.55
Committed: Wed Nov 8 02:21:43 2017 UTC (6 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.54: +1 -1 lines
Log Message:
rollback: better exception handling

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