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