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