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.*; |
7 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
> |
|
9 |
> |
import java.util.HashSet; |
10 |
|
import java.util.concurrent.CancellationException; |
11 |
|
import java.util.concurrent.ExecutionException; |
12 |
|
import java.util.concurrent.ForkJoinPool; |
13 |
+ |
import java.util.concurrent.ForkJoinTask; |
14 |
|
import java.util.concurrent.RecursiveTask; |
15 |
< |
import java.util.concurrent.TimeUnit; |
16 |
< |
import java.util.HashSet; |
15 |
> |
import java.util.concurrent.TimeoutException; |
16 |
> |
|
17 |
> |
import junit.framework.Test; |
18 |
> |
import junit.framework.TestSuite; |
19 |
|
|
20 |
|
public class RecursiveTaskTest extends JSR166TestCase { |
21 |
|
|
22 |
|
public static void main(String[] args) { |
23 |
< |
junit.textui.TestRunner.run(suite()); |
23 |
> |
main(suite(), args); |
24 |
|
} |
25 |
|
public static Test suite() { |
26 |
|
return new TestSuite(RecursiveTaskTest.class); |
41 |
|
} |
42 |
|
|
43 |
|
private <T> T testInvokeOnPool(ForkJoinPool pool, RecursiveTask<T> a) { |
44 |
< |
try { |
45 |
< |
assertFalse(a.isDone()); |
41 |
< |
assertFalse(a.isCompletedNormally()); |
42 |
< |
assertFalse(a.isCompletedAbnormally()); |
43 |
< |
assertFalse(a.isCancelled()); |
44 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
45 |
> |
checkNotDone(a); |
46 |
|
|
47 |
|
T result = pool.invoke(a); |
48 |
|
|
49 |
< |
assertTrue(a.isDone()); |
48 |
< |
assertTrue(a.isCompletedNormally()); |
49 |
< |
assertFalse(a.isCompletedAbnormally()); |
50 |
< |
assertFalse(a.isCancelled()); |
51 |
< |
assertNull(a.getException()); |
49 |
> |
checkCompletedNormally(a, result); |
50 |
|
return result; |
53 |
– |
} finally { |
54 |
– |
joinPool(pool); |
51 |
|
} |
52 |
|
} |
53 |
|
|
54 |
< |
static final class FJException extends RuntimeException { |
55 |
< |
FJException() { super(); } |
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 (! ForkJoinTask.inForkJoinPool()) { |
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(randomTimeout(), randomTimeUnit()); |
73 |
> |
shouldThrow(); |
74 |
> |
} catch (InterruptedException success) { |
75 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
76 |
> |
} |
77 |
> |
|
78 |
> |
try { |
79 |
> |
a.get(randomExpiredTimeout(), randomTimeUnit()); |
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 |
> |
assertFalse(a.cancel(false)); |
94 |
> |
assertFalse(a.cancel(true)); |
95 |
> |
try { |
96 |
> |
assertSame(expected, a.get()); |
97 |
> |
assertSame(expected, a.get(randomTimeout(), randomTimeUnit())); |
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(randomTimeout(), randomTimeUnit()); |
143 |
> |
shouldThrow(); |
144 |
> |
} catch (CancellationException success) { |
145 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
146 |
|
} |
147 |
|
|
148 |
< |
// An invalid return value for Fib |
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.getClass(), a.getException().getClass()); |
154 |
> |
assertNull(a.getRawResult()); |
155 |
> |
assertFalse(a.cancel(false)); |
156 |
> |
assertFalse(a.cancel(true)); |
157 |
> |
|
158 |
> |
try { |
159 |
> |
a.join(); |
160 |
> |
shouldThrow(); |
161 |
> |
} catch (Throwable expected) { |
162 |
> |
assertSame(t.getClass(), expected.getClass()); |
163 |
> |
} |
164 |
> |
|
165 |
> |
try { |
166 |
> |
a.get(); |
167 |
> |
shouldThrow(); |
168 |
> |
} catch (ExecutionException success) { |
169 |
> |
assertSame(t.getClass(), success.getCause().getClass()); |
170 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
171 |
> |
|
172 |
> |
try { |
173 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
174 |
> |
shouldThrow(); |
175 |
> |
} catch (ExecutionException success) { |
176 |
> |
assertSame(t.getClass(), success.getCause().getClass()); |
177 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
178 |
> |
} |
179 |
> |
|
180 |
> |
public static final class FJException extends RuntimeException { |
181 |
> |
public FJException() { super(); } |
182 |
> |
} |
183 |
> |
|
184 |
> |
/** An invalid return value for Fib. */ |
185 |
|
static final Integer NoResult = Integer.valueOf(-17); |
186 |
|
|
187 |
< |
// A simple recursive task for testing |
187 |
> |
/** A simple recursive task for testing. */ |
188 |
|
final class FibTask extends CheckedRecursiveTask<Integer> { |
189 |
|
final int number; |
190 |
|
FibTask(int n) { number = n; } |
196 |
|
f1.fork(); |
197 |
|
return (new FibTask(n - 2)).compute() + f1.join(); |
198 |
|
} |
199 |
+ |
|
200 |
+ |
public void publicSetRawResult(Integer result) { |
201 |
+ |
setRawResult(result); |
202 |
+ |
} |
203 |
|
} |
204 |
|
|
205 |
< |
// A recursive action failing in base case |
205 |
> |
/** A recursive action failing in base case. */ |
206 |
|
final class FailingFibTask extends RecursiveTask<Integer> { |
207 |
|
final int number; |
208 |
|
int result; |
229 |
|
FibTask f = new FibTask(8); |
230 |
|
Integer r = f.invoke(); |
231 |
|
assertEquals(21, (int) r); |
232 |
< |
assertTrue(f.isDone()); |
107 |
< |
assertFalse(f.isCancelled()); |
108 |
< |
assertFalse(f.isCompletedAbnormally()); |
109 |
< |
assertEquals(21, (int) f.getRawResult()); |
232 |
> |
checkCompletedNormally(f, r); |
233 |
|
return r; |
234 |
|
}}; |
235 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
245 |
|
public Integer realCompute() { |
246 |
|
FibTask f = new FibTask(8); |
247 |
|
f.quietlyInvoke(); |
248 |
< |
Integer r = f.getRawResult(); |
249 |
< |
assertEquals(21, (int) r); |
127 |
< |
assertTrue(f.isDone()); |
128 |
< |
assertFalse(f.isCancelled()); |
129 |
< |
assertFalse(f.isCompletedAbnormally()); |
130 |
< |
return r; |
248 |
> |
checkCompletedNormally(f, 21); |
249 |
> |
return NoResult; |
250 |
|
}}; |
251 |
< |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
251 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
252 |
|
} |
253 |
|
|
254 |
|
/** |
261 |
|
assertSame(f, f.fork()); |
262 |
|
Integer r = f.join(); |
263 |
|
assertEquals(21, (int) r); |
264 |
< |
assertTrue(f.isDone()); |
264 |
> |
checkCompletedNormally(f, r); |
265 |
|
return r; |
266 |
|
}}; |
267 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
277 |
|
assertSame(f, f.fork()); |
278 |
|
Integer r = f.get(); |
279 |
|
assertEquals(21, (int) r); |
280 |
< |
assertTrue(f.isDone()); |
280 |
> |
checkCompletedNormally(f, r); |
281 |
|
return r; |
282 |
|
}}; |
283 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
291 |
|
public Integer realCompute() throws Exception { |
292 |
|
FibTask f = new FibTask(8); |
293 |
|
assertSame(f, f.fork()); |
294 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
294 |
> |
Integer r = f.get(LONG_DELAY_MS, MILLISECONDS); |
295 |
|
assertEquals(21, (int) r); |
296 |
< |
assertTrue(f.isDone()); |
296 |
> |
checkCompletedNormally(f, r); |
297 |
|
return r; |
298 |
|
}}; |
299 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
310 |
|
f.quietlyJoin(); |
311 |
|
Integer r = f.getRawResult(); |
312 |
|
assertEquals(21, (int) r); |
313 |
< |
assertTrue(f.isDone()); |
313 |
> |
checkCompletedNormally(f, r); |
314 |
|
return r; |
315 |
|
}}; |
316 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
317 |
|
} |
318 |
|
|
200 |
– |
|
319 |
|
/** |
320 |
|
* helpQuiesce returns when tasks are complete. |
321 |
|
* getQueuedTaskCount returns 0 when quiescent |
325 |
|
public Integer realCompute() { |
326 |
|
FibTask f = new FibTask(8); |
327 |
|
assertSame(f, f.fork()); |
328 |
< |
f.helpQuiesce(); |
329 |
< |
Integer r = f.getRawResult(); |
330 |
< |
assertEquals(21, (int) r); |
213 |
< |
assertTrue(f.isDone()); |
328 |
> |
helpQuiesce(); |
329 |
> |
while (!f.isDone()) // wait out race |
330 |
> |
; |
331 |
|
assertEquals(0, getQueuedTaskCount()); |
332 |
< |
return r; |
332 |
> |
checkCompletedNormally(f, 21); |
333 |
> |
return NoResult; |
334 |
|
}}; |
335 |
< |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
335 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
336 |
|
} |
337 |
|
|
220 |
– |
|
338 |
|
/** |
339 |
|
* invoke task throws exception when task completes abnormally |
340 |
|
*/ |
345 |
|
try { |
346 |
|
f.invoke(); |
347 |
|
shouldThrow(); |
348 |
< |
return NoResult; |
349 |
< |
} catch (FJException success) {} |
348 |
> |
} catch (FJException success) { |
349 |
> |
checkCompletedAbnormally(f, success); |
350 |
> |
} |
351 |
|
return NoResult; |
352 |
|
}}; |
353 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
361 |
|
public Integer realCompute() { |
362 |
|
FailingFibTask f = new FailingFibTask(8); |
363 |
|
f.quietlyInvoke(); |
364 |
< |
assertTrue(f.isDone()); |
364 |
> |
assertTrue(f.getException() instanceof FJException); |
365 |
> |
checkCompletedAbnormally(f, f.getException()); |
366 |
|
return NoResult; |
367 |
|
}}; |
368 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
379 |
|
try { |
380 |
|
Integer r = f.join(); |
381 |
|
shouldThrow(); |
382 |
< |
return r; |
383 |
< |
} catch (FJException success) {} |
382 |
> |
} catch (FJException success) { |
383 |
> |
checkCompletedAbnormally(f, success); |
384 |
> |
} |
385 |
|
return NoResult; |
386 |
|
}}; |
387 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
398 |
|
try { |
399 |
|
Integer r = f.get(); |
400 |
|
shouldThrow(); |
401 |
< |
return r; |
402 |
< |
} catch (ExecutionException success) {} |
401 |
> |
} catch (ExecutionException success) { |
402 |
> |
Throwable cause = success.getCause(); |
403 |
> |
assertTrue(cause instanceof FJException); |
404 |
> |
checkCompletedAbnormally(f, cause); |
405 |
> |
} |
406 |
|
return NoResult; |
407 |
|
}}; |
408 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
417 |
|
FailingFibTask f = new FailingFibTask(8); |
418 |
|
assertSame(f, f.fork()); |
419 |
|
try { |
420 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
420 |
> |
Integer r = f.get(LONG_DELAY_MS, MILLISECONDS); |
421 |
|
shouldThrow(); |
422 |
< |
return r; |
423 |
< |
} catch (ExecutionException success) {} |
422 |
> |
} catch (ExecutionException success) { |
423 |
> |
Throwable cause = success.getCause(); |
424 |
> |
assertTrue(cause instanceof FJException); |
425 |
> |
checkCompletedAbnormally(f, cause); |
426 |
> |
} |
427 |
|
return NoResult; |
428 |
|
}}; |
429 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
438 |
|
FailingFibTask f = new FailingFibTask(8); |
439 |
|
assertSame(f, f.fork()); |
440 |
|
f.quietlyJoin(); |
315 |
– |
assertTrue(f.isDone()); |
316 |
– |
assertTrue(f.isCompletedAbnormally()); |
441 |
|
assertTrue(f.getException() instanceof FJException); |
442 |
+ |
checkCompletedAbnormally(f, f.getException()); |
443 |
|
return NoResult; |
444 |
|
}}; |
445 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
456 |
|
try { |
457 |
|
Integer r = f.invoke(); |
458 |
|
shouldThrow(); |
459 |
< |
return r; |
460 |
< |
} catch (CancellationException success) {} |
459 |
> |
} catch (CancellationException success) { |
460 |
> |
checkCancelled(f); |
461 |
> |
} |
462 |
|
return NoResult; |
463 |
|
}}; |
464 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
476 |
|
try { |
477 |
|
Integer r = f.join(); |
478 |
|
shouldThrow(); |
479 |
< |
return r; |
480 |
< |
} catch (CancellationException success) {} |
479 |
> |
} catch (CancellationException success) { |
480 |
> |
checkCancelled(f); |
481 |
> |
} |
482 |
|
return NoResult; |
483 |
|
}}; |
484 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
496 |
|
try { |
497 |
|
Integer r = f.get(); |
498 |
|
shouldThrow(); |
499 |
< |
return r; |
500 |
< |
} catch (CancellationException success) {} |
499 |
> |
} catch (CancellationException success) { |
500 |
> |
checkCancelled(f); |
501 |
> |
} |
502 |
|
return NoResult; |
503 |
|
}}; |
504 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
514 |
|
assertTrue(f.cancel(true)); |
515 |
|
assertSame(f, f.fork()); |
516 |
|
try { |
517 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
517 |
> |
Integer r = f.get(LONG_DELAY_MS, MILLISECONDS); |
518 |
|
shouldThrow(); |
519 |
< |
return r; |
520 |
< |
} catch (CancellationException success) {} |
519 |
> |
} catch (CancellationException success) { |
520 |
> |
checkCancelled(f); |
521 |
> |
} |
522 |
|
return NoResult; |
523 |
|
}}; |
524 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
534 |
|
assertTrue(f.cancel(true)); |
535 |
|
assertSame(f, f.fork()); |
536 |
|
f.quietlyJoin(); |
537 |
< |
assertTrue(f.isDone()); |
409 |
< |
assertTrue(f.isCompletedAbnormally()); |
410 |
< |
assertTrue(f.isCancelled()); |
411 |
< |
assertTrue(f.getException() instanceof CancellationException); |
537 |
> |
checkCancelled(f); |
538 |
|
return NoResult; |
539 |
|
}}; |
540 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
583 |
|
public void testInForkJoinPool2() { |
584 |
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
585 |
|
public Integer realCompute() { |
586 |
< |
assertTrue(!inForkJoinPool()); |
586 |
> |
assertFalse(inForkJoinPool()); |
587 |
|
return NoResult; |
588 |
|
}}; |
589 |
|
assertSame(NoResult, a.invoke()); |
600 |
|
return NoResult; |
601 |
|
} |
602 |
|
}; |
603 |
< |
a.invoke(); |
603 |
> |
assertSame(NoResult, a.invoke()); |
604 |
|
} |
605 |
|
|
606 |
|
/** |
607 |
< |
* A reinitialized task may be re-invoked |
607 |
> |
* A reinitialized normally completed task may be re-invoked |
608 |
|
*/ |
609 |
|
public void testReinitialize() { |
610 |
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
611 |
|
public Integer realCompute() { |
612 |
|
FibTask f = new FibTask(8); |
613 |
< |
Integer r = f.invoke(); |
614 |
< |
assertEquals(21, (int) r); |
615 |
< |
assertTrue(f.isDone()); |
616 |
< |
assertFalse(f.isCancelled()); |
617 |
< |
assertFalse(f.isCompletedAbnormally()); |
618 |
< |
f.reinitialize(); |
619 |
< |
r = f.invoke(); |
620 |
< |
assertEquals(21, (int) r); |
613 |
> |
checkNotDone(f); |
614 |
> |
|
615 |
> |
for (int i = 0; i < 3; i++) { |
616 |
> |
Integer r = f.invoke(); |
617 |
> |
assertEquals(21, (int) r); |
618 |
> |
checkCompletedNormally(f, r); |
619 |
> |
f.reinitialize(); |
620 |
> |
f.publicSetRawResult(null); |
621 |
> |
checkNotDone(f); |
622 |
> |
} |
623 |
> |
return NoResult; |
624 |
> |
}}; |
625 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
626 |
> |
} |
627 |
> |
|
628 |
> |
/** |
629 |
> |
* A reinitialized abnormally completed task may be re-invoked |
630 |
> |
*/ |
631 |
> |
public void testReinitializeAbnormal() { |
632 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
633 |
> |
public Integer realCompute() { |
634 |
> |
FailingFibTask f = new FailingFibTask(8); |
635 |
> |
checkNotDone(f); |
636 |
> |
|
637 |
> |
for (int i = 0; i < 3; i++) { |
638 |
> |
try { |
639 |
> |
f.invoke(); |
640 |
> |
shouldThrow(); |
641 |
> |
} catch (FJException success) { |
642 |
> |
checkCompletedAbnormally(f, success); |
643 |
> |
} |
644 |
> |
f.reinitialize(); |
645 |
> |
checkNotDone(f); |
646 |
> |
} |
647 |
|
return NoResult; |
648 |
|
}}; |
649 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
660 |
|
try { |
661 |
|
Integer r = f.invoke(); |
662 |
|
shouldThrow(); |
663 |
< |
return r; |
664 |
< |
} catch (FJException success) {} |
663 |
> |
} catch (FJException success) { |
664 |
> |
checkCompletedAbnormally(f, success); |
665 |
> |
} |
666 |
|
return NoResult; |
667 |
|
}}; |
668 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
677 |
|
FibTask f = new FibTask(8); |
678 |
|
f.complete(NoResult); |
679 |
|
Integer r = f.invoke(); |
527 |
– |
assertTrue(f.isDone()); |
680 |
|
assertSame(NoResult, r); |
681 |
+ |
checkCompletedNormally(f, NoResult); |
682 |
|
return r; |
683 |
|
}}; |
684 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
693 |
|
FibTask f = new FibTask(8); |
694 |
|
FibTask g = new FibTask(9); |
695 |
|
invokeAll(f, g); |
696 |
< |
assertTrue(f.isDone()); |
697 |
< |
assertEquals(21, (int) f.join()); |
545 |
< |
assertTrue(g.isDone()); |
546 |
< |
assertEquals(34, (int) g.join()); |
696 |
> |
checkCompletedNormally(f, 21); |
697 |
> |
checkCompletedNormally(g, 34); |
698 |
|
return NoResult; |
699 |
|
}}; |
700 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
708 |
|
public Integer realCompute() { |
709 |
|
FibTask f = new FibTask(8); |
710 |
|
invokeAll(f); |
711 |
< |
assertTrue(f.isDone()); |
561 |
< |
assertEquals(21, (int) f.join()); |
711 |
> |
checkCompletedNormally(f, 21); |
712 |
|
return NoResult; |
713 |
|
}}; |
714 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
725 |
|
FibTask h = new FibTask(7); |
726 |
|
invokeAll(f, g, h); |
727 |
|
assertTrue(f.isDone()); |
578 |
– |
assertEquals(21, (int) f.join()); |
728 |
|
assertTrue(g.isDone()); |
580 |
– |
assertEquals(34, (int) g.join()); |
729 |
|
assertTrue(h.isDone()); |
730 |
< |
assertEquals(13, (int) h.join()); |
730 |
> |
checkCompletedNormally(f, 21); |
731 |
> |
checkCompletedNormally(g, 34); |
732 |
> |
checkCompletedNormally(h, 13); |
733 |
|
return NoResult; |
734 |
|
}}; |
735 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
750 |
|
set.add(h); |
751 |
|
invokeAll(set); |
752 |
|
assertTrue(f.isDone()); |
603 |
– |
assertEquals(21, (int) f.join()); |
753 |
|
assertTrue(g.isDone()); |
605 |
– |
assertEquals(34, (int) g.join()); |
754 |
|
assertTrue(h.isDone()); |
755 |
< |
assertEquals(13, (int) h.join()); |
755 |
> |
checkCompletedNormally(f, 21); |
756 |
> |
checkCompletedNormally(g, 34); |
757 |
> |
checkCompletedNormally(h, 13); |
758 |
|
return NoResult; |
759 |
|
}}; |
760 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
761 |
|
} |
762 |
|
|
763 |
+ |
/** |
764 |
+ |
* invokeAll(tasks) with any null task throws NPE |
765 |
+ |
*/ |
766 |
+ |
public void testInvokeAllNPE() { |
767 |
+ |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
768 |
+ |
public Integer realCompute() { |
769 |
+ |
FibTask f = new FibTask(8); |
770 |
+ |
FibTask g = new FibTask(9); |
771 |
+ |
FibTask h = null; |
772 |
+ |
try { |
773 |
+ |
invokeAll(f, g, h); |
774 |
+ |
shouldThrow(); |
775 |
+ |
} catch (NullPointerException success) {} |
776 |
+ |
return NoResult; |
777 |
+ |
}}; |
778 |
+ |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
779 |
+ |
} |
780 |
|
|
781 |
|
/** |
782 |
|
* invokeAll(t1, t2) throw exception if any task does |
789 |
|
try { |
790 |
|
invokeAll(f, g); |
791 |
|
shouldThrow(); |
792 |
< |
return NoResult; |
793 |
< |
} catch (FJException success) {} |
792 |
> |
} catch (FJException success) { |
793 |
> |
checkCompletedAbnormally(g, success); |
794 |
> |
} |
795 |
|
return NoResult; |
796 |
|
}}; |
797 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
807 |
|
try { |
808 |
|
invokeAll(g); |
809 |
|
shouldThrow(); |
810 |
< |
return NoResult; |
811 |
< |
} catch (FJException success) {} |
810 |
> |
} catch (FJException success) { |
811 |
> |
checkCompletedAbnormally(g, success); |
812 |
> |
} |
813 |
|
return NoResult; |
814 |
|
}}; |
815 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
827 |
|
try { |
828 |
|
invokeAll(f, g, h); |
829 |
|
shouldThrow(); |
830 |
< |
return NoResult; |
831 |
< |
} catch (FJException success) {} |
830 |
> |
} catch (FJException success) { |
831 |
> |
checkCompletedAbnormally(g, success); |
832 |
> |
} |
833 |
|
return NoResult; |
834 |
|
}}; |
835 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
851 |
|
try { |
852 |
|
invokeAll(set); |
853 |
|
shouldThrow(); |
854 |
< |
return NoResult; |
855 |
< |
} catch (FJException success) {} |
854 |
> |
} catch (FJException success) { |
855 |
> |
checkCompletedAbnormally(f, success); |
856 |
> |
} |
857 |
|
return NoResult; |
858 |
|
}}; |
859 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
872 |
|
assertSame(f, f.fork()); |
873 |
|
assertTrue(f.tryUnfork()); |
874 |
|
helpQuiesce(); |
875 |
< |
assertFalse(f.isDone()); |
876 |
< |
assertTrue(g.isDone()); |
875 |
> |
checkNotDone(f); |
876 |
> |
checkCompletedNormally(g, 34); |
877 |
|
return NoResult; |
878 |
|
}}; |
879 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
894 |
|
assertSame(f, f.fork()); |
895 |
|
assertTrue(getSurplusQueuedTaskCount() > 0); |
896 |
|
helpQuiesce(); |
897 |
+ |
assertEquals(0, getSurplusQueuedTaskCount()); |
898 |
+ |
checkCompletedNormally(f, 21); |
899 |
+ |
checkCompletedNormally(g, 34); |
900 |
+ |
checkCompletedNormally(h, 13); |
901 |
|
return NoResult; |
902 |
|
}}; |
903 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
914 |
|
FibTask f = new FibTask(8); |
915 |
|
assertSame(f, f.fork()); |
916 |
|
assertSame(f, peekNextLocalTask()); |
917 |
< |
assertEquals(21, (int) f.join()); |
743 |
< |
assertTrue(f.isDone()); |
917 |
> |
checkCompletesNormally(f, 21); |
918 |
|
helpQuiesce(); |
919 |
+ |
checkCompletedNormally(g, 34); |
920 |
|
return NoResult; |
921 |
|
}}; |
922 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
935 |
|
assertSame(f, f.fork()); |
936 |
|
assertSame(f, pollNextLocalTask()); |
937 |
|
helpQuiesce(); |
938 |
< |
assertFalse(f.isDone()); |
938 |
> |
checkNotDone(f); |
939 |
> |
checkCompletedNormally(g, 34); |
940 |
|
return NoResult; |
941 |
|
}}; |
942 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
954 |
|
assertSame(f, f.fork()); |
955 |
|
assertSame(f, pollTask()); |
956 |
|
helpQuiesce(); |
957 |
< |
assertFalse(f.isDone()); |
958 |
< |
assertTrue(g.isDone()); |
957 |
> |
checkNotDone(f); |
958 |
> |
checkCompletedNormally(g, 34); |
959 |
|
return NoResult; |
960 |
|
}}; |
961 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
974 |
|
assertSame(g, peekNextLocalTask()); |
975 |
|
assertEquals(21, (int) f.join()); |
976 |
|
helpQuiesce(); |
977 |
< |
assertTrue(f.isDone()); |
977 |
> |
checkCompletedNormally(f, 21); |
978 |
> |
checkCompletedNormally(g, 34); |
979 |
|
return NoResult; |
980 |
|
}}; |
981 |
|
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
982 |
|
} |
983 |
|
|
984 |
|
/** |
985 |
< |
* pollNextLocalTask returns least recent unexecuted task |
986 |
< |
* without executing it, in async mode |
985 |
> |
* pollNextLocalTask returns least recent unexecuted task without |
986 |
> |
* executing it, in async mode |
987 |
|
*/ |
988 |
|
public void testPollNextLocalTaskAsync() { |
989 |
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
994 |
|
assertSame(f, f.fork()); |
995 |
|
assertSame(g, pollNextLocalTask()); |
996 |
|
helpQuiesce(); |
997 |
< |
assertTrue(f.isDone()); |
998 |
< |
assertFalse(g.isDone()); |
997 |
> |
checkCompletedNormally(f, 21); |
998 |
> |
checkNotDone(g); |
999 |
|
return NoResult; |
1000 |
|
}}; |
1001 |
|
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
1002 |
|
} |
1003 |
|
|
1004 |
|
/** |
1005 |
< |
* pollTask returns an unexecuted task |
1006 |
< |
* without executing it, in async mode |
1005 |
> |
* pollTask returns an unexecuted task without executing it, in |
1006 |
> |
* async mode |
1007 |
|
*/ |
1008 |
|
public void testPollTaskAsync() { |
1009 |
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
1014 |
|
assertSame(f, f.fork()); |
1015 |
|
assertSame(g, pollTask()); |
1016 |
|
helpQuiesce(); |
1017 |
< |
assertTrue(f.isDone()); |
1018 |
< |
assertFalse(g.isDone()); |
1017 |
> |
checkCompletedNormally(f, 21); |
1018 |
> |
checkNotDone(g); |
1019 |
|
return NoResult; |
1020 |
|
}}; |
1021 |
|
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |