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.43 by dl, Mon Oct 5 23:32:07 2015 UTC vs.
Revision 1.59 by jsr166, Fri Dec 4 20:29:22 2020 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.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;
# 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 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 495 | Line 499 | public class ForkJoinTaskTest extends JS
499                  AsyncFib f = new AsyncFib(8);
500                  assertSame(f, f.fork());
501                  helpQuiesce();
502 +                while (!f.isDone()) // wait out race
503 +                    ;
504                  assertEquals(21, f.number);
505                  assertEquals(0, getQueuedTaskCount());
506                  checkCompletedNormally(f);
# Line 772 | Line 778 | public class ForkJoinTaskTest extends JS
778      }
779  
780      /**
781 +     * completeExceptionally(null) surprisingly has the same effect as
782 +     * completeExceptionally(new RuntimeException())
783 +     */
784 +    public void testCompleteExceptionally_null() {
785 +        RecursiveAction a = new CheckedRecursiveAction() {
786 +            protected void realCompute() {
787 +                AsyncFib f = new AsyncFib(8);
788 +                f.completeExceptionally(null);
789 +                try {
790 +                    f.invoke();
791 +                    shouldThrow();
792 +                } catch (RuntimeException success) {
793 +                    assertSame(success.getClass(), RuntimeException.class);
794 +                    assertNull(success.getCause());
795 +                    checkCompletedAbnormally(f, success);
796 +                }
797 +            }};
798 +        testInvokeOnPool(mainPool(), a);
799 +    }
800 +
801 +    /**
802       * invokeAll(t1, t2) invokes all task arguments
803       */
804      public void testInvokeAll2() {
# Line 871 | Line 898 | public class ForkJoinTaskTest extends JS
898              protected void realCompute() {
899                  AsyncFib f = new AsyncFib(8);
900                  FailingAsyncFib g = new FailingAsyncFib(9);
901 +                ForkJoinTask[] tasks = { f, g };
902 +                shuffle(tasks);
903                  try {
904 <                    invokeAll(f, g);
904 >                    invokeAll(tasks);
905                      shouldThrow();
906                  } catch (FJException success) {
907                      checkCompletedAbnormally(g, success);
# Line 907 | Line 936 | public class ForkJoinTaskTest extends JS
936                  AsyncFib f = new AsyncFib(8);
937                  FailingAsyncFib g = new FailingAsyncFib(9);
938                  AsyncFib h = new AsyncFib(7);
939 +                ForkJoinTask[] tasks = { f, g, h };
940 +                shuffle(tasks);
941                  try {
942 <                    invokeAll(f, g, h);
942 >                    invokeAll(tasks);
943                      shouldThrow();
944                  } catch (FJException success) {
945                      checkCompletedAbnormally(g, success);
# Line 926 | Line 957 | public class ForkJoinTaskTest extends JS
957                  FailingAsyncFib f = new FailingAsyncFib(8);
958                  AsyncFib g = new AsyncFib(9);
959                  AsyncFib h = new AsyncFib(7);
960 <                HashSet set = new HashSet();
961 <                set.add(f);
931 <                set.add(g);
932 <                set.add(h);
960 >                ForkJoinTask[] tasks = { f, g, h };
961 >                shuffle(tasks);
962                  try {
963 <                    invokeAll(set);
963 >                    invokeAll(Arrays.asList(tasks));
964                      shouldThrow();
965                  } catch (FJException success) {
966                      checkCompletedAbnormally(f, success);
# Line 1187 | Line 1216 | public class ForkJoinTaskTest extends JS
1216                  AsyncFib f = new AsyncFib(8);
1217                  assertSame(f, f.fork());
1218                  try {
1219 <                    f.get(5L, null);
1219 >                    f.get(randomTimeout(), null);
1220                      shouldThrow();
1221                  } catch (NullPointerException success) {}
1222              }};
# Line 1538 | Line 1567 | public class ForkJoinTaskTest extends JS
1567              protected void realCompute() {
1568                  AsyncFib f = new AsyncFib(8);
1569                  FailingAsyncFib g = new FailingAsyncFib(9);
1570 +                ForkJoinTask[] tasks = { f, g };
1571 +                shuffle(tasks);
1572                  try {
1573 <                    invokeAll(f, g);
1573 >                    invokeAll(tasks);
1574                      shouldThrow();
1575                  } catch (FJException success) {
1576                      checkCompletedAbnormally(g, success);
# Line 1574 | Line 1605 | public class ForkJoinTaskTest extends JS
1605                  AsyncFib f = new AsyncFib(8);
1606                  FailingAsyncFib g = new FailingAsyncFib(9);
1607                  AsyncFib h = new AsyncFib(7);
1608 +                ForkJoinTask[] tasks = { f, g, h };
1609 +                shuffle(tasks);
1610                  try {
1611 <                    invokeAll(f, g, h);
1611 >                    invokeAll(tasks);
1612                      shouldThrow();
1613                  } catch (FJException success) {
1614                      checkCompletedAbnormally(g, success);
# Line 1593 | Line 1626 | public class ForkJoinTaskTest extends JS
1626                  FailingAsyncFib f = new FailingAsyncFib(8);
1627                  AsyncFib g = new AsyncFib(9);
1628                  AsyncFib h = new AsyncFib(7);
1629 <                HashSet set = new HashSet();
1630 <                set.add(f);
1598 <                set.add(g);
1599 <                set.add(h);
1629 >                ForkJoinTask[] tasks = { f, g, h };
1630 >                shuffle(tasks);
1631                  try {
1632 <                    invokeAll(set);
1632 >                    invokeAll(Arrays.asList(tasks));
1633                      shouldThrow();
1634                  } catch (FJException success) {
1635                      checkCompletedAbnormally(f, success);
# Line 1624 | Line 1655 | public class ForkJoinTaskTest extends JS
1655          testInvokeOnPool(mainPool(), a);
1656      }
1657  
1658 +    /**
1659 +     * adapt(runnable).toString() contains toString of wrapped task
1660 +     */
1661 +    public void testAdapt_Runnable_toString() {
1662 +        if (testImplementationDetails) {
1663 +            Runnable r = () -> {};
1664 +            ForkJoinTask<?> task = ForkJoinTask.adapt(r);
1665 +            assertEquals(
1666 +                identityString(task) + "[Wrapped task = " + r.toString() + "]",
1667 +                task.toString());
1668 +        }
1669 +    }
1670 +
1671 +    /**
1672 +     * adapt(runnable, x).toString() contains toString of wrapped task
1673 +     */
1674 +    public void testAdapt_Runnable_withResult_toString() {
1675 +        if (testImplementationDetails) {
1676 +            Runnable r = () -> {};
1677 +            ForkJoinTask<String> task = ForkJoinTask.adapt(r, "");
1678 +            assertEquals(
1679 +                identityString(task) + "[Wrapped task = " + r.toString() + "]",
1680 +                task.toString());
1681 +        }
1682 +    }
1683 +
1684 +    /**
1685 +     * adapt(callable).toString() contains toString of wrapped task
1686 +     */
1687 +    public void testAdapt_Callable_toString() {
1688 +        if (testImplementationDetails) {
1689 +            Callable<String> c = () -> "";
1690 +            ForkJoinTask<String> task = ForkJoinTask.adapt(c);
1691 +            assertEquals(
1692 +                identityString(task) + "[Wrapped task = " + c.toString() + "]",
1693 +                task.toString());
1694 +        }
1695 +    }
1696 +
1697 +    // adaptInterruptible deferred to its own independent change
1698 +    // https://bugs.openjdk.java.net/browse/JDK-8246587
1699 +
1700 + //     /**
1701 + //      * adaptInterruptible(callable).toString() contains toString of wrapped task
1702 + //      */
1703 + //     public void testAdaptInterruptible_Callable_toString() {
1704 + //         if (testImplementationDetails) {
1705 + //             Callable<String> c = () -> "";
1706 + //             ForkJoinTask<String> task = ForkJoinTask.adaptInterruptible(c);
1707 + //             assertEquals(
1708 + //                 identityString(task) + "[Wrapped task = " + c.toString() + "]",
1709 + //                 task.toString());
1710 + //         }
1711 + //     }
1712   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines