ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveActionTest.java
Revision: 1.21
Committed: Sun Nov 21 19:06:53 2010 UTC (13 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.20: +17 -12 lines
Log Message:
miscellaneous test improvements

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