ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveActionTest.java
Revision: 1.32
Committed: Tue May 31 10:28:06 2011 UTC (12 years, 11 months ago) by dl
Branch: MAIN
Changes since 1.31: +5 -5 lines
Log Message:
Don't rely on reinitialize when effects not guaranteed

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 dl 1.32 f = new FibAction(8);
271 jsr166 1.24 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 dl 1.32 f = new FibAction(8);
284 jsr166 1.24 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 dl 1.32 f = new FibAction(8);
298 jsr166 1.24 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 dl 1.32 f = new FibAction(8);
307 jsr166 1.24 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 dl 1.32 f = new FibAction(8);
316 jsr166 1.24 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 jsr166 1.2 /**
494 dl 1.1 * helpQuiesce returns when tasks are complete.
495     * getQueuedTaskCount returns 0 when quiescent
496     */
497     public void testForkHelpQuiesce() {
498 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
499     public void realCompute() {
500 jsr166 1.7 FibAction f = new FibAction(8);
501 jsr166 1.18 assertSame(f, f.fork());
502 jsr166 1.31 helpQuiesce();
503 jsr166 1.18 assertEquals(21, f.result);
504     assertEquals(0, getQueuedTaskCount());
505 jsr166 1.19 checkCompletedNormally(f);
506 jsr166 1.7 }};
507 jsr166 1.14 testInvokeOnPool(mainPool(), a);
508 dl 1.1 }
509    
510 jsr166 1.2 /**
511 dl 1.1 * invoke task throws exception when task completes abnormally
512     */
513     public void testAbnormalInvoke() {
514 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
515     public void realCompute() {
516     FailingFibAction f = new FailingFibAction(8);
517 jsr166 1.7 try {
518     f.invoke();
519     shouldThrow();
520 jsr166 1.19 } catch (FJException success) {
521 jsr166 1.21 checkCompletedAbnormally(f, success);
522 jsr166 1.19 }
523 jsr166 1.7 }};
524 jsr166 1.14 testInvokeOnPool(mainPool(), a);
525 dl 1.1 }
526    
527 jsr166 1.2 /**
528 jsr166 1.4 * quietlyInvoke task returns when task completes abnormally
529 dl 1.1 */
530     public void testAbnormalQuietlyInvoke() {
531 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
532     public void realCompute() {
533 jsr166 1.7 FailingFibAction f = new FailingFibAction(8);
534     f.quietlyInvoke();
535 jsr166 1.19 assertTrue(f.getException() instanceof FJException);
536 jsr166 1.21 checkCompletedAbnormally(f, f.getException());
537 jsr166 1.7 }};
538 jsr166 1.14 testInvokeOnPool(mainPool(), a);
539 dl 1.1 }
540    
541 jsr166 1.2 /**
542 dl 1.1 * join of a forked task throws exception when task completes abnormally
543     */
544     public void testAbnormalForkJoin() {
545 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
546     public void realCompute() {
547     FailingFibAction f = new FailingFibAction(8);
548     assertSame(f, f.fork());
549 jsr166 1.7 try {
550     f.join();
551     shouldThrow();
552 jsr166 1.19 } catch (FJException success) {
553 jsr166 1.21 checkCompletedAbnormally(f, success);
554 jsr166 1.19 }
555 jsr166 1.7 }};
556 jsr166 1.14 testInvokeOnPool(mainPool(), a);
557 dl 1.1 }
558    
559 jsr166 1.2 /**
560 dl 1.1 * get of a forked task throws exception when task completes abnormally
561     */
562     public void testAbnormalForkGet() {
563 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
564     public void realCompute() throws Exception {
565     FailingFibAction f = new FailingFibAction(8);
566     assertSame(f, f.fork());
567 jsr166 1.7 try {
568     f.get();
569     shouldThrow();
570 jsr166 1.19 } catch (ExecutionException success) {
571 jsr166 1.21 Throwable cause = success.getCause();
572     assertTrue(cause instanceof FJException);
573     checkCompletedAbnormally(f, cause);
574 jsr166 1.19 }
575 jsr166 1.7 }};
576 jsr166 1.14 testInvokeOnPool(mainPool(), a);
577 dl 1.1 }
578    
579 jsr166 1.2 /**
580 dl 1.1 * timed get of a forked task throws exception when task completes abnormally
581     */
582     public void testAbnormalForkTimedGet() {
583 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
584     public void realCompute() throws Exception {
585     FailingFibAction f = new FailingFibAction(8);
586     assertSame(f, f.fork());
587 jsr166 1.7 try {
588     f.get(5L, TimeUnit.SECONDS);
589     shouldThrow();
590 jsr166 1.19 } catch (ExecutionException success) {
591 jsr166 1.21 Throwable cause = success.getCause();
592     assertTrue(cause instanceof FJException);
593     checkCompletedAbnormally(f, cause);
594 jsr166 1.19 }
595 jsr166 1.7 }};
596 jsr166 1.14 testInvokeOnPool(mainPool(), a);
597 dl 1.1 }
598    
599 jsr166 1.2 /**
600 dl 1.1 * quietlyJoin of a forked task returns when task completes abnormally
601     */
602     public void testAbnormalForkQuietlyJoin() {
603 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
604     public void realCompute() {
605 jsr166 1.7 FailingFibAction f = new FailingFibAction(8);
606 jsr166 1.18 assertSame(f, f.fork());
607 jsr166 1.7 f.quietlyJoin();
608 jsr166 1.18 assertTrue(f.getException() instanceof FJException);
609 jsr166 1.21 checkCompletedAbnormally(f, f.getException());
610 jsr166 1.7 }};
611 jsr166 1.14 testInvokeOnPool(mainPool(), a);
612 dl 1.1 }
613    
614 jsr166 1.2 /**
615 dl 1.1 * invoke task throws exception when task cancelled
616     */
617     public void testCancelledInvoke() {
618 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
619     public void realCompute() {
620     FibAction f = new FibAction(8);
621     assertTrue(f.cancel(true));
622 jsr166 1.7 try {
623     f.invoke();
624     shouldThrow();
625 jsr166 1.19 } catch (CancellationException success) {
626     checkCancelled(f);
627     }
628 jsr166 1.7 }};
629 jsr166 1.14 testInvokeOnPool(mainPool(), a);
630 dl 1.1 }
631    
632 jsr166 1.2 /**
633 dl 1.1 * join of a forked task throws exception when task cancelled
634     */
635     public void testCancelledForkJoin() {
636 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
637     public void realCompute() {
638     FibAction f = new FibAction(8);
639     assertTrue(f.cancel(true));
640     assertSame(f, f.fork());
641 jsr166 1.7 try {
642     f.join();
643     shouldThrow();
644 jsr166 1.19 } catch (CancellationException success) {
645     checkCancelled(f);
646     }
647 jsr166 1.7 }};
648 jsr166 1.14 testInvokeOnPool(mainPool(), a);
649 dl 1.1 }
650    
651 jsr166 1.2 /**
652 dl 1.1 * get of a forked task throws exception when task cancelled
653     */
654     public void testCancelledForkGet() {
655 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
656     public void realCompute() throws Exception {
657     FibAction f = new FibAction(8);
658     assertTrue(f.cancel(true));
659     assertSame(f, f.fork());
660 jsr166 1.7 try {
661     f.get();
662     shouldThrow();
663 jsr166 1.19 } catch (CancellationException success) {
664     checkCancelled(f);
665     }
666 jsr166 1.7 }};
667 jsr166 1.14 testInvokeOnPool(mainPool(), a);
668 dl 1.1 }
669    
670 jsr166 1.2 /**
671 dl 1.1 * timed get of a forked task throws exception when task cancelled
672     */
673     public void testCancelledForkTimedGet() {
674 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
675     public void realCompute() throws Exception {
676     FibAction f = new FibAction(8);
677     assertTrue(f.cancel(true));
678     assertSame(f, f.fork());
679 jsr166 1.7 try {
680 jsr166 1.19 f.get(5L, SECONDS);
681 jsr166 1.7 shouldThrow();
682 jsr166 1.19 } catch (CancellationException success) {
683     checkCancelled(f);
684     }
685 jsr166 1.7 }};
686 jsr166 1.14 testInvokeOnPool(mainPool(), a);
687 dl 1.1 }
688    
689 jsr166 1.2 /**
690 dl 1.1 * quietlyJoin of a forked task returns when task cancelled
691     */
692     public void testCancelledForkQuietlyJoin() {
693 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
694     public void realCompute() {
695 jsr166 1.7 FibAction f = new FibAction(8);
696 jsr166 1.18 assertTrue(f.cancel(true));
697     assertSame(f, f.fork());
698 jsr166 1.7 f.quietlyJoin();
699 jsr166 1.19 checkCancelled(f);
700 jsr166 1.7 }};
701 jsr166 1.14 testInvokeOnPool(mainPool(), a);
702 dl 1.1 }
703    
704     /**
705     * getPool of executing task returns its pool
706     */
707     public void testGetPool() {
708 jsr166 1.14 final ForkJoinPool mainPool = mainPool();
709 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
710     public void realCompute() {
711     assertSame(mainPool, getPool());
712 jsr166 1.7 }};
713 jsr166 1.14 testInvokeOnPool(mainPool, a);
714 dl 1.1 }
715    
716     /**
717     * getPool of non-FJ task returns null
718     */
719     public void testGetPool2() {
720 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
721     public void realCompute() {
722     assertNull(getPool());
723 jsr166 1.7 }};
724 jsr166 1.16 assertNull(a.invoke());
725 dl 1.1 }
726    
727     /**
728     * inForkJoinPool of executing task returns true
729     */
730     public void testInForkJoinPool() {
731 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
732     public void realCompute() {
733     assertTrue(inForkJoinPool());
734 jsr166 1.7 }};
735 jsr166 1.14 testInvokeOnPool(mainPool(), a);
736 dl 1.1 }
737    
738     /**
739     * inForkJoinPool of non-FJ task returns false
740     */
741     public void testInForkJoinPool2() {
742 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
743     public void realCompute() {
744     assertFalse(inForkJoinPool());
745 jsr166 1.7 }};
746 jsr166 1.16 assertNull(a.invoke());
747 dl 1.1 }
748    
749     /**
750     * getPool of current thread in pool returns its pool
751     */
752     public void testWorkerGetPool() {
753 jsr166 1.14 final ForkJoinPool mainPool = mainPool();
754 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
755     public void realCompute() {
756 jsr166 1.7 ForkJoinWorkerThread w =
757 jsr166 1.14 (ForkJoinWorkerThread) Thread.currentThread();
758 jsr166 1.18 assertSame(mainPool, w.getPool());
759 jsr166 1.7 }};
760 jsr166 1.14 testInvokeOnPool(mainPool, a);
761 dl 1.1 }
762    
763     /**
764     * getPoolIndex of current thread in pool returns 0 <= value < poolSize
765     */
766     public void testWorkerGetPoolIndex() {
767 jsr166 1.14 final ForkJoinPool mainPool = mainPool();
768 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
769     public void realCompute() {
770 jsr166 1.7 ForkJoinWorkerThread w =
771 jsr166 1.28 (ForkJoinWorkerThread) Thread.currentThread();
772 jsr166 1.19 assertTrue(w.getPoolIndex() >= 0);
773 dl 1.29 // pool size can shrink after assigning index, so cannot check
774     // assertTrue(w.getPoolIndex() < mainPool.getPoolSize());
775 jsr166 1.7 }};
776 jsr166 1.14 testInvokeOnPool(mainPool, a);
777 dl 1.1 }
778    
779     /**
780     * setRawResult(null) succeeds
781     */
782     public void testSetRawResult() {
783 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
784     public void realCompute() {
785 jsr166 1.7 setRawResult(null);
786 jsr166 1.23 assertNull(getRawResult());
787 jsr166 1.7 }};
788 jsr166 1.16 assertNull(a.invoke());
789 dl 1.1 }
790    
791 jsr166 1.2 /**
792 dl 1.1 * A reinitialized task may be re-invoked
793     */
794     public void testReinitialize() {
795 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
796     public void realCompute() {
797 jsr166 1.7 FibAction f = new FibAction(8);
798 jsr166 1.19 checkNotDone(f);
799    
800     for (int i = 0; i < 3; i++) {
801     assertNull(f.invoke());
802     assertEquals(21, f.result);
803     checkCompletedNormally(f);
804     f.reinitialize();
805     checkNotDone(f);
806     }
807 jsr166 1.7 }};
808 jsr166 1.14 testInvokeOnPool(mainPool(), a);
809 dl 1.1 }
810    
811 jsr166 1.2 /**
812 dl 1.1 * invoke task throws exception after invoking completeExceptionally
813     */
814     public void testCompleteExceptionally() {
815 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
816     public void realCompute() {
817     FibAction f = new FibAction(8);
818     f.completeExceptionally(new FJException());
819 jsr166 1.7 try {
820     f.invoke();
821     shouldThrow();
822 jsr166 1.19 } catch (FJException success) {
823 jsr166 1.21 checkCompletedAbnormally(f, success);
824 jsr166 1.19 }
825 jsr166 1.7 }};
826 jsr166 1.14 testInvokeOnPool(mainPool(), a);
827 dl 1.1 }
828    
829 jsr166 1.2 /**
830 dl 1.1 * invoke task suppresses execution invoking complete
831     */
832     public void testComplete() {
833 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
834     public void realCompute() {
835 jsr166 1.7 FibAction f = new FibAction(8);
836     f.complete(null);
837 jsr166 1.18 assertNull(f.invoke());
838     assertEquals(0, f.result);
839 jsr166 1.19 checkCompletedNormally(f);
840 jsr166 1.7 }};
841 jsr166 1.14 testInvokeOnPool(mainPool(), a);
842 dl 1.1 }
843    
844 jsr166 1.2 /**
845 dl 1.1 * invokeAll(t1, t2) invokes all task arguments
846     */
847     public void testInvokeAll2() {
848 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
849     public void realCompute() {
850 jsr166 1.7 FibAction f = new FibAction(8);
851     FibAction g = new FibAction(9);
852     invokeAll(f, g);
853 jsr166 1.19 checkCompletedNormally(f);
854 jsr166 1.18 assertEquals(21, f.result);
855 jsr166 1.19 checkCompletedNormally(g);
856 jsr166 1.18 assertEquals(34, g.result);
857 jsr166 1.7 }};
858 jsr166 1.14 testInvokeOnPool(mainPool(), a);
859 dl 1.1 }
860    
861 jsr166 1.2 /**
862 dl 1.1 * invokeAll(tasks) with 1 argument invokes task
863     */
864     public void testInvokeAll1() {
865 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
866     public void realCompute() {
867 jsr166 1.7 FibAction f = new FibAction(8);
868     invokeAll(f);
869 jsr166 1.19 checkCompletedNormally(f);
870 jsr166 1.18 assertEquals(21, f.result);
871 jsr166 1.7 }};
872 jsr166 1.14 testInvokeOnPool(mainPool(), a);
873 dl 1.1 }
874    
875 jsr166 1.2 /**
876 dl 1.1 * invokeAll(tasks) with > 2 argument invokes tasks
877     */
878     public void testInvokeAll3() {
879 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
880     public void realCompute() {
881 jsr166 1.7 FibAction f = new FibAction(8);
882     FibAction g = new FibAction(9);
883     FibAction h = new FibAction(7);
884     invokeAll(f, g, h);
885 jsr166 1.20 assertTrue(f.isDone());
886     assertTrue(g.isDone());
887     assertTrue(h.isDone());
888 jsr166 1.19 checkCompletedNormally(f);
889 jsr166 1.18 assertEquals(21, f.result);
890 jsr166 1.19 checkCompletedNormally(g);
891 jsr166 1.18 assertEquals(34, g.result);
892 jsr166 1.19 checkCompletedNormally(g);
893 jsr166 1.18 assertEquals(13, h.result);
894 jsr166 1.7 }};
895 jsr166 1.14 testInvokeOnPool(mainPool(), a);
896 dl 1.1 }
897    
898 jsr166 1.2 /**
899 dl 1.1 * invokeAll(collection) invokes all tasks in the collection
900     */
901     public void testInvokeAllCollection() {
902 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
903     public void realCompute() {
904 jsr166 1.7 FibAction f = new FibAction(8);
905     FibAction g = new FibAction(9);
906     FibAction h = new FibAction(7);
907     HashSet set = new HashSet();
908     set.add(f);
909     set.add(g);
910     set.add(h);
911     invokeAll(set);
912 jsr166 1.20 assertTrue(f.isDone());
913     assertTrue(g.isDone());
914     assertTrue(h.isDone());
915 jsr166 1.19 checkCompletedNormally(f);
916 jsr166 1.18 assertEquals(21, f.result);
917 jsr166 1.19 checkCompletedNormally(g);
918 jsr166 1.18 assertEquals(34, g.result);
919 jsr166 1.19 checkCompletedNormally(g);
920 jsr166 1.18 assertEquals(13, h.result);
921 jsr166 1.7 }};
922 jsr166 1.14 testInvokeOnPool(mainPool(), a);
923 dl 1.1 }
924    
925 jsr166 1.2 /**
926 dl 1.1 * invokeAll(tasks) with any null task throws NPE
927     */
928     public void testInvokeAllNPE() {
929 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
930     public void realCompute() {
931     FibAction f = new FibAction(8);
932     FibAction g = new FibAction(9);
933     FibAction h = null;
934 jsr166 1.7 try {
935     invokeAll(f, g, h);
936     shouldThrow();
937 jsr166 1.18 } catch (NullPointerException success) {}
938 jsr166 1.7 }};
939 jsr166 1.14 testInvokeOnPool(mainPool(), a);
940 dl 1.1 }
941    
942 jsr166 1.2 /**
943 dl 1.1 * invokeAll(t1, t2) throw exception if any task does
944     */
945     public void testAbnormalInvokeAll2() {
946 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
947     public void realCompute() {
948     FibAction f = new FibAction(8);
949     FailingFibAction g = new FailingFibAction(9);
950 jsr166 1.7 try {
951     invokeAll(f, g);
952     shouldThrow();
953 jsr166 1.19 } catch (FJException success) {
954 jsr166 1.21 checkCompletedAbnormally(g, success);
955 jsr166 1.19 }
956 jsr166 1.7 }};
957 jsr166 1.14 testInvokeOnPool(mainPool(), a);
958 dl 1.1 }
959    
960 jsr166 1.2 /**
961 dl 1.1 * invokeAll(tasks) with 1 argument throws exception if task does
962     */
963     public void testAbnormalInvokeAll1() {
964 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
965     public void realCompute() {
966     FailingFibAction g = new FailingFibAction(9);
967 jsr166 1.7 try {
968     invokeAll(g);
969     shouldThrow();
970 jsr166 1.19 } catch (FJException success) {
971 jsr166 1.21 checkCompletedAbnormally(g, success);
972 jsr166 1.19 }
973 jsr166 1.7 }};
974 jsr166 1.14 testInvokeOnPool(mainPool(), a);
975 dl 1.1 }
976    
977 jsr166 1.2 /**
978 dl 1.1 * invokeAll(tasks) with > 2 argument throws exception if any task does
979     */
980     public void testAbnormalInvokeAll3() {
981 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
982     public void realCompute() {
983     FibAction f = new FibAction(8);
984     FailingFibAction g = new FailingFibAction(9);
985     FibAction h = new FibAction(7);
986 jsr166 1.7 try {
987     invokeAll(f, g, h);
988     shouldThrow();
989 jsr166 1.19 } catch (FJException success) {
990 jsr166 1.21 checkCompletedAbnormally(g, success);
991 jsr166 1.19 }
992 jsr166 1.7 }};
993 jsr166 1.14 testInvokeOnPool(mainPool(), a);
994 dl 1.1 }
995    
996 jsr166 1.2 /**
997 jsr166 1.3 * invokeAll(collection) throws exception if any task does
998 dl 1.1 */
999     public void testAbnormalInvokeAllCollection() {
1000 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1001     public void realCompute() {
1002     FailingFibAction f = new FailingFibAction(8);
1003     FibAction g = new FibAction(9);
1004     FibAction h = new FibAction(7);
1005     HashSet set = new HashSet();
1006     set.add(f);
1007     set.add(g);
1008     set.add(h);
1009 jsr166 1.7 try {
1010     invokeAll(set);
1011     shouldThrow();
1012 jsr166 1.19 } catch (FJException success) {
1013 jsr166 1.21 checkCompletedAbnormally(f, success);
1014 jsr166 1.19 }
1015 jsr166 1.7 }};
1016 jsr166 1.14 testInvokeOnPool(mainPool(), a);
1017 dl 1.1 }
1018    
1019 jsr166 1.2 /**
1020 dl 1.1 * tryUnfork returns true for most recent unexecuted task,
1021     * and suppresses execution
1022     */
1023     public void testTryUnfork() {
1024 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1025     public void realCompute() {
1026 jsr166 1.7 FibAction g = new FibAction(9);
1027 jsr166 1.18 assertSame(g, g.fork());
1028 jsr166 1.7 FibAction f = new FibAction(8);
1029 jsr166 1.18 assertSame(f, f.fork());
1030     assertTrue(f.tryUnfork());
1031 jsr166 1.7 helpQuiesce();
1032 jsr166 1.19 checkNotDone(f);
1033     checkCompletedNormally(g);
1034 jsr166 1.7 }};
1035 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1036 dl 1.1 }
1037    
1038 jsr166 1.2 /**
1039 dl 1.1 * getSurplusQueuedTaskCount returns > 0 when
1040     * there are more tasks than threads
1041     */
1042     public void testGetSurplusQueuedTaskCount() {
1043 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1044     public void realCompute() {
1045 jsr166 1.7 FibAction h = new FibAction(7);
1046 jsr166 1.18 assertSame(h, h.fork());
1047 jsr166 1.7 FibAction g = new FibAction(9);
1048 jsr166 1.18 assertSame(g, g.fork());
1049 jsr166 1.7 FibAction f = new FibAction(8);
1050 jsr166 1.18 assertSame(f, f.fork());
1051     assertTrue(getSurplusQueuedTaskCount() > 0);
1052 jsr166 1.7 helpQuiesce();
1053 jsr166 1.21 assertEquals(0, getSurplusQueuedTaskCount());
1054 jsr166 1.19 checkCompletedNormally(f);
1055     checkCompletedNormally(g);
1056     checkCompletedNormally(h);
1057 jsr166 1.7 }};
1058 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1059 dl 1.1 }
1060    
1061 jsr166 1.2 /**
1062 dl 1.1 * peekNextLocalTask returns most recent unexecuted task.
1063     */
1064     public void testPeekNextLocalTask() {
1065 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1066     public void realCompute() {
1067 jsr166 1.7 FibAction g = new FibAction(9);
1068 jsr166 1.18 assertSame(g, g.fork());
1069 jsr166 1.7 FibAction f = new FibAction(8);
1070 jsr166 1.18 assertSame(f, f.fork());
1071     assertSame(f, peekNextLocalTask());
1072     assertNull(f.join());
1073 jsr166 1.19 checkCompletedNormally(f);
1074 jsr166 1.7 helpQuiesce();
1075 jsr166 1.19 checkCompletedNormally(f);
1076     checkCompletedNormally(g);
1077 jsr166 1.7 }};
1078 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1079 dl 1.1 }
1080    
1081 jsr166 1.2 /**
1082 dl 1.1 * pollNextLocalTask returns most recent unexecuted task
1083     * without executing it
1084     */
1085     public void testPollNextLocalTask() {
1086 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1087     public void realCompute() {
1088 jsr166 1.7 FibAction g = new FibAction(9);
1089 jsr166 1.18 assertSame(g, g.fork());
1090 jsr166 1.7 FibAction f = new FibAction(8);
1091 jsr166 1.18 assertSame(f, f.fork());
1092     assertSame(f, pollNextLocalTask());
1093 jsr166 1.7 helpQuiesce();
1094 jsr166 1.19 checkNotDone(f);
1095     checkCompletedNormally(g);
1096 jsr166 1.7 }};
1097 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1098 dl 1.1 }
1099    
1100 jsr166 1.2 /**
1101 jsr166 1.19 * pollTask returns an unexecuted task without executing it
1102 dl 1.1 */
1103     public void testPollTask() {
1104 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1105     public void realCompute() {
1106 jsr166 1.7 FibAction g = new FibAction(9);
1107 jsr166 1.18 assertSame(g, g.fork());
1108 jsr166 1.7 FibAction f = new FibAction(8);
1109 jsr166 1.18 assertSame(f, f.fork());
1110     assertSame(f, pollTask());
1111 jsr166 1.7 helpQuiesce();
1112 jsr166 1.19 checkNotDone(f);
1113     checkCompletedNormally(g);
1114 jsr166 1.7 }};
1115 jsr166 1.14 testInvokeOnPool(singletonPool(), a);
1116 dl 1.1 }
1117    
1118 jsr166 1.2 /**
1119 dl 1.1 * peekNextLocalTask returns least recent unexecuted task in async mode
1120     */
1121     public void testPeekNextLocalTaskAsync() {
1122 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1123     public void realCompute() {
1124 jsr166 1.7 FibAction g = new FibAction(9);
1125 jsr166 1.18 assertSame(g, g.fork());
1126 jsr166 1.7 FibAction f = new FibAction(8);
1127 jsr166 1.18 assertSame(f, f.fork());
1128     assertSame(g, peekNextLocalTask());
1129     assertNull(f.join());
1130 jsr166 1.7 helpQuiesce();
1131 jsr166 1.19 checkCompletedNormally(f);
1132     checkCompletedNormally(g);
1133 jsr166 1.7 }};
1134 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
1135 dl 1.1 }
1136    
1137 jsr166 1.2 /**
1138 jsr166 1.19 * pollNextLocalTask returns least recent unexecuted task without
1139     * executing it, in async mode
1140 dl 1.1 */
1141     public void testPollNextLocalTaskAsync() {
1142 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1143     public void realCompute() {
1144 jsr166 1.7 FibAction g = new FibAction(9);
1145 jsr166 1.18 assertSame(g, g.fork());
1146 jsr166 1.7 FibAction f = new FibAction(8);
1147 jsr166 1.18 assertSame(f, f.fork());
1148     assertSame(g, pollNextLocalTask());
1149 jsr166 1.7 helpQuiesce();
1150 jsr166 1.19 checkCompletedNormally(f);
1151     checkNotDone(g);
1152 jsr166 1.7 }};
1153 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
1154 dl 1.1 }
1155    
1156 jsr166 1.2 /**
1157 jsr166 1.19 * pollTask returns an unexecuted task without executing it, in
1158     * async mode
1159 dl 1.1 */
1160     public void testPollTaskAsync() {
1161 jsr166 1.18 RecursiveAction a = new CheckedRecursiveAction() {
1162     public void realCompute() {
1163 jsr166 1.7 FibAction g = new FibAction(9);
1164 jsr166 1.18 assertSame(g, g.fork());
1165 jsr166 1.7 FibAction f = new FibAction(8);
1166 jsr166 1.18 assertSame(f, f.fork());
1167     assertSame(g, pollTask());
1168 jsr166 1.7 helpQuiesce();
1169 jsr166 1.19 checkCompletedNormally(f);
1170     checkNotDone(g);
1171 jsr166 1.7 }};
1172 jsr166 1.14 testInvokeOnPool(asyncSingletonPool(), a);
1173 dl 1.1 }
1174    
1175     }