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.25 by dl, Sun Oct 11 19:53:59 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 242 | Line 243 | public class ForkJoinTask8Test extends J
243              super.completeExceptionally(ex);
244          }
245  
246 +        public boolean cancel(boolean mayInterruptIfRunning) {
247 +            if (super.cancel(mayInterruptIfRunning)) {
248 +                completeExceptionally(new FJException());
249 +                return true;
250 +            }
251 +            return false;
252 +        }
253 +
254          public final void complete() {
255              BinaryAsyncAction a = this;
256              for (;;) {
# Line 264 | Line 273 | public class ForkJoinTask8Test extends J
273          }
274  
275          public final void completeExceptionally(Throwable ex) {
276 <            BinaryAsyncAction a = this;
268 <            while (!a.isCompletedAbnormally()) {
276 >            for (BinaryAsyncAction a = this;;) {
277                  a.completeThisExceptionally(ex);
278                  BinaryAsyncAction s = a.sibling;
279 <                if (s != null)
280 <                    s.cancel(false);
281 <                if (!a.onException() || (a = a.parent) == null)
279 >                if (s != null && !s.isDone())
280 >                    s.completeExceptionally(ex);
281 >                if ((a = a.parent) == null)
282                      break;
283              }
284          }
# Line 302 | Line 310 | public class ForkJoinTask8Test extends J
310              try {
311                  AsyncFib f = this;
312                  int n = f.number;
313 <                if (n > 1) {
314 <                    while (n > 1) {
315 <                        AsyncFib p = f;
316 <                        AsyncFib r = new AsyncFib(n - 2);
317 <                        f = new AsyncFib(--n);
318 <                        p.linkSubtasks(r, f);
311 <                        r.fork();
312 <                    }
313 <                    f.number = n;
313 >                while (n > 1) {
314 >                    AsyncFib p = f;
315 >                    AsyncFib r = new AsyncFib(n - 2);
316 >                    f = new AsyncFib(--n);
317 >                    p.linkSubtasks(r, f);
318 >                    r.fork();
319                  }
320                  f.complete();
321              }
322              catch (Throwable ex) {
323                  compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE);
324              }
325 +            if (getForkJoinTaskTag() == EXCEPTION_STATE)
326 +                throw new FJException();
327              return false;
328          }
329  
# Line 338 | Line 345 | public class ForkJoinTask8Test extends J
345          }
346  
347          public final boolean exec() {
348 <            FailingAsyncFib f = this;
349 <            int n = f.number;
350 <            if (n > 1) {
348 >            try {
349 >                FailingAsyncFib f = this;
350 >                int n = f.number;
351                  while (n > 1) {
352                      FailingAsyncFib p = f;
353                      FailingAsyncFib r = new FailingAsyncFib(n - 2);
# Line 348 | Line 355 | public class ForkJoinTask8Test extends J
355                      p.linkSubtasks(r, f);
356                      r.fork();
357                  }
358 <                f.number = n;
358 >                f.complete();
359              }
360 <            f.complete();
360 >            catch (Throwable ex) {
361 >                compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE);
362 >            }
363 >            if (getForkJoinTaskTag() == EXCEPTION_STATE)
364 >                throw new FJException();
365              return false;
366          }
367  
# Line 873 | Line 884 | public class ForkJoinTask8Test extends J
884      }
885  
886      /**
887 <     * invokeAll(t1, t2) throw exception if any task does
887 >     * invokeAll(tasks) with 1 argument throws exception if task does
888       */
889 <    public void testAbnormalInvokeAll2() {
890 <        testAbnormalInvokeAll2(mainPool());
889 >    public void testAbnormalInvokeAll1() {
890 >        testAbnormalInvokeAll1(mainPool());
891      }
892 <    public void testAbnormalInvokeAll2_Singleton() {
893 <        testAbnormalInvokeAll2(singletonPool());
892 >    public void testAbnormalInvokeAll1_Singleton() {
893 >        testAbnormalInvokeAll1(singletonPool());
894      }
895 <    public void testAbnormalInvokeAll2(ForkJoinPool pool) {
895 >    public void testAbnormalInvokeAll1(ForkJoinPool pool) {
896          RecursiveAction a = new CheckedRecursiveAction() {
897              protected void realCompute() {
887                AsyncFib f = new AsyncFib(8);
898                  FailingAsyncFib g = new FailingAsyncFib(9);
899                  try {
900 <                    invokeAll(f, g);
900 >                    invokeAll(g);
901                      shouldThrow();
902                  } catch (FJException success) {
903                      checkCompletedAbnormally(g, success);
# Line 897 | Line 907 | public class ForkJoinTask8Test extends J
907      }
908  
909      /**
910 <     * invokeAll(tasks) with 1 argument throws exception if task does
910 >     * invokeAll(t1, t2) throw exception if any task does
911       */
912 <    public void testAbnormalInvokeAll1() {
913 <        testAbnormalInvokeAll1(mainPool());
912 >    public void testAbnormalInvokeAll2() {
913 >        testAbnormalInvokeAll2(mainPool());
914      }
915 <    public void testAbnormalInvokeAll1_Singleton() {
916 <        testAbnormalInvokeAll1(singletonPool());
915 >    public void testAbnormalInvokeAll2_Singleton() {
916 >        testAbnormalInvokeAll2(singletonPool());
917      }
918 <    public void testAbnormalInvokeAll1(ForkJoinPool pool) {
918 >    public void testAbnormalInvokeAll2(ForkJoinPool pool) {
919          RecursiveAction a = new CheckedRecursiveAction() {
920              protected void realCompute() {
921 +                AsyncFib f = new AsyncFib(8);
922                  FailingAsyncFib g = new FailingAsyncFib(9);
923 +                ForkJoinTask[] tasks = { f, g };
924 +                Collections.shuffle(Arrays.asList(tasks));
925                  try {
926 <                    invokeAll(g);
926 >                    invokeAll(tasks[0], tasks[1]);
927                      shouldThrow();
928                  } catch (FJException success) {
929                      checkCompletedAbnormally(g, success);
# Line 934 | Line 947 | public class ForkJoinTask8Test extends J
947                  AsyncFib f = new AsyncFib(8);
948                  FailingAsyncFib g = new FailingAsyncFib(9);
949                  AsyncFib h = new AsyncFib(7);
950 +                ForkJoinTask[] tasks = { f, g, h };
951 +                Collections.shuffle(Arrays.asList(tasks));
952                  try {
953 <                    invokeAll(f, g, h);
953 >                    invokeAll(tasks[0], tasks[1], tasks[2]);
954                      shouldThrow();
955                  } catch (FJException success) {
956                      checkCompletedAbnormally(g, success);
# Line 960 | Line 975 | public class ForkJoinTask8Test extends J
975                  AsyncFib g = new AsyncFib(9);
976                  AsyncFib h = new AsyncFib(7);
977                  ForkJoinTask[] tasks = { f, g, h };
978 +                Collections.shuffle(Arrays.asList(tasks));
979                  try {
980                      invokeAll(Arrays.asList(tasks));
981                      shouldThrow();
# Line 1146 | Line 1162 | public class ForkJoinTask8Test extends J
1162          testInvokeOnPool(mainPool(), a);
1163      }
1164  
1165 +    // jdk9
1166 +
1167 +    /**
1168 +     * pollSubmission returns unexecuted submitted task, if present
1169 +     */
1170 +    public void testPollSubmission() {
1171 +        final CountDownLatch done = new CountDownLatch(1);
1172 +        final ForkJoinTask a = ForkJoinTask.adapt(awaiter(done));
1173 +        final ForkJoinTask b = ForkJoinTask.adapt(awaiter(done));
1174 +        final ForkJoinTask c = ForkJoinTask.adapt(awaiter(done));
1175 +        final ForkJoinPool p = singletonPool();
1176 +        try (PoolCleaner cleaner = cleaner(p, done)) {
1177 +            Thread external = new Thread(new CheckedRunnable() {
1178 +                public void realRun() {
1179 +                    p.execute(a);
1180 +                    p.execute(b);
1181 +                    p.execute(c);
1182 +                }});
1183 +            RecursiveAction s = new CheckedRecursiveAction() {
1184 +                protected void realCompute() {
1185 +                    external.start();
1186 +                    try {
1187 +                        external.join();
1188 +                    } catch (Exception ex) {
1189 +                        threadUnexpectedException(ex);
1190 +                    }
1191 +                    assertTrue(p.hasQueuedSubmissions());
1192 +                    assertTrue(Thread.currentThread() instanceof ForkJoinWorkerThread);
1193 +                    ForkJoinTask r = ForkJoinTask.pollSubmission();
1194 +                    assertTrue(r == a || r == b || r == c);
1195 +                    assertFalse(r.isDone());
1196 +                }};
1197 +            p.invoke(s);
1198 +        }
1199 +    }
1200 +
1201   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines