ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveActionTest.java
Revision: 1.20
Committed: Sun Nov 21 08:35:40 2010 UTC (13 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.19: +6 -0 lines
Log Message:
invokeAll completes all tasks on normal return

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     void checkTaskThrew(RecursiveAction a, Throwable t) {
129     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     checkTaskThrew(f, success);
337     }
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     checkTaskThrew(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     checkTaskThrew(f, success);
369     }
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     checkTaskThrew(f, success.getCause());
387     }
388 jsr166 1.7 }};
389 jsr166 1.14 testInvokeOnPool(mainPool(), a);
390 dl 1.1 }
391    
392 jsr166 1.2 /**
393 dl 1.1 * timed get of a forked task throws exception when task completes abnormally
394     */
395     public void testAbnormalForkTimedGet() {
396 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
397     public void realCompute() throws Exception {
398     FailingFibAction f = new FailingFibAction(8);
399     assertSame(f, f.fork());
400 jsr166 1.7 try {
401     f.get(5L, TimeUnit.SECONDS);
402     shouldThrow();
403 jsr166 1.19 } catch (ExecutionException success) {
404     checkTaskThrew(f, success.getCause());
405     }
406 jsr166 1.7 }};
407 jsr166 1.14 testInvokeOnPool(mainPool(), a);
408 dl 1.1 }
409    
410 jsr166 1.2 /**
411 dl 1.1 * quietlyJoin of a forked task returns when task completes abnormally
412     */
413     public void testAbnormalForkQuietlyJoin() {
414 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
415     public void realCompute() {
416 jsr166 1.7 FailingFibAction f = new FailingFibAction(8);
417 jsr166 1.18 assertSame(f, f.fork());
418 jsr166 1.7 f.quietlyJoin();
419 jsr166 1.18 assertTrue(f.getException() instanceof FJException);
420 jsr166 1.19 checkTaskThrew(f, f.getException());
421 jsr166 1.7 }};
422 jsr166 1.14 testInvokeOnPool(mainPool(), a);
423 dl 1.1 }
424    
425 jsr166 1.2 /**
426 dl 1.1 * invoke task throws exception when task cancelled
427     */
428     public void testCancelledInvoke() {
429 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
430     public void realCompute() {
431     FibAction f = new FibAction(8);
432     assertTrue(f.cancel(true));
433 jsr166 1.7 try {
434     f.invoke();
435     shouldThrow();
436 jsr166 1.19 } catch (CancellationException success) {
437     checkCancelled(f);
438     }
439 jsr166 1.7 }};
440 jsr166 1.14 testInvokeOnPool(mainPool(), a);
441 dl 1.1 }
442    
443 jsr166 1.2 /**
444 dl 1.1 * join of a forked task throws exception when task cancelled
445     */
446     public void testCancelledForkJoin() {
447 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
448     public void realCompute() {
449     FibAction f = new FibAction(8);
450     assertTrue(f.cancel(true));
451     assertSame(f, f.fork());
452 jsr166 1.7 try {
453     f.join();
454     shouldThrow();
455 jsr166 1.19 } catch (CancellationException success) {
456     checkCancelled(f);
457     }
458 jsr166 1.7 }};
459 jsr166 1.14 testInvokeOnPool(mainPool(), a);
460 dl 1.1 }
461    
462 jsr166 1.2 /**
463 dl 1.1 * get of a forked task throws exception when task cancelled
464     */
465     public void testCancelledForkGet() {
466 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
467     public void realCompute() throws Exception {
468     FibAction f = new FibAction(8);
469     assertTrue(f.cancel(true));
470     assertSame(f, f.fork());
471 jsr166 1.7 try {
472     f.get();
473     shouldThrow();
474 jsr166 1.19 } catch (CancellationException success) {
475     checkCancelled(f);
476     }
477 jsr166 1.7 }};
478 jsr166 1.14 testInvokeOnPool(mainPool(), a);
479 dl 1.1 }
480    
481 jsr166 1.2 /**
482 dl 1.1 * timed get of a forked task throws exception when task cancelled
483     */
484     public void testCancelledForkTimedGet() {
485 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
486     public void realCompute() throws Exception {
487     FibAction f = new FibAction(8);
488     assertTrue(f.cancel(true));
489     assertSame(f, f.fork());
490 jsr166 1.7 try {
491 jsr166 1.19 f.get(5L, SECONDS);
492 jsr166 1.7 shouldThrow();
493 jsr166 1.19 } catch (CancellationException success) {
494     checkCancelled(f);
495     }
496 jsr166 1.7 }};
497 jsr166 1.14 testInvokeOnPool(mainPool(), a);
498 dl 1.1 }
499    
500 jsr166 1.2 /**
501 dl 1.1 * quietlyJoin of a forked task returns when task cancelled
502     */
503     public void testCancelledForkQuietlyJoin() {
504 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
505     public void realCompute() {
506 jsr166 1.7 FibAction f = new FibAction(8);
507 jsr166 1.18 assertTrue(f.cancel(true));
508     assertSame(f, f.fork());
509 jsr166 1.7 f.quietlyJoin();
510 jsr166 1.19 checkCancelled(f);
511 jsr166 1.7 }};
512 jsr166 1.14 testInvokeOnPool(mainPool(), a);
513 dl 1.1 }
514    
515     /**
516     * getPool of executing task returns its pool
517     */
518     public void testGetPool() {
519 jsr166 1.14 final ForkJoinPool mainPool = mainPool();
520 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
521     public void realCompute() {
522     assertSame(mainPool, getPool());
523 jsr166 1.7 }};
524 jsr166 1.14 testInvokeOnPool(mainPool, a);
525 dl 1.1 }
526    
527     /**
528     * getPool of non-FJ task returns null
529     */
530     public void testGetPool2() {
531 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
532     public void realCompute() {
533     assertNull(getPool());
534 jsr166 1.7 }};
535 jsr166 1.16 assertNull(a.invoke());
536 dl 1.1 }
537    
538     /**
539     * inForkJoinPool of executing task returns true
540     */
541     public void testInForkJoinPool() {
542 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
543     public void realCompute() {
544     assertTrue(inForkJoinPool());
545 jsr166 1.7 }};
546 jsr166 1.14 testInvokeOnPool(mainPool(), a);
547 dl 1.1 }
548    
549     /**
550     * inForkJoinPool of non-FJ task returns false
551     */
552     public void testInForkJoinPool2() {
553 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
554     public void realCompute() {
555     assertFalse(inForkJoinPool());
556 jsr166 1.7 }};
557 jsr166 1.16 assertNull(a.invoke());
558 dl 1.1 }
559    
560     /**
561     * getPool of current thread in pool returns its pool
562     */
563     public void testWorkerGetPool() {
564 jsr166 1.14 final ForkJoinPool mainPool = mainPool();
565 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
566     public void realCompute() {
567 jsr166 1.7 ForkJoinWorkerThread w =
568 jsr166 1.14 (ForkJoinWorkerThread) Thread.currentThread();
569 jsr166 1.18 assertSame(mainPool, w.getPool());
570 jsr166 1.7 }};
571 jsr166 1.14 testInvokeOnPool(mainPool, a);
572 dl 1.1 }
573    
574     /**
575     * getPoolIndex of current thread in pool returns 0 <= value < poolSize
576     */
577     public void testWorkerGetPoolIndex() {
578 jsr166 1.14 final ForkJoinPool mainPool = mainPool();
579 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
580     public void realCompute() {
581 jsr166 1.7 ForkJoinWorkerThread w =
582     (ForkJoinWorkerThread)(Thread.currentThread());
583 jsr166 1.19 assertTrue(w.getPoolIndex() >= 0);
584     assertTrue(w.getPoolIndex() < mainPool.getPoolSize());
585 jsr166 1.7 }};
586 jsr166 1.14 testInvokeOnPool(mainPool, a);
587 dl 1.1 }
588    
589    
590     /**
591     * setRawResult(null) succeeds
592     */
593     public void testSetRawResult() {
594 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
595     public void realCompute() {
596 jsr166 1.7 setRawResult(null);
597     }};
598 jsr166 1.16 assertNull(a.invoke());
599 dl 1.1 }
600    
601 jsr166 1.2 /**
602 dl 1.1 * A reinitialized task may be re-invoked
603     */
604     public void testReinitialize() {
605 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
606     public void realCompute() {
607 jsr166 1.7 FibAction f = new FibAction(8);
608 jsr166 1.19 checkNotDone(f);
609    
610     for (int i = 0; i < 3; i++) {
611     assertNull(f.invoke());
612     assertEquals(21, f.result);
613     checkCompletedNormally(f);
614     f.reinitialize();
615     checkNotDone(f);
616     }
617 jsr166 1.7 }};
618 jsr166 1.14 testInvokeOnPool(mainPool(), a);
619 dl 1.1 }
620    
621 jsr166 1.2 /**
622 dl 1.1 * invoke task throws exception after invoking completeExceptionally
623     */
624     public void testCompleteExceptionally() {
625 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
626     public void realCompute() {
627     FibAction f = new FibAction(8);
628     f.completeExceptionally(new FJException());
629 jsr166 1.7 try {
630     f.invoke();
631     shouldThrow();
632 jsr166 1.19 } catch (FJException success) {
633     checkTaskThrew(f, success);
634     }
635 jsr166 1.7 }};
636 jsr166 1.14 testInvokeOnPool(mainPool(), a);
637 dl 1.1 }
638    
639 jsr166 1.2 /**
640 dl 1.1 * invoke task suppresses execution invoking complete
641     */
642     public void testComplete() {
643 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
644     public void realCompute() {
645 jsr166 1.7 FibAction f = new FibAction(8);
646     f.complete(null);
647 jsr166 1.18 assertNull(f.invoke());
648     assertEquals(0, f.result);
649 jsr166 1.19 checkCompletedNormally(f);
650 jsr166 1.7 }};
651 jsr166 1.14 testInvokeOnPool(mainPool(), a);
652 dl 1.1 }
653    
654 jsr166 1.2 /**
655 dl 1.1 * invokeAll(t1, t2) invokes all task arguments
656     */
657     public void testInvokeAll2() {
658 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
659     public void realCompute() {
660 jsr166 1.7 FibAction f = new FibAction(8);
661     FibAction g = new FibAction(9);
662     invokeAll(f, g);
663 jsr166 1.19 checkCompletedNormally(f);
664 jsr166 1.18 assertEquals(21, f.result);
665 jsr166 1.19 checkCompletedNormally(g);
666 jsr166 1.18 assertEquals(34, g.result);
667 jsr166 1.7 }};
668 jsr166 1.14 testInvokeOnPool(mainPool(), a);
669 dl 1.1 }
670    
671 jsr166 1.2 /**
672 dl 1.1 * invokeAll(tasks) with 1 argument invokes task
673     */
674     public void testInvokeAll1() {
675 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
676     public void realCompute() {
677 jsr166 1.7 FibAction f = new FibAction(8);
678     invokeAll(f);
679 jsr166 1.19 checkCompletedNormally(f);
680 jsr166 1.18 assertEquals(21, f.result);
681 jsr166 1.7 }};
682 jsr166 1.14 testInvokeOnPool(mainPool(), a);
683 dl 1.1 }
684    
685 jsr166 1.2 /**
686 dl 1.1 * invokeAll(tasks) with > 2 argument invokes tasks
687     */
688     public void testInvokeAll3() {
689 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
690     public void realCompute() {
691 jsr166 1.7 FibAction f = new FibAction(8);
692     FibAction g = new FibAction(9);
693     FibAction h = new FibAction(7);
694     invokeAll(f, g, h);
695 jsr166 1.20 assertTrue(f.isDone());
696     assertTrue(g.isDone());
697     assertTrue(h.isDone());
698 jsr166 1.19 checkCompletedNormally(f);
699 jsr166 1.18 assertEquals(21, f.result);
700 jsr166 1.19 checkCompletedNormally(g);
701 jsr166 1.18 assertEquals(34, g.result);
702 jsr166 1.19 checkCompletedNormally(g);
703 jsr166 1.18 assertEquals(13, h.result);
704 jsr166 1.7 }};
705 jsr166 1.14 testInvokeOnPool(mainPool(), a);
706 dl 1.1 }
707    
708 jsr166 1.2 /**
709 dl 1.1 * invokeAll(collection) invokes all tasks in the collection
710     */
711     public void testInvokeAllCollection() {
712 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
713     public void realCompute() {
714 jsr166 1.7 FibAction f = new FibAction(8);
715     FibAction g = new FibAction(9);
716     FibAction h = new FibAction(7);
717     HashSet set = new HashSet();
718     set.add(f);
719     set.add(g);
720     set.add(h);
721     invokeAll(set);
722 jsr166 1.20 assertTrue(f.isDone());
723     assertTrue(g.isDone());
724     assertTrue(h.isDone());
725 jsr166 1.19 checkCompletedNormally(f);
726 jsr166 1.18 assertEquals(21, f.result);
727 jsr166 1.19 checkCompletedNormally(g);
728 jsr166 1.18 assertEquals(34, g.result);
729 jsr166 1.19 checkCompletedNormally(g);
730 jsr166 1.18 assertEquals(13, h.result);
731 jsr166 1.7 }};
732 jsr166 1.14 testInvokeOnPool(mainPool(), a);
733 dl 1.1 }
734    
735    
736 jsr166 1.2 /**
737 dl 1.1 * invokeAll(tasks) with any null task throws NPE
738     */
739     public void testInvokeAllNPE() {
740 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
741     public void realCompute() {
742     FibAction f = new FibAction(8);
743     FibAction g = new FibAction(9);
744     FibAction h = null;
745 jsr166 1.7 try {
746     invokeAll(f, g, h);
747     shouldThrow();
748 jsr166 1.18 } catch (NullPointerException success) {}
749 jsr166 1.7 }};
750 jsr166 1.14 testInvokeOnPool(mainPool(), a);
751 dl 1.1 }
752    
753 jsr166 1.2 /**
754 dl 1.1 * invokeAll(t1, t2) throw exception if any task does
755     */
756     public void testAbnormalInvokeAll2() {
757 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
758     public void realCompute() {
759     FibAction f = new FibAction(8);
760     FailingFibAction g = new FailingFibAction(9);
761 jsr166 1.7 try {
762     invokeAll(f, g);
763     shouldThrow();
764 jsr166 1.19 } catch (FJException success) {
765     checkTaskThrew(g, success);
766     }
767 jsr166 1.7 }};
768 jsr166 1.14 testInvokeOnPool(mainPool(), a);
769 dl 1.1 }
770    
771 jsr166 1.2 /**
772 dl 1.1 * invokeAll(tasks) with 1 argument throws exception if task does
773     */
774     public void testAbnormalInvokeAll1() {
775 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
776     public void realCompute() {
777     FailingFibAction g = new FailingFibAction(9);
778 jsr166 1.7 try {
779     invokeAll(g);
780     shouldThrow();
781 jsr166 1.19 } catch (FJException success) {
782     checkTaskThrew(g, success);
783     }
784 jsr166 1.7 }};
785 jsr166 1.14 testInvokeOnPool(mainPool(), a);
786 dl 1.1 }
787    
788 jsr166 1.2 /**
789 dl 1.1 * invokeAll(tasks) with > 2 argument throws exception if any task does
790     */
791     public void testAbnormalInvokeAll3() {
792 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
793     public void realCompute() {
794     FibAction f = new FibAction(8);
795     FailingFibAction g = new FailingFibAction(9);
796     FibAction h = new FibAction(7);
797 jsr166 1.7 try {
798     invokeAll(f, g, h);
799     shouldThrow();
800 jsr166 1.19 } catch (FJException success) {
801     checkTaskThrew(g, success);
802     }
803 jsr166 1.7 }};
804 jsr166 1.14 testInvokeOnPool(mainPool(), a);
805 dl 1.1 }
806    
807 jsr166 1.2 /**
808 jsr166 1.3 * invokeAll(collection) throws exception if any task does
809 dl 1.1 */
810     public void testAbnormalInvokeAllCollection() {
811 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
812     public void realCompute() {
813     FailingFibAction f = new FailingFibAction(8);
814     FibAction g = new FibAction(9);
815     FibAction h = new FibAction(7);
816     HashSet set = new HashSet();
817     set.add(f);
818     set.add(g);
819     set.add(h);
820 jsr166 1.7 try {
821     invokeAll(set);
822     shouldThrow();
823 jsr166 1.19 } catch (FJException success) {
824     checkTaskThrew(f, success);
825     }
826 jsr166 1.7 }};
827 jsr166 1.14 testInvokeOnPool(mainPool(), a);
828 dl 1.1 }
829    
830 jsr166 1.2 /**
831 dl 1.1 * tryUnfork returns true for most recent unexecuted task,
832     * and suppresses execution
833     */
834     public void testTryUnfork() {
835 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
836     public void realCompute() {
837 jsr166 1.7 FibAction g = new FibAction(9);
838 jsr166 1.18 assertSame(g, g.fork());
839 jsr166 1.7 FibAction f = new FibAction(8);
840 jsr166 1.18 assertSame(f, f.fork());
841     assertTrue(f.tryUnfork());
842 jsr166 1.7 helpQuiesce();
843 jsr166 1.19 checkNotDone(f);
844     checkCompletedNormally(g);
845 jsr166 1.7 }};
846 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
847 dl 1.1 }
848    
849 jsr166 1.2 /**
850 dl 1.1 * getSurplusQueuedTaskCount returns > 0 when
851     * there are more tasks than threads
852     */
853     public void testGetSurplusQueuedTaskCount() {
854 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
855     public void realCompute() {
856 jsr166 1.7 FibAction h = new FibAction(7);
857 jsr166 1.18 assertSame(h, h.fork());
858 jsr166 1.7 FibAction g = new FibAction(9);
859 jsr166 1.18 assertSame(g, g.fork());
860 jsr166 1.7 FibAction f = new FibAction(8);
861 jsr166 1.18 assertSame(f, f.fork());
862     assertTrue(getSurplusQueuedTaskCount() > 0);
863 jsr166 1.7 helpQuiesce();
864 jsr166 1.19 checkCompletedNormally(f);
865     checkCompletedNormally(g);
866     checkCompletedNormally(h);
867 jsr166 1.7 }};
868 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
869 dl 1.1 }
870    
871 jsr166 1.2 /**
872 dl 1.1 * peekNextLocalTask returns most recent unexecuted task.
873     */
874     public void testPeekNextLocalTask() {
875 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
876     public void realCompute() {
877 jsr166 1.7 FibAction g = new FibAction(9);
878 jsr166 1.18 assertSame(g, g.fork());
879 jsr166 1.7 FibAction f = new FibAction(8);
880 jsr166 1.18 assertSame(f, f.fork());
881     assertSame(f, peekNextLocalTask());
882     assertNull(f.join());
883 jsr166 1.19 checkCompletedNormally(f);
884 jsr166 1.7 helpQuiesce();
885 jsr166 1.19 checkCompletedNormally(f);
886     checkCompletedNormally(g);
887 jsr166 1.7 }};
888 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
889 dl 1.1 }
890    
891 jsr166 1.2 /**
892 dl 1.1 * pollNextLocalTask returns most recent unexecuted task
893     * without executing it
894     */
895     public void testPollNextLocalTask() {
896 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
897     public void realCompute() {
898 jsr166 1.7 FibAction g = new FibAction(9);
899 jsr166 1.18 assertSame(g, g.fork());
900 jsr166 1.7 FibAction f = new FibAction(8);
901 jsr166 1.18 assertSame(f, f.fork());
902     assertSame(f, pollNextLocalTask());
903 jsr166 1.7 helpQuiesce();
904 jsr166 1.19 checkNotDone(f);
905     checkCompletedNormally(g);
906 jsr166 1.7 }};
907 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
908 dl 1.1 }
909    
910 jsr166 1.2 /**
911 jsr166 1.19 * pollTask returns an unexecuted task without executing it
912 dl 1.1 */
913     public void testPollTask() {
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     assertSame(f, pollTask());
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 * peekNextLocalTask returns least recent unexecuted task in async mode
930     */
931     public void testPeekNextLocalTaskAsync() {
932 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
933     public void realCompute() {
934 jsr166 1.7 FibAction g = new FibAction(9);
935 jsr166 1.18 assertSame(g, g.fork());
936 jsr166 1.7 FibAction f = new FibAction(8);
937 jsr166 1.18 assertSame(f, f.fork());
938     assertSame(g, peekNextLocalTask());
939     assertNull(f.join());
940 jsr166 1.7 helpQuiesce();
941 jsr166 1.19 checkCompletedNormally(f);
942     checkCompletedNormally(g);
943 jsr166 1.7 }};
944 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
945 dl 1.1 }
946    
947 jsr166 1.2 /**
948 jsr166 1.19 * pollNextLocalTask returns least recent unexecuted task without
949     * executing it, in async mode
950 dl 1.1 */
951     public void testPollNextLocalTaskAsync() {
952 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
953     public void realCompute() {
954 jsr166 1.7 FibAction g = new FibAction(9);
955 jsr166 1.18 assertSame(g, g.fork());
956 jsr166 1.7 FibAction f = new FibAction(8);
957 jsr166 1.18 assertSame(f, f.fork());
958     assertSame(g, pollNextLocalTask());
959 jsr166 1.7 helpQuiesce();
960 jsr166 1.19 checkCompletedNormally(f);
961     checkNotDone(g);
962 jsr166 1.7 }};
963 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
964 dl 1.1 }
965    
966 jsr166 1.2 /**
967 jsr166 1.19 * pollTask returns an unexecuted task without executing it, in
968     * async mode
969 dl 1.1 */
970     public void testPollTaskAsync() {
971 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
972     public void realCompute() {
973 jsr166 1.7 FibAction g = new FibAction(9);
974 jsr166 1.18 assertSame(g, g.fork());
975 jsr166 1.7 FibAction f = new FibAction(8);
976 jsr166 1.18 assertSame(f, f.fork());
977     assertSame(g, pollTask());
978 jsr166 1.7 helpQuiesce();
979 jsr166 1.19 checkCompletedNormally(f);
980     checkNotDone(g);
981 jsr166 1.7 }};
982 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
983 dl 1.1 }
984    
985     }