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

Comparing jsr166/src/test/tck/ForkJoinTaskTest.java (file contents):
Revision 1.39 by jsr166, Wed Dec 31 17:00:58 2014 UTC vs.
Revision 1.52 by jsr166, Mon May 29 19:15:02 2017 UTC

# Line 3 | Line 3
3   * Expert Group and released to the public domain, as explained at
4   * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6 +
7 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 +
9 + import java.util.Arrays;
10   import java.util.HashSet;
11   import java.util.concurrent.CancellationException;
12   import java.util.concurrent.ExecutionException;
# Line 11 | Line 15 | import java.util.concurrent.ForkJoinTask
15   import java.util.concurrent.RecursiveAction;
16   import java.util.concurrent.TimeoutException;
17   import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
18 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
19 < import static java.util.concurrent.TimeUnit.SECONDS;
20 < import junit.framework.*;
18 >
19 > import junit.framework.Test;
20 > import junit.framework.TestSuite;
21  
22   public class ForkJoinTaskTest extends JSR166TestCase {
23  
24      public static void main(String[] args) {
25 <        junit.textui.TestRunner.run(suite());
25 >        main(suite(), args);
26      }
27  
28      public static Test suite() {
# Line 44 | Line 48 | public class ForkJoinTaskTest extends JS
48      }
49  
50      private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
51 <        try {
51 >        try (PoolCleaner cleaner = cleaner(pool)) {
52              assertFalse(a.isDone());
53              assertFalse(a.isCompletedNormally());
54              assertFalse(a.isCompletedAbnormally());
# Line 60 | Line 64 | public class ForkJoinTaskTest extends JS
64              assertFalse(a.isCancelled());
65              assertNull(a.getException());
66              assertNull(a.getRawResult());
63        } finally {
64            joinPool(pool);
67          }
68      }
69  
# Line 74 | Line 76 | public class ForkJoinTaskTest extends JS
76          assertNull(a.getRawResult());
77  
78          try {
79 <            a.get(0L, SECONDS);
79 >            a.get(randomExpiredTimeout(), randomTimeUnit());
80              shouldThrow();
81          } catch (TimeoutException success) {
82          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 94 | Line 96 | public class ForkJoinTaskTest extends JS
96  
97          {
98              Thread.currentThread().interrupt();
99 <            long t0 = System.nanoTime();
99 >            long startTime = System.nanoTime();
100              assertSame(expected, a.join());
101 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
101 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
102              Thread.interrupted();
103          }
104  
105          {
106              Thread.currentThread().interrupt();
107 <            long t0 = System.nanoTime();
107 >            long startTime = System.nanoTime();
108              a.quietlyJoin();        // should be no-op
109 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
109 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
110              Thread.interrupted();
111          }
112  
# Line 112 | Line 114 | public class ForkJoinTaskTest extends JS
114          assertFalse(a.cancel(true));
115          try {
116              assertSame(expected, a.get());
117 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
116 <        try {
117 <            assertSame(expected, a.get(5L, SECONDS));
117 >            assertSame(expected, a.get(randomTimeout(), randomTimeUnit()));
118          } catch (Throwable fail) { threadUnexpectedException(fail); }
119      }
120  
# Line 137 | Line 137 | public class ForkJoinTaskTest extends JS
137          Thread.interrupted();
138  
139          {
140 <            long t0 = System.nanoTime();
140 >            long startTime = System.nanoTime();
141              a.quietlyJoin();        // should be no-op
142 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
142 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
143          }
144  
145          try {
# Line 149 | Line 149 | public class ForkJoinTaskTest extends JS
149          } catch (Throwable fail) { threadUnexpectedException(fail); }
150  
151          try {
152 <            a.get(5L, SECONDS);
152 >            a.get(randomTimeout(), randomTimeUnit());
153              shouldThrow();
154          } catch (CancellationException success) {
155          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 175 | Line 175 | public class ForkJoinTaskTest extends JS
175          Thread.interrupted();
176  
177          {
178 <            long t0 = System.nanoTime();
178 >            long startTime = System.nanoTime();
179              a.quietlyJoin();        // should be no-op
180 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
180 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
181          }
182  
183          try {
# Line 188 | Line 188 | public class ForkJoinTaskTest extends JS
188          } catch (Throwable fail) { threadUnexpectedException(fail); }
189  
190          try {
191 <            a.get(5L, SECONDS);
191 >            a.get(randomTimeout(), randomTimeUnit());
192              shouldThrow();
193          } catch (ExecutionException success) {
194              assertSame(t.getClass(), success.getCause().getClass());
# Line 214 | Line 214 | public class ForkJoinTaskTest extends JS
214              AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class,
215                                                   "controlState");
216  
217 <        private BinaryAsyncAction parent;
217 >        private volatile BinaryAsyncAction parent;
218  
219 <        private BinaryAsyncAction sibling;
219 >        private volatile BinaryAsyncAction sibling;
220  
221          protected BinaryAsyncAction() {
222          }
# Line 251 | Line 251 | public class ForkJoinTaskTest extends JS
251              super.completeExceptionally(ex);
252          }
253  
254 +        public boolean cancel(boolean mayInterruptIfRunning) {
255 +            if (super.cancel(mayInterruptIfRunning)) {
256 +                completeExceptionally(new FJException());
257 +                return true;
258 +            }
259 +            return false;
260 +        }
261 +
262          public final void complete() {
263              BinaryAsyncAction a = this;
264              for (;;) {
# Line 272 | Line 280 | public class ForkJoinTaskTest extends JS
280          }
281  
282          public final void completeExceptionally(Throwable ex) {
283 <            BinaryAsyncAction a = this;
276 <            while (!a.isCompletedAbnormally()) {
283 >            for (BinaryAsyncAction a = this;;) {
284                  a.completeThisExceptionally(ex);
285                  BinaryAsyncAction s = a.sibling;
286 <                if (s != null)
287 <                    s.cancel(false);
288 <                if (!a.onException() || (a = a.parent) == null)
286 >                if (s != null && !s.isDone())
287 >                    s.completeExceptionally(ex);
288 >                if ((a = a.parent) == null)
289                      break;
290              }
291          }
# Line 328 | Line 335 | public class ForkJoinTaskTest extends JS
335          public final boolean exec() {
336              AsyncFib f = this;
337              int n = f.number;
338 <            if (n > 1) {
339 <                while (n > 1) {
340 <                    AsyncFib p = f;
341 <                    AsyncFib r = new AsyncFib(n - 2);
342 <                    f = new AsyncFib(--n);
343 <                    p.linkSubtasks(r, f);
337 <                    r.fork();
338 <                }
339 <                f.number = n;
338 >            while (n > 1) {
339 >                AsyncFib p = f;
340 >                AsyncFib r = new AsyncFib(n - 2);
341 >                f = new AsyncFib(--n);
342 >                p.linkSubtasks(r, f);
343 >                r.fork();
344              }
345              f.complete();
346              return false;
# Line 356 | Line 360 | public class ForkJoinTaskTest extends JS
360          public final boolean exec() {
361              FailingAsyncFib f = this;
362              int n = f.number;
363 <            if (n > 1) {
364 <                while (n > 1) {
365 <                    FailingAsyncFib p = f;
366 <                    FailingAsyncFib r = new FailingAsyncFib(n - 2);
367 <                    f = new FailingAsyncFib(--n);
368 <                    p.linkSubtasks(r, f);
365 <                    r.fork();
366 <                }
367 <                f.number = n;
363 >            while (n > 1) {
364 >                FailingAsyncFib p = f;
365 >                FailingAsyncFib r = new FailingAsyncFib(n - 2);
366 >                f = new FailingAsyncFib(--n);
367 >                p.linkSubtasks(r, f);
368 >                r.fork();
369              }
370              f.complete();
371              return false;
# Line 461 | Line 462 | public class ForkJoinTaskTest extends JS
462                  AsyncFib f = new AsyncFib(8);
463                  assertSame(f, f.fork());
464                  try {
465 <                    f.get(5L, null);
465 >                    f.get(randomTimeout(), null);
466                      shouldThrow();
467                  } catch (NullPointerException success) {}
468              }};
# Line 770 | Line 771 | public class ForkJoinTaskTest extends JS
771      }
772  
773      /**
774 +     * completeExceptionally(null) surprisingly has the same effect as
775 +     * completeExceptionally(new RuntimeException())
776 +     */
777 +    public void testCompleteExceptionally_null() {
778 +        RecursiveAction a = new CheckedRecursiveAction() {
779 +            protected void realCompute() {
780 +                AsyncFib f = new AsyncFib(8);
781 +                f.completeExceptionally(null);
782 +                try {
783 +                    f.invoke();
784 +                    shouldThrow();
785 +                } catch (RuntimeException success) {
786 +                    assertSame(success.getClass(), RuntimeException.class);
787 +                    assertNull(success.getCause());
788 +                    checkCompletedAbnormally(f, success);
789 +                }
790 +            }};
791 +        testInvokeOnPool(mainPool(), a);
792 +    }
793 +
794 +    /**
795       * invokeAll(t1, t2) invokes all task arguments
796       */
797      public void testInvokeAll2() {
# Line 869 | Line 891 | public class ForkJoinTaskTest extends JS
891              protected void realCompute() {
892                  AsyncFib f = new AsyncFib(8);
893                  FailingAsyncFib g = new FailingAsyncFib(9);
894 +                ForkJoinTask[] tasks = { f, g };
895 +                shuffle(tasks);
896                  try {
897 <                    invokeAll(f, g);
897 >                    invokeAll(tasks);
898                      shouldThrow();
899                  } catch (FJException success) {
900                      checkCompletedAbnormally(g, success);
# Line 905 | Line 929 | public class ForkJoinTaskTest extends JS
929                  AsyncFib f = new AsyncFib(8);
930                  FailingAsyncFib g = new FailingAsyncFib(9);
931                  AsyncFib h = new AsyncFib(7);
932 +                ForkJoinTask[] tasks = { f, g, h };
933 +                shuffle(tasks);
934                  try {
935 <                    invokeAll(f, g, h);
935 >                    invokeAll(tasks);
936                      shouldThrow();
937                  } catch (FJException success) {
938                      checkCompletedAbnormally(g, success);
# Line 924 | Line 950 | public class ForkJoinTaskTest extends JS
950                  FailingAsyncFib f = new FailingAsyncFib(8);
951                  AsyncFib g = new AsyncFib(9);
952                  AsyncFib h = new AsyncFib(7);
953 <                HashSet set = new HashSet();
954 <                set.add(f);
929 <                set.add(g);
930 <                set.add(h);
953 >                ForkJoinTask[] tasks = { f, g, h };
954 >                shuffle(tasks);
955                  try {
956 <                    invokeAll(set);
956 >                    invokeAll(Arrays.asList(tasks));
957                      shouldThrow();
958                  } catch (FJException success) {
959                      checkCompletedAbnormally(f, success);
# Line 1185 | Line 1209 | public class ForkJoinTaskTest extends JS
1209                  AsyncFib f = new AsyncFib(8);
1210                  assertSame(f, f.fork());
1211                  try {
1212 <                    f.get(5L, null);
1212 >                    f.get(randomTimeout(), null);
1213                      shouldThrow();
1214                  } catch (NullPointerException success) {}
1215              }};
# Line 1536 | Line 1560 | public class ForkJoinTaskTest extends JS
1560              protected void realCompute() {
1561                  AsyncFib f = new AsyncFib(8);
1562                  FailingAsyncFib g = new FailingAsyncFib(9);
1563 +                ForkJoinTask[] tasks = { f, g };
1564 +                shuffle(tasks);
1565                  try {
1566 <                    invokeAll(f, g);
1566 >                    invokeAll(tasks);
1567                      shouldThrow();
1568                  } catch (FJException success) {
1569                      checkCompletedAbnormally(g, success);
# Line 1572 | Line 1598 | public class ForkJoinTaskTest extends JS
1598                  AsyncFib f = new AsyncFib(8);
1599                  FailingAsyncFib g = new FailingAsyncFib(9);
1600                  AsyncFib h = new AsyncFib(7);
1601 +                ForkJoinTask[] tasks = { f, g, h };
1602 +                shuffle(tasks);
1603                  try {
1604 <                    invokeAll(f, g, h);
1604 >                    invokeAll(tasks);
1605                      shouldThrow();
1606                  } catch (FJException success) {
1607                      checkCompletedAbnormally(g, success);
# Line 1591 | Line 1619 | public class ForkJoinTaskTest extends JS
1619                  FailingAsyncFib f = new FailingAsyncFib(8);
1620                  AsyncFib g = new AsyncFib(9);
1621                  AsyncFib h = new AsyncFib(7);
1622 <                HashSet set = new HashSet();
1623 <                set.add(f);
1596 <                set.add(g);
1597 <                set.add(h);
1622 >                ForkJoinTask[] tasks = { f, g, h };
1623 >                shuffle(tasks);
1624                  try {
1625 <                    invokeAll(set);
1625 >                    invokeAll(Arrays.asList(tasks));
1626                      shouldThrow();
1627                  } catch (FJException success) {
1628                      checkCompletedAbnormally(f, success);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines