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/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.RecursiveAction; |
14 |
import java.util.concurrent.CountedCompleter; |
15 |
import java.util.concurrent.ThreadLocalRandom; |
16 |
import java.util.concurrent.TimeUnit; |
17 |
import java.util.concurrent.TimeoutException; |
18 |
import static java.util.concurrent.TimeUnit.SECONDS; |
19 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
20 |
import java.util.Arrays; |
21 |
import java.util.HashSet; |
22 |
|
23 |
public class ForkJoinPool8Test extends JSR166TestCase { |
24 |
public static void main(String[] args) { |
25 |
junit.textui.TestRunner.run(suite()); |
26 |
} |
27 |
|
28 |
public static Test suite() { |
29 |
return new TestSuite(ForkJoinPool8Test.class); |
30 |
} |
31 |
|
32 |
/** |
33 |
* Common pool exists and has expected parallelism. |
34 |
*/ |
35 |
public void testCommonPoolParallelism() { |
36 |
assertEquals(ForkJoinPool.getCommonPoolParallelism(), |
37 |
ForkJoinPool.commonPool().getParallelism()); |
38 |
} |
39 |
|
40 |
/** |
41 |
* Common pool cannot be shut down |
42 |
*/ |
43 |
public void testCommonPoolShutDown() { |
44 |
assertFalse(ForkJoinPool.commonPool().isShutdown()); |
45 |
assertFalse(ForkJoinPool.commonPool().isTerminating()); |
46 |
assertFalse(ForkJoinPool.commonPool().isTerminated()); |
47 |
ForkJoinPool.commonPool().shutdown(); |
48 |
assertFalse(ForkJoinPool.commonPool().isShutdown()); |
49 |
assertFalse(ForkJoinPool.commonPool().isTerminating()); |
50 |
assertFalse(ForkJoinPool.commonPool().isTerminated()); |
51 |
ForkJoinPool.commonPool().shutdownNow(); |
52 |
assertFalse(ForkJoinPool.commonPool().isShutdown()); |
53 |
assertFalse(ForkJoinPool.commonPool().isTerminating()); |
54 |
assertFalse(ForkJoinPool.commonPool().isTerminated()); |
55 |
} |
56 |
|
57 |
/* |
58 |
* All of the following test methods are adaptations of those for |
59 |
* RecursiveAction and CountedCompleter, but with all actions |
60 |
* executed in the common pool, generally implicitly via |
61 |
* checkInvoke. |
62 |
*/ |
63 |
|
64 |
private void checkInvoke(ForkJoinTask a) { |
65 |
checkNotDone(a); |
66 |
assertNull(a.invoke()); |
67 |
checkCompletedNormally(a); |
68 |
} |
69 |
|
70 |
void checkNotDone(ForkJoinTask a) { |
71 |
assertFalse(a.isDone()); |
72 |
assertFalse(a.isCompletedNormally()); |
73 |
assertFalse(a.isCompletedAbnormally()); |
74 |
assertFalse(a.isCancelled()); |
75 |
assertNull(a.getException()); |
76 |
assertNull(a.getRawResult()); |
77 |
|
78 |
if (! ForkJoinTask.inForkJoinPool()) { |
79 |
Thread.currentThread().interrupt(); |
80 |
try { |
81 |
a.get(); |
82 |
shouldThrow(); |
83 |
} catch (InterruptedException success) { |
84 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
85 |
|
86 |
Thread.currentThread().interrupt(); |
87 |
try { |
88 |
a.get(5L, SECONDS); |
89 |
shouldThrow(); |
90 |
} catch (InterruptedException success) { |
91 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
92 |
} |
93 |
|
94 |
try { |
95 |
a.get(0L, SECONDS); |
96 |
shouldThrow(); |
97 |
} catch (TimeoutException success) { |
98 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
99 |
} |
100 |
|
101 |
void checkCompletedNormally(ForkJoinTask a) { |
102 |
assertTrue(a.isDone()); |
103 |
assertFalse(a.isCancelled()); |
104 |
assertTrue(a.isCompletedNormally()); |
105 |
assertFalse(a.isCompletedAbnormally()); |
106 |
assertNull(a.getException()); |
107 |
assertNull(a.getRawResult()); |
108 |
assertNull(a.join()); |
109 |
assertFalse(a.cancel(false)); |
110 |
assertFalse(a.cancel(true)); |
111 |
try { |
112 |
assertNull(a.get()); |
113 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
114 |
try { |
115 |
assertNull(a.get(5L, SECONDS)); |
116 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
117 |
} |
118 |
|
119 |
void checkCancelled(ForkJoinTask a) { |
120 |
assertTrue(a.isDone()); |
121 |
assertTrue(a.isCancelled()); |
122 |
assertFalse(a.isCompletedNormally()); |
123 |
assertTrue(a.isCompletedAbnormally()); |
124 |
assertTrue(a.getException() instanceof CancellationException); |
125 |
assertNull(a.getRawResult()); |
126 |
|
127 |
try { |
128 |
a.join(); |
129 |
shouldThrow(); |
130 |
} catch (CancellationException success) { |
131 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
132 |
|
133 |
try { |
134 |
a.get(); |
135 |
shouldThrow(); |
136 |
} catch (CancellationException success) { |
137 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
138 |
|
139 |
try { |
140 |
a.get(5L, SECONDS); |
141 |
shouldThrow(); |
142 |
} catch (CancellationException success) { |
143 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
144 |
} |
145 |
|
146 |
void checkCompletedAbnormally(ForkJoinTask a, Throwable t) { |
147 |
assertTrue(a.isDone()); |
148 |
assertFalse(a.isCancelled()); |
149 |
assertFalse(a.isCompletedNormally()); |
150 |
assertTrue(a.isCompletedAbnormally()); |
151 |
assertSame(t.getClass(), a.getException().getClass()); |
152 |
assertNull(a.getRawResult()); |
153 |
assertFalse(a.cancel(false)); |
154 |
assertFalse(a.cancel(true)); |
155 |
|
156 |
try { |
157 |
a.join(); |
158 |
shouldThrow(); |
159 |
} catch (Throwable expected) { |
160 |
assertSame(expected.getClass(), t.getClass()); |
161 |
} |
162 |
|
163 |
try { |
164 |
a.get(); |
165 |
shouldThrow(); |
166 |
} catch (ExecutionException success) { |
167 |
assertSame(t.getClass(), success.getCause().getClass()); |
168 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
169 |
|
170 |
try { |
171 |
a.get(5L, SECONDS); |
172 |
shouldThrow(); |
173 |
} catch (ExecutionException success) { |
174 |
assertSame(t.getClass(), success.getCause().getClass()); |
175 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
176 |
} |
177 |
|
178 |
public static final class FJException extends RuntimeException { |
179 |
public FJException() { super(); } |
180 |
public FJException(Throwable cause) { super(cause); } |
181 |
} |
182 |
|
183 |
// A simple recursive action for testing |
184 |
final class FibAction extends CheckedRecursiveAction { |
185 |
final int number; |
186 |
int result; |
187 |
FibAction(int n) { number = n; } |
188 |
protected void realCompute() { |
189 |
int n = number; |
190 |
if (n <= 1) |
191 |
result = n; |
192 |
else { |
193 |
FibAction f1 = new FibAction(n - 1); |
194 |
FibAction f2 = new FibAction(n - 2); |
195 |
invokeAll(f1, f2); |
196 |
result = f1.result + f2.result; |
197 |
} |
198 |
} |
199 |
} |
200 |
|
201 |
// A recursive action failing in base case |
202 |
static final class FailingFibAction extends RecursiveAction { |
203 |
final int number; |
204 |
int result; |
205 |
FailingFibAction(int n) { number = n; } |
206 |
public void compute() { |
207 |
int n = number; |
208 |
if (n <= 1) |
209 |
throw new FJException(); |
210 |
else { |
211 |
FailingFibAction f1 = new FailingFibAction(n - 1); |
212 |
FailingFibAction f2 = new FailingFibAction(n - 2); |
213 |
invokeAll(f1, f2); |
214 |
result = f1.result + f2.result; |
215 |
} |
216 |
} |
217 |
} |
218 |
|
219 |
/** |
220 |
* invoke returns when task completes normally. |
221 |
* isCompletedAbnormally and isCancelled return false for normally |
222 |
* completed tasks. getRawResult of a RecursiveAction returns null; |
223 |
*/ |
224 |
public void testInvoke() { |
225 |
RecursiveAction a = new CheckedRecursiveAction() { |
226 |
protected void realCompute() { |
227 |
FibAction f = new FibAction(8); |
228 |
assertNull(f.invoke()); |
229 |
assertEquals(21, f.result); |
230 |
checkCompletedNormally(f); |
231 |
}}; |
232 |
checkInvoke(a); |
233 |
} |
234 |
|
235 |
/** |
236 |
* quietlyInvoke task returns when task completes normally. |
237 |
* isCompletedAbnormally and isCancelled return false for normally |
238 |
* completed tasks |
239 |
*/ |
240 |
public void testQuietlyInvoke() { |
241 |
RecursiveAction a = new CheckedRecursiveAction() { |
242 |
protected void realCompute() { |
243 |
FibAction f = new FibAction(8); |
244 |
f.quietlyInvoke(); |
245 |
assertEquals(21, f.result); |
246 |
checkCompletedNormally(f); |
247 |
}}; |
248 |
checkInvoke(a); |
249 |
} |
250 |
|
251 |
/** |
252 |
* join of a forked task returns when task completes |
253 |
*/ |
254 |
public void testForkJoin() { |
255 |
RecursiveAction a = new CheckedRecursiveAction() { |
256 |
protected void realCompute() { |
257 |
FibAction f = new FibAction(8); |
258 |
assertSame(f, f.fork()); |
259 |
assertNull(f.join()); |
260 |
assertEquals(21, f.result); |
261 |
checkCompletedNormally(f); |
262 |
}}; |
263 |
checkInvoke(a); |
264 |
} |
265 |
|
266 |
/** |
267 |
* join/quietlyJoin of a forked task succeeds in the presence of interrupts |
268 |
*/ |
269 |
public void testJoinIgnoresInterrupts() { |
270 |
RecursiveAction a = new CheckedRecursiveAction() { |
271 |
protected void realCompute() { |
272 |
FibAction f = new FibAction(8); |
273 |
final Thread myself = Thread.currentThread(); |
274 |
|
275 |
// test join() |
276 |
assertSame(f, f.fork()); |
277 |
myself.interrupt(); |
278 |
assertTrue(myself.isInterrupted()); |
279 |
assertNull(f.join()); |
280 |
Thread.interrupted(); |
281 |
assertEquals(21, f.result); |
282 |
checkCompletedNormally(f); |
283 |
|
284 |
f = new FibAction(8); |
285 |
f.cancel(true); |
286 |
assertSame(f, f.fork()); |
287 |
myself.interrupt(); |
288 |
assertTrue(myself.isInterrupted()); |
289 |
try { |
290 |
f.join(); |
291 |
shouldThrow(); |
292 |
} catch (CancellationException success) { |
293 |
Thread.interrupted(); |
294 |
checkCancelled(f); |
295 |
} |
296 |
|
297 |
f = new FibAction(8); |
298 |
f.completeExceptionally(new FJException()); |
299 |
assertSame(f, f.fork()); |
300 |
myself.interrupt(); |
301 |
assertTrue(myself.isInterrupted()); |
302 |
try { |
303 |
f.join(); |
304 |
shouldThrow(); |
305 |
} catch (FJException success) { |
306 |
Thread.interrupted(); |
307 |
checkCompletedAbnormally(f, success); |
308 |
} |
309 |
|
310 |
// test quietlyJoin() |
311 |
f = new FibAction(8); |
312 |
assertSame(f, f.fork()); |
313 |
myself.interrupt(); |
314 |
assertTrue(myself.isInterrupted()); |
315 |
f.quietlyJoin(); |
316 |
Thread.interrupted(); |
317 |
assertEquals(21, f.result); |
318 |
checkCompletedNormally(f); |
319 |
|
320 |
f = new FibAction(8); |
321 |
f.cancel(true); |
322 |
assertSame(f, f.fork()); |
323 |
myself.interrupt(); |
324 |
assertTrue(myself.isInterrupted()); |
325 |
f.quietlyJoin(); |
326 |
Thread.interrupted(); |
327 |
checkCancelled(f); |
328 |
|
329 |
f = new FibAction(8); |
330 |
f.completeExceptionally(new FJException()); |
331 |
assertSame(f, f.fork()); |
332 |
myself.interrupt(); |
333 |
assertTrue(myself.isInterrupted()); |
334 |
f.quietlyJoin(); |
335 |
Thread.interrupted(); |
336 |
checkCompletedAbnormally(f, f.getException()); |
337 |
}}; |
338 |
checkInvoke(a); |
339 |
a.reinitialize(); |
340 |
checkInvoke(a); |
341 |
} |
342 |
|
343 |
|
344 |
/** |
345 |
* get of a forked task returns when task completes |
346 |
*/ |
347 |
public void testForkGet() { |
348 |
RecursiveAction a = new CheckedRecursiveAction() { |
349 |
protected void realCompute() throws Exception { |
350 |
FibAction f = new FibAction(8); |
351 |
assertSame(f, f.fork()); |
352 |
assertNull(f.get()); |
353 |
assertEquals(21, f.result); |
354 |
checkCompletedNormally(f); |
355 |
}}; |
356 |
checkInvoke(a); |
357 |
} |
358 |
|
359 |
/** |
360 |
* timed get of a forked task returns when task completes |
361 |
*/ |
362 |
public void testForkTimedGet() { |
363 |
RecursiveAction a = new CheckedRecursiveAction() { |
364 |
protected void realCompute() throws Exception { |
365 |
FibAction f = new FibAction(8); |
366 |
assertSame(f, f.fork()); |
367 |
assertNull(f.get(5L, SECONDS)); |
368 |
assertEquals(21, f.result); |
369 |
checkCompletedNormally(f); |
370 |
}}; |
371 |
checkInvoke(a); |
372 |
} |
373 |
|
374 |
/** |
375 |
* timed get with null time unit throws NPE |
376 |
*/ |
377 |
public void testForkTimedGetNPE() { |
378 |
RecursiveAction a = new CheckedRecursiveAction() { |
379 |
protected void realCompute() throws Exception { |
380 |
FibAction f = new FibAction(8); |
381 |
assertSame(f, f.fork()); |
382 |
try { |
383 |
f.get(5L, null); |
384 |
shouldThrow(); |
385 |
} catch (NullPointerException success) {} |
386 |
}}; |
387 |
checkInvoke(a); |
388 |
} |
389 |
|
390 |
/** |
391 |
* quietlyJoin of a forked task returns when task completes |
392 |
*/ |
393 |
public void testForkQuietlyJoin() { |
394 |
RecursiveAction a = new CheckedRecursiveAction() { |
395 |
protected void realCompute() { |
396 |
FibAction f = new FibAction(8); |
397 |
assertSame(f, f.fork()); |
398 |
f.quietlyJoin(); |
399 |
assertEquals(21, f.result); |
400 |
checkCompletedNormally(f); |
401 |
}}; |
402 |
checkInvoke(a); |
403 |
} |
404 |
|
405 |
/** |
406 |
* invoke task throws exception when task completes abnormally |
407 |
*/ |
408 |
public void testAbnormalInvoke() { |
409 |
RecursiveAction a = new CheckedRecursiveAction() { |
410 |
protected void realCompute() { |
411 |
FailingFibAction f = new FailingFibAction(8); |
412 |
try { |
413 |
f.invoke(); |
414 |
shouldThrow(); |
415 |
} catch (FJException success) { |
416 |
checkCompletedAbnormally(f, success); |
417 |
} |
418 |
}}; |
419 |
checkInvoke(a); |
420 |
} |
421 |
|
422 |
/** |
423 |
* quietlyInvoke task returns when task completes abnormally |
424 |
*/ |
425 |
public void testAbnormalQuietlyInvoke() { |
426 |
RecursiveAction a = new CheckedRecursiveAction() { |
427 |
protected void realCompute() { |
428 |
FailingFibAction f = new FailingFibAction(8); |
429 |
f.quietlyInvoke(); |
430 |
assertTrue(f.getException() instanceof FJException); |
431 |
checkCompletedAbnormally(f, f.getException()); |
432 |
}}; |
433 |
checkInvoke(a); |
434 |
} |
435 |
|
436 |
/** |
437 |
* join of a forked task throws exception when task completes abnormally |
438 |
*/ |
439 |
public void testAbnormalForkJoin() { |
440 |
RecursiveAction a = new CheckedRecursiveAction() { |
441 |
protected void realCompute() { |
442 |
FailingFibAction f = new FailingFibAction(8); |
443 |
assertSame(f, f.fork()); |
444 |
try { |
445 |
f.join(); |
446 |
shouldThrow(); |
447 |
} catch (FJException success) { |
448 |
checkCompletedAbnormally(f, success); |
449 |
} |
450 |
}}; |
451 |
checkInvoke(a); |
452 |
} |
453 |
|
454 |
/** |
455 |
* get of a forked task throws exception when task completes abnormally |
456 |
*/ |
457 |
public void testAbnormalForkGet() { |
458 |
RecursiveAction a = new CheckedRecursiveAction() { |
459 |
protected void realCompute() throws Exception { |
460 |
FailingFibAction f = new FailingFibAction(8); |
461 |
assertSame(f, f.fork()); |
462 |
try { |
463 |
f.get(); |
464 |
shouldThrow(); |
465 |
} catch (ExecutionException success) { |
466 |
Throwable cause = success.getCause(); |
467 |
assertTrue(cause instanceof FJException); |
468 |
checkCompletedAbnormally(f, cause); |
469 |
} |
470 |
}}; |
471 |
checkInvoke(a); |
472 |
} |
473 |
|
474 |
/** |
475 |
* timed get of a forked task throws exception when task completes abnormally |
476 |
*/ |
477 |
public void testAbnormalForkTimedGet() { |
478 |
RecursiveAction a = new CheckedRecursiveAction() { |
479 |
protected void realCompute() throws Exception { |
480 |
FailingFibAction f = new FailingFibAction(8); |
481 |
assertSame(f, f.fork()); |
482 |
try { |
483 |
f.get(5L, TimeUnit.SECONDS); |
484 |
shouldThrow(); |
485 |
} catch (ExecutionException success) { |
486 |
Throwable cause = success.getCause(); |
487 |
assertTrue(cause instanceof FJException); |
488 |
checkCompletedAbnormally(f, cause); |
489 |
} |
490 |
}}; |
491 |
checkInvoke(a); |
492 |
} |
493 |
|
494 |
/** |
495 |
* quietlyJoin of a forked task returns when task completes abnormally |
496 |
*/ |
497 |
public void testAbnormalForkQuietlyJoin() { |
498 |
RecursiveAction a = new CheckedRecursiveAction() { |
499 |
protected void realCompute() { |
500 |
FailingFibAction f = new FailingFibAction(8); |
501 |
assertSame(f, f.fork()); |
502 |
f.quietlyJoin(); |
503 |
assertTrue(f.getException() instanceof FJException); |
504 |
checkCompletedAbnormally(f, f.getException()); |
505 |
}}; |
506 |
checkInvoke(a); |
507 |
} |
508 |
|
509 |
/** |
510 |
* invoke task throws exception when task cancelled |
511 |
*/ |
512 |
public void testCancelledInvoke() { |
513 |
RecursiveAction a = new CheckedRecursiveAction() { |
514 |
protected void realCompute() { |
515 |
FibAction f = new FibAction(8); |
516 |
assertTrue(f.cancel(true)); |
517 |
try { |
518 |
f.invoke(); |
519 |
shouldThrow(); |
520 |
} catch (CancellationException success) { |
521 |
checkCancelled(f); |
522 |
} |
523 |
}}; |
524 |
checkInvoke(a); |
525 |
} |
526 |
|
527 |
/** |
528 |
* join of a forked task throws exception when task cancelled |
529 |
*/ |
530 |
public void testCancelledForkJoin() { |
531 |
RecursiveAction a = new CheckedRecursiveAction() { |
532 |
protected void realCompute() { |
533 |
FibAction f = new FibAction(8); |
534 |
assertTrue(f.cancel(true)); |
535 |
assertSame(f, f.fork()); |
536 |
try { |
537 |
f.join(); |
538 |
shouldThrow(); |
539 |
} catch (CancellationException success) { |
540 |
checkCancelled(f); |
541 |
} |
542 |
}}; |
543 |
checkInvoke(a); |
544 |
} |
545 |
|
546 |
/** |
547 |
* get of a forked task throws exception when task cancelled |
548 |
*/ |
549 |
public void testCancelledForkGet() { |
550 |
RecursiveAction a = new CheckedRecursiveAction() { |
551 |
protected void realCompute() throws Exception { |
552 |
FibAction f = new FibAction(8); |
553 |
assertTrue(f.cancel(true)); |
554 |
assertSame(f, f.fork()); |
555 |
try { |
556 |
f.get(); |
557 |
shouldThrow(); |
558 |
} catch (CancellationException success) { |
559 |
checkCancelled(f); |
560 |
} |
561 |
}}; |
562 |
checkInvoke(a); |
563 |
} |
564 |
|
565 |
/** |
566 |
* timed get of a forked task throws exception when task cancelled |
567 |
*/ |
568 |
public void testCancelledForkTimedGet() { |
569 |
RecursiveAction a = new CheckedRecursiveAction() { |
570 |
protected void realCompute() throws Exception { |
571 |
FibAction f = new FibAction(8); |
572 |
assertTrue(f.cancel(true)); |
573 |
assertSame(f, f.fork()); |
574 |
try { |
575 |
f.get(5L, SECONDS); |
576 |
shouldThrow(); |
577 |
} catch (CancellationException success) { |
578 |
checkCancelled(f); |
579 |
} |
580 |
}}; |
581 |
checkInvoke(a); |
582 |
} |
583 |
|
584 |
/** |
585 |
* quietlyJoin of a forked task returns when task cancelled |
586 |
*/ |
587 |
public void testCancelledForkQuietlyJoin() { |
588 |
RecursiveAction a = new CheckedRecursiveAction() { |
589 |
protected void realCompute() { |
590 |
FibAction f = new FibAction(8); |
591 |
assertTrue(f.cancel(true)); |
592 |
assertSame(f, f.fork()); |
593 |
f.quietlyJoin(); |
594 |
checkCancelled(f); |
595 |
}}; |
596 |
checkInvoke(a); |
597 |
} |
598 |
|
599 |
/** |
600 |
* inForkJoinPool of non-FJ task returns false |
601 |
*/ |
602 |
public void testInForkJoinPool2() { |
603 |
RecursiveAction a = new CheckedRecursiveAction() { |
604 |
protected void realCompute() { |
605 |
assertFalse(inForkJoinPool()); |
606 |
}}; |
607 |
assertNull(a.invoke()); |
608 |
} |
609 |
|
610 |
/** |
611 |
* A reinitialized normally completed task may be re-invoked |
612 |
*/ |
613 |
public void testReinitialize() { |
614 |
RecursiveAction a = new CheckedRecursiveAction() { |
615 |
protected void realCompute() { |
616 |
FibAction f = new FibAction(8); |
617 |
checkNotDone(f); |
618 |
|
619 |
for (int i = 0; i < 3; i++) { |
620 |
assertNull(f.invoke()); |
621 |
assertEquals(21, f.result); |
622 |
checkCompletedNormally(f); |
623 |
f.reinitialize(); |
624 |
checkNotDone(f); |
625 |
} |
626 |
}}; |
627 |
checkInvoke(a); |
628 |
} |
629 |
|
630 |
/** |
631 |
* A reinitialized abnormally completed task may be re-invoked |
632 |
*/ |
633 |
public void testReinitializeAbnormal() { |
634 |
RecursiveAction a = new CheckedRecursiveAction() { |
635 |
protected void realCompute() { |
636 |
FailingFibAction f = new FailingFibAction(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 |
}}; |
650 |
checkInvoke(a); |
651 |
} |
652 |
|
653 |
/** |
654 |
* invoke task throws exception after invoking completeExceptionally |
655 |
*/ |
656 |
public void testCompleteExceptionally() { |
657 |
RecursiveAction a = new CheckedRecursiveAction() { |
658 |
protected void realCompute() { |
659 |
FibAction f = new FibAction(8); |
660 |
f.completeExceptionally(new FJException()); |
661 |
try { |
662 |
f.invoke(); |
663 |
shouldThrow(); |
664 |
} catch (FJException success) { |
665 |
checkCompletedAbnormally(f, success); |
666 |
} |
667 |
}}; |
668 |
checkInvoke(a); |
669 |
} |
670 |
|
671 |
/** |
672 |
* invoke task suppresses execution invoking complete |
673 |
*/ |
674 |
public void testComplete() { |
675 |
RecursiveAction a = new CheckedRecursiveAction() { |
676 |
protected void realCompute() { |
677 |
FibAction f = new FibAction(8); |
678 |
f.complete(null); |
679 |
assertNull(f.invoke()); |
680 |
assertEquals(0, f.result); |
681 |
checkCompletedNormally(f); |
682 |
}}; |
683 |
checkInvoke(a); |
684 |
} |
685 |
|
686 |
/** |
687 |
* invokeAll(t1, t2) invokes all task arguments |
688 |
*/ |
689 |
public void testInvokeAll2() { |
690 |
RecursiveAction a = new CheckedRecursiveAction() { |
691 |
protected void realCompute() { |
692 |
FibAction f = new FibAction(8); |
693 |
FibAction g = new FibAction(9); |
694 |
invokeAll(f, g); |
695 |
checkCompletedNormally(f); |
696 |
assertEquals(21, f.result); |
697 |
checkCompletedNormally(g); |
698 |
assertEquals(34, g.result); |
699 |
}}; |
700 |
checkInvoke(a); |
701 |
} |
702 |
|
703 |
/** |
704 |
* invokeAll(tasks) with 1 argument invokes task |
705 |
*/ |
706 |
public void testInvokeAll1() { |
707 |
RecursiveAction a = new CheckedRecursiveAction() { |
708 |
protected void realCompute() { |
709 |
FibAction f = new FibAction(8); |
710 |
invokeAll(f); |
711 |
checkCompletedNormally(f); |
712 |
assertEquals(21, f.result); |
713 |
}}; |
714 |
checkInvoke(a); |
715 |
} |
716 |
|
717 |
/** |
718 |
* invokeAll(tasks) with > 2 argument invokes tasks |
719 |
*/ |
720 |
public void testInvokeAll3() { |
721 |
RecursiveAction a = new CheckedRecursiveAction() { |
722 |
protected void realCompute() { |
723 |
FibAction f = new FibAction(8); |
724 |
FibAction g = new FibAction(9); |
725 |
FibAction h = new FibAction(7); |
726 |
invokeAll(f, g, h); |
727 |
assertTrue(f.isDone()); |
728 |
assertTrue(g.isDone()); |
729 |
assertTrue(h.isDone()); |
730 |
checkCompletedNormally(f); |
731 |
assertEquals(21, f.result); |
732 |
checkCompletedNormally(g); |
733 |
assertEquals(34, g.result); |
734 |
checkCompletedNormally(g); |
735 |
assertEquals(13, h.result); |
736 |
}}; |
737 |
checkInvoke(a); |
738 |
} |
739 |
|
740 |
/** |
741 |
* invokeAll(collection) invokes all tasks in the collection |
742 |
*/ |
743 |
public void testInvokeAllCollection() { |
744 |
RecursiveAction a = new CheckedRecursiveAction() { |
745 |
protected void realCompute() { |
746 |
FibAction f = new FibAction(8); |
747 |
FibAction g = new FibAction(9); |
748 |
FibAction h = new FibAction(7); |
749 |
HashSet set = new HashSet(); |
750 |
set.add(f); |
751 |
set.add(g); |
752 |
set.add(h); |
753 |
invokeAll(set); |
754 |
assertTrue(f.isDone()); |
755 |
assertTrue(g.isDone()); |
756 |
assertTrue(h.isDone()); |
757 |
checkCompletedNormally(f); |
758 |
assertEquals(21, f.result); |
759 |
checkCompletedNormally(g); |
760 |
assertEquals(34, g.result); |
761 |
checkCompletedNormally(g); |
762 |
assertEquals(13, h.result); |
763 |
}}; |
764 |
checkInvoke(a); |
765 |
} |
766 |
|
767 |
/** |
768 |
* invokeAll(tasks) with any null task throws NPE |
769 |
*/ |
770 |
public void testInvokeAllNPE() { |
771 |
RecursiveAction a = new CheckedRecursiveAction() { |
772 |
protected void realCompute() { |
773 |
FibAction f = new FibAction(8); |
774 |
FibAction g = new FibAction(9); |
775 |
FibAction h = null; |
776 |
try { |
777 |
invokeAll(f, g, h); |
778 |
shouldThrow(); |
779 |
} catch (NullPointerException success) {} |
780 |
}}; |
781 |
checkInvoke(a); |
782 |
} |
783 |
|
784 |
/** |
785 |
* invokeAll(t1, t2) throw exception if any task does |
786 |
*/ |
787 |
public void testAbnormalInvokeAll2() { |
788 |
RecursiveAction a = new CheckedRecursiveAction() { |
789 |
protected void realCompute() { |
790 |
FibAction f = new FibAction(8); |
791 |
FailingFibAction g = new FailingFibAction(9); |
792 |
try { |
793 |
invokeAll(f, g); |
794 |
shouldThrow(); |
795 |
} catch (FJException success) { |
796 |
checkCompletedAbnormally(g, success); |
797 |
} |
798 |
}}; |
799 |
checkInvoke(a); |
800 |
} |
801 |
|
802 |
/** |
803 |
* invokeAll(tasks) with 1 argument throws exception if task does |
804 |
*/ |
805 |
public void testAbnormalInvokeAll1() { |
806 |
RecursiveAction a = new CheckedRecursiveAction() { |
807 |
protected void realCompute() { |
808 |
FailingFibAction g = new FailingFibAction(9); |
809 |
try { |
810 |
invokeAll(g); |
811 |
shouldThrow(); |
812 |
} catch (FJException success) { |
813 |
checkCompletedAbnormally(g, success); |
814 |
} |
815 |
}}; |
816 |
checkInvoke(a); |
817 |
} |
818 |
|
819 |
/** |
820 |
* invokeAll(tasks) with > 2 argument throws exception if any task does |
821 |
*/ |
822 |
public void testAbnormalInvokeAll3() { |
823 |
RecursiveAction a = new CheckedRecursiveAction() { |
824 |
protected void realCompute() { |
825 |
FibAction f = new FibAction(8); |
826 |
FailingFibAction g = new FailingFibAction(9); |
827 |
FibAction h = new FibAction(7); |
828 |
try { |
829 |
invokeAll(f, g, h); |
830 |
shouldThrow(); |
831 |
} catch (FJException success) { |
832 |
checkCompletedAbnormally(g, success); |
833 |
} |
834 |
}}; |
835 |
checkInvoke(a); |
836 |
} |
837 |
|
838 |
/** |
839 |
* invokeAll(collection) throws exception if any task does |
840 |
*/ |
841 |
public void testAbnormalInvokeAllCollection() { |
842 |
RecursiveAction a = new CheckedRecursiveAction() { |
843 |
protected void realCompute() { |
844 |
FailingFibAction f = new FailingFibAction(8); |
845 |
FibAction g = new FibAction(9); |
846 |
FibAction h = new FibAction(7); |
847 |
HashSet set = new HashSet(); |
848 |
set.add(f); |
849 |
set.add(g); |
850 |
set.add(h); |
851 |
try { |
852 |
invokeAll(set); |
853 |
shouldThrow(); |
854 |
} catch (FJException success) { |
855 |
checkCompletedAbnormally(f, success); |
856 |
} |
857 |
}}; |
858 |
checkInvoke(a); |
859 |
} |
860 |
|
861 |
// CountedCompleter versions |
862 |
|
863 |
abstract static class CCF extends CountedCompleter { |
864 |
int number; |
865 |
int rnumber; |
866 |
|
867 |
public CCF(CountedCompleter parent, int n) { |
868 |
super(parent, 1); |
869 |
this.number = n; |
870 |
} |
871 |
|
872 |
public final void compute() { |
873 |
CountedCompleter p; |
874 |
CCF f = this; |
875 |
int n = number; |
876 |
while (n >= 2) { |
877 |
new RCCF(f, n - 2).fork(); |
878 |
f = new LCCF(f, --n); |
879 |
} |
880 |
f.number = n; |
881 |
f.onCompletion(f); |
882 |
if ((p = f.getCompleter()) != null) |
883 |
p.tryComplete(); |
884 |
else |
885 |
f.quietlyComplete(); |
886 |
} |
887 |
} |
888 |
|
889 |
static final class LCCF extends CCF { |
890 |
public LCCF(CountedCompleter parent, int n) { |
891 |
super(parent, n); |
892 |
} |
893 |
public final void onCompletion(CountedCompleter caller) { |
894 |
CCF p = (CCF)getCompleter(); |
895 |
int n = number + rnumber; |
896 |
if (p != null) |
897 |
p.number = n; |
898 |
else |
899 |
number = n; |
900 |
} |
901 |
} |
902 |
static final class RCCF extends CCF { |
903 |
public RCCF(CountedCompleter parent, int n) { |
904 |
super(parent, n); |
905 |
} |
906 |
public final void onCompletion(CountedCompleter caller) { |
907 |
CCF p = (CCF)getCompleter(); |
908 |
int n = number + rnumber; |
909 |
if (p != null) |
910 |
p.rnumber = n; |
911 |
else |
912 |
number = n; |
913 |
} |
914 |
} |
915 |
|
916 |
// Version of CCF with forced failure in left completions |
917 |
abstract static class FailingCCF extends CountedCompleter { |
918 |
int number; |
919 |
int rnumber; |
920 |
|
921 |
public FailingCCF(CountedCompleter parent, int n) { |
922 |
super(parent, 1); |
923 |
this.number = n; |
924 |
} |
925 |
|
926 |
public final void compute() { |
927 |
CountedCompleter p; |
928 |
FailingCCF f = this; |
929 |
int n = number; |
930 |
while (n >= 2) { |
931 |
new RFCCF(f, n - 2).fork(); |
932 |
f = new LFCCF(f, --n); |
933 |
} |
934 |
f.number = n; |
935 |
f.onCompletion(f); |
936 |
if ((p = f.getCompleter()) != null) |
937 |
p.tryComplete(); |
938 |
else |
939 |
f.quietlyComplete(); |
940 |
} |
941 |
} |
942 |
|
943 |
static final class LFCCF extends FailingCCF { |
944 |
public LFCCF(CountedCompleter parent, int n) { |
945 |
super(parent, n); |
946 |
} |
947 |
public final void onCompletion(CountedCompleter caller) { |
948 |
FailingCCF p = (FailingCCF)getCompleter(); |
949 |
int n = number + rnumber; |
950 |
if (p != null) |
951 |
p.number = n; |
952 |
else |
953 |
number = n; |
954 |
} |
955 |
} |
956 |
static final class RFCCF extends FailingCCF { |
957 |
public RFCCF(CountedCompleter parent, int n) { |
958 |
super(parent, n); |
959 |
} |
960 |
public final void onCompletion(CountedCompleter caller) { |
961 |
completeExceptionally(new FJException()); |
962 |
} |
963 |
} |
964 |
|
965 |
/** |
966 |
* invoke returns when task completes normally. |
967 |
* isCompletedAbnormally and isCancelled return false for normally |
968 |
* completed tasks; getRawResult returns null. |
969 |
*/ |
970 |
public void testInvokeCC() { |
971 |
ForkJoinTask a = new CheckedRecursiveAction() { |
972 |
protected void realCompute() { |
973 |
CCF f = new LCCF(null, 8); |
974 |
assertNull(f.invoke()); |
975 |
assertEquals(21, f.number); |
976 |
checkCompletedNormally(f); |
977 |
}}; |
978 |
checkInvoke(a); |
979 |
} |
980 |
|
981 |
/** |
982 |
* quietlyInvoke task returns when task completes normally. |
983 |
* isCompletedAbnormally and isCancelled return false for normally |
984 |
* completed tasks |
985 |
*/ |
986 |
public void testQuietlyInvokeCC() { |
987 |
ForkJoinTask a = new CheckedRecursiveAction() { |
988 |
protected void realCompute() { |
989 |
CCF f = new LCCF(null, 8); |
990 |
f.quietlyInvoke(); |
991 |
assertEquals(21, f.number); |
992 |
checkCompletedNormally(f); |
993 |
}}; |
994 |
checkInvoke(a); |
995 |
} |
996 |
|
997 |
/** |
998 |
* join of a forked task returns when task completes |
999 |
*/ |
1000 |
public void testForkJoinCC() { |
1001 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1002 |
protected void realCompute() { |
1003 |
CCF f = new LCCF(null, 8); |
1004 |
assertSame(f, f.fork()); |
1005 |
assertNull(f.join()); |
1006 |
assertEquals(21, f.number); |
1007 |
checkCompletedNormally(f); |
1008 |
}}; |
1009 |
checkInvoke(a); |
1010 |
} |
1011 |
|
1012 |
/** |
1013 |
* get of a forked task returns when task completes |
1014 |
*/ |
1015 |
public void testForkGetCC() { |
1016 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1017 |
protected void realCompute() throws Exception { |
1018 |
CCF f = new LCCF(null, 8); |
1019 |
assertSame(f, f.fork()); |
1020 |
assertNull(f.get()); |
1021 |
assertEquals(21, f.number); |
1022 |
checkCompletedNormally(f); |
1023 |
}}; |
1024 |
checkInvoke(a); |
1025 |
} |
1026 |
|
1027 |
/** |
1028 |
* timed get of a forked task returns when task completes |
1029 |
*/ |
1030 |
public void testForkTimedGetCC() { |
1031 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1032 |
protected void realCompute() throws Exception { |
1033 |
CCF f = new LCCF(null, 8); |
1034 |
assertSame(f, f.fork()); |
1035 |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
1036 |
assertEquals(21, f.number); |
1037 |
checkCompletedNormally(f); |
1038 |
}}; |
1039 |
checkInvoke(a); |
1040 |
} |
1041 |
|
1042 |
/** |
1043 |
* timed get with null time unit throws NPE |
1044 |
*/ |
1045 |
public void testForkTimedGetNPECC() { |
1046 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1047 |
protected void realCompute() throws Exception { |
1048 |
CCF f = new LCCF(null, 8); |
1049 |
assertSame(f, f.fork()); |
1050 |
try { |
1051 |
f.get(5L, null); |
1052 |
shouldThrow(); |
1053 |
} catch (NullPointerException success) {} |
1054 |
}}; |
1055 |
checkInvoke(a); |
1056 |
} |
1057 |
|
1058 |
/** |
1059 |
* quietlyJoin of a forked task returns when task completes |
1060 |
*/ |
1061 |
public void testForkQuietlyJoinCC() { |
1062 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1063 |
protected void realCompute() { |
1064 |
CCF f = new LCCF(null, 8); |
1065 |
assertSame(f, f.fork()); |
1066 |
f.quietlyJoin(); |
1067 |
assertEquals(21, f.number); |
1068 |
checkCompletedNormally(f); |
1069 |
}}; |
1070 |
checkInvoke(a); |
1071 |
} |
1072 |
|
1073 |
/** |
1074 |
* invoke task throws exception when task completes abnormally |
1075 |
*/ |
1076 |
public void testAbnormalInvokeCC() { |
1077 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1078 |
protected void realCompute() { |
1079 |
FailingCCF f = new LFCCF(null, 8); |
1080 |
try { |
1081 |
f.invoke(); |
1082 |
shouldThrow(); |
1083 |
} catch (FJException success) { |
1084 |
checkCompletedAbnormally(f, success); |
1085 |
} |
1086 |
}}; |
1087 |
checkInvoke(a); |
1088 |
} |
1089 |
|
1090 |
/** |
1091 |
* quietlyInvoke task returns when task completes abnormally |
1092 |
*/ |
1093 |
public void testAbnormalQuietlyInvokeCC() { |
1094 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1095 |
protected void realCompute() { |
1096 |
FailingCCF f = new LFCCF(null, 8); |
1097 |
f.quietlyInvoke(); |
1098 |
assertTrue(f.getException() instanceof FJException); |
1099 |
checkCompletedAbnormally(f, f.getException()); |
1100 |
}}; |
1101 |
checkInvoke(a); |
1102 |
} |
1103 |
|
1104 |
/** |
1105 |
* join of a forked task throws exception when task completes abnormally |
1106 |
*/ |
1107 |
public void testAbnormalForkJoinCC() { |
1108 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1109 |
protected void realCompute() { |
1110 |
FailingCCF f = new LFCCF(null, 8); |
1111 |
assertSame(f, f.fork()); |
1112 |
try { |
1113 |
f.join(); |
1114 |
shouldThrow(); |
1115 |
} catch (FJException success) { |
1116 |
checkCompletedAbnormally(f, success); |
1117 |
} |
1118 |
}}; |
1119 |
checkInvoke(a); |
1120 |
} |
1121 |
|
1122 |
/** |
1123 |
* get of a forked task throws exception when task completes abnormally |
1124 |
*/ |
1125 |
public void testAbnormalForkGetCC() { |
1126 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1127 |
protected void realCompute() throws Exception { |
1128 |
FailingCCF f = new LFCCF(null, 8); |
1129 |
assertSame(f, f.fork()); |
1130 |
try { |
1131 |
f.get(); |
1132 |
shouldThrow(); |
1133 |
} catch (ExecutionException success) { |
1134 |
Throwable cause = success.getCause(); |
1135 |
assertTrue(cause instanceof FJException); |
1136 |
checkCompletedAbnormally(f, cause); |
1137 |
} |
1138 |
}}; |
1139 |
checkInvoke(a); |
1140 |
} |
1141 |
|
1142 |
/** |
1143 |
* timed get of a forked task throws exception when task completes abnormally |
1144 |
*/ |
1145 |
public void testAbnormalForkTimedGetCC() { |
1146 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1147 |
protected void realCompute() throws Exception { |
1148 |
FailingCCF f = new LFCCF(null, 8); |
1149 |
assertSame(f, f.fork()); |
1150 |
try { |
1151 |
f.get(LONG_DELAY_MS, MILLISECONDS); |
1152 |
shouldThrow(); |
1153 |
} catch (ExecutionException success) { |
1154 |
Throwable cause = success.getCause(); |
1155 |
assertTrue(cause instanceof FJException); |
1156 |
checkCompletedAbnormally(f, cause); |
1157 |
} |
1158 |
}}; |
1159 |
checkInvoke(a); |
1160 |
} |
1161 |
|
1162 |
/** |
1163 |
* quietlyJoin of a forked task returns when task completes abnormally |
1164 |
*/ |
1165 |
public void testAbnormalForkQuietlyJoinCC() { |
1166 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1167 |
protected void realCompute() { |
1168 |
FailingCCF f = new LFCCF(null, 8); |
1169 |
assertSame(f, f.fork()); |
1170 |
f.quietlyJoin(); |
1171 |
assertTrue(f.getException() instanceof FJException); |
1172 |
checkCompletedAbnormally(f, f.getException()); |
1173 |
}}; |
1174 |
checkInvoke(a); |
1175 |
} |
1176 |
|
1177 |
/** |
1178 |
* invoke task throws exception when task cancelled |
1179 |
*/ |
1180 |
public void testCancelledInvokeCC() { |
1181 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1182 |
protected void realCompute() { |
1183 |
CCF f = new LCCF(null, 8); |
1184 |
assertTrue(f.cancel(true)); |
1185 |
try { |
1186 |
f.invoke(); |
1187 |
shouldThrow(); |
1188 |
} catch (CancellationException success) { |
1189 |
checkCancelled(f); |
1190 |
} |
1191 |
}}; |
1192 |
checkInvoke(a); |
1193 |
} |
1194 |
|
1195 |
/** |
1196 |
* join of a forked task throws exception when task cancelled |
1197 |
*/ |
1198 |
public void testCancelledForkJoinCC() { |
1199 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1200 |
protected void realCompute() { |
1201 |
CCF f = new LCCF(null, 8); |
1202 |
assertTrue(f.cancel(true)); |
1203 |
assertSame(f, f.fork()); |
1204 |
try { |
1205 |
f.join(); |
1206 |
shouldThrow(); |
1207 |
} catch (CancellationException success) { |
1208 |
checkCancelled(f); |
1209 |
} |
1210 |
}}; |
1211 |
checkInvoke(a); |
1212 |
} |
1213 |
|
1214 |
/** |
1215 |
* get of a forked task throws exception when task cancelled |
1216 |
*/ |
1217 |
public void testCancelledForkGetCC() { |
1218 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1219 |
protected void realCompute() throws Exception { |
1220 |
CCF f = new LCCF(null, 8); |
1221 |
assertTrue(f.cancel(true)); |
1222 |
assertSame(f, f.fork()); |
1223 |
try { |
1224 |
f.get(); |
1225 |
shouldThrow(); |
1226 |
} catch (CancellationException success) { |
1227 |
checkCancelled(f); |
1228 |
} |
1229 |
}}; |
1230 |
checkInvoke(a); |
1231 |
} |
1232 |
|
1233 |
/** |
1234 |
* timed get of a forked task throws exception when task cancelled |
1235 |
*/ |
1236 |
public void testCancelledForkTimedGetCC() throws Exception { |
1237 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1238 |
protected void realCompute() throws Exception { |
1239 |
CCF f = new LCCF(null, 8); |
1240 |
assertTrue(f.cancel(true)); |
1241 |
assertSame(f, f.fork()); |
1242 |
try { |
1243 |
f.get(LONG_DELAY_MS, MILLISECONDS); |
1244 |
shouldThrow(); |
1245 |
} catch (CancellationException success) { |
1246 |
checkCancelled(f); |
1247 |
} |
1248 |
}}; |
1249 |
checkInvoke(a); |
1250 |
} |
1251 |
|
1252 |
/** |
1253 |
* quietlyJoin of a forked task returns when task cancelled |
1254 |
*/ |
1255 |
public void testCancelledForkQuietlyJoinCC() { |
1256 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1257 |
protected void realCompute() { |
1258 |
CCF f = new LCCF(null, 8); |
1259 |
assertTrue(f.cancel(true)); |
1260 |
assertSame(f, f.fork()); |
1261 |
f.quietlyJoin(); |
1262 |
checkCancelled(f); |
1263 |
}}; |
1264 |
checkInvoke(a); |
1265 |
} |
1266 |
|
1267 |
/** |
1268 |
* getPool of non-FJ task returns null |
1269 |
*/ |
1270 |
public void testGetPool2CC() { |
1271 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1272 |
protected void realCompute() { |
1273 |
assertNull(getPool()); |
1274 |
}}; |
1275 |
assertNull(a.invoke()); |
1276 |
} |
1277 |
|
1278 |
/** |
1279 |
* inForkJoinPool of non-FJ task returns false |
1280 |
*/ |
1281 |
public void testInForkJoinPool2CC() { |
1282 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1283 |
protected void realCompute() { |
1284 |
assertFalse(inForkJoinPool()); |
1285 |
}}; |
1286 |
assertNull(a.invoke()); |
1287 |
} |
1288 |
|
1289 |
/** |
1290 |
* setRawResult(null) succeeds |
1291 |
*/ |
1292 |
public void testSetRawResultCC() { |
1293 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1294 |
protected void realCompute() { |
1295 |
setRawResult(null); |
1296 |
assertNull(getRawResult()); |
1297 |
}}; |
1298 |
assertNull(a.invoke()); |
1299 |
} |
1300 |
|
1301 |
/** |
1302 |
* invoke task throws exception after invoking completeExceptionally |
1303 |
*/ |
1304 |
public void testCompleteExceptionally2CC() { |
1305 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1306 |
protected void realCompute() { |
1307 |
CCF f = new LCCF(null, 8); |
1308 |
f.completeExceptionally(new FJException()); |
1309 |
try { |
1310 |
f.invoke(); |
1311 |
shouldThrow(); |
1312 |
} catch (FJException success) { |
1313 |
checkCompletedAbnormally(f, success); |
1314 |
} |
1315 |
}}; |
1316 |
checkInvoke(a); |
1317 |
} |
1318 |
|
1319 |
/** |
1320 |
* invokeAll(t1, t2) invokes all task arguments |
1321 |
*/ |
1322 |
public void testInvokeAll2CC() { |
1323 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1324 |
protected void realCompute() { |
1325 |
CCF f = new LCCF(null, 8); |
1326 |
CCF g = new LCCF(null, 9); |
1327 |
invokeAll(f, g); |
1328 |
assertEquals(21, f.number); |
1329 |
assertEquals(34, g.number); |
1330 |
checkCompletedNormally(f); |
1331 |
checkCompletedNormally(g); |
1332 |
}}; |
1333 |
checkInvoke(a); |
1334 |
} |
1335 |
|
1336 |
/** |
1337 |
* invokeAll(tasks) with 1 argument invokes task |
1338 |
*/ |
1339 |
public void testInvokeAll1CC() { |
1340 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1341 |
protected void realCompute() { |
1342 |
CCF f = new LCCF(null, 8); |
1343 |
invokeAll(f); |
1344 |
checkCompletedNormally(f); |
1345 |
assertEquals(21, f.number); |
1346 |
}}; |
1347 |
checkInvoke(a); |
1348 |
} |
1349 |
|
1350 |
/** |
1351 |
* invokeAll(tasks) with > 2 argument invokes tasks |
1352 |
*/ |
1353 |
public void testInvokeAll3CC() { |
1354 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1355 |
protected void realCompute() { |
1356 |
CCF f = new LCCF(null, 8); |
1357 |
CCF g = new LCCF(null, 9); |
1358 |
CCF h = new LCCF(null, 7); |
1359 |
invokeAll(f, g, h); |
1360 |
assertEquals(21, f.number); |
1361 |
assertEquals(34, g.number); |
1362 |
assertEquals(13, h.number); |
1363 |
checkCompletedNormally(f); |
1364 |
checkCompletedNormally(g); |
1365 |
checkCompletedNormally(h); |
1366 |
}}; |
1367 |
checkInvoke(a); |
1368 |
} |
1369 |
|
1370 |
/** |
1371 |
* invokeAll(collection) invokes all tasks in the collection |
1372 |
*/ |
1373 |
public void testInvokeAllCollectionCC() { |
1374 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1375 |
protected void realCompute() { |
1376 |
CCF f = new LCCF(null, 8); |
1377 |
CCF g = new LCCF(null, 9); |
1378 |
CCF h = new LCCF(null, 7); |
1379 |
HashSet set = new HashSet(); |
1380 |
set.add(f); |
1381 |
set.add(g); |
1382 |
set.add(h); |
1383 |
invokeAll(set); |
1384 |
assertEquals(21, f.number); |
1385 |
assertEquals(34, g.number); |
1386 |
assertEquals(13, h.number); |
1387 |
checkCompletedNormally(f); |
1388 |
checkCompletedNormally(g); |
1389 |
checkCompletedNormally(h); |
1390 |
}}; |
1391 |
checkInvoke(a); |
1392 |
} |
1393 |
|
1394 |
/** |
1395 |
* invokeAll(tasks) with any null task throws NPE |
1396 |
*/ |
1397 |
public void testInvokeAllNPECC() { |
1398 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1399 |
protected void realCompute() { |
1400 |
CCF f = new LCCF(null, 8); |
1401 |
CCF g = new LCCF(null, 9); |
1402 |
CCF h = null; |
1403 |
try { |
1404 |
invokeAll(f, g, h); |
1405 |
shouldThrow(); |
1406 |
} catch (NullPointerException success) {} |
1407 |
}}; |
1408 |
checkInvoke(a); |
1409 |
} |
1410 |
|
1411 |
/** |
1412 |
* invokeAll(t1, t2) throw exception if any task does |
1413 |
*/ |
1414 |
public void testAbnormalInvokeAll2CC() { |
1415 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1416 |
protected void realCompute() { |
1417 |
CCF f = new LCCF(null, 8); |
1418 |
FailingCCF g = new LFCCF(null, 9); |
1419 |
try { |
1420 |
invokeAll(f, g); |
1421 |
shouldThrow(); |
1422 |
} catch (FJException success) { |
1423 |
checkCompletedAbnormally(g, success); |
1424 |
} |
1425 |
}}; |
1426 |
checkInvoke(a); |
1427 |
} |
1428 |
|
1429 |
/** |
1430 |
* invokeAll(tasks) with 1 argument throws exception if task does |
1431 |
*/ |
1432 |
public void testAbnormalInvokeAll1CC() { |
1433 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1434 |
protected void realCompute() { |
1435 |
FailingCCF g = new LFCCF(null, 9); |
1436 |
try { |
1437 |
invokeAll(g); |
1438 |
shouldThrow(); |
1439 |
} catch (FJException success) { |
1440 |
checkCompletedAbnormally(g, success); |
1441 |
} |
1442 |
}}; |
1443 |
checkInvoke(a); |
1444 |
} |
1445 |
|
1446 |
/** |
1447 |
* invokeAll(tasks) with > 2 argument throws exception if any task does |
1448 |
*/ |
1449 |
public void testAbnormalInvokeAll3CC() { |
1450 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1451 |
protected void realCompute() { |
1452 |
CCF f = new LCCF(null, 8); |
1453 |
FailingCCF g = new LFCCF(null, 9); |
1454 |
CCF h = new LCCF(null, 7); |
1455 |
try { |
1456 |
invokeAll(f, g, h); |
1457 |
shouldThrow(); |
1458 |
} catch (FJException success) { |
1459 |
checkCompletedAbnormally(g, success); |
1460 |
} |
1461 |
}}; |
1462 |
checkInvoke(a); |
1463 |
} |
1464 |
|
1465 |
/** |
1466 |
* invokeAll(collection) throws exception if any task does |
1467 |
*/ |
1468 |
public void testAbnormalInvokeAllCollectionCC() { |
1469 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1470 |
protected void realCompute() { |
1471 |
FailingCCF f = new LFCCF(null, 8); |
1472 |
CCF g = new LCCF(null, 9); |
1473 |
CCF h = new LCCF(null, 7); |
1474 |
HashSet set = new HashSet(); |
1475 |
set.add(f); |
1476 |
set.add(g); |
1477 |
set.add(h); |
1478 |
try { |
1479 |
invokeAll(set); |
1480 |
shouldThrow(); |
1481 |
} catch (FJException success) { |
1482 |
checkCompletedAbnormally(f, success); |
1483 |
} |
1484 |
}}; |
1485 |
checkInvoke(a); |
1486 |
} |
1487 |
|
1488 |
} |