ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveActionTest.java
Revision: 1.27
Committed: Tue Nov 23 08:48:23 2010 UTC (13 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.26: +3 -2 lines
Log Message:
use ForkJoinTask.inForkJoinPool

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