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.29 by jsr166, Mon May 29 19:15:02 2017 UTC

# Line 5 | Line 5
5   */
6  
7   import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 import static java.util.concurrent.TimeUnit.SECONDS;
8  
9   import java.util.Arrays;
10 + import java.util.concurrent.CountDownLatch;
11   import java.util.concurrent.ExecutionException;
12   import java.util.concurrent.ForkJoinPool;
13   import java.util.concurrent.ForkJoinTask;
14 + import java.util.concurrent.ForkJoinWorkerThread;
15   import java.util.concurrent.RecursiveAction;
16   import java.util.concurrent.TimeoutException;
17  
# Line 34 | Line 35 | public class ForkJoinTask8Test extends J
35      static final short EXCEPTION_STATE = 1;
36  
37      public static void main(String[] args) {
38 <        junit.textui.TestRunner.run(suite());
38 >        main(suite(), args);
39      }
40  
41      public static Test suite() {
# Line 71 | Line 72 | public class ForkJoinTask8Test extends J
72      }
73  
74      private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
75 <        try {
75 >        try (PoolCleaner cleaner = cleaner(pool)) {
76              assertFalse(a.isDone());
77              assertFalse(a.isCompletedNormally());
78              assertFalse(a.isCompletedAbnormally());
# Line 87 | Line 88 | public class ForkJoinTask8Test extends J
88              assertFalse(a.isCancelled());
89              assertNull(a.getException());
90              assertNull(a.getRawResult());
90        } finally {
91            joinPool(pool);
91          }
92      }
93  
# Line 100 | Line 99 | public class ForkJoinTask8Test extends J
99          assertNull(a.getException());
100          assertNull(a.getRawResult());
101          if (a instanceof BinaryAsyncAction)
102 <            assertTrue(((BinaryAsyncAction)a).getForkJoinTaskTag() == INITIAL_STATE);
102 >            assertEquals(INITIAL_STATE,
103 >                         ((BinaryAsyncAction)a).getForkJoinTaskTag());
104  
105          try {
106 <            a.get(0L, SECONDS);
106 >            a.get(randomExpiredTimeout(), randomTimeUnit());
107              shouldThrow();
108          } catch (TimeoutException success) {
109          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 121 | Line 121 | public class ForkJoinTask8Test extends J
121          assertNull(a.getException());
122          assertSame(expected, a.getRawResult());
123          if (a instanceof BinaryAsyncAction)
124 <            assertTrue(((BinaryAsyncAction)a).getForkJoinTaskTag() == COMPLETE_STATE);
124 >            assertEquals(COMPLETE_STATE,
125 >                         ((BinaryAsyncAction)a).getForkJoinTaskTag());
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) < LONG_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) < LONG_DELAY_MS);
140              Thread.interrupted();
141          }
142  
# Line 143 | Line 144 | public class ForkJoinTask8Test extends J
144          assertFalse(a.cancel(true));
145          try {
146              assertSame(expected, a.get());
147 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
147 <        try {
148 <            assertSame(expected, a.get(5L, SECONDS));
147 >            assertSame(expected, a.get(randomTimeout(), randomTimeUnit()));
148          } catch (Throwable fail) { threadUnexpectedException(fail); }
149      }
150  
# Line 171 | Line 170 | public class ForkJoinTask8Test extends J
170          Thread.interrupted();
171  
172          {
173 <            long t0 = System.nanoTime();
173 >            long startTime = System.nanoTime();
174              a.quietlyJoin();        // should be no-op
175 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
175 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
176          }
177  
178          try {
# Line 184 | Line 183 | public class ForkJoinTask8Test extends J
183          } catch (Throwable fail) { threadUnexpectedException(fail); }
184  
185          try {
186 <            a.get(5L, SECONDS);
186 >            a.get(randomTimeout(), randomTimeUnit());
187              shouldThrow();
188          } catch (ExecutionException success) {
189              assertSame(t.getClass(), success.getCause().getClass());
# Line 197 | Line 196 | public class ForkJoinTask8Test extends J
196  
197      abstract static class BinaryAsyncAction extends ForkJoinTask<Void> {
198  
199 <        private BinaryAsyncAction parent;
199 >        private volatile BinaryAsyncAction parent;
200  
201 <        private BinaryAsyncAction sibling;
201 >        private volatile BinaryAsyncAction sibling;
202  
203          protected BinaryAsyncAction() {
204              setForkJoinTaskTag(INITIAL_STATE);
# Line 242 | Line 241 | public class ForkJoinTask8Test extends J
241              super.completeExceptionally(ex);
242          }
243  
244 +        public boolean cancel(boolean mayInterruptIfRunning) {
245 +            if (super.cancel(mayInterruptIfRunning)) {
246 +                completeExceptionally(new FJException());
247 +                return true;
248 +            }
249 +            return false;
250 +        }
251 +
252          public final void complete() {
253              BinaryAsyncAction a = this;
254              for (;;) {
# Line 264 | Line 271 | public class ForkJoinTask8Test extends J
271          }
272  
273          public final void completeExceptionally(Throwable ex) {
274 <            BinaryAsyncAction a = this;
268 <            while (!a.isCompletedAbnormally()) {
274 >            for (BinaryAsyncAction a = this;;) {
275                  a.completeThisExceptionally(ex);
276                  BinaryAsyncAction s = a.sibling;
277 <                if (s != null)
278 <                    s.cancel(false);
279 <                if (!a.onException() || (a = a.parent) == null)
277 >                if (s != null && !s.isDone())
278 >                    s.completeExceptionally(ex);
279 >                if ((a = a.parent) == null)
280                      break;
281              }
282          }
# Line 302 | Line 308 | public class ForkJoinTask8Test extends J
308              try {
309                  AsyncFib f = this;
310                  int n = f.number;
311 <                if (n > 1) {
312 <                    while (n > 1) {
313 <                        AsyncFib p = f;
314 <                        AsyncFib r = new AsyncFib(n - 2);
315 <                        f = new AsyncFib(--n);
316 <                        p.linkSubtasks(r, f);
311 <                        r.fork();
312 <                    }
313 <                    f.number = n;
311 >                while (n > 1) {
312 >                    AsyncFib p = f;
313 >                    AsyncFib r = new AsyncFib(n - 2);
314 >                    f = new AsyncFib(--n);
315 >                    p.linkSubtasks(r, f);
316 >                    r.fork();
317                  }
318                  f.complete();
319              }
320              catch (Throwable ex) {
321                  compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE);
322              }
323 +            if (getForkJoinTaskTag() == EXCEPTION_STATE)
324 +                throw new FJException();
325              return false;
326          }
327  
# Line 338 | Line 343 | public class ForkJoinTask8Test extends J
343          }
344  
345          public final boolean exec() {
346 <            FailingAsyncFib f = this;
347 <            int n = f.number;
348 <            if (n > 1) {
346 >            try {
347 >                FailingAsyncFib f = this;
348 >                int n = f.number;
349                  while (n > 1) {
350                      FailingAsyncFib p = f;
351                      FailingAsyncFib r = new FailingAsyncFib(n - 2);
# Line 348 | Line 353 | public class ForkJoinTask8Test extends J
353                      p.linkSubtasks(r, f);
354                      r.fork();
355                  }
356 <                f.number = n;
356 >                f.complete();
357 >            }
358 >            catch (Throwable ex) {
359 >                compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE);
360              }
361 <            f.complete();
361 >            if (getForkJoinTaskTag() == EXCEPTION_STATE)
362 >                throw new FJException();
363              return false;
364          }
365  
# Line 476 | Line 485 | public class ForkJoinTask8Test extends J
485                  AsyncFib f = new AsyncFib(8);
486                  assertSame(f, f.fork());
487                  try {
488 <                    f.get(5L, null);
488 >                    f.get(randomTimeout(), null);
489                      shouldThrow();
490                  } catch (NullPointerException success) {}
491              }};
# Line 873 | Line 882 | public class ForkJoinTask8Test extends J
882      }
883  
884      /**
885 <     * invokeAll(t1, t2) throw exception if any task does
885 >     * invokeAll(tasks) with 1 argument throws exception if task does
886       */
887 <    public void testAbnormalInvokeAll2() {
888 <        testAbnormalInvokeAll2(mainPool());
887 >    public void testAbnormalInvokeAll1() {
888 >        testAbnormalInvokeAll1(mainPool());
889      }
890 <    public void testAbnormalInvokeAll2_Singleton() {
891 <        testAbnormalInvokeAll2(singletonPool());
890 >    public void testAbnormalInvokeAll1_Singleton() {
891 >        testAbnormalInvokeAll1(singletonPool());
892      }
893 <    public void testAbnormalInvokeAll2(ForkJoinPool pool) {
893 >    public void testAbnormalInvokeAll1(ForkJoinPool pool) {
894          RecursiveAction a = new CheckedRecursiveAction() {
895              protected void realCompute() {
887                AsyncFib f = new AsyncFib(8);
896                  FailingAsyncFib g = new FailingAsyncFib(9);
897                  try {
898 <                    invokeAll(f, g);
898 >                    invokeAll(g);
899                      shouldThrow();
900                  } catch (FJException success) {
901                      checkCompletedAbnormally(g, success);
# Line 897 | Line 905 | public class ForkJoinTask8Test extends J
905      }
906  
907      /**
908 <     * invokeAll(tasks) with 1 argument throws exception if task does
908 >     * invokeAll(t1, t2) throw exception if any task does
909       */
910 <    public void testAbnormalInvokeAll1() {
911 <        testAbnormalInvokeAll1(mainPool());
910 >    public void testAbnormalInvokeAll2() {
911 >        testAbnormalInvokeAll2(mainPool());
912      }
913 <    public void testAbnormalInvokeAll1_Singleton() {
914 <        testAbnormalInvokeAll1(singletonPool());
913 >    public void testAbnormalInvokeAll2_Singleton() {
914 >        testAbnormalInvokeAll2(singletonPool());
915      }
916 <    public void testAbnormalInvokeAll1(ForkJoinPool pool) {
916 >    public void testAbnormalInvokeAll2(ForkJoinPool pool) {
917          RecursiveAction a = new CheckedRecursiveAction() {
918              protected void realCompute() {
919 +                AsyncFib f = new AsyncFib(8);
920                  FailingAsyncFib g = new FailingAsyncFib(9);
921 +                ForkJoinTask[] tasks = { f, g };
922 +                shuffle(tasks);
923                  try {
924 <                    invokeAll(g);
924 >                    invokeAll(tasks[0], tasks[1]);
925                      shouldThrow();
926                  } catch (FJException success) {
927                      checkCompletedAbnormally(g, success);
# Line 934 | Line 945 | public class ForkJoinTask8Test extends J
945                  AsyncFib f = new AsyncFib(8);
946                  FailingAsyncFib g = new FailingAsyncFib(9);
947                  AsyncFib h = new AsyncFib(7);
948 +                ForkJoinTask[] tasks = { f, g, h };
949 +                shuffle(tasks);
950                  try {
951 <                    invokeAll(f, g, h);
951 >                    invokeAll(tasks[0], tasks[1], tasks[2]);
952                      shouldThrow();
953                  } catch (FJException success) {
954                      checkCompletedAbnormally(g, success);
# Line 960 | Line 973 | public class ForkJoinTask8Test extends J
973                  AsyncFib g = new AsyncFib(9);
974                  AsyncFib h = new AsyncFib(7);
975                  ForkJoinTask[] tasks = { f, g, h };
976 +                shuffle(tasks);
977                  try {
978                      invokeAll(Arrays.asList(tasks));
979                      shouldThrow();
# Line 1146 | Line 1160 | public class ForkJoinTask8Test extends J
1160          testInvokeOnPool(mainPool(), a);
1161      }
1162  
1163 +    // jdk9
1164 +
1165 +    /**
1166 +     * pollSubmission returns unexecuted submitted task, if present
1167 +     */
1168 +    public void testPollSubmission() {
1169 +        final CountDownLatch done = new CountDownLatch(1);
1170 +        final ForkJoinTask a = ForkJoinTask.adapt(awaiter(done));
1171 +        final ForkJoinTask b = ForkJoinTask.adapt(awaiter(done));
1172 +        final ForkJoinTask c = ForkJoinTask.adapt(awaiter(done));
1173 +        final ForkJoinPool p = singletonPool();
1174 +        try (PoolCleaner cleaner = cleaner(p, done)) {
1175 +            Thread external = new Thread(new CheckedRunnable() {
1176 +                public void realRun() {
1177 +                    p.execute(a);
1178 +                    p.execute(b);
1179 +                    p.execute(c);
1180 +                }});
1181 +            RecursiveAction s = new CheckedRecursiveAction() {
1182 +                protected void realCompute() {
1183 +                    external.start();
1184 +                    try {
1185 +                        external.join();
1186 +                    } catch (Exception ex) {
1187 +                        threadUnexpectedException(ex);
1188 +                    }
1189 +                    assertTrue(p.hasQueuedSubmissions());
1190 +                    assertTrue(Thread.currentThread() instanceof ForkJoinWorkerThread);
1191 +                    ForkJoinTask r = ForkJoinTask.pollSubmission();
1192 +                    assertTrue(r == a || r == b || r == c);
1193 +                    assertFalse(r.isDone());
1194 +                }};
1195 +            p.invoke(s);
1196 +        }
1197 +    }
1198 +
1199   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines