ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinTaskTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ForkJoinTaskTest.java (file contents):
Revision 1.17 by jsr166, Thu Sep 16 00:52:49 2010 UTC vs.
Revision 1.25 by jsr166, Sun Nov 21 20:32:15 2010 UTC

# Line 10 | Line 10 | import java.util.concurrent.ForkJoinTask
10   import java.util.concurrent.ForkJoinWorkerThread;
11   import java.util.concurrent.RecursiveAction;
12   import java.util.concurrent.TimeUnit;
13 + import java.util.concurrent.TimeoutException;
14   import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
15 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
16 + import static java.util.concurrent.TimeUnit.SECONDS;
17   import java.util.HashSet;
18   import junit.framework.*;
19  
# Line 24 | Line 27 | public class ForkJoinTaskTest extends JS
27          return new TestSuite(ForkJoinTaskTest.class);
28      }
29  
30 +    // Runs with "mainPool" use > 1 thread. singletonPool tests use 1
31 +    static final int mainPoolSize =
32 +        Math.max(2, Runtime.getRuntime().availableProcessors());
33 +
34      private static ForkJoinPool mainPool() {
35 <        return new ForkJoinPool();
35 >        return new ForkJoinPool(mainPoolSize);
36      }
37  
38      private static ForkJoinPool singletonPool() {
# Line 45 | Line 52 | public class ForkJoinTaskTest extends JS
52              assertFalse(a.isCompletedAbnormally());
53              assertFalse(a.isCancelled());
54              assertNull(a.getException());
55 +            assertNull(a.getRawResult());
56  
57              assertNull(pool.invoke(a));
58  
# Line 53 | Line 61 | public class ForkJoinTaskTest extends JS
61              assertFalse(a.isCompletedAbnormally());
62              assertFalse(a.isCancelled());
63              assertNull(a.getException());
64 +            assertNull(a.getRawResult());
65          } finally {
66              joinPool(pool);
67          }
68      }
69  
70 +    void checkNotDone(ForkJoinTask a) {
71 +        assertFalse(a.isDone());
72 +        assertFalse(a.isCompletedNormally());
73 +        assertFalse(a.isCompletedAbnormally());
74 +        assertFalse(a.isCancelled());
75 +        assertNull(a.getException());
76 +        assertNull(a.getRawResult());
77 +
78 +        try {
79 +            a.get(0L, SECONDS);
80 +            shouldThrow();
81 +        } catch (TimeoutException success) {
82 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
83 +    }
84 +
85 +    <T> void checkCompletedNormally(ForkJoinTask<T> a) {
86 +        checkCompletedNormally(a, null);
87 +    }
88 +
89 +    <T> void checkCompletedNormally(ForkJoinTask<T> a, T expected) {
90 +        assertTrue(a.isDone());
91 +        assertFalse(a.isCancelled());
92 +        assertTrue(a.isCompletedNormally());
93 +        assertFalse(a.isCompletedAbnormally());
94 +        assertNull(a.getException());
95 +        assertSame(expected, a.getRawResult());
96 +        assertSame(expected, a.join());
97 +        assertFalse(a.cancel(false));
98 +        assertFalse(a.cancel(true));
99 +        try {
100 +            assertSame(expected, a.get());
101 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
102 +        try {
103 +            assertSame(expected, a.get(5L, SECONDS));
104 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
105 +    }
106 +
107 +    void checkCancelled(ForkJoinTask a) {
108 +        assertTrue(a.isDone());
109 +        assertTrue(a.isCancelled());
110 +        assertFalse(a.isCompletedNormally());
111 +        assertTrue(a.isCompletedAbnormally());
112 +        assertTrue(a.getException() instanceof CancellationException);
113 +        assertNull(a.getRawResult());
114 +        assertTrue(a.cancel(false));
115 +        assertTrue(a.cancel(true));
116 +
117 +        try {
118 +            a.join();
119 +            shouldThrow();
120 +        } catch (CancellationException success) {
121 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
122 +
123 +        try {
124 +            a.get();
125 +            shouldThrow();
126 +        } catch (CancellationException success) {
127 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
128 +
129 +        try {
130 +            a.get(5L, SECONDS);
131 +            shouldThrow();
132 +        } catch (CancellationException success) {
133 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
134 +    }
135 +
136 +    void checkCompletedAbnormally(ForkJoinTask a, Throwable t) {
137 +        assertTrue(a.isDone());
138 +        assertFalse(a.isCancelled());
139 +        assertFalse(a.isCompletedNormally());
140 +        assertTrue(a.isCompletedAbnormally());
141 +        assertSame(t, a.getException());
142 +        assertNull(a.getRawResult());
143 +        assertFalse(a.cancel(false));
144 +        assertFalse(a.cancel(true));
145 +
146 +        try {
147 +            a.join();
148 +            shouldThrow();
149 +        } catch (Throwable expected) {
150 +            assertSame(t, expected);
151 +        }
152 +
153 +        try {
154 +            a.get();
155 +            shouldThrow();
156 +        } catch (ExecutionException success) {
157 +            assertSame(t, success.getCause());
158 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
159 +
160 +        try {
161 +            a.get(5L, SECONDS);
162 +            shouldThrow();
163 +        } catch (ExecutionException success) {
164 +            assertSame(t, success.getCause());
165 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
166 +    }
167 +
168      /*
169       * Testing coverage notes:
170       *
# Line 70 | Line 177 | public class ForkJoinTaskTest extends JS
177          FJException() { super(); }
178      }
179  
180 <    static abstract class BinaryAsyncAction extends ForkJoinTask<Void> {
180 >    abstract static class BinaryAsyncAction extends ForkJoinTask<Void> {
181          private volatile int controlState;
182  
183          static final AtomicIntegerFieldUpdater<BinaryAsyncAction> controlStateUpdater =
# Line 250 | Line 357 | public class ForkJoinTaskTest extends JS
357                  AsyncFib f = new AsyncFib(8);
358                  assertNull(f.invoke());
359                  assertEquals(21, f.number);
360 <                assertTrue(f.isDone());
254 <                assertFalse(f.isCancelled());
255 <                assertFalse(f.isCompletedAbnormally());
256 <                assertNull(f.getRawResult());
360 >                checkCompletedNormally(f);
361              }};
362          testInvokeOnPool(mainPool(), a);
363      }
# Line 269 | Line 373 | public class ForkJoinTaskTest extends JS
373                  AsyncFib f = new AsyncFib(8);
374                  f.quietlyInvoke();
375                  assertEquals(21, f.number);
376 <                assertTrue(f.isDone());
273 <                assertFalse(f.isCancelled());
274 <                assertFalse(f.isCompletedAbnormally());
275 <                assertNull(f.getRawResult());
376 >                checkCompletedNormally(f);
377              }};
378          testInvokeOnPool(mainPool(), a);
379      }
# Line 287 | Line 388 | public class ForkJoinTaskTest extends JS
388                  assertSame(f, f.fork());
389                  assertNull(f.join());
390                  assertEquals(21, f.number);
391 <                assertTrue(f.isDone());
291 <                assertNull(f.getRawResult());
391 >                checkCompletedNormally(f);
392              }};
393          testInvokeOnPool(mainPool(), a);
394      }
# Line 303 | Line 403 | public class ForkJoinTaskTest extends JS
403                  assertSame(f, f.fork());
404                  assertNull(f.get());
405                  assertEquals(21, f.number);
406 <                assertTrue(f.isDone());
406 >                checkCompletedNormally(f);
407              }};
408          testInvokeOnPool(mainPool(), a);
409      }
# Line 316 | Line 416 | public class ForkJoinTaskTest extends JS
416              public void realCompute() throws Exception {
417                  AsyncFib f = new AsyncFib(8);
418                  assertSame(f, f.fork());
419 <                assertNull(f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
419 >                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
420                  assertEquals(21, f.number);
421 <                assertTrue(f.isDone());
421 >                checkCompletedNormally(f);
422              }};
423          testInvokeOnPool(mainPool(), a);
424      }
# Line 349 | Line 449 | public class ForkJoinTaskTest extends JS
449                  assertSame(f, f.fork());
450                  f.quietlyJoin();
451                  assertEquals(21, f.number);
452 <                assertTrue(f.isDone());
452 >                checkCompletedNormally(f);
453              }};
454          testInvokeOnPool(mainPool(), a);
455      }
# Line 366 | Line 466 | public class ForkJoinTaskTest extends JS
466                  assertSame(f, f.fork());
467                  f.helpQuiesce();
468                  assertEquals(21, f.number);
369                assertTrue(f.isDone());
469                  assertEquals(0, getQueuedTaskCount());
470 +                checkCompletedNormally(f);
471              }};
472          testInvokeOnPool(mainPool(), a);
473      }
# Line 383 | Line 483 | public class ForkJoinTaskTest extends JS
483                  try {
484                      f.invoke();
485                      shouldThrow();
486 <                } catch (FJException success) {}
486 >                } catch (FJException success) {
487 >                    checkCompletedAbnormally(f, success);
488 >                }
489              }};
490          testInvokeOnPool(mainPool(), a);
491      }
# Line 396 | Line 498 | public class ForkJoinTaskTest extends JS
498              public void realCompute() {
499                  FailingAsyncFib f = new FailingAsyncFib(8);
500                  f.quietlyInvoke();
501 <                assertTrue(f.isDone());
501 >                assertTrue(f.getException() instanceof FJException);
502 >                checkCompletedAbnormally(f, f.getException());
503              }};
504          testInvokeOnPool(mainPool(), a);
505      }
# Line 412 | Line 515 | public class ForkJoinTaskTest extends JS
515                  try {
516                      f.join();
517                      shouldThrow();
518 <                } catch (FJException success) {}
518 >                } catch (FJException success) {
519 >                    checkCompletedAbnormally(f, success);
520 >                }
521              }};
522          testInvokeOnPool(mainPool(), a);
523      }
# Line 428 | Line 533 | public class ForkJoinTaskTest extends JS
533                  try {
534                      f.get();
535                      shouldThrow();
536 <                } catch (ExecutionException success) {}
536 >                } catch (ExecutionException success) {
537 >                    Throwable cause = success.getCause();
538 >                    assertTrue(cause instanceof FJException);
539 >                    checkCompletedAbnormally(f, cause);
540 >                }
541              }};
542          testInvokeOnPool(mainPool(), a);
543      }
# Line 442 | Line 551 | public class ForkJoinTaskTest extends JS
551                  FailingAsyncFib f = new FailingAsyncFib(8);
552                  assertSame(f, f.fork());
553                  try {
554 <                    f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
554 >                    f.get(LONG_DELAY_MS, MILLISECONDS);
555                      shouldThrow();
556 <                } catch (ExecutionException success) {}
556 >                } catch (ExecutionException success) {
557 >                    Throwable cause = success.getCause();
558 >                    assertTrue(cause instanceof FJException);
559 >                    checkCompletedAbnormally(f, cause);
560 >                }
561              }};
562          testInvokeOnPool(mainPool(), a);
563      }
# Line 458 | Line 571 | public class ForkJoinTaskTest extends JS
571                  FailingAsyncFib f = new FailingAsyncFib(8);
572                  assertSame(f, f.fork());
573                  f.quietlyJoin();
461                assertTrue(f.isDone());
462                assertTrue(f.isCompletedAbnormally());
574                  assertTrue(f.getException() instanceof FJException);
575 +                checkCompletedAbnormally(f, f.getException());
576              }};
577          testInvokeOnPool(mainPool(), a);
578      }
# Line 476 | Line 588 | public class ForkJoinTaskTest extends JS
588                  try {
589                      f.invoke();
590                      shouldThrow();
591 <                } catch (CancellationException success) {}
591 >                } catch (CancellationException success) {
592 >                    checkCancelled(f);
593 >                }
594              }};
595          testInvokeOnPool(mainPool(), a);
596      }
# Line 493 | Line 607 | public class ForkJoinTaskTest extends JS
607                  try {
608                      f.join();
609                      shouldThrow();
610 <                } catch (CancellationException success) {}
610 >                } catch (CancellationException success) {
611 >                    checkCancelled(f);
612 >                }
613              }};
614          testInvokeOnPool(mainPool(), a);
615      }
# Line 510 | Line 626 | public class ForkJoinTaskTest extends JS
626                  try {
627                      f.get();
628                      shouldThrow();
629 <                } catch (CancellationException success) {}
629 >                } catch (CancellationException success) {
630 >                    checkCancelled(f);
631 >                }
632              }};
633          testInvokeOnPool(mainPool(), a);
634      }
# Line 525 | Line 643 | public class ForkJoinTaskTest extends JS
643                  assertTrue(f.cancel(true));
644                  assertSame(f, f.fork());
645                  try {
646 <                    f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
646 >                    f.get(LONG_DELAY_MS, MILLISECONDS);
647                      shouldThrow();
648 <                } catch (CancellationException success) {}
648 >                } catch (CancellationException success) {
649 >                    checkCancelled(f);
650 >                }
651              }};
652          testInvokeOnPool(mainPool(), a);
653      }
# Line 542 | Line 662 | public class ForkJoinTaskTest extends JS
662                  assertTrue(f.cancel(true));
663                  assertSame(f, f.fork());
664                  f.quietlyJoin();
665 <                assertTrue(f.isDone());
546 <                assertTrue(f.isCompletedAbnormally());
547 <                assertTrue(f.isCancelled());
548 <                assertTrue(f.getException() instanceof CancellationException);
665 >                checkCancelled(f);
666              }};
667          testInvokeOnPool(mainPool(), a);
668      }
# Line 590 | Line 707 | public class ForkJoinTaskTest extends JS
707      public void testInForkJoinPool2() {
708          RecursiveAction a = new CheckedRecursiveAction() {
709              public void realCompute() {
710 <                assertTrue(!inForkJoinPool());
710 >                assertFalse(inForkJoinPool());
711              }};
712          assertNull(a.invoke());
713      }
# Line 617 | Line 734 | public class ForkJoinTaskTest extends JS
734                  try {
735                      f.invoke();
736                      shouldThrow();
737 <                } catch (FJException success) {}
737 >                } catch (FJException success) {
738 >                    checkCompletedAbnormally(f, success);
739 >                }
740              }};
741          testInvokeOnPool(mainPool(), a);
742      }
# Line 631 | Line 750 | public class ForkJoinTaskTest extends JS
750                  AsyncFib f = new AsyncFib(8);
751                  AsyncFib g = new AsyncFib(9);
752                  invokeAll(f, g);
634                assertTrue(f.isDone());
753                  assertEquals(21, f.number);
636                assertTrue(g.isDone());
754                  assertEquals(34, g.number);
755 +                checkCompletedNormally(f);
756 +                checkCompletedNormally(g);
757              }};
758          testInvokeOnPool(mainPool(), a);
759      }
# Line 647 | Line 766 | public class ForkJoinTaskTest extends JS
766              public void realCompute() {
767                  AsyncFib f = new AsyncFib(8);
768                  invokeAll(f);
769 <                assertTrue(f.isDone());
769 >                checkCompletedNormally(f);
770                  assertEquals(21, f.number);
771              }};
772          testInvokeOnPool(mainPool(), a);
# Line 663 | Line 782 | public class ForkJoinTaskTest extends JS
782                  AsyncFib g = new AsyncFib(9);
783                  AsyncFib h = new AsyncFib(7);
784                  invokeAll(f, g, h);
666                assertTrue(f.isDone());
785                  assertEquals(21, f.number);
668                assertTrue(g.isDone());
786                  assertEquals(34, g.number);
670                assertTrue(h.isDone());
787                  assertEquals(13, h.number);
788 +                checkCompletedNormally(f);
789 +                checkCompletedNormally(g);
790 +                checkCompletedNormally(h);
791              }};
792          testInvokeOnPool(mainPool(), a);
793      }
# Line 687 | Line 806 | public class ForkJoinTaskTest extends JS
806                  set.add(g);
807                  set.add(h);
808                  invokeAll(set);
690                assertTrue(f.isDone());
809                  assertEquals(21, f.number);
692                assertTrue(g.isDone());
810                  assertEquals(34, g.number);
694                assertTrue(h.isDone());
811                  assertEquals(13, h.number);
812 +                checkCompletedNormally(f);
813 +                checkCompletedNormally(g);
814 +                checkCompletedNormally(h);
815              }};
816          testInvokeOnPool(mainPool(), a);
817      }
# Line 726 | Line 845 | public class ForkJoinTaskTest extends JS
845                  try {
846                      invokeAll(f, g);
847                      shouldThrow();
848 <                } catch (FJException success) {}
848 >                } catch (FJException success) {
849 >                    checkCompletedAbnormally(g, success);
850 >                }
851              }};
852          testInvokeOnPool(mainPool(), a);
853      }
# Line 741 | Line 862 | public class ForkJoinTaskTest extends JS
862                  try {
863                      invokeAll(g);
864                      shouldThrow();
865 <                } catch (FJException success) {}
865 >                } catch (FJException success) {
866 >                    checkCompletedAbnormally(g, success);
867 >                }
868              }};
869          testInvokeOnPool(mainPool(), a);
870      }
# Line 758 | Line 881 | public class ForkJoinTaskTest extends JS
881                  try {
882                      invokeAll(f, g, h);
883                      shouldThrow();
884 <                } catch (FJException success) {}
884 >                } catch (FJException success) {
885 >                    checkCompletedAbnormally(g, success);
886 >                }
887              }};
888          testInvokeOnPool(mainPool(), a);
889      }
# Line 779 | Line 904 | public class ForkJoinTaskTest extends JS
904                  try {
905                      invokeAll(set);
906                      shouldThrow();
907 <                } catch (FJException success) {}
907 >                } catch (FJException success) {
908 >                    checkCompletedAbnormally(f, success);
909 >                }
910              }};
911          testInvokeOnPool(mainPool(), a);
912      }
# Line 797 | Line 924 | public class ForkJoinTaskTest extends JS
924                  assertSame(f, f.fork());
925                  assertTrue(f.tryUnfork());
926                  helpQuiesce();
927 <                assertFalse(f.isDone());
928 <                assertTrue(g.isDone());
927 >                checkNotDone(f);
928 >                checkCompletedNormally(g);
929              }};
930          testInvokeOnPool(singletonPool(), a);
931      }
# Line 818 | Line 945 | public class ForkJoinTaskTest extends JS
945                  assertSame(f, f.fork());
946                  assertTrue(getSurplusQueuedTaskCount() > 0);
947                  helpQuiesce();
948 +                assertEquals(0, getSurplusQueuedTaskCount());
949 +                checkCompletedNormally(f);
950 +                checkCompletedNormally(g);
951 +                checkCompletedNormally(h);
952              }};
953          testInvokeOnPool(singletonPool(), a);
954      }
# Line 834 | Line 965 | public class ForkJoinTaskTest extends JS
965                  assertSame(f, f.fork());
966                  assertSame(f, peekNextLocalTask());
967                  assertNull(f.join());
968 <                assertTrue(f.isDone());
968 >                checkCompletedNormally(f);
969                  helpQuiesce();
970 +                checkCompletedNormally(g);
971              }};
972          testInvokeOnPool(singletonPool(), a);
973      }
974  
975      /**
976 <     * pollNextLocalTask returns most recent unexecuted task
977 <     * without executing it
976 >     * pollNextLocalTask returns most recent unexecuted task without
977 >     * executing it
978       */
979      public void testPollNextLocalTask() {
980          RecursiveAction a = new CheckedRecursiveAction() {
# Line 853 | Line 985 | public class ForkJoinTaskTest extends JS
985                  assertSame(f, f.fork());
986                  assertSame(f, pollNextLocalTask());
987                  helpQuiesce();
988 <                assertFalse(f.isDone());
988 >                checkNotDone(f);
989 >                assertEquals(34, g.number);
990 >                checkCompletedNormally(g);
991              }};
992          testInvokeOnPool(singletonPool(), a);
993      }
994  
995      /**
996 <     * pollTask returns an unexecuted task
863 <     * without executing it
996 >     * pollTask returns an unexecuted task without executing it
997       */
998      public void testPollTask() {
999          RecursiveAction a = new CheckedRecursiveAction() {
# Line 871 | Line 1004 | public class ForkJoinTaskTest extends JS
1004                  assertSame(f, f.fork());
1005                  assertSame(f, pollTask());
1006                  helpQuiesce();
1007 <                assertFalse(f.isDone());
1008 <                assertTrue(g.isDone());
1007 >                checkNotDone(f);
1008 >                checkCompletedNormally(g);
1009              }};
1010          testInvokeOnPool(singletonPool(), a);
1011      }
# Line 890 | Line 1023 | public class ForkJoinTaskTest extends JS
1023                  assertSame(g, peekNextLocalTask());
1024                  assertNull(f.join());
1025                  helpQuiesce();
1026 <                assertTrue(f.isDone());
1026 >                checkCompletedNormally(f);
1027 >                assertEquals(34, g.number);
1028 >                checkCompletedNormally(g);
1029              }};
1030          testInvokeOnPool(asyncSingletonPool(), a);
1031      }
1032  
1033      /**
1034 <     * pollNextLocalTask returns least recent unexecuted task
1035 <     * without executing it, in async mode
1034 >     * pollNextLocalTask returns least recent unexecuted task without
1035 >     * executing it, in async mode
1036       */
1037      public void testPollNextLocalTaskAsync() {
1038          RecursiveAction a = new CheckedRecursiveAction() {
# Line 908 | Line 1043 | public class ForkJoinTaskTest extends JS
1043                  assertSame(f, f.fork());
1044                  assertSame(g, pollNextLocalTask());
1045                  helpQuiesce();
1046 <                assertTrue(f.isDone());
1047 <                assertFalse(g.isDone());
1046 >                assertEquals(21, f.number);
1047 >                checkCompletedNormally(f);
1048 >                checkNotDone(g);
1049              }};
1050          testInvokeOnPool(asyncSingletonPool(), a);
1051      }
1052  
1053      /**
1054 <     * pollTask returns an unexecuted task
1055 <     * without executing it, in async mode
1054 >     * pollTask returns an unexecuted task without executing it, in
1055 >     * async mode
1056       */
1057      public void testPollTaskAsync() {
1058          RecursiveAction a = new CheckedRecursiveAction() {
# Line 927 | Line 1063 | public class ForkJoinTaskTest extends JS
1063                  assertSame(f, f.fork());
1064                  assertSame(g, pollTask());
1065                  helpQuiesce();
1066 <                assertTrue(f.isDone());
1067 <                assertFalse(g.isDone());
1066 >                assertEquals(21, f.number);
1067 >                checkCompletedNormally(f);
1068 >                checkNotDone(g);
1069              }};
1070          testInvokeOnPool(asyncSingletonPool(), a);
1071      }
1072 +
1073 +    // versions for singleton pools
1074 +
1075 +    /**
1076 +     * invoke returns when task completes normally.
1077 +     * isCompletedAbnormally and isCancelled return false for normally
1078 +     * completed tasks. getRawResult of a RecursiveAction returns null;
1079 +     */
1080 +    public void testInvokeSingleton() {
1081 +        RecursiveAction a = new CheckedRecursiveAction() {
1082 +            public void realCompute() {
1083 +                AsyncFib f = new AsyncFib(8);
1084 +                assertNull(f.invoke());
1085 +                assertEquals(21, f.number);
1086 +                checkCompletedNormally(f);
1087 +            }};
1088 +        testInvokeOnPool(singletonPool(), a);
1089 +    }
1090 +
1091 +    /**
1092 +     * quietlyInvoke task returns when task completes normally.
1093 +     * isCompletedAbnormally and isCancelled return false for normally
1094 +     * completed tasks
1095 +     */
1096 +    public void testQuietlyInvokeSingleton() {
1097 +        RecursiveAction a = new CheckedRecursiveAction() {
1098 +            public void realCompute() {
1099 +                AsyncFib f = new AsyncFib(8);
1100 +                f.quietlyInvoke();
1101 +                assertEquals(21, f.number);
1102 +                checkCompletedNormally(f);
1103 +            }};
1104 +        testInvokeOnPool(singletonPool(), a);
1105 +    }
1106 +
1107 +    /**
1108 +     * join of a forked task returns when task completes
1109 +     */
1110 +    public void testForkJoinSingleton() {
1111 +        RecursiveAction a = new CheckedRecursiveAction() {
1112 +            public void realCompute() {
1113 +                AsyncFib f = new AsyncFib(8);
1114 +                assertSame(f, f.fork());
1115 +                assertNull(f.join());
1116 +                assertEquals(21, f.number);
1117 +                checkCompletedNormally(f);
1118 +            }};
1119 +        testInvokeOnPool(singletonPool(), a);
1120 +    }
1121 +
1122 +    /**
1123 +     * get of a forked task returns when task completes
1124 +     */
1125 +    public void testForkGetSingleton() {
1126 +        RecursiveAction a = new CheckedRecursiveAction() {
1127 +            public void realCompute() throws Exception {
1128 +                AsyncFib f = new AsyncFib(8);
1129 +                assertSame(f, f.fork());
1130 +                assertNull(f.get());
1131 +                assertEquals(21, f.number);
1132 +                checkCompletedNormally(f);
1133 +            }};
1134 +        testInvokeOnPool(singletonPool(), a);
1135 +    }
1136 +
1137 +    /**
1138 +     * timed get of a forked task returns when task completes
1139 +     */
1140 +    public void testForkTimedGetSingleton() {
1141 +        RecursiveAction a = new CheckedRecursiveAction() {
1142 +            public void realCompute() throws Exception {
1143 +                AsyncFib f = new AsyncFib(8);
1144 +                assertSame(f, f.fork());
1145 +                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1146 +                assertEquals(21, f.number);
1147 +                checkCompletedNormally(f);
1148 +            }};
1149 +        testInvokeOnPool(singletonPool(), a);
1150 +    }
1151 +
1152 +    /**
1153 +     * timed get with null time unit throws NPE
1154 +     */
1155 +    public void testForkTimedGetNPESingleton() {
1156 +        RecursiveAction a = new CheckedRecursiveAction() {
1157 +            public void realCompute() throws Exception {
1158 +                AsyncFib f = new AsyncFib(8);
1159 +                assertSame(f, f.fork());
1160 +                try {
1161 +                    f.get(5L, null);
1162 +                    shouldThrow();
1163 +                } catch (NullPointerException success) {}
1164 +            }};
1165 +        testInvokeOnPool(singletonPool(), a);
1166 +    }
1167 +
1168 +    /**
1169 +     * quietlyJoin of a forked task returns when task completes
1170 +     */
1171 +    public void testForkQuietlyJoinSingleton() {
1172 +        RecursiveAction a = new CheckedRecursiveAction() {
1173 +            public void realCompute() {
1174 +                AsyncFib f = new AsyncFib(8);
1175 +                assertSame(f, f.fork());
1176 +                f.quietlyJoin();
1177 +                assertEquals(21, f.number);
1178 +                checkCompletedNormally(f);
1179 +            }};
1180 +        testInvokeOnPool(singletonPool(), a);
1181 +    }
1182 +
1183 +
1184 +    /**
1185 +     * helpQuiesce returns when tasks are complete.
1186 +     * getQueuedTaskCount returns 0 when quiescent
1187 +     */
1188 +    public void testForkHelpQuiesceSingleton() {
1189 +        RecursiveAction a = new CheckedRecursiveAction() {
1190 +            public void realCompute() {
1191 +                AsyncFib f = new AsyncFib(8);
1192 +                assertSame(f, f.fork());
1193 +                f.helpQuiesce();
1194 +                assertEquals(0, getQueuedTaskCount());
1195 +                assertEquals(21, f.number);
1196 +                checkCompletedNormally(f);
1197 +            }};
1198 +        testInvokeOnPool(singletonPool(), a);
1199 +    }
1200 +
1201 +
1202 +    /**
1203 +     * invoke task throws exception when task completes abnormally
1204 +     */
1205 +    public void testAbnormalInvokeSingleton() {
1206 +        RecursiveAction a = new CheckedRecursiveAction() {
1207 +            public void realCompute() {
1208 +                FailingAsyncFib f = new FailingAsyncFib(8);
1209 +                try {
1210 +                    f.invoke();
1211 +                    shouldThrow();
1212 +                } catch (FJException success) {
1213 +                    checkCompletedAbnormally(f, success);
1214 +                }
1215 +            }};
1216 +        testInvokeOnPool(singletonPool(), a);
1217 +    }
1218 +
1219 +    /**
1220 +     * quietlyInvoke task returns when task completes abnormally
1221 +     */
1222 +    public void testAbnormalQuietlyInvokeSingleton() {
1223 +        RecursiveAction a = new CheckedRecursiveAction() {
1224 +            public void realCompute() {
1225 +                FailingAsyncFib f = new FailingAsyncFib(8);
1226 +                f.quietlyInvoke();
1227 +                assertTrue(f.getException() instanceof FJException);
1228 +                checkCompletedAbnormally(f, f.getException());
1229 +            }};
1230 +        testInvokeOnPool(singletonPool(), a);
1231 +    }
1232 +
1233 +    /**
1234 +     * join of a forked task throws exception when task completes abnormally
1235 +     */
1236 +    public void testAbnormalForkJoinSingleton() {
1237 +        RecursiveAction a = new CheckedRecursiveAction() {
1238 +            public void realCompute() {
1239 +                FailingAsyncFib f = new FailingAsyncFib(8);
1240 +                assertSame(f, f.fork());
1241 +                try {
1242 +                    f.join();
1243 +                    shouldThrow();
1244 +                } catch (FJException success) {
1245 +                    checkCompletedAbnormally(f, success);
1246 +                }
1247 +            }};
1248 +        testInvokeOnPool(singletonPool(), a);
1249 +    }
1250 +
1251 +    /**
1252 +     * get of a forked task throws exception when task completes abnormally
1253 +     */
1254 +    public void testAbnormalForkGetSingleton() {
1255 +        RecursiveAction a = new CheckedRecursiveAction() {
1256 +            public void realCompute() throws Exception {
1257 +                FailingAsyncFib f = new FailingAsyncFib(8);
1258 +                assertSame(f, f.fork());
1259 +                try {
1260 +                    f.get();
1261 +                    shouldThrow();
1262 +                } catch (ExecutionException success) {
1263 +                    Throwable cause = success.getCause();
1264 +                    assertTrue(cause instanceof FJException);
1265 +                    checkCompletedAbnormally(f, cause);
1266 +                }
1267 +            }};
1268 +        testInvokeOnPool(singletonPool(), a);
1269 +    }
1270 +
1271 +    /**
1272 +     * timed get of a forked task throws exception when task completes abnormally
1273 +     */
1274 +    public void testAbnormalForkTimedGetSingleton() {
1275 +        RecursiveAction a = new CheckedRecursiveAction() {
1276 +            public void realCompute() throws Exception {
1277 +                FailingAsyncFib f = new FailingAsyncFib(8);
1278 +                assertSame(f, f.fork());
1279 +                try {
1280 +                    f.get(LONG_DELAY_MS, MILLISECONDS);
1281 +                    shouldThrow();
1282 +                } catch (ExecutionException success) {
1283 +                    Throwable cause = success.getCause();
1284 +                    assertTrue(cause instanceof FJException);
1285 +                    checkCompletedAbnormally(f, cause);
1286 +                }
1287 +            }};
1288 +        testInvokeOnPool(singletonPool(), a);
1289 +    }
1290 +
1291 +    /**
1292 +     * quietlyJoin of a forked task returns when task completes abnormally
1293 +     */
1294 +    public void testAbnormalForkQuietlyJoinSingleton() {
1295 +        RecursiveAction a = new CheckedRecursiveAction() {
1296 +            public void realCompute() {
1297 +                FailingAsyncFib f = new FailingAsyncFib(8);
1298 +                assertSame(f, f.fork());
1299 +                f.quietlyJoin();
1300 +                assertTrue(f.getException() instanceof FJException);
1301 +                checkCompletedAbnormally(f, f.getException());
1302 +            }};
1303 +        testInvokeOnPool(singletonPool(), a);
1304 +    }
1305 +
1306 +    /**
1307 +     * invoke task throws exception when task cancelled
1308 +     */
1309 +    public void testCancelledInvokeSingleton() {
1310 +        RecursiveAction a = new CheckedRecursiveAction() {
1311 +            public void realCompute() {
1312 +                AsyncFib f = new AsyncFib(8);
1313 +                assertTrue(f.cancel(true));
1314 +                try {
1315 +                    f.invoke();
1316 +                    shouldThrow();
1317 +                } catch (CancellationException success) {
1318 +                    checkCancelled(f);
1319 +                }
1320 +            }};
1321 +        testInvokeOnPool(singletonPool(), a);
1322 +    }
1323 +
1324 +    /**
1325 +     * join of a forked task throws exception when task cancelled
1326 +     */
1327 +    public void testCancelledForkJoinSingleton() {
1328 +        RecursiveAction a = new CheckedRecursiveAction() {
1329 +            public void realCompute() {
1330 +                AsyncFib f = new AsyncFib(8);
1331 +                assertTrue(f.cancel(true));
1332 +                assertSame(f, f.fork());
1333 +                try {
1334 +                    f.join();
1335 +                    shouldThrow();
1336 +                } catch (CancellationException success) {
1337 +                    checkCancelled(f);
1338 +                }
1339 +            }};
1340 +        testInvokeOnPool(singletonPool(), a);
1341 +    }
1342 +
1343 +    /**
1344 +     * get of a forked task throws exception when task cancelled
1345 +     */
1346 +    public void testCancelledForkGetSingleton() {
1347 +        RecursiveAction a = new CheckedRecursiveAction() {
1348 +            public void realCompute() throws Exception {
1349 +                AsyncFib f = new AsyncFib(8);
1350 +                assertTrue(f.cancel(true));
1351 +                assertSame(f, f.fork());
1352 +                try {
1353 +                    f.get();
1354 +                    shouldThrow();
1355 +                } catch (CancellationException success) {
1356 +                    checkCancelled(f);
1357 +                }
1358 +            }};
1359 +        testInvokeOnPool(singletonPool(), a);
1360 +    }
1361 +
1362 +    /**
1363 +     * timed get of a forked task throws exception when task cancelled
1364 +     */
1365 +    public void testCancelledForkTimedGetSingleton() throws Exception {
1366 +        RecursiveAction a = new CheckedRecursiveAction() {
1367 +            public void realCompute() throws Exception {
1368 +                AsyncFib f = new AsyncFib(8);
1369 +                assertTrue(f.cancel(true));
1370 +                assertSame(f, f.fork());
1371 +                try {
1372 +                    f.get(LONG_DELAY_MS, MILLISECONDS);
1373 +                    shouldThrow();
1374 +                } catch (CancellationException success) {
1375 +                    checkCancelled(f);
1376 +                }
1377 +            }};
1378 +        testInvokeOnPool(singletonPool(), a);
1379 +    }
1380 +
1381 +    /**
1382 +     * quietlyJoin of a forked task returns when task cancelled
1383 +     */
1384 +    public void testCancelledForkQuietlyJoinSingleton() {
1385 +        RecursiveAction a = new CheckedRecursiveAction() {
1386 +            public void realCompute() {
1387 +                AsyncFib f = new AsyncFib(8);
1388 +                assertTrue(f.cancel(true));
1389 +                assertSame(f, f.fork());
1390 +                f.quietlyJoin();
1391 +                checkCancelled(f);
1392 +            }};
1393 +        testInvokeOnPool(singletonPool(), a);
1394 +    }
1395 +
1396 +    /**
1397 +     * invoke task throws exception after invoking completeExceptionally
1398 +     */
1399 +    public void testCompleteExceptionallySingleton() {
1400 +        RecursiveAction a = new CheckedRecursiveAction() {
1401 +            public void realCompute() {
1402 +                AsyncFib f = new AsyncFib(8);
1403 +                f.completeExceptionally(new FJException());
1404 +                try {
1405 +                    f.invoke();
1406 +                    shouldThrow();
1407 +                } catch (FJException success) {
1408 +                    checkCompletedAbnormally(f, success);
1409 +                }
1410 +            }};
1411 +        testInvokeOnPool(singletonPool(), a);
1412 +    }
1413 +
1414 +    /**
1415 +     * invokeAll(t1, t2) invokes all task arguments
1416 +     */
1417 +    public void testInvokeAll2Singleton() {
1418 +        RecursiveAction a = new CheckedRecursiveAction() {
1419 +            public void realCompute() {
1420 +                AsyncFib f = new AsyncFib(8);
1421 +                AsyncFib g = new AsyncFib(9);
1422 +                invokeAll(f, g);
1423 +                assertEquals(21, f.number);
1424 +                assertEquals(34, g.number);
1425 +                checkCompletedNormally(f);
1426 +                checkCompletedNormally(g);
1427 +            }};
1428 +        testInvokeOnPool(singletonPool(), a);
1429 +    }
1430 +
1431 +    /**
1432 +     * invokeAll(tasks) with 1 argument invokes task
1433 +     */
1434 +    public void testInvokeAll1Singleton() {
1435 +        RecursiveAction a = new CheckedRecursiveAction() {
1436 +            public void realCompute() {
1437 +                AsyncFib f = new AsyncFib(8);
1438 +                invokeAll(f);
1439 +                checkCompletedNormally(f);
1440 +                assertEquals(21, f.number);
1441 +            }};
1442 +        testInvokeOnPool(singletonPool(), a);
1443 +    }
1444 +
1445 +    /**
1446 +     * invokeAll(tasks) with > 2 argument invokes tasks
1447 +     */
1448 +    public void testInvokeAll3Singleton() {
1449 +        RecursiveAction a = new CheckedRecursiveAction() {
1450 +            public void realCompute() {
1451 +                AsyncFib f = new AsyncFib(8);
1452 +                AsyncFib g = new AsyncFib(9);
1453 +                AsyncFib h = new AsyncFib(7);
1454 +                invokeAll(f, g, h);
1455 +                assertEquals(21, f.number);
1456 +                assertEquals(34, g.number);
1457 +                assertEquals(13, h.number);
1458 +                checkCompletedNormally(f);
1459 +                checkCompletedNormally(g);
1460 +                checkCompletedNormally(h);
1461 +            }};
1462 +        testInvokeOnPool(singletonPool(), a);
1463 +    }
1464 +
1465 +    /**
1466 +     * invokeAll(collection) invokes all tasks in the collection
1467 +     */
1468 +    public void testInvokeAllCollectionSingleton() {
1469 +        RecursiveAction a = new CheckedRecursiveAction() {
1470 +            public void realCompute() {
1471 +                AsyncFib f = new AsyncFib(8);
1472 +                AsyncFib g = new AsyncFib(9);
1473 +                AsyncFib h = new AsyncFib(7);
1474 +                HashSet set = new HashSet();
1475 +                set.add(f);
1476 +                set.add(g);
1477 +                set.add(h);
1478 +                invokeAll(set);
1479 +                assertEquals(21, f.number);
1480 +                assertEquals(34, g.number);
1481 +                assertEquals(13, h.number);
1482 +                checkCompletedNormally(f);
1483 +                checkCompletedNormally(g);
1484 +                checkCompletedNormally(h);
1485 +            }};
1486 +        testInvokeOnPool(singletonPool(), a);
1487 +    }
1488 +
1489 +
1490 +    /**
1491 +     * invokeAll(tasks) with any null task throws NPE
1492 +     */
1493 +    public void testInvokeAllNPESingleton() {
1494 +        RecursiveAction a = new CheckedRecursiveAction() {
1495 +            public void realCompute() {
1496 +                AsyncFib f = new AsyncFib(8);
1497 +                AsyncFib g = new AsyncFib(9);
1498 +                AsyncFib h = null;
1499 +                try {
1500 +                    invokeAll(f, g, h);
1501 +                    shouldThrow();
1502 +                } catch (NullPointerException success) {}
1503 +            }};
1504 +        testInvokeOnPool(singletonPool(), a);
1505 +    }
1506 +
1507 +    /**
1508 +     * invokeAll(t1, t2) throw exception if any task does
1509 +     */
1510 +    public void testAbnormalInvokeAll2Singleton() {
1511 +        RecursiveAction a = new CheckedRecursiveAction() {
1512 +            public void realCompute() {
1513 +                AsyncFib f = new AsyncFib(8);
1514 +                FailingAsyncFib g = new FailingAsyncFib(9);
1515 +                try {
1516 +                    invokeAll(f, g);
1517 +                    shouldThrow();
1518 +                } catch (FJException success) {
1519 +                    checkCompletedAbnormally(g, success);
1520 +                }
1521 +            }};
1522 +        testInvokeOnPool(singletonPool(), a);
1523 +    }
1524 +
1525 +    /**
1526 +     * invokeAll(tasks) with 1 argument throws exception if task does
1527 +     */
1528 +    public void testAbnormalInvokeAll1Singleton() {
1529 +        RecursiveAction a = new CheckedRecursiveAction() {
1530 +            public void realCompute() {
1531 +                FailingAsyncFib g = new FailingAsyncFib(9);
1532 +                try {
1533 +                    invokeAll(g);
1534 +                    shouldThrow();
1535 +                } catch (FJException success) {
1536 +                    checkCompletedAbnormally(g, success);
1537 +                }
1538 +            }};
1539 +        testInvokeOnPool(singletonPool(), a);
1540 +    }
1541 +
1542 +    /**
1543 +     * invokeAll(tasks) with > 2 argument throws exception if any task does
1544 +     */
1545 +    public void testAbnormalInvokeAll3Singleton() {
1546 +        RecursiveAction a = new CheckedRecursiveAction() {
1547 +            public void realCompute() {
1548 +                AsyncFib f = new AsyncFib(8);
1549 +                FailingAsyncFib g = new FailingAsyncFib(9);
1550 +                AsyncFib h = new AsyncFib(7);
1551 +                try {
1552 +                    invokeAll(f, g, h);
1553 +                    shouldThrow();
1554 +                } catch (FJException success) {
1555 +                    checkCompletedAbnormally(g, success);
1556 +                }
1557 +            }};
1558 +        testInvokeOnPool(singletonPool(), a);
1559 +    }
1560 +
1561 +    /**
1562 +     * invokeAll(collection)  throws exception if any task does
1563 +     */
1564 +    public void testAbnormalInvokeAllCollectionSingleton() {
1565 +        RecursiveAction a = new CheckedRecursiveAction() {
1566 +            public void realCompute() {
1567 +                FailingAsyncFib f = new FailingAsyncFib(8);
1568 +                AsyncFib g = new AsyncFib(9);
1569 +                AsyncFib h = new AsyncFib(7);
1570 +                HashSet set = new HashSet();
1571 +                set.add(f);
1572 +                set.add(g);
1573 +                set.add(h);
1574 +                try {
1575 +                    invokeAll(set);
1576 +                    shouldThrow();
1577 +                } catch (FJException success) {
1578 +                    checkCompletedAbnormally(f, success);
1579 +                }
1580 +            }};
1581 +        testInvokeOnPool(singletonPool(), a);
1582 +    }
1583 +
1584   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines