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