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