ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveActionTest.java
Revision: 1.25
Committed: Tue Nov 23 06:33:26 2010 UTC (13 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.24: +7 -7 lines
Log Message:
join/quietlyJoin is permitted to clear interrupt status

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