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.ForkJoinWorkerThread; |
13 |
|
import java.util.concurrent.RecursiveTask; |
14 |
|
import java.util.concurrent.TimeUnit; |
15 |
+ |
import java.util.concurrent.TimeoutException; |
16 |
+ |
import static java.util.concurrent.TimeUnit.SECONDS; |
17 |
|
import java.util.HashSet; |
18 |
|
|
19 |
|
public class RecursiveTaskTest extends JSR166TestCase { |
41 |
|
|
42 |
|
private <T> T testInvokeOnPool(ForkJoinPool pool, RecursiveTask<T> a) { |
43 |
|
try { |
44 |
< |
assertFalse(a.isDone()); |
41 |
< |
assertFalse(a.isCompletedNormally()); |
42 |
< |
assertFalse(a.isCompletedAbnormally()); |
43 |
< |
assertFalse(a.isCancelled()); |
44 |
> |
checkNotDone(a); |
45 |
|
|
46 |
|
T result = pool.invoke(a); |
47 |
|
|
48 |
< |
assertTrue(a.isDone()); |
48 |
< |
assertTrue(a.isCompletedNormally()); |
49 |
< |
assertFalse(a.isCompletedAbnormally()); |
50 |
< |
assertFalse(a.isCancelled()); |
51 |
< |
assertNull(a.getException()); |
48 |
> |
checkCompletedNormally(a, result); |
49 |
|
return result; |
50 |
|
} finally { |
51 |
|
joinPool(pool); |
52 |
|
} |
53 |
|
} |
54 |
|
|
55 |
< |
static final class FJException extends RuntimeException { |
56 |
< |
FJException() { super(); } |
55 |
> |
void checkNotDone(RecursiveTask a) { |
56 |
> |
assertFalse(a.isDone()); |
57 |
> |
assertFalse(a.isCompletedNormally()); |
58 |
> |
assertFalse(a.isCompletedAbnormally()); |
59 |
> |
assertFalse(a.isCancelled()); |
60 |
> |
assertNull(a.getException()); |
61 |
> |
assertNull(a.getRawResult()); |
62 |
> |
|
63 |
> |
if (! ForkJoinTask.inForkJoinPool()) { |
64 |
> |
Thread.currentThread().interrupt(); |
65 |
> |
try { |
66 |
> |
a.get(); |
67 |
> |
shouldThrow(); |
68 |
> |
} catch (InterruptedException success) { |
69 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
70 |
> |
|
71 |
> |
Thread.currentThread().interrupt(); |
72 |
> |
try { |
73 |
> |
a.get(5L, SECONDS); |
74 |
> |
shouldThrow(); |
75 |
> |
} catch (InterruptedException success) { |
76 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
77 |
> |
} |
78 |
> |
|
79 |
> |
try { |
80 |
> |
a.get(0L, SECONDS); |
81 |
> |
shouldThrow(); |
82 |
> |
} catch (TimeoutException success) { |
83 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
84 |
> |
} |
85 |
> |
|
86 |
> |
<T> void checkCompletedNormally(RecursiveTask<T> a, T expected) { |
87 |
> |
assertTrue(a.isDone()); |
88 |
> |
assertFalse(a.isCancelled()); |
89 |
> |
assertTrue(a.isCompletedNormally()); |
90 |
> |
assertFalse(a.isCompletedAbnormally()); |
91 |
> |
assertNull(a.getException()); |
92 |
> |
assertSame(expected, a.getRawResult()); |
93 |
> |
assertSame(expected, a.join()); |
94 |
> |
assertFalse(a.cancel(false)); |
95 |
> |
assertFalse(a.cancel(true)); |
96 |
> |
try { |
97 |
> |
assertSame(expected, a.get()); |
98 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
99 |
> |
try { |
100 |
> |
assertSame(expected, a.get(5L, SECONDS)); |
101 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
102 |
> |
} |
103 |
> |
|
104 |
> |
/** |
105 |
> |
* Waits for the task to complete, and checks that when it does, |
106 |
> |
* it will have an Integer result equals to the given int. |
107 |
> |
*/ |
108 |
> |
void checkCompletesNormally(RecursiveTask<Integer> a, int expected) { |
109 |
> |
Integer r = a.join(); |
110 |
> |
assertEquals(expected, (int) r); |
111 |
> |
checkCompletedNormally(a, r); |
112 |
> |
} |
113 |
> |
|
114 |
> |
/** |
115 |
> |
* Like checkCompletesNormally, but verifies that the task has |
116 |
> |
* already completed. |
117 |
> |
*/ |
118 |
> |
void checkCompletedNormally(RecursiveTask<Integer> a, int expected) { |
119 |
> |
Integer r = a.getRawResult(); |
120 |
> |
assertEquals(expected, (int) r); |
121 |
> |
checkCompletedNormally(a, r); |
122 |
> |
} |
123 |
> |
|
124 |
> |
void checkCancelled(RecursiveTask a) { |
125 |
> |
assertTrue(a.isDone()); |
126 |
> |
assertTrue(a.isCancelled()); |
127 |
> |
assertFalse(a.isCompletedNormally()); |
128 |
> |
assertTrue(a.isCompletedAbnormally()); |
129 |
> |
assertTrue(a.getException() instanceof CancellationException); |
130 |
> |
assertNull(a.getRawResult()); |
131 |
> |
|
132 |
> |
try { |
133 |
> |
a.join(); |
134 |
> |
shouldThrow(); |
135 |
> |
} catch (CancellationException success) { |
136 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
137 |
> |
|
138 |
> |
try { |
139 |
> |
a.get(); |
140 |
> |
shouldThrow(); |
141 |
> |
} catch (CancellationException success) { |
142 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
143 |
> |
|
144 |
> |
try { |
145 |
> |
a.get(5L, SECONDS); |
146 |
> |
shouldThrow(); |
147 |
> |
} catch (CancellationException success) { |
148 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
149 |
> |
} |
150 |
> |
|
151 |
> |
void checkCompletedAbnormally(RecursiveTask a, Throwable t) { |
152 |
> |
assertTrue(a.isDone()); |
153 |
> |
assertFalse(a.isCancelled()); |
154 |
> |
assertFalse(a.isCompletedNormally()); |
155 |
> |
assertTrue(a.isCompletedAbnormally()); |
156 |
> |
assertSame(t.getClass(), a.getException().getClass()); |
157 |
> |
assertNull(a.getRawResult()); |
158 |
> |
assertFalse(a.cancel(false)); |
159 |
> |
assertFalse(a.cancel(true)); |
160 |
> |
|
161 |
> |
try { |
162 |
> |
a.join(); |
163 |
> |
shouldThrow(); |
164 |
> |
} catch (Throwable expected) { |
165 |
> |
assertSame(t.getClass(), expected.getClass()); |
166 |
> |
} |
167 |
> |
|
168 |
> |
try { |
169 |
> |
a.get(); |
170 |
> |
shouldThrow(); |
171 |
> |
} catch (ExecutionException success) { |
172 |
> |
assertSame(t.getClass(), success.getCause().getClass()); |
173 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
174 |
> |
|
175 |
> |
try { |
176 |
> |
a.get(5L, SECONDS); |
177 |
> |
shouldThrow(); |
178 |
> |
} catch (ExecutionException success) { |
179 |
> |
assertSame(t.getClass(), success.getCause().getClass()); |
180 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
181 |
> |
} |
182 |
> |
|
183 |
> |
public static final class FJException extends RuntimeException { |
184 |
> |
public FJException() { super(); } |
185 |
|
} |
186 |
|
|
187 |
|
// An invalid return value for Fib |
199 |
|
f1.fork(); |
200 |
|
return (new FibTask(n - 2)).compute() + f1.join(); |
201 |
|
} |
202 |
+ |
|
203 |
+ |
public void publicSetRawResult(Integer result) { |
204 |
+ |
setRawResult(result); |
205 |
+ |
} |
206 |
|
} |
207 |
|
|
208 |
|
// A recursive action failing in base case |
232 |
|
FibTask f = new FibTask(8); |
233 |
|
Integer r = f.invoke(); |
234 |
|
assertEquals(21, (int) r); |
235 |
< |
assertTrue(f.isDone()); |
107 |
< |
assertFalse(f.isCancelled()); |
108 |
< |
assertFalse(f.isCompletedAbnormally()); |
109 |
< |
assertEquals(21, (int) f.getRawResult()); |
235 |
> |
checkCompletedNormally(f, r); |
236 |
|
return r; |
237 |
|
}}; |
238 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
248 |
|
public Integer realCompute() { |
249 |
|
FibTask f = new FibTask(8); |
250 |
|
f.quietlyInvoke(); |
251 |
< |
Integer r = f.getRawResult(); |
252 |
< |
assertEquals(21, (int) r); |
127 |
< |
assertTrue(f.isDone()); |
128 |
< |
assertFalse(f.isCancelled()); |
129 |
< |
assertFalse(f.isCompletedAbnormally()); |
130 |
< |
return r; |
251 |
> |
checkCompletedNormally(f, 21); |
252 |
> |
return NoResult; |
253 |
|
}}; |
254 |
< |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
254 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
255 |
|
} |
256 |
|
|
257 |
|
/** |
264 |
|
assertSame(f, f.fork()); |
265 |
|
Integer r = f.join(); |
266 |
|
assertEquals(21, (int) r); |
267 |
< |
assertTrue(f.isDone()); |
267 |
> |
checkCompletedNormally(f, r); |
268 |
|
return r; |
269 |
|
}}; |
270 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
280 |
|
assertSame(f, f.fork()); |
281 |
|
Integer r = f.get(); |
282 |
|
assertEquals(21, (int) r); |
283 |
< |
assertTrue(f.isDone()); |
283 |
> |
checkCompletedNormally(f, r); |
284 |
|
return r; |
285 |
|
}}; |
286 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
294 |
|
public Integer realCompute() throws Exception { |
295 |
|
FibTask f = new FibTask(8); |
296 |
|
assertSame(f, f.fork()); |
297 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
297 |
> |
Integer r = f.get(5L, SECONDS); |
298 |
|
assertEquals(21, (int) r); |
299 |
< |
assertTrue(f.isDone()); |
299 |
> |
checkCompletedNormally(f, r); |
300 |
|
return r; |
301 |
|
}}; |
302 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
313 |
|
f.quietlyJoin(); |
314 |
|
Integer r = f.getRawResult(); |
315 |
|
assertEquals(21, (int) r); |
316 |
< |
assertTrue(f.isDone()); |
316 |
> |
checkCompletedNormally(f, r); |
317 |
|
return r; |
318 |
|
}}; |
319 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
330 |
|
FibTask f = new FibTask(8); |
331 |
|
assertSame(f, f.fork()); |
332 |
|
f.helpQuiesce(); |
211 |
– |
Integer r = f.getRawResult(); |
212 |
– |
assertEquals(21, (int) r); |
213 |
– |
assertTrue(f.isDone()); |
333 |
|
assertEquals(0, getQueuedTaskCount()); |
334 |
< |
return r; |
334 |
> |
checkCompletedNormally(f, 21); |
335 |
> |
return NoResult; |
336 |
|
}}; |
337 |
< |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
337 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
338 |
|
} |
339 |
|
|
340 |
|
|
348 |
|
try { |
349 |
|
f.invoke(); |
350 |
|
shouldThrow(); |
351 |
< |
return NoResult; |
352 |
< |
} catch (FJException success) {} |
351 |
> |
} catch (FJException success) { |
352 |
> |
checkCompletedAbnormally(f, success); |
353 |
> |
} |
354 |
|
return NoResult; |
355 |
|
}}; |
356 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
364 |
|
public Integer realCompute() { |
365 |
|
FailingFibTask f = new FailingFibTask(8); |
366 |
|
f.quietlyInvoke(); |
367 |
< |
assertTrue(f.isDone()); |
367 |
> |
assertTrue(f.getException() instanceof FJException); |
368 |
> |
checkCompletedAbnormally(f, f.getException()); |
369 |
|
return NoResult; |
370 |
|
}}; |
371 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
382 |
|
try { |
383 |
|
Integer r = f.join(); |
384 |
|
shouldThrow(); |
385 |
< |
return r; |
386 |
< |
} catch (FJException success) {} |
385 |
> |
} catch (FJException success) { |
386 |
> |
checkCompletedAbnormally(f, success); |
387 |
> |
} |
388 |
|
return NoResult; |
389 |
|
}}; |
390 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
401 |
|
try { |
402 |
|
Integer r = f.get(); |
403 |
|
shouldThrow(); |
404 |
< |
return r; |
405 |
< |
} catch (ExecutionException success) {} |
404 |
> |
} catch (ExecutionException success) { |
405 |
> |
Throwable cause = success.getCause(); |
406 |
> |
assertTrue(cause instanceof FJException); |
407 |
> |
checkCompletedAbnormally(f, cause); |
408 |
> |
} |
409 |
|
return NoResult; |
410 |
|
}}; |
411 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
420 |
|
FailingFibTask f = new FailingFibTask(8); |
421 |
|
assertSame(f, f.fork()); |
422 |
|
try { |
423 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
423 |
> |
Integer r = f.get(5L, SECONDS); |
424 |
|
shouldThrow(); |
425 |
< |
return r; |
426 |
< |
} catch (ExecutionException success) {} |
425 |
> |
} catch (ExecutionException success) { |
426 |
> |
Throwable cause = success.getCause(); |
427 |
> |
assertTrue(cause instanceof FJException); |
428 |
> |
checkCompletedAbnormally(f, cause); |
429 |
> |
} |
430 |
|
return NoResult; |
431 |
|
}}; |
432 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
441 |
|
FailingFibTask f = new FailingFibTask(8); |
442 |
|
assertSame(f, f.fork()); |
443 |
|
f.quietlyJoin(); |
315 |
– |
assertTrue(f.isDone()); |
316 |
– |
assertTrue(f.isCompletedAbnormally()); |
444 |
|
assertTrue(f.getException() instanceof FJException); |
445 |
+ |
checkCompletedAbnormally(f, f.getException()); |
446 |
|
return NoResult; |
447 |
|
}}; |
448 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
459 |
|
try { |
460 |
|
Integer r = f.invoke(); |
461 |
|
shouldThrow(); |
462 |
< |
return r; |
463 |
< |
} catch (CancellationException success) {} |
462 |
> |
} catch (CancellationException success) { |
463 |
> |
checkCancelled(f); |
464 |
> |
} |
465 |
|
return NoResult; |
466 |
|
}}; |
467 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
479 |
|
try { |
480 |
|
Integer r = f.join(); |
481 |
|
shouldThrow(); |
482 |
< |
return r; |
483 |
< |
} catch (CancellationException success) {} |
482 |
> |
} catch (CancellationException success) { |
483 |
> |
checkCancelled(f); |
484 |
> |
} |
485 |
|
return NoResult; |
486 |
|
}}; |
487 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
499 |
|
try { |
500 |
|
Integer r = f.get(); |
501 |
|
shouldThrow(); |
502 |
< |
return r; |
503 |
< |
} catch (CancellationException success) {} |
502 |
> |
} catch (CancellationException success) { |
503 |
> |
checkCancelled(f); |
504 |
> |
} |
505 |
|
return NoResult; |
506 |
|
}}; |
507 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
517 |
|
assertTrue(f.cancel(true)); |
518 |
|
assertSame(f, f.fork()); |
519 |
|
try { |
520 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
520 |
> |
Integer r = f.get(5L, SECONDS); |
521 |
|
shouldThrow(); |
522 |
< |
return r; |
523 |
< |
} catch (CancellationException success) {} |
522 |
> |
} catch (CancellationException success) { |
523 |
> |
checkCancelled(f); |
524 |
> |
} |
525 |
|
return NoResult; |
526 |
|
}}; |
527 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
537 |
|
assertTrue(f.cancel(true)); |
538 |
|
assertSame(f, f.fork()); |
539 |
|
f.quietlyJoin(); |
540 |
< |
assertTrue(f.isDone()); |
409 |
< |
assertTrue(f.isCompletedAbnormally()); |
410 |
< |
assertTrue(f.isCancelled()); |
411 |
< |
assertTrue(f.getException() instanceof CancellationException); |
540 |
> |
checkCancelled(f); |
541 |
|
return NoResult; |
542 |
|
}}; |
543 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
586 |
|
public void testInForkJoinPool2() { |
587 |
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
588 |
|
public Integer realCompute() { |
589 |
< |
assertTrue(!inForkJoinPool()); |
589 |
> |
assertFalse(inForkJoinPool()); |
590 |
|
return NoResult; |
591 |
|
}}; |
592 |
|
assertSame(NoResult, a.invoke()); |
603 |
|
return NoResult; |
604 |
|
} |
605 |
|
}; |
606 |
< |
a.invoke(); |
606 |
> |
assertSame(NoResult, a.invoke()); |
607 |
|
} |
608 |
|
|
609 |
|
/** |
610 |
< |
* A reinitialized task may be re-invoked |
610 |
> |
* A reinitialized normally completed task may be re-invoked |
611 |
|
*/ |
612 |
|
public void testReinitialize() { |
613 |
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
614 |
|
public Integer realCompute() { |
615 |
|
FibTask f = new FibTask(8); |
616 |
< |
Integer r = f.invoke(); |
617 |
< |
assertEquals(21, (int) r); |
618 |
< |
assertTrue(f.isDone()); |
619 |
< |
assertFalse(f.isCancelled()); |
620 |
< |
assertFalse(f.isCompletedAbnormally()); |
621 |
< |
f.reinitialize(); |
622 |
< |
r = f.invoke(); |
623 |
< |
assertEquals(21, (int) r); |
616 |
> |
checkNotDone(f); |
617 |
> |
|
618 |
> |
for (int i = 0; i < 3; i++) { |
619 |
> |
Integer r = f.invoke(); |
620 |
> |
assertEquals(21, (int) r); |
621 |
> |
checkCompletedNormally(f, r); |
622 |
> |
f.reinitialize(); |
623 |
> |
f.publicSetRawResult(null); |
624 |
> |
checkNotDone(f); |
625 |
> |
} |
626 |
> |
return NoResult; |
627 |
> |
}}; |
628 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
629 |
> |
} |
630 |
> |
|
631 |
> |
/** |
632 |
> |
* A reinitialized abnormally completed task may be re-invoked |
633 |
> |
*/ |
634 |
> |
public void testReinitializeAbnormal() { |
635 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
636 |
> |
public Integer realCompute() { |
637 |
> |
FailingFibTask f = new FailingFibTask(8); |
638 |
> |
checkNotDone(f); |
639 |
> |
|
640 |
> |
for (int i = 0; i < 3; i++) { |
641 |
> |
try { |
642 |
> |
f.invoke(); |
643 |
> |
shouldThrow(); |
644 |
> |
} catch (FJException success) { |
645 |
> |
checkCompletedAbnormally(f, success); |
646 |
> |
} |
647 |
> |
f.reinitialize(); |
648 |
> |
checkNotDone(f); |
649 |
> |
} |
650 |
|
return NoResult; |
651 |
|
}}; |
652 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
663 |
|
try { |
664 |
|
Integer r = f.invoke(); |
665 |
|
shouldThrow(); |
666 |
< |
return r; |
667 |
< |
} catch (FJException success) {} |
666 |
> |
} catch (FJException success) { |
667 |
> |
checkCompletedAbnormally(f, success); |
668 |
> |
} |
669 |
|
return NoResult; |
670 |
|
}}; |
671 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
680 |
|
FibTask f = new FibTask(8); |
681 |
|
f.complete(NoResult); |
682 |
|
Integer r = f.invoke(); |
527 |
– |
assertTrue(f.isDone()); |
683 |
|
assertSame(NoResult, r); |
684 |
+ |
checkCompletedNormally(f, NoResult); |
685 |
|
return r; |
686 |
|
}}; |
687 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
696 |
|
FibTask f = new FibTask(8); |
697 |
|
FibTask g = new FibTask(9); |
698 |
|
invokeAll(f, g); |
699 |
< |
assertTrue(f.isDone()); |
700 |
< |
assertEquals(21, (int) f.join()); |
545 |
< |
assertTrue(g.isDone()); |
546 |
< |
assertEquals(34, (int) g.join()); |
699 |
> |
checkCompletedNormally(f, 21); |
700 |
> |
checkCompletedNormally(g, 34); |
701 |
|
return NoResult; |
702 |
|
}}; |
703 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
711 |
|
public Integer realCompute() { |
712 |
|
FibTask f = new FibTask(8); |
713 |
|
invokeAll(f); |
714 |
< |
assertTrue(f.isDone()); |
561 |
< |
assertEquals(21, (int) f.join()); |
714 |
> |
checkCompletedNormally(f, 21); |
715 |
|
return NoResult; |
716 |
|
}}; |
717 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
728 |
|
FibTask h = new FibTask(7); |
729 |
|
invokeAll(f, g, h); |
730 |
|
assertTrue(f.isDone()); |
578 |
– |
assertEquals(21, (int) f.join()); |
731 |
|
assertTrue(g.isDone()); |
580 |
– |
assertEquals(34, (int) g.join()); |
732 |
|
assertTrue(h.isDone()); |
733 |
< |
assertEquals(13, (int) h.join()); |
733 |
> |
checkCompletedNormally(f, 21); |
734 |
> |
checkCompletedNormally(g, 34); |
735 |
> |
checkCompletedNormally(h, 13); |
736 |
|
return NoResult; |
737 |
|
}}; |
738 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
753 |
|
set.add(h); |
754 |
|
invokeAll(set); |
755 |
|
assertTrue(f.isDone()); |
603 |
– |
assertEquals(21, (int) f.join()); |
756 |
|
assertTrue(g.isDone()); |
605 |
– |
assertEquals(34, (int) g.join()); |
757 |
|
assertTrue(h.isDone()); |
758 |
< |
assertEquals(13, (int) h.join()); |
758 |
> |
checkCompletedNormally(f, 21); |
759 |
> |
checkCompletedNormally(g, 34); |
760 |
> |
checkCompletedNormally(h, 13); |
761 |
|
return NoResult; |
762 |
|
}}; |
763 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
765 |
|
|
766 |
|
|
767 |
|
/** |
768 |
+ |
* invokeAll(tasks) with any null task throws NPE |
769 |
+ |
*/ |
770 |
+ |
public void testInvokeAllNPE() { |
771 |
+ |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
772 |
+ |
public Integer realCompute() { |
773 |
+ |
FibTask f = new FibTask(8); |
774 |
+ |
FibTask g = new FibTask(9); |
775 |
+ |
FibTask h = null; |
776 |
+ |
try { |
777 |
+ |
invokeAll(f, g, h); |
778 |
+ |
shouldThrow(); |
779 |
+ |
} catch (NullPointerException success) {} |
780 |
+ |
return NoResult; |
781 |
+ |
}}; |
782 |
+ |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
783 |
+ |
} |
784 |
+ |
|
785 |
+ |
/** |
786 |
|
* invokeAll(t1, t2) throw exception if any task does |
787 |
|
*/ |
788 |
|
public void testAbnormalInvokeAll2() { |
793 |
|
try { |
794 |
|
invokeAll(f, g); |
795 |
|
shouldThrow(); |
796 |
< |
return NoResult; |
797 |
< |
} catch (FJException success) {} |
796 |
> |
} catch (FJException success) { |
797 |
> |
checkCompletedAbnormally(g, success); |
798 |
> |
} |
799 |
|
return NoResult; |
800 |
|
}}; |
801 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
811 |
|
try { |
812 |
|
invokeAll(g); |
813 |
|
shouldThrow(); |
814 |
< |
return NoResult; |
815 |
< |
} catch (FJException success) {} |
814 |
> |
} catch (FJException success) { |
815 |
> |
checkCompletedAbnormally(g, success); |
816 |
> |
} |
817 |
|
return NoResult; |
818 |
|
}}; |
819 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
831 |
|
try { |
832 |
|
invokeAll(f, g, h); |
833 |
|
shouldThrow(); |
834 |
< |
return NoResult; |
835 |
< |
} catch (FJException success) {} |
834 |
> |
} catch (FJException success) { |
835 |
> |
checkCompletedAbnormally(g, success); |
836 |
> |
} |
837 |
|
return NoResult; |
838 |
|
}}; |
839 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
855 |
|
try { |
856 |
|
invokeAll(set); |
857 |
|
shouldThrow(); |
858 |
< |
return NoResult; |
859 |
< |
} catch (FJException success) {} |
858 |
> |
} catch (FJException success) { |
859 |
> |
checkCompletedAbnormally(f, success); |
860 |
> |
} |
861 |
|
return NoResult; |
862 |
|
}}; |
863 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
876 |
|
assertSame(f, f.fork()); |
877 |
|
assertTrue(f.tryUnfork()); |
878 |
|
helpQuiesce(); |
879 |
< |
assertFalse(f.isDone()); |
880 |
< |
assertTrue(g.isDone()); |
879 |
> |
checkNotDone(f); |
880 |
> |
checkCompletedNormally(g, 34); |
881 |
|
return NoResult; |
882 |
|
}}; |
883 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
898 |
|
assertSame(f, f.fork()); |
899 |
|
assertTrue(getSurplusQueuedTaskCount() > 0); |
900 |
|
helpQuiesce(); |
901 |
+ |
assertEquals(0, getSurplusQueuedTaskCount()); |
902 |
+ |
checkCompletedNormally(f, 21); |
903 |
+ |
checkCompletedNormally(g, 34); |
904 |
+ |
checkCompletedNormally(h, 13); |
905 |
|
return NoResult; |
906 |
|
}}; |
907 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
918 |
|
FibTask f = new FibTask(8); |
919 |
|
assertSame(f, f.fork()); |
920 |
|
assertSame(f, peekNextLocalTask()); |
921 |
< |
assertEquals(21, (int) f.join()); |
743 |
< |
assertTrue(f.isDone()); |
921 |
> |
checkCompletesNormally(f, 21); |
922 |
|
helpQuiesce(); |
923 |
+ |
checkCompletedNormally(g, 34); |
924 |
|
return NoResult; |
925 |
|
}}; |
926 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
939 |
|
assertSame(f, f.fork()); |
940 |
|
assertSame(f, pollNextLocalTask()); |
941 |
|
helpQuiesce(); |
942 |
< |
assertFalse(f.isDone()); |
942 |
> |
checkNotDone(f); |
943 |
> |
checkCompletedNormally(g, 34); |
944 |
|
return NoResult; |
945 |
|
}}; |
946 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
958 |
|
assertSame(f, f.fork()); |
959 |
|
assertSame(f, pollTask()); |
960 |
|
helpQuiesce(); |
961 |
< |
assertFalse(f.isDone()); |
962 |
< |
assertTrue(g.isDone()); |
961 |
> |
checkNotDone(f); |
962 |
> |
checkCompletedNormally(g, 34); |
963 |
|
return NoResult; |
964 |
|
}}; |
965 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
978 |
|
assertSame(g, peekNextLocalTask()); |
979 |
|
assertEquals(21, (int) f.join()); |
980 |
|
helpQuiesce(); |
981 |
< |
assertTrue(f.isDone()); |
981 |
> |
checkCompletedNormally(f, 21); |
982 |
> |
checkCompletedNormally(g, 34); |
983 |
|
return NoResult; |
984 |
|
}}; |
985 |
|
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
986 |
|
} |
987 |
|
|
988 |
|
/** |
989 |
< |
* pollNextLocalTask returns least recent unexecuted task |
990 |
< |
* without executing it, in async mode |
989 |
> |
* pollNextLocalTask returns least recent unexecuted task without |
990 |
> |
* executing it, in async mode |
991 |
|
*/ |
992 |
|
public void testPollNextLocalTaskAsync() { |
993 |
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
998 |
|
assertSame(f, f.fork()); |
999 |
|
assertSame(g, pollNextLocalTask()); |
1000 |
|
helpQuiesce(); |
1001 |
< |
assertTrue(f.isDone()); |
1002 |
< |
assertFalse(g.isDone()); |
1001 |
> |
checkCompletedNormally(f, 21); |
1002 |
> |
checkNotDone(g); |
1003 |
|
return NoResult; |
1004 |
|
}}; |
1005 |
|
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
1006 |
|
} |
1007 |
|
|
1008 |
|
/** |
1009 |
< |
* pollTask returns an unexecuted task |
1010 |
< |
* without executing it, in async mode |
1009 |
> |
* pollTask returns an unexecuted task without executing it, in |
1010 |
> |
* async mode |
1011 |
|
*/ |
1012 |
|
public void testPollTaskAsync() { |
1013 |
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
1018 |
|
assertSame(f, f.fork()); |
1019 |
|
assertSame(g, pollTask()); |
1020 |
|
helpQuiesce(); |
1021 |
< |
assertTrue(f.isDone()); |
1022 |
< |
assertFalse(g.isDone()); |
1021 |
> |
checkCompletedNormally(f, 21); |
1022 |
> |
checkNotDone(g); |
1023 |
|
return NoResult; |
1024 |
|
}}; |
1025 |
|
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |