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.19 by jsr166, Mon Sep 27 19:15:16 2010 UTC vs.
Revision 1.24 by jsr166, Sun Nov 21 19:06:53 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 +        try {
98 +            assertSame(expected, a.get());
99 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
100 +        try {
101 +            assertSame(expected, a.get(5L, SECONDS));
102 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
103 +    }
104 +
105 +    void checkCancelled(ForkJoinTask a) {
106 +        assertTrue(a.isDone());
107 +        assertTrue(a.isCancelled());
108 +        assertFalse(a.isCompletedNormally());
109 +        assertTrue(a.isCompletedAbnormally());
110 +        assertTrue(a.getException() instanceof CancellationException);
111 +        assertNull(a.getRawResult());
112 +
113 +        try {
114 +            a.join();
115 +            shouldThrow();
116 +        } catch (CancellationException success) {
117 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
118 +
119 +        try {
120 +            a.get();
121 +            shouldThrow();
122 +        } catch (CancellationException success) {
123 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
124 +
125 +        try {
126 +            a.get(5L, SECONDS);
127 +            shouldThrow();
128 +        } catch (CancellationException success) {
129 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
130 +    }
131 +
132 +    void checkCompletedAbnormally(ForkJoinTask a, Throwable t) {
133 +        assertTrue(a.isDone());
134 +        assertFalse(a.isCancelled());
135 +        assertFalse(a.isCompletedNormally());
136 +        assertTrue(a.isCompletedAbnormally());
137 +        assertSame(t, a.getException());
138 +        assertNull(a.getRawResult());
139 +
140 +        try {
141 +            a.join();
142 +            shouldThrow();
143 +        } catch (Throwable expected) {
144 +            assertSame(t, expected);
145 +        }
146 +
147 +        try {
148 +            a.get();
149 +            shouldThrow();
150 +        } catch (ExecutionException success) {
151 +            assertSame(t, success.getCause());
152 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
153 +
154 +        try {
155 +            a.get(5L, SECONDS);
156 +            shouldThrow();
157 +        } catch (ExecutionException success) {
158 +            assertSame(t, success.getCause());
159 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
160 +    }
161 +
162      /*
163       * Testing coverage notes:
164       *
# Line 251 | Line 351 | public class ForkJoinTaskTest extends JS
351                  AsyncFib f = new AsyncFib(8);
352                  assertNull(f.invoke());
353                  assertEquals(21, f.number);
354 <                assertTrue(f.isDone());
255 <                assertFalse(f.isCancelled());
256 <                assertFalse(f.isCompletedAbnormally());
257 <                assertNull(f.getRawResult());
354 >                checkCompletedNormally(f);
355              }};
356          testInvokeOnPool(mainPool(), a);
357      }
# Line 270 | Line 367 | public class ForkJoinTaskTest extends JS
367                  AsyncFib f = new AsyncFib(8);
368                  f.quietlyInvoke();
369                  assertEquals(21, f.number);
370 <                assertTrue(f.isDone());
274 <                assertFalse(f.isCancelled());
275 <                assertFalse(f.isCompletedAbnormally());
276 <                assertNull(f.getRawResult());
370 >                checkCompletedNormally(f);
371              }};
372          testInvokeOnPool(mainPool(), a);
373      }
# Line 288 | Line 382 | public class ForkJoinTaskTest extends JS
382                  assertSame(f, f.fork());
383                  assertNull(f.join());
384                  assertEquals(21, f.number);
385 <                assertTrue(f.isDone());
292 <                assertNull(f.getRawResult());
385 >                checkCompletedNormally(f);
386              }};
387          testInvokeOnPool(mainPool(), a);
388      }
# Line 304 | Line 397 | public class ForkJoinTaskTest extends JS
397                  assertSame(f, f.fork());
398                  assertNull(f.get());
399                  assertEquals(21, f.number);
400 <                assertTrue(f.isDone());
400 >                checkCompletedNormally(f);
401              }};
402          testInvokeOnPool(mainPool(), a);
403      }
# Line 319 | Line 412 | public class ForkJoinTaskTest extends JS
412                  assertSame(f, f.fork());
413                  assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
414                  assertEquals(21, f.number);
415 <                assertTrue(f.isDone());
415 >                checkCompletedNormally(f);
416              }};
417          testInvokeOnPool(mainPool(), a);
418      }
# Line 350 | Line 443 | public class ForkJoinTaskTest extends JS
443                  assertSame(f, f.fork());
444                  f.quietlyJoin();
445                  assertEquals(21, f.number);
446 <                assertTrue(f.isDone());
446 >                checkCompletedNormally(f);
447              }};
448          testInvokeOnPool(mainPool(), a);
449      }
# Line 367 | Line 460 | public class ForkJoinTaskTest extends JS
460                  assertSame(f, f.fork());
461                  f.helpQuiesce();
462                  assertEquals(21, f.number);
370                assertTrue(f.isDone());
463                  assertEquals(0, getQueuedTaskCount());
464 +                checkCompletedNormally(f);
465              }};
466          testInvokeOnPool(mainPool(), a);
467      }
# Line 384 | Line 477 | public class ForkJoinTaskTest extends JS
477                  try {
478                      f.invoke();
479                      shouldThrow();
480 <                } catch (FJException success) {}
480 >                } catch (FJException success) {
481 >                    checkCompletedAbnormally(f, success);
482 >                }
483              }};
484          testInvokeOnPool(mainPool(), a);
485      }
# Line 397 | Line 492 | public class ForkJoinTaskTest extends JS
492              public void realCompute() {
493                  FailingAsyncFib f = new FailingAsyncFib(8);
494                  f.quietlyInvoke();
495 <                assertTrue(f.isDone());
495 >                assertTrue(f.getException() instanceof FJException);
496 >                checkCompletedAbnormally(f, f.getException());
497              }};
498          testInvokeOnPool(mainPool(), a);
499      }
# Line 413 | Line 509 | public class ForkJoinTaskTest extends JS
509                  try {
510                      f.join();
511                      shouldThrow();
512 <                } catch (FJException success) {}
512 >                } catch (FJException success) {
513 >                    checkCompletedAbnormally(f, success);
514 >                }
515              }};
516          testInvokeOnPool(mainPool(), a);
517      }
# Line 432 | Line 530 | public class ForkJoinTaskTest extends JS
530                  } catch (ExecutionException success) {
531                      Throwable cause = success.getCause();
532                      assertTrue(cause instanceof FJException);
533 <                    assertTrue(f.isDone());
436 <                    assertTrue(f.isCompletedAbnormally());
437 <                    assertSame(cause, f.getException());
533 >                    checkCompletedAbnormally(f, cause);
534                  }
535              }};
536          testInvokeOnPool(mainPool(), a);
# Line 454 | Line 550 | public class ForkJoinTaskTest extends JS
550                  } catch (ExecutionException success) {
551                      Throwable cause = success.getCause();
552                      assertTrue(cause instanceof FJException);
553 <                    assertTrue(f.isDone());
458 <                    assertTrue(f.isCompletedAbnormally());
459 <                    assertSame(cause, f.getException());
553 >                    checkCompletedAbnormally(f, cause);
554                  }
555              }};
556          testInvokeOnPool(mainPool(), a);
# Line 471 | Line 565 | public class ForkJoinTaskTest extends JS
565                  FailingAsyncFib f = new FailingAsyncFib(8);
566                  assertSame(f, f.fork());
567                  f.quietlyJoin();
474                assertTrue(f.isDone());
475                assertTrue(f.isCompletedAbnormally());
568                  assertTrue(f.getException() instanceof FJException);
569 +                checkCompletedAbnormally(f, f.getException());
570              }};
571          testInvokeOnPool(mainPool(), a);
572      }
# Line 490 | Line 583 | public class ForkJoinTaskTest extends JS
583                      f.invoke();
584                      shouldThrow();
585                  } catch (CancellationException success) {
586 <                    assertTrue(f.isDone());
494 <                    assertTrue(f.isCancelled());
495 <                    assertTrue(f.isCompletedAbnormally());
496 <                    assertTrue(f.getException() instanceof CancellationException);
586 >                    checkCancelled(f);
587                  }
588              }};
589          testInvokeOnPool(mainPool(), a);
# Line 512 | Line 602 | public class ForkJoinTaskTest extends JS
602                      f.join();
603                      shouldThrow();
604                  } catch (CancellationException success) {
605 <                    assertTrue(f.isDone());
516 <                    assertTrue(f.isCancelled());
517 <                    assertTrue(f.isCompletedAbnormally());
518 <                    assertTrue(f.getException() instanceof CancellationException);
605 >                    checkCancelled(f);
606                  }
607              }};
608          testInvokeOnPool(mainPool(), a);
# Line 534 | Line 621 | public class ForkJoinTaskTest extends JS
621                      f.get();
622                      shouldThrow();
623                  } catch (CancellationException success) {
624 <                    assertTrue(f.isDone());
538 <                    assertTrue(f.isCancelled());
539 <                    assertTrue(f.isCompletedAbnormally());
540 <                    assertTrue(f.getException() instanceof CancellationException);
624 >                    checkCancelled(f);
625                  }
626              }};
627          testInvokeOnPool(mainPool(), a);
# Line 556 | Line 640 | public class ForkJoinTaskTest extends JS
640                      f.get(LONG_DELAY_MS, MILLISECONDS);
641                      shouldThrow();
642                  } catch (CancellationException success) {
643 <                    assertTrue(f.isDone());
560 <                    assertTrue(f.isCancelled());
561 <                    assertTrue(f.isCompletedAbnormally());
562 <                    assertTrue(f.getException() instanceof CancellationException);
643 >                    checkCancelled(f);
644                  }
645              }};
646          testInvokeOnPool(mainPool(), a);
# Line 575 | Line 656 | public class ForkJoinTaskTest extends JS
656                  assertTrue(f.cancel(true));
657                  assertSame(f, f.fork());
658                  f.quietlyJoin();
659 <                assertTrue(f.isDone());
579 <                assertTrue(f.isCompletedAbnormally());
580 <                assertTrue(f.isCancelled());
581 <                assertTrue(f.getException() instanceof CancellationException);
659 >                checkCancelled(f);
660              }};
661          testInvokeOnPool(mainPool(), a);
662      }
# Line 623 | Line 701 | public class ForkJoinTaskTest extends JS
701      public void testInForkJoinPool2() {
702          RecursiveAction a = new CheckedRecursiveAction() {
703              public void realCompute() {
704 <                assertTrue(!inForkJoinPool());
704 >                assertFalse(inForkJoinPool());
705              }};
706          assertNull(a.invoke());
707      }
# Line 650 | Line 728 | public class ForkJoinTaskTest extends JS
728                  try {
729                      f.invoke();
730                      shouldThrow();
731 <                } catch (FJException success) {}
731 >                } catch (FJException success) {
732 >                    checkCompletedAbnormally(f, success);
733 >                }
734              }};
735          testInvokeOnPool(mainPool(), a);
736      }
# Line 664 | Line 744 | public class ForkJoinTaskTest extends JS
744                  AsyncFib f = new AsyncFib(8);
745                  AsyncFib g = new AsyncFib(9);
746                  invokeAll(f, g);
667                assertTrue(f.isDone());
747                  assertEquals(21, f.number);
669                assertTrue(g.isDone());
748                  assertEquals(34, g.number);
749 +                checkCompletedNormally(f);
750 +                checkCompletedNormally(g);
751              }};
752          testInvokeOnPool(mainPool(), a);
753      }
# Line 680 | Line 760 | public class ForkJoinTaskTest extends JS
760              public void realCompute() {
761                  AsyncFib f = new AsyncFib(8);
762                  invokeAll(f);
763 <                assertTrue(f.isDone());
763 >                checkCompletedNormally(f);
764                  assertEquals(21, f.number);
765              }};
766          testInvokeOnPool(mainPool(), a);
# Line 696 | Line 776 | public class ForkJoinTaskTest extends JS
776                  AsyncFib g = new AsyncFib(9);
777                  AsyncFib h = new AsyncFib(7);
778                  invokeAll(f, g, h);
699                assertTrue(f.isDone());
779                  assertEquals(21, f.number);
701                assertTrue(g.isDone());
780                  assertEquals(34, g.number);
703                assertTrue(h.isDone());
781                  assertEquals(13, h.number);
782 +                checkCompletedNormally(f);
783 +                checkCompletedNormally(g);
784 +                checkCompletedNormally(h);
785              }};
786          testInvokeOnPool(mainPool(), a);
787      }
# Line 720 | Line 800 | public class ForkJoinTaskTest extends JS
800                  set.add(g);
801                  set.add(h);
802                  invokeAll(set);
723                assertTrue(f.isDone());
803                  assertEquals(21, f.number);
725                assertTrue(g.isDone());
804                  assertEquals(34, g.number);
727                assertTrue(h.isDone());
805                  assertEquals(13, h.number);
806 +                checkCompletedNormally(f);
807 +                checkCompletedNormally(g);
808 +                checkCompletedNormally(h);
809              }};
810          testInvokeOnPool(mainPool(), a);
811      }
# Line 759 | Line 839 | public class ForkJoinTaskTest extends JS
839                  try {
840                      invokeAll(f, g);
841                      shouldThrow();
842 <                } catch (FJException success) {}
842 >                } catch (FJException success) {
843 >                    checkCompletedAbnormally(g, success);
844 >                }
845              }};
846          testInvokeOnPool(mainPool(), a);
847      }
# Line 774 | Line 856 | public class ForkJoinTaskTest extends JS
856                  try {
857                      invokeAll(g);
858                      shouldThrow();
859 <                } catch (FJException success) {}
859 >                } catch (FJException success) {
860 >                    checkCompletedAbnormally(g, success);
861 >                }
862              }};
863          testInvokeOnPool(mainPool(), a);
864      }
# Line 791 | Line 875 | public class ForkJoinTaskTest extends JS
875                  try {
876                      invokeAll(f, g, h);
877                      shouldThrow();
878 <                } catch (FJException success) {}
878 >                } catch (FJException success) {
879 >                    checkCompletedAbnormally(g, success);
880 >                }
881              }};
882          testInvokeOnPool(mainPool(), a);
883      }
# Line 812 | Line 898 | public class ForkJoinTaskTest extends JS
898                  try {
899                      invokeAll(set);
900                      shouldThrow();
901 <                } catch (FJException success) {}
901 >                } catch (FJException success) {
902 >                    checkCompletedAbnormally(f, success);
903 >                }
904              }};
905          testInvokeOnPool(mainPool(), a);
906      }
# Line 830 | Line 918 | public class ForkJoinTaskTest extends JS
918                  assertSame(f, f.fork());
919                  assertTrue(f.tryUnfork());
920                  helpQuiesce();
921 <                assertFalse(f.isDone());
922 <                assertTrue(g.isDone());
921 >                checkNotDone(f);
922 >                checkCompletedNormally(g);
923              }};
924          testInvokeOnPool(singletonPool(), a);
925      }
# Line 851 | Line 939 | public class ForkJoinTaskTest extends JS
939                  assertSame(f, f.fork());
940                  assertTrue(getSurplusQueuedTaskCount() > 0);
941                  helpQuiesce();
942 +                assertEquals(0, getSurplusQueuedTaskCount());
943 +                checkCompletedNormally(f);
944 +                checkCompletedNormally(g);
945 +                checkCompletedNormally(h);
946              }};
947          testInvokeOnPool(singletonPool(), a);
948      }
# Line 867 | Line 959 | public class ForkJoinTaskTest extends JS
959                  assertSame(f, f.fork());
960                  assertSame(f, peekNextLocalTask());
961                  assertNull(f.join());
962 <                assertTrue(f.isDone());
962 >                checkCompletedNormally(f);
963                  helpQuiesce();
964 +                checkCompletedNormally(g);
965              }};
966          testInvokeOnPool(singletonPool(), a);
967      }
968  
969      /**
970 <     * pollNextLocalTask returns most recent unexecuted task
971 <     * without executing it
970 >     * pollNextLocalTask returns most recent unexecuted task without
971 >     * executing it
972       */
973      public void testPollNextLocalTask() {
974          RecursiveAction a = new CheckedRecursiveAction() {
# Line 886 | Line 979 | public class ForkJoinTaskTest extends JS
979                  assertSame(f, f.fork());
980                  assertSame(f, pollNextLocalTask());
981                  helpQuiesce();
982 <                assertFalse(f.isDone());
982 >                checkNotDone(f);
983 >                assertEquals(34, g.number);
984 >                checkCompletedNormally(g);
985              }};
986          testInvokeOnPool(singletonPool(), a);
987      }
988  
989      /**
990 <     * pollTask returns an unexecuted task
896 <     * without executing it
990 >     * pollTask returns an unexecuted task without executing it
991       */
992      public void testPollTask() {
993          RecursiveAction a = new CheckedRecursiveAction() {
# Line 904 | Line 998 | public class ForkJoinTaskTest extends JS
998                  assertSame(f, f.fork());
999                  assertSame(f, pollTask());
1000                  helpQuiesce();
1001 <                assertFalse(f.isDone());
1002 <                assertTrue(g.isDone());
1001 >                checkNotDone(f);
1002 >                checkCompletedNormally(g);
1003              }};
1004          testInvokeOnPool(singletonPool(), a);
1005      }
# Line 923 | Line 1017 | public class ForkJoinTaskTest extends JS
1017                  assertSame(g, peekNextLocalTask());
1018                  assertNull(f.join());
1019                  helpQuiesce();
1020 <                assertTrue(f.isDone());
1020 >                checkCompletedNormally(f);
1021 >                assertEquals(34, g.number);
1022 >                checkCompletedNormally(g);
1023              }};
1024          testInvokeOnPool(asyncSingletonPool(), a);
1025      }
1026  
1027      /**
1028 <     * pollNextLocalTask returns least recent unexecuted task
1029 <     * without executing it, in async mode
1028 >     * pollNextLocalTask returns least recent unexecuted task without
1029 >     * executing it, in async mode
1030       */
1031      public void testPollNextLocalTaskAsync() {
1032          RecursiveAction a = new CheckedRecursiveAction() {
# Line 941 | Line 1037 | public class ForkJoinTaskTest extends JS
1037                  assertSame(f, f.fork());
1038                  assertSame(g, pollNextLocalTask());
1039                  helpQuiesce();
1040 <                assertTrue(f.isDone());
1041 <                assertFalse(g.isDone());
1040 >                assertEquals(21, f.number);
1041 >                checkCompletedNormally(f);
1042 >                checkNotDone(g);
1043              }};
1044          testInvokeOnPool(asyncSingletonPool(), a);
1045      }
1046  
1047      /**
1048 <     * pollTask returns an unexecuted task
1049 <     * without executing it, in async mode
1048 >     * pollTask returns an unexecuted task without executing it, in
1049 >     * async mode
1050       */
1051      public void testPollTaskAsync() {
1052          RecursiveAction a = new CheckedRecursiveAction() {
# Line 960 | Line 1057 | public class ForkJoinTaskTest extends JS
1057                  assertSame(f, f.fork());
1058                  assertSame(g, pollTask());
1059                  helpQuiesce();
1060 <                assertTrue(f.isDone());
1061 <                assertFalse(g.isDone());
1060 >                assertEquals(21, f.number);
1061 >                checkCompletedNormally(f);
1062 >                checkNotDone(g);
1063              }};
1064          testInvokeOnPool(asyncSingletonPool(), a);
1065      }
1066 +
1067 +    // versions for singleton pools
1068 +
1069 +    /**
1070 +     * invoke returns when task completes normally.
1071 +     * isCompletedAbnormally and isCancelled return false for normally
1072 +     * completed tasks. getRawResult of a RecursiveAction returns null;
1073 +     */
1074 +    public void testInvokeSingleton() {
1075 +        RecursiveAction a = new CheckedRecursiveAction() {
1076 +            public void realCompute() {
1077 +                AsyncFib f = new AsyncFib(8);
1078 +                assertNull(f.invoke());
1079 +                assertEquals(21, f.number);
1080 +                checkCompletedNormally(f);
1081 +            }};
1082 +        testInvokeOnPool(singletonPool(), a);
1083 +    }
1084 +
1085 +    /**
1086 +     * quietlyInvoke task returns when task completes normally.
1087 +     * isCompletedAbnormally and isCancelled return false for normally
1088 +     * completed tasks
1089 +     */
1090 +    public void testQuietlyInvokeSingleton() {
1091 +        RecursiveAction a = new CheckedRecursiveAction() {
1092 +            public void realCompute() {
1093 +                AsyncFib f = new AsyncFib(8);
1094 +                f.quietlyInvoke();
1095 +                assertEquals(21, f.number);
1096 +                checkCompletedNormally(f);
1097 +            }};
1098 +        testInvokeOnPool(singletonPool(), a);
1099 +    }
1100 +
1101 +    /**
1102 +     * join of a forked task returns when task completes
1103 +     */
1104 +    public void testForkJoinSingleton() {
1105 +        RecursiveAction a = new CheckedRecursiveAction() {
1106 +            public void realCompute() {
1107 +                AsyncFib f = new AsyncFib(8);
1108 +                assertSame(f, f.fork());
1109 +                assertNull(f.join());
1110 +                assertEquals(21, f.number);
1111 +                checkCompletedNormally(f);
1112 +            }};
1113 +        testInvokeOnPool(singletonPool(), a);
1114 +    }
1115 +
1116 +    /**
1117 +     * get of a forked task returns when task completes
1118 +     */
1119 +    public void testForkGetSingleton() {
1120 +        RecursiveAction a = new CheckedRecursiveAction() {
1121 +            public void realCompute() throws Exception {
1122 +                AsyncFib f = new AsyncFib(8);
1123 +                assertSame(f, f.fork());
1124 +                assertNull(f.get());
1125 +                assertEquals(21, f.number);
1126 +                checkCompletedNormally(f);
1127 +            }};
1128 +        testInvokeOnPool(singletonPool(), a);
1129 +    }
1130 +
1131 +    /**
1132 +     * timed get of a forked task returns when task completes
1133 +     */
1134 +    public void testForkTimedGetSingleton() {
1135 +        RecursiveAction a = new CheckedRecursiveAction() {
1136 +            public void realCompute() throws Exception {
1137 +                AsyncFib f = new AsyncFib(8);
1138 +                assertSame(f, f.fork());
1139 +                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1140 +                assertEquals(21, f.number);
1141 +                checkCompletedNormally(f);
1142 +            }};
1143 +        testInvokeOnPool(singletonPool(), a);
1144 +    }
1145 +
1146 +    /**
1147 +     * timed get with null time unit throws NPE
1148 +     */
1149 +    public void testForkTimedGetNPESingleton() {
1150 +        RecursiveAction a = new CheckedRecursiveAction() {
1151 +            public void realCompute() throws Exception {
1152 +                AsyncFib f = new AsyncFib(8);
1153 +                assertSame(f, f.fork());
1154 +                try {
1155 +                    f.get(5L, null);
1156 +                    shouldThrow();
1157 +                } catch (NullPointerException success) {}
1158 +            }};
1159 +        testInvokeOnPool(singletonPool(), a);
1160 +    }
1161 +
1162 +    /**
1163 +     * quietlyJoin of a forked task returns when task completes
1164 +     */
1165 +    public void testForkQuietlyJoinSingleton() {
1166 +        RecursiveAction a = new CheckedRecursiveAction() {
1167 +            public void realCompute() {
1168 +                AsyncFib f = new AsyncFib(8);
1169 +                assertSame(f, f.fork());
1170 +                f.quietlyJoin();
1171 +                assertEquals(21, f.number);
1172 +                checkCompletedNormally(f);
1173 +            }};
1174 +        testInvokeOnPool(singletonPool(), a);
1175 +    }
1176 +
1177 +
1178 +    /**
1179 +     * helpQuiesce returns when tasks are complete.
1180 +     * getQueuedTaskCount returns 0 when quiescent
1181 +     */
1182 +    public void testForkHelpQuiesceSingleton() {
1183 +        RecursiveAction a = new CheckedRecursiveAction() {
1184 +            public void realCompute() {
1185 +                AsyncFib f = new AsyncFib(8);
1186 +                assertSame(f, f.fork());
1187 +                f.helpQuiesce();
1188 +                assertEquals(0, getQueuedTaskCount());
1189 +                assertEquals(21, f.number);
1190 +                checkCompletedNormally(f);
1191 +            }};
1192 +        testInvokeOnPool(singletonPool(), a);
1193 +    }
1194 +
1195 +
1196 +    /**
1197 +     * invoke task throws exception when task completes abnormally
1198 +     */
1199 +    public void testAbnormalInvokeSingleton() {
1200 +        RecursiveAction a = new CheckedRecursiveAction() {
1201 +            public void realCompute() {
1202 +                FailingAsyncFib f = new FailingAsyncFib(8);
1203 +                try {
1204 +                    f.invoke();
1205 +                    shouldThrow();
1206 +                } catch (FJException success) {
1207 +                    checkCompletedAbnormally(f, success);
1208 +                }
1209 +            }};
1210 +        testInvokeOnPool(singletonPool(), a);
1211 +    }
1212 +
1213 +    /**
1214 +     * quietlyInvoke task returns when task completes abnormally
1215 +     */
1216 +    public void testAbnormalQuietlyInvokeSingleton() {
1217 +        RecursiveAction a = new CheckedRecursiveAction() {
1218 +            public void realCompute() {
1219 +                FailingAsyncFib f = new FailingAsyncFib(8);
1220 +                f.quietlyInvoke();
1221 +                assertTrue(f.getException() instanceof FJException);
1222 +                checkCompletedAbnormally(f, f.getException());
1223 +            }};
1224 +        testInvokeOnPool(singletonPool(), a);
1225 +    }
1226 +
1227 +    /**
1228 +     * join of a forked task throws exception when task completes abnormally
1229 +     */
1230 +    public void testAbnormalForkJoinSingleton() {
1231 +        RecursiveAction a = new CheckedRecursiveAction() {
1232 +            public void realCompute() {
1233 +                FailingAsyncFib f = new FailingAsyncFib(8);
1234 +                assertSame(f, f.fork());
1235 +                try {
1236 +                    f.join();
1237 +                    shouldThrow();
1238 +                } catch (FJException success) {
1239 +                    checkCompletedAbnormally(f, success);
1240 +                }
1241 +            }};
1242 +        testInvokeOnPool(singletonPool(), a);
1243 +    }
1244 +
1245 +    /**
1246 +     * get of a forked task throws exception when task completes abnormally
1247 +     */
1248 +    public void testAbnormalForkGetSingleton() {
1249 +        RecursiveAction a = new CheckedRecursiveAction() {
1250 +            public void realCompute() throws Exception {
1251 +                FailingAsyncFib f = new FailingAsyncFib(8);
1252 +                assertSame(f, f.fork());
1253 +                try {
1254 +                    f.get();
1255 +                    shouldThrow();
1256 +                } catch (ExecutionException success) {
1257 +                    Throwable cause = success.getCause();
1258 +                    assertTrue(cause instanceof FJException);
1259 +                    checkCompletedAbnormally(f, cause);
1260 +                }
1261 +            }};
1262 +        testInvokeOnPool(singletonPool(), a);
1263 +    }
1264 +
1265 +    /**
1266 +     * timed get of a forked task throws exception when task completes abnormally
1267 +     */
1268 +    public void testAbnormalForkTimedGetSingleton() {
1269 +        RecursiveAction a = new CheckedRecursiveAction() {
1270 +            public void realCompute() throws Exception {
1271 +                FailingAsyncFib f = new FailingAsyncFib(8);
1272 +                assertSame(f, f.fork());
1273 +                try {
1274 +                    f.get(LONG_DELAY_MS, MILLISECONDS);
1275 +                    shouldThrow();
1276 +                } catch (ExecutionException success) {
1277 +                    Throwable cause = success.getCause();
1278 +                    assertTrue(cause instanceof FJException);
1279 +                    checkCompletedAbnormally(f, cause);
1280 +                }
1281 +            }};
1282 +        testInvokeOnPool(singletonPool(), a);
1283 +    }
1284 +
1285 +    /**
1286 +     * quietlyJoin of a forked task returns when task completes abnormally
1287 +     */
1288 +    public void testAbnormalForkQuietlyJoinSingleton() {
1289 +        RecursiveAction a = new CheckedRecursiveAction() {
1290 +            public void realCompute() {
1291 +                FailingAsyncFib f = new FailingAsyncFib(8);
1292 +                assertSame(f, f.fork());
1293 +                f.quietlyJoin();
1294 +                assertTrue(f.getException() instanceof FJException);
1295 +                checkCompletedAbnormally(f, f.getException());
1296 +            }};
1297 +        testInvokeOnPool(singletonPool(), a);
1298 +    }
1299 +
1300 +    /**
1301 +     * invoke task throws exception when task cancelled
1302 +     */
1303 +    public void testCancelledInvokeSingleton() {
1304 +        RecursiveAction a = new CheckedRecursiveAction() {
1305 +            public void realCompute() {
1306 +                AsyncFib f = new AsyncFib(8);
1307 +                assertTrue(f.cancel(true));
1308 +                try {
1309 +                    f.invoke();
1310 +                    shouldThrow();
1311 +                } catch (CancellationException success) {
1312 +                    checkCancelled(f);
1313 +                }
1314 +            }};
1315 +        testInvokeOnPool(singletonPool(), a);
1316 +    }
1317 +
1318 +    /**
1319 +     * join of a forked task throws exception when task cancelled
1320 +     */
1321 +    public void testCancelledForkJoinSingleton() {
1322 +        RecursiveAction a = new CheckedRecursiveAction() {
1323 +            public void realCompute() {
1324 +                AsyncFib f = new AsyncFib(8);
1325 +                assertTrue(f.cancel(true));
1326 +                assertSame(f, f.fork());
1327 +                try {
1328 +                    f.join();
1329 +                    shouldThrow();
1330 +                } catch (CancellationException success) {
1331 +                    checkCancelled(f);
1332 +                }
1333 +            }};
1334 +        testInvokeOnPool(singletonPool(), a);
1335 +    }
1336 +
1337 +    /**
1338 +     * get of a forked task throws exception when task cancelled
1339 +     */
1340 +    public void testCancelledForkGetSingleton() {
1341 +        RecursiveAction a = new CheckedRecursiveAction() {
1342 +            public void realCompute() throws Exception {
1343 +                AsyncFib f = new AsyncFib(8);
1344 +                assertTrue(f.cancel(true));
1345 +                assertSame(f, f.fork());
1346 +                try {
1347 +                    f.get();
1348 +                    shouldThrow();
1349 +                } catch (CancellationException success) {
1350 +                    checkCancelled(f);
1351 +                }
1352 +            }};
1353 +        testInvokeOnPool(singletonPool(), a);
1354 +    }
1355 +
1356 +    /**
1357 +     * timed get of a forked task throws exception when task cancelled
1358 +     */
1359 +    public void testCancelledForkTimedGetSingleton() throws Exception {
1360 +        RecursiveAction a = new CheckedRecursiveAction() {
1361 +            public void realCompute() throws Exception {
1362 +                AsyncFib f = new AsyncFib(8);
1363 +                assertTrue(f.cancel(true));
1364 +                assertSame(f, f.fork());
1365 +                try {
1366 +                    f.get(LONG_DELAY_MS, MILLISECONDS);
1367 +                    shouldThrow();
1368 +                } catch (CancellationException success) {
1369 +                    checkCancelled(f);
1370 +                }
1371 +            }};
1372 +        testInvokeOnPool(singletonPool(), a);
1373 +    }
1374 +
1375 +    /**
1376 +     * quietlyJoin of a forked task returns when task cancelled
1377 +     */
1378 +    public void testCancelledForkQuietlyJoinSingleton() {
1379 +        RecursiveAction a = new CheckedRecursiveAction() {
1380 +            public void realCompute() {
1381 +                AsyncFib f = new AsyncFib(8);
1382 +                assertTrue(f.cancel(true));
1383 +                assertSame(f, f.fork());
1384 +                f.quietlyJoin();
1385 +                checkCancelled(f);
1386 +            }};
1387 +        testInvokeOnPool(singletonPool(), a);
1388 +    }
1389 +
1390 +    /**
1391 +     * invoke task throws exception after invoking completeExceptionally
1392 +     */
1393 +    public void testCompleteExceptionallySingleton() {
1394 +        RecursiveAction a = new CheckedRecursiveAction() {
1395 +            public void realCompute() {
1396 +                AsyncFib f = new AsyncFib(8);
1397 +                f.completeExceptionally(new FJException());
1398 +                try {
1399 +                    f.invoke();
1400 +                    shouldThrow();
1401 +                } catch (FJException success) {
1402 +                    checkCompletedAbnormally(f, success);
1403 +                }
1404 +            }};
1405 +        testInvokeOnPool(singletonPool(), a);
1406 +    }
1407 +
1408 +    /**
1409 +     * invokeAll(t1, t2) invokes all task arguments
1410 +     */
1411 +    public void testInvokeAll2Singleton() {
1412 +        RecursiveAction a = new CheckedRecursiveAction() {
1413 +            public void realCompute() {
1414 +                AsyncFib f = new AsyncFib(8);
1415 +                AsyncFib g = new AsyncFib(9);
1416 +                invokeAll(f, g);
1417 +                assertEquals(21, f.number);
1418 +                assertEquals(34, g.number);
1419 +                checkCompletedNormally(f);
1420 +                checkCompletedNormally(g);
1421 +            }};
1422 +        testInvokeOnPool(singletonPool(), a);
1423 +    }
1424 +
1425 +    /**
1426 +     * invokeAll(tasks) with 1 argument invokes task
1427 +     */
1428 +    public void testInvokeAll1Singleton() {
1429 +        RecursiveAction a = new CheckedRecursiveAction() {
1430 +            public void realCompute() {
1431 +                AsyncFib f = new AsyncFib(8);
1432 +                invokeAll(f);
1433 +                checkCompletedNormally(f);
1434 +                assertEquals(21, f.number);
1435 +            }};
1436 +        testInvokeOnPool(singletonPool(), a);
1437 +    }
1438 +
1439 +    /**
1440 +     * invokeAll(tasks) with > 2 argument invokes tasks
1441 +     */
1442 +    public void testInvokeAll3Singleton() {
1443 +        RecursiveAction a = new CheckedRecursiveAction() {
1444 +            public void realCompute() {
1445 +                AsyncFib f = new AsyncFib(8);
1446 +                AsyncFib g = new AsyncFib(9);
1447 +                AsyncFib h = new AsyncFib(7);
1448 +                invokeAll(f, g, h);
1449 +                assertEquals(21, f.number);
1450 +                assertEquals(34, g.number);
1451 +                assertEquals(13, h.number);
1452 +                checkCompletedNormally(f);
1453 +                checkCompletedNormally(g);
1454 +                checkCompletedNormally(h);
1455 +            }};
1456 +        testInvokeOnPool(singletonPool(), a);
1457 +    }
1458 +
1459 +    /**
1460 +     * invokeAll(collection) invokes all tasks in the collection
1461 +     */
1462 +    public void testInvokeAllCollectionSingleton() {
1463 +        RecursiveAction a = new CheckedRecursiveAction() {
1464 +            public void realCompute() {
1465 +                AsyncFib f = new AsyncFib(8);
1466 +                AsyncFib g = new AsyncFib(9);
1467 +                AsyncFib h = new AsyncFib(7);
1468 +                HashSet set = new HashSet();
1469 +                set.add(f);
1470 +                set.add(g);
1471 +                set.add(h);
1472 +                invokeAll(set);
1473 +                assertEquals(21, f.number);
1474 +                assertEquals(34, g.number);
1475 +                assertEquals(13, h.number);
1476 +                checkCompletedNormally(f);
1477 +                checkCompletedNormally(g);
1478 +                checkCompletedNormally(h);
1479 +            }};
1480 +        testInvokeOnPool(singletonPool(), a);
1481 +    }
1482 +
1483 +
1484 +    /**
1485 +     * invokeAll(tasks) with any null task throws NPE
1486 +     */
1487 +    public void testInvokeAllNPESingleton() {
1488 +        RecursiveAction a = new CheckedRecursiveAction() {
1489 +            public void realCompute() {
1490 +                AsyncFib f = new AsyncFib(8);
1491 +                AsyncFib g = new AsyncFib(9);
1492 +                AsyncFib h = null;
1493 +                try {
1494 +                    invokeAll(f, g, h);
1495 +                    shouldThrow();
1496 +                } catch (NullPointerException success) {}
1497 +            }};
1498 +        testInvokeOnPool(singletonPool(), a);
1499 +    }
1500 +
1501 +    /**
1502 +     * invokeAll(t1, t2) throw exception if any task does
1503 +     */
1504 +    public void testAbnormalInvokeAll2Singleton() {
1505 +        RecursiveAction a = new CheckedRecursiveAction() {
1506 +            public void realCompute() {
1507 +                AsyncFib f = new AsyncFib(8);
1508 +                FailingAsyncFib g = new FailingAsyncFib(9);
1509 +                try {
1510 +                    invokeAll(f, g);
1511 +                    shouldThrow();
1512 +                } catch (FJException success) {
1513 +                    checkCompletedAbnormally(g, success);
1514 +                }
1515 +            }};
1516 +        testInvokeOnPool(singletonPool(), a);
1517 +    }
1518 +
1519 +    /**
1520 +     * invokeAll(tasks) with 1 argument throws exception if task does
1521 +     */
1522 +    public void testAbnormalInvokeAll1Singleton() {
1523 +        RecursiveAction a = new CheckedRecursiveAction() {
1524 +            public void realCompute() {
1525 +                FailingAsyncFib g = new FailingAsyncFib(9);
1526 +                try {
1527 +                    invokeAll(g);
1528 +                    shouldThrow();
1529 +                } catch (FJException success) {
1530 +                    checkCompletedAbnormally(g, success);
1531 +                }
1532 +            }};
1533 +        testInvokeOnPool(singletonPool(), a);
1534 +    }
1535 +
1536 +    /**
1537 +     * invokeAll(tasks) with > 2 argument throws exception if any task does
1538 +     */
1539 +    public void testAbnormalInvokeAll3Singleton() {
1540 +        RecursiveAction a = new CheckedRecursiveAction() {
1541 +            public void realCompute() {
1542 +                AsyncFib f = new AsyncFib(8);
1543 +                FailingAsyncFib g = new FailingAsyncFib(9);
1544 +                AsyncFib h = new AsyncFib(7);
1545 +                try {
1546 +                    invokeAll(f, g, h);
1547 +                    shouldThrow();
1548 +                } catch (FJException success) {
1549 +                    checkCompletedAbnormally(g, success);
1550 +                }
1551 +            }};
1552 +        testInvokeOnPool(singletonPool(), a);
1553 +    }
1554 +
1555 +    /**
1556 +     * invokeAll(collection)  throws exception if any task does
1557 +     */
1558 +    public void testAbnormalInvokeAllCollectionSingleton() {
1559 +        RecursiveAction a = new CheckedRecursiveAction() {
1560 +            public void realCompute() {
1561 +                FailingAsyncFib f = new FailingAsyncFib(8);
1562 +                AsyncFib g = new AsyncFib(9);
1563 +                AsyncFib h = new AsyncFib(7);
1564 +                HashSet set = new HashSet();
1565 +                set.add(f);
1566 +                set.add(g);
1567 +                set.add(h);
1568 +                try {
1569 +                    invokeAll(set);
1570 +                    shouldThrow();
1571 +                } catch (FJException success) {
1572 +                    checkCompletedAbnormally(f, success);
1573 +                }
1574 +            }};
1575 +        testInvokeOnPool(singletonPool(), a);
1576 +    }
1577 +
1578   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines