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

Comparing jsr166/src/test/tck/ForkJoinTask8Test.java (file contents):
Revision 1.12 by jsr166, Sat Feb 7 22:32:48 2015 UTC vs.
Revision 1.22 by dl, Thu Oct 8 23:02:21 2015 UTC

# Line 8 | Line 8 | import static java.util.concurrent.TimeU
8   import static java.util.concurrent.TimeUnit.SECONDS;
9  
10   import java.util.Arrays;
11 + import java.util.Collections;
12 + import java.util.concurrent.CountDownLatch;
13   import java.util.concurrent.ExecutionException;
14   import java.util.concurrent.ForkJoinPool;
15   import java.util.concurrent.ForkJoinTask;
16 + import java.util.concurrent.ForkJoinWorkerThread;
17   import java.util.concurrent.RecursiveAction;
18   import java.util.concurrent.TimeoutException;
19  
# Line 34 | Line 37 | public class ForkJoinTask8Test extends J
37      static final short EXCEPTION_STATE = 1;
38  
39      public static void main(String[] args) {
40 <        junit.textui.TestRunner.run(suite());
40 >        main(suite(), args);
41      }
42  
43      public static Test suite() {
# Line 71 | Line 74 | public class ForkJoinTask8Test extends J
74      }
75  
76      private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
77 <        try {
77 >        try (PoolCleaner cleaner = cleaner(pool)) {
78              assertFalse(a.isDone());
79              assertFalse(a.isCompletedNormally());
80              assertFalse(a.isCompletedAbnormally());
# Line 87 | Line 90 | public class ForkJoinTask8Test extends J
90              assertFalse(a.isCancelled());
91              assertNull(a.getException());
92              assertNull(a.getRawResult());
90        } finally {
91            joinPool(pool);
93          }
94      }
95  
# Line 125 | Line 126 | public class ForkJoinTask8Test extends J
126  
127          {
128              Thread.currentThread().interrupt();
129 <            long t0 = System.nanoTime();
129 >            long startTime = System.nanoTime();
130              assertSame(expected, a.join());
131 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
131 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
132              Thread.interrupted();
133          }
134  
135          {
136              Thread.currentThread().interrupt();
137 <            long t0 = System.nanoTime();
137 >            long startTime = System.nanoTime();
138              a.quietlyJoin();        // should be no-op
139 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
139 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
140              Thread.interrupted();
141          }
142  
# Line 171 | Line 172 | public class ForkJoinTask8Test extends J
172          Thread.interrupted();
173  
174          {
175 <            long t0 = System.nanoTime();
175 >            long startTime = System.nanoTime();
176              a.quietlyJoin();        // should be no-op
177 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
177 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
178          }
179  
180          try {
# Line 197 | Line 198 | public class ForkJoinTask8Test extends J
198  
199      abstract static class BinaryAsyncAction extends ForkJoinTask<Void> {
200  
201 <        private BinaryAsyncAction parent;
201 >        private volatile BinaryAsyncAction parent;
202  
203 <        private BinaryAsyncAction sibling;
203 >        private volatile BinaryAsyncAction sibling;
204  
205          protected BinaryAsyncAction() {
206              setForkJoinTaskTag(INITIAL_STATE);
# Line 316 | Line 317 | public class ForkJoinTask8Test extends J
317              }
318              catch (Throwable ex) {
319                  compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE);
320 +                throw new Error(ex);
321              }
322              return false;
323          }
# Line 873 | Line 875 | public class ForkJoinTask8Test extends J
875      }
876  
877      /**
878 <     * invokeAll(t1, t2) throw exception if any task does
878 >     * invokeAll(tasks) with 1 argument throws exception if task does
879       */
880 <    public void testAbnormalInvokeAll2() {
881 <        testAbnormalInvokeAll2(mainPool());
880 >    public void testAbnormalInvokeAll1() {
881 >        testAbnormalInvokeAll1(mainPool());
882      }
883 <    public void testAbnormalInvokeAll2_Singleton() {
884 <        testAbnormalInvokeAll2(singletonPool());
883 >    public void testAbnormalInvokeAll1_Singleton() {
884 >        testAbnormalInvokeAll1(singletonPool());
885      }
886 <    public void testAbnormalInvokeAll2(ForkJoinPool pool) {
886 >    public void testAbnormalInvokeAll1(ForkJoinPool pool) {
887          RecursiveAction a = new CheckedRecursiveAction() {
888              protected void realCompute() {
887                AsyncFib f = new AsyncFib(8);
889                  FailingAsyncFib g = new FailingAsyncFib(9);
890                  try {
891 <                    invokeAll(f, g);
891 >                    invokeAll(g);
892                      shouldThrow();
893                  } catch (FJException success) {
894                      checkCompletedAbnormally(g, success);
# Line 897 | Line 898 | public class ForkJoinTask8Test extends J
898      }
899  
900      /**
901 <     * invokeAll(tasks) with 1 argument throws exception if task does
901 >     * invokeAll(t1, t2) throw exception if any task does
902       */
903 <    public void testAbnormalInvokeAll1() {
904 <        testAbnormalInvokeAll1(mainPool());
903 >    public void testAbnormalInvokeAll2() {
904 >        testAbnormalInvokeAll2(mainPool());
905      }
906 <    public void testAbnormalInvokeAll1_Singleton() {
907 <        testAbnormalInvokeAll1(singletonPool());
906 >    public void testAbnormalInvokeAll2_Singleton() {
907 >        testAbnormalInvokeAll2(singletonPool());
908      }
909 <    public void testAbnormalInvokeAll1(ForkJoinPool pool) {
909 >    public void testAbnormalInvokeAll2(ForkJoinPool pool) {
910          RecursiveAction a = new CheckedRecursiveAction() {
911              protected void realCompute() {
912 +                AsyncFib f = new AsyncFib(8);
913                  FailingAsyncFib g = new FailingAsyncFib(9);
914 +                ForkJoinTask[] tasks = { f, g };
915 +                Collections.shuffle(Arrays.asList(tasks));
916                  try {
917 <                    invokeAll(g);
917 >                    invokeAll(tasks[0], tasks[1]);
918                      shouldThrow();
919                  } catch (FJException success) {
920                      checkCompletedAbnormally(g, success);
# Line 934 | Line 938 | public class ForkJoinTask8Test extends J
938                  AsyncFib f = new AsyncFib(8);
939                  FailingAsyncFib g = new FailingAsyncFib(9);
940                  AsyncFib h = new AsyncFib(7);
941 +                ForkJoinTask[] tasks = { f, g, h };
942 +                Collections.shuffle(Arrays.asList(tasks));
943                  try {
944 <                    invokeAll(f, g, h);
944 >                    invokeAll(tasks[0], tasks[1], tasks[2]);
945                      shouldThrow();
946                  } catch (FJException success) {
947                      checkCompletedAbnormally(g, success);
# Line 960 | Line 966 | public class ForkJoinTask8Test extends J
966                  AsyncFib g = new AsyncFib(9);
967                  AsyncFib h = new AsyncFib(7);
968                  ForkJoinTask[] tasks = { f, g, h };
969 +                Collections.shuffle(Arrays.asList(tasks));
970                  try {
971                      invokeAll(Arrays.asList(tasks));
972                      shouldThrow();
# Line 1146 | Line 1153 | public class ForkJoinTask8Test extends J
1153          testInvokeOnPool(mainPool(), a);
1154      }
1155  
1156 +    // jdk9
1157 +
1158 +    /**
1159 +     * pollSubmission returns unexecuted submitted task, if present
1160 +     */
1161 +    public void testPollSubmission() {
1162 +        final CountDownLatch done = new CountDownLatch(1);
1163 +        final ForkJoinTask a = ForkJoinTask.adapt(awaiter(done));
1164 +        final ForkJoinTask b = ForkJoinTask.adapt(awaiter(done));
1165 +        final ForkJoinTask c = ForkJoinTask.adapt(awaiter(done));
1166 +        final ForkJoinPool p = singletonPool();
1167 +        try (PoolCleaner cleaner = cleaner(p, done)) {
1168 +            Thread external = new Thread(new CheckedRunnable() {
1169 +                public void realRun() {
1170 +                    p.execute(a);
1171 +                    p.execute(b);
1172 +                    p.execute(c);
1173 +                }});
1174 +            RecursiveAction s = new CheckedRecursiveAction() {
1175 +                protected void realCompute() {
1176 +                    external.start();
1177 +                    try {
1178 +                        external.join();
1179 +                    } catch (Exception ex) {
1180 +                        threadUnexpectedException(ex);
1181 +                    }
1182 +                    assertTrue(p.hasQueuedSubmissions());
1183 +                    assertTrue(Thread.currentThread() instanceof ForkJoinWorkerThread);
1184 +                    ForkJoinTask r = ForkJoinTask.pollSubmission();
1185 +                    assertTrue(r == a || r == b || r == c);
1186 +                    assertFalse(r.isDone());
1187 +                }};
1188 +            p.invoke(s);
1189 +        }
1190 +    }
1191 +
1192   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines