ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveActionTest.java
Revision: 1.29
Committed: Tue Feb 22 01:18:59 2011 UTC (13 years, 2 months ago) by dl
Branch: MAIN
Changes since 1.28: +9 -7 lines
Log Message:
Tests for expected exceptions should only assert same class, not identity

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