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