ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinTaskTest.java
Revision: 1.41
Committed: Sat Apr 25 04:55:30 2015 UTC (9 years ago) by jsr166
Branch: MAIN
Changes since 1.40: +1 -1 lines
Log Message:
improve main methods; respect system properties; actually fail if a test fails

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