ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveActionTest.java
Revision: 1.30
Committed: Tue Mar 15 19:47:07 2011 UTC (13 years, 2 months ago) by jsr166
Branch: MAIN
CVS Tags: release-1_7_0
Changes since 1.29: +1 -1 lines
Log Message:
Update Creative Commons license URL in legal notices

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 jsr166 1.30 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
6 jsr166 1.14
7 dl 1.1 import junit.framework.*;
8 jsr166 1.15 import java.util.concurrent.CancellationException;
9 jsr166 1.26 import java.util.concurrent.SynchronousQueue;
10 jsr166 1.15 import java.util.concurrent.ExecutionException;
11     import java.util.concurrent.ForkJoinPool;
12 jsr166 1.27 import java.util.concurrent.ForkJoinTask;
13 jsr166 1.15 import java.util.concurrent.ForkJoinWorkerThread;
14     import java.util.concurrent.RecursiveAction;
15     import java.util.concurrent.TimeUnit;
16 jsr166 1.19 import java.util.concurrent.TimeoutException;
17     import static java.util.concurrent.TimeUnit.SECONDS;
18 jsr166 1.15 import java.util.HashSet;
19 dl 1.1
20     public class RecursiveActionTest extends JSR166TestCase {
21    
22     public static void main(String[] args) {
23 jsr166 1.11 junit.textui.TestRunner.run(suite());
24 dl 1.1 }
25 jsr166 1.13
26 dl 1.1 public static Test suite() {
27 jsr166 1.9 return new TestSuite(RecursiveActionTest.class);
28 dl 1.1 }
29    
30 jsr166 1.14 private static ForkJoinPool mainPool() {
31     return new ForkJoinPool();
32     }
33    
34     private static ForkJoinPool singletonPool() {
35     return new ForkJoinPool(1);
36     }
37    
38     private static ForkJoinPool asyncSingletonPool() {
39     return new ForkJoinPool(1,
40     ForkJoinPool.defaultForkJoinWorkerThreadFactory,
41     null, true);
42     }
43    
44     private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
45     try {
46 jsr166 1.19 checkNotDone(a);
47 jsr166 1.18
48     assertNull(pool.invoke(a));
49    
50 jsr166 1.19 checkCompletedNormally(a);
51 jsr166 1.14 } finally {
52     joinPool(pool);
53     }
54     }
55 dl 1.1
56 jsr166 1.19 void checkNotDone(RecursiveAction a) {
57     assertFalse(a.isDone());
58     assertFalse(a.isCompletedNormally());
59     assertFalse(a.isCompletedAbnormally());
60     assertFalse(a.isCancelled());
61     assertNull(a.getException());
62     assertNull(a.getRawResult());
63    
64 jsr166 1.27 if (! ForkJoinTask.inForkJoinPool()) {
65 jsr166 1.19 Thread.currentThread().interrupt();
66     try {
67     a.get();
68     shouldThrow();
69     } catch (InterruptedException success) {
70     } catch (Throwable fail) { threadUnexpectedException(fail); }
71    
72     Thread.currentThread().interrupt();
73     try {
74     a.get(5L, SECONDS);
75     shouldThrow();
76     } catch (InterruptedException success) {
77     } catch (Throwable fail) { threadUnexpectedException(fail); }
78     }
79    
80     try {
81     a.get(0L, SECONDS);
82     shouldThrow();
83     } catch (TimeoutException success) {
84     } catch (Throwable fail) { threadUnexpectedException(fail); }
85     }
86    
87     void checkCompletedNormally(RecursiveAction a) {
88     assertTrue(a.isDone());
89     assertFalse(a.isCancelled());
90     assertTrue(a.isCompletedNormally());
91     assertFalse(a.isCompletedAbnormally());
92     assertNull(a.getException());
93     assertNull(a.getRawResult());
94     assertNull(a.join());
95 jsr166 1.22 assertFalse(a.cancel(false));
96     assertFalse(a.cancel(true));
97 jsr166 1.19 try {
98     assertNull(a.get());
99     } catch (Throwable fail) { threadUnexpectedException(fail); }
100     try {
101     assertNull(a.get(5L, SECONDS));
102     } catch (Throwable fail) { threadUnexpectedException(fail); }
103     }
104    
105     void checkCancelled(RecursiveAction a) {
106     assertTrue(a.isDone());
107     assertTrue(a.isCancelled());
108     assertFalse(a.isCompletedNormally());
109     assertTrue(a.isCompletedAbnormally());
110     assertTrue(a.getException() instanceof CancellationException);
111     assertNull(a.getRawResult());
112    
113     try {
114     a.join();
115     shouldThrow();
116     } catch (CancellationException success) {
117     } catch (Throwable fail) { threadUnexpectedException(fail); }
118    
119     try {
120     a.get();
121     shouldThrow();
122     } catch (CancellationException success) {
123     } catch (Throwable fail) { threadUnexpectedException(fail); }
124    
125     try {
126     a.get(5L, SECONDS);
127     shouldThrow();
128     } catch (CancellationException success) {
129     } catch (Throwable fail) { threadUnexpectedException(fail); }
130     }
131    
132 jsr166 1.21 void checkCompletedAbnormally(RecursiveAction a, Throwable t) {
133 jsr166 1.19 assertTrue(a.isDone());
134     assertFalse(a.isCancelled());
135     assertFalse(a.isCompletedNormally());
136     assertTrue(a.isCompletedAbnormally());
137 dl 1.29 assertSame(t.getClass(), a.getException().getClass());
138 jsr166 1.19 assertNull(a.getRawResult());
139 jsr166 1.22 assertFalse(a.cancel(false));
140     assertFalse(a.cancel(true));
141 jsr166 1.19
142     try {
143     a.join();
144     shouldThrow();
145     } catch (Throwable expected) {
146 dl 1.29 assertSame(expected.getClass(), t.getClass());
147 jsr166 1.19 }
148    
149     try {
150     a.get();
151     shouldThrow();
152     } catch (ExecutionException success) {
153 dl 1.29 assertSame(t.getClass(), success.getCause().getClass());
154 jsr166 1.19 } catch (Throwable fail) { threadUnexpectedException(fail); }
155    
156     try {
157     a.get(5L, SECONDS);
158     shouldThrow();
159     } catch (ExecutionException success) {
160 dl 1.29 assertSame(t.getClass(), success.getCause().getClass());
161 jsr166 1.19 } catch (Throwable fail) { threadUnexpectedException(fail); }
162     }
163    
164 dl 1.29 public static final class FJException extends RuntimeException {
165     public FJException() { super(); }
166     public FJException(Throwable cause) { super(cause); }
167 dl 1.1 }
168    
169     // A simple recursive action for testing
170 jsr166 1.18 final class FibAction extends CheckedRecursiveAction {
171 dl 1.1 final int number;
172     int result;
173     FibAction(int n) { number = n; }
174 jsr166 1.18 public void realCompute() {
175 dl 1.1 int n = number;
176     if (n <= 1)
177     result = n;
178     else {
179     FibAction f1 = new FibAction(n - 1);
180     FibAction f2 = new FibAction(n - 2);
181     invokeAll(f1, f2);
182     result = f1.result + f2.result;
183     }
184     }
185     }
186    
187     // A recursive action failing in base case
188 jsr166 1.2 static final class FailingFibAction extends RecursiveAction {
189 dl 1.1 final int number;
190     int result;
191     FailingFibAction(int n) { number = n; }
192     public void compute() {
193     int n = number;
194     if (n <= 1)
195     throw new FJException();
196     else {
197     FailingFibAction f1 = new FailingFibAction(n - 1);
198     FailingFibAction f2 = new FailingFibAction(n - 2);
199     invokeAll(f1, f2);
200     result = f1.result + f2.result;
201     }
202     }
203     }
204    
205 jsr166 1.2 /**
206 dl 1.1 * invoke returns when task completes normally.
207     * isCompletedAbnormally and isCancelled return false for normally
208     * completed tasks. getRawResult of a RecursiveAction returns null;
209     */
210     public void testInvoke() {
211 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
212     public void realCompute() {
213 jsr166 1.7 FibAction f = new FibAction(8);
214 jsr166 1.18 assertNull(f.invoke());
215     assertEquals(21, f.result);
216 jsr166 1.19 checkCompletedNormally(f);
217 jsr166 1.7 }};
218 jsr166 1.14 testInvokeOnPool(mainPool(), a);
219 dl 1.1 }
220    
221 jsr166 1.2 /**
222 dl 1.1 * quietlyInvoke task returns when task completes normally.
223     * isCompletedAbnormally and isCancelled return false for normally
224     * completed tasks
225     */
226     public void testQuietlyInvoke() {
227 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
228     public void realCompute() {
229 jsr166 1.7 FibAction f = new FibAction(8);
230     f.quietlyInvoke();
231 jsr166 1.18 assertEquals(21, f.result);
232 jsr166 1.19 checkCompletedNormally(f);
233 jsr166 1.7 }};
234 jsr166 1.14 testInvokeOnPool(mainPool(), a);
235 dl 1.1 }
236    
237 jsr166 1.2 /**
238 dl 1.1 * join of a forked task returns when task completes
239     */
240     public void testForkJoin() {
241 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
242     public void realCompute() {
243 jsr166 1.7 FibAction f = new FibAction(8);
244 jsr166 1.18 assertSame(f, f.fork());
245     assertNull(f.join());
246     assertEquals(21, f.result);
247 jsr166 1.19 checkCompletedNormally(f);
248 jsr166 1.7 }};
249 jsr166 1.14 testInvokeOnPool(mainPool(), a);
250 dl 1.1 }
251    
252 jsr166 1.2 /**
253 jsr166 1.25 * join/quietlyJoin of a forked task succeeds in the presence of interrupts
254 jsr166 1.24 */
255     public void testJoinIgnoresInterrupts() {
256     RecursiveAction a = new CheckedRecursiveAction() {
257     public void realCompute() {
258     FibAction f = new FibAction(8);
259 jsr166 1.28 final Thread myself = Thread.currentThread();
260 jsr166 1.24
261     // test join()
262     assertSame(f, f.fork());
263 jsr166 1.28 myself.interrupt();
264     assertTrue(myself.isInterrupted());
265 jsr166 1.24 assertNull(f.join());
266 jsr166 1.25 Thread.interrupted();
267 jsr166 1.24 assertEquals(21, f.result);
268     checkCompletedNormally(f);
269    
270     f.reinitialize();
271     f.cancel(true);
272     assertSame(f, f.fork());
273 jsr166 1.28 myself.interrupt();
274     assertTrue(myself.isInterrupted());
275 jsr166 1.24 try {
276     f.join();
277     shouldThrow();
278     } catch (CancellationException success) {
279 jsr166 1.25 Thread.interrupted();
280 jsr166 1.24 checkCancelled(f);
281     }
282    
283     f.reinitialize();
284     f.completeExceptionally(new FJException());
285     assertSame(f, f.fork());
286 jsr166 1.28 myself.interrupt();
287     assertTrue(myself.isInterrupted());
288 jsr166 1.24 try {
289     f.join();
290     shouldThrow();
291     } catch (FJException success) {
292 jsr166 1.25 Thread.interrupted();
293 jsr166 1.24 checkCompletedAbnormally(f, success);
294     }
295    
296     // test quietlyJoin()
297     f.reinitialize();
298     assertSame(f, f.fork());
299 jsr166 1.28 myself.interrupt();
300     assertTrue(myself.isInterrupted());
301 jsr166 1.24 f.quietlyJoin();
302 jsr166 1.25 Thread.interrupted();
303 jsr166 1.24 assertEquals(21, f.result);
304     checkCompletedNormally(f);
305    
306     f.reinitialize();
307     f.cancel(true);
308     assertSame(f, f.fork());
309 jsr166 1.28 myself.interrupt();
310     assertTrue(myself.isInterrupted());
311 jsr166 1.24 f.quietlyJoin();
312 jsr166 1.25 Thread.interrupted();
313 jsr166 1.24 checkCancelled(f);
314    
315     f.reinitialize();
316     f.completeExceptionally(new FJException());
317     assertSame(f, f.fork());
318 jsr166 1.28 myself.interrupt();
319     assertTrue(myself.isInterrupted());
320 jsr166 1.24 f.quietlyJoin();
321 jsr166 1.25 Thread.interrupted();
322 jsr166 1.24 checkCompletedAbnormally(f, f.getException());
323     }};
324     testInvokeOnPool(mainPool(), a);
325     a.reinitialize();
326     testInvokeOnPool(singletonPool(), a);
327     }
328    
329     /**
330 jsr166 1.26 * join/quietlyJoin of a forked task when not in ForkJoinPool
331     * succeeds in the presence of interrupts
332     */
333     public void testJoinIgnoresInterruptsOutsideForkJoinPool() {
334     final SynchronousQueue<FibAction[]> sq =
335     new SynchronousQueue<FibAction[]>();
336     RecursiveAction a = new CheckedRecursiveAction() {
337     public void realCompute() throws InterruptedException {
338     FibAction[] fibActions = new FibAction[6];
339     for (int i = 0; i < fibActions.length; i++)
340     fibActions[i] = new FibAction(8);
341    
342     fibActions[1].cancel(false);
343     fibActions[2].completeExceptionally(new FJException());
344     fibActions[4].cancel(true);
345     fibActions[5].completeExceptionally(new FJException());
346    
347     for (int i = 0; i < fibActions.length; i++)
348     fibActions[i].fork();
349    
350     sq.put(fibActions);
351    
352     helpQuiesce();
353     }};
354    
355     Runnable r = new CheckedRunnable() {
356     public void realRun() throws InterruptedException {
357     FibAction[] fibActions = sq.take();
358     FibAction f;
359 jsr166 1.28 final Thread myself = Thread.currentThread();
360 jsr166 1.26
361     // test join() ------------
362    
363     f = fibActions[0];
364 jsr166 1.27 assertFalse(ForkJoinTask.inForkJoinPool());
365 jsr166 1.28 myself.interrupt();
366     assertTrue(myself.isInterrupted());
367 jsr166 1.26 assertNull(f.join());
368     assertTrue(Thread.interrupted());
369     assertEquals(21, f.result);
370     checkCompletedNormally(f);
371    
372     f = fibActions[1];
373 jsr166 1.28 myself.interrupt();
374     assertTrue(myself.isInterrupted());
375 jsr166 1.26 try {
376     f.join();
377     shouldThrow();
378     } catch (CancellationException success) {
379     assertTrue(Thread.interrupted());
380     checkCancelled(f);
381     }
382    
383     f = fibActions[2];
384 jsr166 1.28 myself.interrupt();
385     assertTrue(myself.isInterrupted());
386 jsr166 1.26 try {
387     f.join();
388     shouldThrow();
389     } catch (FJException success) {
390     assertTrue(Thread.interrupted());
391     checkCompletedAbnormally(f, success);
392     }
393    
394     // test quietlyJoin() ---------
395    
396     f = fibActions[3];
397 jsr166 1.28 myself.interrupt();
398     assertTrue(myself.isInterrupted());
399 jsr166 1.26 f.quietlyJoin();
400     assertTrue(Thread.interrupted());
401     assertEquals(21, f.result);
402     checkCompletedNormally(f);
403    
404     f = fibActions[4];
405 jsr166 1.28 myself.interrupt();
406     assertTrue(myself.isInterrupted());
407 jsr166 1.26 f.quietlyJoin();
408     assertTrue(Thread.interrupted());
409     checkCancelled(f);
410    
411     f = fibActions[5];
412 jsr166 1.28 myself.interrupt();
413     assertTrue(myself.isInterrupted());
414 jsr166 1.26 f.quietlyJoin();
415     assertTrue(Thread.interrupted());
416     assertTrue(f.getException() instanceof FJException);
417     checkCompletedAbnormally(f, f.getException());
418     }};
419    
420     Thread t;
421    
422     t = newStartedThread(r);
423     testInvokeOnPool(mainPool(), a);
424     awaitTermination(t, LONG_DELAY_MS);
425    
426     a.reinitialize();
427     t = newStartedThread(r);
428     testInvokeOnPool(singletonPool(), a);
429     awaitTermination(t, LONG_DELAY_MS);
430     }
431    
432     /**
433 dl 1.1 * get of a forked task returns when task completes
434     */
435     public void testForkGet() {
436 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
437     public void realCompute() throws Exception {
438     FibAction f = new FibAction(8);
439     assertSame(f, f.fork());
440     assertNull(f.get());
441     assertEquals(21, f.result);
442 jsr166 1.19 checkCompletedNormally(f);
443 jsr166 1.7 }};
444 jsr166 1.14 testInvokeOnPool(mainPool(), a);
445 dl 1.1 }
446    
447 jsr166 1.2 /**
448 dl 1.1 * timed get of a forked task returns when task completes
449     */
450     public void testForkTimedGet() {
451 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
452     public void realCompute() throws Exception {
453     FibAction f = new FibAction(8);
454     assertSame(f, f.fork());
455 jsr166 1.19 assertNull(f.get(5L, SECONDS));
456 jsr166 1.18 assertEquals(21, f.result);
457 jsr166 1.19 checkCompletedNormally(f);
458 jsr166 1.7 }};
459 jsr166 1.14 testInvokeOnPool(mainPool(), a);
460 dl 1.1 }
461    
462 jsr166 1.2 /**
463 dl 1.1 * timed get with null time unit throws NPE
464     */
465     public void testForkTimedGetNPE() {
466 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
467     public void realCompute() throws Exception {
468     FibAction f = new FibAction(8);
469     assertSame(f, f.fork());
470 jsr166 1.8 try {
471     f.get(5L, null);
472     shouldThrow();
473 jsr166 1.18 } catch (NullPointerException success) {}
474 jsr166 1.8 }};
475 jsr166 1.14 testInvokeOnPool(mainPool(), a);
476 dl 1.1 }
477    
478 jsr166 1.2 /**
479 dl 1.1 * quietlyJoin of a forked task returns when task completes
480     */
481     public void testForkQuietlyJoin() {
482 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
483     public void realCompute() {
484 jsr166 1.7 FibAction f = new FibAction(8);
485 jsr166 1.18 assertSame(f, f.fork());
486 jsr166 1.7 f.quietlyJoin();
487 jsr166 1.18 assertEquals(21, f.result);
488 jsr166 1.19 checkCompletedNormally(f);
489 jsr166 1.7 }};
490 jsr166 1.14 testInvokeOnPool(mainPool(), a);
491 dl 1.1 }
492    
493    
494 jsr166 1.2 /**
495 dl 1.1 * helpQuiesce returns when tasks are complete.
496     * getQueuedTaskCount returns 0 when quiescent
497     */
498     public void testForkHelpQuiesce() {
499 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
500     public void realCompute() {
501 jsr166 1.7 FibAction f = new FibAction(8);
502 jsr166 1.18 assertSame(f, f.fork());
503 jsr166 1.7 f.helpQuiesce();
504 jsr166 1.18 assertEquals(21, f.result);
505     assertEquals(0, getQueuedTaskCount());
506 jsr166 1.19 checkCompletedNormally(f);
507 jsr166 1.7 }};
508 jsr166 1.14 testInvokeOnPool(mainPool(), a);
509 dl 1.1 }
510    
511    
512 jsr166 1.2 /**
513 dl 1.1 * invoke task throws exception when task completes abnormally
514     */
515     public void testAbnormalInvoke() {
516 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
517     public void realCompute() {
518     FailingFibAction f = new FailingFibAction(8);
519 jsr166 1.7 try {
520     f.invoke();
521     shouldThrow();
522 jsr166 1.19 } catch (FJException success) {
523 jsr166 1.21 checkCompletedAbnormally(f, success);
524 jsr166 1.19 }
525 jsr166 1.7 }};
526 jsr166 1.14 testInvokeOnPool(mainPool(), a);
527 dl 1.1 }
528    
529 jsr166 1.2 /**
530 jsr166 1.4 * quietlyInvoke task returns when task completes abnormally
531 dl 1.1 */
532     public void testAbnormalQuietlyInvoke() {
533 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
534     public void realCompute() {
535 jsr166 1.7 FailingFibAction f = new FailingFibAction(8);
536     f.quietlyInvoke();
537 jsr166 1.19 assertTrue(f.getException() instanceof FJException);
538 jsr166 1.21 checkCompletedAbnormally(f, f.getException());
539 jsr166 1.7 }};
540 jsr166 1.14 testInvokeOnPool(mainPool(), a);
541 dl 1.1 }
542    
543 jsr166 1.2 /**
544 dl 1.1 * join of a forked task throws exception when task completes abnormally
545     */
546     public void testAbnormalForkJoin() {
547 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
548     public void realCompute() {
549     FailingFibAction f = new FailingFibAction(8);
550     assertSame(f, f.fork());
551 jsr166 1.7 try {
552     f.join();
553     shouldThrow();
554 jsr166 1.19 } catch (FJException success) {
555 jsr166 1.21 checkCompletedAbnormally(f, success);
556 jsr166 1.19 }
557 jsr166 1.7 }};
558 jsr166 1.14 testInvokeOnPool(mainPool(), a);
559 dl 1.1 }
560    
561 jsr166 1.2 /**
562 dl 1.1 * get of a forked task throws exception when task completes abnormally
563     */
564     public void testAbnormalForkGet() {
565 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
566     public void realCompute() throws Exception {
567     FailingFibAction f = new FailingFibAction(8);
568     assertSame(f, f.fork());
569 jsr166 1.7 try {
570     f.get();
571     shouldThrow();
572 jsr166 1.19 } catch (ExecutionException success) {
573 jsr166 1.21 Throwable cause = success.getCause();
574     assertTrue(cause instanceof FJException);
575     checkCompletedAbnormally(f, cause);
576 jsr166 1.19 }
577 jsr166 1.7 }};
578 jsr166 1.14 testInvokeOnPool(mainPool(), a);
579 dl 1.1 }
580    
581 jsr166 1.2 /**
582 dl 1.1 * timed get of a forked task throws exception when task completes abnormally
583     */
584     public void testAbnormalForkTimedGet() {
585 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
586     public void realCompute() throws Exception {
587     FailingFibAction f = new FailingFibAction(8);
588     assertSame(f, f.fork());
589 jsr166 1.7 try {
590     f.get(5L, TimeUnit.SECONDS);
591     shouldThrow();
592 jsr166 1.19 } catch (ExecutionException success) {
593 jsr166 1.21 Throwable cause = success.getCause();
594     assertTrue(cause instanceof FJException);
595     checkCompletedAbnormally(f, cause);
596 jsr166 1.19 }
597 jsr166 1.7 }};
598 jsr166 1.14 testInvokeOnPool(mainPool(), a);
599 dl 1.1 }
600    
601 jsr166 1.2 /**
602 dl 1.1 * quietlyJoin of a forked task returns when task completes abnormally
603     */
604     public void testAbnormalForkQuietlyJoin() {
605 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
606     public void realCompute() {
607 jsr166 1.7 FailingFibAction f = new FailingFibAction(8);
608 jsr166 1.18 assertSame(f, f.fork());
609 jsr166 1.7 f.quietlyJoin();
610 jsr166 1.18 assertTrue(f.getException() instanceof FJException);
611 jsr166 1.21 checkCompletedAbnormally(f, f.getException());
612 jsr166 1.7 }};
613 jsr166 1.14 testInvokeOnPool(mainPool(), a);
614 dl 1.1 }
615    
616 jsr166 1.2 /**
617 dl 1.1 * invoke task throws exception when task cancelled
618     */
619     public void testCancelledInvoke() {
620 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
621     public void realCompute() {
622     FibAction f = new FibAction(8);
623     assertTrue(f.cancel(true));
624 jsr166 1.7 try {
625     f.invoke();
626     shouldThrow();
627 jsr166 1.19 } catch (CancellationException success) {
628     checkCancelled(f);
629     }
630 jsr166 1.7 }};
631 jsr166 1.14 testInvokeOnPool(mainPool(), a);
632 dl 1.1 }
633    
634 jsr166 1.2 /**
635 dl 1.1 * join of a forked task throws exception when task cancelled
636     */
637     public void testCancelledForkJoin() {
638 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
639     public void realCompute() {
640     FibAction f = new FibAction(8);
641     assertTrue(f.cancel(true));
642     assertSame(f, f.fork());
643 jsr166 1.7 try {
644     f.join();
645     shouldThrow();
646 jsr166 1.19 } catch (CancellationException success) {
647     checkCancelled(f);
648     }
649 jsr166 1.7 }};
650 jsr166 1.14 testInvokeOnPool(mainPool(), a);
651 dl 1.1 }
652    
653 jsr166 1.2 /**
654 dl 1.1 * get of a forked task throws exception when task cancelled
655     */
656     public void testCancelledForkGet() {
657 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
658     public void realCompute() throws Exception {
659     FibAction f = new FibAction(8);
660     assertTrue(f.cancel(true));
661     assertSame(f, f.fork());
662 jsr166 1.7 try {
663     f.get();
664     shouldThrow();
665 jsr166 1.19 } catch (CancellationException success) {
666     checkCancelled(f);
667     }
668 jsr166 1.7 }};
669 jsr166 1.14 testInvokeOnPool(mainPool(), a);
670 dl 1.1 }
671    
672 jsr166 1.2 /**
673 dl 1.1 * timed get of a forked task throws exception when task cancelled
674     */
675     public void testCancelledForkTimedGet() {
676 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
677     public void realCompute() throws Exception {
678     FibAction f = new FibAction(8);
679     assertTrue(f.cancel(true));
680     assertSame(f, f.fork());
681 jsr166 1.7 try {
682 jsr166 1.19 f.get(5L, SECONDS);
683 jsr166 1.7 shouldThrow();
684 jsr166 1.19 } catch (CancellationException success) {
685     checkCancelled(f);
686     }
687 jsr166 1.7 }};
688 jsr166 1.14 testInvokeOnPool(mainPool(), a);
689 dl 1.1 }
690    
691 jsr166 1.2 /**
692 dl 1.1 * quietlyJoin of a forked task returns when task cancelled
693     */
694     public void testCancelledForkQuietlyJoin() {
695 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
696     public void realCompute() {
697 jsr166 1.7 FibAction f = new FibAction(8);
698 jsr166 1.18 assertTrue(f.cancel(true));
699     assertSame(f, f.fork());
700 jsr166 1.7 f.quietlyJoin();
701 jsr166 1.19 checkCancelled(f);
702 jsr166 1.7 }};
703 jsr166 1.14 testInvokeOnPool(mainPool(), a);
704 dl 1.1 }
705    
706     /**
707     * getPool of executing task returns its pool
708     */
709     public void testGetPool() {
710 jsr166 1.14 final ForkJoinPool mainPool = mainPool();
711 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
712     public void realCompute() {
713     assertSame(mainPool, getPool());
714 jsr166 1.7 }};
715 jsr166 1.14 testInvokeOnPool(mainPool, a);
716 dl 1.1 }
717    
718     /**
719     * getPool of non-FJ task returns null
720     */
721     public void testGetPool2() {
722 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
723     public void realCompute() {
724     assertNull(getPool());
725 jsr166 1.7 }};
726 jsr166 1.16 assertNull(a.invoke());
727 dl 1.1 }
728    
729     /**
730     * inForkJoinPool of executing task returns true
731     */
732     public void testInForkJoinPool() {
733 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
734     public void realCompute() {
735     assertTrue(inForkJoinPool());
736 jsr166 1.7 }};
737 jsr166 1.14 testInvokeOnPool(mainPool(), a);
738 dl 1.1 }
739    
740     /**
741     * inForkJoinPool of non-FJ task returns false
742     */
743     public void testInForkJoinPool2() {
744 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
745     public void realCompute() {
746     assertFalse(inForkJoinPool());
747 jsr166 1.7 }};
748 jsr166 1.16 assertNull(a.invoke());
749 dl 1.1 }
750    
751     /**
752     * getPool of current thread in pool returns its pool
753     */
754     public void testWorkerGetPool() {
755 jsr166 1.14 final ForkJoinPool mainPool = mainPool();
756 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
757     public void realCompute() {
758 jsr166 1.7 ForkJoinWorkerThread w =
759 jsr166 1.14 (ForkJoinWorkerThread) Thread.currentThread();
760 jsr166 1.18 assertSame(mainPool, w.getPool());
761 jsr166 1.7 }};
762 jsr166 1.14 testInvokeOnPool(mainPool, a);
763 dl 1.1 }
764    
765     /**
766     * getPoolIndex of current thread in pool returns 0 <= value < poolSize
767     */
768     public void testWorkerGetPoolIndex() {
769 jsr166 1.14 final ForkJoinPool mainPool = mainPool();
770 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
771     public void realCompute() {
772 jsr166 1.7 ForkJoinWorkerThread w =
773 jsr166 1.28 (ForkJoinWorkerThread) Thread.currentThread();
774 jsr166 1.19 assertTrue(w.getPoolIndex() >= 0);
775 dl 1.29 // pool size can shrink after assigning index, so cannot check
776     // assertTrue(w.getPoolIndex() < mainPool.getPoolSize());
777 jsr166 1.7 }};
778 jsr166 1.14 testInvokeOnPool(mainPool, a);
779 dl 1.1 }
780    
781    
782     /**
783     * setRawResult(null) succeeds
784     */
785     public void testSetRawResult() {
786 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
787     public void realCompute() {
788 jsr166 1.7 setRawResult(null);
789 jsr166 1.23 assertNull(getRawResult());
790 jsr166 1.7 }};
791 jsr166 1.16 assertNull(a.invoke());
792 dl 1.1 }
793    
794 jsr166 1.2 /**
795 dl 1.1 * A reinitialized task may be re-invoked
796     */
797     public void testReinitialize() {
798 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
799     public void realCompute() {
800 jsr166 1.7 FibAction f = new FibAction(8);
801 jsr166 1.19 checkNotDone(f);
802    
803     for (int i = 0; i < 3; i++) {
804     assertNull(f.invoke());
805     assertEquals(21, f.result);
806     checkCompletedNormally(f);
807     f.reinitialize();
808     checkNotDone(f);
809     }
810 jsr166 1.7 }};
811 jsr166 1.14 testInvokeOnPool(mainPool(), a);
812 dl 1.1 }
813    
814 jsr166 1.2 /**
815 dl 1.1 * invoke task throws exception after invoking completeExceptionally
816     */
817     public void testCompleteExceptionally() {
818 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
819     public void realCompute() {
820     FibAction f = new FibAction(8);
821     f.completeExceptionally(new FJException());
822 jsr166 1.7 try {
823     f.invoke();
824     shouldThrow();
825 jsr166 1.19 } catch (FJException success) {
826 jsr166 1.21 checkCompletedAbnormally(f, success);
827 jsr166 1.19 }
828 jsr166 1.7 }};
829 jsr166 1.14 testInvokeOnPool(mainPool(), a);
830 dl 1.1 }
831    
832 jsr166 1.2 /**
833 dl 1.1 * invoke task suppresses execution invoking complete
834     */
835     public void testComplete() {
836 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
837     public void realCompute() {
838 jsr166 1.7 FibAction f = new FibAction(8);
839     f.complete(null);
840 jsr166 1.18 assertNull(f.invoke());
841     assertEquals(0, f.result);
842 jsr166 1.19 checkCompletedNormally(f);
843 jsr166 1.7 }};
844 jsr166 1.14 testInvokeOnPool(mainPool(), a);
845 dl 1.1 }
846    
847 jsr166 1.2 /**
848 dl 1.1 * invokeAll(t1, t2) invokes all task arguments
849     */
850     public void testInvokeAll2() {
851 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
852     public void realCompute() {
853 jsr166 1.7 FibAction f = new FibAction(8);
854     FibAction g = new FibAction(9);
855     invokeAll(f, g);
856 jsr166 1.19 checkCompletedNormally(f);
857 jsr166 1.18 assertEquals(21, f.result);
858 jsr166 1.19 checkCompletedNormally(g);
859 jsr166 1.18 assertEquals(34, g.result);
860 jsr166 1.7 }};
861 jsr166 1.14 testInvokeOnPool(mainPool(), a);
862 dl 1.1 }
863    
864 jsr166 1.2 /**
865 dl 1.1 * invokeAll(tasks) with 1 argument invokes task
866     */
867     public void testInvokeAll1() {
868 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
869     public void realCompute() {
870 jsr166 1.7 FibAction f = new FibAction(8);
871     invokeAll(f);
872 jsr166 1.19 checkCompletedNormally(f);
873 jsr166 1.18 assertEquals(21, f.result);
874 jsr166 1.7 }};
875 jsr166 1.14 testInvokeOnPool(mainPool(), a);
876 dl 1.1 }
877    
878 jsr166 1.2 /**
879 dl 1.1 * invokeAll(tasks) with > 2 argument invokes tasks
880     */
881     public void testInvokeAll3() {
882 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
883     public void realCompute() {
884 jsr166 1.7 FibAction f = new FibAction(8);
885     FibAction g = new FibAction(9);
886     FibAction h = new FibAction(7);
887     invokeAll(f, g, h);
888 jsr166 1.20 assertTrue(f.isDone());
889     assertTrue(g.isDone());
890     assertTrue(h.isDone());
891 jsr166 1.19 checkCompletedNormally(f);
892 jsr166 1.18 assertEquals(21, f.result);
893 jsr166 1.19 checkCompletedNormally(g);
894 jsr166 1.18 assertEquals(34, g.result);
895 jsr166 1.19 checkCompletedNormally(g);
896 jsr166 1.18 assertEquals(13, h.result);
897 jsr166 1.7 }};
898 jsr166 1.14 testInvokeOnPool(mainPool(), a);
899 dl 1.1 }
900    
901 jsr166 1.2 /**
902 dl 1.1 * invokeAll(collection) invokes all tasks in the collection
903     */
904     public void testInvokeAllCollection() {
905 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
906     public void realCompute() {
907 jsr166 1.7 FibAction f = new FibAction(8);
908     FibAction g = new FibAction(9);
909     FibAction h = new FibAction(7);
910     HashSet set = new HashSet();
911     set.add(f);
912     set.add(g);
913     set.add(h);
914     invokeAll(set);
915 jsr166 1.20 assertTrue(f.isDone());
916     assertTrue(g.isDone());
917     assertTrue(h.isDone());
918 jsr166 1.19 checkCompletedNormally(f);
919 jsr166 1.18 assertEquals(21, f.result);
920 jsr166 1.19 checkCompletedNormally(g);
921 jsr166 1.18 assertEquals(34, g.result);
922 jsr166 1.19 checkCompletedNormally(g);
923 jsr166 1.18 assertEquals(13, h.result);
924 jsr166 1.7 }};
925 jsr166 1.14 testInvokeOnPool(mainPool(), a);
926 dl 1.1 }
927    
928    
929 jsr166 1.2 /**
930 dl 1.1 * invokeAll(tasks) with any null task throws NPE
931     */
932     public void testInvokeAllNPE() {
933 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
934     public void realCompute() {
935     FibAction f = new FibAction(8);
936     FibAction g = new FibAction(9);
937     FibAction h = null;
938 jsr166 1.7 try {
939     invokeAll(f, g, h);
940     shouldThrow();
941 jsr166 1.18 } catch (NullPointerException success) {}
942 jsr166 1.7 }};
943 jsr166 1.14 testInvokeOnPool(mainPool(), a);
944 dl 1.1 }
945    
946 jsr166 1.2 /**
947 dl 1.1 * invokeAll(t1, t2) throw exception if any task does
948     */
949     public void testAbnormalInvokeAll2() {
950 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
951     public void realCompute() {
952     FibAction f = new FibAction(8);
953     FailingFibAction g = new FailingFibAction(9);
954 jsr166 1.7 try {
955     invokeAll(f, g);
956     shouldThrow();
957 jsr166 1.19 } catch (FJException success) {
958 jsr166 1.21 checkCompletedAbnormally(g, success);
959 jsr166 1.19 }
960 jsr166 1.7 }};
961 jsr166 1.14 testInvokeOnPool(mainPool(), a);
962 dl 1.1 }
963    
964 jsr166 1.2 /**
965 dl 1.1 * invokeAll(tasks) with 1 argument throws exception if task does
966     */
967     public void testAbnormalInvokeAll1() {
968 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
969     public void realCompute() {
970     FailingFibAction g = new FailingFibAction(9);
971 jsr166 1.7 try {
972     invokeAll(g);
973     shouldThrow();
974 jsr166 1.19 } catch (FJException success) {
975 jsr166 1.21 checkCompletedAbnormally(g, success);
976 jsr166 1.19 }
977 jsr166 1.7 }};
978 jsr166 1.14 testInvokeOnPool(mainPool(), a);
979 dl 1.1 }
980    
981 jsr166 1.2 /**
982 dl 1.1 * invokeAll(tasks) with > 2 argument throws exception if any task does
983     */
984     public void testAbnormalInvokeAll3() {
985 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
986     public void realCompute() {
987     FibAction f = new FibAction(8);
988     FailingFibAction g = new FailingFibAction(9);
989     FibAction h = new FibAction(7);
990 jsr166 1.7 try {
991     invokeAll(f, g, h);
992     shouldThrow();
993 jsr166 1.19 } catch (FJException success) {
994 jsr166 1.21 checkCompletedAbnormally(g, success);
995 jsr166 1.19 }
996 jsr166 1.7 }};
997 jsr166 1.14 testInvokeOnPool(mainPool(), a);
998 dl 1.1 }
999    
1000 jsr166 1.2 /**
1001 jsr166 1.3 * invokeAll(collection) throws exception if any task does
1002 dl 1.1 */
1003     public void testAbnormalInvokeAllCollection() {
1004 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1005     public void realCompute() {
1006     FailingFibAction f = new FailingFibAction(8);
1007     FibAction g = new FibAction(9);
1008     FibAction h = new FibAction(7);
1009     HashSet set = new HashSet();
1010     set.add(f);
1011     set.add(g);
1012     set.add(h);
1013 jsr166 1.7 try {
1014     invokeAll(set);
1015     shouldThrow();
1016 jsr166 1.19 } catch (FJException success) {
1017 jsr166 1.21 checkCompletedAbnormally(f, success);
1018 jsr166 1.19 }
1019 jsr166 1.7 }};
1020 jsr166 1.14 testInvokeOnPool(mainPool(), a);
1021 dl 1.1 }
1022    
1023 jsr166 1.2 /**
1024 dl 1.1 * tryUnfork returns true for most recent unexecuted task,
1025     * and suppresses execution
1026     */
1027     public void testTryUnfork() {
1028 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1029     public void realCompute() {
1030 jsr166 1.7 FibAction g = new FibAction(9);
1031 jsr166 1.18 assertSame(g, g.fork());
1032 jsr166 1.7 FibAction f = new FibAction(8);
1033 jsr166 1.18 assertSame(f, f.fork());
1034     assertTrue(f.tryUnfork());
1035 jsr166 1.7 helpQuiesce();
1036 jsr166 1.19 checkNotDone(f);
1037     checkCompletedNormally(g);
1038 jsr166 1.7 }};
1039 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1040 dl 1.1 }
1041    
1042 jsr166 1.2 /**
1043 dl 1.1 * getSurplusQueuedTaskCount returns > 0 when
1044     * there are more tasks than threads
1045     */
1046     public void testGetSurplusQueuedTaskCount() {
1047 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1048     public void realCompute() {
1049 jsr166 1.7 FibAction h = new FibAction(7);
1050 jsr166 1.18 assertSame(h, h.fork());
1051 jsr166 1.7 FibAction g = new FibAction(9);
1052 jsr166 1.18 assertSame(g, g.fork());
1053 jsr166 1.7 FibAction f = new FibAction(8);
1054 jsr166 1.18 assertSame(f, f.fork());
1055     assertTrue(getSurplusQueuedTaskCount() > 0);
1056 jsr166 1.7 helpQuiesce();
1057 jsr166 1.21 assertEquals(0, getSurplusQueuedTaskCount());
1058 jsr166 1.19 checkCompletedNormally(f);
1059     checkCompletedNormally(g);
1060     checkCompletedNormally(h);
1061 jsr166 1.7 }};
1062 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1063 dl 1.1 }
1064    
1065 jsr166 1.2 /**
1066 dl 1.1 * peekNextLocalTask returns most recent unexecuted task.
1067     */
1068     public void testPeekNextLocalTask() {
1069 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1070     public void realCompute() {
1071 jsr166 1.7 FibAction g = new FibAction(9);
1072 jsr166 1.18 assertSame(g, g.fork());
1073 jsr166 1.7 FibAction f = new FibAction(8);
1074 jsr166 1.18 assertSame(f, f.fork());
1075     assertSame(f, peekNextLocalTask());
1076     assertNull(f.join());
1077 jsr166 1.19 checkCompletedNormally(f);
1078 jsr166 1.7 helpQuiesce();
1079 jsr166 1.19 checkCompletedNormally(f);
1080     checkCompletedNormally(g);
1081 jsr166 1.7 }};
1082 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1083 dl 1.1 }
1084    
1085 jsr166 1.2 /**
1086 dl 1.1 * pollNextLocalTask returns most recent unexecuted task
1087     * without executing it
1088     */
1089     public void testPollNextLocalTask() {
1090 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1091     public void realCompute() {
1092 jsr166 1.7 FibAction g = new FibAction(9);
1093 jsr166 1.18 assertSame(g, g.fork());
1094 jsr166 1.7 FibAction f = new FibAction(8);
1095 jsr166 1.18 assertSame(f, f.fork());
1096     assertSame(f, pollNextLocalTask());
1097 jsr166 1.7 helpQuiesce();
1098 jsr166 1.19 checkNotDone(f);
1099     checkCompletedNormally(g);
1100 jsr166 1.7 }};
1101 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1102 dl 1.1 }
1103    
1104 jsr166 1.2 /**
1105 jsr166 1.19 * pollTask returns an unexecuted task without executing it
1106 dl 1.1 */
1107     public void testPollTask() {
1108 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1109     public void realCompute() {
1110 jsr166 1.7 FibAction g = new FibAction(9);
1111 jsr166 1.18 assertSame(g, g.fork());
1112 jsr166 1.7 FibAction f = new FibAction(8);
1113 jsr166 1.18 assertSame(f, f.fork());
1114     assertSame(f, pollTask());
1115 jsr166 1.7 helpQuiesce();
1116 jsr166 1.19 checkNotDone(f);
1117     checkCompletedNormally(g);
1118 jsr166 1.7 }};
1119 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1120 dl 1.1 }
1121    
1122 jsr166 1.2 /**
1123 dl 1.1 * peekNextLocalTask returns least recent unexecuted task in async mode
1124     */
1125     public void testPeekNextLocalTaskAsync() {
1126 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1127     public void realCompute() {
1128 jsr166 1.7 FibAction g = new FibAction(9);
1129 jsr166 1.18 assertSame(g, g.fork());
1130 jsr166 1.7 FibAction f = new FibAction(8);
1131 jsr166 1.18 assertSame(f, f.fork());
1132     assertSame(g, peekNextLocalTask());
1133     assertNull(f.join());
1134 jsr166 1.7 helpQuiesce();
1135 jsr166 1.19 checkCompletedNormally(f);
1136     checkCompletedNormally(g);
1137 jsr166 1.7 }};
1138 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
1139 dl 1.1 }
1140    
1141 jsr166 1.2 /**
1142 jsr166 1.19 * pollNextLocalTask returns least recent unexecuted task without
1143     * executing it, in async mode
1144 dl 1.1 */
1145     public void testPollNextLocalTaskAsync() {
1146 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1147     public void realCompute() {
1148 jsr166 1.7 FibAction g = new FibAction(9);
1149 jsr166 1.18 assertSame(g, g.fork());
1150 jsr166 1.7 FibAction f = new FibAction(8);
1151 jsr166 1.18 assertSame(f, f.fork());
1152     assertSame(g, pollNextLocalTask());
1153 jsr166 1.7 helpQuiesce();
1154 jsr166 1.19 checkCompletedNormally(f);
1155     checkNotDone(g);
1156 jsr166 1.7 }};
1157 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
1158 dl 1.1 }
1159    
1160 jsr166 1.2 /**
1161 jsr166 1.19 * pollTask returns an unexecuted task without executing it, in
1162     * async mode
1163 dl 1.1 */
1164     public void testPollTaskAsync() {
1165 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1166     public void realCompute() {
1167 jsr166 1.7 FibAction g = new FibAction(9);
1168 jsr166 1.18 assertSame(g, g.fork());
1169 jsr166 1.7 FibAction f = new FibAction(8);
1170 jsr166 1.18 assertSame(f, f.fork());
1171     assertSame(g, pollTask());
1172 jsr166 1.7 helpQuiesce();
1173 jsr166 1.19 checkCompletedNormally(f);
1174     checkNotDone(g);
1175 jsr166 1.7 }};
1176 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
1177 dl 1.1 }
1178    
1179     }