ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveTaskTest.java
Revision: 1.15
Committed: Mon Sep 13 20:48:58 2010 UTC (13 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.14: +6 -2 lines
Log Message:
fix imports

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