ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveActionTest.java
Revision: 1.17
Committed: Wed Sep 15 12:46:57 2010 UTC (13 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.16: +2 -1 lines
Log Message:
Keep FJP GC-live

File Contents

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