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.18 by jsr166, Fri Sep 17 00:55:19 2010 UTC vs.
Revision 1.27 by jsr166, Mon Nov 22 07:50:50 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 25 | 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 46 | 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 54 | 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 71 | 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 243 | Line 349 | public class ForkJoinTaskTest extends JS
349      /**
350       * invoke returns when task completes normally.
351       * isCompletedAbnormally and isCancelled return false for normally
352 <     * completed tasks. getRawResult of a RecursiveAction returns null;
352 >     * completed tasks; getRawResult returns null.
353       */
354      public void testInvoke() {
355          RecursiveAction a = new CheckedRecursiveAction() {
# Line 251 | 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());
255 <                assertFalse(f.isCancelled());
256 <                assertFalse(f.isCompletedAbnormally());
257 <                assertNull(f.getRawResult());
360 >                checkCompletedNormally(f);
361              }};
362          testInvokeOnPool(mainPool(), a);
363      }
# Line 270 | 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());
274 <                assertFalse(f.isCancelled());
275 <                assertFalse(f.isCompletedAbnormally());
276 <                assertNull(f.getRawResult());
376 >                checkCompletedNormally(f);
377              }};
378          testInvokeOnPool(mainPool(), a);
379      }
# Line 288 | 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());
292 <                assertNull(f.getRawResult());
391 >                checkCompletedNormally(f);
392              }};
393          testInvokeOnPool(mainPool(), a);
394      }
# Line 304 | 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 319 | Line 418 | public class ForkJoinTaskTest extends JS
418                  assertSame(f, f.fork());
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 350 | 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 367 | Line 466 | public class ForkJoinTaskTest extends JS
466                  assertSame(f, f.fork());
467                  f.helpQuiesce();
468                  assertEquals(21, f.number);
370                assertTrue(f.isDone());
469                  assertEquals(0, getQueuedTaskCount());
470 +                checkCompletedNormally(f);
471              }};
472          testInvokeOnPool(mainPool(), a);
473      }
# Line 384 | 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 397 | 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 413 | 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 432 | Line 536 | public class ForkJoinTaskTest extends JS
536                  } catch (ExecutionException success) {
537                      Throwable cause = success.getCause();
538                      assertTrue(cause instanceof FJException);
539 <                    assertTrue(f.isDone());
436 <                    assertTrue(f.isCompletedAbnormally());
437 <                    assertSame(cause, f.getException());
539 >                    checkCompletedAbnormally(f, cause);
540                  }
541              }};
542          testInvokeOnPool(mainPool(), a);
# Line 454 | Line 556 | public class ForkJoinTaskTest extends JS
556                  } catch (ExecutionException success) {
557                      Throwable cause = success.getCause();
558                      assertTrue(cause instanceof FJException);
559 <                    assertTrue(f.isDone());
458 <                    assertTrue(f.isCompletedAbnormally());
459 <                    assertSame(cause, f.getException());
559 >                    checkCompletedAbnormally(f, cause);
560                  }
561              }};
562          testInvokeOnPool(mainPool(), a);
# Line 471 | Line 571 | public class ForkJoinTaskTest extends JS
571                  FailingAsyncFib f = new FailingAsyncFib(8);
572                  assertSame(f, f.fork());
573                  f.quietlyJoin();
474                assertTrue(f.isDone());
475                assertTrue(f.isCompletedAbnormally());
574                  assertTrue(f.getException() instanceof FJException);
575 +                checkCompletedAbnormally(f, f.getException());
576              }};
577          testInvokeOnPool(mainPool(), a);
578      }
# Line 490 | Line 589 | public class ForkJoinTaskTest extends JS
589                      f.invoke();
590                      shouldThrow();
591                  } catch (CancellationException success) {
592 <                    assertTrue(f.isDone());
494 <                    assertTrue(f.isCancelled());
495 <                    assertTrue(f.isCompletedAbnormally());
496 <                    assertTrue(f.getException() instanceof CancellationException);
592 >                    checkCancelled(f);
593                  }
594              }};
595          testInvokeOnPool(mainPool(), a);
# Line 512 | Line 608 | public class ForkJoinTaskTest extends JS
608                      f.join();
609                      shouldThrow();
610                  } catch (CancellationException success) {
611 <                    assertTrue(f.isDone());
516 <                    assertTrue(f.isCancelled());
517 <                    assertTrue(f.isCompletedAbnormally());
518 <                    assertTrue(f.getException() instanceof CancellationException);
611 >                    checkCancelled(f);
612                  }
613              }};
614          testInvokeOnPool(mainPool(), a);
# Line 534 | Line 627 | public class ForkJoinTaskTest extends JS
627                      f.get();
628                      shouldThrow();
629                  } catch (CancellationException success) {
630 <                    assertTrue(f.isDone());
538 <                    assertTrue(f.isCancelled());
539 <                    assertTrue(f.isCompletedAbnormally());
540 <                    assertTrue(f.getException() instanceof CancellationException);
630 >                    checkCancelled(f);
631                  }
632              }};
633          testInvokeOnPool(mainPool(), a);
# Line 556 | Line 646 | public class ForkJoinTaskTest extends JS
646                      f.get(LONG_DELAY_MS, MILLISECONDS);
647                      shouldThrow();
648                  } catch (CancellationException success) {
649 <                    assertTrue(f.isDone());
560 <                    assertTrue(f.isCancelled());
561 <                    assertTrue(f.isCompletedAbnormally());
562 <                    assertTrue(f.getException() instanceof CancellationException);
649 >                    checkCancelled(f);
650                  }
651              }};
652          testInvokeOnPool(mainPool(), a);
# Line 575 | Line 662 | public class ForkJoinTaskTest extends JS
662                  assertTrue(f.cancel(true));
663                  assertSame(f, f.fork());
664                  f.quietlyJoin();
665 <                assertTrue(f.isDone());
579 <                assertTrue(f.isCompletedAbnormally());
580 <                assertTrue(f.isCancelled());
581 <                assertTrue(f.getException() instanceof CancellationException);
665 >                checkCancelled(f);
666              }};
667          testInvokeOnPool(mainPool(), a);
668      }
# Line 623 | 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 635 | Line 719 | public class ForkJoinTaskTest extends JS
719          RecursiveAction a = new CheckedRecursiveAction() {
720              public void realCompute() {
721                  setRawResult(null);
722 +                assertNull(getRawResult());
723              }};
724          assertNull(a.invoke());
725      }
# Line 650 | Line 735 | public class ForkJoinTaskTest extends JS
735                  try {
736                      f.invoke();
737                      shouldThrow();
738 <                } catch (FJException success) {}
738 >                } catch (FJException success) {
739 >                    checkCompletedAbnormally(f, success);
740 >                }
741              }};
742          testInvokeOnPool(mainPool(), a);
743      }
# Line 664 | Line 751 | public class ForkJoinTaskTest extends JS
751                  AsyncFib f = new AsyncFib(8);
752                  AsyncFib g = new AsyncFib(9);
753                  invokeAll(f, g);
667                assertTrue(f.isDone());
754                  assertEquals(21, f.number);
669                assertTrue(g.isDone());
755                  assertEquals(34, g.number);
756 +                checkCompletedNormally(f);
757 +                checkCompletedNormally(g);
758              }};
759          testInvokeOnPool(mainPool(), a);
760      }
# Line 680 | Line 767 | public class ForkJoinTaskTest extends JS
767              public void realCompute() {
768                  AsyncFib f = new AsyncFib(8);
769                  invokeAll(f);
770 <                assertTrue(f.isDone());
770 >                checkCompletedNormally(f);
771                  assertEquals(21, f.number);
772              }};
773          testInvokeOnPool(mainPool(), a);
# Line 696 | Line 783 | public class ForkJoinTaskTest extends JS
783                  AsyncFib g = new AsyncFib(9);
784                  AsyncFib h = new AsyncFib(7);
785                  invokeAll(f, g, h);
699                assertTrue(f.isDone());
786                  assertEquals(21, f.number);
701                assertTrue(g.isDone());
787                  assertEquals(34, g.number);
703                assertTrue(h.isDone());
788                  assertEquals(13, h.number);
789 +                checkCompletedNormally(f);
790 +                checkCompletedNormally(g);
791 +                checkCompletedNormally(h);
792              }};
793          testInvokeOnPool(mainPool(), a);
794      }
# Line 720 | Line 807 | public class ForkJoinTaskTest extends JS
807                  set.add(g);
808                  set.add(h);
809                  invokeAll(set);
723                assertTrue(f.isDone());
810                  assertEquals(21, f.number);
725                assertTrue(g.isDone());
811                  assertEquals(34, g.number);
727                assertTrue(h.isDone());
812                  assertEquals(13, h.number);
813 +                checkCompletedNormally(f);
814 +                checkCompletedNormally(g);
815 +                checkCompletedNormally(h);
816              }};
817          testInvokeOnPool(mainPool(), a);
818      }
# Line 759 | Line 846 | public class ForkJoinTaskTest extends JS
846                  try {
847                      invokeAll(f, g);
848                      shouldThrow();
849 <                } catch (FJException success) {}
849 >                } catch (FJException success) {
850 >                    checkCompletedAbnormally(g, success);
851 >                }
852              }};
853          testInvokeOnPool(mainPool(), a);
854      }
# Line 774 | Line 863 | public class ForkJoinTaskTest extends JS
863                  try {
864                      invokeAll(g);
865                      shouldThrow();
866 <                } catch (FJException success) {}
866 >                } catch (FJException success) {
867 >                    checkCompletedAbnormally(g, success);
868 >                }
869              }};
870          testInvokeOnPool(mainPool(), a);
871      }
# Line 791 | Line 882 | public class ForkJoinTaskTest extends JS
882                  try {
883                      invokeAll(f, g, h);
884                      shouldThrow();
885 <                } catch (FJException success) {}
885 >                } catch (FJException success) {
886 >                    checkCompletedAbnormally(g, success);
887 >                }
888              }};
889          testInvokeOnPool(mainPool(), a);
890      }
# Line 812 | Line 905 | public class ForkJoinTaskTest extends JS
905                  try {
906                      invokeAll(set);
907                      shouldThrow();
908 <                } catch (FJException success) {}
908 >                } catch (FJException success) {
909 >                    checkCompletedAbnormally(f, success);
910 >                }
911              }};
912          testInvokeOnPool(mainPool(), a);
913      }
# Line 830 | Line 925 | public class ForkJoinTaskTest extends JS
925                  assertSame(f, f.fork());
926                  assertTrue(f.tryUnfork());
927                  helpQuiesce();
928 <                assertFalse(f.isDone());
929 <                assertTrue(g.isDone());
928 >                checkNotDone(f);
929 >                checkCompletedNormally(g);
930              }};
931          testInvokeOnPool(singletonPool(), a);
932      }
# Line 851 | Line 946 | public class ForkJoinTaskTest extends JS
946                  assertSame(f, f.fork());
947                  assertTrue(getSurplusQueuedTaskCount() > 0);
948                  helpQuiesce();
949 +                assertEquals(0, getSurplusQueuedTaskCount());
950 +                checkCompletedNormally(f);
951 +                checkCompletedNormally(g);
952 +                checkCompletedNormally(h);
953              }};
954          testInvokeOnPool(singletonPool(), a);
955      }
# Line 867 | Line 966 | public class ForkJoinTaskTest extends JS
966                  assertSame(f, f.fork());
967                  assertSame(f, peekNextLocalTask());
968                  assertNull(f.join());
969 <                assertTrue(f.isDone());
969 >                checkCompletedNormally(f);
970                  helpQuiesce();
971 +                checkCompletedNormally(g);
972              }};
973          testInvokeOnPool(singletonPool(), a);
974      }
975  
976      /**
977 <     * pollNextLocalTask returns most recent unexecuted task
978 <     * without executing it
977 >     * pollNextLocalTask returns most recent unexecuted task without
978 >     * executing it
979       */
980      public void testPollNextLocalTask() {
981          RecursiveAction a = new CheckedRecursiveAction() {
# Line 886 | Line 986 | public class ForkJoinTaskTest extends JS
986                  assertSame(f, f.fork());
987                  assertSame(f, pollNextLocalTask());
988                  helpQuiesce();
989 <                assertFalse(f.isDone());
989 >                checkNotDone(f);
990 >                assertEquals(34, g.number);
991 >                checkCompletedNormally(g);
992              }};
993          testInvokeOnPool(singletonPool(), a);
994      }
995  
996      /**
997 <     * pollTask returns an unexecuted task
896 <     * without executing it
997 >     * pollTask returns an unexecuted task without executing it
998       */
999      public void testPollTask() {
1000          RecursiveAction a = new CheckedRecursiveAction() {
# Line 904 | Line 1005 | public class ForkJoinTaskTest extends JS
1005                  assertSame(f, f.fork());
1006                  assertSame(f, pollTask());
1007                  helpQuiesce();
1008 <                assertFalse(f.isDone());
1009 <                assertTrue(g.isDone());
1008 >                checkNotDone(f);
1009 >                checkCompletedNormally(g);
1010              }};
1011          testInvokeOnPool(singletonPool(), a);
1012      }
# Line 923 | Line 1024 | public class ForkJoinTaskTest extends JS
1024                  assertSame(g, peekNextLocalTask());
1025                  assertNull(f.join());
1026                  helpQuiesce();
1027 <                assertTrue(f.isDone());
1027 >                checkCompletedNormally(f);
1028 >                assertEquals(34, g.number);
1029 >                checkCompletedNormally(g);
1030              }};
1031          testInvokeOnPool(asyncSingletonPool(), a);
1032      }
1033  
1034      /**
1035 <     * pollNextLocalTask returns least recent unexecuted task
1036 <     * without executing it, in async mode
1035 >     * pollNextLocalTask returns least recent unexecuted task without
1036 >     * executing it, in async mode
1037       */
1038      public void testPollNextLocalTaskAsync() {
1039          RecursiveAction a = new CheckedRecursiveAction() {
# Line 941 | Line 1044 | public class ForkJoinTaskTest extends JS
1044                  assertSame(f, f.fork());
1045                  assertSame(g, pollNextLocalTask());
1046                  helpQuiesce();
1047 <                assertTrue(f.isDone());
1048 <                assertFalse(g.isDone());
1047 >                assertEquals(21, f.number);
1048 >                checkCompletedNormally(f);
1049 >                checkNotDone(g);
1050              }};
1051          testInvokeOnPool(asyncSingletonPool(), a);
1052      }
1053  
1054      /**
1055 <     * pollTask returns an unexecuted task
1056 <     * without executing it, in async mode
1055 >     * pollTask returns an unexecuted task without executing it, in
1056 >     * async mode
1057       */
1058      public void testPollTaskAsync() {
1059          RecursiveAction a = new CheckedRecursiveAction() {
# Line 960 | Line 1064 | public class ForkJoinTaskTest extends JS
1064                  assertSame(f, f.fork());
1065                  assertSame(g, pollTask());
1066                  helpQuiesce();
1067 <                assertTrue(f.isDone());
1068 <                assertFalse(g.isDone());
1067 >                assertEquals(21, f.number);
1068 >                checkCompletedNormally(f);
1069 >                checkNotDone(g);
1070              }};
1071          testInvokeOnPool(asyncSingletonPool(), a);
1072      }
1073 +
1074 +    // versions for singleton pools
1075 +
1076 +    /**
1077 +     * invoke returns when task completes normally.
1078 +     * isCompletedAbnormally and isCancelled return false for normally
1079 +     * completed tasks; getRawResult returns null.
1080 +     */
1081 +    public void testInvokeSingleton() {
1082 +        RecursiveAction a = new CheckedRecursiveAction() {
1083 +            public void realCompute() {
1084 +                AsyncFib f = new AsyncFib(8);
1085 +                assertNull(f.invoke());
1086 +                assertEquals(21, f.number);
1087 +                checkCompletedNormally(f);
1088 +            }};
1089 +        testInvokeOnPool(singletonPool(), a);
1090 +    }
1091 +
1092 +    /**
1093 +     * quietlyInvoke task returns when task completes normally.
1094 +     * isCompletedAbnormally and isCancelled return false for normally
1095 +     * completed tasks
1096 +     */
1097 +    public void testQuietlyInvokeSingleton() {
1098 +        RecursiveAction a = new CheckedRecursiveAction() {
1099 +            public void realCompute() {
1100 +                AsyncFib f = new AsyncFib(8);
1101 +                f.quietlyInvoke();
1102 +                assertEquals(21, f.number);
1103 +                checkCompletedNormally(f);
1104 +            }};
1105 +        testInvokeOnPool(singletonPool(), a);
1106 +    }
1107 +
1108 +    /**
1109 +     * join of a forked task returns when task completes
1110 +     */
1111 +    public void testForkJoinSingleton() {
1112 +        RecursiveAction a = new CheckedRecursiveAction() {
1113 +            public void realCompute() {
1114 +                AsyncFib f = new AsyncFib(8);
1115 +                assertSame(f, f.fork());
1116 +                assertNull(f.join());
1117 +                assertEquals(21, f.number);
1118 +                checkCompletedNormally(f);
1119 +            }};
1120 +        testInvokeOnPool(singletonPool(), a);
1121 +    }
1122 +
1123 +    /**
1124 +     * get of a forked task returns when task completes
1125 +     */
1126 +    public void testForkGetSingleton() {
1127 +        RecursiveAction a = new CheckedRecursiveAction() {
1128 +            public void realCompute() throws Exception {
1129 +                AsyncFib f = new AsyncFib(8);
1130 +                assertSame(f, f.fork());
1131 +                assertNull(f.get());
1132 +                assertEquals(21, f.number);
1133 +                checkCompletedNormally(f);
1134 +            }};
1135 +        testInvokeOnPool(singletonPool(), a);
1136 +    }
1137 +
1138 +    /**
1139 +     * timed get of a forked task returns when task completes
1140 +     */
1141 +    public void testForkTimedGetSingleton() {
1142 +        RecursiveAction a = new CheckedRecursiveAction() {
1143 +            public void realCompute() throws Exception {
1144 +                AsyncFib f = new AsyncFib(8);
1145 +                assertSame(f, f.fork());
1146 +                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1147 +                assertEquals(21, f.number);
1148 +                checkCompletedNormally(f);
1149 +            }};
1150 +        testInvokeOnPool(singletonPool(), a);
1151 +    }
1152 +
1153 +    /**
1154 +     * timed get with null time unit throws NPE
1155 +     */
1156 +    public void testForkTimedGetNPESingleton() {
1157 +        RecursiveAction a = new CheckedRecursiveAction() {
1158 +            public void realCompute() throws Exception {
1159 +                AsyncFib f = new AsyncFib(8);
1160 +                assertSame(f, f.fork());
1161 +                try {
1162 +                    f.get(5L, null);
1163 +                    shouldThrow();
1164 +                } catch (NullPointerException success) {}
1165 +            }};
1166 +        testInvokeOnPool(singletonPool(), a);
1167 +    }
1168 +
1169 +    /**
1170 +     * quietlyJoin of a forked task returns when task completes
1171 +     */
1172 +    public void testForkQuietlyJoinSingleton() {
1173 +        RecursiveAction a = new CheckedRecursiveAction() {
1174 +            public void realCompute() {
1175 +                AsyncFib f = new AsyncFib(8);
1176 +                assertSame(f, f.fork());
1177 +                f.quietlyJoin();
1178 +                assertEquals(21, f.number);
1179 +                checkCompletedNormally(f);
1180 +            }};
1181 +        testInvokeOnPool(singletonPool(), a);
1182 +    }
1183 +
1184 +
1185 +    /**
1186 +     * helpQuiesce returns when tasks are complete.
1187 +     * getQueuedTaskCount returns 0 when quiescent
1188 +     */
1189 +    public void testForkHelpQuiesceSingleton() {
1190 +        RecursiveAction a = new CheckedRecursiveAction() {
1191 +            public void realCompute() {
1192 +                AsyncFib f = new AsyncFib(8);
1193 +                assertSame(f, f.fork());
1194 +                f.helpQuiesce();
1195 +                assertEquals(0, getQueuedTaskCount());
1196 +                assertEquals(21, f.number);
1197 +                checkCompletedNormally(f);
1198 +            }};
1199 +        testInvokeOnPool(singletonPool(), a);
1200 +    }
1201 +
1202 +
1203 +    /**
1204 +     * invoke task throws exception when task completes abnormally
1205 +     */
1206 +    public void testAbnormalInvokeSingleton() {
1207 +        RecursiveAction a = new CheckedRecursiveAction() {
1208 +            public void realCompute() {
1209 +                FailingAsyncFib f = new FailingAsyncFib(8);
1210 +                try {
1211 +                    f.invoke();
1212 +                    shouldThrow();
1213 +                } catch (FJException success) {
1214 +                    checkCompletedAbnormally(f, success);
1215 +                }
1216 +            }};
1217 +        testInvokeOnPool(singletonPool(), a);
1218 +    }
1219 +
1220 +    /**
1221 +     * quietlyInvoke task returns when task completes abnormally
1222 +     */
1223 +    public void testAbnormalQuietlyInvokeSingleton() {
1224 +        RecursiveAction a = new CheckedRecursiveAction() {
1225 +            public void realCompute() {
1226 +                FailingAsyncFib f = new FailingAsyncFib(8);
1227 +                f.quietlyInvoke();
1228 +                assertTrue(f.getException() instanceof FJException);
1229 +                checkCompletedAbnormally(f, f.getException());
1230 +            }};
1231 +        testInvokeOnPool(singletonPool(), a);
1232 +    }
1233 +
1234 +    /**
1235 +     * join of a forked task throws exception when task completes abnormally
1236 +     */
1237 +    public void testAbnormalForkJoinSingleton() {
1238 +        RecursiveAction a = new CheckedRecursiveAction() {
1239 +            public void realCompute() {
1240 +                FailingAsyncFib f = new FailingAsyncFib(8);
1241 +                assertSame(f, f.fork());
1242 +                try {
1243 +                    f.join();
1244 +                    shouldThrow();
1245 +                } catch (FJException success) {
1246 +                    checkCompletedAbnormally(f, success);
1247 +                }
1248 +            }};
1249 +        testInvokeOnPool(singletonPool(), a);
1250 +    }
1251 +
1252 +    /**
1253 +     * get of a forked task throws exception when task completes abnormally
1254 +     */
1255 +    public void testAbnormalForkGetSingleton() {
1256 +        RecursiveAction a = new CheckedRecursiveAction() {
1257 +            public void realCompute() throws Exception {
1258 +                FailingAsyncFib f = new FailingAsyncFib(8);
1259 +                assertSame(f, f.fork());
1260 +                try {
1261 +                    f.get();
1262 +                    shouldThrow();
1263 +                } catch (ExecutionException success) {
1264 +                    Throwable cause = success.getCause();
1265 +                    assertTrue(cause instanceof FJException);
1266 +                    checkCompletedAbnormally(f, cause);
1267 +                }
1268 +            }};
1269 +        testInvokeOnPool(singletonPool(), a);
1270 +    }
1271 +
1272 +    /**
1273 +     * timed get of a forked task throws exception when task completes abnormally
1274 +     */
1275 +    public void testAbnormalForkTimedGetSingleton() {
1276 +        RecursiveAction a = new CheckedRecursiveAction() {
1277 +            public void realCompute() throws Exception {
1278 +                FailingAsyncFib f = new FailingAsyncFib(8);
1279 +                assertSame(f, f.fork());
1280 +                try {
1281 +                    f.get(LONG_DELAY_MS, MILLISECONDS);
1282 +                    shouldThrow();
1283 +                } catch (ExecutionException success) {
1284 +                    Throwable cause = success.getCause();
1285 +                    assertTrue(cause instanceof FJException);
1286 +                    checkCompletedAbnormally(f, cause);
1287 +                }
1288 +            }};
1289 +        testInvokeOnPool(singletonPool(), a);
1290 +    }
1291 +
1292 +    /**
1293 +     * quietlyJoin of a forked task returns when task completes abnormally
1294 +     */
1295 +    public void testAbnormalForkQuietlyJoinSingleton() {
1296 +        RecursiveAction a = new CheckedRecursiveAction() {
1297 +            public void realCompute() {
1298 +                FailingAsyncFib f = new FailingAsyncFib(8);
1299 +                assertSame(f, f.fork());
1300 +                f.quietlyJoin();
1301 +                assertTrue(f.getException() instanceof FJException);
1302 +                checkCompletedAbnormally(f, f.getException());
1303 +            }};
1304 +        testInvokeOnPool(singletonPool(), a);
1305 +    }
1306 +
1307 +    /**
1308 +     * invoke task throws exception when task cancelled
1309 +     */
1310 +    public void testCancelledInvokeSingleton() {
1311 +        RecursiveAction a = new CheckedRecursiveAction() {
1312 +            public void realCompute() {
1313 +                AsyncFib f = new AsyncFib(8);
1314 +                assertTrue(f.cancel(true));
1315 +                try {
1316 +                    f.invoke();
1317 +                    shouldThrow();
1318 +                } catch (CancellationException success) {
1319 +                    checkCancelled(f);
1320 +                }
1321 +            }};
1322 +        testInvokeOnPool(singletonPool(), a);
1323 +    }
1324 +
1325 +    /**
1326 +     * join of a forked task throws exception when task cancelled
1327 +     */
1328 +    public void testCancelledForkJoinSingleton() {
1329 +        RecursiveAction a = new CheckedRecursiveAction() {
1330 +            public void realCompute() {
1331 +                AsyncFib f = new AsyncFib(8);
1332 +                assertTrue(f.cancel(true));
1333 +                assertSame(f, f.fork());
1334 +                try {
1335 +                    f.join();
1336 +                    shouldThrow();
1337 +                } catch (CancellationException success) {
1338 +                    checkCancelled(f);
1339 +                }
1340 +            }};
1341 +        testInvokeOnPool(singletonPool(), a);
1342 +    }
1343 +
1344 +    /**
1345 +     * get of a forked task throws exception when task cancelled
1346 +     */
1347 +    public void testCancelledForkGetSingleton() {
1348 +        RecursiveAction a = new CheckedRecursiveAction() {
1349 +            public void realCompute() throws Exception {
1350 +                AsyncFib f = new AsyncFib(8);
1351 +                assertTrue(f.cancel(true));
1352 +                assertSame(f, f.fork());
1353 +                try {
1354 +                    f.get();
1355 +                    shouldThrow();
1356 +                } catch (CancellationException success) {
1357 +                    checkCancelled(f);
1358 +                }
1359 +            }};
1360 +        testInvokeOnPool(singletonPool(), a);
1361 +    }
1362 +
1363 +    /**
1364 +     * timed get of a forked task throws exception when task cancelled
1365 +     */
1366 +    public void testCancelledForkTimedGetSingleton() throws Exception {
1367 +        RecursiveAction a = new CheckedRecursiveAction() {
1368 +            public void realCompute() throws Exception {
1369 +                AsyncFib f = new AsyncFib(8);
1370 +                assertTrue(f.cancel(true));
1371 +                assertSame(f, f.fork());
1372 +                try {
1373 +                    f.get(LONG_DELAY_MS, MILLISECONDS);
1374 +                    shouldThrow();
1375 +                } catch (CancellationException success) {
1376 +                    checkCancelled(f);
1377 +                }
1378 +            }};
1379 +        testInvokeOnPool(singletonPool(), a);
1380 +    }
1381 +
1382 +    /**
1383 +     * quietlyJoin of a forked task returns when task cancelled
1384 +     */
1385 +    public void testCancelledForkQuietlyJoinSingleton() {
1386 +        RecursiveAction a = new CheckedRecursiveAction() {
1387 +            public void realCompute() {
1388 +                AsyncFib f = new AsyncFib(8);
1389 +                assertTrue(f.cancel(true));
1390 +                assertSame(f, f.fork());
1391 +                f.quietlyJoin();
1392 +                checkCancelled(f);
1393 +            }};
1394 +        testInvokeOnPool(singletonPool(), a);
1395 +    }
1396 +
1397 +    /**
1398 +     * invoke task throws exception after invoking completeExceptionally
1399 +     */
1400 +    public void testCompleteExceptionallySingleton() {
1401 +        RecursiveAction a = new CheckedRecursiveAction() {
1402 +            public void realCompute() {
1403 +                AsyncFib f = new AsyncFib(8);
1404 +                f.completeExceptionally(new FJException());
1405 +                try {
1406 +                    f.invoke();
1407 +                    shouldThrow();
1408 +                } catch (FJException success) {
1409 +                    checkCompletedAbnormally(f, success);
1410 +                }
1411 +            }};
1412 +        testInvokeOnPool(singletonPool(), a);
1413 +    }
1414 +
1415 +    /**
1416 +     * invokeAll(t1, t2) invokes all task arguments
1417 +     */
1418 +    public void testInvokeAll2Singleton() {
1419 +        RecursiveAction a = new CheckedRecursiveAction() {
1420 +            public void realCompute() {
1421 +                AsyncFib f = new AsyncFib(8);
1422 +                AsyncFib g = new AsyncFib(9);
1423 +                invokeAll(f, g);
1424 +                assertEquals(21, f.number);
1425 +                assertEquals(34, g.number);
1426 +                checkCompletedNormally(f);
1427 +                checkCompletedNormally(g);
1428 +            }};
1429 +        testInvokeOnPool(singletonPool(), a);
1430 +    }
1431 +
1432 +    /**
1433 +     * invokeAll(tasks) with 1 argument invokes task
1434 +     */
1435 +    public void testInvokeAll1Singleton() {
1436 +        RecursiveAction a = new CheckedRecursiveAction() {
1437 +            public void realCompute() {
1438 +                AsyncFib f = new AsyncFib(8);
1439 +                invokeAll(f);
1440 +                checkCompletedNormally(f);
1441 +                assertEquals(21, f.number);
1442 +            }};
1443 +        testInvokeOnPool(singletonPool(), a);
1444 +    }
1445 +
1446 +    /**
1447 +     * invokeAll(tasks) with > 2 argument invokes tasks
1448 +     */
1449 +    public void testInvokeAll3Singleton() {
1450 +        RecursiveAction a = new CheckedRecursiveAction() {
1451 +            public void realCompute() {
1452 +                AsyncFib f = new AsyncFib(8);
1453 +                AsyncFib g = new AsyncFib(9);
1454 +                AsyncFib h = new AsyncFib(7);
1455 +                invokeAll(f, g, h);
1456 +                assertEquals(21, f.number);
1457 +                assertEquals(34, g.number);
1458 +                assertEquals(13, h.number);
1459 +                checkCompletedNormally(f);
1460 +                checkCompletedNormally(g);
1461 +                checkCompletedNormally(h);
1462 +            }};
1463 +        testInvokeOnPool(singletonPool(), a);
1464 +    }
1465 +
1466 +    /**
1467 +     * invokeAll(collection) invokes all tasks in the collection
1468 +     */
1469 +    public void testInvokeAllCollectionSingleton() {
1470 +        RecursiveAction a = new CheckedRecursiveAction() {
1471 +            public void realCompute() {
1472 +                AsyncFib f = new AsyncFib(8);
1473 +                AsyncFib g = new AsyncFib(9);
1474 +                AsyncFib h = new AsyncFib(7);
1475 +                HashSet set = new HashSet();
1476 +                set.add(f);
1477 +                set.add(g);
1478 +                set.add(h);
1479 +                invokeAll(set);
1480 +                assertEquals(21, f.number);
1481 +                assertEquals(34, g.number);
1482 +                assertEquals(13, h.number);
1483 +                checkCompletedNormally(f);
1484 +                checkCompletedNormally(g);
1485 +                checkCompletedNormally(h);
1486 +            }};
1487 +        testInvokeOnPool(singletonPool(), a);
1488 +    }
1489 +
1490 +
1491 +    /**
1492 +     * invokeAll(tasks) with any null task throws NPE
1493 +     */
1494 +    public void testInvokeAllNPESingleton() {
1495 +        RecursiveAction a = new CheckedRecursiveAction() {
1496 +            public void realCompute() {
1497 +                AsyncFib f = new AsyncFib(8);
1498 +                AsyncFib g = new AsyncFib(9);
1499 +                AsyncFib h = null;
1500 +                try {
1501 +                    invokeAll(f, g, h);
1502 +                    shouldThrow();
1503 +                } catch (NullPointerException success) {}
1504 +            }};
1505 +        testInvokeOnPool(singletonPool(), a);
1506 +    }
1507 +
1508 +    /**
1509 +     * invokeAll(t1, t2) throw exception if any task does
1510 +     */
1511 +    public void testAbnormalInvokeAll2Singleton() {
1512 +        RecursiveAction a = new CheckedRecursiveAction() {
1513 +            public void realCompute() {
1514 +                AsyncFib f = new AsyncFib(8);
1515 +                FailingAsyncFib g = new FailingAsyncFib(9);
1516 +                try {
1517 +                    invokeAll(f, g);
1518 +                    shouldThrow();
1519 +                } catch (FJException success) {
1520 +                    checkCompletedAbnormally(g, success);
1521 +                }
1522 +            }};
1523 +        testInvokeOnPool(singletonPool(), a);
1524 +    }
1525 +
1526 +    /**
1527 +     * invokeAll(tasks) with 1 argument throws exception if task does
1528 +     */
1529 +    public void testAbnormalInvokeAll1Singleton() {
1530 +        RecursiveAction a = new CheckedRecursiveAction() {
1531 +            public void realCompute() {
1532 +                FailingAsyncFib g = new FailingAsyncFib(9);
1533 +                try {
1534 +                    invokeAll(g);
1535 +                    shouldThrow();
1536 +                } catch (FJException success) {
1537 +                    checkCompletedAbnormally(g, success);
1538 +                }
1539 +            }};
1540 +        testInvokeOnPool(singletonPool(), a);
1541 +    }
1542 +
1543 +    /**
1544 +     * invokeAll(tasks) with > 2 argument throws exception if any task does
1545 +     */
1546 +    public void testAbnormalInvokeAll3Singleton() {
1547 +        RecursiveAction a = new CheckedRecursiveAction() {
1548 +            public void realCompute() {
1549 +                AsyncFib f = new AsyncFib(8);
1550 +                FailingAsyncFib g = new FailingAsyncFib(9);
1551 +                AsyncFib h = new AsyncFib(7);
1552 +                try {
1553 +                    invokeAll(f, g, h);
1554 +                    shouldThrow();
1555 +                } catch (FJException success) {
1556 +                    checkCompletedAbnormally(g, success);
1557 +                }
1558 +            }};
1559 +        testInvokeOnPool(singletonPool(), a);
1560 +    }
1561 +
1562 +    /**
1563 +     * invokeAll(collection)  throws exception if any task does
1564 +     */
1565 +    public void testAbnormalInvokeAllCollectionSingleton() {
1566 +        RecursiveAction a = new CheckedRecursiveAction() {
1567 +            public void realCompute() {
1568 +                FailingAsyncFib f = new FailingAsyncFib(8);
1569 +                AsyncFib g = new AsyncFib(9);
1570 +                AsyncFib h = new AsyncFib(7);
1571 +                HashSet set = new HashSet();
1572 +                set.add(f);
1573 +                set.add(g);
1574 +                set.add(h);
1575 +                try {
1576 +                    invokeAll(set);
1577 +                    shouldThrow();
1578 +                } catch (FJException success) {
1579 +                    checkCompletedAbnormally(f, success);
1580 +                }
1581 +            }};
1582 +        testInvokeOnPool(singletonPool(), a);
1583 +    }
1584 +
1585   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines