ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveTaskTest.java
Revision: 1.26
Committed: Tue Feb 22 01:18:59 2011 UTC (13 years, 2 months ago) by dl
Branch: MAIN
Changes since 1.25: +6 -6 lines
Log Message:
Tests for expected exceptions should only assert same class, not identity

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4     * http://creativecommons.org/licenses/publicdomain
5     */
6 jsr166 1.13
7 dl 1.1 import junit.framework.*;
8 jsr166 1.15 import java.util.concurrent.CancellationException;
9     import java.util.concurrent.ExecutionException;
10     import java.util.concurrent.ForkJoinPool;
11 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    
323 jsr166 1.2 /**
324 dl 1.1 * helpQuiesce returns when tasks are complete.
325     * getQueuedTaskCount returns 0 when quiescent
326     */
327     public void testForkHelpQuiesce() {
328 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
329     public Integer realCompute() {
330 jsr166 1.7 FibTask f = new FibTask(8);
331 jsr166 1.19 assertSame(f, f.fork());
332 jsr166 1.7 f.helpQuiesce();
333 jsr166 1.19 assertEquals(0, getQueuedTaskCount());
334 jsr166 1.20 checkCompletedNormally(f, 21);
335     return NoResult;
336 jsr166 1.19 }};
337 jsr166 1.20 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
338 dl 1.1 }
339    
340    
341 jsr166 1.2 /**
342 dl 1.1 * invoke task throws exception when task completes abnormally
343     */
344     public void testAbnormalInvoke() {
345 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
346     public Integer realCompute() {
347     FailingFibTask f = new FailingFibTask(8);
348 jsr166 1.7 try {
349     f.invoke();
350     shouldThrow();
351 jsr166 1.20 } catch (FJException success) {
352 jsr166 1.22 checkCompletedAbnormally(f, success);
353 jsr166 1.20 }
354 jsr166 1.7 return NoResult;
355 jsr166 1.19 }};
356 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
357 dl 1.1 }
358    
359 jsr166 1.2 /**
360 jsr166 1.4 * quietlyInvoke task returns when task completes abnormally
361 dl 1.1 */
362     public void testAbnormalQuietlyInvoke() {
363 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
364     public Integer realCompute() {
365 jsr166 1.7 FailingFibTask f = new FailingFibTask(8);
366     f.quietlyInvoke();
367 jsr166 1.20 assertTrue(f.getException() instanceof FJException);
368 jsr166 1.22 checkCompletedAbnormally(f, f.getException());
369 jsr166 1.7 return NoResult;
370 jsr166 1.19 }};
371 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
372 dl 1.1 }
373    
374 jsr166 1.2 /**
375 dl 1.1 * join of a forked task throws exception when task completes abnormally
376     */
377     public void testAbnormalForkJoin() {
378 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
379     public Integer realCompute() {
380     FailingFibTask f = new FailingFibTask(8);
381     assertSame(f, f.fork());
382 jsr166 1.7 try {
383     Integer r = f.join();
384     shouldThrow();
385 jsr166 1.20 } catch (FJException success) {
386 jsr166 1.22 checkCompletedAbnormally(f, success);
387 jsr166 1.20 }
388 jsr166 1.7 return NoResult;
389 jsr166 1.19 }};
390 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
391 dl 1.1 }
392    
393 jsr166 1.2 /**
394 dl 1.1 * get of a forked task throws exception when task completes abnormally
395     */
396     public void testAbnormalForkGet() {
397 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
398     public Integer realCompute() throws Exception {
399     FailingFibTask f = new FailingFibTask(8);
400     assertSame(f, f.fork());
401 jsr166 1.7 try {
402     Integer r = f.get();
403     shouldThrow();
404 jsr166 1.20 } catch (ExecutionException success) {
405 jsr166 1.22 Throwable cause = success.getCause();
406     assertTrue(cause instanceof FJException);
407     checkCompletedAbnormally(f, cause);
408 jsr166 1.20 }
409 jsr166 1.7 return NoResult;
410 jsr166 1.19 }};
411 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
412 dl 1.1 }
413    
414 jsr166 1.2 /**
415 dl 1.1 * timed get of a forked task throws exception when task completes abnormally
416     */
417     public void testAbnormalForkTimedGet() {
418 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
419     public Integer realCompute() throws Exception {
420     FailingFibTask f = new FailingFibTask(8);
421     assertSame(f, f.fork());
422 jsr166 1.7 try {
423 jsr166 1.20 Integer r = f.get(5L, SECONDS);
424 jsr166 1.7 shouldThrow();
425 jsr166 1.20 } catch (ExecutionException success) {
426 jsr166 1.22 Throwable cause = success.getCause();
427     assertTrue(cause instanceof FJException);
428     checkCompletedAbnormally(f, cause);
429 jsr166 1.20 }
430 jsr166 1.7 return NoResult;
431 jsr166 1.19 }};
432 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
433 dl 1.1 }
434    
435 jsr166 1.2 /**
436 dl 1.1 * quietlyJoin of a forked task returns when task completes abnormally
437     */
438     public void testAbnormalForkQuietlyJoin() {
439 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
440     public Integer realCompute() {
441 jsr166 1.7 FailingFibTask f = new FailingFibTask(8);
442 jsr166 1.19 assertSame(f, f.fork());
443 jsr166 1.7 f.quietlyJoin();
444 jsr166 1.19 assertTrue(f.getException() instanceof FJException);
445 jsr166 1.22 checkCompletedAbnormally(f, f.getException());
446 jsr166 1.7 return NoResult;
447 jsr166 1.19 }};
448 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
449 dl 1.1 }
450    
451 jsr166 1.2 /**
452 dl 1.1 * invoke task throws exception when task cancelled
453     */
454     public void testCancelledInvoke() {
455 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
456     public Integer realCompute() {
457     FibTask f = new FibTask(8);
458     assertTrue(f.cancel(true));
459 jsr166 1.7 try {
460     Integer r = f.invoke();
461     shouldThrow();
462 jsr166 1.20 } catch (CancellationException success) {
463     checkCancelled(f);
464     }
465 jsr166 1.7 return NoResult;
466 jsr166 1.19 }};
467 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
468 dl 1.1 }
469    
470 jsr166 1.2 /**
471 dl 1.1 * join of a forked task throws exception when task cancelled
472     */
473     public void testCancelledForkJoin() {
474 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
475     public Integer realCompute() {
476     FibTask f = new FibTask(8);
477     assertTrue(f.cancel(true));
478     assertSame(f, f.fork());
479 jsr166 1.7 try {
480     Integer r = f.join();
481     shouldThrow();
482 jsr166 1.20 } catch (CancellationException success) {
483     checkCancelled(f);
484     }
485 jsr166 1.7 return NoResult;
486 jsr166 1.19 }};
487 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
488 dl 1.1 }
489    
490 jsr166 1.2 /**
491 dl 1.1 * get of a forked task throws exception when task cancelled
492     */
493     public void testCancelledForkGet() {
494 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
495     public Integer realCompute() throws Exception {
496     FibTask f = new FibTask(8);
497     assertTrue(f.cancel(true));
498     assertSame(f, f.fork());
499 jsr166 1.7 try {
500     Integer r = f.get();
501     shouldThrow();
502 jsr166 1.20 } catch (CancellationException success) {
503     checkCancelled(f);
504     }
505 jsr166 1.7 return NoResult;
506 jsr166 1.19 }};
507 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
508 dl 1.1 }
509    
510 jsr166 1.2 /**
511 dl 1.1 * timed get of a forked task throws exception when task cancelled
512     */
513     public void testCancelledForkTimedGet() {
514 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
515     public Integer realCompute() throws Exception {
516     FibTask f = new FibTask(8);
517     assertTrue(f.cancel(true));
518     assertSame(f, f.fork());
519 jsr166 1.7 try {
520 jsr166 1.20 Integer r = f.get(5L, SECONDS);
521 jsr166 1.7 shouldThrow();
522 jsr166 1.20 } catch (CancellationException success) {
523     checkCancelled(f);
524     }
525 jsr166 1.7 return NoResult;
526 jsr166 1.19 }};
527 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
528 dl 1.1 }
529    
530 jsr166 1.2 /**
531 dl 1.1 * quietlyJoin of a forked task returns when task cancelled
532     */
533     public void testCancelledForkQuietlyJoin() {
534 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
535     public Integer realCompute() {
536 jsr166 1.7 FibTask f = new FibTask(8);
537 jsr166 1.19 assertTrue(f.cancel(true));
538     assertSame(f, f.fork());
539 jsr166 1.7 f.quietlyJoin();
540 jsr166 1.20 checkCancelled(f);
541 jsr166 1.7 return NoResult;
542 jsr166 1.19 }};
543 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
544 dl 1.1 }
545    
546     /**
547     * getPool of executing task returns its pool
548     */
549     public void testGetPool() {
550 jsr166 1.13 final ForkJoinPool mainPool = mainPool();
551 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
552     public Integer realCompute() {
553     assertSame(mainPool, getPool());
554 jsr166 1.7 return NoResult;
555 jsr166 1.19 }};
556 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(mainPool, a));
557 dl 1.1 }
558    
559     /**
560     * getPool of non-FJ task returns null
561     */
562     public void testGetPool2() {
563 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
564     public Integer realCompute() {
565     assertNull(getPool());
566 dl 1.1 return NoResult;
567 jsr166 1.19 }};
568 jsr166 1.16 assertSame(NoResult, a.invoke());
569 dl 1.1 }
570 jsr166 1.2
571 dl 1.1 /**
572     * inForkJoinPool of executing task returns true
573     */
574     public void testInForkJoinPool() {
575 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
576     public Integer realCompute() {
577     assertTrue(inForkJoinPool());
578 jsr166 1.7 return NoResult;
579 jsr166 1.19 }};
580 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
581 dl 1.1 }
582    
583     /**
584     * inForkJoinPool of non-FJ task returns false
585     */
586     public void testInForkJoinPool2() {
587 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
588     public Integer realCompute() {
589 jsr166 1.22 assertFalse(inForkJoinPool());
590 jsr166 1.7 return NoResult;
591 jsr166 1.19 }};
592 jsr166 1.16 assertSame(NoResult, a.invoke());
593 dl 1.1 }
594    
595     /**
596 dl 1.17 * The value set by setRawResult is returned by getRawResult
597 dl 1.1 */
598     public void testSetRawResult() {
599 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
600     public Integer realCompute() {
601 jsr166 1.7 setRawResult(NoResult);
602 jsr166 1.19 assertSame(NoResult, getRawResult());
603 jsr166 1.7 return NoResult;
604     }
605     };
606 jsr166 1.24 assertSame(NoResult, a.invoke());
607 dl 1.1 }
608    
609 jsr166 1.2 /**
610 jsr166 1.20 * A reinitialized normally completed task may be re-invoked
611 dl 1.1 */
612     public void testReinitialize() {
613 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
614     public Integer realCompute() {
615 jsr166 1.7 FibTask f = new FibTask(8);
616 jsr166 1.20 checkNotDone(f);
617    
618     for (int i = 0; i < 3; i++) {
619     Integer r = f.invoke();
620     assertEquals(21, (int) r);
621     checkCompletedNormally(f, r);
622     f.reinitialize();
623     f.publicSetRawResult(null);
624     checkNotDone(f);
625     }
626     return NoResult;
627     }};
628     assertSame(NoResult, testInvokeOnPool(mainPool(), a));
629     }
630    
631     /**
632     * A reinitialized abnormally completed task may be re-invoked
633     */
634     public void testReinitializeAbnormal() {
635     RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
636     public Integer realCompute() {
637     FailingFibTask f = new FailingFibTask(8);
638     checkNotDone(f);
639    
640     for (int i = 0; i < 3; i++) {
641     try {
642     f.invoke();
643     shouldThrow();
644     } catch (FJException success) {
645 jsr166 1.22 checkCompletedAbnormally(f, success);
646 jsr166 1.20 }
647     f.reinitialize();
648     checkNotDone(f);
649     }
650 jsr166 1.7 return NoResult;
651 jsr166 1.19 }};
652 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
653 dl 1.1 }
654    
655 jsr166 1.2 /**
656 dl 1.1 * invoke task throws exception after invoking completeExceptionally
657     */
658     public void testCompleteExceptionally() {
659 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
660     public Integer realCompute() {
661     FibTask f = new FibTask(8);
662     f.completeExceptionally(new FJException());
663 jsr166 1.7 try {
664     Integer r = f.invoke();
665     shouldThrow();
666 jsr166 1.20 } catch (FJException success) {
667 jsr166 1.22 checkCompletedAbnormally(f, success);
668 jsr166 1.20 }
669 jsr166 1.7 return NoResult;
670 jsr166 1.19 }};
671 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
672 dl 1.1 }
673    
674 jsr166 1.2 /**
675 dl 1.1 * invoke task suppresses execution invoking complete
676     */
677     public void testComplete() {
678 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
679     public Integer realCompute() {
680 jsr166 1.7 FibTask f = new FibTask(8);
681     f.complete(NoResult);
682     Integer r = f.invoke();
683 jsr166 1.19 assertSame(NoResult, r);
684 jsr166 1.20 checkCompletedNormally(f, NoResult);
685 jsr166 1.7 return r;
686 jsr166 1.19 }};
687 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
688 dl 1.1 }
689    
690 jsr166 1.2 /**
691 dl 1.1 * invokeAll(t1, t2) invokes all task arguments
692     */
693     public void testInvokeAll2() {
694 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
695     public Integer realCompute() {
696 jsr166 1.7 FibTask f = new FibTask(8);
697     FibTask g = new FibTask(9);
698     invokeAll(f, g);
699 jsr166 1.21 checkCompletedNormally(f, 21);
700     checkCompletedNormally(g, 34);
701 jsr166 1.7 return NoResult;
702 jsr166 1.19 }};
703 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
704 dl 1.1 }
705    
706 jsr166 1.2 /**
707 dl 1.1 * invokeAll(tasks) with 1 argument invokes task
708     */
709     public void testInvokeAll1() {
710 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
711     public Integer realCompute() {
712 jsr166 1.7 FibTask f = new FibTask(8);
713     invokeAll(f);
714 jsr166 1.21 checkCompletedNormally(f, 21);
715 jsr166 1.7 return NoResult;
716 jsr166 1.19 }};
717 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
718 dl 1.1 }
719    
720 jsr166 1.2 /**
721 dl 1.1 * invokeAll(tasks) with > 2 argument invokes tasks
722     */
723     public void testInvokeAll3() {
724 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
725     public Integer realCompute() {
726 jsr166 1.7 FibTask f = new FibTask(8);
727     FibTask g = new FibTask(9);
728     FibTask h = new FibTask(7);
729     invokeAll(f, g, h);
730 jsr166 1.21 assertTrue(f.isDone());
731     assertTrue(g.isDone());
732     assertTrue(h.isDone());
733     checkCompletedNormally(f, 21);
734     checkCompletedNormally(g, 34);
735     checkCompletedNormally(h, 13);
736 jsr166 1.7 return NoResult;
737 jsr166 1.19 }};
738 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
739 dl 1.1 }
740    
741 jsr166 1.2 /**
742 dl 1.1 * invokeAll(collection) invokes all tasks in the collection
743     */
744     public void testInvokeAllCollection() {
745 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
746     public Integer realCompute() {
747 jsr166 1.7 FibTask f = new FibTask(8);
748     FibTask g = new FibTask(9);
749     FibTask h = new FibTask(7);
750     HashSet set = new HashSet();
751     set.add(f);
752     set.add(g);
753     set.add(h);
754     invokeAll(set);
755 jsr166 1.21 assertTrue(f.isDone());
756     assertTrue(g.isDone());
757     assertTrue(h.isDone());
758     checkCompletedNormally(f, 21);
759     checkCompletedNormally(g, 34);
760     checkCompletedNormally(h, 13);
761 jsr166 1.7 return NoResult;
762 jsr166 1.19 }};
763 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
764 dl 1.1 }
765    
766    
767 jsr166 1.2 /**
768 jsr166 1.20 * invokeAll(tasks) with any null task throws NPE
769     */
770     public void testInvokeAllNPE() {
771     RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
772     public Integer realCompute() {
773     FibTask f = new FibTask(8);
774     FibTask g = new FibTask(9);
775     FibTask h = null;
776     try {
777     invokeAll(f, g, h);
778     shouldThrow();
779     } catch (NullPointerException success) {}
780     return NoResult;
781     }};
782     assertSame(NoResult, testInvokeOnPool(mainPool(), a));
783     }
784    
785     /**
786 dl 1.1 * invokeAll(t1, t2) throw exception if any task does
787     */
788     public void testAbnormalInvokeAll2() {
789 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
790     public Integer realCompute() {
791     FibTask f = new FibTask(8);
792     FailingFibTask g = new FailingFibTask(9);
793 jsr166 1.7 try {
794     invokeAll(f, g);
795     shouldThrow();
796 jsr166 1.20 } catch (FJException success) {
797 jsr166 1.22 checkCompletedAbnormally(g, success);
798 jsr166 1.20 }
799 jsr166 1.7 return NoResult;
800 jsr166 1.19 }};
801 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
802 dl 1.1 }
803    
804 jsr166 1.2 /**
805 dl 1.1 * invokeAll(tasks) with 1 argument throws exception if task does
806     */
807     public void testAbnormalInvokeAll1() {
808 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
809     public Integer realCompute() {
810     FailingFibTask g = new FailingFibTask(9);
811 jsr166 1.7 try {
812     invokeAll(g);
813     shouldThrow();
814 jsr166 1.20 } catch (FJException success) {
815 jsr166 1.22 checkCompletedAbnormally(g, success);
816 jsr166 1.20 }
817 jsr166 1.7 return NoResult;
818 jsr166 1.19 }};
819 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
820 dl 1.1 }
821    
822 jsr166 1.2 /**
823 dl 1.1 * invokeAll(tasks) with > 2 argument throws exception if any task does
824     */
825     public void testAbnormalInvokeAll3() {
826 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
827     public Integer realCompute() {
828     FibTask f = new FibTask(8);
829     FailingFibTask g = new FailingFibTask(9);
830     FibTask h = new FibTask(7);
831 jsr166 1.7 try {
832     invokeAll(f, g, h);
833     shouldThrow();
834 jsr166 1.20 } catch (FJException success) {
835 jsr166 1.22 checkCompletedAbnormally(g, success);
836 jsr166 1.20 }
837 jsr166 1.7 return NoResult;
838 jsr166 1.19 }};
839 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
840 dl 1.1 }
841    
842 jsr166 1.2 /**
843 jsr166 1.3 * invokeAll(collection) throws exception if any task does
844 dl 1.1 */
845     public void testAbnormalInvokeAllCollection() {
846 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
847     public Integer realCompute() {
848     FailingFibTask f = new FailingFibTask(8);
849     FibTask g = new FibTask(9);
850     FibTask h = new FibTask(7);
851     HashSet set = new HashSet();
852     set.add(f);
853     set.add(g);
854     set.add(h);
855 jsr166 1.7 try {
856     invokeAll(set);
857     shouldThrow();
858 jsr166 1.20 } catch (FJException success) {
859 jsr166 1.22 checkCompletedAbnormally(f, success);
860 jsr166 1.20 }
861 jsr166 1.7 return NoResult;
862 jsr166 1.19 }};
863 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
864 dl 1.1 }
865    
866 jsr166 1.2 /**
867 dl 1.1 * tryUnfork returns true for most recent unexecuted task,
868     * and suppresses execution
869     */
870     public void testTryUnfork() {
871 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
872     public Integer realCompute() {
873 jsr166 1.7 FibTask g = new FibTask(9);
874 jsr166 1.19 assertSame(g, g.fork());
875 jsr166 1.7 FibTask f = new FibTask(8);
876 jsr166 1.19 assertSame(f, f.fork());
877     assertTrue(f.tryUnfork());
878 jsr166 1.7 helpQuiesce();
879 jsr166 1.20 checkNotDone(f);
880     checkCompletedNormally(g, 34);
881 jsr166 1.7 return NoResult;
882 jsr166 1.19 }};
883 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(singletonPool(), a));
884 dl 1.1 }
885    
886 jsr166 1.2 /**
887 dl 1.1 * getSurplusQueuedTaskCount returns > 0 when
888     * there are more tasks than threads
889     */
890     public void testGetSurplusQueuedTaskCount() {
891 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
892     public Integer realCompute() {
893 jsr166 1.7 FibTask h = new FibTask(7);
894 jsr166 1.19 assertSame(h, h.fork());
895 jsr166 1.7 FibTask g = new FibTask(9);
896 jsr166 1.19 assertSame(g, g.fork());
897 jsr166 1.7 FibTask f = new FibTask(8);
898 jsr166 1.19 assertSame(f, f.fork());
899     assertTrue(getSurplusQueuedTaskCount() > 0);
900 jsr166 1.7 helpQuiesce();
901 jsr166 1.22 assertEquals(0, getSurplusQueuedTaskCount());
902 jsr166 1.20 checkCompletedNormally(f, 21);
903     checkCompletedNormally(g, 34);
904     checkCompletedNormally(h, 13);
905 jsr166 1.7 return NoResult;
906 jsr166 1.19 }};
907 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(singletonPool(), a));
908 dl 1.1 }
909    
910 jsr166 1.2 /**
911 dl 1.1 * peekNextLocalTask returns most recent unexecuted task.
912     */
913     public void testPeekNextLocalTask() {
914 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
915     public Integer realCompute() {
916 jsr166 1.7 FibTask g = new FibTask(9);
917 jsr166 1.19 assertSame(g, g.fork());
918 jsr166 1.7 FibTask f = new FibTask(8);
919 jsr166 1.19 assertSame(f, f.fork());
920     assertSame(f, peekNextLocalTask());
921 jsr166 1.20 checkCompletesNormally(f, 21);
922 jsr166 1.7 helpQuiesce();
923 jsr166 1.20 checkCompletedNormally(g, 34);
924 jsr166 1.7 return NoResult;
925 jsr166 1.19 }};
926 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(singletonPool(), a));
927 dl 1.1 }
928    
929 jsr166 1.2 /**
930 dl 1.1 * pollNextLocalTask returns most recent unexecuted task
931     * without executing it
932     */
933     public void testPollNextLocalTask() {
934 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
935     public Integer realCompute() {
936 jsr166 1.7 FibTask g = new FibTask(9);
937 jsr166 1.19 assertSame(g, g.fork());
938 jsr166 1.7 FibTask f = new FibTask(8);
939 jsr166 1.19 assertSame(f, f.fork());
940     assertSame(f, pollNextLocalTask());
941 jsr166 1.7 helpQuiesce();
942 jsr166 1.20 checkNotDone(f);
943     checkCompletedNormally(g, 34);
944 jsr166 1.7 return NoResult;
945 jsr166 1.19 }};
946 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(singletonPool(), a));
947 dl 1.1 }
948    
949 jsr166 1.2 /**
950 jsr166 1.13 * pollTask returns an unexecuted task without executing it
951 dl 1.1 */
952     public void testPollTask() {
953 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
954     public Integer realCompute() {
955 jsr166 1.7 FibTask g = new FibTask(9);
956 jsr166 1.19 assertSame(g, g.fork());
957 jsr166 1.7 FibTask f = new FibTask(8);
958 jsr166 1.19 assertSame(f, f.fork());
959     assertSame(f, pollTask());
960 jsr166 1.7 helpQuiesce();
961 jsr166 1.20 checkNotDone(f);
962     checkCompletedNormally(g, 34);
963 jsr166 1.7 return NoResult;
964 jsr166 1.19 }};
965 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(singletonPool(), a));
966 dl 1.1 }
967    
968 jsr166 1.2 /**
969 dl 1.1 * peekNextLocalTask returns least recent unexecuted task in async mode
970     */
971     public void testPeekNextLocalTaskAsync() {
972 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
973     public Integer realCompute() {
974 jsr166 1.7 FibTask g = new FibTask(9);
975 jsr166 1.19 assertSame(g, g.fork());
976 jsr166 1.7 FibTask f = new FibTask(8);
977 jsr166 1.19 assertSame(f, f.fork());
978     assertSame(g, peekNextLocalTask());
979     assertEquals(21, (int) f.join());
980 jsr166 1.7 helpQuiesce();
981 jsr166 1.20 checkCompletedNormally(f, 21);
982     checkCompletedNormally(g, 34);
983 jsr166 1.7 return NoResult;
984 jsr166 1.19 }};
985 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a));
986 dl 1.1 }
987    
988 jsr166 1.2 /**
989 jsr166 1.20 * pollNextLocalTask returns least recent unexecuted task without
990     * executing it, in async mode
991 dl 1.1 */
992     public void testPollNextLocalTaskAsync() {
993 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
994     public Integer realCompute() {
995 jsr166 1.7 FibTask g = new FibTask(9);
996 jsr166 1.19 assertSame(g, g.fork());
997 jsr166 1.7 FibTask f = new FibTask(8);
998 jsr166 1.19 assertSame(f, f.fork());
999     assertSame(g, pollNextLocalTask());
1000 jsr166 1.7 helpQuiesce();
1001 jsr166 1.20 checkCompletedNormally(f, 21);
1002     checkNotDone(g);
1003 jsr166 1.7 return NoResult;
1004 jsr166 1.19 }};
1005 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a));
1006 dl 1.1 }
1007    
1008 jsr166 1.2 /**
1009 jsr166 1.20 * pollTask returns an unexecuted task without executing it, in
1010     * async mode
1011 dl 1.1 */
1012     public void testPollTaskAsync() {
1013 jsr166 1.19 RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
1014     public Integer realCompute() {
1015 jsr166 1.7 FibTask g = new FibTask(9);
1016 jsr166 1.19 assertSame(g, g.fork());
1017 jsr166 1.7 FibTask f = new FibTask(8);
1018 jsr166 1.19 assertSame(f, f.fork());
1019     assertSame(g, pollTask());
1020 jsr166 1.7 helpQuiesce();
1021 jsr166 1.20 checkCompletedNormally(f, 21);
1022     checkNotDone(g);
1023 jsr166 1.7 return NoResult;
1024 jsr166 1.19 }};
1025 jsr166 1.13 assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a));
1026 dl 1.1 }
1027    
1028     }