ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinTask8Test.java
Revision: 1.12
Committed: Sat Feb 7 22:32:48 2015 UTC (9 years, 2 months ago) by jsr166
Branch: MAIN
Changes since 1.11: +260 -534 lines
Log Message:
refactor singleton pool code; automate checking of fib results

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     * http://creativecommons.org/publicdomain/zero/1.0/
5     */
6 jsr166 1.7
7     import static java.util.concurrent.TimeUnit.MILLISECONDS;
8     import static java.util.concurrent.TimeUnit.SECONDS;
9    
10 jsr166 1.12 import java.util.Arrays;
11 dl 1.1 import java.util.concurrent.ExecutionException;
12     import java.util.concurrent.ForkJoinPool;
13     import java.util.concurrent.ForkJoinTask;
14     import java.util.concurrent.RecursiveAction;
15     import java.util.concurrent.TimeoutException;
16 jsr166 1.7
17     import junit.framework.Test;
18     import junit.framework.TestSuite;
19 dl 1.1
20     public class ForkJoinTask8Test extends JSR166TestCase {
21    
22     /*
23     * Testing notes: This differs from ForkJoinTaskTest mainly by
24     * defining a version of BinaryAsyncAction that uses JDK8 task
25     * tags for control state, thereby testing getForkJoinTaskTag,
26     * setForkJoinTaskTag, and compareAndSetForkJoinTaskTag across
27     * various contexts. Most of the test methods using it are
28     * otherwise identical, but omitting retest of those dealing with
29     * cancellation, which is not represented in this tag scheme.
30     */
31    
32     static final short INITIAL_STATE = -1;
33     static final short COMPLETE_STATE = 0;
34     static final short EXCEPTION_STATE = 1;
35    
36     public static void main(String[] args) {
37     junit.textui.TestRunner.run(suite());
38     }
39    
40     public static Test suite() {
41     return new TestSuite(ForkJoinTask8Test.class);
42     }
43    
44     // Runs with "mainPool" use > 1 thread. singletonPool tests use 1
45     static final int mainPoolSize =
46     Math.max(2, Runtime.getRuntime().availableProcessors());
47    
48     private static ForkJoinPool mainPool() {
49     return new ForkJoinPool(mainPoolSize);
50     }
51    
52     private static ForkJoinPool singletonPool() {
53     return new ForkJoinPool(1);
54     }
55    
56     private static ForkJoinPool asyncSingletonPool() {
57     return new ForkJoinPool(1,
58     ForkJoinPool.defaultForkJoinWorkerThreadFactory,
59     null, true);
60     }
61    
62 jsr166 1.12 // Compute fib naively and efficiently
63     final int[] fib;
64     {
65     int[] fib = new int[10];
66     fib[0] = 0;
67     fib[1] = 1;
68     for (int i = 2; i < fib.length; i++)
69     fib[i] = fib[i - 1] + fib[i - 2];
70     this.fib = fib;
71     }
72    
73 dl 1.1 private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
74     try {
75     assertFalse(a.isDone());
76     assertFalse(a.isCompletedNormally());
77     assertFalse(a.isCompletedAbnormally());
78     assertFalse(a.isCancelled());
79     assertNull(a.getException());
80     assertNull(a.getRawResult());
81    
82     assertNull(pool.invoke(a));
83    
84     assertTrue(a.isDone());
85     assertTrue(a.isCompletedNormally());
86     assertFalse(a.isCompletedAbnormally());
87     assertFalse(a.isCancelled());
88     assertNull(a.getException());
89     assertNull(a.getRawResult());
90     } finally {
91     joinPool(pool);
92     }
93     }
94    
95     void checkNotDone(ForkJoinTask a) {
96     assertFalse(a.isDone());
97     assertFalse(a.isCompletedNormally());
98     assertFalse(a.isCompletedAbnormally());
99     assertFalse(a.isCancelled());
100     assertNull(a.getException());
101     assertNull(a.getRawResult());
102     if (a instanceof BinaryAsyncAction)
103     assertTrue(((BinaryAsyncAction)a).getForkJoinTaskTag() == INITIAL_STATE);
104    
105     try {
106     a.get(0L, SECONDS);
107     shouldThrow();
108     } catch (TimeoutException success) {
109     } catch (Throwable fail) { threadUnexpectedException(fail); }
110     }
111    
112     <T> void checkCompletedNormally(ForkJoinTask<T> a) {
113     checkCompletedNormally(a, null);
114     }
115    
116     <T> void checkCompletedNormally(ForkJoinTask<T> a, T expected) {
117     assertTrue(a.isDone());
118     assertFalse(a.isCancelled());
119     assertTrue(a.isCompletedNormally());
120     assertFalse(a.isCompletedAbnormally());
121     assertNull(a.getException());
122     assertSame(expected, a.getRawResult());
123     if (a instanceof BinaryAsyncAction)
124     assertTrue(((BinaryAsyncAction)a).getForkJoinTaskTag() == COMPLETE_STATE);
125    
126     {
127     Thread.currentThread().interrupt();
128     long t0 = System.nanoTime();
129     assertSame(expected, a.join());
130     assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
131     Thread.interrupted();
132     }
133    
134     {
135     Thread.currentThread().interrupt();
136     long t0 = System.nanoTime();
137     a.quietlyJoin(); // should be no-op
138     assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
139     Thread.interrupted();
140     }
141    
142     assertFalse(a.cancel(false));
143     assertFalse(a.cancel(true));
144     try {
145     assertSame(expected, a.get());
146     } catch (Throwable fail) { threadUnexpectedException(fail); }
147     try {
148     assertSame(expected, a.get(5L, SECONDS));
149     } catch (Throwable fail) { threadUnexpectedException(fail); }
150     }
151    
152     void checkCompletedAbnormally(ForkJoinTask a, Throwable t) {
153     assertTrue(a.isDone());
154     assertFalse(a.isCancelled());
155     assertFalse(a.isCompletedNormally());
156     assertTrue(a.isCompletedAbnormally());
157     assertSame(t.getClass(), a.getException().getClass());
158     assertNull(a.getRawResult());
159     assertFalse(a.cancel(false));
160     assertFalse(a.cancel(true));
161     if (a instanceof BinaryAsyncAction)
162     assertTrue(((BinaryAsyncAction)a).getForkJoinTaskTag() != INITIAL_STATE);
163    
164     try {
165     Thread.currentThread().interrupt();
166     a.join();
167     shouldThrow();
168     } catch (Throwable expected) {
169     assertSame(t.getClass(), expected.getClass());
170     }
171     Thread.interrupted();
172    
173     {
174     long t0 = System.nanoTime();
175     a.quietlyJoin(); // should be no-op
176     assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
177     }
178    
179     try {
180     a.get();
181     shouldThrow();
182     } catch (ExecutionException success) {
183     assertSame(t.getClass(), success.getCause().getClass());
184     } catch (Throwable fail) { threadUnexpectedException(fail); }
185    
186     try {
187     a.get(5L, SECONDS);
188     shouldThrow();
189     } catch (ExecutionException success) {
190     assertSame(t.getClass(), success.getCause().getClass());
191     } catch (Throwable fail) { threadUnexpectedException(fail); }
192     }
193    
194     public static final class FJException extends RuntimeException {
195     FJException() { super(); }
196     }
197    
198     abstract static class BinaryAsyncAction extends ForkJoinTask<Void> {
199    
200     private BinaryAsyncAction parent;
201    
202     private BinaryAsyncAction sibling;
203    
204     protected BinaryAsyncAction() {
205     setForkJoinTaskTag(INITIAL_STATE);
206     }
207    
208     public final Void getRawResult() { return null; }
209     protected final void setRawResult(Void mustBeNull) { }
210    
211     public final void linkSubtasks(BinaryAsyncAction x, BinaryAsyncAction y) {
212     x.parent = y.parent = this;
213     x.sibling = y;
214     y.sibling = x;
215     }
216    
217     protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) {
218 jsr166 1.2 if (this.getForkJoinTaskTag() != COMPLETE_STATE ||
219     x.getForkJoinTaskTag() != COMPLETE_STATE ||
220 dl 1.1 y.getForkJoinTaskTag() != COMPLETE_STATE) {
221     completeThisExceptionally(new FJException());
222     }
223     }
224    
225     protected boolean onException() {
226     return true;
227     }
228    
229     public void linkAndForkSubtasks(BinaryAsyncAction x, BinaryAsyncAction y) {
230     linkSubtasks(x, y);
231     y.fork();
232     x.fork();
233     }
234    
235     private void completeThis() {
236     setForkJoinTaskTag(COMPLETE_STATE);
237     super.complete(null);
238     }
239    
240     private void completeThisExceptionally(Throwable ex) {
241     setForkJoinTaskTag(EXCEPTION_STATE);
242     super.completeExceptionally(ex);
243     }
244    
245     public final void complete() {
246     BinaryAsyncAction a = this;
247     for (;;) {
248     BinaryAsyncAction s = a.sibling;
249     BinaryAsyncAction p = a.parent;
250     a.sibling = null;
251     a.parent = null;
252     a.completeThis();
253 jsr166 1.2 if (p == null ||
254 dl 1.1 p.compareAndSetForkJoinTaskTag(INITIAL_STATE, COMPLETE_STATE))
255     break;
256     try {
257     p.onComplete(a, s);
258     } catch (Throwable rex) {
259     p.completeExceptionally(rex);
260     return;
261     }
262     a = p;
263     }
264     }
265    
266     public final void completeExceptionally(Throwable ex) {
267     BinaryAsyncAction a = this;
268     while (!a.isCompletedAbnormally()) {
269     a.completeThisExceptionally(ex);
270     BinaryAsyncAction s = a.sibling;
271     if (s != null)
272     s.cancel(false);
273     if (!a.onException() || (a = a.parent) == null)
274     break;
275     }
276     }
277    
278     public final BinaryAsyncAction getParent() {
279     return parent;
280     }
281    
282     public BinaryAsyncAction getSibling() {
283     return sibling;
284     }
285    
286     public void reinitialize() {
287     parent = sibling = null;
288     super.reinitialize();
289     }
290    
291     }
292    
293 jsr166 1.12 final class AsyncFib extends BinaryAsyncAction {
294 dl 1.1 int number;
295 jsr166 1.12 int expectedResult;
296     public AsyncFib(int number) {
297     this.number = number;
298     this.expectedResult = fib[number];
299 dl 1.1 }
300    
301     public final boolean exec() {
302     try {
303     AsyncFib f = this;
304     int n = f.number;
305     if (n > 1) {
306     while (n > 1) {
307     AsyncFib p = f;
308     AsyncFib r = new AsyncFib(n - 2);
309     f = new AsyncFib(--n);
310     p.linkSubtasks(r, f);
311     r.fork();
312     }
313     f.number = n;
314     }
315     f.complete();
316     }
317 jsr166 1.2 catch (Throwable ex) {
318 dl 1.1 compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE);
319     }
320     return false;
321     }
322    
323     protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) {
324     number = ((AsyncFib)x).number + ((AsyncFib)y).number;
325     super.onComplete(x, y);
326     }
327 jsr166 1.12
328     public void checkCompletedNormally() {
329     assertEquals(expectedResult, number);
330     ForkJoinTask8Test.this.checkCompletedNormally(this);
331     }
332 dl 1.1 }
333    
334     static final class FailingAsyncFib extends BinaryAsyncAction {
335     int number;
336     public FailingAsyncFib(int n) {
337     this.number = n;
338     }
339    
340     public final boolean exec() {
341     FailingAsyncFib f = this;
342     int n = f.number;
343     if (n > 1) {
344     while (n > 1) {
345     FailingAsyncFib p = f;
346     FailingAsyncFib r = new FailingAsyncFib(n - 2);
347     f = new FailingAsyncFib(--n);
348     p.linkSubtasks(r, f);
349     r.fork();
350     }
351     f.number = n;
352     }
353     f.complete();
354     return false;
355     }
356    
357     protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) {
358     completeExceptionally(new FJException());
359     }
360     }
361    
362     /**
363     * invoke returns when task completes normally.
364     * isCompletedAbnormally and isCancelled return false for normally
365     * completed tasks; getRawResult returns null.
366     */
367     public void testInvoke() {
368 jsr166 1.12 testInvoke(mainPool());
369     }
370     public void testInvoke_Singleton() {
371     testInvoke(singletonPool());
372     }
373     public void testInvoke(ForkJoinPool pool) {
374 dl 1.1 RecursiveAction a = new CheckedRecursiveAction() {
375     protected void realCompute() {
376     AsyncFib f = new AsyncFib(8);
377     assertNull(f.invoke());
378 jsr166 1.12 f.checkCompletedNormally();
379 dl 1.1 }};
380 jsr166 1.12 testInvokeOnPool(pool, a);
381 dl 1.1 }
382    
383     /**
384     * quietlyInvoke task returns when task completes normally.
385     * isCompletedAbnormally and isCancelled return false for normally
386     * completed tasks
387     */
388     public void testQuietlyInvoke() {
389 jsr166 1.12 testQuietlyInvoke(mainPool());
390     }
391     public void testQuietlyInvoke_Singleton() {
392     testQuietlyInvoke(singletonPool());
393     }
394     public void testQuietlyInvoke(ForkJoinPool pool) {
395 dl 1.1 RecursiveAction a = new CheckedRecursiveAction() {
396     protected void realCompute() {
397     AsyncFib f = new AsyncFib(8);
398     f.quietlyInvoke();
399 jsr166 1.12 f.checkCompletedNormally();
400 dl 1.1 }};
401 jsr166 1.12 testInvokeOnPool(pool, a);
402 dl 1.1 }
403    
404     /**
405     * join of a forked task returns when task completes
406     */
407     public void testForkJoin() {
408 jsr166 1.12 testForkJoin(mainPool());
409     }
410     public void testForkJoin_Singleton() {
411     testForkJoin(singletonPool());
412     }
413     public void testForkJoin(ForkJoinPool pool) {
414 dl 1.1 RecursiveAction a = new CheckedRecursiveAction() {
415     protected void realCompute() {
416     AsyncFib f = new AsyncFib(8);
417     assertSame(f, f.fork());
418     assertNull(f.join());
419 jsr166 1.12 f.checkCompletedNormally();
420 dl 1.1 }};
421 jsr166 1.12 testInvokeOnPool(pool, a);
422 dl 1.1 }
423    
424     /**
425     * get of a forked task returns when task completes
426     */
427     public void testForkGet() {
428 jsr166 1.12 testForkGet(mainPool());
429     }
430     public void testForkGet_Singleton() {
431     testForkGet(singletonPool());
432     }
433     public void testForkGet(ForkJoinPool pool) {
434 dl 1.1 RecursiveAction a = new CheckedRecursiveAction() {
435     protected void realCompute() throws Exception {
436     AsyncFib f = new AsyncFib(8);
437     assertSame(f, f.fork());
438     assertNull(f.get());
439 jsr166 1.12 f.checkCompletedNormally();
440 dl 1.1 }};
441 jsr166 1.12 testInvokeOnPool(pool, a);
442 dl 1.1 }
443    
444     /**
445     * timed get of a forked task returns when task completes
446     */
447     public void testForkTimedGet() {
448 jsr166 1.12 testForkTimedGet(mainPool());
449     }
450     public void testForkTimedGet_Singleton() {
451     testForkTimedGet(singletonPool());
452     }
453     public void testForkTimedGet(ForkJoinPool pool) {
454 dl 1.1 RecursiveAction a = new CheckedRecursiveAction() {
455     protected void realCompute() throws Exception {
456     AsyncFib f = new AsyncFib(8);
457     assertSame(f, f.fork());
458     assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
459 jsr166 1.12 f.checkCompletedNormally();
460 dl 1.1 }};
461 jsr166 1.12 testInvokeOnPool(pool, a);
462 dl 1.1 }
463    
464     /**
465 jsr166 1.12 * timed get with null time unit throws NullPointerException
466 dl 1.1 */
467 jsr166 1.12 public void testForkTimedGetNullTimeUnit() {
468     testForkTimedGetNullTimeUnit(mainPool());
469     }
470     public void testForkTimedGetNullTimeUnit_Singleton() {
471     testForkTimedGet(singletonPool());
472     }
473     public void testForkTimedGetNullTimeUnit(ForkJoinPool pool) {
474 dl 1.1 RecursiveAction a = new CheckedRecursiveAction() {
475     protected void realCompute() throws Exception {
476     AsyncFib f = new AsyncFib(8);
477     assertSame(f, f.fork());
478     try {
479     f.get(5L, null);
480     shouldThrow();
481     } catch (NullPointerException success) {}
482     }};
483 jsr166 1.12 testInvokeOnPool(pool, a);
484 dl 1.1 }
485    
486     /**
487     * quietlyJoin of a forked task returns when task completes
488     */
489     public void testForkQuietlyJoin() {
490 jsr166 1.12 testForkQuietlyJoin(mainPool());
491     }
492     public void testForkQuietlyJoin_Singleton() {
493     testForkQuietlyJoin(singletonPool());
494     }
495     public void testForkQuietlyJoin(ForkJoinPool pool) {
496 dl 1.1 RecursiveAction a = new CheckedRecursiveAction() {
497     protected void realCompute() {
498     AsyncFib f = new AsyncFib(8);
499     assertSame(f, f.fork());
500     f.quietlyJoin();
501 jsr166 1.12 f.checkCompletedNormally();
502 dl 1.1 }};
503 jsr166 1.12 testInvokeOnPool(pool, a);
504 dl 1.1 }
505    
506     /**
507     * helpQuiesce returns when tasks are complete.
508     * getQueuedTaskCount returns 0 when quiescent
509     */
510     public void testForkHelpQuiesce() {
511 jsr166 1.12 testForkHelpQuiesce(mainPool());
512     }
513     public void testForkHelpQuiesce_Singleton() {
514     testForkHelpQuiesce(singletonPool());
515     }
516     public void testForkHelpQuiesce(ForkJoinPool pool) {
517 dl 1.1 RecursiveAction a = new CheckedRecursiveAction() {
518     protected void realCompute() {
519     AsyncFib f = new AsyncFib(8);
520     assertSame(f, f.fork());
521     helpQuiesce();
522     assertEquals(0, getQueuedTaskCount());
523 jsr166 1.12 f.checkCompletedNormally();
524 dl 1.1 }};
525 jsr166 1.12 testInvokeOnPool(pool, a);
526 dl 1.1 }
527    
528     /**
529     * invoke task throws exception when task completes abnormally
530     */
531     public void testAbnormalInvoke() {
532 jsr166 1.12 testAbnormalInvoke(mainPool());
533     }
534     public void testAbnormalInvoke_Singleton() {
535     testAbnormalInvoke(singletonPool());
536     }
537     public void testAbnormalInvoke(ForkJoinPool pool) {
538 dl 1.1 RecursiveAction a = new CheckedRecursiveAction() {
539     protected void realCompute() {
540     FailingAsyncFib f = new FailingAsyncFib(8);
541     try {
542     f.invoke();
543     shouldThrow();
544     } catch (FJException success) {
545     checkCompletedAbnormally(f, success);
546     }
547     }};
548 jsr166 1.12 testInvokeOnPool(pool, a);
549 dl 1.1 }
550    
551     /**
552     * quietlyInvoke task returns when task completes abnormally
553     */
554     public void testAbnormalQuietlyInvoke() {
555 jsr166 1.12 testAbnormalQuietlyInvoke(mainPool());
556     }
557     public void testAbnormalQuietlyInvoke_Singleton() {
558     testAbnormalQuietlyInvoke(singletonPool());
559     }
560     public void testAbnormalQuietlyInvoke(ForkJoinPool pool) {
561 dl 1.1 RecursiveAction a = new CheckedRecursiveAction() {
562     protected void realCompute() {
563     FailingAsyncFib f = new FailingAsyncFib(8);
564     f.quietlyInvoke();
565     assertTrue(f.getException() instanceof FJException);
566     checkCompletedAbnormally(f, f.getException());
567     }};
568 jsr166 1.12 testInvokeOnPool(pool, a);
569 dl 1.1 }
570    
571     /**
572     * join of a forked task throws exception when task completes abnormally
573     */
574     public void testAbnormalForkJoin() {
575 jsr166 1.12 testAbnormalForkJoin(mainPool());
576     }
577     public void testAbnormalForkJoin_Singleton() {
578     testAbnormalForkJoin(singletonPool());
579     }
580     public void testAbnormalForkJoin(ForkJoinPool pool) {
581 dl 1.1 RecursiveAction a = new CheckedRecursiveAction() {
582     protected void realCompute() {
583     FailingAsyncFib f = new FailingAsyncFib(8);
584     assertSame(f, f.fork());
585     try {
586     f.join();
587     shouldThrow();
588     } catch (FJException success) {
589     checkCompletedAbnormally(f, success);
590     }
591     }};
592 jsr166 1.12 testInvokeOnPool(pool, a);
593 dl 1.1 }
594    
595     /**
596     * get of a forked task throws exception when task completes abnormally
597     */
598     public void testAbnormalForkGet() {
599 jsr166 1.12 testAbnormalForkGet(mainPool());
600     }
601     public void testAbnormalForkGet_Singleton() {
602     testAbnormalForkJoin(singletonPool());
603     }
604     public void testAbnormalForkGet(ForkJoinPool pool) {
605 dl 1.1 RecursiveAction a = new CheckedRecursiveAction() {
606     protected void realCompute() throws Exception {
607     FailingAsyncFib f = new FailingAsyncFib(8);
608     assertSame(f, f.fork());
609     try {
610     f.get();
611     shouldThrow();
612     } catch (ExecutionException success) {
613     Throwable cause = success.getCause();
614     assertTrue(cause instanceof FJException);
615     checkCompletedAbnormally(f, cause);
616     }
617     }};
618 jsr166 1.12 testInvokeOnPool(pool, a);
619 dl 1.1 }
620    
621     /**
622     * timed get of a forked task throws exception when task completes abnormally
623     */
624     public void testAbnormalForkTimedGet() {
625 jsr166 1.12 testAbnormalForkTimedGet(mainPool());
626     }
627     public void testAbnormalForkTimedGet_Singleton() {
628     testAbnormalForkTimedGet(singletonPool());
629     }
630     public void testAbnormalForkTimedGet(ForkJoinPool pool) {
631 dl 1.1 RecursiveAction a = new CheckedRecursiveAction() {
632     protected void realCompute() throws Exception {
633     FailingAsyncFib f = new FailingAsyncFib(8);
634     assertSame(f, f.fork());
635     try {
636     f.get(LONG_DELAY_MS, MILLISECONDS);
637     shouldThrow();
638     } catch (ExecutionException success) {
639     Throwable cause = success.getCause();
640     assertTrue(cause instanceof FJException);
641     checkCompletedAbnormally(f, cause);
642     }
643     }};
644 jsr166 1.12 testInvokeOnPool(pool, a);
645 dl 1.1 }
646    
647     /**
648     * quietlyJoin of a forked task returns when task completes abnormally
649     */
650     public void testAbnormalForkQuietlyJoin() {
651 jsr166 1.12 testAbnormalForkQuietlyJoin(mainPool());
652     }
653     public void testAbnormalForkQuietlyJoin_Singleton() {
654     testAbnormalForkQuietlyJoin(singletonPool());
655     }
656     public void testAbnormalForkQuietlyJoin(ForkJoinPool pool) {
657 dl 1.1 RecursiveAction a = new CheckedRecursiveAction() {
658     protected void realCompute() {
659     FailingAsyncFib f = new FailingAsyncFib(8);
660     assertSame(f, f.fork());
661     f.quietlyJoin();
662     assertTrue(f.getException() instanceof FJException);
663     checkCompletedAbnormally(f, f.getException());
664     }};
665 jsr166 1.12 testInvokeOnPool(pool, a);
666 dl 1.1 }
667    
668     /**
669     * getPool of executing task returns its pool
670     */
671     public void testGetPool() {
672 jsr166 1.12 testGetPool(mainPool());
673     }
674     public void testGetPool_Singleton() {
675     testGetPool(singletonPool());
676     }
677     public void testGetPool(ForkJoinPool pool) {
678 dl 1.1 RecursiveAction a = new CheckedRecursiveAction() {
679     protected void realCompute() {
680 jsr166 1.12 assertSame(pool, getPool());
681 dl 1.1 }};
682 jsr166 1.12 testInvokeOnPool(pool, a);
683 dl 1.1 }
684    
685     /**
686     * getPool of non-FJ task returns null
687     */
688     public void testGetPool2() {
689     RecursiveAction a = new CheckedRecursiveAction() {
690     protected void realCompute() {
691     assertNull(getPool());
692     }};
693     assertNull(a.invoke());
694     }
695    
696     /**
697     * inForkJoinPool of executing task returns true
698     */
699     public void testInForkJoinPool() {
700 jsr166 1.12 testInForkJoinPool(mainPool());
701     }
702     public void testInForkJoinPool_Singleton() {
703     testInForkJoinPool(singletonPool());
704     }
705     public void testInForkJoinPool(ForkJoinPool pool) {
706 dl 1.1 RecursiveAction a = new CheckedRecursiveAction() {
707     protected void realCompute() {
708     assertTrue(inForkJoinPool());
709     }};
710 jsr166 1.12 testInvokeOnPool(pool, a);
711 dl 1.1 }
712    
713     /**
714     * inForkJoinPool of non-FJ task returns false
715     */
716     public void testInForkJoinPool2() {
717     RecursiveAction a = new CheckedRecursiveAction() {
718     protected void realCompute() {
719     assertFalse(inForkJoinPool());
720     }};
721     assertNull(a.invoke());
722     }
723    
724     /**
725     * setRawResult(null) succeeds
726     */
727     public void testSetRawResult() {
728     RecursiveAction a = new CheckedRecursiveAction() {
729     protected void realCompute() {
730     setRawResult(null);
731     assertNull(getRawResult());
732     }};
733     assertNull(a.invoke());
734     }
735    
736     /**
737     * invoke task throws exception after invoking completeExceptionally
738     */
739     public void testCompleteExceptionally() {
740 jsr166 1.12 testCompleteExceptionally(mainPool());
741     }
742     public void testCompleteExceptionally_Singleton() {
743     testCompleteExceptionally(singletonPool());
744     }
745     public void testCompleteExceptionally(ForkJoinPool pool) {
746 dl 1.1 RecursiveAction a = new CheckedRecursiveAction() {
747     protected void realCompute() {
748     AsyncFib f = new AsyncFib(8);
749     f.completeExceptionally(new FJException());
750     try {
751     f.invoke();
752     shouldThrow();
753     } catch (FJException success) {
754     checkCompletedAbnormally(f, success);
755     }
756     }};
757 jsr166 1.12 testInvokeOnPool(pool, a);
758 dl 1.1 }
759    
760     /**
761 jsr166 1.12 * invokeAll(tasks) with 1 argument invokes task
762 dl 1.1 */
763 jsr166 1.12 public void testInvokeAll1() {
764     testInvokeAll1(mainPool());
765     }
766     public void testInvokeAll1_Singleton() {
767     testInvokeAll1(singletonPool());
768     }
769     public void testInvokeAll1(ForkJoinPool pool) {
770 dl 1.1 RecursiveAction a = new CheckedRecursiveAction() {
771     protected void realCompute() {
772     AsyncFib f = new AsyncFib(8);
773 jsr166 1.12 invokeAll(f);
774     f.checkCompletedNormally();
775 dl 1.1 }};
776 jsr166 1.12 testInvokeOnPool(pool, a);
777 dl 1.1 }
778    
779     /**
780 jsr166 1.12 * invokeAll(t1, t2) invokes all task arguments
781 dl 1.1 */
782 jsr166 1.12 public void testInvokeAll2() {
783     testInvokeAll2(mainPool());
784     }
785     public void testInvokeAll2_Singleton() {
786     testInvokeAll2(singletonPool());
787     }
788     public void testInvokeAll2(ForkJoinPool pool) {
789 dl 1.1 RecursiveAction a = new CheckedRecursiveAction() {
790     protected void realCompute() {
791 jsr166 1.12 AsyncFib[] tasks = {
792     new AsyncFib(8),
793     new AsyncFib(9),
794     };
795     invokeAll(tasks[0], tasks[1]);
796     for (AsyncFib task : tasks) assertTrue(task.isDone());
797     for (AsyncFib task : tasks) task.checkCompletedNormally();
798 dl 1.1 }};
799 jsr166 1.12 testInvokeOnPool(pool, a);
800 dl 1.1 }
801    
802     /**
803     * invokeAll(tasks) with > 2 argument invokes tasks
804     */
805     public void testInvokeAll3() {
806 jsr166 1.12 testInvokeAll3(mainPool());
807     }
808     public void testInvokeAll3_Singleton() {
809     testInvokeAll3(singletonPool());
810     }
811     public void testInvokeAll3(ForkJoinPool pool) {
812 dl 1.1 RecursiveAction a = new CheckedRecursiveAction() {
813     protected void realCompute() {
814 jsr166 1.12 AsyncFib[] tasks = {
815     new AsyncFib(8),
816     new AsyncFib(9),
817     new AsyncFib(7),
818     };
819     invokeAll(tasks[0], tasks[1], tasks[2]);
820     for (AsyncFib task : tasks) assertTrue(task.isDone());
821     for (AsyncFib task : tasks) task.checkCompletedNormally();
822 dl 1.1 }};
823 jsr166 1.12 testInvokeOnPool(pool, a);
824 dl 1.1 }
825    
826     /**
827     * invokeAll(collection) invokes all tasks in the collection
828     */
829     public void testInvokeAllCollection() {
830 jsr166 1.12 testInvokeAllCollection(mainPool());
831     }
832     public void testInvokeAllCollection_Singleton() {
833     testInvokeAllCollection(singletonPool());
834     }
835     public void testInvokeAllCollection(ForkJoinPool pool) {
836 dl 1.1 RecursiveAction a = new CheckedRecursiveAction() {
837     protected void realCompute() {
838 jsr166 1.12 AsyncFib[] tasks = {
839     new AsyncFib(8),
840     new AsyncFib(9),
841     new AsyncFib(7),
842     };
843     invokeAll(Arrays.asList(tasks));
844     for (AsyncFib task : tasks) assertTrue(task.isDone());
845     for (AsyncFib task : tasks) task.checkCompletedNormally();
846 dl 1.1 }};
847 jsr166 1.12 testInvokeOnPool(pool, a);
848 dl 1.1 }
849    
850     /**
851 jsr166 1.10 * invokeAll(tasks) with any null task throws NullPointerException
852 dl 1.1 */
853 jsr166 1.10 public void testInvokeAllNullTask() {
854 jsr166 1.12 testInvokeAllNullTask(mainPool());
855     }
856     public void testInvokeAllNullTask_Singleton() {
857     testInvokeAllNullTask(singletonPool());
858     }
859     public void testInvokeAllNullTask(ForkJoinPool pool) {
860 dl 1.1 RecursiveAction a = new CheckedRecursiveAction() {
861     protected void realCompute() {
862 jsr166 1.10 AsyncFib nul = null;
863 jsr166 1.9 Runnable[] throwingActions = {
864 jsr166 1.10 () -> invokeAll(nul),
865     () -> invokeAll(nul, nul),
866 jsr166 1.12 () -> invokeAll(new AsyncFib(8), new AsyncFib(9), nul),
867     () -> invokeAll(new AsyncFib(8), nul, new AsyncFib(9)),
868     () -> invokeAll(nul, new AsyncFib(8), new AsyncFib(9)),
869 jsr166 1.9 };
870     assertThrows(NullPointerException.class, throwingActions);
871 dl 1.1 }};
872 jsr166 1.12 testInvokeOnPool(pool, a);
873 dl 1.1 }
874    
875     /**
876     * invokeAll(t1, t2) throw exception if any task does
877     */
878     public void testAbnormalInvokeAll2() {
879 jsr166 1.12 testAbnormalInvokeAll2(mainPool());
880     }
881     public void testAbnormalInvokeAll2_Singleton() {
882     testAbnormalInvokeAll2(singletonPool());
883     }
884     public void testAbnormalInvokeAll2(ForkJoinPool pool) {
885 dl 1.1 RecursiveAction a = new CheckedRecursiveAction() {
886     protected void realCompute() {
887     AsyncFib f = new AsyncFib(8);
888     FailingAsyncFib g = new FailingAsyncFib(9);
889     try {
890     invokeAll(f, g);
891     shouldThrow();
892     } catch (FJException success) {
893     checkCompletedAbnormally(g, success);
894     }
895     }};
896 jsr166 1.12 testInvokeOnPool(pool, a);
897 dl 1.1 }
898    
899     /**
900     * invokeAll(tasks) with 1 argument throws exception if task does
901     */
902     public void testAbnormalInvokeAll1() {
903 jsr166 1.12 testAbnormalInvokeAll1(mainPool());
904     }
905     public void testAbnormalInvokeAll1_Singleton() {
906     testAbnormalInvokeAll1(singletonPool());
907     }
908     public void testAbnormalInvokeAll1(ForkJoinPool pool) {
909 dl 1.1 RecursiveAction a = new CheckedRecursiveAction() {
910     protected void realCompute() {
911     FailingAsyncFib g = new FailingAsyncFib(9);
912     try {
913     invokeAll(g);
914     shouldThrow();
915     } catch (FJException success) {
916     checkCompletedAbnormally(g, success);
917     }
918     }};
919 jsr166 1.12 testInvokeOnPool(pool, a);
920 dl 1.1 }
921    
922     /**
923     * invokeAll(tasks) with > 2 argument throws exception if any task does
924     */
925     public void testAbnormalInvokeAll3() {
926 jsr166 1.12 testAbnormalInvokeAll3(mainPool());
927     }
928     public void testAbnormalInvokeAll3_Singleton() {
929     testAbnormalInvokeAll3(singletonPool());
930     }
931     public void testAbnormalInvokeAll3(ForkJoinPool pool) {
932 dl 1.1 RecursiveAction a = new CheckedRecursiveAction() {
933     protected void realCompute() {
934     AsyncFib f = new AsyncFib(8);
935     FailingAsyncFib g = new FailingAsyncFib(9);
936     AsyncFib h = new AsyncFib(7);
937     try {
938     invokeAll(f, g, h);
939     shouldThrow();
940     } catch (FJException success) {
941     checkCompletedAbnormally(g, success);
942     }
943     }};
944 jsr166 1.12 testInvokeOnPool(pool, a);
945 dl 1.1 }
946    
947     /**
948 jsr166 1.4 * invokeAll(collection) throws exception if any task does
949 dl 1.1 */
950     public void testAbnormalInvokeAllCollection() {
951 jsr166 1.12 testAbnormalInvokeAllCollection(mainPool());
952     }
953     public void testAbnormalInvokeAllCollection_Singleton() {
954     testAbnormalInvokeAllCollection(singletonPool());
955     }
956     public void testAbnormalInvokeAllCollection(ForkJoinPool pool) {
957 dl 1.1 RecursiveAction a = new CheckedRecursiveAction() {
958     protected void realCompute() {
959     FailingAsyncFib f = new FailingAsyncFib(8);
960     AsyncFib g = new AsyncFib(9);
961     AsyncFib h = new AsyncFib(7);
962 jsr166 1.12 ForkJoinTask[] tasks = { f, g, h };
963 dl 1.1 try {
964 jsr166 1.12 invokeAll(Arrays.asList(tasks));
965 dl 1.1 shouldThrow();
966     } catch (FJException success) {
967     checkCompletedAbnormally(f, success);
968     }
969     }};
970 jsr166 1.12 testInvokeOnPool(pool, a);
971 dl 1.1 }
972    
973     /**
974     * tryUnfork returns true for most recent unexecuted task,
975     * and suppresses execution
976     */
977     public void testTryUnfork() {
978     RecursiveAction a = new CheckedRecursiveAction() {
979     protected void realCompute() {
980     AsyncFib g = new AsyncFib(9);
981     assertSame(g, g.fork());
982     AsyncFib f = new AsyncFib(8);
983     assertSame(f, f.fork());
984     assertTrue(f.tryUnfork());
985     helpQuiesce();
986     checkNotDone(f);
987 jsr166 1.12 g.checkCompletedNormally();
988 dl 1.1 }};
989     testInvokeOnPool(singletonPool(), a);
990     }
991    
992     /**
993     * getSurplusQueuedTaskCount returns > 0 when
994     * there are more tasks than threads
995     */
996     public void testGetSurplusQueuedTaskCount() {
997     RecursiveAction a = new CheckedRecursiveAction() {
998     protected void realCompute() {
999     AsyncFib h = new AsyncFib(7);
1000     assertSame(h, h.fork());
1001     AsyncFib g = new AsyncFib(9);
1002     assertSame(g, g.fork());
1003     AsyncFib f = new AsyncFib(8);
1004     assertSame(f, f.fork());
1005     assertTrue(getSurplusQueuedTaskCount() > 0);
1006     helpQuiesce();
1007     assertEquals(0, getSurplusQueuedTaskCount());
1008 jsr166 1.12 f.checkCompletedNormally();
1009     g.checkCompletedNormally();
1010     h.checkCompletedNormally();
1011 dl 1.1 }};
1012     testInvokeOnPool(singletonPool(), a);
1013     }
1014    
1015     /**
1016     * peekNextLocalTask returns most recent unexecuted task.
1017     */
1018     public void testPeekNextLocalTask() {
1019     RecursiveAction a = new CheckedRecursiveAction() {
1020     protected void realCompute() {
1021     AsyncFib g = new AsyncFib(9);
1022     assertSame(g, g.fork());
1023     AsyncFib f = new AsyncFib(8);
1024     assertSame(f, f.fork());
1025     assertSame(f, peekNextLocalTask());
1026     assertNull(f.join());
1027 jsr166 1.12 f.checkCompletedNormally();
1028 dl 1.1 helpQuiesce();
1029 jsr166 1.12 g.checkCompletedNormally();
1030 dl 1.1 }};
1031     testInvokeOnPool(singletonPool(), a);
1032     }
1033    
1034     /**
1035     * pollNextLocalTask returns most recent unexecuted task without
1036     * executing it
1037     */
1038     public void testPollNextLocalTask() {
1039     RecursiveAction a = new CheckedRecursiveAction() {
1040     protected void realCompute() {
1041     AsyncFib g = new AsyncFib(9);
1042     assertSame(g, g.fork());
1043     AsyncFib f = new AsyncFib(8);
1044     assertSame(f, f.fork());
1045     assertSame(f, pollNextLocalTask());
1046     helpQuiesce();
1047     checkNotDone(f);
1048 jsr166 1.12 g.checkCompletedNormally();
1049 dl 1.1 }};
1050     testInvokeOnPool(singletonPool(), a);
1051     }
1052    
1053     /**
1054     * pollTask returns an unexecuted task without executing it
1055     */
1056     public void testPollTask() {
1057     RecursiveAction a = new CheckedRecursiveAction() {
1058     protected void realCompute() {
1059     AsyncFib g = new AsyncFib(9);
1060     assertSame(g, g.fork());
1061     AsyncFib f = new AsyncFib(8);
1062     assertSame(f, f.fork());
1063     assertSame(f, pollTask());
1064     helpQuiesce();
1065     checkNotDone(f);
1066 jsr166 1.12 g.checkCompletedNormally();
1067 dl 1.1 }};
1068     testInvokeOnPool(singletonPool(), a);
1069     }
1070    
1071     /**
1072     * peekNextLocalTask returns least recent unexecuted task in async mode
1073     */
1074     public void testPeekNextLocalTaskAsync() {
1075     RecursiveAction a = new CheckedRecursiveAction() {
1076     protected void realCompute() {
1077     AsyncFib g = new AsyncFib(9);
1078     assertSame(g, g.fork());
1079     AsyncFib f = new AsyncFib(8);
1080     assertSame(f, f.fork());
1081     assertSame(g, peekNextLocalTask());
1082     assertNull(f.join());
1083     helpQuiesce();
1084 jsr166 1.12 f.checkCompletedNormally();
1085     g.checkCompletedNormally();
1086 dl 1.1 }};
1087     testInvokeOnPool(asyncSingletonPool(), a);
1088     }
1089    
1090     /**
1091     * pollNextLocalTask returns least recent unexecuted task without
1092     * executing it, in async mode
1093     */
1094     public void testPollNextLocalTaskAsync() {
1095     RecursiveAction a = new CheckedRecursiveAction() {
1096     protected void realCompute() {
1097     AsyncFib g = new AsyncFib(9);
1098     assertSame(g, g.fork());
1099     AsyncFib f = new AsyncFib(8);
1100     assertSame(f, f.fork());
1101     assertSame(g, pollNextLocalTask());
1102     helpQuiesce();
1103 jsr166 1.12 f.checkCompletedNormally();
1104 dl 1.1 checkNotDone(g);
1105     }};
1106     testInvokeOnPool(asyncSingletonPool(), a);
1107     }
1108    
1109     /**
1110     * pollTask returns an unexecuted task without executing it, in
1111     * async mode
1112     */
1113     public void testPollTaskAsync() {
1114     RecursiveAction a = new CheckedRecursiveAction() {
1115     protected void realCompute() {
1116     AsyncFib g = new AsyncFib(9);
1117     assertSame(g, g.fork());
1118     AsyncFib f = new AsyncFib(8);
1119     assertSame(f, f.fork());
1120     assertSame(g, pollTask());
1121     helpQuiesce();
1122 jsr166 1.12 f.checkCompletedNormally();
1123 dl 1.1 checkNotDone(g);
1124     }};
1125     testInvokeOnPool(asyncSingletonPool(), a);
1126     }
1127    
1128     /**
1129     * ForkJoinTask.quietlyComplete returns when task completes
1130     * normally without setting a value. The most recent value
1131     * established by setRawResult(V) (or null by default) is returned
1132     * from invoke.
1133     */
1134     public void testQuietlyComplete() {
1135     RecursiveAction a = new CheckedRecursiveAction() {
1136     protected void realCompute() {
1137     AsyncFib f = new AsyncFib(8);
1138     f.quietlyComplete();
1139     assertEquals(8, f.number);
1140     assertTrue(f.isDone());
1141     assertFalse(f.isCancelled());
1142     assertTrue(f.isCompletedNormally());
1143     assertFalse(f.isCompletedAbnormally());
1144     assertNull(f.getException());
1145     }};
1146     testInvokeOnPool(mainPool(), a);
1147     }
1148    
1149     }