8 |
|
import java.util.concurrent.CancellationException; |
9 |
|
import java.util.concurrent.ExecutionException; |
10 |
|
import java.util.concurrent.ForkJoinPool; |
11 |
+ |
import java.util.concurrent.ForkJoinWorkerThread; |
12 |
|
import java.util.concurrent.RecursiveTask; |
13 |
|
import java.util.concurrent.TimeUnit; |
14 |
+ |
import java.util.concurrent.TimeoutException; |
15 |
+ |
import static java.util.concurrent.TimeUnit.SECONDS; |
16 |
|
import java.util.HashSet; |
17 |
|
|
18 |
|
public class RecursiveTaskTest extends JSR166TestCase { |
40 |
|
|
41 |
|
private <T> T testInvokeOnPool(ForkJoinPool pool, RecursiveTask<T> a) { |
42 |
|
try { |
43 |
< |
assertFalse(a.isDone()); |
41 |
< |
assertFalse(a.isCompletedNormally()); |
42 |
< |
assertFalse(a.isCompletedAbnormally()); |
43 |
< |
assertFalse(a.isCancelled()); |
43 |
> |
checkNotDone(a); |
44 |
|
|
45 |
|
T result = pool.invoke(a); |
46 |
|
|
47 |
< |
assertTrue(a.isDone()); |
48 |
< |
assertTrue(a.isCompletedNormally()); |
49 |
< |
assertFalse(a.isCompletedAbnormally()); |
50 |
< |
assertFalse(a.isCancelled()); |
51 |
< |
assertNull(a.getException()); |
47 |
> |
checkCompletedNormally(a, result); |
48 |
|
return result; |
49 |
|
} finally { |
50 |
|
joinPool(pool); |
51 |
|
} |
52 |
|
} |
53 |
|
|
54 |
+ |
void checkNotDone(RecursiveTask a) { |
55 |
+ |
assertFalse(a.isDone()); |
56 |
+ |
assertFalse(a.isCompletedNormally()); |
57 |
+ |
assertFalse(a.isCompletedAbnormally()); |
58 |
+ |
assertFalse(a.isCancelled()); |
59 |
+ |
assertNull(a.getException()); |
60 |
+ |
assertNull(a.getRawResult()); |
61 |
+ |
|
62 |
+ |
if (! (Thread.currentThread() instanceof ForkJoinWorkerThread)) { |
63 |
+ |
Thread.currentThread().interrupt(); |
64 |
+ |
try { |
65 |
+ |
a.get(); |
66 |
+ |
shouldThrow(); |
67 |
+ |
} catch (InterruptedException success) { |
68 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
69 |
+ |
|
70 |
+ |
Thread.currentThread().interrupt(); |
71 |
+ |
try { |
72 |
+ |
a.get(5L, SECONDS); |
73 |
+ |
shouldThrow(); |
74 |
+ |
} catch (InterruptedException success) { |
75 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
76 |
+ |
} |
77 |
+ |
|
78 |
+ |
try { |
79 |
+ |
a.get(0L, SECONDS); |
80 |
+ |
shouldThrow(); |
81 |
+ |
} catch (TimeoutException success) { |
82 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
83 |
+ |
} |
84 |
+ |
|
85 |
+ |
<T> void checkCompletedNormally(RecursiveTask<T> a, T expected) { |
86 |
+ |
assertTrue(a.isDone()); |
87 |
+ |
assertFalse(a.isCancelled()); |
88 |
+ |
assertTrue(a.isCompletedNormally()); |
89 |
+ |
assertFalse(a.isCompletedAbnormally()); |
90 |
+ |
assertNull(a.getException()); |
91 |
+ |
assertSame(expected, a.getRawResult()); |
92 |
+ |
assertSame(expected, a.join()); |
93 |
+ |
try { |
94 |
+ |
assertSame(expected, a.get()); |
95 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
96 |
+ |
try { |
97 |
+ |
assertSame(expected, a.get(5L, SECONDS)); |
98 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
99 |
+ |
} |
100 |
+ |
|
101 |
+ |
/** |
102 |
+ |
* Waits for the task to complete, and checks that when it does, |
103 |
+ |
* it will have an Integer result equals to the given int. |
104 |
+ |
*/ |
105 |
+ |
void checkCompletesNormally(RecursiveTask<Integer> a, int expected) { |
106 |
+ |
Integer r = a.join(); |
107 |
+ |
assertEquals(expected, (int) r); |
108 |
+ |
checkCompletedNormally(a, r); |
109 |
+ |
} |
110 |
+ |
|
111 |
+ |
/** |
112 |
+ |
* Like checkCompletesNormally, but verifies that the task has |
113 |
+ |
* already completed. |
114 |
+ |
*/ |
115 |
+ |
void checkCompletedNormally(RecursiveTask<Integer> a, int expected) { |
116 |
+ |
Integer r = a.getRawResult(); |
117 |
+ |
assertEquals(expected, (int) r); |
118 |
+ |
checkCompletedNormally(a, r); |
119 |
+ |
} |
120 |
+ |
|
121 |
+ |
void checkCancelled(RecursiveTask a) { |
122 |
+ |
assertTrue(a.isDone()); |
123 |
+ |
assertTrue(a.isCancelled()); |
124 |
+ |
assertFalse(a.isCompletedNormally()); |
125 |
+ |
assertTrue(a.isCompletedAbnormally()); |
126 |
+ |
assertTrue(a.getException() instanceof CancellationException); |
127 |
+ |
assertNull(a.getRawResult()); |
128 |
+ |
|
129 |
+ |
try { |
130 |
+ |
a.join(); |
131 |
+ |
shouldThrow(); |
132 |
+ |
} catch (CancellationException success) { |
133 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
134 |
+ |
|
135 |
+ |
try { |
136 |
+ |
a.get(); |
137 |
+ |
shouldThrow(); |
138 |
+ |
} catch (CancellationException success) { |
139 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
140 |
+ |
|
141 |
+ |
try { |
142 |
+ |
a.get(5L, SECONDS); |
143 |
+ |
shouldThrow(); |
144 |
+ |
} catch (CancellationException success) { |
145 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
146 |
+ |
} |
147 |
+ |
|
148 |
+ |
void checkTaskThrew(RecursiveTask a, Throwable t) { |
149 |
+ |
assertTrue(a.isDone()); |
150 |
+ |
assertFalse(a.isCancelled()); |
151 |
+ |
assertFalse(a.isCompletedNormally()); |
152 |
+ |
assertTrue(a.isCompletedAbnormally()); |
153 |
+ |
assertSame(t, a.getException()); |
154 |
+ |
assertNull(a.getRawResult()); |
155 |
+ |
|
156 |
+ |
try { |
157 |
+ |
a.join(); |
158 |
+ |
shouldThrow(); |
159 |
+ |
} catch (Throwable expected) { |
160 |
+ |
assertSame(t, expected); |
161 |
+ |
} |
162 |
+ |
|
163 |
+ |
try { |
164 |
+ |
a.get(); |
165 |
+ |
shouldThrow(); |
166 |
+ |
} catch (ExecutionException success) { |
167 |
+ |
assertSame(t, success.getCause()); |
168 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
169 |
+ |
|
170 |
+ |
try { |
171 |
+ |
a.get(5L, SECONDS); |
172 |
+ |
shouldThrow(); |
173 |
+ |
} catch (ExecutionException success) { |
174 |
+ |
assertSame(t, success.getCause()); |
175 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
176 |
+ |
} |
177 |
+ |
|
178 |
|
static final class FJException extends RuntimeException { |
179 |
|
FJException() { super(); } |
180 |
|
} |
194 |
|
f1.fork(); |
195 |
|
return (new FibTask(n - 2)).compute() + f1.join(); |
196 |
|
} |
197 |
+ |
|
198 |
+ |
public void publicSetRawResult(Integer result) { |
199 |
+ |
setRawResult(result); |
200 |
+ |
} |
201 |
|
} |
202 |
|
|
203 |
|
// A recursive action failing in base case |
227 |
|
FibTask f = new FibTask(8); |
228 |
|
Integer r = f.invoke(); |
229 |
|
assertEquals(21, (int) r); |
230 |
< |
assertTrue(f.isDone()); |
107 |
< |
assertFalse(f.isCancelled()); |
108 |
< |
assertFalse(f.isCompletedAbnormally()); |
109 |
< |
assertEquals(21, (int) f.getRawResult()); |
230 |
> |
checkCompletedNormally(f, r); |
231 |
|
return r; |
232 |
|
}}; |
233 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
243 |
|
public Integer realCompute() { |
244 |
|
FibTask f = new FibTask(8); |
245 |
|
f.quietlyInvoke(); |
246 |
< |
Integer r = f.getRawResult(); |
247 |
< |
assertEquals(21, (int) r); |
127 |
< |
assertTrue(f.isDone()); |
128 |
< |
assertFalse(f.isCancelled()); |
129 |
< |
assertFalse(f.isCompletedAbnormally()); |
130 |
< |
return r; |
246 |
> |
checkCompletedNormally(f, 21); |
247 |
> |
return NoResult; |
248 |
|
}}; |
249 |
< |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
249 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
250 |
|
} |
251 |
|
|
252 |
|
/** |
259 |
|
assertSame(f, f.fork()); |
260 |
|
Integer r = f.join(); |
261 |
|
assertEquals(21, (int) r); |
262 |
< |
assertTrue(f.isDone()); |
262 |
> |
checkCompletedNormally(f, r); |
263 |
|
return r; |
264 |
|
}}; |
265 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
275 |
|
assertSame(f, f.fork()); |
276 |
|
Integer r = f.get(); |
277 |
|
assertEquals(21, (int) r); |
278 |
< |
assertTrue(f.isDone()); |
278 |
> |
checkCompletedNormally(f, r); |
279 |
|
return r; |
280 |
|
}}; |
281 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
289 |
|
public Integer realCompute() throws Exception { |
290 |
|
FibTask f = new FibTask(8); |
291 |
|
assertSame(f, f.fork()); |
292 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
292 |
> |
Integer r = f.get(5L, SECONDS); |
293 |
|
assertEquals(21, (int) r); |
294 |
< |
assertTrue(f.isDone()); |
294 |
> |
checkCompletedNormally(f, r); |
295 |
|
return r; |
296 |
|
}}; |
297 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
308 |
|
f.quietlyJoin(); |
309 |
|
Integer r = f.getRawResult(); |
310 |
|
assertEquals(21, (int) r); |
311 |
< |
assertTrue(f.isDone()); |
311 |
> |
checkCompletedNormally(f, r); |
312 |
|
return r; |
313 |
|
}}; |
314 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
325 |
|
FibTask f = new FibTask(8); |
326 |
|
assertSame(f, f.fork()); |
327 |
|
f.helpQuiesce(); |
211 |
– |
Integer r = f.getRawResult(); |
212 |
– |
assertEquals(21, (int) r); |
213 |
– |
assertTrue(f.isDone()); |
328 |
|
assertEquals(0, getQueuedTaskCount()); |
329 |
< |
return r; |
329 |
> |
checkCompletedNormally(f, 21); |
330 |
> |
return NoResult; |
331 |
|
}}; |
332 |
< |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
332 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
333 |
|
} |
334 |
|
|
335 |
|
|
343 |
|
try { |
344 |
|
f.invoke(); |
345 |
|
shouldThrow(); |
346 |
< |
return NoResult; |
347 |
< |
} catch (FJException success) {} |
346 |
> |
} catch (FJException success) { |
347 |
> |
checkTaskThrew(f, success); |
348 |
> |
} |
349 |
|
return NoResult; |
350 |
|
}}; |
351 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
359 |
|
public Integer realCompute() { |
360 |
|
FailingFibTask f = new FailingFibTask(8); |
361 |
|
f.quietlyInvoke(); |
362 |
< |
assertTrue(f.isDone()); |
362 |
> |
assertTrue(f.getException() instanceof FJException); |
363 |
> |
checkTaskThrew(f, f.getException()); |
364 |
|
return NoResult; |
365 |
|
}}; |
366 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
377 |
|
try { |
378 |
|
Integer r = f.join(); |
379 |
|
shouldThrow(); |
380 |
< |
return r; |
381 |
< |
} catch (FJException success) {} |
380 |
> |
} catch (FJException success) { |
381 |
> |
checkTaskThrew(f, success); |
382 |
> |
} |
383 |
|
return NoResult; |
384 |
|
}}; |
385 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
396 |
|
try { |
397 |
|
Integer r = f.get(); |
398 |
|
shouldThrow(); |
399 |
< |
return r; |
400 |
< |
} catch (ExecutionException success) {} |
399 |
> |
} catch (ExecutionException success) { |
400 |
> |
checkTaskThrew(f, success.getCause()); |
401 |
> |
} |
402 |
|
return NoResult; |
403 |
|
}}; |
404 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
413 |
|
FailingFibTask f = new FailingFibTask(8); |
414 |
|
assertSame(f, f.fork()); |
415 |
|
try { |
416 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
416 |
> |
Integer r = f.get(5L, SECONDS); |
417 |
|
shouldThrow(); |
418 |
< |
return r; |
419 |
< |
} catch (ExecutionException success) {} |
418 |
> |
} catch (ExecutionException success) { |
419 |
> |
checkTaskThrew(f, success.getCause()); |
420 |
> |
} |
421 |
|
return NoResult; |
422 |
|
}}; |
423 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
432 |
|
FailingFibTask f = new FailingFibTask(8); |
433 |
|
assertSame(f, f.fork()); |
434 |
|
f.quietlyJoin(); |
315 |
– |
assertTrue(f.isDone()); |
316 |
– |
assertTrue(f.isCompletedAbnormally()); |
435 |
|
assertTrue(f.getException() instanceof FJException); |
436 |
+ |
checkTaskThrew(f, f.getException()); |
437 |
|
return NoResult; |
438 |
|
}}; |
439 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
450 |
|
try { |
451 |
|
Integer r = f.invoke(); |
452 |
|
shouldThrow(); |
453 |
< |
return r; |
454 |
< |
} catch (CancellationException success) {} |
453 |
> |
} catch (CancellationException success) { |
454 |
> |
checkCancelled(f); |
455 |
> |
} |
456 |
|
return NoResult; |
457 |
|
}}; |
458 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
470 |
|
try { |
471 |
|
Integer r = f.join(); |
472 |
|
shouldThrow(); |
473 |
< |
return r; |
474 |
< |
} catch (CancellationException success) {} |
473 |
> |
} catch (CancellationException success) { |
474 |
> |
checkCancelled(f); |
475 |
> |
} |
476 |
|
return NoResult; |
477 |
|
}}; |
478 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
490 |
|
try { |
491 |
|
Integer r = f.get(); |
492 |
|
shouldThrow(); |
493 |
< |
return r; |
494 |
< |
} catch (CancellationException success) {} |
493 |
> |
} catch (CancellationException success) { |
494 |
> |
checkCancelled(f); |
495 |
> |
} |
496 |
|
return NoResult; |
497 |
|
}}; |
498 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
508 |
|
assertTrue(f.cancel(true)); |
509 |
|
assertSame(f, f.fork()); |
510 |
|
try { |
511 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
511 |
> |
Integer r = f.get(5L, SECONDS); |
512 |
|
shouldThrow(); |
513 |
< |
return r; |
514 |
< |
} catch (CancellationException success) {} |
513 |
> |
} catch (CancellationException success) { |
514 |
> |
checkCancelled(f); |
515 |
> |
} |
516 |
|
return NoResult; |
517 |
|
}}; |
518 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
528 |
|
assertTrue(f.cancel(true)); |
529 |
|
assertSame(f, f.fork()); |
530 |
|
f.quietlyJoin(); |
531 |
< |
assertTrue(f.isDone()); |
409 |
< |
assertTrue(f.isCompletedAbnormally()); |
410 |
< |
assertTrue(f.isCancelled()); |
411 |
< |
assertTrue(f.getException() instanceof CancellationException); |
531 |
> |
checkCancelled(f); |
532 |
|
return NoResult; |
533 |
|
}}; |
534 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
598 |
|
} |
599 |
|
|
600 |
|
/** |
601 |
< |
* A reinitialized task may be re-invoked |
601 |
> |
* A reinitialized normally completed task may be re-invoked |
602 |
|
*/ |
603 |
|
public void testReinitialize() { |
604 |
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
605 |
|
public Integer realCompute() { |
606 |
|
FibTask f = new FibTask(8); |
607 |
< |
Integer r = f.invoke(); |
608 |
< |
assertEquals(21, (int) r); |
609 |
< |
assertTrue(f.isDone()); |
610 |
< |
assertFalse(f.isCancelled()); |
611 |
< |
assertFalse(f.isCompletedAbnormally()); |
612 |
< |
f.reinitialize(); |
613 |
< |
r = f.invoke(); |
614 |
< |
assertEquals(21, (int) r); |
607 |
> |
checkNotDone(f); |
608 |
> |
|
609 |
> |
for (int i = 0; i < 3; i++) { |
610 |
> |
Integer r = f.invoke(); |
611 |
> |
assertEquals(21, (int) r); |
612 |
> |
checkCompletedNormally(f, r); |
613 |
> |
f.reinitialize(); |
614 |
> |
f.publicSetRawResult(null); |
615 |
> |
checkNotDone(f); |
616 |
> |
} |
617 |
> |
return NoResult; |
618 |
> |
}}; |
619 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
620 |
> |
} |
621 |
> |
|
622 |
> |
/** |
623 |
> |
* A reinitialized abnormally completed task may be re-invoked |
624 |
> |
*/ |
625 |
> |
public void testReinitializeAbnormal() { |
626 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
627 |
> |
public Integer realCompute() { |
628 |
> |
FailingFibTask f = new FailingFibTask(8); |
629 |
> |
checkNotDone(f); |
630 |
> |
|
631 |
> |
for (int i = 0; i < 3; i++) { |
632 |
> |
try { |
633 |
> |
f.invoke(); |
634 |
> |
shouldThrow(); |
635 |
> |
} catch (FJException success) { |
636 |
> |
checkTaskThrew(f, success); |
637 |
> |
} |
638 |
> |
f.reinitialize(); |
639 |
> |
checkNotDone(f); |
640 |
> |
} |
641 |
|
return NoResult; |
642 |
|
}}; |
643 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
654 |
|
try { |
655 |
|
Integer r = f.invoke(); |
656 |
|
shouldThrow(); |
657 |
< |
return r; |
658 |
< |
} catch (FJException success) {} |
657 |
> |
} catch (FJException success) { |
658 |
> |
checkTaskThrew(f, success); |
659 |
> |
} |
660 |
|
return NoResult; |
661 |
|
}}; |
662 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
671 |
|
FibTask f = new FibTask(8); |
672 |
|
f.complete(NoResult); |
673 |
|
Integer r = f.invoke(); |
527 |
– |
assertTrue(f.isDone()); |
674 |
|
assertSame(NoResult, r); |
675 |
+ |
checkCompletedNormally(f, NoResult); |
676 |
|
return r; |
677 |
|
}}; |
678 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
687 |
|
FibTask f = new FibTask(8); |
688 |
|
FibTask g = new FibTask(9); |
689 |
|
invokeAll(f, g); |
690 |
< |
assertTrue(f.isDone()); |
691 |
< |
assertEquals(21, (int) f.join()); |
545 |
< |
assertTrue(g.isDone()); |
546 |
< |
assertEquals(34, (int) g.join()); |
690 |
> |
checkCompletedNormally(f, 21); |
691 |
> |
checkCompletedNormally(g, 34); |
692 |
|
return NoResult; |
693 |
|
}}; |
694 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
702 |
|
public Integer realCompute() { |
703 |
|
FibTask f = new FibTask(8); |
704 |
|
invokeAll(f); |
705 |
< |
assertTrue(f.isDone()); |
561 |
< |
assertEquals(21, (int) f.join()); |
705 |
> |
checkCompletedNormally(f, 21); |
706 |
|
return NoResult; |
707 |
|
}}; |
708 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
719 |
|
FibTask h = new FibTask(7); |
720 |
|
invokeAll(f, g, h); |
721 |
|
assertTrue(f.isDone()); |
578 |
– |
assertEquals(21, (int) f.join()); |
722 |
|
assertTrue(g.isDone()); |
580 |
– |
assertEquals(34, (int) g.join()); |
723 |
|
assertTrue(h.isDone()); |
724 |
< |
assertEquals(13, (int) h.join()); |
724 |
> |
checkCompletedNormally(f, 21); |
725 |
> |
checkCompletedNormally(g, 34); |
726 |
> |
checkCompletedNormally(h, 13); |
727 |
|
return NoResult; |
728 |
|
}}; |
729 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
744 |
|
set.add(h); |
745 |
|
invokeAll(set); |
746 |
|
assertTrue(f.isDone()); |
603 |
– |
assertEquals(21, (int) f.join()); |
747 |
|
assertTrue(g.isDone()); |
605 |
– |
assertEquals(34, (int) g.join()); |
748 |
|
assertTrue(h.isDone()); |
749 |
< |
assertEquals(13, (int) h.join()); |
749 |
> |
checkCompletedNormally(f, 21); |
750 |
> |
checkCompletedNormally(g, 34); |
751 |
> |
checkCompletedNormally(h, 13); |
752 |
|
return NoResult; |
753 |
|
}}; |
754 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
756 |
|
|
757 |
|
|
758 |
|
/** |
759 |
+ |
* invokeAll(tasks) with any null task throws NPE |
760 |
+ |
*/ |
761 |
+ |
public void testInvokeAllNPE() { |
762 |
+ |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
763 |
+ |
public Integer realCompute() { |
764 |
+ |
FibTask f = new FibTask(8); |
765 |
+ |
FibTask g = new FibTask(9); |
766 |
+ |
FibTask h = null; |
767 |
+ |
try { |
768 |
+ |
invokeAll(f, g, h); |
769 |
+ |
shouldThrow(); |
770 |
+ |
} catch (NullPointerException success) {} |
771 |
+ |
return NoResult; |
772 |
+ |
}}; |
773 |
+ |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
774 |
+ |
} |
775 |
+ |
|
776 |
+ |
/** |
777 |
|
* invokeAll(t1, t2) throw exception if any task does |
778 |
|
*/ |
779 |
|
public void testAbnormalInvokeAll2() { |
784 |
|
try { |
785 |
|
invokeAll(f, g); |
786 |
|
shouldThrow(); |
787 |
< |
return NoResult; |
788 |
< |
} catch (FJException success) {} |
787 |
> |
} catch (FJException success) { |
788 |
> |
checkTaskThrew(g, success); |
789 |
> |
} |
790 |
|
return NoResult; |
791 |
|
}}; |
792 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
802 |
|
try { |
803 |
|
invokeAll(g); |
804 |
|
shouldThrow(); |
805 |
< |
return NoResult; |
806 |
< |
} catch (FJException success) {} |
805 |
> |
} catch (FJException success) { |
806 |
> |
checkTaskThrew(g, success); |
807 |
> |
} |
808 |
|
return NoResult; |
809 |
|
}}; |
810 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
822 |
|
try { |
823 |
|
invokeAll(f, g, h); |
824 |
|
shouldThrow(); |
825 |
< |
return NoResult; |
826 |
< |
} catch (FJException success) {} |
825 |
> |
} catch (FJException success) { |
826 |
> |
checkTaskThrew(g, success); |
827 |
> |
} |
828 |
|
return NoResult; |
829 |
|
}}; |
830 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
846 |
|
try { |
847 |
|
invokeAll(set); |
848 |
|
shouldThrow(); |
849 |
< |
return NoResult; |
850 |
< |
} catch (FJException success) {} |
849 |
> |
} catch (FJException success) { |
850 |
> |
checkTaskThrew(f, success); |
851 |
> |
} |
852 |
|
return NoResult; |
853 |
|
}}; |
854 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
867 |
|
assertSame(f, f.fork()); |
868 |
|
assertTrue(f.tryUnfork()); |
869 |
|
helpQuiesce(); |
870 |
< |
assertFalse(f.isDone()); |
871 |
< |
assertTrue(g.isDone()); |
870 |
> |
checkNotDone(f); |
871 |
> |
checkCompletedNormally(g, 34); |
872 |
|
return NoResult; |
873 |
|
}}; |
874 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
889 |
|
assertSame(f, f.fork()); |
890 |
|
assertTrue(getSurplusQueuedTaskCount() > 0); |
891 |
|
helpQuiesce(); |
892 |
+ |
checkCompletedNormally(f, 21); |
893 |
+ |
checkCompletedNormally(g, 34); |
894 |
+ |
checkCompletedNormally(h, 13); |
895 |
|
return NoResult; |
896 |
|
}}; |
897 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
908 |
|
FibTask f = new FibTask(8); |
909 |
|
assertSame(f, f.fork()); |
910 |
|
assertSame(f, peekNextLocalTask()); |
911 |
< |
assertEquals(21, (int) f.join()); |
743 |
< |
assertTrue(f.isDone()); |
911 |
> |
checkCompletesNormally(f, 21); |
912 |
|
helpQuiesce(); |
913 |
+ |
checkCompletedNormally(g, 34); |
914 |
|
return NoResult; |
915 |
|
}}; |
916 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
929 |
|
assertSame(f, f.fork()); |
930 |
|
assertSame(f, pollNextLocalTask()); |
931 |
|
helpQuiesce(); |
932 |
< |
assertFalse(f.isDone()); |
932 |
> |
checkNotDone(f); |
933 |
> |
checkCompletedNormally(g, 34); |
934 |
|
return NoResult; |
935 |
|
}}; |
936 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
948 |
|
assertSame(f, f.fork()); |
949 |
|
assertSame(f, pollTask()); |
950 |
|
helpQuiesce(); |
951 |
< |
assertFalse(f.isDone()); |
952 |
< |
assertTrue(g.isDone()); |
951 |
> |
checkNotDone(f); |
952 |
> |
checkCompletedNormally(g, 34); |
953 |
|
return NoResult; |
954 |
|
}}; |
955 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
968 |
|
assertSame(g, peekNextLocalTask()); |
969 |
|
assertEquals(21, (int) f.join()); |
970 |
|
helpQuiesce(); |
971 |
< |
assertTrue(f.isDone()); |
971 |
> |
checkCompletedNormally(f, 21); |
972 |
> |
checkCompletedNormally(g, 34); |
973 |
|
return NoResult; |
974 |
|
}}; |
975 |
|
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
976 |
|
} |
977 |
|
|
978 |
|
/** |
979 |
< |
* pollNextLocalTask returns least recent unexecuted task |
980 |
< |
* without executing it, in async mode |
979 |
> |
* pollNextLocalTask returns least recent unexecuted task without |
980 |
> |
* executing it, in async mode |
981 |
|
*/ |
982 |
|
public void testPollNextLocalTaskAsync() { |
983 |
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
988 |
|
assertSame(f, f.fork()); |
989 |
|
assertSame(g, pollNextLocalTask()); |
990 |
|
helpQuiesce(); |
991 |
< |
assertTrue(f.isDone()); |
992 |
< |
assertFalse(g.isDone()); |
991 |
> |
checkCompletedNormally(f, 21); |
992 |
> |
checkNotDone(g); |
993 |
|
return NoResult; |
994 |
|
}}; |
995 |
|
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
996 |
|
} |
997 |
|
|
998 |
|
/** |
999 |
< |
* pollTask returns an unexecuted task |
1000 |
< |
* without executing it, in async mode |
999 |
> |
* pollTask returns an unexecuted task without executing it, in |
1000 |
> |
* async mode |
1001 |
|
*/ |
1002 |
|
public void testPollTaskAsync() { |
1003 |
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
1008 |
|
assertSame(f, f.fork()); |
1009 |
|
assertSame(g, pollTask()); |
1010 |
|
helpQuiesce(); |
1011 |
< |
assertTrue(f.isDone()); |
1012 |
< |
assertFalse(g.isDone()); |
1011 |
> |
checkCompletedNormally(f, 21); |
1012 |
> |
checkNotDone(g); |
1013 |
|
return NoResult; |
1014 |
|
}}; |
1015 |
|
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |