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.35 by dl, Sun Jul 21 22:24:18 2013 UTC vs.
Revision 1.56 by jsr166, Sun Jul 22 20:47:22 2018 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 < import java.util.concurrent.ExecutionException;
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.Callable;
12   import java.util.concurrent.CancellationException;
13 + import java.util.concurrent.ExecutionException;
14   import java.util.concurrent.ForkJoinPool;
15   import java.util.concurrent.ForkJoinTask;
10 import java.util.concurrent.ForkJoinWorkerThread;
16   import java.util.concurrent.RecursiveAction;
12 import java.util.concurrent.TimeUnit;
17   import java.util.concurrent.TimeoutException;
18   import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
19 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
20 < import static java.util.concurrent.TimeUnit.SECONDS;
21 < import java.util.HashSet;
18 < import junit.framework.*;
19 >
20 > import junit.framework.Test;
21 > import junit.framework.TestSuite;
22  
23   public class ForkJoinTaskTest extends JSR166TestCase {
24  
25      public static void main(String[] args) {
26 <        junit.textui.TestRunner.run(suite());
26 >        main(suite(), args);
27      }
28  
29      public static Test suite() {
# Line 46 | Line 49 | public class ForkJoinTaskTest extends JS
49      }
50  
51      private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
52 <        try {
52 >        try (PoolCleaner cleaner = cleaner(pool)) {
53              assertFalse(a.isDone());
54              assertFalse(a.isCompletedNormally());
55              assertFalse(a.isCompletedAbnormally());
# Line 62 | Line 65 | public class ForkJoinTaskTest extends JS
65              assertFalse(a.isCancelled());
66              assertNull(a.getException());
67              assertNull(a.getRawResult());
65        } finally {
66            joinPool(pool);
68          }
69      }
70  
# Line 76 | Line 77 | public class ForkJoinTaskTest extends JS
77          assertNull(a.getRawResult());
78  
79          try {
80 <            a.get(0L, SECONDS);
80 >            a.get(randomExpiredTimeout(), randomTimeUnit());
81              shouldThrow();
82          } catch (TimeoutException success) {
83          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 86 | Line 87 | public class ForkJoinTaskTest extends JS
87          checkCompletedNormally(a, null);
88      }
89  
90 <    <T> void checkCompletedNormally(ForkJoinTask<T> a, T expected) {
90 >    <T> void checkCompletedNormally(ForkJoinTask<T> a, T expectedValue) {
91          assertTrue(a.isDone());
92          assertFalse(a.isCancelled());
93          assertTrue(a.isCompletedNormally());
94          assertFalse(a.isCompletedAbnormally());
95          assertNull(a.getException());
96 <        assertSame(expected, a.getRawResult());
96 >        assertSame(expectedValue, a.getRawResult());
97  
98          {
99              Thread.currentThread().interrupt();
100 <            long t0 = System.nanoTime();
101 <            assertSame(expected, a.join());
102 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
100 >            long startTime = System.nanoTime();
101 >            assertSame(expectedValue, a.join());
102 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
103              Thread.interrupted();
104          }
105  
106          {
107              Thread.currentThread().interrupt();
108 <            long t0 = System.nanoTime();
108 >            long startTime = System.nanoTime();
109              a.quietlyJoin();        // should be no-op
110 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
110 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
111              Thread.interrupted();
112          }
113  
114          assertFalse(a.cancel(false));
115          assertFalse(a.cancel(true));
116 +
117 +        T v1 = null, v2 = null;
118          try {
119 <            assertSame(expected, a.get());
120 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
118 <        try {
119 <            assertSame(expected, a.get(5L, SECONDS));
119 >            v1 = a.get();
120 >            v2 = a.get(randomTimeout(), randomTimeUnit());
121          } catch (Throwable fail) { threadUnexpectedException(fail); }
122 +        assertSame(expectedValue, v1);
123 +        assertSame(expectedValue, v2);
124      }
125  
126      void checkCancelled(ForkJoinTask a) {
# Line 139 | Line 142 | public class ForkJoinTaskTest extends JS
142          Thread.interrupted();
143  
144          {
145 <            long t0 = System.nanoTime();
145 >            long startTime = System.nanoTime();
146              a.quietlyJoin();        // should be no-op
147 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
147 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
148          }
149  
150          try {
# Line 151 | Line 154 | public class ForkJoinTaskTest extends JS
154          } catch (Throwable fail) { threadUnexpectedException(fail); }
155  
156          try {
157 <            a.get(5L, SECONDS);
157 >            a.get(randomTimeout(), randomTimeUnit());
158              shouldThrow();
159          } catch (CancellationException success) {
160          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 177 | Line 180 | public class ForkJoinTaskTest extends JS
180          Thread.interrupted();
181  
182          {
183 <            long t0 = System.nanoTime();
183 >            long startTime = System.nanoTime();
184              a.quietlyJoin();        // should be no-op
185 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
185 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
186          }
187  
188          try {
# Line 190 | Line 193 | public class ForkJoinTaskTest extends JS
193          } catch (Throwable fail) { threadUnexpectedException(fail); }
194  
195          try {
196 <            a.get(5L, SECONDS);
196 >            a.get(randomTimeout(), randomTimeUnit());
197              shouldThrow();
198          } catch (ExecutionException success) {
199              assertSame(t.getClass(), success.getCause().getClass());
# Line 216 | Line 219 | public class ForkJoinTaskTest extends JS
219              AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class,
220                                                   "controlState");
221  
222 <        private BinaryAsyncAction parent;
222 >        private volatile BinaryAsyncAction parent;
223  
224 <        private BinaryAsyncAction sibling;
224 >        private volatile BinaryAsyncAction sibling;
225  
226          protected BinaryAsyncAction() {
227          }
# Line 253 | Line 256 | public class ForkJoinTaskTest extends JS
256              super.completeExceptionally(ex);
257          }
258  
259 +        public boolean cancel(boolean mayInterruptIfRunning) {
260 +            if (super.cancel(mayInterruptIfRunning)) {
261 +                completeExceptionally(new FJException());
262 +                return true;
263 +            }
264 +            return false;
265 +        }
266 +
267          public final void complete() {
268              BinaryAsyncAction a = this;
269              for (;;) {
# Line 274 | Line 285 | public class ForkJoinTaskTest extends JS
285          }
286  
287          public final void completeExceptionally(Throwable ex) {
288 <            BinaryAsyncAction a = this;
278 <            while (!a.isCompletedAbnormally()) {
288 >            for (BinaryAsyncAction a = this;;) {
289                  a.completeThisExceptionally(ex);
290                  BinaryAsyncAction s = a.sibling;
291 <                if (s != null)
292 <                    s.cancel(false);
293 <                if (!a.onException() || (a = a.parent) == null)
291 >                if (s != null && !s.isDone())
292 >                    s.completeExceptionally(ex);
293 >                if ((a = a.parent) == null)
294                      break;
295              }
296          }
# Line 330 | Line 340 | public class ForkJoinTaskTest extends JS
340          public final boolean exec() {
341              AsyncFib f = this;
342              int n = f.number;
343 <            if (n > 1) {
344 <                while (n > 1) {
345 <                    AsyncFib p = f;
346 <                    AsyncFib r = new AsyncFib(n - 2);
347 <                    f = new AsyncFib(--n);
348 <                    p.linkSubtasks(r, f);
339 <                    r.fork();
340 <                }
341 <                f.number = n;
343 >            while (n > 1) {
344 >                AsyncFib p = f;
345 >                AsyncFib r = new AsyncFib(n - 2);
346 >                f = new AsyncFib(--n);
347 >                p.linkSubtasks(r, f);
348 >                r.fork();
349              }
350              f.complete();
351              return false;
# Line 358 | Line 365 | public class ForkJoinTaskTest extends JS
365          public final boolean exec() {
366              FailingAsyncFib f = this;
367              int n = f.number;
368 <            if (n > 1) {
369 <                while (n > 1) {
370 <                    FailingAsyncFib p = f;
371 <                    FailingAsyncFib r = new FailingAsyncFib(n - 2);
372 <                    f = new FailingAsyncFib(--n);
373 <                    p.linkSubtasks(r, f);
367 <                    r.fork();
368 <                }
369 <                f.number = n;
368 >            while (n > 1) {
369 >                FailingAsyncFib p = f;
370 >                FailingAsyncFib r = new FailingAsyncFib(n - 2);
371 >                f = new FailingAsyncFib(--n);
372 >                p.linkSubtasks(r, f);
373 >                r.fork();
374              }
375              f.complete();
376              return false;
# Line 463 | Line 467 | public class ForkJoinTaskTest extends JS
467                  AsyncFib f = new AsyncFib(8);
468                  assertSame(f, f.fork());
469                  try {
470 <                    f.get(5L, null);
470 >                    f.get(randomTimeout(), null);
471                      shouldThrow();
472                  } catch (NullPointerException success) {}
473              }};
# Line 772 | Line 776 | public class ForkJoinTaskTest extends JS
776      }
777  
778      /**
779 +     * completeExceptionally(null) surprisingly has the same effect as
780 +     * completeExceptionally(new RuntimeException())
781 +     */
782 +    public void testCompleteExceptionally_null() {
783 +        RecursiveAction a = new CheckedRecursiveAction() {
784 +            protected void realCompute() {
785 +                AsyncFib f = new AsyncFib(8);
786 +                f.completeExceptionally(null);
787 +                try {
788 +                    f.invoke();
789 +                    shouldThrow();
790 +                } catch (RuntimeException success) {
791 +                    assertSame(success.getClass(), RuntimeException.class);
792 +                    assertNull(success.getCause());
793 +                    checkCompletedAbnormally(f, success);
794 +                }
795 +            }};
796 +        testInvokeOnPool(mainPool(), a);
797 +    }
798 +
799 +    /**
800       * invokeAll(t1, t2) invokes all task arguments
801       */
802      public void testInvokeAll2() {
# Line 871 | Line 896 | public class ForkJoinTaskTest extends JS
896              protected void realCompute() {
897                  AsyncFib f = new AsyncFib(8);
898                  FailingAsyncFib g = new FailingAsyncFib(9);
899 +                ForkJoinTask[] tasks = { f, g };
900 +                shuffle(tasks);
901                  try {
902 <                    invokeAll(f, g);
902 >                    invokeAll(tasks);
903                      shouldThrow();
904                  } catch (FJException success) {
905                      checkCompletedAbnormally(g, success);
# Line 907 | Line 934 | public class ForkJoinTaskTest extends JS
934                  AsyncFib f = new AsyncFib(8);
935                  FailingAsyncFib g = new FailingAsyncFib(9);
936                  AsyncFib h = new AsyncFib(7);
937 +                ForkJoinTask[] tasks = { f, g, h };
938 +                shuffle(tasks);
939                  try {
940 <                    invokeAll(f, g, h);
940 >                    invokeAll(tasks);
941                      shouldThrow();
942                  } catch (FJException success) {
943                      checkCompletedAbnormally(g, success);
# Line 918 | Line 947 | public class ForkJoinTaskTest extends JS
947      }
948  
949      /**
950 <     * invokeAll(collection)  throws exception if any task does
950 >     * invokeAll(collection) throws exception if any task does
951       */
952      public void testAbnormalInvokeAllCollection() {
953          RecursiveAction a = new CheckedRecursiveAction() {
# Line 926 | Line 955 | public class ForkJoinTaskTest extends JS
955                  FailingAsyncFib f = new FailingAsyncFib(8);
956                  AsyncFib g = new AsyncFib(9);
957                  AsyncFib h = new AsyncFib(7);
958 <                HashSet set = new HashSet();
959 <                set.add(f);
931 <                set.add(g);
932 <                set.add(h);
958 >                ForkJoinTask[] tasks = { f, g, h };
959 >                shuffle(tasks);
960                  try {
961 <                    invokeAll(set);
961 >                    invokeAll(Arrays.asList(tasks));
962                      shouldThrow();
963                  } catch (FJException success) {
964                      checkCompletedAbnormally(f, success);
# Line 1187 | Line 1214 | public class ForkJoinTaskTest extends JS
1214                  AsyncFib f = new AsyncFib(8);
1215                  assertSame(f, f.fork());
1216                  try {
1217 <                    f.get(5L, null);
1217 >                    f.get(randomTimeout(), null);
1218                      shouldThrow();
1219                  } catch (NullPointerException success) {}
1220              }};
# Line 1538 | Line 1565 | public class ForkJoinTaskTest extends JS
1565              protected void realCompute() {
1566                  AsyncFib f = new AsyncFib(8);
1567                  FailingAsyncFib g = new FailingAsyncFib(9);
1568 +                ForkJoinTask[] tasks = { f, g };
1569 +                shuffle(tasks);
1570                  try {
1571 <                    invokeAll(f, g);
1571 >                    invokeAll(tasks);
1572                      shouldThrow();
1573                  } catch (FJException success) {
1574                      checkCompletedAbnormally(g, success);
# Line 1574 | Line 1603 | public class ForkJoinTaskTest extends JS
1603                  AsyncFib f = new AsyncFib(8);
1604                  FailingAsyncFib g = new FailingAsyncFib(9);
1605                  AsyncFib h = new AsyncFib(7);
1606 +                ForkJoinTask[] tasks = { f, g, h };
1607 +                shuffle(tasks);
1608                  try {
1609 <                    invokeAll(f, g, h);
1609 >                    invokeAll(tasks);
1610                      shouldThrow();
1611                  } catch (FJException success) {
1612                      checkCompletedAbnormally(g, success);
# Line 1585 | Line 1616 | public class ForkJoinTaskTest extends JS
1616      }
1617  
1618      /**
1619 <     * invokeAll(collection)  throws exception if any task does
1619 >     * invokeAll(collection) throws exception if any task does
1620       */
1621      public void testAbnormalInvokeAllCollectionSingleton() {
1622          RecursiveAction a = new CheckedRecursiveAction() {
# Line 1593 | Line 1624 | public class ForkJoinTaskTest extends JS
1624                  FailingAsyncFib f = new FailingAsyncFib(8);
1625                  AsyncFib g = new AsyncFib(9);
1626                  AsyncFib h = new AsyncFib(7);
1627 <                HashSet set = new HashSet();
1628 <                set.add(f);
1598 <                set.add(g);
1599 <                set.add(h);
1627 >                ForkJoinTask[] tasks = { f, g, h };
1628 >                shuffle(tasks);
1629                  try {
1630 <                    invokeAll(set);
1630 >                    invokeAll(Arrays.asList(tasks));
1631                      shouldThrow();
1632                  } catch (FJException success) {
1633                      checkCompletedAbnormally(f, success);
# Line 1607 | Line 1636 | public class ForkJoinTaskTest extends JS
1636          testInvokeOnPool(singletonPool(), a);
1637      }
1638  
1610
1639      /**
1640       * ForkJoinTask.quietlyComplete returns when task completes
1641       * normally without setting a value. The most recent value
# Line 1625 | Line 1653 | public class ForkJoinTaskTest extends JS
1653          testInvokeOnPool(mainPool(), a);
1654      }
1655  
1656 +    /**
1657 +     * adapt(runnable).toString() contains toString of wrapped task
1658 +     */
1659 +    public void testAdapt_Runnable_toString() {
1660 +        if (testImplementationDetails) {
1661 +            Runnable r = () -> {};
1662 +            ForkJoinTask<?> task = ForkJoinTask.adapt(r);
1663 +            assertEquals(
1664 +                identityString(task) + "[Wrapped task = " + r.toString() + "]",
1665 +                task.toString());
1666 +        }
1667 +    }
1668 +
1669 +    /**
1670 +     * adapt(runnable, x).toString() contains toString of wrapped task
1671 +     */
1672 +    public void testAdapt_Runnable_withResult_toString() {
1673 +        if (testImplementationDetails) {
1674 +            Runnable r = () -> {};
1675 +            ForkJoinTask<String> task = ForkJoinTask.adapt(r, "");
1676 +            assertEquals(
1677 +                identityString(task) + "[Wrapped task = " + r.toString() + "]",
1678 +                task.toString());
1679 +        }
1680 +    }
1681 +
1682 +    /**
1683 +     * adapt(callable).toString() contains toString of wrapped task
1684 +     */
1685 +    public void testAdapt_Callable_toString() {
1686 +        if (testImplementationDetails) {
1687 +            Callable<String> c = () -> "";
1688 +            ForkJoinTask<String> task = ForkJoinTask.adapt(c);
1689 +            assertEquals(
1690 +                identityString(task) + "[Wrapped task = " + c.toString() + "]",
1691 +                task.toString());
1692 +        }
1693 +    }
1694   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines