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)); |
320 |
|
} |
321 |
|
|
200 |
– |
|
322 |
|
/** |
323 |
|
* helpQuiesce returns when tasks are complete. |
324 |
|
* getQueuedTaskCount returns 0 when quiescent |
328 |
|
public Integer realCompute() { |
329 |
|
FibTask f = new FibTask(8); |
330 |
|
assertSame(f, f.fork()); |
331 |
< |
f.helpQuiesce(); |
211 |
< |
Integer r = f.getRawResult(); |
212 |
< |
assertEquals(21, (int) r); |
213 |
< |
assertTrue(f.isDone()); |
331 |
> |
helpQuiesce(); |
332 |
|
assertEquals(0, getQueuedTaskCount()); |
333 |
< |
return r; |
333 |
> |
checkCompletedNormally(f, 21); |
334 |
> |
return NoResult; |
335 |
|
}}; |
336 |
< |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
336 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
337 |
|
} |
338 |
|
|
220 |
– |
|
339 |
|
/** |
340 |
|
* invoke task throws exception when task completes abnormally |
341 |
|
*/ |
346 |
|
try { |
347 |
|
f.invoke(); |
348 |
|
shouldThrow(); |
349 |
< |
return NoResult; |
350 |
< |
} catch (FJException success) {} |
349 |
> |
} catch (FJException success) { |
350 |
> |
checkCompletedAbnormally(f, success); |
351 |
> |
} |
352 |
|
return NoResult; |
353 |
|
}}; |
354 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
362 |
|
public Integer realCompute() { |
363 |
|
FailingFibTask f = new FailingFibTask(8); |
364 |
|
f.quietlyInvoke(); |
365 |
< |
assertTrue(f.isDone()); |
365 |
> |
assertTrue(f.getException() instanceof FJException); |
366 |
> |
checkCompletedAbnormally(f, f.getException()); |
367 |
|
return NoResult; |
368 |
|
}}; |
369 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
380 |
|
try { |
381 |
|
Integer r = f.join(); |
382 |
|
shouldThrow(); |
383 |
< |
return r; |
384 |
< |
} catch (FJException success) {} |
383 |
> |
} catch (FJException success) { |
384 |
> |
checkCompletedAbnormally(f, success); |
385 |
> |
} |
386 |
|
return NoResult; |
387 |
|
}}; |
388 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
399 |
|
try { |
400 |
|
Integer r = f.get(); |
401 |
|
shouldThrow(); |
402 |
< |
return r; |
403 |
< |
} catch (ExecutionException success) {} |
402 |
> |
} catch (ExecutionException success) { |
403 |
> |
Throwable cause = success.getCause(); |
404 |
> |
assertTrue(cause instanceof FJException); |
405 |
> |
checkCompletedAbnormally(f, cause); |
406 |
> |
} |
407 |
|
return NoResult; |
408 |
|
}}; |
409 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
418 |
|
FailingFibTask f = new FailingFibTask(8); |
419 |
|
assertSame(f, f.fork()); |
420 |
|
try { |
421 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
421 |
> |
Integer r = f.get(5L, SECONDS); |
422 |
|
shouldThrow(); |
423 |
< |
return r; |
424 |
< |
} catch (ExecutionException success) {} |
423 |
> |
} catch (ExecutionException success) { |
424 |
> |
Throwable cause = success.getCause(); |
425 |
> |
assertTrue(cause instanceof FJException); |
426 |
> |
checkCompletedAbnormally(f, cause); |
427 |
> |
} |
428 |
|
return NoResult; |
429 |
|
}}; |
430 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
439 |
|
FailingFibTask f = new FailingFibTask(8); |
440 |
|
assertSame(f, f.fork()); |
441 |
|
f.quietlyJoin(); |
315 |
– |
assertTrue(f.isDone()); |
316 |
– |
assertTrue(f.isCompletedAbnormally()); |
442 |
|
assertTrue(f.getException() instanceof FJException); |
443 |
+ |
checkCompletedAbnormally(f, f.getException()); |
444 |
|
return NoResult; |
445 |
|
}}; |
446 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
457 |
|
try { |
458 |
|
Integer r = f.invoke(); |
459 |
|
shouldThrow(); |
460 |
< |
return r; |
461 |
< |
} catch (CancellationException success) {} |
460 |
> |
} catch (CancellationException success) { |
461 |
> |
checkCancelled(f); |
462 |
> |
} |
463 |
|
return NoResult; |
464 |
|
}}; |
465 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
477 |
|
try { |
478 |
|
Integer r = f.join(); |
479 |
|
shouldThrow(); |
480 |
< |
return r; |
481 |
< |
} catch (CancellationException success) {} |
480 |
> |
} catch (CancellationException success) { |
481 |
> |
checkCancelled(f); |
482 |
> |
} |
483 |
|
return NoResult; |
484 |
|
}}; |
485 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
497 |
|
try { |
498 |
|
Integer r = f.get(); |
499 |
|
shouldThrow(); |
500 |
< |
return r; |
501 |
< |
} catch (CancellationException success) {} |
500 |
> |
} catch (CancellationException success) { |
501 |
> |
checkCancelled(f); |
502 |
> |
} |
503 |
|
return NoResult; |
504 |
|
}}; |
505 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
515 |
|
assertTrue(f.cancel(true)); |
516 |
|
assertSame(f, f.fork()); |
517 |
|
try { |
518 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
518 |
> |
Integer r = f.get(5L, SECONDS); |
519 |
|
shouldThrow(); |
520 |
< |
return r; |
521 |
< |
} catch (CancellationException success) {} |
520 |
> |
} catch (CancellationException success) { |
521 |
> |
checkCancelled(f); |
522 |
> |
} |
523 |
|
return NoResult; |
524 |
|
}}; |
525 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
535 |
|
assertTrue(f.cancel(true)); |
536 |
|
assertSame(f, f.fork()); |
537 |
|
f.quietlyJoin(); |
538 |
< |
assertTrue(f.isDone()); |
409 |
< |
assertTrue(f.isCompletedAbnormally()); |
410 |
< |
assertTrue(f.isCancelled()); |
411 |
< |
assertTrue(f.getException() instanceof CancellationException); |
538 |
> |
checkCancelled(f); |
539 |
|
return NoResult; |
540 |
|
}}; |
541 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
584 |
|
public void testInForkJoinPool2() { |
585 |
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
586 |
|
public Integer realCompute() { |
587 |
< |
assertTrue(!inForkJoinPool()); |
587 |
> |
assertFalse(inForkJoinPool()); |
588 |
|
return NoResult; |
589 |
|
}}; |
590 |
|
assertSame(NoResult, a.invoke()); |
601 |
|
return NoResult; |
602 |
|
} |
603 |
|
}; |
604 |
< |
a.invoke(); |
604 |
> |
assertSame(NoResult, a.invoke()); |
605 |
|
} |
606 |
|
|
607 |
|
/** |
608 |
< |
* A reinitialized task may be re-invoked |
608 |
> |
* A reinitialized normally completed task may be re-invoked |
609 |
|
*/ |
610 |
|
public void testReinitialize() { |
611 |
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
612 |
|
public Integer realCompute() { |
613 |
|
FibTask f = new FibTask(8); |
614 |
< |
Integer r = f.invoke(); |
615 |
< |
assertEquals(21, (int) r); |
616 |
< |
assertTrue(f.isDone()); |
617 |
< |
assertFalse(f.isCancelled()); |
618 |
< |
assertFalse(f.isCompletedAbnormally()); |
619 |
< |
f.reinitialize(); |
620 |
< |
r = f.invoke(); |
621 |
< |
assertEquals(21, (int) r); |
614 |
> |
checkNotDone(f); |
615 |
> |
|
616 |
> |
for (int i = 0; i < 3; i++) { |
617 |
> |
Integer r = f.invoke(); |
618 |
> |
assertEquals(21, (int) r); |
619 |
> |
checkCompletedNormally(f, r); |
620 |
> |
f.reinitialize(); |
621 |
> |
f.publicSetRawResult(null); |
622 |
> |
checkNotDone(f); |
623 |
> |
} |
624 |
> |
return NoResult; |
625 |
> |
}}; |
626 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
627 |
> |
} |
628 |
> |
|
629 |
> |
/** |
630 |
> |
* A reinitialized abnormally completed task may be re-invoked |
631 |
> |
*/ |
632 |
> |
public void testReinitializeAbnormal() { |
633 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
634 |
> |
public Integer realCompute() { |
635 |
> |
FailingFibTask f = new FailingFibTask(8); |
636 |
> |
checkNotDone(f); |
637 |
> |
|
638 |
> |
for (int i = 0; i < 3; i++) { |
639 |
> |
try { |
640 |
> |
f.invoke(); |
641 |
> |
shouldThrow(); |
642 |
> |
} catch (FJException success) { |
643 |
> |
checkCompletedAbnormally(f, success); |
644 |
> |
} |
645 |
> |
f.reinitialize(); |
646 |
> |
checkNotDone(f); |
647 |
> |
} |
648 |
|
return NoResult; |
649 |
|
}}; |
650 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
661 |
|
try { |
662 |
|
Integer r = f.invoke(); |
663 |
|
shouldThrow(); |
664 |
< |
return r; |
665 |
< |
} catch (FJException success) {} |
664 |
> |
} catch (FJException success) { |
665 |
> |
checkCompletedAbnormally(f, success); |
666 |
> |
} |
667 |
|
return NoResult; |
668 |
|
}}; |
669 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
678 |
|
FibTask f = new FibTask(8); |
679 |
|
f.complete(NoResult); |
680 |
|
Integer r = f.invoke(); |
527 |
– |
assertTrue(f.isDone()); |
681 |
|
assertSame(NoResult, r); |
682 |
+ |
checkCompletedNormally(f, NoResult); |
683 |
|
return r; |
684 |
|
}}; |
685 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
694 |
|
FibTask f = new FibTask(8); |
695 |
|
FibTask g = new FibTask(9); |
696 |
|
invokeAll(f, g); |
697 |
< |
assertTrue(f.isDone()); |
698 |
< |
assertEquals(21, (int) f.join()); |
545 |
< |
assertTrue(g.isDone()); |
546 |
< |
assertEquals(34, (int) g.join()); |
697 |
> |
checkCompletedNormally(f, 21); |
698 |
> |
checkCompletedNormally(g, 34); |
699 |
|
return NoResult; |
700 |
|
}}; |
701 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
709 |
|
public Integer realCompute() { |
710 |
|
FibTask f = new FibTask(8); |
711 |
|
invokeAll(f); |
712 |
< |
assertTrue(f.isDone()); |
561 |
< |
assertEquals(21, (int) f.join()); |
712 |
> |
checkCompletedNormally(f, 21); |
713 |
|
return NoResult; |
714 |
|
}}; |
715 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
726 |
|
FibTask h = new FibTask(7); |
727 |
|
invokeAll(f, g, h); |
728 |
|
assertTrue(f.isDone()); |
578 |
– |
assertEquals(21, (int) f.join()); |
729 |
|
assertTrue(g.isDone()); |
580 |
– |
assertEquals(34, (int) g.join()); |
730 |
|
assertTrue(h.isDone()); |
731 |
< |
assertEquals(13, (int) h.join()); |
731 |
> |
checkCompletedNormally(f, 21); |
732 |
> |
checkCompletedNormally(g, 34); |
733 |
> |
checkCompletedNormally(h, 13); |
734 |
|
return NoResult; |
735 |
|
}}; |
736 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
751 |
|
set.add(h); |
752 |
|
invokeAll(set); |
753 |
|
assertTrue(f.isDone()); |
603 |
– |
assertEquals(21, (int) f.join()); |
754 |
|
assertTrue(g.isDone()); |
605 |
– |
assertEquals(34, (int) g.join()); |
755 |
|
assertTrue(h.isDone()); |
756 |
< |
assertEquals(13, (int) h.join()); |
756 |
> |
checkCompletedNormally(f, 21); |
757 |
> |
checkCompletedNormally(g, 34); |
758 |
> |
checkCompletedNormally(h, 13); |
759 |
|
return NoResult; |
760 |
|
}}; |
761 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
762 |
|
} |
763 |
|
|
764 |
+ |
/** |
765 |
+ |
* invokeAll(tasks) with any null task throws NPE |
766 |
+ |
*/ |
767 |
+ |
public void testInvokeAllNPE() { |
768 |
+ |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
769 |
+ |
public Integer realCompute() { |
770 |
+ |
FibTask f = new FibTask(8); |
771 |
+ |
FibTask g = new FibTask(9); |
772 |
+ |
FibTask h = null; |
773 |
+ |
try { |
774 |
+ |
invokeAll(f, g, h); |
775 |
+ |
shouldThrow(); |
776 |
+ |
} catch (NullPointerException success) {} |
777 |
+ |
return NoResult; |
778 |
+ |
}}; |
779 |
+ |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
780 |
+ |
} |
781 |
|
|
782 |
|
/** |
783 |
|
* invokeAll(t1, t2) throw exception if any task does |
790 |
|
try { |
791 |
|
invokeAll(f, g); |
792 |
|
shouldThrow(); |
793 |
< |
return NoResult; |
794 |
< |
} catch (FJException success) {} |
793 |
> |
} catch (FJException success) { |
794 |
> |
checkCompletedAbnormally(g, success); |
795 |
> |
} |
796 |
|
return NoResult; |
797 |
|
}}; |
798 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
808 |
|
try { |
809 |
|
invokeAll(g); |
810 |
|
shouldThrow(); |
811 |
< |
return NoResult; |
812 |
< |
} catch (FJException success) {} |
811 |
> |
} catch (FJException success) { |
812 |
> |
checkCompletedAbnormally(g, success); |
813 |
> |
} |
814 |
|
return NoResult; |
815 |
|
}}; |
816 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
828 |
|
try { |
829 |
|
invokeAll(f, g, h); |
830 |
|
shouldThrow(); |
831 |
< |
return NoResult; |
832 |
< |
} catch (FJException success) {} |
831 |
> |
} catch (FJException success) { |
832 |
> |
checkCompletedAbnormally(g, success); |
833 |
> |
} |
834 |
|
return NoResult; |
835 |
|
}}; |
836 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
852 |
|
try { |
853 |
|
invokeAll(set); |
854 |
|
shouldThrow(); |
855 |
< |
return NoResult; |
856 |
< |
} catch (FJException success) {} |
855 |
> |
} catch (FJException success) { |
856 |
> |
checkCompletedAbnormally(f, success); |
857 |
> |
} |
858 |
|
return NoResult; |
859 |
|
}}; |
860 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
873 |
|
assertSame(f, f.fork()); |
874 |
|
assertTrue(f.tryUnfork()); |
875 |
|
helpQuiesce(); |
876 |
< |
assertFalse(f.isDone()); |
877 |
< |
assertTrue(g.isDone()); |
876 |
> |
checkNotDone(f); |
877 |
> |
checkCompletedNormally(g, 34); |
878 |
|
return NoResult; |
879 |
|
}}; |
880 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
895 |
|
assertSame(f, f.fork()); |
896 |
|
assertTrue(getSurplusQueuedTaskCount() > 0); |
897 |
|
helpQuiesce(); |
898 |
+ |
assertEquals(0, getSurplusQueuedTaskCount()); |
899 |
+ |
checkCompletedNormally(f, 21); |
900 |
+ |
checkCompletedNormally(g, 34); |
901 |
+ |
checkCompletedNormally(h, 13); |
902 |
|
return NoResult; |
903 |
|
}}; |
904 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
915 |
|
FibTask f = new FibTask(8); |
916 |
|
assertSame(f, f.fork()); |
917 |
|
assertSame(f, peekNextLocalTask()); |
918 |
< |
assertEquals(21, (int) f.join()); |
743 |
< |
assertTrue(f.isDone()); |
918 |
> |
checkCompletesNormally(f, 21); |
919 |
|
helpQuiesce(); |
920 |
+ |
checkCompletedNormally(g, 34); |
921 |
|
return NoResult; |
922 |
|
}}; |
923 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
936 |
|
assertSame(f, f.fork()); |
937 |
|
assertSame(f, pollNextLocalTask()); |
938 |
|
helpQuiesce(); |
939 |
< |
assertFalse(f.isDone()); |
939 |
> |
checkNotDone(f); |
940 |
> |
checkCompletedNormally(g, 34); |
941 |
|
return NoResult; |
942 |
|
}}; |
943 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
955 |
|
assertSame(f, f.fork()); |
956 |
|
assertSame(f, pollTask()); |
957 |
|
helpQuiesce(); |
958 |
< |
assertFalse(f.isDone()); |
959 |
< |
assertTrue(g.isDone()); |
958 |
> |
checkNotDone(f); |
959 |
> |
checkCompletedNormally(g, 34); |
960 |
|
return NoResult; |
961 |
|
}}; |
962 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
975 |
|
assertSame(g, peekNextLocalTask()); |
976 |
|
assertEquals(21, (int) f.join()); |
977 |
|
helpQuiesce(); |
978 |
< |
assertTrue(f.isDone()); |
978 |
> |
checkCompletedNormally(f, 21); |
979 |
> |
checkCompletedNormally(g, 34); |
980 |
|
return NoResult; |
981 |
|
}}; |
982 |
|
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
983 |
|
} |
984 |
|
|
985 |
|
/** |
986 |
< |
* pollNextLocalTask returns least recent unexecuted task |
987 |
< |
* without executing it, in async mode |
986 |
> |
* pollNextLocalTask returns least recent unexecuted task without |
987 |
> |
* executing it, in async mode |
988 |
|
*/ |
989 |
|
public void testPollNextLocalTaskAsync() { |
990 |
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
995 |
|
assertSame(f, f.fork()); |
996 |
|
assertSame(g, pollNextLocalTask()); |
997 |
|
helpQuiesce(); |
998 |
< |
assertTrue(f.isDone()); |
999 |
< |
assertFalse(g.isDone()); |
998 |
> |
checkCompletedNormally(f, 21); |
999 |
> |
checkNotDone(g); |
1000 |
|
return NoResult; |
1001 |
|
}}; |
1002 |
|
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
1003 |
|
} |
1004 |
|
|
1005 |
|
/** |
1006 |
< |
* pollTask returns an unexecuted task |
1007 |
< |
* without executing it, in async mode |
1006 |
> |
* pollTask returns an unexecuted task without executing it, in |
1007 |
> |
* async mode |
1008 |
|
*/ |
1009 |
|
public void testPollTaskAsync() { |
1010 |
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
1015 |
|
assertSame(f, f.fork()); |
1016 |
|
assertSame(g, pollTask()); |
1017 |
|
helpQuiesce(); |
1018 |
< |
assertTrue(f.isDone()); |
1019 |
< |
assertFalse(g.isDone()); |
1018 |
> |
checkCompletedNormally(f, 21); |
1019 |
> |
checkNotDone(g); |
1020 |
|
return NoResult; |
1021 |
|
}}; |
1022 |
|
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |