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.20 by jsr166, Sun Nov 21 08:35:40 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 +        try {
94 +            assertNull(a.get());
95 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
96 +        try {
97 +            assertNull(a.get(5L, SECONDS));
98 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
99 +    }
100 +
101 +    void checkCancelled(RecursiveAction a) {
102 +        assertTrue(a.isDone());
103 +        assertTrue(a.isCancelled());
104 +        assertFalse(a.isCompletedNormally());
105 +        assertTrue(a.isCompletedAbnormally());
106 +        assertTrue(a.getException() instanceof CancellationException);
107 +        assertNull(a.getRawResult());
108 +
109 +        try {
110 +            a.join();
111 +            shouldThrow();
112 +        } catch (CancellationException success) {
113 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
114 +
115 +        try {
116 +            a.get();
117 +            shouldThrow();
118 +        } catch (CancellationException success) {
119 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
120 +
121 +        try {
122 +            a.get(5L, SECONDS);
123 +            shouldThrow();
124 +        } catch (CancellationException success) {
125 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
126 +    }
127 +
128 +    void checkTaskThrew(RecursiveAction a, Throwable t) {
129 +        assertTrue(a.isDone());
130 +        assertFalse(a.isCancelled());
131 +        assertFalse(a.isCompletedNormally());
132 +        assertTrue(a.isCompletedAbnormally());
133 +        assertSame(t, a.getException());
134 +        assertNull(a.getRawResult());
135 +
136 +        try {
137 +            a.join();
138 +            shouldThrow();
139 +        } catch (Throwable expected) {
140 +            assertSame(t, expected);
141 +        }
142 +
143 +        try {
144 +            a.get();
145 +            shouldThrow();
146 +        } catch (ExecutionException success) {
147 +            assertSame(t, success.getCause());
148 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
149 +
150 +        try {
151 +            a.get(5L, SECONDS);
152 +            shouldThrow();
153 +        } catch (ExecutionException success) {
154 +            assertSame(t, success.getCause());
155 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
156 +    }
157 +
158      static final class FJException extends RuntimeException {
159          FJException() { super(); }
160      }
# Line 108 | Line 206 | public class RecursiveActionTest extends
206                  FibAction f = new FibAction(8);
207                  assertNull(f.invoke());
208                  assertEquals(21, f.result);
209 <                assertTrue(f.isDone());
112 <                assertFalse(f.isCancelled());
113 <                assertFalse(f.isCompletedAbnormally());
114 <                assertNull(f.getRawResult());
209 >                checkCompletedNormally(f);
210              }};
211          testInvokeOnPool(mainPool(), a);
212      }
# Line 127 | Line 222 | public class RecursiveActionTest extends
222                  FibAction f = new FibAction(8);
223                  f.quietlyInvoke();
224                  assertEquals(21, f.result);
225 <                assertTrue(f.isDone());
131 <                assertFalse(f.isCancelled());
132 <                assertFalse(f.isCompletedAbnormally());
133 <                assertNull(f.getRawResult());
225 >                checkCompletedNormally(f);
226              }};
227          testInvokeOnPool(mainPool(), a);
228      }
# Line 145 | Line 237 | public class RecursiveActionTest extends
237                  assertSame(f, f.fork());
238                  assertNull(f.join());
239                  assertEquals(21, f.result);
240 <                assertTrue(f.isDone());
149 <                assertNull(f.getRawResult());
240 >                checkCompletedNormally(f);
241              }};
242          testInvokeOnPool(mainPool(), a);
243      }
# Line 161 | Line 252 | public class RecursiveActionTest extends
252                  assertSame(f, f.fork());
253                  assertNull(f.get());
254                  assertEquals(21, f.result);
255 <                assertTrue(f.isDone());
255 >                checkCompletedNormally(f);
256              }};
257          testInvokeOnPool(mainPool(), a);
258      }
# Line 174 | Line 265 | public class RecursiveActionTest extends
265              public void realCompute() throws Exception {
266                  FibAction f = new FibAction(8);
267                  assertSame(f, f.fork());
268 <                assertNull(f.get(5L, TimeUnit.SECONDS));
268 >                assertNull(f.get(5L, SECONDS));
269                  assertEquals(21, f.result);
270 <                assertTrue(f.isDone());
270 >                checkCompletedNormally(f);
271              }};
272          testInvokeOnPool(mainPool(), a);
273      }
# Line 207 | Line 298 | public class RecursiveActionTest extends
298                  assertSame(f, f.fork());
299                  f.quietlyJoin();
300                  assertEquals(21, f.result);
301 <                assertTrue(f.isDone());
301 >                checkCompletedNormally(f);
302              }};
303          testInvokeOnPool(mainPool(), a);
304      }
# Line 224 | Line 315 | public class RecursiveActionTest extends
315                  assertSame(f, f.fork());
316                  f.helpQuiesce();
317                  assertEquals(21, f.result);
227                assertTrue(f.isDone());
318                  assertEquals(0, getQueuedTaskCount());
319 +                checkCompletedNormally(f);
320              }};
321          testInvokeOnPool(mainPool(), a);
322      }
# Line 241 | Line 332 | public class RecursiveActionTest extends
332                  try {
333                      f.invoke();
334                      shouldThrow();
335 <                } catch (FJException success) {}
335 >                } catch (FJException success) {
336 >                    checkTaskThrew(f, success);
337 >                }
338              }};
339          testInvokeOnPool(mainPool(), a);
340      }
# Line 254 | Line 347 | public class RecursiveActionTest extends
347              public void realCompute() {
348                  FailingFibAction f = new FailingFibAction(8);
349                  f.quietlyInvoke();
350 <                assertTrue(f.isDone());
350 >                assertTrue(f.getException() instanceof FJException);
351 >                checkTaskThrew(f, f.getException());
352              }};
353          testInvokeOnPool(mainPool(), a);
354      }
# Line 270 | Line 364 | public class RecursiveActionTest extends
364                  try {
365                      f.join();
366                      shouldThrow();
367 <                } catch (FJException success) {}
367 >                } catch (FJException success) {
368 >                    checkTaskThrew(f, success);
369 >                }
370              }};
371          testInvokeOnPool(mainPool(), a);
372      }
# Line 286 | Line 382 | public class RecursiveActionTest extends
382                  try {
383                      f.get();
384                      shouldThrow();
385 <                } catch (ExecutionException success) {}
385 >                } catch (ExecutionException success) {
386 >                    checkTaskThrew(f, success.getCause());
387 >                }
388              }};
389          testInvokeOnPool(mainPool(), a);
390      }
# Line 302 | Line 400 | public class RecursiveActionTest extends
400                  try {
401                      f.get(5L, TimeUnit.SECONDS);
402                      shouldThrow();
403 <                } catch (ExecutionException success) {}
403 >                } catch (ExecutionException success) {
404 >                    checkTaskThrew(f, success.getCause());
405 >                }
406              }};
407          testInvokeOnPool(mainPool(), a);
408      }
# Line 316 | Line 416 | public class RecursiveActionTest extends
416                  FailingFibAction f = new FailingFibAction(8);
417                  assertSame(f, f.fork());
418                  f.quietlyJoin();
319                assertTrue(f.isDone());
320                assertTrue(f.isCompletedAbnormally());
419                  assertTrue(f.getException() instanceof FJException);
420 +                checkTaskThrew(f, f.getException());
421              }};
422          testInvokeOnPool(mainPool(), a);
423      }
# Line 334 | Line 433 | public class RecursiveActionTest extends
433                  try {
434                      f.invoke();
435                      shouldThrow();
436 <                } catch (CancellationException success) {}
436 >                } catch (CancellationException success) {
437 >                    checkCancelled(f);
438 >                }
439              }};
440          testInvokeOnPool(mainPool(), a);
441      }
# Line 351 | Line 452 | public class RecursiveActionTest extends
452                  try {
453                      f.join();
454                      shouldThrow();
455 <                } catch (CancellationException success) {}
455 >                } catch (CancellationException success) {
456 >                    checkCancelled(f);
457 >                }
458              }};
459          testInvokeOnPool(mainPool(), a);
460      }
# Line 368 | Line 471 | public class RecursiveActionTest extends
471                  try {
472                      f.get();
473                      shouldThrow();
474 <                } catch (CancellationException success) {}
474 >                } catch (CancellationException success) {
475 >                    checkCancelled(f);
476 >                }
477              }};
478          testInvokeOnPool(mainPool(), a);
479      }
# Line 383 | Line 488 | public class RecursiveActionTest extends
488                  assertTrue(f.cancel(true));
489                  assertSame(f, f.fork());
490                  try {
491 <                    f.get(5L, TimeUnit.SECONDS);
491 >                    f.get(5L, SECONDS);
492                      shouldThrow();
493 <                } catch (CancellationException success) {}
493 >                } catch (CancellationException success) {
494 >                    checkCancelled(f);
495 >                }
496              }};
497          testInvokeOnPool(mainPool(), a);
498      }
# Line 400 | Line 507 | public class RecursiveActionTest extends
507                  assertTrue(f.cancel(true));
508                  assertSame(f, f.fork());
509                  f.quietlyJoin();
510 <                assertTrue(f.isDone());
404 <                assertTrue(f.isCompletedAbnormally());
405 <                assertTrue(f.isCancelled());
406 <                assertTrue(f.getException() instanceof CancellationException);
510 >                checkCancelled(f);
511              }};
512          testInvokeOnPool(mainPool(), a);
513      }
# Line 476 | Line 580 | public class RecursiveActionTest extends
580              public void realCompute() {
581                  ForkJoinWorkerThread w =
582                      (ForkJoinWorkerThread)(Thread.currentThread());
583 <                int idx = w.getPoolIndex();
584 <                assertTrue(idx >= 0);
481 <                assertTrue(idx < mainPool.getPoolSize());
583 >                assertTrue(w.getPoolIndex() >= 0);
584 >                assertTrue(w.getPoolIndex() < mainPool.getPoolSize());
585              }};
586          testInvokeOnPool(mainPool, a);
587      }
# Line 502 | Line 605 | public class RecursiveActionTest extends
605          RecursiveAction a = new CheckedRecursiveAction() {
606              public void realCompute() {
607                  FibAction f = new FibAction(8);
608 <                assertNull(f.invoke());
609 <                assertEquals(21, f.result);
610 <                assertTrue(f.isDone());
611 <                assertFalse(f.isCancelled());
612 <                assertFalse(f.isCompletedAbnormally());
613 <                f.reinitialize();
614 <                assertNull(f.invoke());
615 <                assertEquals(21, f.result);
608 >                checkNotDone(f);
609 >
610 >                for (int i = 0; i < 3; i++) {
611 >                    assertNull(f.invoke());
612 >                    assertEquals(21, f.result);
613 >                    checkCompletedNormally(f);
614 >                    f.reinitialize();
615 >                    checkNotDone(f);
616 >                }
617              }};
618          testInvokeOnPool(mainPool(), a);
619      }
# Line 525 | Line 629 | public class RecursiveActionTest extends
629                  try {
630                      f.invoke();
631                      shouldThrow();
632 <                } catch (FJException success) {}
632 >                } catch (FJException success) {
633 >                    checkTaskThrew(f, success);
634 >                }
635              }};
636          testInvokeOnPool(mainPool(), a);
637      }
# Line 539 | Line 645 | public class RecursiveActionTest extends
645                  FibAction f = new FibAction(8);
646                  f.complete(null);
647                  assertNull(f.invoke());
542                assertTrue(f.isDone());
648                  assertEquals(0, f.result);
649 +                checkCompletedNormally(f);
650              }};
651          testInvokeOnPool(mainPool(), a);
652      }
# Line 554 | Line 660 | public class RecursiveActionTest extends
660                  FibAction f = new FibAction(8);
661                  FibAction g = new FibAction(9);
662                  invokeAll(f, g);
663 <                assertTrue(f.isDone());
663 >                checkCompletedNormally(f);
664                  assertEquals(21, f.result);
665 <                assertTrue(g.isDone());
665 >                checkCompletedNormally(g);
666                  assertEquals(34, g.result);
667              }};
668          testInvokeOnPool(mainPool(), a);
# Line 570 | Line 676 | public class RecursiveActionTest extends
676              public void realCompute() {
677                  FibAction f = new FibAction(8);
678                  invokeAll(f);
679 <                assertTrue(f.isDone());
679 >                checkCompletedNormally(f);
680                  assertEquals(21, f.result);
681              }};
682          testInvokeOnPool(mainPool(), a);
# Line 587 | Line 693 | public class RecursiveActionTest extends
693                  FibAction h = new FibAction(7);
694                  invokeAll(f, g, h);
695                  assertTrue(f.isDone());
590                assertEquals(21, f.result);
696                  assertTrue(g.isDone());
592                assertEquals(34, g.result);
697                  assertTrue(h.isDone());
698 +                checkCompletedNormally(f);
699 +                assertEquals(21, f.result);
700 +                checkCompletedNormally(g);
701 +                assertEquals(34, g.result);
702 +                checkCompletedNormally(g);
703                  assertEquals(13, h.result);
704              }};
705          testInvokeOnPool(mainPool(), a);
# Line 611 | Line 720 | public class RecursiveActionTest extends
720                  set.add(h);
721                  invokeAll(set);
722                  assertTrue(f.isDone());
614                assertEquals(21, f.result);
723                  assertTrue(g.isDone());
616                assertEquals(34, g.result);
724                  assertTrue(h.isDone());
725 +                checkCompletedNormally(f);
726 +                assertEquals(21, f.result);
727 +                checkCompletedNormally(g);
728 +                assertEquals(34, g.result);
729 +                checkCompletedNormally(g);
730                  assertEquals(13, h.result);
731              }};
732          testInvokeOnPool(mainPool(), a);
# Line 649 | Line 761 | public class RecursiveActionTest extends
761                  try {
762                      invokeAll(f, g);
763                      shouldThrow();
764 <                } catch (FJException success) {}
764 >                } catch (FJException success) {
765 >                    checkTaskThrew(g, success);
766 >                }
767              }};
768          testInvokeOnPool(mainPool(), a);
769      }
# Line 664 | Line 778 | public class RecursiveActionTest extends
778                  try {
779                      invokeAll(g);
780                      shouldThrow();
781 <                } catch (FJException success) {}
781 >                } catch (FJException success) {
782 >                    checkTaskThrew(g, success);
783 >                }
784              }};
785          testInvokeOnPool(mainPool(), a);
786      }
# Line 681 | Line 797 | public class RecursiveActionTest extends
797                  try {
798                      invokeAll(f, g, h);
799                      shouldThrow();
800 <                } catch (FJException success) {}
800 >                } catch (FJException success) {
801 >                    checkTaskThrew(g, success);
802 >                }
803              }};
804          testInvokeOnPool(mainPool(), a);
805      }
# Line 702 | Line 820 | public class RecursiveActionTest extends
820                  try {
821                      invokeAll(set);
822                      shouldThrow();
823 <                } catch (FJException success) {}
823 >                } catch (FJException success) {
824 >                    checkTaskThrew(f, success);
825 >                }
826              }};
827          testInvokeOnPool(mainPool(), a);
828      }
# Line 720 | Line 840 | public class RecursiveActionTest extends
840                  assertSame(f, f.fork());
841                  assertTrue(f.tryUnfork());
842                  helpQuiesce();
843 <                assertFalse(f.isDone());
844 <                assertTrue(g.isDone());
843 >                checkNotDone(f);
844 >                checkCompletedNormally(g);
845              }};
846          testInvokeOnPool(singletonPool(), a);
847      }
# Line 741 | Line 861 | public class RecursiveActionTest extends
861                  assertSame(f, f.fork());
862                  assertTrue(getSurplusQueuedTaskCount() > 0);
863                  helpQuiesce();
864 +                checkCompletedNormally(f);
865 +                checkCompletedNormally(g);
866 +                checkCompletedNormally(h);
867              }};
868          testInvokeOnPool(singletonPool(), a);
869      }
# Line 757 | Line 880 | public class RecursiveActionTest extends
880                  assertSame(f, f.fork());
881                  assertSame(f, peekNextLocalTask());
882                  assertNull(f.join());
883 <                assertTrue(f.isDone());
883 >                checkCompletedNormally(f);
884                  helpQuiesce();
885 +                checkCompletedNormally(f);
886 +                checkCompletedNormally(g);
887              }};
888          testInvokeOnPool(singletonPool(), a);
889      }
# Line 776 | Line 901 | public class RecursiveActionTest extends
901                  assertSame(f, f.fork());
902                  assertSame(f, pollNextLocalTask());
903                  helpQuiesce();
904 <                assertFalse(f.isDone());
904 >                checkNotDone(f);
905 >                checkCompletedNormally(g);
906              }};
907          testInvokeOnPool(singletonPool(), a);
908      }
909  
910      /**
911 <     * pollTask returns an unexecuted task
786 <     * without executing it
911 >     * pollTask returns an unexecuted task without executing it
912       */
913      public void testPollTask() {
914          RecursiveAction a = new CheckedRecursiveAction() {
# Line 794 | Line 919 | public class RecursiveActionTest extends
919                  assertSame(f, f.fork());
920                  assertSame(f, pollTask());
921                  helpQuiesce();
922 <                assertFalse(f.isDone());
923 <                assertTrue(g.isDone());
922 >                checkNotDone(f);
923 >                checkCompletedNormally(g);
924              }};
925          testInvokeOnPool(singletonPool(), a);
926      }
# Line 813 | Line 938 | public class RecursiveActionTest extends
938                  assertSame(g, peekNextLocalTask());
939                  assertNull(f.join());
940                  helpQuiesce();
941 <                assertTrue(f.isDone());
941 >                checkCompletedNormally(f);
942 >                checkCompletedNormally(g);
943              }};
944          testInvokeOnPool(asyncSingletonPool(), a);
945      }
946  
947      /**
948 <     * pollNextLocalTask returns least recent unexecuted task
949 <     * without executing it, in async mode
948 >     * pollNextLocalTask returns least recent unexecuted task without
949 >     * executing it, in async mode
950       */
951      public void testPollNextLocalTaskAsync() {
952          RecursiveAction a = new CheckedRecursiveAction() {
# Line 831 | Line 957 | public class RecursiveActionTest extends
957                  assertSame(f, f.fork());
958                  assertSame(g, pollNextLocalTask());
959                  helpQuiesce();
960 <                assertTrue(f.isDone());
961 <                assertFalse(g.isDone());
960 >                checkCompletedNormally(f);
961 >                checkNotDone(g);
962              }};
963          testInvokeOnPool(asyncSingletonPool(), a);
964      }
965  
966      /**
967 <     * pollTask returns an unexecuted task
968 <     * without executing it, in async mode
967 >     * pollTask returns an unexecuted task without executing it, in
968 >     * async mode
969       */
970      public void testPollTaskAsync() {
971          RecursiveAction a = new CheckedRecursiveAction() {
# Line 850 | Line 976 | public class RecursiveActionTest extends
976                  assertSame(f, f.fork());
977                  assertSame(g, pollTask());
978                  helpQuiesce();
979 <                assertTrue(f.isDone());
980 <                assertFalse(g.isDone());
979 >                checkCompletedNormally(f);
980 >                checkNotDone(g);
981              }};
982          testInvokeOnPool(asyncSingletonPool(), a);
983      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines