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