ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveActionTest.java
Revision: 1.8
Committed: Tue Nov 17 12:31:23 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.7: +10 -11 lines
Log Message:
whitespace

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