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

Comparing jsr166/src/test/tck/RecursiveActionTest.java (file contents):
Revision 1.18 by jsr166, Thu Sep 16 00:52:49 2010 UTC vs.
Revision 1.23 by jsr166, Mon Nov 22 07:50:50 2010 UTC

# Line 11 | Line 11 | import java.util.concurrent.ForkJoinPool
11   import java.util.concurrent.ForkJoinWorkerThread;
12   import java.util.concurrent.RecursiveAction;
13   import java.util.concurrent.TimeUnit;
14 + import java.util.concurrent.TimeoutException;
15 + import static java.util.concurrent.TimeUnit.SECONDS;
16   import java.util.HashSet;
17  
18   public class RecursiveActionTest extends JSR166TestCase {
# Line 39 | Line 41 | public class RecursiveActionTest extends
41  
42      private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
43          try {
44 <            assertFalse(a.isDone());
43 <            assertFalse(a.isCompletedNormally());
44 <            assertFalse(a.isCompletedAbnormally());
45 <            assertFalse(a.isCancelled());
46 <            assertNull(a.getException());
44 >            checkNotDone(a);
45  
46              assertNull(pool.invoke(a));
47  
48 <            assertTrue(a.isDone());
51 <            assertTrue(a.isCompletedNormally());
52 <            assertFalse(a.isCompletedAbnormally());
53 <            assertFalse(a.isCancelled());
54 <            assertNull(a.getException());
48 >            checkCompletedNormally(a);
49          } finally {
50              joinPool(pool);
51          }
52      }
53  
54 +    void checkNotDone(RecursiveAction a) {
55 +        assertFalse(a.isDone());
56 +        assertFalse(a.isCompletedNormally());
57 +        assertFalse(a.isCompletedAbnormally());
58 +        assertFalse(a.isCancelled());
59 +        assertNull(a.getException());
60 +        assertNull(a.getRawResult());
61 +
62 +        if (! (Thread.currentThread() instanceof ForkJoinWorkerThread)) {
63 +            Thread.currentThread().interrupt();
64 +            try {
65 +                a.get();
66 +                shouldThrow();
67 +            } catch (InterruptedException success) {
68 +            } catch (Throwable fail) { threadUnexpectedException(fail); }
69 +
70 +            Thread.currentThread().interrupt();
71 +            try {
72 +                a.get(5L, SECONDS);
73 +                shouldThrow();
74 +            } catch (InterruptedException success) {
75 +            } catch (Throwable fail) { threadUnexpectedException(fail); }
76 +        }
77 +
78 +        try {
79 +            a.get(0L, SECONDS);
80 +            shouldThrow();
81 +        } catch (TimeoutException success) {
82 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
83 +    }
84 +
85 +    void checkCompletedNormally(RecursiveAction a) {
86 +        assertTrue(a.isDone());
87 +        assertFalse(a.isCancelled());
88 +        assertTrue(a.isCompletedNormally());
89 +        assertFalse(a.isCompletedAbnormally());
90 +        assertNull(a.getException());
91 +        assertNull(a.getRawResult());
92 +        assertNull(a.join());
93 +        assertFalse(a.cancel(false));
94 +        assertFalse(a.cancel(true));
95 +        try {
96 +            assertNull(a.get());
97 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
98 +        try {
99 +            assertNull(a.get(5L, SECONDS));
100 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
101 +    }
102 +
103 +    void checkCancelled(RecursiveAction a) {
104 +        assertTrue(a.isDone());
105 +        assertTrue(a.isCancelled());
106 +        assertFalse(a.isCompletedNormally());
107 +        assertTrue(a.isCompletedAbnormally());
108 +        assertTrue(a.getException() instanceof CancellationException);
109 +        assertNull(a.getRawResult());
110 +
111 +        try {
112 +            a.join();
113 +            shouldThrow();
114 +        } catch (CancellationException success) {
115 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
116 +
117 +        try {
118 +            a.get();
119 +            shouldThrow();
120 +        } catch (CancellationException success) {
121 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
122 +
123 +        try {
124 +            a.get(5L, SECONDS);
125 +            shouldThrow();
126 +        } catch (CancellationException success) {
127 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
128 +    }
129 +
130 +    void checkCompletedAbnormally(RecursiveAction a, Throwable t) {
131 +        assertTrue(a.isDone());
132 +        assertFalse(a.isCancelled());
133 +        assertFalse(a.isCompletedNormally());
134 +        assertTrue(a.isCompletedAbnormally());
135 +        assertSame(t, a.getException());
136 +        assertNull(a.getRawResult());
137 +        assertFalse(a.cancel(false));
138 +        assertFalse(a.cancel(true));
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      static final class FJException extends RuntimeException {
163          FJException() { super(); }
164      }
# Line 108 | Line 210 | public class RecursiveActionTest extends
210                  FibAction f = new FibAction(8);
211                  assertNull(f.invoke());
212                  assertEquals(21, f.result);
213 <                assertTrue(f.isDone());
112 <                assertFalse(f.isCancelled());
113 <                assertFalse(f.isCompletedAbnormally());
114 <                assertNull(f.getRawResult());
213 >                checkCompletedNormally(f);
214              }};
215          testInvokeOnPool(mainPool(), a);
216      }
# Line 127 | Line 226 | public class RecursiveActionTest extends
226                  FibAction f = new FibAction(8);
227                  f.quietlyInvoke();
228                  assertEquals(21, f.result);
229 <                assertTrue(f.isDone());
131 <                assertFalse(f.isCancelled());
132 <                assertFalse(f.isCompletedAbnormally());
133 <                assertNull(f.getRawResult());
229 >                checkCompletedNormally(f);
230              }};
231          testInvokeOnPool(mainPool(), a);
232      }
# Line 145 | Line 241 | public class RecursiveActionTest extends
241                  assertSame(f, f.fork());
242                  assertNull(f.join());
243                  assertEquals(21, f.result);
244 <                assertTrue(f.isDone());
149 <                assertNull(f.getRawResult());
244 >                checkCompletedNormally(f);
245              }};
246          testInvokeOnPool(mainPool(), a);
247      }
# Line 161 | Line 256 | public class RecursiveActionTest extends
256                  assertSame(f, f.fork());
257                  assertNull(f.get());
258                  assertEquals(21, f.result);
259 <                assertTrue(f.isDone());
259 >                checkCompletedNormally(f);
260              }};
261          testInvokeOnPool(mainPool(), a);
262      }
# Line 174 | Line 269 | public class RecursiveActionTest extends
269              public void realCompute() throws Exception {
270                  FibAction f = new FibAction(8);
271                  assertSame(f, f.fork());
272 <                assertNull(f.get(5L, TimeUnit.SECONDS));
272 >                assertNull(f.get(5L, SECONDS));
273                  assertEquals(21, f.result);
274 <                assertTrue(f.isDone());
274 >                checkCompletedNormally(f);
275              }};
276          testInvokeOnPool(mainPool(), a);
277      }
# Line 207 | Line 302 | public class RecursiveActionTest extends
302                  assertSame(f, f.fork());
303                  f.quietlyJoin();
304                  assertEquals(21, f.result);
305 <                assertTrue(f.isDone());
305 >                checkCompletedNormally(f);
306              }};
307          testInvokeOnPool(mainPool(), a);
308      }
# Line 224 | Line 319 | public class RecursiveActionTest extends
319                  assertSame(f, f.fork());
320                  f.helpQuiesce();
321                  assertEquals(21, f.result);
227                assertTrue(f.isDone());
322                  assertEquals(0, getQueuedTaskCount());
323 +                checkCompletedNormally(f);
324              }};
325          testInvokeOnPool(mainPool(), a);
326      }
# Line 241 | Line 336 | public class RecursiveActionTest extends
336                  try {
337                      f.invoke();
338                      shouldThrow();
339 <                } catch (FJException success) {}
339 >                } catch (FJException success) {
340 >                    checkCompletedAbnormally(f, success);
341 >                }
342              }};
343          testInvokeOnPool(mainPool(), a);
344      }
# Line 254 | Line 351 | public class RecursiveActionTest extends
351              public void realCompute() {
352                  FailingFibAction f = new FailingFibAction(8);
353                  f.quietlyInvoke();
354 <                assertTrue(f.isDone());
354 >                assertTrue(f.getException() instanceof FJException);
355 >                checkCompletedAbnormally(f, f.getException());
356              }};
357          testInvokeOnPool(mainPool(), a);
358      }
# Line 270 | Line 368 | public class RecursiveActionTest extends
368                  try {
369                      f.join();
370                      shouldThrow();
371 <                } catch (FJException success) {}
371 >                } catch (FJException success) {
372 >                    checkCompletedAbnormally(f, success);
373 >                }
374              }};
375          testInvokeOnPool(mainPool(), a);
376      }
# Line 286 | Line 386 | public class RecursiveActionTest extends
386                  try {
387                      f.get();
388                      shouldThrow();
389 <                } catch (ExecutionException success) {}
389 >                } catch (ExecutionException success) {
390 >                    Throwable cause = success.getCause();
391 >                    assertTrue(cause instanceof FJException);
392 >                    checkCompletedAbnormally(f, cause);
393 >                }
394              }};
395          testInvokeOnPool(mainPool(), a);
396      }
# Line 302 | Line 406 | public class RecursiveActionTest extends
406                  try {
407                      f.get(5L, TimeUnit.SECONDS);
408                      shouldThrow();
409 <                } catch (ExecutionException success) {}
409 >                } catch (ExecutionException success) {
410 >                    Throwable cause = success.getCause();
411 >                    assertTrue(cause instanceof FJException);
412 >                    checkCompletedAbnormally(f, cause);
413 >                }
414              }};
415          testInvokeOnPool(mainPool(), a);
416      }
# Line 316 | Line 424 | public class RecursiveActionTest extends
424                  FailingFibAction f = new FailingFibAction(8);
425                  assertSame(f, f.fork());
426                  f.quietlyJoin();
319                assertTrue(f.isDone());
320                assertTrue(f.isCompletedAbnormally());
427                  assertTrue(f.getException() instanceof FJException);
428 +                checkCompletedAbnormally(f, f.getException());
429              }};
430          testInvokeOnPool(mainPool(), a);
431      }
# Line 334 | Line 441 | public class RecursiveActionTest extends
441                  try {
442                      f.invoke();
443                      shouldThrow();
444 <                } catch (CancellationException success) {}
444 >                } catch (CancellationException success) {
445 >                    checkCancelled(f);
446 >                }
447              }};
448          testInvokeOnPool(mainPool(), a);
449      }
# Line 351 | Line 460 | public class RecursiveActionTest extends
460                  try {
461                      f.join();
462                      shouldThrow();
463 <                } catch (CancellationException success) {}
463 >                } catch (CancellationException success) {
464 >                    checkCancelled(f);
465 >                }
466              }};
467          testInvokeOnPool(mainPool(), a);
468      }
# Line 368 | Line 479 | public class RecursiveActionTest extends
479                  try {
480                      f.get();
481                      shouldThrow();
482 <                } catch (CancellationException success) {}
482 >                } catch (CancellationException success) {
483 >                    checkCancelled(f);
484 >                }
485              }};
486          testInvokeOnPool(mainPool(), a);
487      }
# Line 383 | Line 496 | public class RecursiveActionTest extends
496                  assertTrue(f.cancel(true));
497                  assertSame(f, f.fork());
498                  try {
499 <                    f.get(5L, TimeUnit.SECONDS);
499 >                    f.get(5L, SECONDS);
500                      shouldThrow();
501 <                } catch (CancellationException success) {}
501 >                } catch (CancellationException success) {
502 >                    checkCancelled(f);
503 >                }
504              }};
505          testInvokeOnPool(mainPool(), a);
506      }
# Line 400 | Line 515 | public class RecursiveActionTest extends
515                  assertTrue(f.cancel(true));
516                  assertSame(f, f.fork());
517                  f.quietlyJoin();
518 <                assertTrue(f.isDone());
404 <                assertTrue(f.isCompletedAbnormally());
405 <                assertTrue(f.isCancelled());
406 <                assertTrue(f.getException() instanceof CancellationException);
518 >                checkCancelled(f);
519              }};
520          testInvokeOnPool(mainPool(), a);
521      }
# Line 476 | Line 588 | public class RecursiveActionTest extends
588              public void realCompute() {
589                  ForkJoinWorkerThread w =
590                      (ForkJoinWorkerThread)(Thread.currentThread());
591 <                int idx = w.getPoolIndex();
592 <                assertTrue(idx >= 0);
481 <                assertTrue(idx < mainPool.getPoolSize());
591 >                assertTrue(w.getPoolIndex() >= 0);
592 >                assertTrue(w.getPoolIndex() < mainPool.getPoolSize());
593              }};
594          testInvokeOnPool(mainPool, a);
595      }
# Line 491 | Line 602 | public class RecursiveActionTest extends
602          RecursiveAction a = new CheckedRecursiveAction() {
603              public void realCompute() {
604                  setRawResult(null);
605 +                assertNull(getRawResult());
606              }};
607          assertNull(a.invoke());
608      }
# Line 502 | Line 614 | public class RecursiveActionTest extends
614          RecursiveAction a = new CheckedRecursiveAction() {
615              public void realCompute() {
616                  FibAction f = new FibAction(8);
617 <                assertNull(f.invoke());
618 <                assertEquals(21, f.result);
619 <                assertTrue(f.isDone());
620 <                assertFalse(f.isCancelled());
621 <                assertFalse(f.isCompletedAbnormally());
622 <                f.reinitialize();
623 <                assertNull(f.invoke());
624 <                assertEquals(21, f.result);
617 >                checkNotDone(f);
618 >
619 >                for (int i = 0; i < 3; i++) {
620 >                    assertNull(f.invoke());
621 >                    assertEquals(21, f.result);
622 >                    checkCompletedNormally(f);
623 >                    f.reinitialize();
624 >                    checkNotDone(f);
625 >                }
626              }};
627          testInvokeOnPool(mainPool(), a);
628      }
# Line 525 | Line 638 | public class RecursiveActionTest extends
638                  try {
639                      f.invoke();
640                      shouldThrow();
641 <                } catch (FJException success) {}
641 >                } catch (FJException success) {
642 >                    checkCompletedAbnormally(f, success);
643 >                }
644              }};
645          testInvokeOnPool(mainPool(), a);
646      }
# Line 539 | Line 654 | public class RecursiveActionTest extends
654                  FibAction f = new FibAction(8);
655                  f.complete(null);
656                  assertNull(f.invoke());
542                assertTrue(f.isDone());
657                  assertEquals(0, f.result);
658 +                checkCompletedNormally(f);
659              }};
660          testInvokeOnPool(mainPool(), a);
661      }
# Line 554 | Line 669 | public class RecursiveActionTest extends
669                  FibAction f = new FibAction(8);
670                  FibAction g = new FibAction(9);
671                  invokeAll(f, g);
672 <                assertTrue(f.isDone());
672 >                checkCompletedNormally(f);
673                  assertEquals(21, f.result);
674 <                assertTrue(g.isDone());
674 >                checkCompletedNormally(g);
675                  assertEquals(34, g.result);
676              }};
677          testInvokeOnPool(mainPool(), a);
# Line 570 | Line 685 | public class RecursiveActionTest extends
685              public void realCompute() {
686                  FibAction f = new FibAction(8);
687                  invokeAll(f);
688 <                assertTrue(f.isDone());
688 >                checkCompletedNormally(f);
689                  assertEquals(21, f.result);
690              }};
691          testInvokeOnPool(mainPool(), a);
# Line 587 | Line 702 | public class RecursiveActionTest extends
702                  FibAction h = new FibAction(7);
703                  invokeAll(f, g, h);
704                  assertTrue(f.isDone());
590                assertEquals(21, f.result);
705                  assertTrue(g.isDone());
592                assertEquals(34, g.result);
706                  assertTrue(h.isDone());
707 +                checkCompletedNormally(f);
708 +                assertEquals(21, f.result);
709 +                checkCompletedNormally(g);
710 +                assertEquals(34, g.result);
711 +                checkCompletedNormally(g);
712                  assertEquals(13, h.result);
713              }};
714          testInvokeOnPool(mainPool(), a);
# Line 611 | Line 729 | public class RecursiveActionTest extends
729                  set.add(h);
730                  invokeAll(set);
731                  assertTrue(f.isDone());
614                assertEquals(21, f.result);
732                  assertTrue(g.isDone());
616                assertEquals(34, g.result);
733                  assertTrue(h.isDone());
734 +                checkCompletedNormally(f);
735 +                assertEquals(21, f.result);
736 +                checkCompletedNormally(g);
737 +                assertEquals(34, g.result);
738 +                checkCompletedNormally(g);
739                  assertEquals(13, h.result);
740              }};
741          testInvokeOnPool(mainPool(), a);
# Line 649 | Line 770 | public class RecursiveActionTest extends
770                  try {
771                      invokeAll(f, g);
772                      shouldThrow();
773 <                } catch (FJException success) {}
773 >                } catch (FJException success) {
774 >                    checkCompletedAbnormally(g, success);
775 >                }
776              }};
777          testInvokeOnPool(mainPool(), a);
778      }
# Line 664 | Line 787 | public class RecursiveActionTest extends
787                  try {
788                      invokeAll(g);
789                      shouldThrow();
790 <                } catch (FJException success) {}
790 >                } catch (FJException success) {
791 >                    checkCompletedAbnormally(g, success);
792 >                }
793              }};
794          testInvokeOnPool(mainPool(), a);
795      }
# Line 681 | Line 806 | public class RecursiveActionTest extends
806                  try {
807                      invokeAll(f, g, h);
808                      shouldThrow();
809 <                } catch (FJException success) {}
809 >                } catch (FJException success) {
810 >                    checkCompletedAbnormally(g, success);
811 >                }
812              }};
813          testInvokeOnPool(mainPool(), a);
814      }
# Line 702 | Line 829 | public class RecursiveActionTest extends
829                  try {
830                      invokeAll(set);
831                      shouldThrow();
832 <                } catch (FJException success) {}
832 >                } catch (FJException success) {
833 >                    checkCompletedAbnormally(f, success);
834 >                }
835              }};
836          testInvokeOnPool(mainPool(), a);
837      }
# Line 720 | Line 849 | public class RecursiveActionTest extends
849                  assertSame(f, f.fork());
850                  assertTrue(f.tryUnfork());
851                  helpQuiesce();
852 <                assertFalse(f.isDone());
853 <                assertTrue(g.isDone());
852 >                checkNotDone(f);
853 >                checkCompletedNormally(g);
854              }};
855          testInvokeOnPool(singletonPool(), a);
856      }
# Line 741 | Line 870 | public class RecursiveActionTest extends
870                  assertSame(f, f.fork());
871                  assertTrue(getSurplusQueuedTaskCount() > 0);
872                  helpQuiesce();
873 +                assertEquals(0, getSurplusQueuedTaskCount());
874 +                checkCompletedNormally(f);
875 +                checkCompletedNormally(g);
876 +                checkCompletedNormally(h);
877              }};
878          testInvokeOnPool(singletonPool(), a);
879      }
# Line 757 | Line 890 | public class RecursiveActionTest extends
890                  assertSame(f, f.fork());
891                  assertSame(f, peekNextLocalTask());
892                  assertNull(f.join());
893 <                assertTrue(f.isDone());
893 >                checkCompletedNormally(f);
894                  helpQuiesce();
895 +                checkCompletedNormally(f);
896 +                checkCompletedNormally(g);
897              }};
898          testInvokeOnPool(singletonPool(), a);
899      }
# Line 776 | Line 911 | public class RecursiveActionTest extends
911                  assertSame(f, f.fork());
912                  assertSame(f, pollNextLocalTask());
913                  helpQuiesce();
914 <                assertFalse(f.isDone());
914 >                checkNotDone(f);
915 >                checkCompletedNormally(g);
916              }};
917          testInvokeOnPool(singletonPool(), a);
918      }
919  
920      /**
921 <     * pollTask returns an unexecuted task
786 <     * without executing it
921 >     * pollTask returns an unexecuted task without executing it
922       */
923      public void testPollTask() {
924          RecursiveAction a = new CheckedRecursiveAction() {
# Line 794 | Line 929 | public class RecursiveActionTest extends
929                  assertSame(f, f.fork());
930                  assertSame(f, pollTask());
931                  helpQuiesce();
932 <                assertFalse(f.isDone());
933 <                assertTrue(g.isDone());
932 >                checkNotDone(f);
933 >                checkCompletedNormally(g);
934              }};
935          testInvokeOnPool(singletonPool(), a);
936      }
# Line 813 | Line 948 | public class RecursiveActionTest extends
948                  assertSame(g, peekNextLocalTask());
949                  assertNull(f.join());
950                  helpQuiesce();
951 <                assertTrue(f.isDone());
951 >                checkCompletedNormally(f);
952 >                checkCompletedNormally(g);
953              }};
954          testInvokeOnPool(asyncSingletonPool(), a);
955      }
956  
957      /**
958 <     * pollNextLocalTask returns least recent unexecuted task
959 <     * without executing it, in async mode
958 >     * pollNextLocalTask returns least recent unexecuted task without
959 >     * executing it, in async mode
960       */
961      public void testPollNextLocalTaskAsync() {
962          RecursiveAction a = new CheckedRecursiveAction() {
# Line 831 | Line 967 | public class RecursiveActionTest extends
967                  assertSame(f, f.fork());
968                  assertSame(g, pollNextLocalTask());
969                  helpQuiesce();
970 <                assertTrue(f.isDone());
971 <                assertFalse(g.isDone());
970 >                checkCompletedNormally(f);
971 >                checkNotDone(g);
972              }};
973          testInvokeOnPool(asyncSingletonPool(), a);
974      }
975  
976      /**
977 <     * pollTask returns an unexecuted task
978 <     * without executing it, in async mode
977 >     * pollTask returns an unexecuted task without executing it, in
978 >     * async mode
979       */
980      public void testPollTaskAsync() {
981          RecursiveAction a = new CheckedRecursiveAction() {
# Line 850 | Line 986 | public class RecursiveActionTest extends
986                  assertSame(f, f.fork());
987                  assertSame(g, pollTask());
988                  helpQuiesce();
989 <                assertTrue(f.isDone());
990 <                assertFalse(g.isDone());
989 >                checkCompletedNormally(f);
990 >                checkNotDone(g);
991              }};
992          testInvokeOnPool(asyncSingletonPool(), a);
993      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines