ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveActionTest.java
Revision: 1.21
Committed: Sun Nov 21 19:06:53 2010 UTC (13 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.20: +17 -12 lines
Log Message:
miscellaneous test improvements

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