ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveTaskTest.java
Revision: 1.29
Committed: Sat Jun 18 14:33:38 2011 UTC (12 years, 10 months ago) by jsr166
Branch: MAIN
Changes since 1.28: +0 -3 lines
Log Message:
whitespace

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