ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveTaskTest.java
Revision: 1.36
Committed: Mon May 29 19:15:02 2017 UTC (6 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.35: +9 -11 lines
Log Message:
more timeout handling rework; remove most uses of SMALL_DELAY_MS; randomize timeouts and TimeUnits; remove hardcoded 5 second timeouts

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