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.21 by jsr166, Sun Nov 21 19:06:53 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 checkCompletedAbnormally(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 >                    checkCompletedAbnormally(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 >                checkCompletedAbnormally(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 >                    checkCompletedAbnormally(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 >                    Throwable cause = success.getCause();
387 >                    assertTrue(cause instanceof FJException);
388 >                    checkCompletedAbnormally(f, cause);
389 >                }
390              }};
391          testInvokeOnPool(mainPool(), a);
392      }
# Line 302 | Line 402 | public class RecursiveActionTest extends
402                  try {
403                      f.get(5L, TimeUnit.SECONDS);
404                      shouldThrow();
405 <                } catch (ExecutionException success) {}
405 >                } catch (ExecutionException success) {
406 >                    Throwable cause = success.getCause();
407 >                    assertTrue(cause instanceof FJException);
408 >                    checkCompletedAbnormally(f, cause);
409 >                }
410              }};
411          testInvokeOnPool(mainPool(), a);
412      }
# Line 316 | Line 420 | public class RecursiveActionTest extends
420                  FailingFibAction f = new FailingFibAction(8);
421                  assertSame(f, f.fork());
422                  f.quietlyJoin();
319                assertTrue(f.isDone());
320                assertTrue(f.isCompletedAbnormally());
423                  assertTrue(f.getException() instanceof FJException);
424 +                checkCompletedAbnormally(f, f.getException());
425              }};
426          testInvokeOnPool(mainPool(), a);
427      }
# Line 334 | Line 437 | public class RecursiveActionTest extends
437                  try {
438                      f.invoke();
439                      shouldThrow();
440 <                } catch (CancellationException success) {}
440 >                } catch (CancellationException success) {
441 >                    checkCancelled(f);
442 >                }
443              }};
444          testInvokeOnPool(mainPool(), a);
445      }
# Line 351 | Line 456 | public class RecursiveActionTest extends
456                  try {
457                      f.join();
458                      shouldThrow();
459 <                } catch (CancellationException success) {}
459 >                } catch (CancellationException success) {
460 >                    checkCancelled(f);
461 >                }
462              }};
463          testInvokeOnPool(mainPool(), a);
464      }
# Line 368 | Line 475 | public class RecursiveActionTest extends
475                  try {
476                      f.get();
477                      shouldThrow();
478 <                } catch (CancellationException success) {}
478 >                } catch (CancellationException success) {
479 >                    checkCancelled(f);
480 >                }
481              }};
482          testInvokeOnPool(mainPool(), a);
483      }
# Line 383 | Line 492 | public class RecursiveActionTest extends
492                  assertTrue(f.cancel(true));
493                  assertSame(f, f.fork());
494                  try {
495 <                    f.get(5L, TimeUnit.SECONDS);
495 >                    f.get(5L, SECONDS);
496                      shouldThrow();
497 <                } catch (CancellationException success) {}
497 >                } catch (CancellationException success) {
498 >                    checkCancelled(f);
499 >                }
500              }};
501          testInvokeOnPool(mainPool(), a);
502      }
# Line 400 | Line 511 | public class RecursiveActionTest extends
511                  assertTrue(f.cancel(true));
512                  assertSame(f, f.fork());
513                  f.quietlyJoin();
514 <                assertTrue(f.isDone());
404 <                assertTrue(f.isCompletedAbnormally());
405 <                assertTrue(f.isCancelled());
406 <                assertTrue(f.getException() instanceof CancellationException);
514 >                checkCancelled(f);
515              }};
516          testInvokeOnPool(mainPool(), a);
517      }
# Line 476 | Line 584 | public class RecursiveActionTest extends
584              public void realCompute() {
585                  ForkJoinWorkerThread w =
586                      (ForkJoinWorkerThread)(Thread.currentThread());
587 <                int idx = w.getPoolIndex();
588 <                assertTrue(idx >= 0);
481 <                assertTrue(idx < mainPool.getPoolSize());
587 >                assertTrue(w.getPoolIndex() >= 0);
588 >                assertTrue(w.getPoolIndex() < mainPool.getPoolSize());
589              }};
590          testInvokeOnPool(mainPool, a);
591      }
# Line 502 | Line 609 | public class RecursiveActionTest extends
609          RecursiveAction a = new CheckedRecursiveAction() {
610              public void realCompute() {
611                  FibAction f = new FibAction(8);
612 <                assertNull(f.invoke());
613 <                assertEquals(21, f.result);
614 <                assertTrue(f.isDone());
615 <                assertFalse(f.isCancelled());
616 <                assertFalse(f.isCompletedAbnormally());
617 <                f.reinitialize();
618 <                assertNull(f.invoke());
619 <                assertEquals(21, f.result);
612 >                checkNotDone(f);
613 >
614 >                for (int i = 0; i < 3; i++) {
615 >                    assertNull(f.invoke());
616 >                    assertEquals(21, f.result);
617 >                    checkCompletedNormally(f);
618 >                    f.reinitialize();
619 >                    checkNotDone(f);
620 >                }
621              }};
622          testInvokeOnPool(mainPool(), a);
623      }
# Line 525 | Line 633 | public class RecursiveActionTest extends
633                  try {
634                      f.invoke();
635                      shouldThrow();
636 <                } catch (FJException success) {}
636 >                } catch (FJException success) {
637 >                    checkCompletedAbnormally(f, success);
638 >                }
639              }};
640          testInvokeOnPool(mainPool(), a);
641      }
# Line 539 | Line 649 | public class RecursiveActionTest extends
649                  FibAction f = new FibAction(8);
650                  f.complete(null);
651                  assertNull(f.invoke());
542                assertTrue(f.isDone());
652                  assertEquals(0, f.result);
653 +                checkCompletedNormally(f);
654              }};
655          testInvokeOnPool(mainPool(), a);
656      }
# Line 554 | Line 664 | public class RecursiveActionTest extends
664                  FibAction f = new FibAction(8);
665                  FibAction g = new FibAction(9);
666                  invokeAll(f, g);
667 <                assertTrue(f.isDone());
667 >                checkCompletedNormally(f);
668                  assertEquals(21, f.result);
669 <                assertTrue(g.isDone());
669 >                checkCompletedNormally(g);
670                  assertEquals(34, g.result);
671              }};
672          testInvokeOnPool(mainPool(), a);
# Line 570 | Line 680 | public class RecursiveActionTest extends
680              public void realCompute() {
681                  FibAction f = new FibAction(8);
682                  invokeAll(f);
683 <                assertTrue(f.isDone());
683 >                checkCompletedNormally(f);
684                  assertEquals(21, f.result);
685              }};
686          testInvokeOnPool(mainPool(), a);
# Line 587 | Line 697 | public class RecursiveActionTest extends
697                  FibAction h = new FibAction(7);
698                  invokeAll(f, g, h);
699                  assertTrue(f.isDone());
590                assertEquals(21, f.result);
700                  assertTrue(g.isDone());
592                assertEquals(34, g.result);
701                  assertTrue(h.isDone());
702 +                checkCompletedNormally(f);
703 +                assertEquals(21, f.result);
704 +                checkCompletedNormally(g);
705 +                assertEquals(34, g.result);
706 +                checkCompletedNormally(g);
707                  assertEquals(13, h.result);
708              }};
709          testInvokeOnPool(mainPool(), a);
# Line 611 | Line 724 | public class RecursiveActionTest extends
724                  set.add(h);
725                  invokeAll(set);
726                  assertTrue(f.isDone());
614                assertEquals(21, f.result);
727                  assertTrue(g.isDone());
616                assertEquals(34, g.result);
728                  assertTrue(h.isDone());
729 +                checkCompletedNormally(f);
730 +                assertEquals(21, f.result);
731 +                checkCompletedNormally(g);
732 +                assertEquals(34, g.result);
733 +                checkCompletedNormally(g);
734                  assertEquals(13, h.result);
735              }};
736          testInvokeOnPool(mainPool(), a);
# Line 649 | Line 765 | public class RecursiveActionTest extends
765                  try {
766                      invokeAll(f, g);
767                      shouldThrow();
768 <                } catch (FJException success) {}
768 >                } catch (FJException success) {
769 >                    checkCompletedAbnormally(g, success);
770 >                }
771              }};
772          testInvokeOnPool(mainPool(), a);
773      }
# Line 664 | Line 782 | public class RecursiveActionTest extends
782                  try {
783                      invokeAll(g);
784                      shouldThrow();
785 <                } catch (FJException success) {}
785 >                } catch (FJException success) {
786 >                    checkCompletedAbnormally(g, success);
787 >                }
788              }};
789          testInvokeOnPool(mainPool(), a);
790      }
# Line 681 | Line 801 | public class RecursiveActionTest extends
801                  try {
802                      invokeAll(f, g, h);
803                      shouldThrow();
804 <                } catch (FJException success) {}
804 >                } catch (FJException success) {
805 >                    checkCompletedAbnormally(g, success);
806 >                }
807              }};
808          testInvokeOnPool(mainPool(), a);
809      }
# Line 702 | Line 824 | public class RecursiveActionTest extends
824                  try {
825                      invokeAll(set);
826                      shouldThrow();
827 <                } catch (FJException success) {}
827 >                } catch (FJException success) {
828 >                    checkCompletedAbnormally(f, success);
829 >                }
830              }};
831          testInvokeOnPool(mainPool(), a);
832      }
# Line 720 | Line 844 | public class RecursiveActionTest extends
844                  assertSame(f, f.fork());
845                  assertTrue(f.tryUnfork());
846                  helpQuiesce();
847 <                assertFalse(f.isDone());
848 <                assertTrue(g.isDone());
847 >                checkNotDone(f);
848 >                checkCompletedNormally(g);
849              }};
850          testInvokeOnPool(singletonPool(), a);
851      }
# Line 741 | Line 865 | public class RecursiveActionTest extends
865                  assertSame(f, f.fork());
866                  assertTrue(getSurplusQueuedTaskCount() > 0);
867                  helpQuiesce();
868 +                assertEquals(0, getSurplusQueuedTaskCount());
869 +                checkCompletedNormally(f);
870 +                checkCompletedNormally(g);
871 +                checkCompletedNormally(h);
872              }};
873          testInvokeOnPool(singletonPool(), a);
874      }
# Line 757 | Line 885 | public class RecursiveActionTest extends
885                  assertSame(f, f.fork());
886                  assertSame(f, peekNextLocalTask());
887                  assertNull(f.join());
888 <                assertTrue(f.isDone());
888 >                checkCompletedNormally(f);
889                  helpQuiesce();
890 +                checkCompletedNormally(f);
891 +                checkCompletedNormally(g);
892              }};
893          testInvokeOnPool(singletonPool(), a);
894      }
# Line 776 | Line 906 | public class RecursiveActionTest extends
906                  assertSame(f, f.fork());
907                  assertSame(f, pollNextLocalTask());
908                  helpQuiesce();
909 <                assertFalse(f.isDone());
909 >                checkNotDone(f);
910 >                checkCompletedNormally(g);
911              }};
912          testInvokeOnPool(singletonPool(), a);
913      }
914  
915      /**
916 <     * pollTask returns an unexecuted task
786 <     * without executing it
916 >     * pollTask returns an unexecuted task without executing it
917       */
918      public void testPollTask() {
919          RecursiveAction a = new CheckedRecursiveAction() {
# Line 794 | Line 924 | public class RecursiveActionTest extends
924                  assertSame(f, f.fork());
925                  assertSame(f, pollTask());
926                  helpQuiesce();
927 <                assertFalse(f.isDone());
928 <                assertTrue(g.isDone());
927 >                checkNotDone(f);
928 >                checkCompletedNormally(g);
929              }};
930          testInvokeOnPool(singletonPool(), a);
931      }
# Line 813 | Line 943 | public class RecursiveActionTest extends
943                  assertSame(g, peekNextLocalTask());
944                  assertNull(f.join());
945                  helpQuiesce();
946 <                assertTrue(f.isDone());
946 >                checkCompletedNormally(f);
947 >                checkCompletedNormally(g);
948              }};
949          testInvokeOnPool(asyncSingletonPool(), a);
950      }
951  
952      /**
953 <     * pollNextLocalTask returns least recent unexecuted task
954 <     * without executing it, in async mode
953 >     * pollNextLocalTask returns least recent unexecuted task without
954 >     * executing it, in async mode
955       */
956      public void testPollNextLocalTaskAsync() {
957          RecursiveAction a = new CheckedRecursiveAction() {
# Line 831 | Line 962 | public class RecursiveActionTest extends
962                  assertSame(f, f.fork());
963                  assertSame(g, pollNextLocalTask());
964                  helpQuiesce();
965 <                assertTrue(f.isDone());
966 <                assertFalse(g.isDone());
965 >                checkCompletedNormally(f);
966 >                checkNotDone(g);
967              }};
968          testInvokeOnPool(asyncSingletonPool(), a);
969      }
970  
971      /**
972 <     * pollTask returns an unexecuted task
973 <     * without executing it, in async mode
972 >     * pollTask returns an unexecuted task without executing it, in
973 >     * async mode
974       */
975      public void testPollTaskAsync() {
976          RecursiveAction a = new CheckedRecursiveAction() {
# Line 850 | Line 981 | public class RecursiveActionTest extends
981                  assertSame(f, f.fork());
982                  assertSame(g, pollTask());
983                  helpQuiesce();
984 <                assertTrue(f.isDone());
985 <                assertFalse(g.isDone());
984 >                checkCompletedNormally(f);
985 >                checkNotDone(g);
986              }};
987          testInvokeOnPool(asyncSingletonPool(), a);
988      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines