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