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.49 by jsr166, Sun Oct 18 16:40:57 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 467 | 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 897 | 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 935 | 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 956 | 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);
960 <                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 1215 | 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 1567 | 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 1605 | 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 1626 | 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);
1630 <                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 1655 | 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