1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
import junit.framework.*; |
8 |
|
import java.util.concurrent.CancellationException; |
9 |
|
import java.util.concurrent.ExecutionException; |
10 |
|
import java.util.concurrent.ForkJoinPool; |
11 |
+ |
import java.util.concurrent.ForkJoinTask; |
12 |
|
import java.util.concurrent.RecursiveTask; |
13 |
< |
import java.util.concurrent.TimeUnit; |
13 |
> |
import java.util.concurrent.TimeoutException; |
14 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
15 |
|
import java.util.HashSet; |
16 |
|
|
17 |
|
public class RecursiveTaskTest extends JSR166TestCase { |
39 |
|
|
40 |
|
private <T> T testInvokeOnPool(ForkJoinPool pool, RecursiveTask<T> a) { |
41 |
|
try { |
42 |
< |
assertFalse(a.isDone()); |
41 |
< |
assertFalse(a.isCompletedNormally()); |
42 |
< |
assertFalse(a.isCompletedAbnormally()); |
43 |
< |
assertFalse(a.isCancelled()); |
42 |
> |
checkNotDone(a); |
43 |
|
|
44 |
|
T result = pool.invoke(a); |
45 |
|
|
46 |
< |
assertTrue(a.isDone()); |
48 |
< |
assertTrue(a.isCompletedNormally()); |
49 |
< |
assertFalse(a.isCompletedAbnormally()); |
50 |
< |
assertFalse(a.isCancelled()); |
51 |
< |
assertNull(a.getException()); |
46 |
> |
checkCompletedNormally(a, result); |
47 |
|
return result; |
48 |
|
} finally { |
49 |
|
joinPool(pool); |
50 |
|
} |
51 |
|
} |
52 |
|
|
53 |
< |
static final class FJException extends RuntimeException { |
54 |
< |
FJException() { super(); } |
53 |
> |
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 |
> |
if (! ForkJoinTask.inForkJoinPool()) { |
62 |
> |
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 |
> |
assertFalse(a.cancel(false)); |
93 |
> |
assertFalse(a.cancel(true)); |
94 |
> |
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 |
> |
void checkCompletedAbnormally(RecursiveTask a, Throwable t) { |
150 |
> |
assertTrue(a.isDone()); |
151 |
> |
assertFalse(a.isCancelled()); |
152 |
> |
assertFalse(a.isCompletedNormally()); |
153 |
> |
assertTrue(a.isCompletedAbnormally()); |
154 |
> |
assertSame(t.getClass(), a.getException().getClass()); |
155 |
> |
assertNull(a.getRawResult()); |
156 |
> |
assertFalse(a.cancel(false)); |
157 |
> |
assertFalse(a.cancel(true)); |
158 |
> |
|
159 |
> |
try { |
160 |
> |
a.join(); |
161 |
> |
shouldThrow(); |
162 |
> |
} catch (Throwable expected) { |
163 |
> |
assertSame(t.getClass(), expected.getClass()); |
164 |
> |
} |
165 |
> |
|
166 |
> |
try { |
167 |
> |
a.get(); |
168 |
> |
shouldThrow(); |
169 |
> |
} catch (ExecutionException success) { |
170 |
> |
assertSame(t.getClass(), success.getCause().getClass()); |
171 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
172 |
> |
|
173 |
> |
try { |
174 |
> |
a.get(5L, SECONDS); |
175 |
> |
shouldThrow(); |
176 |
> |
} catch (ExecutionException success) { |
177 |
> |
assertSame(t.getClass(), success.getCause().getClass()); |
178 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
179 |
> |
} |
180 |
> |
|
181 |
> |
public static final class FJException extends RuntimeException { |
182 |
> |
public FJException() { super(); } |
183 |
|
} |
184 |
|
|
185 |
|
// An invalid return value for Fib |
197 |
|
f1.fork(); |
198 |
|
return (new FibTask(n - 2)).compute() + f1.join(); |
199 |
|
} |
200 |
+ |
|
201 |
+ |
public void publicSetRawResult(Integer result) { |
202 |
+ |
setRawResult(result); |
203 |
+ |
} |
204 |
|
} |
205 |
|
|
206 |
|
// A recursive action failing in base case |
230 |
|
FibTask f = new FibTask(8); |
231 |
|
Integer r = f.invoke(); |
232 |
|
assertEquals(21, (int) r); |
233 |
< |
assertTrue(f.isDone()); |
107 |
< |
assertFalse(f.isCancelled()); |
108 |
< |
assertFalse(f.isCompletedAbnormally()); |
109 |
< |
assertEquals(21, (int) f.getRawResult()); |
233 |
> |
checkCompletedNormally(f, r); |
234 |
|
return r; |
235 |
|
}}; |
236 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
246 |
|
public Integer realCompute() { |
247 |
|
FibTask f = new FibTask(8); |
248 |
|
f.quietlyInvoke(); |
249 |
< |
Integer r = f.getRawResult(); |
250 |
< |
assertEquals(21, (int) r); |
127 |
< |
assertTrue(f.isDone()); |
128 |
< |
assertFalse(f.isCancelled()); |
129 |
< |
assertFalse(f.isCompletedAbnormally()); |
130 |
< |
return r; |
249 |
> |
checkCompletedNormally(f, 21); |
250 |
> |
return NoResult; |
251 |
|
}}; |
252 |
< |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
252 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
253 |
|
} |
254 |
|
|
255 |
|
/** |
262 |
|
assertSame(f, f.fork()); |
263 |
|
Integer r = f.join(); |
264 |
|
assertEquals(21, (int) r); |
265 |
< |
assertTrue(f.isDone()); |
265 |
> |
checkCompletedNormally(f, r); |
266 |
|
return r; |
267 |
|
}}; |
268 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
278 |
|
assertSame(f, f.fork()); |
279 |
|
Integer r = f.get(); |
280 |
|
assertEquals(21, (int) r); |
281 |
< |
assertTrue(f.isDone()); |
281 |
> |
checkCompletedNormally(f, r); |
282 |
|
return r; |
283 |
|
}}; |
284 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
292 |
|
public Integer realCompute() throws Exception { |
293 |
|
FibTask f = new FibTask(8); |
294 |
|
assertSame(f, f.fork()); |
295 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
295 |
> |
Integer r = f.get(5L, SECONDS); |
296 |
|
assertEquals(21, (int) r); |
297 |
< |
assertTrue(f.isDone()); |
297 |
> |
checkCompletedNormally(f, r); |
298 |
|
return r; |
299 |
|
}}; |
300 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
311 |
|
f.quietlyJoin(); |
312 |
|
Integer r = f.getRawResult(); |
313 |
|
assertEquals(21, (int) r); |
314 |
< |
assertTrue(f.isDone()); |
314 |
> |
checkCompletedNormally(f, r); |
315 |
|
return r; |
316 |
|
}}; |
317 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
318 |
|
} |
319 |
|
|
200 |
– |
|
320 |
|
/** |
321 |
|
* helpQuiesce returns when tasks are complete. |
322 |
|
* getQueuedTaskCount returns 0 when quiescent |
326 |
|
public Integer realCompute() { |
327 |
|
FibTask f = new FibTask(8); |
328 |
|
assertSame(f, f.fork()); |
329 |
< |
f.helpQuiesce(); |
211 |
< |
Integer r = f.getRawResult(); |
212 |
< |
assertEquals(21, (int) r); |
213 |
< |
assertTrue(f.isDone()); |
329 |
> |
helpQuiesce(); |
330 |
|
assertEquals(0, getQueuedTaskCount()); |
331 |
< |
return r; |
331 |
> |
checkCompletedNormally(f, 21); |
332 |
> |
return NoResult; |
333 |
|
}}; |
334 |
< |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
334 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
335 |
|
} |
336 |
|
|
220 |
– |
|
337 |
|
/** |
338 |
|
* invoke task throws exception when task completes abnormally |
339 |
|
*/ |
344 |
|
try { |
345 |
|
f.invoke(); |
346 |
|
shouldThrow(); |
347 |
< |
return NoResult; |
348 |
< |
} catch (FJException success) {} |
347 |
> |
} catch (FJException success) { |
348 |
> |
checkCompletedAbnormally(f, success); |
349 |
> |
} |
350 |
|
return NoResult; |
351 |
|
}}; |
352 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
360 |
|
public Integer realCompute() { |
361 |
|
FailingFibTask f = new FailingFibTask(8); |
362 |
|
f.quietlyInvoke(); |
363 |
< |
assertTrue(f.isDone()); |
363 |
> |
assertTrue(f.getException() instanceof FJException); |
364 |
> |
checkCompletedAbnormally(f, f.getException()); |
365 |
|
return NoResult; |
366 |
|
}}; |
367 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
378 |
|
try { |
379 |
|
Integer r = f.join(); |
380 |
|
shouldThrow(); |
381 |
< |
return r; |
382 |
< |
} catch (FJException success) {} |
381 |
> |
} catch (FJException success) { |
382 |
> |
checkCompletedAbnormally(f, success); |
383 |
> |
} |
384 |
|
return NoResult; |
385 |
|
}}; |
386 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
397 |
|
try { |
398 |
|
Integer r = f.get(); |
399 |
|
shouldThrow(); |
400 |
< |
return r; |
401 |
< |
} catch (ExecutionException success) {} |
400 |
> |
} catch (ExecutionException success) { |
401 |
> |
Throwable cause = success.getCause(); |
402 |
> |
assertTrue(cause instanceof FJException); |
403 |
> |
checkCompletedAbnormally(f, cause); |
404 |
> |
} |
405 |
|
return NoResult; |
406 |
|
}}; |
407 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
416 |
|
FailingFibTask f = new FailingFibTask(8); |
417 |
|
assertSame(f, f.fork()); |
418 |
|
try { |
419 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
419 |
> |
Integer r = f.get(5L, SECONDS); |
420 |
|
shouldThrow(); |
421 |
< |
return r; |
422 |
< |
} catch (ExecutionException success) {} |
421 |
> |
} catch (ExecutionException success) { |
422 |
> |
Throwable cause = success.getCause(); |
423 |
> |
assertTrue(cause instanceof FJException); |
424 |
> |
checkCompletedAbnormally(f, cause); |
425 |
> |
} |
426 |
|
return NoResult; |
427 |
|
}}; |
428 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
437 |
|
FailingFibTask f = new FailingFibTask(8); |
438 |
|
assertSame(f, f.fork()); |
439 |
|
f.quietlyJoin(); |
315 |
– |
assertTrue(f.isDone()); |
316 |
– |
assertTrue(f.isCompletedAbnormally()); |
440 |
|
assertTrue(f.getException() instanceof FJException); |
441 |
+ |
checkCompletedAbnormally(f, f.getException()); |
442 |
|
return NoResult; |
443 |
|
}}; |
444 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
455 |
|
try { |
456 |
|
Integer r = f.invoke(); |
457 |
|
shouldThrow(); |
458 |
< |
return r; |
459 |
< |
} catch (CancellationException success) {} |
458 |
> |
} catch (CancellationException success) { |
459 |
> |
checkCancelled(f); |
460 |
> |
} |
461 |
|
return NoResult; |
462 |
|
}}; |
463 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
475 |
|
try { |
476 |
|
Integer r = f.join(); |
477 |
|
shouldThrow(); |
478 |
< |
return r; |
479 |
< |
} catch (CancellationException success) {} |
478 |
> |
} catch (CancellationException success) { |
479 |
> |
checkCancelled(f); |
480 |
> |
} |
481 |
|
return NoResult; |
482 |
|
}}; |
483 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
495 |
|
try { |
496 |
|
Integer r = f.get(); |
497 |
|
shouldThrow(); |
498 |
< |
return r; |
499 |
< |
} catch (CancellationException success) {} |
498 |
> |
} catch (CancellationException success) { |
499 |
> |
checkCancelled(f); |
500 |
> |
} |
501 |
|
return NoResult; |
502 |
|
}}; |
503 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
513 |
|
assertTrue(f.cancel(true)); |
514 |
|
assertSame(f, f.fork()); |
515 |
|
try { |
516 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
516 |
> |
Integer r = f.get(5L, SECONDS); |
517 |
|
shouldThrow(); |
518 |
< |
return r; |
519 |
< |
} catch (CancellationException success) {} |
518 |
> |
} catch (CancellationException success) { |
519 |
> |
checkCancelled(f); |
520 |
> |
} |
521 |
|
return NoResult; |
522 |
|
}}; |
523 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
533 |
|
assertTrue(f.cancel(true)); |
534 |
|
assertSame(f, f.fork()); |
535 |
|
f.quietlyJoin(); |
536 |
< |
assertTrue(f.isDone()); |
409 |
< |
assertTrue(f.isCompletedAbnormally()); |
410 |
< |
assertTrue(f.isCancelled()); |
411 |
< |
assertTrue(f.getException() instanceof CancellationException); |
536 |
> |
checkCancelled(f); |
537 |
|
return NoResult; |
538 |
|
}}; |
539 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
582 |
|
public void testInForkJoinPool2() { |
583 |
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
584 |
|
public Integer realCompute() { |
585 |
< |
assertTrue(!inForkJoinPool()); |
585 |
> |
assertFalse(inForkJoinPool()); |
586 |
|
return NoResult; |
587 |
|
}}; |
588 |
|
assertSame(NoResult, a.invoke()); |
599 |
|
return NoResult; |
600 |
|
} |
601 |
|
}; |
602 |
< |
a.invoke(); |
602 |
> |
assertSame(NoResult, a.invoke()); |
603 |
|
} |
604 |
|
|
605 |
|
/** |
606 |
< |
* A reinitialized task may be re-invoked |
606 |
> |
* A reinitialized normally completed task may be re-invoked |
607 |
|
*/ |
608 |
|
public void testReinitialize() { |
609 |
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
610 |
|
public Integer realCompute() { |
611 |
|
FibTask f = new FibTask(8); |
612 |
< |
Integer r = f.invoke(); |
613 |
< |
assertEquals(21, (int) r); |
614 |
< |
assertTrue(f.isDone()); |
615 |
< |
assertFalse(f.isCancelled()); |
616 |
< |
assertFalse(f.isCompletedAbnormally()); |
617 |
< |
f.reinitialize(); |
618 |
< |
r = f.invoke(); |
619 |
< |
assertEquals(21, (int) r); |
612 |
> |
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 |
> |
checkCompletedAbnormally(f, success); |
642 |
> |
} |
643 |
> |
f.reinitialize(); |
644 |
> |
checkNotDone(f); |
645 |
> |
} |
646 |
|
return NoResult; |
647 |
|
}}; |
648 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
659 |
|
try { |
660 |
|
Integer r = f.invoke(); |
661 |
|
shouldThrow(); |
662 |
< |
return r; |
663 |
< |
} catch (FJException success) {} |
662 |
> |
} catch (FJException success) { |
663 |
> |
checkCompletedAbnormally(f, success); |
664 |
> |
} |
665 |
|
return NoResult; |
666 |
|
}}; |
667 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
676 |
|
FibTask f = new FibTask(8); |
677 |
|
f.complete(NoResult); |
678 |
|
Integer r = f.invoke(); |
527 |
– |
assertTrue(f.isDone()); |
679 |
|
assertSame(NoResult, r); |
680 |
+ |
checkCompletedNormally(f, NoResult); |
681 |
|
return r; |
682 |
|
}}; |
683 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
692 |
|
FibTask f = new FibTask(8); |
693 |
|
FibTask g = new FibTask(9); |
694 |
|
invokeAll(f, g); |
695 |
< |
assertTrue(f.isDone()); |
696 |
< |
assertEquals(21, (int) f.join()); |
545 |
< |
assertTrue(g.isDone()); |
546 |
< |
assertEquals(34, (int) g.join()); |
695 |
> |
checkCompletedNormally(f, 21); |
696 |
> |
checkCompletedNormally(g, 34); |
697 |
|
return NoResult; |
698 |
|
}}; |
699 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
707 |
|
public Integer realCompute() { |
708 |
|
FibTask f = new FibTask(8); |
709 |
|
invokeAll(f); |
710 |
< |
assertTrue(f.isDone()); |
561 |
< |
assertEquals(21, (int) f.join()); |
710 |
> |
checkCompletedNormally(f, 21); |
711 |
|
return NoResult; |
712 |
|
}}; |
713 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
724 |
|
FibTask h = new FibTask(7); |
725 |
|
invokeAll(f, g, h); |
726 |
|
assertTrue(f.isDone()); |
578 |
– |
assertEquals(21, (int) f.join()); |
727 |
|
assertTrue(g.isDone()); |
580 |
– |
assertEquals(34, (int) g.join()); |
728 |
|
assertTrue(h.isDone()); |
729 |
< |
assertEquals(13, (int) h.join()); |
729 |
> |
checkCompletedNormally(f, 21); |
730 |
> |
checkCompletedNormally(g, 34); |
731 |
> |
checkCompletedNormally(h, 13); |
732 |
|
return NoResult; |
733 |
|
}}; |
734 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
749 |
|
set.add(h); |
750 |
|
invokeAll(set); |
751 |
|
assertTrue(f.isDone()); |
603 |
– |
assertEquals(21, (int) f.join()); |
752 |
|
assertTrue(g.isDone()); |
605 |
– |
assertEquals(34, (int) g.join()); |
753 |
|
assertTrue(h.isDone()); |
754 |
< |
assertEquals(13, (int) h.join()); |
754 |
> |
checkCompletedNormally(f, 21); |
755 |
> |
checkCompletedNormally(g, 34); |
756 |
> |
checkCompletedNormally(h, 13); |
757 |
|
return NoResult; |
758 |
|
}}; |
759 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
760 |
|
} |
761 |
|
|
762 |
+ |
/** |
763 |
+ |
* 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 |
|
* invokeAll(t1, t2) throw exception if any task does |
788 |
|
try { |
789 |
|
invokeAll(f, g); |
790 |
|
shouldThrow(); |
791 |
< |
return NoResult; |
792 |
< |
} catch (FJException success) {} |
791 |
> |
} catch (FJException success) { |
792 |
> |
checkCompletedAbnormally(g, success); |
793 |
> |
} |
794 |
|
return NoResult; |
795 |
|
}}; |
796 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
806 |
|
try { |
807 |
|
invokeAll(g); |
808 |
|
shouldThrow(); |
809 |
< |
return NoResult; |
810 |
< |
} catch (FJException success) {} |
809 |
> |
} catch (FJException success) { |
810 |
> |
checkCompletedAbnormally(g, success); |
811 |
> |
} |
812 |
|
return NoResult; |
813 |
|
}}; |
814 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
826 |
|
try { |
827 |
|
invokeAll(f, g, h); |
828 |
|
shouldThrow(); |
829 |
< |
return NoResult; |
830 |
< |
} catch (FJException success) {} |
829 |
> |
} catch (FJException success) { |
830 |
> |
checkCompletedAbnormally(g, success); |
831 |
> |
} |
832 |
|
return NoResult; |
833 |
|
}}; |
834 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
850 |
|
try { |
851 |
|
invokeAll(set); |
852 |
|
shouldThrow(); |
853 |
< |
return NoResult; |
854 |
< |
} catch (FJException success) {} |
853 |
> |
} catch (FJException success) { |
854 |
> |
checkCompletedAbnormally(f, success); |
855 |
> |
} |
856 |
|
return NoResult; |
857 |
|
}}; |
858 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
871 |
|
assertSame(f, f.fork()); |
872 |
|
assertTrue(f.tryUnfork()); |
873 |
|
helpQuiesce(); |
874 |
< |
assertFalse(f.isDone()); |
875 |
< |
assertTrue(g.isDone()); |
874 |
> |
checkNotDone(f); |
875 |
> |
checkCompletedNormally(g, 34); |
876 |
|
return NoResult; |
877 |
|
}}; |
878 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
893 |
|
assertSame(f, f.fork()); |
894 |
|
assertTrue(getSurplusQueuedTaskCount() > 0); |
895 |
|
helpQuiesce(); |
896 |
+ |
assertEquals(0, getSurplusQueuedTaskCount()); |
897 |
+ |
checkCompletedNormally(f, 21); |
898 |
+ |
checkCompletedNormally(g, 34); |
899 |
+ |
checkCompletedNormally(h, 13); |
900 |
|
return NoResult; |
901 |
|
}}; |
902 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
913 |
|
FibTask f = new FibTask(8); |
914 |
|
assertSame(f, f.fork()); |
915 |
|
assertSame(f, peekNextLocalTask()); |
916 |
< |
assertEquals(21, (int) f.join()); |
743 |
< |
assertTrue(f.isDone()); |
916 |
> |
checkCompletesNormally(f, 21); |
917 |
|
helpQuiesce(); |
918 |
+ |
checkCompletedNormally(g, 34); |
919 |
|
return NoResult; |
920 |
|
}}; |
921 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
934 |
|
assertSame(f, f.fork()); |
935 |
|
assertSame(f, pollNextLocalTask()); |
936 |
|
helpQuiesce(); |
937 |
< |
assertFalse(f.isDone()); |
937 |
> |
checkNotDone(f); |
938 |
> |
checkCompletedNormally(g, 34); |
939 |
|
return NoResult; |
940 |
|
}}; |
941 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
953 |
|
assertSame(f, f.fork()); |
954 |
|
assertSame(f, pollTask()); |
955 |
|
helpQuiesce(); |
956 |
< |
assertFalse(f.isDone()); |
957 |
< |
assertTrue(g.isDone()); |
956 |
> |
checkNotDone(f); |
957 |
> |
checkCompletedNormally(g, 34); |
958 |
|
return NoResult; |
959 |
|
}}; |
960 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
973 |
|
assertSame(g, peekNextLocalTask()); |
974 |
|
assertEquals(21, (int) f.join()); |
975 |
|
helpQuiesce(); |
976 |
< |
assertTrue(f.isDone()); |
976 |
> |
checkCompletedNormally(f, 21); |
977 |
> |
checkCompletedNormally(g, 34); |
978 |
|
return NoResult; |
979 |
|
}}; |
980 |
|
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
981 |
|
} |
982 |
|
|
983 |
|
/** |
984 |
< |
* pollNextLocalTask returns least recent unexecuted task |
985 |
< |
* without executing it, in async mode |
984 |
> |
* pollNextLocalTask returns least recent unexecuted task without |
985 |
> |
* executing it, in async mode |
986 |
|
*/ |
987 |
|
public void testPollNextLocalTaskAsync() { |
988 |
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
993 |
|
assertSame(f, f.fork()); |
994 |
|
assertSame(g, pollNextLocalTask()); |
995 |
|
helpQuiesce(); |
996 |
< |
assertTrue(f.isDone()); |
997 |
< |
assertFalse(g.isDone()); |
996 |
> |
checkCompletedNormally(f, 21); |
997 |
> |
checkNotDone(g); |
998 |
|
return NoResult; |
999 |
|
}}; |
1000 |
|
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
1001 |
|
} |
1002 |
|
|
1003 |
|
/** |
1004 |
< |
* pollTask returns an unexecuted task |
1005 |
< |
* without executing it, in async mode |
1004 |
> |
* pollTask returns an unexecuted task without executing it, in |
1005 |
> |
* async mode |
1006 |
|
*/ |
1007 |
|
public void testPollTaskAsync() { |
1008 |
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
1013 |
|
assertSame(f, f.fork()); |
1014 |
|
assertSame(g, pollTask()); |
1015 |
|
helpQuiesce(); |
1016 |
< |
assertTrue(f.isDone()); |
1017 |
< |
assertFalse(g.isDone()); |
1016 |
> |
checkCompletedNormally(f, 21); |
1017 |
> |
checkNotDone(g); |
1018 |
|
return NoResult; |
1019 |
|
}}; |
1020 |
|
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |