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 |
* get of a forked task returns when task completes |
345 |
*/ |
346 |
public void testForkGet() { |
347 |
RecursiveAction a = new CheckedRecursiveAction() { |
348 |
protected void realCompute() throws Exception { |
349 |
FibAction f = new FibAction(8); |
350 |
assertSame(f, f.fork()); |
351 |
assertNull(f.get()); |
352 |
assertEquals(21, f.result); |
353 |
checkCompletedNormally(f); |
354 |
}}; |
355 |
checkInvoke(a); |
356 |
} |
357 |
|
358 |
/** |
359 |
* timed get of a forked task returns when task completes |
360 |
*/ |
361 |
public void testForkTimedGet() { |
362 |
RecursiveAction a = new CheckedRecursiveAction() { |
363 |
protected void realCompute() throws Exception { |
364 |
FibAction f = new FibAction(8); |
365 |
assertSame(f, f.fork()); |
366 |
assertNull(f.get(5L, SECONDS)); |
367 |
assertEquals(21, f.result); |
368 |
checkCompletedNormally(f); |
369 |
}}; |
370 |
checkInvoke(a); |
371 |
} |
372 |
|
373 |
/** |
374 |
* timed get with null time unit throws NPE |
375 |
*/ |
376 |
public void testForkTimedGetNPE() { |
377 |
RecursiveAction a = new CheckedRecursiveAction() { |
378 |
protected void realCompute() throws Exception { |
379 |
FibAction f = new FibAction(8); |
380 |
assertSame(f, f.fork()); |
381 |
try { |
382 |
f.get(5L, null); |
383 |
shouldThrow(); |
384 |
} catch (NullPointerException success) {} |
385 |
}}; |
386 |
checkInvoke(a); |
387 |
} |
388 |
|
389 |
/** |
390 |
* quietlyJoin of a forked task returns when task completes |
391 |
*/ |
392 |
public void testForkQuietlyJoin() { |
393 |
RecursiveAction a = new CheckedRecursiveAction() { |
394 |
protected void realCompute() { |
395 |
FibAction f = new FibAction(8); |
396 |
assertSame(f, f.fork()); |
397 |
f.quietlyJoin(); |
398 |
assertEquals(21, f.result); |
399 |
checkCompletedNormally(f); |
400 |
}}; |
401 |
checkInvoke(a); |
402 |
} |
403 |
|
404 |
/** |
405 |
* invoke task throws exception when task completes abnormally |
406 |
*/ |
407 |
public void testAbnormalInvoke() { |
408 |
RecursiveAction a = new CheckedRecursiveAction() { |
409 |
protected void realCompute() { |
410 |
FailingFibAction f = new FailingFibAction(8); |
411 |
try { |
412 |
f.invoke(); |
413 |
shouldThrow(); |
414 |
} catch (FJException success) { |
415 |
checkCompletedAbnormally(f, success); |
416 |
} |
417 |
}}; |
418 |
checkInvoke(a); |
419 |
} |
420 |
|
421 |
/** |
422 |
* quietlyInvoke task returns when task completes abnormally |
423 |
*/ |
424 |
public void testAbnormalQuietlyInvoke() { |
425 |
RecursiveAction a = new CheckedRecursiveAction() { |
426 |
protected void realCompute() { |
427 |
FailingFibAction f = new FailingFibAction(8); |
428 |
f.quietlyInvoke(); |
429 |
assertTrue(f.getException() instanceof FJException); |
430 |
checkCompletedAbnormally(f, f.getException()); |
431 |
}}; |
432 |
checkInvoke(a); |
433 |
} |
434 |
|
435 |
/** |
436 |
* join of a forked task throws exception when task completes abnormally |
437 |
*/ |
438 |
public void testAbnormalForkJoin() { |
439 |
RecursiveAction a = new CheckedRecursiveAction() { |
440 |
protected void realCompute() { |
441 |
FailingFibAction f = new FailingFibAction(8); |
442 |
assertSame(f, f.fork()); |
443 |
try { |
444 |
f.join(); |
445 |
shouldThrow(); |
446 |
} catch (FJException success) { |
447 |
checkCompletedAbnormally(f, success); |
448 |
} |
449 |
}}; |
450 |
checkInvoke(a); |
451 |
} |
452 |
|
453 |
/** |
454 |
* get of a forked task throws exception when task completes abnormally |
455 |
*/ |
456 |
public void testAbnormalForkGet() { |
457 |
RecursiveAction a = new CheckedRecursiveAction() { |
458 |
protected void realCompute() throws Exception { |
459 |
FailingFibAction f = new FailingFibAction(8); |
460 |
assertSame(f, f.fork()); |
461 |
try { |
462 |
f.get(); |
463 |
shouldThrow(); |
464 |
} catch (ExecutionException success) { |
465 |
Throwable cause = success.getCause(); |
466 |
assertTrue(cause instanceof FJException); |
467 |
checkCompletedAbnormally(f, cause); |
468 |
} |
469 |
}}; |
470 |
checkInvoke(a); |
471 |
} |
472 |
|
473 |
/** |
474 |
* timed get of a forked task throws exception when task completes abnormally |
475 |
*/ |
476 |
public void testAbnormalForkTimedGet() { |
477 |
RecursiveAction a = new CheckedRecursiveAction() { |
478 |
protected void realCompute() throws Exception { |
479 |
FailingFibAction f = new FailingFibAction(8); |
480 |
assertSame(f, f.fork()); |
481 |
try { |
482 |
f.get(5L, TimeUnit.SECONDS); |
483 |
shouldThrow(); |
484 |
} catch (ExecutionException success) { |
485 |
Throwable cause = success.getCause(); |
486 |
assertTrue(cause instanceof FJException); |
487 |
checkCompletedAbnormally(f, cause); |
488 |
} |
489 |
}}; |
490 |
checkInvoke(a); |
491 |
} |
492 |
|
493 |
/** |
494 |
* quietlyJoin of a forked task returns when task completes abnormally |
495 |
*/ |
496 |
public void testAbnormalForkQuietlyJoin() { |
497 |
RecursiveAction a = new CheckedRecursiveAction() { |
498 |
protected void realCompute() { |
499 |
FailingFibAction f = new FailingFibAction(8); |
500 |
assertSame(f, f.fork()); |
501 |
f.quietlyJoin(); |
502 |
assertTrue(f.getException() instanceof FJException); |
503 |
checkCompletedAbnormally(f, f.getException()); |
504 |
}}; |
505 |
checkInvoke(a); |
506 |
} |
507 |
|
508 |
/** |
509 |
* invoke task throws exception when task cancelled |
510 |
*/ |
511 |
public void testCancelledInvoke() { |
512 |
RecursiveAction a = new CheckedRecursiveAction() { |
513 |
protected void realCompute() { |
514 |
FibAction f = new FibAction(8); |
515 |
assertTrue(f.cancel(true)); |
516 |
try { |
517 |
f.invoke(); |
518 |
shouldThrow(); |
519 |
} catch (CancellationException success) { |
520 |
checkCancelled(f); |
521 |
} |
522 |
}}; |
523 |
checkInvoke(a); |
524 |
} |
525 |
|
526 |
/** |
527 |
* join of a forked task throws exception when task cancelled |
528 |
*/ |
529 |
public void testCancelledForkJoin() { |
530 |
RecursiveAction a = new CheckedRecursiveAction() { |
531 |
protected void realCompute() { |
532 |
FibAction f = new FibAction(8); |
533 |
assertTrue(f.cancel(true)); |
534 |
assertSame(f, f.fork()); |
535 |
try { |
536 |
f.join(); |
537 |
shouldThrow(); |
538 |
} catch (CancellationException success) { |
539 |
checkCancelled(f); |
540 |
} |
541 |
}}; |
542 |
checkInvoke(a); |
543 |
} |
544 |
|
545 |
/** |
546 |
* get of a forked task throws exception when task cancelled |
547 |
*/ |
548 |
public void testCancelledForkGet() { |
549 |
RecursiveAction a = new CheckedRecursiveAction() { |
550 |
protected void realCompute() throws Exception { |
551 |
FibAction f = new FibAction(8); |
552 |
assertTrue(f.cancel(true)); |
553 |
assertSame(f, f.fork()); |
554 |
try { |
555 |
f.get(); |
556 |
shouldThrow(); |
557 |
} catch (CancellationException success) { |
558 |
checkCancelled(f); |
559 |
} |
560 |
}}; |
561 |
checkInvoke(a); |
562 |
} |
563 |
|
564 |
/** |
565 |
* timed get of a forked task throws exception when task cancelled |
566 |
*/ |
567 |
public void testCancelledForkTimedGet() { |
568 |
RecursiveAction a = new CheckedRecursiveAction() { |
569 |
protected void realCompute() throws Exception { |
570 |
FibAction f = new FibAction(8); |
571 |
assertTrue(f.cancel(true)); |
572 |
assertSame(f, f.fork()); |
573 |
try { |
574 |
f.get(5L, SECONDS); |
575 |
shouldThrow(); |
576 |
} catch (CancellationException success) { |
577 |
checkCancelled(f); |
578 |
} |
579 |
}}; |
580 |
checkInvoke(a); |
581 |
} |
582 |
|
583 |
/** |
584 |
* quietlyJoin of a forked task returns when task cancelled |
585 |
*/ |
586 |
public void testCancelledForkQuietlyJoin() { |
587 |
RecursiveAction a = new CheckedRecursiveAction() { |
588 |
protected void realCompute() { |
589 |
FibAction f = new FibAction(8); |
590 |
assertTrue(f.cancel(true)); |
591 |
assertSame(f, f.fork()); |
592 |
f.quietlyJoin(); |
593 |
checkCancelled(f); |
594 |
}}; |
595 |
checkInvoke(a); |
596 |
} |
597 |
|
598 |
/** |
599 |
* inForkJoinPool of non-FJ task returns false |
600 |
*/ |
601 |
public void testInForkJoinPool2() { |
602 |
RecursiveAction a = new CheckedRecursiveAction() { |
603 |
protected void realCompute() { |
604 |
assertFalse(inForkJoinPool()); |
605 |
}}; |
606 |
assertNull(a.invoke()); |
607 |
} |
608 |
|
609 |
/** |
610 |
* A reinitialized normally completed task may be re-invoked |
611 |
*/ |
612 |
public void testReinitialize() { |
613 |
RecursiveAction a = new CheckedRecursiveAction() { |
614 |
protected void realCompute() { |
615 |
FibAction f = new FibAction(8); |
616 |
checkNotDone(f); |
617 |
|
618 |
for (int i = 0; i < 3; i++) { |
619 |
assertNull(f.invoke()); |
620 |
assertEquals(21, f.result); |
621 |
checkCompletedNormally(f); |
622 |
f.reinitialize(); |
623 |
checkNotDone(f); |
624 |
} |
625 |
}}; |
626 |
checkInvoke(a); |
627 |
} |
628 |
|
629 |
/** |
630 |
* A reinitialized abnormally completed task may be re-invoked |
631 |
*/ |
632 |
public void testReinitializeAbnormal() { |
633 |
RecursiveAction a = new CheckedRecursiveAction() { |
634 |
protected void realCompute() { |
635 |
FailingFibAction f = new FailingFibAction(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 |
}}; |
649 |
checkInvoke(a); |
650 |
} |
651 |
|
652 |
/** |
653 |
* invoke task throws exception after invoking completeExceptionally |
654 |
*/ |
655 |
public void testCompleteExceptionally() { |
656 |
RecursiveAction a = new CheckedRecursiveAction() { |
657 |
protected void realCompute() { |
658 |
FibAction f = new FibAction(8); |
659 |
f.completeExceptionally(new FJException()); |
660 |
try { |
661 |
f.invoke(); |
662 |
shouldThrow(); |
663 |
} catch (FJException success) { |
664 |
checkCompletedAbnormally(f, success); |
665 |
} |
666 |
}}; |
667 |
checkInvoke(a); |
668 |
} |
669 |
|
670 |
/** |
671 |
* invoke task suppresses execution invoking complete |
672 |
*/ |
673 |
public void testComplete() { |
674 |
RecursiveAction a = new CheckedRecursiveAction() { |
675 |
protected void realCompute() { |
676 |
FibAction f = new FibAction(8); |
677 |
f.complete(null); |
678 |
assertNull(f.invoke()); |
679 |
assertEquals(0, f.result); |
680 |
checkCompletedNormally(f); |
681 |
}}; |
682 |
checkInvoke(a); |
683 |
} |
684 |
|
685 |
/** |
686 |
* invokeAll(t1, t2) invokes all task arguments |
687 |
*/ |
688 |
public void testInvokeAll2() { |
689 |
RecursiveAction a = new CheckedRecursiveAction() { |
690 |
protected void realCompute() { |
691 |
FibAction f = new FibAction(8); |
692 |
FibAction g = new FibAction(9); |
693 |
invokeAll(f, g); |
694 |
checkCompletedNormally(f); |
695 |
assertEquals(21, f.result); |
696 |
checkCompletedNormally(g); |
697 |
assertEquals(34, g.result); |
698 |
}}; |
699 |
checkInvoke(a); |
700 |
} |
701 |
|
702 |
/** |
703 |
* invokeAll(tasks) with 1 argument invokes task |
704 |
*/ |
705 |
public void testInvokeAll1() { |
706 |
RecursiveAction a = new CheckedRecursiveAction() { |
707 |
protected void realCompute() { |
708 |
FibAction f = new FibAction(8); |
709 |
invokeAll(f); |
710 |
checkCompletedNormally(f); |
711 |
assertEquals(21, f.result); |
712 |
}}; |
713 |
checkInvoke(a); |
714 |
} |
715 |
|
716 |
/** |
717 |
* invokeAll(tasks) with > 2 argument invokes tasks |
718 |
*/ |
719 |
public void testInvokeAll3() { |
720 |
RecursiveAction a = new CheckedRecursiveAction() { |
721 |
protected void realCompute() { |
722 |
FibAction f = new FibAction(8); |
723 |
FibAction g = new FibAction(9); |
724 |
FibAction h = new FibAction(7); |
725 |
invokeAll(f, g, h); |
726 |
assertTrue(f.isDone()); |
727 |
assertTrue(g.isDone()); |
728 |
assertTrue(h.isDone()); |
729 |
checkCompletedNormally(f); |
730 |
assertEquals(21, f.result); |
731 |
checkCompletedNormally(g); |
732 |
assertEquals(34, g.result); |
733 |
checkCompletedNormally(g); |
734 |
assertEquals(13, h.result); |
735 |
}}; |
736 |
checkInvoke(a); |
737 |
} |
738 |
|
739 |
/** |
740 |
* invokeAll(collection) invokes all tasks in the collection |
741 |
*/ |
742 |
public void testInvokeAllCollection() { |
743 |
RecursiveAction a = new CheckedRecursiveAction() { |
744 |
protected void realCompute() { |
745 |
FibAction f = new FibAction(8); |
746 |
FibAction g = new FibAction(9); |
747 |
FibAction h = new FibAction(7); |
748 |
HashSet set = new HashSet(); |
749 |
set.add(f); |
750 |
set.add(g); |
751 |
set.add(h); |
752 |
invokeAll(set); |
753 |
assertTrue(f.isDone()); |
754 |
assertTrue(g.isDone()); |
755 |
assertTrue(h.isDone()); |
756 |
checkCompletedNormally(f); |
757 |
assertEquals(21, f.result); |
758 |
checkCompletedNormally(g); |
759 |
assertEquals(34, g.result); |
760 |
checkCompletedNormally(g); |
761 |
assertEquals(13, h.result); |
762 |
}}; |
763 |
checkInvoke(a); |
764 |
} |
765 |
|
766 |
/** |
767 |
* invokeAll(tasks) with any null task throws NPE |
768 |
*/ |
769 |
public void testInvokeAllNPE() { |
770 |
RecursiveAction a = new CheckedRecursiveAction() { |
771 |
protected void realCompute() { |
772 |
FibAction f = new FibAction(8); |
773 |
FibAction g = new FibAction(9); |
774 |
FibAction h = null; |
775 |
try { |
776 |
invokeAll(f, g, h); |
777 |
shouldThrow(); |
778 |
} catch (NullPointerException success) {} |
779 |
}}; |
780 |
checkInvoke(a); |
781 |
} |
782 |
|
783 |
/** |
784 |
* invokeAll(t1, t2) throw exception if any task does |
785 |
*/ |
786 |
public void testAbnormalInvokeAll2() { |
787 |
RecursiveAction a = new CheckedRecursiveAction() { |
788 |
protected void realCompute() { |
789 |
FibAction f = new FibAction(8); |
790 |
FailingFibAction g = new FailingFibAction(9); |
791 |
try { |
792 |
invokeAll(f, g); |
793 |
shouldThrow(); |
794 |
} catch (FJException success) { |
795 |
checkCompletedAbnormally(g, success); |
796 |
} |
797 |
}}; |
798 |
checkInvoke(a); |
799 |
} |
800 |
|
801 |
/** |
802 |
* invokeAll(tasks) with 1 argument throws exception if task does |
803 |
*/ |
804 |
public void testAbnormalInvokeAll1() { |
805 |
RecursiveAction a = new CheckedRecursiveAction() { |
806 |
protected void realCompute() { |
807 |
FailingFibAction g = new FailingFibAction(9); |
808 |
try { |
809 |
invokeAll(g); |
810 |
shouldThrow(); |
811 |
} catch (FJException success) { |
812 |
checkCompletedAbnormally(g, success); |
813 |
} |
814 |
}}; |
815 |
checkInvoke(a); |
816 |
} |
817 |
|
818 |
/** |
819 |
* invokeAll(tasks) with > 2 argument throws exception if any task does |
820 |
*/ |
821 |
public void testAbnormalInvokeAll3() { |
822 |
RecursiveAction a = new CheckedRecursiveAction() { |
823 |
protected void realCompute() { |
824 |
FibAction f = new FibAction(8); |
825 |
FailingFibAction g = new FailingFibAction(9); |
826 |
FibAction h = new FibAction(7); |
827 |
try { |
828 |
invokeAll(f, g, h); |
829 |
shouldThrow(); |
830 |
} catch (FJException success) { |
831 |
checkCompletedAbnormally(g, success); |
832 |
} |
833 |
}}; |
834 |
checkInvoke(a); |
835 |
} |
836 |
|
837 |
/** |
838 |
* invokeAll(collection) throws exception if any task does |
839 |
*/ |
840 |
public void testAbnormalInvokeAllCollection() { |
841 |
RecursiveAction a = new CheckedRecursiveAction() { |
842 |
protected void realCompute() { |
843 |
FailingFibAction f = new FailingFibAction(8); |
844 |
FibAction g = new FibAction(9); |
845 |
FibAction h = new FibAction(7); |
846 |
HashSet set = new HashSet(); |
847 |
set.add(f); |
848 |
set.add(g); |
849 |
set.add(h); |
850 |
try { |
851 |
invokeAll(set); |
852 |
shouldThrow(); |
853 |
} catch (FJException success) { |
854 |
checkCompletedAbnormally(f, success); |
855 |
} |
856 |
}}; |
857 |
checkInvoke(a); |
858 |
} |
859 |
|
860 |
// CountedCompleter versions |
861 |
|
862 |
abstract static class CCF extends CountedCompleter { |
863 |
int number; |
864 |
int rnumber; |
865 |
|
866 |
public CCF(CountedCompleter parent, int n) { |
867 |
super(parent, 1); |
868 |
this.number = n; |
869 |
} |
870 |
|
871 |
public final void compute() { |
872 |
CountedCompleter p; |
873 |
CCF f = this; |
874 |
int n = number; |
875 |
while (n >= 2) { |
876 |
new RCCF(f, n - 2).fork(); |
877 |
f = new LCCF(f, --n); |
878 |
} |
879 |
f.number = n; |
880 |
f.onCompletion(f); |
881 |
if ((p = f.getCompleter()) != null) |
882 |
p.tryComplete(); |
883 |
else |
884 |
f.quietlyComplete(); |
885 |
} |
886 |
} |
887 |
|
888 |
static final class LCCF extends CCF { |
889 |
public LCCF(CountedCompleter parent, int n) { |
890 |
super(parent, n); |
891 |
} |
892 |
public final void onCompletion(CountedCompleter caller) { |
893 |
CCF p = (CCF)getCompleter(); |
894 |
int n = number + rnumber; |
895 |
if (p != null) |
896 |
p.number = n; |
897 |
else |
898 |
number = n; |
899 |
} |
900 |
} |
901 |
static final class RCCF extends CCF { |
902 |
public RCCF(CountedCompleter parent, int n) { |
903 |
super(parent, n); |
904 |
} |
905 |
public final void onCompletion(CountedCompleter caller) { |
906 |
CCF p = (CCF)getCompleter(); |
907 |
int n = number + rnumber; |
908 |
if (p != null) |
909 |
p.rnumber = n; |
910 |
else |
911 |
number = n; |
912 |
} |
913 |
} |
914 |
|
915 |
// Version of CCF with forced failure in left completions |
916 |
abstract static class FailingCCF extends CountedCompleter { |
917 |
int number; |
918 |
int rnumber; |
919 |
|
920 |
public FailingCCF(CountedCompleter parent, int n) { |
921 |
super(parent, 1); |
922 |
this.number = n; |
923 |
} |
924 |
|
925 |
public final void compute() { |
926 |
CountedCompleter p; |
927 |
FailingCCF f = this; |
928 |
int n = number; |
929 |
while (n >= 2) { |
930 |
new RFCCF(f, n - 2).fork(); |
931 |
f = new LFCCF(f, --n); |
932 |
} |
933 |
f.number = n; |
934 |
f.onCompletion(f); |
935 |
if ((p = f.getCompleter()) != null) |
936 |
p.tryComplete(); |
937 |
else |
938 |
f.quietlyComplete(); |
939 |
} |
940 |
} |
941 |
|
942 |
static final class LFCCF extends FailingCCF { |
943 |
public LFCCF(CountedCompleter parent, int n) { |
944 |
super(parent, n); |
945 |
} |
946 |
public final void onCompletion(CountedCompleter caller) { |
947 |
FailingCCF p = (FailingCCF)getCompleter(); |
948 |
int n = number + rnumber; |
949 |
if (p != null) |
950 |
p.number = n; |
951 |
else |
952 |
number = n; |
953 |
} |
954 |
} |
955 |
static final class RFCCF extends FailingCCF { |
956 |
public RFCCF(CountedCompleter parent, int n) { |
957 |
super(parent, n); |
958 |
} |
959 |
public final void onCompletion(CountedCompleter caller) { |
960 |
completeExceptionally(new FJException()); |
961 |
} |
962 |
} |
963 |
|
964 |
/** |
965 |
* invoke returns when task completes normally. |
966 |
* isCompletedAbnormally and isCancelled return false for normally |
967 |
* completed tasks; getRawResult returns null. |
968 |
*/ |
969 |
public void testInvokeCC() { |
970 |
ForkJoinTask a = new CheckedRecursiveAction() { |
971 |
protected void realCompute() { |
972 |
CCF f = new LCCF(null, 8); |
973 |
assertNull(f.invoke()); |
974 |
assertEquals(21, f.number); |
975 |
checkCompletedNormally(f); |
976 |
}}; |
977 |
checkInvoke(a); |
978 |
} |
979 |
|
980 |
/** |
981 |
* quietlyInvoke task returns when task completes normally. |
982 |
* isCompletedAbnormally and isCancelled return false for normally |
983 |
* completed tasks |
984 |
*/ |
985 |
public void testQuietlyInvokeCC() { |
986 |
ForkJoinTask a = new CheckedRecursiveAction() { |
987 |
protected void realCompute() { |
988 |
CCF f = new LCCF(null, 8); |
989 |
f.quietlyInvoke(); |
990 |
assertEquals(21, f.number); |
991 |
checkCompletedNormally(f); |
992 |
}}; |
993 |
checkInvoke(a); |
994 |
} |
995 |
|
996 |
/** |
997 |
* join of a forked task returns when task completes |
998 |
*/ |
999 |
public void testForkJoinCC() { |
1000 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1001 |
protected void realCompute() { |
1002 |
CCF f = new LCCF(null, 8); |
1003 |
assertSame(f, f.fork()); |
1004 |
assertNull(f.join()); |
1005 |
assertEquals(21, f.number); |
1006 |
checkCompletedNormally(f); |
1007 |
}}; |
1008 |
checkInvoke(a); |
1009 |
} |
1010 |
|
1011 |
/** |
1012 |
* get of a forked task returns when task completes |
1013 |
*/ |
1014 |
public void testForkGetCC() { |
1015 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1016 |
protected void realCompute() throws Exception { |
1017 |
CCF f = new LCCF(null, 8); |
1018 |
assertSame(f, f.fork()); |
1019 |
assertNull(f.get()); |
1020 |
assertEquals(21, f.number); |
1021 |
checkCompletedNormally(f); |
1022 |
}}; |
1023 |
checkInvoke(a); |
1024 |
} |
1025 |
|
1026 |
/** |
1027 |
* timed get of a forked task returns when task completes |
1028 |
*/ |
1029 |
public void testForkTimedGetCC() { |
1030 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1031 |
protected void realCompute() throws Exception { |
1032 |
CCF f = new LCCF(null, 8); |
1033 |
assertSame(f, f.fork()); |
1034 |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
1035 |
assertEquals(21, f.number); |
1036 |
checkCompletedNormally(f); |
1037 |
}}; |
1038 |
checkInvoke(a); |
1039 |
} |
1040 |
|
1041 |
/** |
1042 |
* timed get with null time unit throws NPE |
1043 |
*/ |
1044 |
public void testForkTimedGetNPECC() { |
1045 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1046 |
protected void realCompute() throws Exception { |
1047 |
CCF f = new LCCF(null, 8); |
1048 |
assertSame(f, f.fork()); |
1049 |
try { |
1050 |
f.get(5L, null); |
1051 |
shouldThrow(); |
1052 |
} catch (NullPointerException success) {} |
1053 |
}}; |
1054 |
checkInvoke(a); |
1055 |
} |
1056 |
|
1057 |
/** |
1058 |
* quietlyJoin of a forked task returns when task completes |
1059 |
*/ |
1060 |
public void testForkQuietlyJoinCC() { |
1061 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1062 |
protected void realCompute() { |
1063 |
CCF f = new LCCF(null, 8); |
1064 |
assertSame(f, f.fork()); |
1065 |
f.quietlyJoin(); |
1066 |
assertEquals(21, f.number); |
1067 |
checkCompletedNormally(f); |
1068 |
}}; |
1069 |
checkInvoke(a); |
1070 |
} |
1071 |
|
1072 |
/** |
1073 |
* invoke task throws exception when task completes abnormally |
1074 |
*/ |
1075 |
public void testAbnormalInvokeCC() { |
1076 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1077 |
protected void realCompute() { |
1078 |
FailingCCF f = new LFCCF(null, 8); |
1079 |
try { |
1080 |
f.invoke(); |
1081 |
shouldThrow(); |
1082 |
} catch (FJException success) { |
1083 |
checkCompletedAbnormally(f, success); |
1084 |
} |
1085 |
}}; |
1086 |
checkInvoke(a); |
1087 |
} |
1088 |
|
1089 |
/** |
1090 |
* quietlyInvoke task returns when task completes abnormally |
1091 |
*/ |
1092 |
public void testAbnormalQuietlyInvokeCC() { |
1093 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1094 |
protected void realCompute() { |
1095 |
FailingCCF f = new LFCCF(null, 8); |
1096 |
f.quietlyInvoke(); |
1097 |
assertTrue(f.getException() instanceof FJException); |
1098 |
checkCompletedAbnormally(f, f.getException()); |
1099 |
}}; |
1100 |
checkInvoke(a); |
1101 |
} |
1102 |
|
1103 |
/** |
1104 |
* join of a forked task throws exception when task completes abnormally |
1105 |
*/ |
1106 |
public void testAbnormalForkJoinCC() { |
1107 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1108 |
protected void realCompute() { |
1109 |
FailingCCF f = new LFCCF(null, 8); |
1110 |
assertSame(f, f.fork()); |
1111 |
try { |
1112 |
f.join(); |
1113 |
shouldThrow(); |
1114 |
} catch (FJException success) { |
1115 |
checkCompletedAbnormally(f, success); |
1116 |
} |
1117 |
}}; |
1118 |
checkInvoke(a); |
1119 |
} |
1120 |
|
1121 |
/** |
1122 |
* get of a forked task throws exception when task completes abnormally |
1123 |
*/ |
1124 |
public void testAbnormalForkGetCC() { |
1125 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1126 |
protected void realCompute() throws Exception { |
1127 |
FailingCCF f = new LFCCF(null, 8); |
1128 |
assertSame(f, f.fork()); |
1129 |
try { |
1130 |
f.get(); |
1131 |
shouldThrow(); |
1132 |
} catch (ExecutionException success) { |
1133 |
Throwable cause = success.getCause(); |
1134 |
assertTrue(cause instanceof FJException); |
1135 |
checkCompletedAbnormally(f, cause); |
1136 |
} |
1137 |
}}; |
1138 |
checkInvoke(a); |
1139 |
} |
1140 |
|
1141 |
/** |
1142 |
* timed get of a forked task throws exception when task completes abnormally |
1143 |
*/ |
1144 |
public void testAbnormalForkTimedGetCC() { |
1145 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1146 |
protected void realCompute() throws Exception { |
1147 |
FailingCCF f = new LFCCF(null, 8); |
1148 |
assertSame(f, f.fork()); |
1149 |
try { |
1150 |
f.get(LONG_DELAY_MS, MILLISECONDS); |
1151 |
shouldThrow(); |
1152 |
} catch (ExecutionException success) { |
1153 |
Throwable cause = success.getCause(); |
1154 |
assertTrue(cause instanceof FJException); |
1155 |
checkCompletedAbnormally(f, cause); |
1156 |
} |
1157 |
}}; |
1158 |
checkInvoke(a); |
1159 |
} |
1160 |
|
1161 |
/** |
1162 |
* quietlyJoin of a forked task returns when task completes abnormally |
1163 |
*/ |
1164 |
public void testAbnormalForkQuietlyJoinCC() { |
1165 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1166 |
protected void realCompute() { |
1167 |
FailingCCF f = new LFCCF(null, 8); |
1168 |
assertSame(f, f.fork()); |
1169 |
f.quietlyJoin(); |
1170 |
assertTrue(f.getException() instanceof FJException); |
1171 |
checkCompletedAbnormally(f, f.getException()); |
1172 |
}}; |
1173 |
checkInvoke(a); |
1174 |
} |
1175 |
|
1176 |
/** |
1177 |
* invoke task throws exception when task cancelled |
1178 |
*/ |
1179 |
public void testCancelledInvokeCC() { |
1180 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1181 |
protected void realCompute() { |
1182 |
CCF f = new LCCF(null, 8); |
1183 |
assertTrue(f.cancel(true)); |
1184 |
try { |
1185 |
f.invoke(); |
1186 |
shouldThrow(); |
1187 |
} catch (CancellationException success) { |
1188 |
checkCancelled(f); |
1189 |
} |
1190 |
}}; |
1191 |
checkInvoke(a); |
1192 |
} |
1193 |
|
1194 |
/** |
1195 |
* join of a forked task throws exception when task cancelled |
1196 |
*/ |
1197 |
public void testCancelledForkJoinCC() { |
1198 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1199 |
protected void realCompute() { |
1200 |
CCF f = new LCCF(null, 8); |
1201 |
assertTrue(f.cancel(true)); |
1202 |
assertSame(f, f.fork()); |
1203 |
try { |
1204 |
f.join(); |
1205 |
shouldThrow(); |
1206 |
} catch (CancellationException success) { |
1207 |
checkCancelled(f); |
1208 |
} |
1209 |
}}; |
1210 |
checkInvoke(a); |
1211 |
} |
1212 |
|
1213 |
/** |
1214 |
* get of a forked task throws exception when task cancelled |
1215 |
*/ |
1216 |
public void testCancelledForkGetCC() { |
1217 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1218 |
protected void realCompute() throws Exception { |
1219 |
CCF f = new LCCF(null, 8); |
1220 |
assertTrue(f.cancel(true)); |
1221 |
assertSame(f, f.fork()); |
1222 |
try { |
1223 |
f.get(); |
1224 |
shouldThrow(); |
1225 |
} catch (CancellationException success) { |
1226 |
checkCancelled(f); |
1227 |
} |
1228 |
}}; |
1229 |
checkInvoke(a); |
1230 |
} |
1231 |
|
1232 |
/** |
1233 |
* timed get of a forked task throws exception when task cancelled |
1234 |
*/ |
1235 |
public void testCancelledForkTimedGetCC() throws Exception { |
1236 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1237 |
protected void realCompute() throws Exception { |
1238 |
CCF f = new LCCF(null, 8); |
1239 |
assertTrue(f.cancel(true)); |
1240 |
assertSame(f, f.fork()); |
1241 |
try { |
1242 |
f.get(LONG_DELAY_MS, MILLISECONDS); |
1243 |
shouldThrow(); |
1244 |
} catch (CancellationException success) { |
1245 |
checkCancelled(f); |
1246 |
} |
1247 |
}}; |
1248 |
checkInvoke(a); |
1249 |
} |
1250 |
|
1251 |
/** |
1252 |
* quietlyJoin of a forked task returns when task cancelled |
1253 |
*/ |
1254 |
public void testCancelledForkQuietlyJoinCC() { |
1255 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1256 |
protected void realCompute() { |
1257 |
CCF f = new LCCF(null, 8); |
1258 |
assertTrue(f.cancel(true)); |
1259 |
assertSame(f, f.fork()); |
1260 |
f.quietlyJoin(); |
1261 |
checkCancelled(f); |
1262 |
}}; |
1263 |
checkInvoke(a); |
1264 |
} |
1265 |
|
1266 |
/** |
1267 |
* getPool of non-FJ task returns null |
1268 |
*/ |
1269 |
public void testGetPool2CC() { |
1270 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1271 |
protected void realCompute() { |
1272 |
assertNull(getPool()); |
1273 |
}}; |
1274 |
assertNull(a.invoke()); |
1275 |
} |
1276 |
|
1277 |
/** |
1278 |
* inForkJoinPool of non-FJ task returns false |
1279 |
*/ |
1280 |
public void testInForkJoinPool2CC() { |
1281 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1282 |
protected void realCompute() { |
1283 |
assertFalse(inForkJoinPool()); |
1284 |
}}; |
1285 |
assertNull(a.invoke()); |
1286 |
} |
1287 |
|
1288 |
/** |
1289 |
* setRawResult(null) succeeds |
1290 |
*/ |
1291 |
public void testSetRawResultCC() { |
1292 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1293 |
protected void realCompute() { |
1294 |
setRawResult(null); |
1295 |
assertNull(getRawResult()); |
1296 |
}}; |
1297 |
assertNull(a.invoke()); |
1298 |
} |
1299 |
|
1300 |
/** |
1301 |
* invoke task throws exception after invoking completeExceptionally |
1302 |
*/ |
1303 |
public void testCompleteExceptionally2CC() { |
1304 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1305 |
protected void realCompute() { |
1306 |
CCF f = new LCCF(null, 8); |
1307 |
f.completeExceptionally(new FJException()); |
1308 |
try { |
1309 |
f.invoke(); |
1310 |
shouldThrow(); |
1311 |
} catch (FJException success) { |
1312 |
checkCompletedAbnormally(f, success); |
1313 |
} |
1314 |
}}; |
1315 |
checkInvoke(a); |
1316 |
} |
1317 |
|
1318 |
/** |
1319 |
* invokeAll(t1, t2) invokes all task arguments |
1320 |
*/ |
1321 |
public void testInvokeAll2CC() { |
1322 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1323 |
protected void realCompute() { |
1324 |
CCF f = new LCCF(null, 8); |
1325 |
CCF g = new LCCF(null, 9); |
1326 |
invokeAll(f, g); |
1327 |
assertEquals(21, f.number); |
1328 |
assertEquals(34, g.number); |
1329 |
checkCompletedNormally(f); |
1330 |
checkCompletedNormally(g); |
1331 |
}}; |
1332 |
checkInvoke(a); |
1333 |
} |
1334 |
|
1335 |
/** |
1336 |
* invokeAll(tasks) with 1 argument invokes task |
1337 |
*/ |
1338 |
public void testInvokeAll1CC() { |
1339 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1340 |
protected void realCompute() { |
1341 |
CCF f = new LCCF(null, 8); |
1342 |
invokeAll(f); |
1343 |
checkCompletedNormally(f); |
1344 |
assertEquals(21, f.number); |
1345 |
}}; |
1346 |
checkInvoke(a); |
1347 |
} |
1348 |
|
1349 |
/** |
1350 |
* invokeAll(tasks) with > 2 argument invokes tasks |
1351 |
*/ |
1352 |
public void testInvokeAll3CC() { |
1353 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1354 |
protected void realCompute() { |
1355 |
CCF f = new LCCF(null, 8); |
1356 |
CCF g = new LCCF(null, 9); |
1357 |
CCF h = new LCCF(null, 7); |
1358 |
invokeAll(f, g, h); |
1359 |
assertEquals(21, f.number); |
1360 |
assertEquals(34, g.number); |
1361 |
assertEquals(13, h.number); |
1362 |
checkCompletedNormally(f); |
1363 |
checkCompletedNormally(g); |
1364 |
checkCompletedNormally(h); |
1365 |
}}; |
1366 |
checkInvoke(a); |
1367 |
} |
1368 |
|
1369 |
/** |
1370 |
* invokeAll(collection) invokes all tasks in the collection |
1371 |
*/ |
1372 |
public void testInvokeAllCollectionCC() { |
1373 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1374 |
protected void realCompute() { |
1375 |
CCF f = new LCCF(null, 8); |
1376 |
CCF g = new LCCF(null, 9); |
1377 |
CCF h = new LCCF(null, 7); |
1378 |
HashSet set = new HashSet(); |
1379 |
set.add(f); |
1380 |
set.add(g); |
1381 |
set.add(h); |
1382 |
invokeAll(set); |
1383 |
assertEquals(21, f.number); |
1384 |
assertEquals(34, g.number); |
1385 |
assertEquals(13, h.number); |
1386 |
checkCompletedNormally(f); |
1387 |
checkCompletedNormally(g); |
1388 |
checkCompletedNormally(h); |
1389 |
}}; |
1390 |
checkInvoke(a); |
1391 |
} |
1392 |
|
1393 |
/** |
1394 |
* invokeAll(tasks) with any null task throws NPE |
1395 |
*/ |
1396 |
public void testInvokeAllNPECC() { |
1397 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1398 |
protected void realCompute() { |
1399 |
CCF f = new LCCF(null, 8); |
1400 |
CCF g = new LCCF(null, 9); |
1401 |
CCF h = null; |
1402 |
try { |
1403 |
invokeAll(f, g, h); |
1404 |
shouldThrow(); |
1405 |
} catch (NullPointerException success) {} |
1406 |
}}; |
1407 |
checkInvoke(a); |
1408 |
} |
1409 |
|
1410 |
/** |
1411 |
* invokeAll(t1, t2) throw exception if any task does |
1412 |
*/ |
1413 |
public void testAbnormalInvokeAll2CC() { |
1414 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1415 |
protected void realCompute() { |
1416 |
CCF f = new LCCF(null, 8); |
1417 |
FailingCCF g = new LFCCF(null, 9); |
1418 |
try { |
1419 |
invokeAll(f, g); |
1420 |
shouldThrow(); |
1421 |
} catch (FJException success) { |
1422 |
checkCompletedAbnormally(g, success); |
1423 |
} |
1424 |
}}; |
1425 |
checkInvoke(a); |
1426 |
} |
1427 |
|
1428 |
/** |
1429 |
* invokeAll(tasks) with 1 argument throws exception if task does |
1430 |
*/ |
1431 |
public void testAbnormalInvokeAll1CC() { |
1432 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1433 |
protected void realCompute() { |
1434 |
FailingCCF g = new LFCCF(null, 9); |
1435 |
try { |
1436 |
invokeAll(g); |
1437 |
shouldThrow(); |
1438 |
} catch (FJException success) { |
1439 |
checkCompletedAbnormally(g, success); |
1440 |
} |
1441 |
}}; |
1442 |
checkInvoke(a); |
1443 |
} |
1444 |
|
1445 |
/** |
1446 |
* invokeAll(tasks) with > 2 argument throws exception if any task does |
1447 |
*/ |
1448 |
public void testAbnormalInvokeAll3CC() { |
1449 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1450 |
protected void realCompute() { |
1451 |
CCF f = new LCCF(null, 8); |
1452 |
FailingCCF g = new LFCCF(null, 9); |
1453 |
CCF h = new LCCF(null, 7); |
1454 |
try { |
1455 |
invokeAll(f, g, h); |
1456 |
shouldThrow(); |
1457 |
} catch (FJException success) { |
1458 |
checkCompletedAbnormally(g, success); |
1459 |
} |
1460 |
}}; |
1461 |
checkInvoke(a); |
1462 |
} |
1463 |
|
1464 |
/** |
1465 |
* invokeAll(collection) throws exception if any task does |
1466 |
*/ |
1467 |
public void testAbnormalInvokeAllCollectionCC() { |
1468 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1469 |
protected void realCompute() { |
1470 |
FailingCCF f = new LFCCF(null, 8); |
1471 |
CCF g = new LCCF(null, 9); |
1472 |
CCF h = new LCCF(null, 7); |
1473 |
HashSet set = new HashSet(); |
1474 |
set.add(f); |
1475 |
set.add(g); |
1476 |
set.add(h); |
1477 |
try { |
1478 |
invokeAll(set); |
1479 |
shouldThrow(); |
1480 |
} catch (FJException success) { |
1481 |
checkCompletedAbnormally(f, success); |
1482 |
} |
1483 |
}}; |
1484 |
checkInvoke(a); |
1485 |
} |
1486 |
|
1487 |
/** |
1488 |
* awaitQuiescence by a worker is equivalent in effect to |
1489 |
* ForkJoinTask.helpQuiesce() |
1490 |
*/ |
1491 |
public void testAwaitQuiescence1() throws Exception { |
1492 |
final ForkJoinPool p = new ForkJoinPool(); |
1493 |
try { |
1494 |
final long startTime = System.nanoTime(); |
1495 |
assertTrue(p.isQuiescent()); |
1496 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1497 |
protected void realCompute() { |
1498 |
FibAction f = new FibAction(8); |
1499 |
assertSame(f, f.fork()); |
1500 |
boolean quiescent = ForkJoinTask.getPool().awaitQuiescence(MEDIUM_DELAY_MS, TimeUnit.SECONDS); |
1501 |
assertTrue(quiescent); |
1502 |
while (!f.isDone()) { |
1503 |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1504 |
threadFail("timed out"); |
1505 |
assertFalse(p.getAsyncMode()); |
1506 |
assertFalse(p.isShutdown()); |
1507 |
assertFalse(p.isTerminating()); |
1508 |
assertFalse(p.isTerminated()); |
1509 |
Thread.yield(); |
1510 |
} |
1511 |
assertFalse(p.isQuiescent()); |
1512 |
assertEquals(0, ForkJoinTask.getQueuedTaskCount()); |
1513 |
assertEquals(21, f.result); |
1514 |
}}; |
1515 |
p.execute(a); |
1516 |
while (!a.isDone() || !p.isQuiescent()) { |
1517 |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1518 |
throw new AssertionFailedError("timed out"); |
1519 |
assertFalse(p.getAsyncMode()); |
1520 |
assertFalse(p.isShutdown()); |
1521 |
assertFalse(p.isTerminating()); |
1522 |
assertFalse(p.isTerminated()); |
1523 |
Thread.yield(); |
1524 |
} |
1525 |
assertEquals(0, p.getQueuedTaskCount()); |
1526 |
assertFalse(p.getAsyncMode()); |
1527 |
assertEquals(0, p.getActiveThreadCount()); |
1528 |
assertEquals(0, p.getQueuedTaskCount()); |
1529 |
assertEquals(0, p.getQueuedSubmissionCount()); |
1530 |
assertFalse(p.hasQueuedSubmissions()); |
1531 |
assertFalse(p.isShutdown()); |
1532 |
assertFalse(p.isTerminating()); |
1533 |
assertFalse(p.isTerminated()); |
1534 |
} finally { |
1535 |
joinPool(p); |
1536 |
} |
1537 |
} |
1538 |
|
1539 |
/** |
1540 |
* awaitQuiescence returns when pool isQuiescent() or the indicated |
1541 |
* timeout elapsed |
1542 |
*/ |
1543 |
public void testAwaitQuiescence2() throws Exception { |
1544 |
final ForkJoinPool p = new ForkJoinPool(); |
1545 |
try { |
1546 |
assertTrue(p.isQuiescent()); |
1547 |
for (;;) { |
1548 |
final long startTime = System.nanoTime(); |
1549 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1550 |
protected void realCompute() { |
1551 |
FibAction f = new FibAction(8); |
1552 |
assertSame(f, f.fork()); |
1553 |
ForkJoinTask.helpQuiesce(); |
1554 |
while (!f.isDone()) { |
1555 |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1556 |
threadFail("timed out"); |
1557 |
assertFalse(p.getAsyncMode()); |
1558 |
assertFalse(p.isShutdown()); |
1559 |
assertFalse(p.isTerminating()); |
1560 |
assertFalse(p.isTerminated()); |
1561 |
Thread.yield(); |
1562 |
} |
1563 |
assertEquals(0, ForkJoinTask.getQueuedTaskCount()); |
1564 |
assertEquals(21, f.result); |
1565 |
}}; |
1566 |
p.execute(a); |
1567 |
if (a.isDone() || p.isQuiescent()) |
1568 |
continue; // Already done so cannot test; retry |
1569 |
while (!p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS)) { |
1570 |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1571 |
threadFail("timed out"); |
1572 |
assertFalse(p.getAsyncMode()); |
1573 |
assertFalse(p.isShutdown()); |
1574 |
assertFalse(p.isTerminating()); |
1575 |
assertFalse(p.isTerminated()); |
1576 |
Thread.yield(); |
1577 |
} |
1578 |
assertTrue(p.isQuiescent()); |
1579 |
assertTrue(a.isDone()); |
1580 |
assertEquals(0, p.getQueuedTaskCount()); |
1581 |
assertFalse(p.getAsyncMode()); |
1582 |
assertEquals(0, p.getActiveThreadCount()); |
1583 |
assertEquals(0, p.getQueuedTaskCount()); |
1584 |
assertEquals(0, p.getQueuedSubmissionCount()); |
1585 |
assertFalse(p.hasQueuedSubmissions()); |
1586 |
assertFalse(p.isShutdown()); |
1587 |
assertFalse(p.isTerminating()); |
1588 |
assertFalse(p.isTerminated()); |
1589 |
break; |
1590 |
} |
1591 |
} finally { |
1592 |
joinPool(p); |
1593 |
} |
1594 |
} |
1595 |
|
1596 |
} |