ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveActionTest.java
Revision: 1.26
Committed: Tue Nov 23 07:38:40 2010 UTC (13 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.25: +97 -0 lines
Log Message:
new test testJoinIgnoresInterruptsOutsideForkJoinPool

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