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