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