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 checkCompletedAbnormally(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 |
> |
checkCompletedAbnormally(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 |
> |
checkCompletedAbnormally(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 |
> |
checkCompletedAbnormally(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 |
> |
Throwable cause = success.getCause(); |
401 |
> |
assertTrue(cause instanceof FJException); |
402 |
> |
checkCompletedAbnormally(f, cause); |
403 |
> |
} |
404 |
|
return NoResult; |
405 |
|
}}; |
406 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
415 |
|
FailingFibTask f = new FailingFibTask(8); |
416 |
|
assertSame(f, f.fork()); |
417 |
|
try { |
418 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
418 |
> |
Integer r = f.get(5L, SECONDS); |
419 |
|
shouldThrow(); |
420 |
< |
return r; |
421 |
< |
} catch (ExecutionException success) {} |
420 |
> |
} catch (ExecutionException success) { |
421 |
> |
Throwable cause = success.getCause(); |
422 |
> |
assertTrue(cause instanceof FJException); |
423 |
> |
checkCompletedAbnormally(f, cause); |
424 |
> |
} |
425 |
|
return NoResult; |
426 |
|
}}; |
427 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
436 |
|
FailingFibTask f = new FailingFibTask(8); |
437 |
|
assertSame(f, f.fork()); |
438 |
|
f.quietlyJoin(); |
315 |
– |
assertTrue(f.isDone()); |
316 |
– |
assertTrue(f.isCompletedAbnormally()); |
439 |
|
assertTrue(f.getException() instanceof FJException); |
440 |
+ |
checkCompletedAbnormally(f, f.getException()); |
441 |
|
return NoResult; |
442 |
|
}}; |
443 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
454 |
|
try { |
455 |
|
Integer r = f.invoke(); |
456 |
|
shouldThrow(); |
457 |
< |
return r; |
458 |
< |
} catch (CancellationException success) {} |
457 |
> |
} catch (CancellationException success) { |
458 |
> |
checkCancelled(f); |
459 |
> |
} |
460 |
|
return NoResult; |
461 |
|
}}; |
462 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
474 |
|
try { |
475 |
|
Integer r = f.join(); |
476 |
|
shouldThrow(); |
477 |
< |
return r; |
478 |
< |
} catch (CancellationException success) {} |
477 |
> |
} catch (CancellationException success) { |
478 |
> |
checkCancelled(f); |
479 |
> |
} |
480 |
|
return NoResult; |
481 |
|
}}; |
482 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
494 |
|
try { |
495 |
|
Integer r = f.get(); |
496 |
|
shouldThrow(); |
497 |
< |
return r; |
498 |
< |
} catch (CancellationException success) {} |
497 |
> |
} catch (CancellationException success) { |
498 |
> |
checkCancelled(f); |
499 |
> |
} |
500 |
|
return NoResult; |
501 |
|
}}; |
502 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
512 |
|
assertTrue(f.cancel(true)); |
513 |
|
assertSame(f, f.fork()); |
514 |
|
try { |
515 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
515 |
> |
Integer r = f.get(5L, SECONDS); |
516 |
|
shouldThrow(); |
517 |
< |
return r; |
518 |
< |
} catch (CancellationException success) {} |
517 |
> |
} catch (CancellationException success) { |
518 |
> |
checkCancelled(f); |
519 |
> |
} |
520 |
|
return NoResult; |
521 |
|
}}; |
522 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
532 |
|
assertTrue(f.cancel(true)); |
533 |
|
assertSame(f, f.fork()); |
534 |
|
f.quietlyJoin(); |
535 |
< |
assertTrue(f.isDone()); |
409 |
< |
assertTrue(f.isCompletedAbnormally()); |
410 |
< |
assertTrue(f.isCancelled()); |
411 |
< |
assertTrue(f.getException() instanceof CancellationException); |
535 |
> |
checkCancelled(f); |
536 |
|
return NoResult; |
537 |
|
}}; |
538 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
581 |
|
public void testInForkJoinPool2() { |
582 |
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
583 |
|
public Integer realCompute() { |
584 |
< |
assertTrue(!inForkJoinPool()); |
584 |
> |
assertFalse(inForkJoinPool()); |
585 |
|
return NoResult; |
586 |
|
}}; |
587 |
|
assertSame(NoResult, a.invoke()); |
602 |
|
} |
603 |
|
|
604 |
|
/** |
605 |
< |
* A reinitialized task may be re-invoked |
605 |
> |
* A reinitialized normally completed task may be re-invoked |
606 |
|
*/ |
607 |
|
public void testReinitialize() { |
608 |
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
609 |
|
public Integer realCompute() { |
610 |
|
FibTask f = new FibTask(8); |
611 |
< |
Integer r = f.invoke(); |
612 |
< |
assertEquals(21, (int) r); |
613 |
< |
assertTrue(f.isDone()); |
614 |
< |
assertFalse(f.isCancelled()); |
615 |
< |
assertFalse(f.isCompletedAbnormally()); |
616 |
< |
f.reinitialize(); |
617 |
< |
r = f.invoke(); |
618 |
< |
assertEquals(21, (int) r); |
611 |
> |
checkNotDone(f); |
612 |
> |
|
613 |
> |
for (int i = 0; i < 3; i++) { |
614 |
> |
Integer r = f.invoke(); |
615 |
> |
assertEquals(21, (int) r); |
616 |
> |
checkCompletedNormally(f, r); |
617 |
> |
f.reinitialize(); |
618 |
> |
f.publicSetRawResult(null); |
619 |
> |
checkNotDone(f); |
620 |
> |
} |
621 |
> |
return NoResult; |
622 |
> |
}}; |
623 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
624 |
> |
} |
625 |
> |
|
626 |
> |
/** |
627 |
> |
* A reinitialized abnormally completed task may be re-invoked |
628 |
> |
*/ |
629 |
> |
public void testReinitializeAbnormal() { |
630 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
631 |
> |
public Integer realCompute() { |
632 |
> |
FailingFibTask f = new FailingFibTask(8); |
633 |
> |
checkNotDone(f); |
634 |
> |
|
635 |
> |
for (int i = 0; i < 3; i++) { |
636 |
> |
try { |
637 |
> |
f.invoke(); |
638 |
> |
shouldThrow(); |
639 |
> |
} catch (FJException success) { |
640 |
> |
checkCompletedAbnormally(f, success); |
641 |
> |
} |
642 |
> |
f.reinitialize(); |
643 |
> |
checkNotDone(f); |
644 |
> |
} |
645 |
|
return NoResult; |
646 |
|
}}; |
647 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
658 |
|
try { |
659 |
|
Integer r = f.invoke(); |
660 |
|
shouldThrow(); |
661 |
< |
return r; |
662 |
< |
} catch (FJException success) {} |
661 |
> |
} catch (FJException success) { |
662 |
> |
checkCompletedAbnormally(f, success); |
663 |
> |
} |
664 |
|
return NoResult; |
665 |
|
}}; |
666 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
675 |
|
FibTask f = new FibTask(8); |
676 |
|
f.complete(NoResult); |
677 |
|
Integer r = f.invoke(); |
527 |
– |
assertTrue(f.isDone()); |
678 |
|
assertSame(NoResult, r); |
679 |
+ |
checkCompletedNormally(f, NoResult); |
680 |
|
return r; |
681 |
|
}}; |
682 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
691 |
|
FibTask f = new FibTask(8); |
692 |
|
FibTask g = new FibTask(9); |
693 |
|
invokeAll(f, g); |
694 |
< |
assertTrue(f.isDone()); |
695 |
< |
assertEquals(21, (int) f.join()); |
545 |
< |
assertTrue(g.isDone()); |
546 |
< |
assertEquals(34, (int) g.join()); |
694 |
> |
checkCompletedNormally(f, 21); |
695 |
> |
checkCompletedNormally(g, 34); |
696 |
|
return NoResult; |
697 |
|
}}; |
698 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
706 |
|
public Integer realCompute() { |
707 |
|
FibTask f = new FibTask(8); |
708 |
|
invokeAll(f); |
709 |
< |
assertTrue(f.isDone()); |
561 |
< |
assertEquals(21, (int) f.join()); |
709 |
> |
checkCompletedNormally(f, 21); |
710 |
|
return NoResult; |
711 |
|
}}; |
712 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
723 |
|
FibTask h = new FibTask(7); |
724 |
|
invokeAll(f, g, h); |
725 |
|
assertTrue(f.isDone()); |
578 |
– |
assertEquals(21, (int) f.join()); |
726 |
|
assertTrue(g.isDone()); |
580 |
– |
assertEquals(34, (int) g.join()); |
727 |
|
assertTrue(h.isDone()); |
728 |
< |
assertEquals(13, (int) h.join()); |
728 |
> |
checkCompletedNormally(f, 21); |
729 |
> |
checkCompletedNormally(g, 34); |
730 |
> |
checkCompletedNormally(h, 13); |
731 |
|
return NoResult; |
732 |
|
}}; |
733 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
748 |
|
set.add(h); |
749 |
|
invokeAll(set); |
750 |
|
assertTrue(f.isDone()); |
603 |
– |
assertEquals(21, (int) f.join()); |
751 |
|
assertTrue(g.isDone()); |
605 |
– |
assertEquals(34, (int) g.join()); |
752 |
|
assertTrue(h.isDone()); |
753 |
< |
assertEquals(13, (int) h.join()); |
753 |
> |
checkCompletedNormally(f, 21); |
754 |
> |
checkCompletedNormally(g, 34); |
755 |
> |
checkCompletedNormally(h, 13); |
756 |
|
return NoResult; |
757 |
|
}}; |
758 |
|
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 |
782 |
|
*/ |
783 |
|
public void testAbnormalInvokeAll2() { |
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)); |