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.22 by jsr166, Sun Nov 21 20:32:15 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 502 | Line 613 | public class RecursiveActionTest extends
613          RecursiveAction a = new CheckedRecursiveAction() {
614              public void realCompute() {
615                  FibAction f = new FibAction(8);
616 <                assertNull(f.invoke());
617 <                assertEquals(21, f.result);
618 <                assertTrue(f.isDone());
619 <                assertFalse(f.isCancelled());
620 <                assertFalse(f.isCompletedAbnormally());
621 <                f.reinitialize();
622 <                assertNull(f.invoke());
623 <                assertEquals(21, f.result);
616 >                checkNotDone(f);
617 >
618 >                for (int i = 0; i < 3; i++) {
619 >                    assertNull(f.invoke());
620 >                    assertEquals(21, f.result);
621 >                    checkCompletedNormally(f);
622 >                    f.reinitialize();
623 >                    checkNotDone(f);
624 >                }
625              }};
626          testInvokeOnPool(mainPool(), a);
627      }
# Line 525 | Line 637 | public class RecursiveActionTest extends
637                  try {
638                      f.invoke();
639                      shouldThrow();
640 <                } catch (FJException success) {}
640 >                } catch (FJException success) {
641 >                    checkCompletedAbnormally(f, success);
642 >                }
643              }};
644          testInvokeOnPool(mainPool(), a);
645      }
# Line 539 | Line 653 | public class RecursiveActionTest extends
653                  FibAction f = new FibAction(8);
654                  f.complete(null);
655                  assertNull(f.invoke());
542                assertTrue(f.isDone());
656                  assertEquals(0, f.result);
657 +                checkCompletedNormally(f);
658              }};
659          testInvokeOnPool(mainPool(), a);
660      }
# Line 554 | Line 668 | public class RecursiveActionTest extends
668                  FibAction f = new FibAction(8);
669                  FibAction g = new FibAction(9);
670                  invokeAll(f, g);
671 <                assertTrue(f.isDone());
671 >                checkCompletedNormally(f);
672                  assertEquals(21, f.result);
673 <                assertTrue(g.isDone());
673 >                checkCompletedNormally(g);
674                  assertEquals(34, g.result);
675              }};
676          testInvokeOnPool(mainPool(), a);
# Line 570 | Line 684 | public class RecursiveActionTest extends
684              public void realCompute() {
685                  FibAction f = new FibAction(8);
686                  invokeAll(f);
687 <                assertTrue(f.isDone());
687 >                checkCompletedNormally(f);
688                  assertEquals(21, f.result);
689              }};
690          testInvokeOnPool(mainPool(), a);
# Line 587 | Line 701 | public class RecursiveActionTest extends
701                  FibAction h = new FibAction(7);
702                  invokeAll(f, g, h);
703                  assertTrue(f.isDone());
590                assertEquals(21, f.result);
704                  assertTrue(g.isDone());
592                assertEquals(34, g.result);
705                  assertTrue(h.isDone());
706 +                checkCompletedNormally(f);
707 +                assertEquals(21, f.result);
708 +                checkCompletedNormally(g);
709 +                assertEquals(34, g.result);
710 +                checkCompletedNormally(g);
711                  assertEquals(13, h.result);
712              }};
713          testInvokeOnPool(mainPool(), a);
# Line 611 | Line 728 | public class RecursiveActionTest extends
728                  set.add(h);
729                  invokeAll(set);
730                  assertTrue(f.isDone());
614                assertEquals(21, f.result);
731                  assertTrue(g.isDone());
616                assertEquals(34, g.result);
732                  assertTrue(h.isDone());
733 +                checkCompletedNormally(f);
734 +                assertEquals(21, f.result);
735 +                checkCompletedNormally(g);
736 +                assertEquals(34, g.result);
737 +                checkCompletedNormally(g);
738                  assertEquals(13, h.result);
739              }};
740          testInvokeOnPool(mainPool(), a);
# Line 649 | Line 769 | public class RecursiveActionTest extends
769                  try {
770                      invokeAll(f, g);
771                      shouldThrow();
772 <                } catch (FJException success) {}
772 >                } catch (FJException success) {
773 >                    checkCompletedAbnormally(g, success);
774 >                }
775              }};
776          testInvokeOnPool(mainPool(), a);
777      }
# Line 664 | Line 786 | public class RecursiveActionTest extends
786                  try {
787                      invokeAll(g);
788                      shouldThrow();
789 <                } catch (FJException success) {}
789 >                } catch (FJException success) {
790 >                    checkCompletedAbnormally(g, success);
791 >                }
792              }};
793          testInvokeOnPool(mainPool(), a);
794      }
# Line 681 | Line 805 | public class RecursiveActionTest extends
805                  try {
806                      invokeAll(f, g, h);
807                      shouldThrow();
808 <                } catch (FJException success) {}
808 >                } catch (FJException success) {
809 >                    checkCompletedAbnormally(g, success);
810 >                }
811              }};
812          testInvokeOnPool(mainPool(), a);
813      }
# Line 702 | Line 828 | public class RecursiveActionTest extends
828                  try {
829                      invokeAll(set);
830                      shouldThrow();
831 <                } catch (FJException success) {}
831 >                } catch (FJException success) {
832 >                    checkCompletedAbnormally(f, success);
833 >                }
834              }};
835          testInvokeOnPool(mainPool(), a);
836      }
# Line 720 | Line 848 | public class RecursiveActionTest extends
848                  assertSame(f, f.fork());
849                  assertTrue(f.tryUnfork());
850                  helpQuiesce();
851 <                assertFalse(f.isDone());
852 <                assertTrue(g.isDone());
851 >                checkNotDone(f);
852 >                checkCompletedNormally(g);
853              }};
854          testInvokeOnPool(singletonPool(), a);
855      }
# Line 741 | Line 869 | public class RecursiveActionTest extends
869                  assertSame(f, f.fork());
870                  assertTrue(getSurplusQueuedTaskCount() > 0);
871                  helpQuiesce();
872 +                assertEquals(0, getSurplusQueuedTaskCount());
873 +                checkCompletedNormally(f);
874 +                checkCompletedNormally(g);
875 +                checkCompletedNormally(h);
876              }};
877          testInvokeOnPool(singletonPool(), a);
878      }
# Line 757 | Line 889 | public class RecursiveActionTest extends
889                  assertSame(f, f.fork());
890                  assertSame(f, peekNextLocalTask());
891                  assertNull(f.join());
892 <                assertTrue(f.isDone());
892 >                checkCompletedNormally(f);
893                  helpQuiesce();
894 +                checkCompletedNormally(f);
895 +                checkCompletedNormally(g);
896              }};
897          testInvokeOnPool(singletonPool(), a);
898      }
# Line 776 | Line 910 | public class RecursiveActionTest extends
910                  assertSame(f, f.fork());
911                  assertSame(f, pollNextLocalTask());
912                  helpQuiesce();
913 <                assertFalse(f.isDone());
913 >                checkNotDone(f);
914 >                checkCompletedNormally(g);
915              }};
916          testInvokeOnPool(singletonPool(), a);
917      }
918  
919      /**
920 <     * pollTask returns an unexecuted task
786 <     * without executing it
920 >     * pollTask returns an unexecuted task without executing it
921       */
922      public void testPollTask() {
923          RecursiveAction a = new CheckedRecursiveAction() {
# Line 794 | Line 928 | public class RecursiveActionTest extends
928                  assertSame(f, f.fork());
929                  assertSame(f, pollTask());
930                  helpQuiesce();
931 <                assertFalse(f.isDone());
932 <                assertTrue(g.isDone());
931 >                checkNotDone(f);
932 >                checkCompletedNormally(g);
933              }};
934          testInvokeOnPool(singletonPool(), a);
935      }
# Line 813 | Line 947 | public class RecursiveActionTest extends
947                  assertSame(g, peekNextLocalTask());
948                  assertNull(f.join());
949                  helpQuiesce();
950 <                assertTrue(f.isDone());
950 >                checkCompletedNormally(f);
951 >                checkCompletedNormally(g);
952              }};
953          testInvokeOnPool(asyncSingletonPool(), a);
954      }
955  
956      /**
957 <     * pollNextLocalTask returns least recent unexecuted task
958 <     * without executing it, in async mode
957 >     * pollNextLocalTask returns least recent unexecuted task without
958 >     * executing it, in async mode
959       */
960      public void testPollNextLocalTaskAsync() {
961          RecursiveAction a = new CheckedRecursiveAction() {
# Line 831 | Line 966 | public class RecursiveActionTest extends
966                  assertSame(f, f.fork());
967                  assertSame(g, pollNextLocalTask());
968                  helpQuiesce();
969 <                assertTrue(f.isDone());
970 <                assertFalse(g.isDone());
969 >                checkCompletedNormally(f);
970 >                checkNotDone(g);
971              }};
972          testInvokeOnPool(asyncSingletonPool(), a);
973      }
974  
975      /**
976 <     * pollTask returns an unexecuted task
977 <     * without executing it, in async mode
976 >     * pollTask returns an unexecuted task without executing it, in
977 >     * async mode
978       */
979      public void testPollTaskAsync() {
980          RecursiveAction a = new CheckedRecursiveAction() {
# Line 850 | Line 985 | public class RecursiveActionTest extends
985                  assertSame(f, f.fork());
986                  assertSame(g, pollTask());
987                  helpQuiesce();
988 <                assertTrue(f.isDone());
989 <                assertFalse(g.isDone());
988 >                checkCompletedNormally(f);
989 >                checkNotDone(g);
990              }};
991          testInvokeOnPool(asyncSingletonPool(), a);
992      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines