ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveActionTest.java
Revision: 1.14
Committed: Mon Sep 13 07:51:18 2010 UTC (13 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.13: +69 -50 lines
Log Message:
avoid static ForkJoinPools; create a new pool for each test instead

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