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