ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveTaskTest.java
Revision: 1.30
Committed: Wed Dec 31 16:44:02 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.29: +0 -2 lines
Log Message:
remove unused imports

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