ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinTaskTest.java
Revision: 1.46
Committed: Sun Oct 11 13:30:30 2015 UTC (8 years, 6 months ago) by dl
Branch: MAIN
Changes since 1.45: +12 -5 lines
Log Message:
fix cancellation in test class

File Contents

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