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