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 |
private static ForkJoinPool mainPool() { |
29 |
return new ForkJoinPool(); |
30 |
} |
31 |
|
32 |
private static ForkJoinPool singletonPool() { |
33 |
return new ForkJoinPool(1); |
34 |
} |
35 |
|
36 |
private static ForkJoinPool asyncSingletonPool() { |
37 |
return new ForkJoinPool(1, |
38 |
ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
39 |
null, true); |
40 |
} |
41 |
|
42 |
private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { |
43 |
try { |
44 |
assertFalse(a.isDone()); |
45 |
assertFalse(a.isCompletedNormally()); |
46 |
assertFalse(a.isCompletedAbnormally()); |
47 |
assertFalse(a.isCancelled()); |
48 |
assertNull(a.getException()); |
49 |
|
50 |
assertNull(pool.invoke(a)); |
51 |
|
52 |
assertTrue(a.isDone()); |
53 |
assertTrue(a.isCompletedNormally()); |
54 |
assertFalse(a.isCompletedAbnormally()); |
55 |
assertFalse(a.isCancelled()); |
56 |
assertNull(a.getException()); |
57 |
} finally { |
58 |
joinPool(pool); |
59 |
} |
60 |
} |
61 |
|
62 |
/* |
63 |
* Testing coverage notes: |
64 |
* |
65 |
* To test extension methods and overrides, most tests use |
66 |
* BinaryAsyncAction extension class that processes joins |
67 |
* differently than supplied Recursive forms. |
68 |
*/ |
69 |
|
70 |
static final class FJException extends RuntimeException { |
71 |
FJException() { super(); } |
72 |
} |
73 |
|
74 |
static abstract class BinaryAsyncAction extends ForkJoinTask<Void> { |
75 |
private volatile int controlState; |
76 |
|
77 |
static final AtomicIntegerFieldUpdater<BinaryAsyncAction> controlStateUpdater = |
78 |
AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class, |
79 |
"controlState"); |
80 |
|
81 |
private BinaryAsyncAction parent; |
82 |
|
83 |
private BinaryAsyncAction sibling; |
84 |
|
85 |
protected BinaryAsyncAction() { |
86 |
} |
87 |
|
88 |
public final Void getRawResult() { return null; } |
89 |
protected final void setRawResult(Void mustBeNull) { } |
90 |
|
91 |
public final void linkSubtasks(BinaryAsyncAction x, BinaryAsyncAction y) { |
92 |
x.parent = y.parent = this; |
93 |
x.sibling = y; |
94 |
y.sibling = x; |
95 |
} |
96 |
|
97 |
protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) { |
98 |
} |
99 |
|
100 |
protected boolean onException() { |
101 |
return true; |
102 |
} |
103 |
|
104 |
public void linkAndForkSubtasks(BinaryAsyncAction x, BinaryAsyncAction y) { |
105 |
linkSubtasks(x, y); |
106 |
y.fork(); |
107 |
x.fork(); |
108 |
} |
109 |
|
110 |
private void completeThis() { |
111 |
super.complete(null); |
112 |
} |
113 |
|
114 |
private void completeThisExceptionally(Throwable ex) { |
115 |
super.completeExceptionally(ex); |
116 |
} |
117 |
|
118 |
public final void complete() { |
119 |
BinaryAsyncAction a = this; |
120 |
for (;;) { |
121 |
BinaryAsyncAction s = a.sibling; |
122 |
BinaryAsyncAction p = a.parent; |
123 |
a.sibling = null; |
124 |
a.parent = null; |
125 |
a.completeThis(); |
126 |
if (p == null || p.compareAndSetControlState(0, 1)) |
127 |
break; |
128 |
try { |
129 |
p.onComplete(a, s); |
130 |
} catch (Throwable rex) { |
131 |
p.completeExceptionally(rex); |
132 |
return; |
133 |
} |
134 |
a = p; |
135 |
} |
136 |
} |
137 |
|
138 |
public final void completeExceptionally(Throwable ex) { |
139 |
BinaryAsyncAction a = this; |
140 |
while (!a.isCompletedAbnormally()) { |
141 |
a.completeThisExceptionally(ex); |
142 |
BinaryAsyncAction s = a.sibling; |
143 |
if (s != null) |
144 |
s.cancel(false); |
145 |
if (!a.onException() || (a = a.parent) == null) |
146 |
break; |
147 |
} |
148 |
} |
149 |
|
150 |
public final BinaryAsyncAction getParent() { |
151 |
return parent; |
152 |
} |
153 |
|
154 |
public BinaryAsyncAction getSibling() { |
155 |
return sibling; |
156 |
} |
157 |
|
158 |
public void reinitialize() { |
159 |
parent = sibling = null; |
160 |
super.reinitialize(); |
161 |
} |
162 |
|
163 |
protected final int getControlState() { |
164 |
return controlState; |
165 |
} |
166 |
|
167 |
protected final boolean compareAndSetControlState(int expect, |
168 |
int update) { |
169 |
return controlStateUpdater.compareAndSet(this, expect, update); |
170 |
} |
171 |
|
172 |
protected final void setControlState(int value) { |
173 |
controlState = value; |
174 |
} |
175 |
|
176 |
protected final void incrementControlState() { |
177 |
controlStateUpdater.incrementAndGet(this); |
178 |
} |
179 |
|
180 |
protected final void decrementControlState() { |
181 |
controlStateUpdater.decrementAndGet(this); |
182 |
} |
183 |
|
184 |
} |
185 |
|
186 |
static final class AsyncFib extends BinaryAsyncAction { |
187 |
int number; |
188 |
public AsyncFib(int n) { |
189 |
this.number = n; |
190 |
} |
191 |
|
192 |
public final boolean exec() { |
193 |
AsyncFib f = this; |
194 |
int n = f.number; |
195 |
if (n > 1) { |
196 |
while (n > 1) { |
197 |
AsyncFib p = f; |
198 |
AsyncFib r = new AsyncFib(n - 2); |
199 |
f = new AsyncFib(--n); |
200 |
p.linkSubtasks(r, f); |
201 |
r.fork(); |
202 |
} |
203 |
f.number = n; |
204 |
} |
205 |
f.complete(); |
206 |
return false; |
207 |
} |
208 |
|
209 |
protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) { |
210 |
number = ((AsyncFib)x).number + ((AsyncFib)y).number; |
211 |
} |
212 |
} |
213 |
|
214 |
|
215 |
static final class FailingAsyncFib extends BinaryAsyncAction { |
216 |
int number; |
217 |
public FailingAsyncFib(int n) { |
218 |
this.number = n; |
219 |
} |
220 |
|
221 |
public final boolean exec() { |
222 |
FailingAsyncFib f = this; |
223 |
int n = f.number; |
224 |
if (n > 1) { |
225 |
while (n > 1) { |
226 |
FailingAsyncFib p = f; |
227 |
FailingAsyncFib r = new FailingAsyncFib(n - 2); |
228 |
f = new FailingAsyncFib(--n); |
229 |
p.linkSubtasks(r, f); |
230 |
r.fork(); |
231 |
} |
232 |
f.number = n; |
233 |
} |
234 |
f.complete(); |
235 |
return false; |
236 |
} |
237 |
|
238 |
protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) { |
239 |
completeExceptionally(new FJException()); |
240 |
} |
241 |
} |
242 |
|
243 |
/** |
244 |
* invoke returns when task completes normally. |
245 |
* isCompletedAbnormally and isCancelled return false for normally |
246 |
* completed tasks. getRawResult of a RecursiveAction returns null; |
247 |
*/ |
248 |
public void testInvoke() { |
249 |
RecursiveAction a = new CheckedRecursiveAction() { |
250 |
public void realCompute() { |
251 |
AsyncFib f = new AsyncFib(8); |
252 |
assertNull(f.invoke()); |
253 |
assertEquals(21, f.number); |
254 |
assertTrue(f.isDone()); |
255 |
assertFalse(f.isCancelled()); |
256 |
assertFalse(f.isCompletedAbnormally()); |
257 |
assertNull(f.getRawResult()); |
258 |
}}; |
259 |
testInvokeOnPool(mainPool(), a); |
260 |
} |
261 |
|
262 |
/** |
263 |
* quietlyInvoke task returns when task completes normally. |
264 |
* isCompletedAbnormally and isCancelled return false for normally |
265 |
* completed tasks |
266 |
*/ |
267 |
public void testQuietlyInvoke() { |
268 |
RecursiveAction a = new CheckedRecursiveAction() { |
269 |
public void realCompute() { |
270 |
AsyncFib f = new AsyncFib(8); |
271 |
f.quietlyInvoke(); |
272 |
assertEquals(21, f.number); |
273 |
assertTrue(f.isDone()); |
274 |
assertFalse(f.isCancelled()); |
275 |
assertFalse(f.isCompletedAbnormally()); |
276 |
assertNull(f.getRawResult()); |
277 |
}}; |
278 |
testInvokeOnPool(mainPool(), a); |
279 |
} |
280 |
|
281 |
/** |
282 |
* join of a forked task returns when task completes |
283 |
*/ |
284 |
public void testForkJoin() { |
285 |
RecursiveAction a = new CheckedRecursiveAction() { |
286 |
public void realCompute() { |
287 |
AsyncFib f = new AsyncFib(8); |
288 |
assertSame(f, f.fork()); |
289 |
assertNull(f.join()); |
290 |
assertEquals(21, f.number); |
291 |
assertTrue(f.isDone()); |
292 |
assertNull(f.getRawResult()); |
293 |
}}; |
294 |
testInvokeOnPool(mainPool(), a); |
295 |
} |
296 |
|
297 |
/** |
298 |
* get of a forked task returns when task completes |
299 |
*/ |
300 |
public void testForkGet() { |
301 |
RecursiveAction a = new CheckedRecursiveAction() { |
302 |
public void realCompute() throws Exception { |
303 |
AsyncFib f = new AsyncFib(8); |
304 |
assertSame(f, f.fork()); |
305 |
assertNull(f.get()); |
306 |
assertEquals(21, f.number); |
307 |
assertTrue(f.isDone()); |
308 |
}}; |
309 |
testInvokeOnPool(mainPool(), a); |
310 |
} |
311 |
|
312 |
/** |
313 |
* timed get of a forked task returns when task completes |
314 |
*/ |
315 |
public void testForkTimedGet() { |
316 |
RecursiveAction a = new CheckedRecursiveAction() { |
317 |
public void realCompute() throws Exception { |
318 |
AsyncFib f = new AsyncFib(8); |
319 |
assertSame(f, f.fork()); |
320 |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
321 |
assertEquals(21, f.number); |
322 |
assertTrue(f.isDone()); |
323 |
}}; |
324 |
testInvokeOnPool(mainPool(), a); |
325 |
} |
326 |
|
327 |
/** |
328 |
* timed get with null time unit throws NPE |
329 |
*/ |
330 |
public void testForkTimedGetNPE() { |
331 |
RecursiveAction a = new CheckedRecursiveAction() { |
332 |
public void realCompute() throws Exception { |
333 |
AsyncFib f = new AsyncFib(8); |
334 |
assertSame(f, f.fork()); |
335 |
try { |
336 |
f.get(5L, null); |
337 |
shouldThrow(); |
338 |
} catch (NullPointerException success) {} |
339 |
}}; |
340 |
testInvokeOnPool(mainPool(), a); |
341 |
} |
342 |
|
343 |
/** |
344 |
* quietlyJoin of a forked task returns when task completes |
345 |
*/ |
346 |
public void testForkQuietlyJoin() { |
347 |
RecursiveAction a = new CheckedRecursiveAction() { |
348 |
public void realCompute() { |
349 |
AsyncFib f = new AsyncFib(8); |
350 |
assertSame(f, f.fork()); |
351 |
f.quietlyJoin(); |
352 |
assertEquals(21, f.number); |
353 |
assertTrue(f.isDone()); |
354 |
}}; |
355 |
testInvokeOnPool(mainPool(), a); |
356 |
} |
357 |
|
358 |
|
359 |
/** |
360 |
* helpQuiesce returns when tasks are complete. |
361 |
* getQueuedTaskCount returns 0 when quiescent |
362 |
*/ |
363 |
public void testForkHelpQuiesce() { |
364 |
RecursiveAction a = new CheckedRecursiveAction() { |
365 |
public void realCompute() { |
366 |
AsyncFib f = new AsyncFib(8); |
367 |
assertSame(f, f.fork()); |
368 |
f.helpQuiesce(); |
369 |
assertEquals(21, f.number); |
370 |
assertTrue(f.isDone()); |
371 |
assertEquals(0, getQueuedTaskCount()); |
372 |
}}; |
373 |
testInvokeOnPool(mainPool(), a); |
374 |
} |
375 |
|
376 |
|
377 |
/** |
378 |
* invoke task throws exception when task completes abnormally |
379 |
*/ |
380 |
public void testAbnormalInvoke() { |
381 |
RecursiveAction a = new CheckedRecursiveAction() { |
382 |
public void realCompute() { |
383 |
FailingAsyncFib f = new FailingAsyncFib(8); |
384 |
try { |
385 |
f.invoke(); |
386 |
shouldThrow(); |
387 |
} catch (FJException success) {} |
388 |
}}; |
389 |
testInvokeOnPool(mainPool(), a); |
390 |
} |
391 |
|
392 |
/** |
393 |
* quietlyInvoke task returns when task completes abnormally |
394 |
*/ |
395 |
public void testAbnormalQuietlyInvoke() { |
396 |
RecursiveAction a = new CheckedRecursiveAction() { |
397 |
public void realCompute() { |
398 |
FailingAsyncFib f = new FailingAsyncFib(8); |
399 |
f.quietlyInvoke(); |
400 |
assertTrue(f.isDone()); |
401 |
}}; |
402 |
testInvokeOnPool(mainPool(), a); |
403 |
} |
404 |
|
405 |
/** |
406 |
* join of a forked task throws exception when task completes abnormally |
407 |
*/ |
408 |
public void testAbnormalForkJoin() { |
409 |
RecursiveAction a = new CheckedRecursiveAction() { |
410 |
public void realCompute() { |
411 |
FailingAsyncFib f = new FailingAsyncFib(8); |
412 |
assertSame(f, f.fork()); |
413 |
try { |
414 |
f.join(); |
415 |
shouldThrow(); |
416 |
} catch (FJException success) {} |
417 |
}}; |
418 |
testInvokeOnPool(mainPool(), a); |
419 |
} |
420 |
|
421 |
/** |
422 |
* get of a forked task throws exception when task completes abnormally |
423 |
*/ |
424 |
public void testAbnormalForkGet() { |
425 |
RecursiveAction a = new CheckedRecursiveAction() { |
426 |
public void realCompute() throws Exception { |
427 |
FailingAsyncFib f = new FailingAsyncFib(8); |
428 |
assertSame(f, f.fork()); |
429 |
try { |
430 |
f.get(); |
431 |
shouldThrow(); |
432 |
} catch (ExecutionException success) { |
433 |
Throwable cause = success.getCause(); |
434 |
assertTrue(cause instanceof FJException); |
435 |
assertTrue(f.isDone()); |
436 |
assertTrue(f.isCompletedAbnormally()); |
437 |
assertSame(cause, f.getException()); |
438 |
} |
439 |
}}; |
440 |
testInvokeOnPool(mainPool(), a); |
441 |
} |
442 |
|
443 |
/** |
444 |
* timed get of a forked task throws exception when task completes abnormally |
445 |
*/ |
446 |
public void testAbnormalForkTimedGet() { |
447 |
RecursiveAction a = new CheckedRecursiveAction() { |
448 |
public void realCompute() throws Exception { |
449 |
FailingAsyncFib f = new FailingAsyncFib(8); |
450 |
assertSame(f, f.fork()); |
451 |
try { |
452 |
f.get(LONG_DELAY_MS, MILLISECONDS); |
453 |
shouldThrow(); |
454 |
} catch (ExecutionException success) { |
455 |
Throwable cause = success.getCause(); |
456 |
assertTrue(cause instanceof FJException); |
457 |
assertTrue(f.isDone()); |
458 |
assertTrue(f.isCompletedAbnormally()); |
459 |
assertSame(cause, f.getException()); |
460 |
} |
461 |
}}; |
462 |
testInvokeOnPool(mainPool(), a); |
463 |
} |
464 |
|
465 |
/** |
466 |
* quietlyJoin of a forked task returns when task completes abnormally |
467 |
*/ |
468 |
public void testAbnormalForkQuietlyJoin() { |
469 |
RecursiveAction a = new CheckedRecursiveAction() { |
470 |
public void realCompute() { |
471 |
FailingAsyncFib f = new FailingAsyncFib(8); |
472 |
assertSame(f, f.fork()); |
473 |
f.quietlyJoin(); |
474 |
assertTrue(f.isDone()); |
475 |
assertTrue(f.isCompletedAbnormally()); |
476 |
assertTrue(f.getException() instanceof FJException); |
477 |
}}; |
478 |
testInvokeOnPool(mainPool(), a); |
479 |
} |
480 |
|
481 |
/** |
482 |
* invoke task throws exception when task cancelled |
483 |
*/ |
484 |
public void testCancelledInvoke() { |
485 |
RecursiveAction a = new CheckedRecursiveAction() { |
486 |
public void realCompute() { |
487 |
AsyncFib f = new AsyncFib(8); |
488 |
assertTrue(f.cancel(true)); |
489 |
try { |
490 |
f.invoke(); |
491 |
shouldThrow(); |
492 |
} catch (CancellationException success) { |
493 |
assertTrue(f.isDone()); |
494 |
assertTrue(f.isCancelled()); |
495 |
assertTrue(f.isCompletedAbnormally()); |
496 |
assertTrue(f.getException() instanceof CancellationException); |
497 |
} |
498 |
}}; |
499 |
testInvokeOnPool(mainPool(), a); |
500 |
} |
501 |
|
502 |
/** |
503 |
* join of a forked task throws exception when task cancelled |
504 |
*/ |
505 |
public void testCancelledForkJoin() { |
506 |
RecursiveAction a = new CheckedRecursiveAction() { |
507 |
public void realCompute() { |
508 |
AsyncFib f = new AsyncFib(8); |
509 |
assertTrue(f.cancel(true)); |
510 |
assertSame(f, f.fork()); |
511 |
try { |
512 |
f.join(); |
513 |
shouldThrow(); |
514 |
} catch (CancellationException success) { |
515 |
assertTrue(f.isDone()); |
516 |
assertTrue(f.isCancelled()); |
517 |
assertTrue(f.isCompletedAbnormally()); |
518 |
assertTrue(f.getException() instanceof CancellationException); |
519 |
} |
520 |
}}; |
521 |
testInvokeOnPool(mainPool(), a); |
522 |
} |
523 |
|
524 |
/** |
525 |
* get of a forked task throws exception when task cancelled |
526 |
*/ |
527 |
public void testCancelledForkGet() { |
528 |
RecursiveAction a = new CheckedRecursiveAction() { |
529 |
public void realCompute() throws Exception { |
530 |
AsyncFib f = new AsyncFib(8); |
531 |
assertTrue(f.cancel(true)); |
532 |
assertSame(f, f.fork()); |
533 |
try { |
534 |
f.get(); |
535 |
shouldThrow(); |
536 |
} catch (CancellationException success) { |
537 |
assertTrue(f.isDone()); |
538 |
assertTrue(f.isCancelled()); |
539 |
assertTrue(f.isCompletedAbnormally()); |
540 |
assertTrue(f.getException() instanceof CancellationException); |
541 |
} |
542 |
}}; |
543 |
testInvokeOnPool(mainPool(), a); |
544 |
} |
545 |
|
546 |
/** |
547 |
* timed get of a forked task throws exception when task cancelled |
548 |
*/ |
549 |
public void testCancelledForkTimedGet() throws Exception { |
550 |
RecursiveAction a = new CheckedRecursiveAction() { |
551 |
public void realCompute() throws Exception { |
552 |
AsyncFib f = new AsyncFib(8); |
553 |
assertTrue(f.cancel(true)); |
554 |
assertSame(f, f.fork()); |
555 |
try { |
556 |
f.get(LONG_DELAY_MS, MILLISECONDS); |
557 |
shouldThrow(); |
558 |
} catch (CancellationException success) { |
559 |
assertTrue(f.isDone()); |
560 |
assertTrue(f.isCancelled()); |
561 |
assertTrue(f.isCompletedAbnormally()); |
562 |
assertTrue(f.getException() instanceof CancellationException); |
563 |
} |
564 |
}}; |
565 |
testInvokeOnPool(mainPool(), a); |
566 |
} |
567 |
|
568 |
/** |
569 |
* quietlyJoin of a forked task returns when task cancelled |
570 |
*/ |
571 |
public void testCancelledForkQuietlyJoin() { |
572 |
RecursiveAction a = new CheckedRecursiveAction() { |
573 |
public void realCompute() { |
574 |
AsyncFib f = new AsyncFib(8); |
575 |
assertTrue(f.cancel(true)); |
576 |
assertSame(f, f.fork()); |
577 |
f.quietlyJoin(); |
578 |
assertTrue(f.isDone()); |
579 |
assertTrue(f.isCompletedAbnormally()); |
580 |
assertTrue(f.isCancelled()); |
581 |
assertTrue(f.getException() instanceof CancellationException); |
582 |
}}; |
583 |
testInvokeOnPool(mainPool(), a); |
584 |
} |
585 |
|
586 |
/** |
587 |
* getPool of executing task returns its pool |
588 |
*/ |
589 |
public void testGetPool() { |
590 |
final ForkJoinPool mainPool = mainPool(); |
591 |
RecursiveAction a = new CheckedRecursiveAction() { |
592 |
public void realCompute() { |
593 |
assertSame(mainPool, getPool()); |
594 |
}}; |
595 |
testInvokeOnPool(mainPool, a); |
596 |
} |
597 |
|
598 |
/** |
599 |
* getPool of non-FJ task returns null |
600 |
*/ |
601 |
public void testGetPool2() { |
602 |
RecursiveAction a = new CheckedRecursiveAction() { |
603 |
public void realCompute() { |
604 |
assertNull(getPool()); |
605 |
}}; |
606 |
assertNull(a.invoke()); |
607 |
} |
608 |
|
609 |
/** |
610 |
* inForkJoinPool of executing task returns true |
611 |
*/ |
612 |
public void testInForkJoinPool() { |
613 |
RecursiveAction a = new CheckedRecursiveAction() { |
614 |
public void realCompute() { |
615 |
assertTrue(inForkJoinPool()); |
616 |
}}; |
617 |
testInvokeOnPool(mainPool(), a); |
618 |
} |
619 |
|
620 |
/** |
621 |
* inForkJoinPool of non-FJ task returns false |
622 |
*/ |
623 |
public void testInForkJoinPool2() { |
624 |
RecursiveAction a = new CheckedRecursiveAction() { |
625 |
public void realCompute() { |
626 |
assertTrue(!inForkJoinPool()); |
627 |
}}; |
628 |
assertNull(a.invoke()); |
629 |
} |
630 |
|
631 |
/** |
632 |
* setRawResult(null) succeeds |
633 |
*/ |
634 |
public void testSetRawResult() { |
635 |
RecursiveAction a = new CheckedRecursiveAction() { |
636 |
public void realCompute() { |
637 |
setRawResult(null); |
638 |
}}; |
639 |
assertNull(a.invoke()); |
640 |
} |
641 |
|
642 |
/** |
643 |
* invoke task throws exception after invoking completeExceptionally |
644 |
*/ |
645 |
public void testCompleteExceptionally() { |
646 |
RecursiveAction a = new CheckedRecursiveAction() { |
647 |
public void realCompute() { |
648 |
AsyncFib f = new AsyncFib(8); |
649 |
f.completeExceptionally(new FJException()); |
650 |
try { |
651 |
f.invoke(); |
652 |
shouldThrow(); |
653 |
} catch (FJException success) {} |
654 |
}}; |
655 |
testInvokeOnPool(mainPool(), a); |
656 |
} |
657 |
|
658 |
/** |
659 |
* invokeAll(t1, t2) invokes all task arguments |
660 |
*/ |
661 |
public void testInvokeAll2() { |
662 |
RecursiveAction a = new CheckedRecursiveAction() { |
663 |
public void realCompute() { |
664 |
AsyncFib f = new AsyncFib(8); |
665 |
AsyncFib g = new AsyncFib(9); |
666 |
invokeAll(f, g); |
667 |
assertTrue(f.isDone()); |
668 |
assertEquals(21, f.number); |
669 |
assertTrue(g.isDone()); |
670 |
assertEquals(34, g.number); |
671 |
}}; |
672 |
testInvokeOnPool(mainPool(), a); |
673 |
} |
674 |
|
675 |
/** |
676 |
* invokeAll(tasks) with 1 argument invokes task |
677 |
*/ |
678 |
public void testInvokeAll1() { |
679 |
RecursiveAction a = new CheckedRecursiveAction() { |
680 |
public void realCompute() { |
681 |
AsyncFib f = new AsyncFib(8); |
682 |
invokeAll(f); |
683 |
assertTrue(f.isDone()); |
684 |
assertEquals(21, f.number); |
685 |
}}; |
686 |
testInvokeOnPool(mainPool(), a); |
687 |
} |
688 |
|
689 |
/** |
690 |
* invokeAll(tasks) with > 2 argument invokes tasks |
691 |
*/ |
692 |
public void testInvokeAll3() { |
693 |
RecursiveAction a = new CheckedRecursiveAction() { |
694 |
public void realCompute() { |
695 |
AsyncFib f = new AsyncFib(8); |
696 |
AsyncFib g = new AsyncFib(9); |
697 |
AsyncFib h = new AsyncFib(7); |
698 |
invokeAll(f, g, h); |
699 |
assertTrue(f.isDone()); |
700 |
assertEquals(21, f.number); |
701 |
assertTrue(g.isDone()); |
702 |
assertEquals(34, g.number); |
703 |
assertTrue(h.isDone()); |
704 |
assertEquals(13, h.number); |
705 |
}}; |
706 |
testInvokeOnPool(mainPool(), a); |
707 |
} |
708 |
|
709 |
/** |
710 |
* invokeAll(collection) invokes all tasks in the collection |
711 |
*/ |
712 |
public void testInvokeAllCollection() { |
713 |
RecursiveAction a = new CheckedRecursiveAction() { |
714 |
public void realCompute() { |
715 |
AsyncFib f = new AsyncFib(8); |
716 |
AsyncFib g = new AsyncFib(9); |
717 |
AsyncFib h = new AsyncFib(7); |
718 |
HashSet set = new HashSet(); |
719 |
set.add(f); |
720 |
set.add(g); |
721 |
set.add(h); |
722 |
invokeAll(set); |
723 |
assertTrue(f.isDone()); |
724 |
assertEquals(21, f.number); |
725 |
assertTrue(g.isDone()); |
726 |
assertEquals(34, g.number); |
727 |
assertTrue(h.isDone()); |
728 |
assertEquals(13, h.number); |
729 |
}}; |
730 |
testInvokeOnPool(mainPool(), a); |
731 |
} |
732 |
|
733 |
|
734 |
/** |
735 |
* invokeAll(tasks) with any null task throws NPE |
736 |
*/ |
737 |
public void testInvokeAllNPE() { |
738 |
RecursiveAction a = new CheckedRecursiveAction() { |
739 |
public void realCompute() { |
740 |
AsyncFib f = new AsyncFib(8); |
741 |
AsyncFib g = new AsyncFib(9); |
742 |
AsyncFib h = null; |
743 |
try { |
744 |
invokeAll(f, g, h); |
745 |
shouldThrow(); |
746 |
} catch (NullPointerException success) {} |
747 |
}}; |
748 |
testInvokeOnPool(mainPool(), a); |
749 |
} |
750 |
|
751 |
/** |
752 |
* invokeAll(t1, t2) throw exception if any task does |
753 |
*/ |
754 |
public void testAbnormalInvokeAll2() { |
755 |
RecursiveAction a = new CheckedRecursiveAction() { |
756 |
public void realCompute() { |
757 |
AsyncFib f = new AsyncFib(8); |
758 |
FailingAsyncFib g = new FailingAsyncFib(9); |
759 |
try { |
760 |
invokeAll(f, g); |
761 |
shouldThrow(); |
762 |
} catch (FJException success) {} |
763 |
}}; |
764 |
testInvokeOnPool(mainPool(), a); |
765 |
} |
766 |
|
767 |
/** |
768 |
* invokeAll(tasks) with 1 argument throws exception if task does |
769 |
*/ |
770 |
public void testAbnormalInvokeAll1() { |
771 |
RecursiveAction a = new CheckedRecursiveAction() { |
772 |
public void realCompute() { |
773 |
FailingAsyncFib g = new FailingAsyncFib(9); |
774 |
try { |
775 |
invokeAll(g); |
776 |
shouldThrow(); |
777 |
} catch (FJException success) {} |
778 |
}}; |
779 |
testInvokeOnPool(mainPool(), a); |
780 |
} |
781 |
|
782 |
/** |
783 |
* invokeAll(tasks) with > 2 argument throws exception if any task does |
784 |
*/ |
785 |
public void testAbnormalInvokeAll3() { |
786 |
RecursiveAction a = new CheckedRecursiveAction() { |
787 |
public void realCompute() { |
788 |
AsyncFib f = new AsyncFib(8); |
789 |
FailingAsyncFib g = new FailingAsyncFib(9); |
790 |
AsyncFib h = new AsyncFib(7); |
791 |
try { |
792 |
invokeAll(f, g, h); |
793 |
shouldThrow(); |
794 |
} catch (FJException success) {} |
795 |
}}; |
796 |
testInvokeOnPool(mainPool(), a); |
797 |
} |
798 |
|
799 |
/** |
800 |
* invokeAll(collection) throws exception if any task does |
801 |
*/ |
802 |
public void testAbnormalInvokeAllCollection() { |
803 |
RecursiveAction a = new CheckedRecursiveAction() { |
804 |
public void realCompute() { |
805 |
FailingAsyncFib f = new FailingAsyncFib(8); |
806 |
AsyncFib g = new AsyncFib(9); |
807 |
AsyncFib h = new AsyncFib(7); |
808 |
HashSet set = new HashSet(); |
809 |
set.add(f); |
810 |
set.add(g); |
811 |
set.add(h); |
812 |
try { |
813 |
invokeAll(set); |
814 |
shouldThrow(); |
815 |
} catch (FJException success) {} |
816 |
}}; |
817 |
testInvokeOnPool(mainPool(), a); |
818 |
} |
819 |
|
820 |
/** |
821 |
* tryUnfork returns true for most recent unexecuted task, |
822 |
* and suppresses execution |
823 |
*/ |
824 |
public void testTryUnfork() { |
825 |
RecursiveAction a = new CheckedRecursiveAction() { |
826 |
public void realCompute() { |
827 |
AsyncFib g = new AsyncFib(9); |
828 |
assertSame(g, g.fork()); |
829 |
AsyncFib f = new AsyncFib(8); |
830 |
assertSame(f, f.fork()); |
831 |
assertTrue(f.tryUnfork()); |
832 |
helpQuiesce(); |
833 |
assertFalse(f.isDone()); |
834 |
assertTrue(g.isDone()); |
835 |
}}; |
836 |
testInvokeOnPool(singletonPool(), a); |
837 |
} |
838 |
|
839 |
/** |
840 |
* getSurplusQueuedTaskCount returns > 0 when |
841 |
* there are more tasks than threads |
842 |
*/ |
843 |
public void testGetSurplusQueuedTaskCount() { |
844 |
RecursiveAction a = new CheckedRecursiveAction() { |
845 |
public void realCompute() { |
846 |
AsyncFib h = new AsyncFib(7); |
847 |
assertSame(h, h.fork()); |
848 |
AsyncFib g = new AsyncFib(9); |
849 |
assertSame(g, g.fork()); |
850 |
AsyncFib f = new AsyncFib(8); |
851 |
assertSame(f, f.fork()); |
852 |
assertTrue(getSurplusQueuedTaskCount() > 0); |
853 |
helpQuiesce(); |
854 |
}}; |
855 |
testInvokeOnPool(singletonPool(), a); |
856 |
} |
857 |
|
858 |
/** |
859 |
* peekNextLocalTask returns most recent unexecuted task. |
860 |
*/ |
861 |
public void testPeekNextLocalTask() { |
862 |
RecursiveAction a = new CheckedRecursiveAction() { |
863 |
public void realCompute() { |
864 |
AsyncFib g = new AsyncFib(9); |
865 |
assertSame(g, g.fork()); |
866 |
AsyncFib f = new AsyncFib(8); |
867 |
assertSame(f, f.fork()); |
868 |
assertSame(f, peekNextLocalTask()); |
869 |
assertNull(f.join()); |
870 |
assertTrue(f.isDone()); |
871 |
helpQuiesce(); |
872 |
}}; |
873 |
testInvokeOnPool(singletonPool(), a); |
874 |
} |
875 |
|
876 |
/** |
877 |
* pollNextLocalTask returns most recent unexecuted task |
878 |
* without executing it |
879 |
*/ |
880 |
public void testPollNextLocalTask() { |
881 |
RecursiveAction a = new CheckedRecursiveAction() { |
882 |
public void realCompute() { |
883 |
AsyncFib g = new AsyncFib(9); |
884 |
assertSame(g, g.fork()); |
885 |
AsyncFib f = new AsyncFib(8); |
886 |
assertSame(f, f.fork()); |
887 |
assertSame(f, pollNextLocalTask()); |
888 |
helpQuiesce(); |
889 |
assertFalse(f.isDone()); |
890 |
}}; |
891 |
testInvokeOnPool(singletonPool(), a); |
892 |
} |
893 |
|
894 |
/** |
895 |
* pollTask returns an unexecuted task |
896 |
* without executing it |
897 |
*/ |
898 |
public void testPollTask() { |
899 |
RecursiveAction a = new CheckedRecursiveAction() { |
900 |
public 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 |
assertFalse(f.isDone()); |
908 |
assertTrue(g.isDone()); |
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 |
public 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 |
assertTrue(f.isDone()); |
927 |
}}; |
928 |
testInvokeOnPool(asyncSingletonPool(), a); |
929 |
} |
930 |
|
931 |
/** |
932 |
* pollNextLocalTask returns least recent unexecuted task |
933 |
* without executing it, in async mode |
934 |
*/ |
935 |
public void testPollNextLocalTaskAsync() { |
936 |
RecursiveAction a = new CheckedRecursiveAction() { |
937 |
public void realCompute() { |
938 |
AsyncFib g = new AsyncFib(9); |
939 |
assertSame(g, g.fork()); |
940 |
AsyncFib f = new AsyncFib(8); |
941 |
assertSame(f, f.fork()); |
942 |
assertSame(g, pollNextLocalTask()); |
943 |
helpQuiesce(); |
944 |
assertTrue(f.isDone()); |
945 |
assertFalse(g.isDone()); |
946 |
}}; |
947 |
testInvokeOnPool(asyncSingletonPool(), a); |
948 |
} |
949 |
|
950 |
/** |
951 |
* pollTask returns an unexecuted task |
952 |
* without executing it, in async mode |
953 |
*/ |
954 |
public void testPollTaskAsync() { |
955 |
RecursiveAction a = new CheckedRecursiveAction() { |
956 |
public void realCompute() { |
957 |
AsyncFib g = new AsyncFib(9); |
958 |
assertSame(g, g.fork()); |
959 |
AsyncFib f = new AsyncFib(8); |
960 |
assertSame(f, f.fork()); |
961 |
assertSame(g, pollTask()); |
962 |
helpQuiesce(); |
963 |
assertTrue(f.isDone()); |
964 |
assertFalse(g.isDone()); |
965 |
}}; |
966 |
testInvokeOnPool(asyncSingletonPool(), a); |
967 |
} |
968 |
} |