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.45 by jsr166, Wed Oct 7 22:39:31 2015 UTC vs.
Revision 1.56 by jsr166, Sun Jul 22 20:47:22 2018 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;
11 import java.util.Collections;
10   import java.util.HashSet;
11 < import java.util.List;
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 79 | 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 89 | 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 startTime = System.nanoTime();
101 <            assertSame(expected, a.join());
102 <            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
101 >            assertSame(expectedValue, a.join());
102 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
103              Thread.interrupted();
104          }
105  
# Line 109 | Line 107 | public class ForkJoinTaskTest extends JS
107              Thread.currentThread().interrupt();
108              long startTime = System.nanoTime();
109              a.quietlyJoin();        // should be no-op
110 <            assertTrue(millisElapsedSince(startTime) < 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); }
121 <        try {
122 <            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 144 | Line 144 | public class ForkJoinTaskTest extends JS
144          {
145              long startTime = System.nanoTime();
146              a.quietlyJoin();        // should be no-op
147 <            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
147 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
148          }
149  
150          try {
# Line 154 | 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 182 | Line 182 | public class ForkJoinTaskTest extends JS
182          {
183              long startTime = System.nanoTime();
184              a.quietlyJoin();        // should be no-op
185 <            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
185 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
186          }
187  
188          try {
# Line 193 | 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 256 | 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 277 | Line 285 | public class ForkJoinTaskTest extends JS
285          }
286  
287          public final void completeExceptionally(Throwable ex) {
288 <            BinaryAsyncAction a = this;
281 <            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 333 | 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);
342 <                    r.fork();
343 <                }
344 <                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 361 | 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);
370 <                    r.fork();
371 <                }
372 <                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 466 | 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 775 | 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 875 | Line 897 | public class ForkJoinTaskTest extends JS
897                  AsyncFib f = new AsyncFib(8);
898                  FailingAsyncFib g = new FailingAsyncFib(9);
899                  ForkJoinTask[] tasks = { f, g };
900 <                Collections.shuffle(Arrays.asList(tasks));
900 >                shuffle(tasks);
901                  try {
902                      invokeAll(tasks);
903                      shouldThrow();
# Line 913 | Line 935 | public class ForkJoinTaskTest extends JS
935                  FailingAsyncFib g = new FailingAsyncFib(9);
936                  AsyncFib h = new AsyncFib(7);
937                  ForkJoinTask[] tasks = { f, g, h };
938 <                Collections.shuffle(Arrays.asList(tasks));
938 >                shuffle(tasks);
939                  try {
940                      invokeAll(tasks);
941                      shouldThrow();
# Line 934 | Line 956 | public class ForkJoinTaskTest extends JS
956                  AsyncFib g = new AsyncFib(9);
957                  AsyncFib h = new AsyncFib(7);
958                  ForkJoinTask[] tasks = { f, g, h };
959 <                List taskList = Arrays.asList(tasks);
938 <                Collections.shuffle(taskList);
959 >                shuffle(tasks);
960                  try {
961 <                    invokeAll(taskList);
961 >                    invokeAll(Arrays.asList(tasks));
962                      shouldThrow();
963                  } catch (FJException success) {
964                      checkCompletedAbnormally(f, success);
# Line 1193 | 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 1545 | Line 1566 | public class ForkJoinTaskTest extends JS
1566                  AsyncFib f = new AsyncFib(8);
1567                  FailingAsyncFib g = new FailingAsyncFib(9);
1568                  ForkJoinTask[] tasks = { f, g };
1569 <                Collections.shuffle(Arrays.asList(tasks));
1569 >                shuffle(tasks);
1570                  try {
1571                      invokeAll(tasks);
1572                      shouldThrow();
# Line 1583 | Line 1604 | public class ForkJoinTaskTest extends JS
1604                  FailingAsyncFib g = new FailingAsyncFib(9);
1605                  AsyncFib h = new AsyncFib(7);
1606                  ForkJoinTask[] tasks = { f, g, h };
1607 <                Collections.shuffle(Arrays.asList(tasks));
1607 >                shuffle(tasks);
1608                  try {
1609                      invokeAll(tasks);
1610                      shouldThrow();
# Line 1604 | Line 1625 | public class ForkJoinTaskTest extends JS
1625                  AsyncFib g = new AsyncFib(9);
1626                  AsyncFib h = new AsyncFib(7);
1627                  ForkJoinTask[] tasks = { f, g, h };
1628 <                List taskList = Arrays.asList(tasks);
1608 <                Collections.shuffle(taskList);
1628 >                shuffle(tasks);
1629                  try {
1630 <                    invokeAll(taskList);
1630 >                    invokeAll(Arrays.asList(tasks));
1631                      shouldThrow();
1632                  } catch (FJException success) {
1633                      checkCompletedAbnormally(f, success);
# Line 1633 | 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