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