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