ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinTaskTest.java
Revision: 1.44
Committed: Tue Oct 6 00:36:55 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.43: +8 -8 lines
Log Message:
t0 -> startTime

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4 jsr166 1.31 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
6 jsr166 1.40
7     import static java.util.concurrent.TimeUnit.MILLISECONDS;
8     import static java.util.concurrent.TimeUnit.SECONDS;
9    
10 jsr166 1.39 import java.util.HashSet;
11     import java.util.concurrent.CancellationException;
12 dl 1.8 import java.util.concurrent.ExecutionException;
13     import java.util.concurrent.ForkJoinPool;
14     import java.util.concurrent.ForkJoinTask;
15     import java.util.concurrent.RecursiveAction;
16 jsr166 1.24 import java.util.concurrent.TimeoutException;
17 jsr166 1.15 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
18 jsr166 1.40
19     import junit.framework.Test;
20     import junit.framework.TestSuite;
21 dl 1.1
22     public class ForkJoinTaskTest extends JSR166TestCase {
23    
24     public static void main(String[] args) {
25 jsr166 1.41 main(suite(), args);
26 dl 1.1 }
27 jsr166 1.12
28 dl 1.1 public static Test suite() {
29 jsr166 1.5 return new TestSuite(ForkJoinTaskTest.class);
30 dl 1.1 }
31    
32 dl 1.20 // Runs with "mainPool" use > 1 thread. singletonPool tests use 1
33 jsr166 1.21 static final int mainPoolSize =
34 dl 1.20 Math.max(2, Runtime.getRuntime().availableProcessors());
35    
36 jsr166 1.14 private static ForkJoinPool mainPool() {
37 dl 1.20 return new ForkJoinPool(mainPoolSize);
38 jsr166 1.14 }
39    
40     private static ForkJoinPool singletonPool() {
41     return new ForkJoinPool(1);
42     }
43    
44     private static ForkJoinPool asyncSingletonPool() {
45     return new ForkJoinPool(1,
46     ForkJoinPool.defaultForkJoinWorkerThreadFactory,
47     null, true);
48     }
49    
50     private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
51 jsr166 1.42 try (PoolCleaner cleaner = cleaner(pool)) {
52 jsr166 1.17 assertFalse(a.isDone());
53     assertFalse(a.isCompletedNormally());
54     assertFalse(a.isCompletedAbnormally());
55     assertFalse(a.isCancelled());
56     assertNull(a.getException());
57 jsr166 1.24 assertNull(a.getRawResult());
58 jsr166 1.17
59     assertNull(pool.invoke(a));
60    
61     assertTrue(a.isDone());
62     assertTrue(a.isCompletedNormally());
63     assertFalse(a.isCompletedAbnormally());
64     assertFalse(a.isCancelled());
65     assertNull(a.getException());
66 jsr166 1.24 assertNull(a.getRawResult());
67 jsr166 1.14 }
68     }
69    
70 jsr166 1.24 void checkNotDone(ForkJoinTask a) {
71     assertFalse(a.isDone());
72     assertFalse(a.isCompletedNormally());
73     assertFalse(a.isCompletedAbnormally());
74     assertFalse(a.isCancelled());
75     assertNull(a.getException());
76     assertNull(a.getRawResult());
77    
78     try {
79     a.get(0L, SECONDS);
80     shouldThrow();
81     } catch (TimeoutException success) {
82     } catch (Throwable fail) { threadUnexpectedException(fail); }
83     }
84    
85     <T> void checkCompletedNormally(ForkJoinTask<T> a) {
86     checkCompletedNormally(a, null);
87     }
88    
89     <T> void checkCompletedNormally(ForkJoinTask<T> a, T expected) {
90     assertTrue(a.isDone());
91     assertFalse(a.isCancelled());
92     assertTrue(a.isCompletedNormally());
93     assertFalse(a.isCompletedAbnormally());
94     assertNull(a.getException());
95     assertSame(expected, a.getRawResult());
96 jsr166 1.28
97     {
98     Thread.currentThread().interrupt();
99 jsr166 1.44 long startTime = System.nanoTime();
100 jsr166 1.28 assertSame(expected, a.join());
101 jsr166 1.44 assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
102 jsr166 1.29 Thread.interrupted();
103 jsr166 1.28 }
104    
105     {
106     Thread.currentThread().interrupt();
107 jsr166 1.44 long startTime = System.nanoTime();
108 jsr166 1.28 a.quietlyJoin(); // should be no-op
109 jsr166 1.44 assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
110 jsr166 1.29 Thread.interrupted();
111 jsr166 1.28 }
112    
113 jsr166 1.25 assertFalse(a.cancel(false));
114     assertFalse(a.cancel(true));
115 jsr166 1.24 try {
116     assertSame(expected, a.get());
117     } catch (Throwable fail) { threadUnexpectedException(fail); }
118     try {
119     assertSame(expected, a.get(5L, SECONDS));
120     } catch (Throwable fail) { threadUnexpectedException(fail); }
121     }
122    
123     void checkCancelled(ForkJoinTask a) {
124     assertTrue(a.isDone());
125     assertTrue(a.isCancelled());
126     assertFalse(a.isCompletedNormally());
127     assertTrue(a.isCompletedAbnormally());
128     assertTrue(a.getException() instanceof CancellationException);
129     assertNull(a.getRawResult());
130 jsr166 1.25 assertTrue(a.cancel(false));
131     assertTrue(a.cancel(true));
132 jsr166 1.24
133     try {
134 jsr166 1.28 Thread.currentThread().interrupt();
135 jsr166 1.24 a.join();
136     shouldThrow();
137     } catch (CancellationException success) {
138     } catch (Throwable fail) { threadUnexpectedException(fail); }
139 jsr166 1.29 Thread.interrupted();
140 jsr166 1.24
141 jsr166 1.28 {
142 jsr166 1.44 long startTime = System.nanoTime();
143 jsr166 1.28 a.quietlyJoin(); // should be no-op
144 jsr166 1.44 assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
145 jsr166 1.28 }
146    
147 jsr166 1.24 try {
148     a.get();
149     shouldThrow();
150     } catch (CancellationException success) {
151     } catch (Throwable fail) { threadUnexpectedException(fail); }
152    
153     try {
154     a.get(5L, SECONDS);
155     shouldThrow();
156     } catch (CancellationException success) {
157     } catch (Throwable fail) { threadUnexpectedException(fail); }
158     }
159    
160     void checkCompletedAbnormally(ForkJoinTask a, Throwable t) {
161     assertTrue(a.isDone());
162     assertFalse(a.isCancelled());
163     assertFalse(a.isCompletedNormally());
164     assertTrue(a.isCompletedAbnormally());
165 dl 1.30 assertSame(t.getClass(), a.getException().getClass());
166 jsr166 1.24 assertNull(a.getRawResult());
167 jsr166 1.25 assertFalse(a.cancel(false));
168     assertFalse(a.cancel(true));
169 jsr166 1.24
170     try {
171 jsr166 1.28 Thread.currentThread().interrupt();
172 jsr166 1.24 a.join();
173     shouldThrow();
174     } catch (Throwable expected) {
175 dl 1.30 assertSame(t.getClass(), expected.getClass());
176 jsr166 1.24 }
177 jsr166 1.29 Thread.interrupted();
178 jsr166 1.24
179 jsr166 1.28 {
180 jsr166 1.44 long startTime = System.nanoTime();
181 jsr166 1.28 a.quietlyJoin(); // should be no-op
182 jsr166 1.44 assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
183 jsr166 1.28 }
184    
185 jsr166 1.24 try {
186     a.get();
187     shouldThrow();
188     } catch (ExecutionException success) {
189 dl 1.30 assertSame(t.getClass(), success.getCause().getClass());
190 jsr166 1.24 } catch (Throwable fail) { threadUnexpectedException(fail); }
191    
192     try {
193     a.get(5L, SECONDS);
194     shouldThrow();
195     } catch (ExecutionException success) {
196 dl 1.30 assertSame(t.getClass(), success.getCause().getClass());
197 jsr166 1.24 } catch (Throwable fail) { threadUnexpectedException(fail); }
198     }
199    
200 jsr166 1.14 /*
201 dl 1.1 * Testing coverage notes:
202 jsr166 1.2 *
203 dl 1.1 * To test extension methods and overrides, most tests use
204     * BinaryAsyncAction extension class that processes joins
205     * differently than supplied Recursive forms.
206 jsr166 1.2 */
207 dl 1.1
208 dl 1.30 public static final class FJException extends RuntimeException {
209 dl 1.1 FJException() { super(); }
210     }
211    
212 jsr166 1.19 abstract static class BinaryAsyncAction extends ForkJoinTask<Void> {
213 dl 1.1 private volatile int controlState;
214    
215     static final AtomicIntegerFieldUpdater<BinaryAsyncAction> controlStateUpdater =
216 jsr166 1.2 AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class,
217 dl 1.1 "controlState");
218    
219 dl 1.43 private volatile BinaryAsyncAction parent;
220 dl 1.1
221 dl 1.43 private volatile BinaryAsyncAction sibling;
222 dl 1.1
223     protected BinaryAsyncAction() {
224     }
225    
226     public final Void getRawResult() { return null; }
227     protected final void setRawResult(Void mustBeNull) { }
228    
229     public final void linkSubtasks(BinaryAsyncAction x, BinaryAsyncAction y) {
230     x.parent = y.parent = this;
231     x.sibling = y;
232     y.sibling = x;
233     }
234    
235     protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) {
236     }
237    
238     protected boolean onException() {
239     return true;
240     }
241    
242     public void linkAndForkSubtasks(BinaryAsyncAction x, BinaryAsyncAction y) {
243     linkSubtasks(x, y);
244     y.fork();
245     x.fork();
246     }
247    
248     private void completeThis() {
249     super.complete(null);
250     }
251    
252     private void completeThisExceptionally(Throwable ex) {
253     super.completeExceptionally(ex);
254     }
255    
256     public final void complete() {
257     BinaryAsyncAction a = this;
258     for (;;) {
259     BinaryAsyncAction s = a.sibling;
260     BinaryAsyncAction p = a.parent;
261     a.sibling = null;
262     a.parent = null;
263     a.completeThis();
264     if (p == null || p.compareAndSetControlState(0, 1))
265     break;
266     try {
267     p.onComplete(a, s);
268 jsr166 1.2 } catch (Throwable rex) {
269 dl 1.1 p.completeExceptionally(rex);
270     return;
271     }
272     a = p;
273     }
274     }
275    
276     public final void completeExceptionally(Throwable ex) {
277     BinaryAsyncAction a = this;
278     while (!a.isCompletedAbnormally()) {
279     a.completeThisExceptionally(ex);
280     BinaryAsyncAction s = a.sibling;
281     if (s != null)
282     s.cancel(false);
283     if (!a.onException() || (a = a.parent) == null)
284     break;
285     }
286     }
287    
288     public final BinaryAsyncAction getParent() {
289     return parent;
290     }
291    
292     public BinaryAsyncAction getSibling() {
293     return sibling;
294     }
295    
296     public void reinitialize() {
297     parent = sibling = null;
298     super.reinitialize();
299     }
300    
301     protected final int getControlState() {
302     return controlState;
303     }
304    
305 jsr166 1.2 protected final boolean compareAndSetControlState(int expect,
306 dl 1.1 int update) {
307     return controlStateUpdater.compareAndSet(this, expect, update);
308     }
309    
310     protected final void setControlState(int value) {
311     controlState = value;
312     }
313    
314     protected final void incrementControlState() {
315     controlStateUpdater.incrementAndGet(this);
316     }
317    
318     protected final void decrementControlState() {
319     controlStateUpdater.decrementAndGet(this);
320     }
321    
322     }
323    
324 jsr166 1.6 static final class AsyncFib extends BinaryAsyncAction {
325 dl 1.1 int number;
326 jsr166 1.2 public AsyncFib(int n) {
327 dl 1.1 this.number = n;
328     }
329 jsr166 1.2
330 dl 1.1 public final boolean exec() {
331     AsyncFib f = this;
332     int n = f.number;
333     if (n > 1) {
334     while (n > 1) {
335     AsyncFib p = f;
336     AsyncFib r = new AsyncFib(n - 2);
337     f = new AsyncFib(--n);
338     p.linkSubtasks(r, f);
339     r.fork();
340     }
341     f.number = n;
342     }
343     f.complete();
344     return false;
345     }
346    
347     protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) {
348     number = ((AsyncFib)x).number + ((AsyncFib)y).number;
349     }
350     }
351    
352 jsr166 1.6 static final class FailingAsyncFib extends BinaryAsyncAction {
353 dl 1.1 int number;
354 jsr166 1.2 public FailingAsyncFib(int n) {
355 dl 1.1 this.number = n;
356     }
357 jsr166 1.2
358 dl 1.1 public final boolean exec() {
359     FailingAsyncFib f = this;
360     int n = f.number;
361     if (n > 1) {
362     while (n > 1) {
363     FailingAsyncFib p = f;
364     FailingAsyncFib r = new FailingAsyncFib(n - 2);
365     f = new FailingAsyncFib(--n);
366     p.linkSubtasks(r, f);
367     r.fork();
368     }
369     f.number = n;
370     }
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     f.get(5L, null);
467     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 dl 1.1 * invokeAll(t1, t2) invokes all task arguments
776     */
777     public void testInvokeAll2() {
778 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
779 jsr166 1.34 protected void realCompute() {
780 jsr166 1.12 AsyncFib f = new AsyncFib(8);
781     AsyncFib g = new AsyncFib(9);
782     invokeAll(f, g);
783 jsr166 1.17 assertEquals(21, f.number);
784     assertEquals(34, g.number);
785 jsr166 1.24 checkCompletedNormally(f);
786     checkCompletedNormally(g);
787 jsr166 1.12 }};
788 jsr166 1.14 testInvokeOnPool(mainPool(), a);
789 dl 1.1 }
790    
791 jsr166 1.2 /**
792 dl 1.1 * invokeAll(tasks) with 1 argument invokes task
793     */
794     public void testInvokeAll1() {
795 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
796 jsr166 1.34 protected void realCompute() {
797 jsr166 1.12 AsyncFib f = new AsyncFib(8);
798     invokeAll(f);
799 jsr166 1.24 checkCompletedNormally(f);
800 jsr166 1.17 assertEquals(21, f.number);
801 jsr166 1.12 }};
802 jsr166 1.14 testInvokeOnPool(mainPool(), a);
803 dl 1.1 }
804    
805 jsr166 1.2 /**
806 dl 1.1 * invokeAll(tasks) with > 2 argument invokes tasks
807     */
808     public void testInvokeAll3() {
809 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
810 jsr166 1.34 protected void realCompute() {
811 jsr166 1.12 AsyncFib f = new AsyncFib(8);
812     AsyncFib g = new AsyncFib(9);
813     AsyncFib h = new AsyncFib(7);
814     invokeAll(f, g, h);
815 jsr166 1.17 assertEquals(21, f.number);
816     assertEquals(34, g.number);
817     assertEquals(13, h.number);
818 jsr166 1.24 checkCompletedNormally(f);
819     checkCompletedNormally(g);
820     checkCompletedNormally(h);
821 jsr166 1.12 }};
822 jsr166 1.14 testInvokeOnPool(mainPool(), a);
823 dl 1.1 }
824    
825 jsr166 1.2 /**
826 dl 1.1 * invokeAll(collection) invokes all tasks in the collection
827     */
828     public void testInvokeAllCollection() {
829 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
830 jsr166 1.34 protected void realCompute() {
831 jsr166 1.12 AsyncFib f = new AsyncFib(8);
832     AsyncFib g = new AsyncFib(9);
833     AsyncFib h = new AsyncFib(7);
834     HashSet set = new HashSet();
835     set.add(f);
836     set.add(g);
837     set.add(h);
838     invokeAll(set);
839 jsr166 1.17 assertEquals(21, f.number);
840     assertEquals(34, g.number);
841     assertEquals(13, h.number);
842 jsr166 1.24 checkCompletedNormally(f);
843     checkCompletedNormally(g);
844     checkCompletedNormally(h);
845 jsr166 1.12 }};
846 jsr166 1.14 testInvokeOnPool(mainPool(), a);
847 dl 1.1 }
848    
849 jsr166 1.2 /**
850 dl 1.1 * invokeAll(tasks) with any null task throws NPE
851     */
852     public void testInvokeAllNPE() {
853 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
854 jsr166 1.34 protected void realCompute() {
855 jsr166 1.17 AsyncFib f = new AsyncFib(8);
856     AsyncFib g = new AsyncFib(9);
857     AsyncFib h = null;
858 jsr166 1.12 try {
859     invokeAll(f, g, h);
860     shouldThrow();
861 jsr166 1.17 } catch (NullPointerException success) {}
862 jsr166 1.12 }};
863 jsr166 1.14 testInvokeOnPool(mainPool(), a);
864 dl 1.1 }
865    
866 jsr166 1.2 /**
867 dl 1.1 * invokeAll(t1, t2) throw exception if any task does
868     */
869     public void testAbnormalInvokeAll2() {
870 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
871 jsr166 1.34 protected void realCompute() {
872 jsr166 1.17 AsyncFib f = new AsyncFib(8);
873     FailingAsyncFib g = new FailingAsyncFib(9);
874 jsr166 1.12 try {
875     invokeAll(f, g);
876     shouldThrow();
877 jsr166 1.24 } catch (FJException success) {
878     checkCompletedAbnormally(g, success);
879     }
880 jsr166 1.12 }};
881 jsr166 1.14 testInvokeOnPool(mainPool(), a);
882 dl 1.1 }
883    
884 jsr166 1.2 /**
885 dl 1.1 * invokeAll(tasks) with 1 argument throws exception if task does
886     */
887     public void testAbnormalInvokeAll1() {
888 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
889 jsr166 1.34 protected void realCompute() {
890 jsr166 1.17 FailingAsyncFib g = new FailingAsyncFib(9);
891 jsr166 1.12 try {
892     invokeAll(g);
893     shouldThrow();
894 jsr166 1.24 } catch (FJException success) {
895     checkCompletedAbnormally(g, success);
896     }
897 jsr166 1.12 }};
898 jsr166 1.14 testInvokeOnPool(mainPool(), a);
899 dl 1.1 }
900    
901 jsr166 1.2 /**
902 dl 1.1 * invokeAll(tasks) with > 2 argument throws exception if any task does
903     */
904     public void testAbnormalInvokeAll3() {
905 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
906 jsr166 1.34 protected void realCompute() {
907 jsr166 1.17 AsyncFib f = new AsyncFib(8);
908     FailingAsyncFib g = new FailingAsyncFib(9);
909     AsyncFib h = new AsyncFib(7);
910 jsr166 1.12 try {
911     invokeAll(f, g, h);
912     shouldThrow();
913 jsr166 1.24 } catch (FJException success) {
914     checkCompletedAbnormally(g, success);
915     }
916 jsr166 1.12 }};
917 jsr166 1.14 testInvokeOnPool(mainPool(), a);
918 dl 1.1 }
919    
920 jsr166 1.2 /**
921 jsr166 1.37 * invokeAll(collection) throws exception if any task does
922 dl 1.1 */
923     public void testAbnormalInvokeAllCollection() {
924 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
925 jsr166 1.34 protected void realCompute() {
926 jsr166 1.17 FailingAsyncFib f = new FailingAsyncFib(8);
927     AsyncFib g = new AsyncFib(9);
928     AsyncFib h = new AsyncFib(7);
929     HashSet set = new HashSet();
930     set.add(f);
931     set.add(g);
932     set.add(h);
933 jsr166 1.12 try {
934     invokeAll(set);
935     shouldThrow();
936 jsr166 1.24 } catch (FJException success) {
937     checkCompletedAbnormally(f, success);
938     }
939 jsr166 1.12 }};
940 jsr166 1.14 testInvokeOnPool(mainPool(), a);
941 dl 1.1 }
942    
943 jsr166 1.2 /**
944 dl 1.1 * tryUnfork returns true for most recent unexecuted task,
945     * and suppresses execution
946     */
947     public void testTryUnfork() {
948 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
949 jsr166 1.34 protected void realCompute() {
950 jsr166 1.12 AsyncFib g = new AsyncFib(9);
951 jsr166 1.17 assertSame(g, g.fork());
952 jsr166 1.12 AsyncFib f = new AsyncFib(8);
953 jsr166 1.17 assertSame(f, f.fork());
954     assertTrue(f.tryUnfork());
955 jsr166 1.12 helpQuiesce();
956 jsr166 1.24 checkNotDone(f);
957     checkCompletedNormally(g);
958 jsr166 1.12 }};
959 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
960 dl 1.1 }
961    
962 jsr166 1.2 /**
963 dl 1.1 * getSurplusQueuedTaskCount returns > 0 when
964     * there are more tasks than threads
965     */
966     public void testGetSurplusQueuedTaskCount() {
967 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
968 jsr166 1.34 protected void realCompute() {
969 jsr166 1.12 AsyncFib h = new AsyncFib(7);
970 jsr166 1.17 assertSame(h, h.fork());
971 jsr166 1.12 AsyncFib g = new AsyncFib(9);
972 jsr166 1.17 assertSame(g, g.fork());
973 jsr166 1.12 AsyncFib f = new AsyncFib(8);
974 jsr166 1.17 assertSame(f, f.fork());
975     assertTrue(getSurplusQueuedTaskCount() > 0);
976 jsr166 1.12 helpQuiesce();
977 jsr166 1.24 assertEquals(0, getSurplusQueuedTaskCount());
978     checkCompletedNormally(f);
979     checkCompletedNormally(g);
980     checkCompletedNormally(h);
981 jsr166 1.12 }};
982 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
983 dl 1.1 }
984    
985 jsr166 1.2 /**
986 dl 1.1 * peekNextLocalTask returns most recent unexecuted task.
987     */
988     public void testPeekNextLocalTask() {
989 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
990 jsr166 1.34 protected void realCompute() {
991 jsr166 1.12 AsyncFib g = new AsyncFib(9);
992 jsr166 1.17 assertSame(g, g.fork());
993 jsr166 1.12 AsyncFib f = new AsyncFib(8);
994 jsr166 1.17 assertSame(f, f.fork());
995     assertSame(f, peekNextLocalTask());
996     assertNull(f.join());
997 jsr166 1.24 checkCompletedNormally(f);
998 jsr166 1.12 helpQuiesce();
999 jsr166 1.24 checkCompletedNormally(g);
1000 jsr166 1.12 }};
1001 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1002 dl 1.1 }
1003    
1004 jsr166 1.2 /**
1005 jsr166 1.24 * pollNextLocalTask returns most recent unexecuted task without
1006     * executing it
1007 dl 1.1 */
1008     public void testPollNextLocalTask() {
1009 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
1010 jsr166 1.34 protected void realCompute() {
1011 jsr166 1.12 AsyncFib g = new AsyncFib(9);
1012 jsr166 1.17 assertSame(g, g.fork());
1013 jsr166 1.12 AsyncFib f = new AsyncFib(8);
1014 jsr166 1.17 assertSame(f, f.fork());
1015     assertSame(f, pollNextLocalTask());
1016 jsr166 1.12 helpQuiesce();
1017 jsr166 1.24 checkNotDone(f);
1018     assertEquals(34, g.number);
1019     checkCompletedNormally(g);
1020 jsr166 1.12 }};
1021 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1022 dl 1.1 }
1023    
1024 jsr166 1.2 /**
1025 jsr166 1.24 * pollTask returns an unexecuted task without executing it
1026 dl 1.1 */
1027     public void testPollTask() {
1028 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
1029 jsr166 1.34 protected void realCompute() {
1030 jsr166 1.12 AsyncFib g = new AsyncFib(9);
1031 jsr166 1.17 assertSame(g, g.fork());
1032 jsr166 1.12 AsyncFib f = new AsyncFib(8);
1033 jsr166 1.17 assertSame(f, f.fork());
1034     assertSame(f, pollTask());
1035 jsr166 1.12 helpQuiesce();
1036 jsr166 1.24 checkNotDone(f);
1037     checkCompletedNormally(g);
1038 jsr166 1.12 }};
1039 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1040 dl 1.1 }
1041    
1042 jsr166 1.2 /**
1043 dl 1.1 * peekNextLocalTask returns least recent unexecuted task in async mode
1044     */
1045     public void testPeekNextLocalTaskAsync() {
1046 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
1047 jsr166 1.34 protected void realCompute() {
1048 jsr166 1.12 AsyncFib g = new AsyncFib(9);
1049 jsr166 1.17 assertSame(g, g.fork());
1050 jsr166 1.12 AsyncFib f = new AsyncFib(8);
1051 jsr166 1.17 assertSame(f, f.fork());
1052     assertSame(g, peekNextLocalTask());
1053     assertNull(f.join());
1054 jsr166 1.12 helpQuiesce();
1055 jsr166 1.24 checkCompletedNormally(f);
1056     assertEquals(34, g.number);
1057     checkCompletedNormally(g);
1058 jsr166 1.12 }};
1059 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
1060 dl 1.1 }
1061    
1062 jsr166 1.2 /**
1063 jsr166 1.24 * pollNextLocalTask returns least recent unexecuted task without
1064     * executing it, in async mode
1065 dl 1.1 */
1066     public void testPollNextLocalTaskAsync() {
1067 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
1068 jsr166 1.34 protected void realCompute() {
1069 jsr166 1.12 AsyncFib g = new AsyncFib(9);
1070 jsr166 1.17 assertSame(g, g.fork());
1071 jsr166 1.12 AsyncFib f = new AsyncFib(8);
1072 jsr166 1.17 assertSame(f, f.fork());
1073     assertSame(g, pollNextLocalTask());
1074 jsr166 1.12 helpQuiesce();
1075 jsr166 1.24 assertEquals(21, f.number);
1076     checkCompletedNormally(f);
1077     checkNotDone(g);
1078 jsr166 1.12 }};
1079 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
1080 dl 1.1 }
1081    
1082 jsr166 1.2 /**
1083 jsr166 1.24 * pollTask returns an unexecuted task without executing it, in
1084     * async mode
1085 dl 1.1 */
1086     public void testPollTaskAsync() {
1087 jsr166 1.17 RecursiveAction a = new CheckedRecursiveAction() {
1088 jsr166 1.34 protected void realCompute() {
1089 jsr166 1.12 AsyncFib g = new AsyncFib(9);
1090 jsr166 1.17 assertSame(g, g.fork());
1091 jsr166 1.12 AsyncFib f = new AsyncFib(8);
1092 jsr166 1.17 assertSame(f, f.fork());
1093     assertSame(g, pollTask());
1094 jsr166 1.12 helpQuiesce();
1095 jsr166 1.24 assertEquals(21, f.number);
1096     checkCompletedNormally(f);
1097     checkNotDone(g);
1098 jsr166 1.12 }};
1099 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
1100 dl 1.1 }
1101 dl 1.20
1102     // versions for singleton pools
1103    
1104     /**
1105     * invoke returns when task completes normally.
1106     * isCompletedAbnormally and isCancelled return false for normally
1107 jsr166 1.26 * completed tasks; getRawResult returns null.
1108 dl 1.20 */
1109     public void testInvokeSingleton() {
1110     RecursiveAction a = new CheckedRecursiveAction() {
1111 jsr166 1.34 protected void realCompute() {
1112 dl 1.20 AsyncFib f = new AsyncFib(8);
1113     assertNull(f.invoke());
1114     assertEquals(21, f.number);
1115 jsr166 1.24 checkCompletedNormally(f);
1116 dl 1.20 }};
1117     testInvokeOnPool(singletonPool(), a);
1118     }
1119    
1120     /**
1121     * quietlyInvoke task returns when task completes normally.
1122     * isCompletedAbnormally and isCancelled return false for normally
1123     * completed tasks
1124     */
1125     public void testQuietlyInvokeSingleton() {
1126     RecursiveAction a = new CheckedRecursiveAction() {
1127 jsr166 1.34 protected void realCompute() {
1128 dl 1.20 AsyncFib f = new AsyncFib(8);
1129     f.quietlyInvoke();
1130     assertEquals(21, f.number);
1131 jsr166 1.24 checkCompletedNormally(f);
1132 dl 1.20 }};
1133     testInvokeOnPool(singletonPool(), a);
1134     }
1135    
1136     /**
1137     * join of a forked task returns when task completes
1138     */
1139     public void testForkJoinSingleton() {
1140     RecursiveAction a = new CheckedRecursiveAction() {
1141 jsr166 1.34 protected void realCompute() {
1142 dl 1.20 AsyncFib f = new AsyncFib(8);
1143     assertSame(f, f.fork());
1144     assertNull(f.join());
1145     assertEquals(21, f.number);
1146 jsr166 1.24 checkCompletedNormally(f);
1147 dl 1.20 }};
1148     testInvokeOnPool(singletonPool(), a);
1149     }
1150    
1151     /**
1152     * get of a forked task returns when task completes
1153     */
1154     public void testForkGetSingleton() {
1155     RecursiveAction a = new CheckedRecursiveAction() {
1156 jsr166 1.34 protected void realCompute() throws Exception {
1157 dl 1.20 AsyncFib f = new AsyncFib(8);
1158     assertSame(f, f.fork());
1159     assertNull(f.get());
1160     assertEquals(21, f.number);
1161 jsr166 1.24 checkCompletedNormally(f);
1162 dl 1.20 }};
1163     testInvokeOnPool(singletonPool(), a);
1164     }
1165    
1166     /**
1167     * timed get of a forked task returns when task completes
1168     */
1169     public void testForkTimedGetSingleton() {
1170     RecursiveAction a = new CheckedRecursiveAction() {
1171 jsr166 1.34 protected void realCompute() throws Exception {
1172 dl 1.20 AsyncFib f = new AsyncFib(8);
1173     assertSame(f, f.fork());
1174     assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1175     assertEquals(21, f.number);
1176 jsr166 1.24 checkCompletedNormally(f);
1177 dl 1.20 }};
1178     testInvokeOnPool(singletonPool(), a);
1179     }
1180    
1181     /**
1182     * timed get with null time unit throws NPE
1183     */
1184     public void testForkTimedGetNPESingleton() {
1185     RecursiveAction a = new CheckedRecursiveAction() {
1186 jsr166 1.34 protected void realCompute() throws Exception {
1187 dl 1.20 AsyncFib f = new AsyncFib(8);
1188     assertSame(f, f.fork());
1189     try {
1190     f.get(5L, null);
1191     shouldThrow();
1192     } catch (NullPointerException success) {}
1193     }};
1194     testInvokeOnPool(singletonPool(), a);
1195     }
1196    
1197     /**
1198     * quietlyJoin of a forked task returns when task completes
1199     */
1200     public void testForkQuietlyJoinSingleton() {
1201     RecursiveAction a = new CheckedRecursiveAction() {
1202 jsr166 1.34 protected void realCompute() {
1203 dl 1.20 AsyncFib f = new AsyncFib(8);
1204     assertSame(f, f.fork());
1205     f.quietlyJoin();
1206     assertEquals(21, f.number);
1207 jsr166 1.24 checkCompletedNormally(f);
1208 dl 1.20 }};
1209     testInvokeOnPool(singletonPool(), a);
1210     }
1211    
1212     /**
1213     * helpQuiesce returns when tasks are complete.
1214     * getQueuedTaskCount returns 0 when quiescent
1215     */
1216     public void testForkHelpQuiesceSingleton() {
1217     RecursiveAction a = new CheckedRecursiveAction() {
1218 jsr166 1.34 protected void realCompute() {
1219 dl 1.20 AsyncFib f = new AsyncFib(8);
1220     assertSame(f, f.fork());
1221 jsr166 1.32 helpQuiesce();
1222 jsr166 1.24 assertEquals(0, getQueuedTaskCount());
1223 dl 1.20 assertEquals(21, f.number);
1224 jsr166 1.24 checkCompletedNormally(f);
1225 dl 1.20 }};
1226     testInvokeOnPool(singletonPool(), a);
1227     }
1228    
1229     /**
1230     * invoke task throws exception when task completes abnormally
1231     */
1232     public void testAbnormalInvokeSingleton() {
1233     RecursiveAction a = new CheckedRecursiveAction() {
1234 jsr166 1.34 protected void realCompute() {
1235 dl 1.20 FailingAsyncFib f = new FailingAsyncFib(8);
1236     try {
1237     f.invoke();
1238     shouldThrow();
1239 jsr166 1.24 } catch (FJException success) {
1240     checkCompletedAbnormally(f, success);
1241     }
1242 dl 1.20 }};
1243     testInvokeOnPool(singletonPool(), a);
1244     }
1245    
1246     /**
1247     * quietlyInvoke task returns when task completes abnormally
1248     */
1249     public void testAbnormalQuietlyInvokeSingleton() {
1250     RecursiveAction a = new CheckedRecursiveAction() {
1251 jsr166 1.34 protected void realCompute() {
1252 dl 1.20 FailingAsyncFib f = new FailingAsyncFib(8);
1253     f.quietlyInvoke();
1254 jsr166 1.24 assertTrue(f.getException() instanceof FJException);
1255     checkCompletedAbnormally(f, f.getException());
1256 dl 1.20 }};
1257     testInvokeOnPool(singletonPool(), a);
1258     }
1259    
1260     /**
1261     * join of a forked task throws exception when task completes abnormally
1262     */
1263     public void testAbnormalForkJoinSingleton() {
1264     RecursiveAction a = new CheckedRecursiveAction() {
1265 jsr166 1.34 protected void realCompute() {
1266 dl 1.20 FailingAsyncFib f = new FailingAsyncFib(8);
1267     assertSame(f, f.fork());
1268     try {
1269     f.join();
1270     shouldThrow();
1271 jsr166 1.24 } catch (FJException success) {
1272     checkCompletedAbnormally(f, success);
1273     }
1274 dl 1.20 }};
1275     testInvokeOnPool(singletonPool(), a);
1276     }
1277    
1278     /**
1279     * get of a forked task throws exception when task completes abnormally
1280     */
1281     public void testAbnormalForkGetSingleton() {
1282     RecursiveAction a = new CheckedRecursiveAction() {
1283 jsr166 1.34 protected void realCompute() throws Exception {
1284 dl 1.20 FailingAsyncFib f = new FailingAsyncFib(8);
1285     assertSame(f, f.fork());
1286     try {
1287     f.get();
1288     shouldThrow();
1289     } catch (ExecutionException success) {
1290     Throwable cause = success.getCause();
1291     assertTrue(cause instanceof FJException);
1292 jsr166 1.24 checkCompletedAbnormally(f, cause);
1293 dl 1.20 }
1294     }};
1295     testInvokeOnPool(singletonPool(), a);
1296     }
1297    
1298     /**
1299     * timed get of a forked task throws exception when task completes abnormally
1300     */
1301     public void testAbnormalForkTimedGetSingleton() {
1302     RecursiveAction a = new CheckedRecursiveAction() {
1303 jsr166 1.34 protected void realCompute() throws Exception {
1304 dl 1.20 FailingAsyncFib f = new FailingAsyncFib(8);
1305     assertSame(f, f.fork());
1306     try {
1307     f.get(LONG_DELAY_MS, MILLISECONDS);
1308     shouldThrow();
1309     } catch (ExecutionException success) {
1310     Throwable cause = success.getCause();
1311     assertTrue(cause instanceof FJException);
1312 jsr166 1.24 checkCompletedAbnormally(f, cause);
1313 dl 1.20 }
1314     }};
1315     testInvokeOnPool(singletonPool(), a);
1316     }
1317    
1318     /**
1319     * quietlyJoin of a forked task returns when task completes abnormally
1320     */
1321     public void testAbnormalForkQuietlyJoinSingleton() {
1322     RecursiveAction a = new CheckedRecursiveAction() {
1323 jsr166 1.34 protected void realCompute() {
1324 dl 1.20 FailingAsyncFib f = new FailingAsyncFib(8);
1325     assertSame(f, f.fork());
1326     f.quietlyJoin();
1327     assertTrue(f.getException() instanceof FJException);
1328 jsr166 1.24 checkCompletedAbnormally(f, f.getException());
1329 dl 1.20 }};
1330     testInvokeOnPool(singletonPool(), a);
1331     }
1332    
1333     /**
1334     * invoke task throws exception when task cancelled
1335     */
1336     public void testCancelledInvokeSingleton() {
1337     RecursiveAction a = new CheckedRecursiveAction() {
1338 jsr166 1.34 protected void realCompute() {
1339 dl 1.20 AsyncFib f = new AsyncFib(8);
1340     assertTrue(f.cancel(true));
1341     try {
1342     f.invoke();
1343     shouldThrow();
1344     } catch (CancellationException success) {
1345 jsr166 1.24 checkCancelled(f);
1346 dl 1.20 }
1347     }};
1348     testInvokeOnPool(singletonPool(), a);
1349     }
1350    
1351     /**
1352     * join of a forked task throws exception when task cancelled
1353     */
1354     public void testCancelledForkJoinSingleton() {
1355     RecursiveAction a = new CheckedRecursiveAction() {
1356 jsr166 1.34 protected void realCompute() {
1357 dl 1.20 AsyncFib f = new AsyncFib(8);
1358     assertTrue(f.cancel(true));
1359     assertSame(f, f.fork());
1360     try {
1361     f.join();
1362     shouldThrow();
1363     } catch (CancellationException success) {
1364 jsr166 1.24 checkCancelled(f);
1365 dl 1.20 }
1366     }};
1367     testInvokeOnPool(singletonPool(), a);
1368     }
1369    
1370     /**
1371     * get of a forked task throws exception when task cancelled
1372     */
1373     public void testCancelledForkGetSingleton() {
1374     RecursiveAction a = new CheckedRecursiveAction() {
1375 jsr166 1.34 protected void realCompute() throws Exception {
1376 dl 1.20 AsyncFib f = new AsyncFib(8);
1377     assertTrue(f.cancel(true));
1378     assertSame(f, f.fork());
1379     try {
1380     f.get();
1381     shouldThrow();
1382     } catch (CancellationException success) {
1383 jsr166 1.24 checkCancelled(f);
1384 dl 1.20 }
1385     }};
1386     testInvokeOnPool(singletonPool(), a);
1387     }
1388    
1389     /**
1390     * timed get of a forked task throws exception when task cancelled
1391     */
1392     public void testCancelledForkTimedGetSingleton() throws Exception {
1393     RecursiveAction a = new CheckedRecursiveAction() {
1394 jsr166 1.34 protected void realCompute() throws Exception {
1395 dl 1.20 AsyncFib f = new AsyncFib(8);
1396     assertTrue(f.cancel(true));
1397     assertSame(f, f.fork());
1398     try {
1399     f.get(LONG_DELAY_MS, MILLISECONDS);
1400     shouldThrow();
1401     } catch (CancellationException success) {
1402 jsr166 1.24 checkCancelled(f);
1403 dl 1.20 }
1404     }};
1405     testInvokeOnPool(singletonPool(), a);
1406     }
1407    
1408     /**
1409     * quietlyJoin of a forked task returns when task cancelled
1410     */
1411     public void testCancelledForkQuietlyJoinSingleton() {
1412     RecursiveAction a = new CheckedRecursiveAction() {
1413 jsr166 1.34 protected void realCompute() {
1414 dl 1.20 AsyncFib f = new AsyncFib(8);
1415     assertTrue(f.cancel(true));
1416     assertSame(f, f.fork());
1417     f.quietlyJoin();
1418 jsr166 1.24 checkCancelled(f);
1419 dl 1.20 }};
1420     testInvokeOnPool(singletonPool(), a);
1421     }
1422    
1423     /**
1424     * invoke task throws exception after invoking completeExceptionally
1425     */
1426     public void testCompleteExceptionallySingleton() {
1427     RecursiveAction a = new CheckedRecursiveAction() {
1428 jsr166 1.34 protected void realCompute() {
1429 dl 1.20 AsyncFib f = new AsyncFib(8);
1430     f.completeExceptionally(new FJException());
1431     try {
1432     f.invoke();
1433     shouldThrow();
1434 jsr166 1.24 } catch (FJException success) {
1435     checkCompletedAbnormally(f, success);
1436     }
1437 dl 1.20 }};
1438     testInvokeOnPool(singletonPool(), a);
1439     }
1440    
1441     /**
1442     * invokeAll(t1, t2) invokes all task arguments
1443     */
1444     public void testInvokeAll2Singleton() {
1445     RecursiveAction a = new CheckedRecursiveAction() {
1446 jsr166 1.34 protected void realCompute() {
1447 dl 1.20 AsyncFib f = new AsyncFib(8);
1448     AsyncFib g = new AsyncFib(9);
1449     invokeAll(f, g);
1450     assertEquals(21, f.number);
1451     assertEquals(34, g.number);
1452 jsr166 1.24 checkCompletedNormally(f);
1453     checkCompletedNormally(g);
1454 dl 1.20 }};
1455     testInvokeOnPool(singletonPool(), a);
1456     }
1457    
1458     /**
1459     * invokeAll(tasks) with 1 argument invokes task
1460     */
1461     public void testInvokeAll1Singleton() {
1462     RecursiveAction a = new CheckedRecursiveAction() {
1463 jsr166 1.34 protected void realCompute() {
1464 dl 1.20 AsyncFib f = new AsyncFib(8);
1465     invokeAll(f);
1466 jsr166 1.24 checkCompletedNormally(f);
1467 dl 1.20 assertEquals(21, f.number);
1468     }};
1469     testInvokeOnPool(singletonPool(), a);
1470     }
1471    
1472     /**
1473     * invokeAll(tasks) with > 2 argument invokes tasks
1474     */
1475     public void testInvokeAll3Singleton() {
1476     RecursiveAction a = new CheckedRecursiveAction() {
1477 jsr166 1.34 protected void realCompute() {
1478 dl 1.20 AsyncFib f = new AsyncFib(8);
1479     AsyncFib g = new AsyncFib(9);
1480     AsyncFib h = new AsyncFib(7);
1481     invokeAll(f, g, h);
1482     assertEquals(21, f.number);
1483     assertEquals(34, g.number);
1484     assertEquals(13, h.number);
1485 jsr166 1.24 checkCompletedNormally(f);
1486     checkCompletedNormally(g);
1487     checkCompletedNormally(h);
1488 dl 1.20 }};
1489     testInvokeOnPool(singletonPool(), a);
1490     }
1491    
1492     /**
1493     * invokeAll(collection) invokes all tasks in the collection
1494     */
1495     public void testInvokeAllCollectionSingleton() {
1496     RecursiveAction a = new CheckedRecursiveAction() {
1497 jsr166 1.34 protected void realCompute() {
1498 dl 1.20 AsyncFib f = new AsyncFib(8);
1499     AsyncFib g = new AsyncFib(9);
1500     AsyncFib h = new AsyncFib(7);
1501     HashSet set = new HashSet();
1502     set.add(f);
1503     set.add(g);
1504     set.add(h);
1505     invokeAll(set);
1506     assertEquals(21, f.number);
1507     assertEquals(34, g.number);
1508     assertEquals(13, h.number);
1509 jsr166 1.24 checkCompletedNormally(f);
1510     checkCompletedNormally(g);
1511     checkCompletedNormally(h);
1512 dl 1.20 }};
1513     testInvokeOnPool(singletonPool(), a);
1514     }
1515    
1516     /**
1517     * invokeAll(tasks) with any null task throws NPE
1518     */
1519     public void testInvokeAllNPESingleton() {
1520     RecursiveAction a = new CheckedRecursiveAction() {
1521 jsr166 1.34 protected void realCompute() {
1522 dl 1.20 AsyncFib f = new AsyncFib(8);
1523     AsyncFib g = new AsyncFib(9);
1524     AsyncFib h = null;
1525     try {
1526     invokeAll(f, g, h);
1527     shouldThrow();
1528     } catch (NullPointerException success) {}
1529     }};
1530     testInvokeOnPool(singletonPool(), a);
1531     }
1532    
1533     /**
1534     * invokeAll(t1, t2) throw exception if any task does
1535     */
1536     public void testAbnormalInvokeAll2Singleton() {
1537     RecursiveAction a = new CheckedRecursiveAction() {
1538 jsr166 1.34 protected void realCompute() {
1539 dl 1.20 AsyncFib f = new AsyncFib(8);
1540     FailingAsyncFib g = new FailingAsyncFib(9);
1541     try {
1542     invokeAll(f, g);
1543     shouldThrow();
1544 jsr166 1.24 } catch (FJException success) {
1545     checkCompletedAbnormally(g, success);
1546     }
1547 dl 1.20 }};
1548     testInvokeOnPool(singletonPool(), a);
1549     }
1550    
1551     /**
1552     * invokeAll(tasks) with 1 argument throws exception if task does
1553     */
1554     public void testAbnormalInvokeAll1Singleton() {
1555     RecursiveAction a = new CheckedRecursiveAction() {
1556 jsr166 1.34 protected void realCompute() {
1557 dl 1.20 FailingAsyncFib g = new FailingAsyncFib(9);
1558     try {
1559     invokeAll(g);
1560     shouldThrow();
1561 jsr166 1.24 } catch (FJException success) {
1562     checkCompletedAbnormally(g, success);
1563     }
1564 dl 1.20 }};
1565     testInvokeOnPool(singletonPool(), a);
1566     }
1567    
1568     /**
1569     * invokeAll(tasks) with > 2 argument throws exception if any task does
1570     */
1571     public void testAbnormalInvokeAll3Singleton() {
1572     RecursiveAction a = new CheckedRecursiveAction() {
1573 jsr166 1.34 protected void realCompute() {
1574 dl 1.20 AsyncFib f = new AsyncFib(8);
1575     FailingAsyncFib g = new FailingAsyncFib(9);
1576     AsyncFib h = new AsyncFib(7);
1577     try {
1578     invokeAll(f, g, h);
1579     shouldThrow();
1580 jsr166 1.24 } catch (FJException success) {
1581     checkCompletedAbnormally(g, success);
1582     }
1583 dl 1.20 }};
1584     testInvokeOnPool(singletonPool(), a);
1585     }
1586    
1587     /**
1588 jsr166 1.37 * invokeAll(collection) throws exception if any task does
1589 dl 1.20 */
1590     public void testAbnormalInvokeAllCollectionSingleton() {
1591     RecursiveAction a = new CheckedRecursiveAction() {
1592 jsr166 1.34 protected void realCompute() {
1593 dl 1.20 FailingAsyncFib f = new FailingAsyncFib(8);
1594     AsyncFib g = new AsyncFib(9);
1595     AsyncFib h = new AsyncFib(7);
1596     HashSet set = new HashSet();
1597     set.add(f);
1598     set.add(g);
1599     set.add(h);
1600     try {
1601     invokeAll(set);
1602     shouldThrow();
1603 jsr166 1.24 } catch (FJException success) {
1604     checkCompletedAbnormally(f, success);
1605     }
1606 dl 1.20 }};
1607     testInvokeOnPool(singletonPool(), a);
1608     }
1609    
1610 dl 1.35 /**
1611     * ForkJoinTask.quietlyComplete returns when task completes
1612     * normally without setting a value. The most recent value
1613     * established by setRawResult(V) (or null by default) is returned
1614     * from invoke.
1615     */
1616     public void testQuietlyComplete() {
1617     RecursiveAction a = new CheckedRecursiveAction() {
1618     protected void realCompute() {
1619     AsyncFib f = new AsyncFib(8);
1620     f.quietlyComplete();
1621     assertEquals(8, f.number);
1622     checkCompletedNormally(f);
1623     }};
1624     testInvokeOnPool(mainPool(), a);
1625     }
1626    
1627 dl 1.1 }