--- jsr166/src/test/tck/ForkJoinTaskTest.java 2015/10/11 15:34:07 1.47 +++ jsr166/src/test/tck/ForkJoinTaskTest.java 2017/05/29 19:15:02 1.52 @@ -5,12 +5,9 @@ */ import static java.util.concurrent.TimeUnit.MILLISECONDS; -import static java.util.concurrent.TimeUnit.SECONDS; import java.util.Arrays; -import java.util.Collections; import java.util.HashSet; -import java.util.List; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; @@ -79,7 +76,7 @@ public class ForkJoinTaskTest extends JS assertNull(a.getRawResult()); try { - a.get(0L, SECONDS); + a.get(randomExpiredTimeout(), randomTimeUnit()); shouldThrow(); } catch (TimeoutException success) { } catch (Throwable fail) { threadUnexpectedException(fail); } @@ -101,7 +98,7 @@ public class ForkJoinTaskTest extends JS Thread.currentThread().interrupt(); long startTime = System.nanoTime(); assertSame(expected, a.join()); - assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); Thread.interrupted(); } @@ -109,7 +106,7 @@ public class ForkJoinTaskTest extends JS Thread.currentThread().interrupt(); long startTime = System.nanoTime(); a.quietlyJoin(); // should be no-op - assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); Thread.interrupted(); } @@ -117,9 +114,7 @@ public class ForkJoinTaskTest extends JS assertFalse(a.cancel(true)); try { assertSame(expected, a.get()); - } catch (Throwable fail) { threadUnexpectedException(fail); } - try { - assertSame(expected, a.get(5L, SECONDS)); + assertSame(expected, a.get(randomTimeout(), randomTimeUnit())); } catch (Throwable fail) { threadUnexpectedException(fail); } } @@ -144,7 +139,7 @@ public class ForkJoinTaskTest extends JS { long startTime = System.nanoTime(); a.quietlyJoin(); // should be no-op - assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } try { @@ -154,7 +149,7 @@ public class ForkJoinTaskTest extends JS } catch (Throwable fail) { threadUnexpectedException(fail); } try { - a.get(5L, SECONDS); + a.get(randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (CancellationException success) { } catch (Throwable fail) { threadUnexpectedException(fail); } @@ -182,7 +177,7 @@ public class ForkJoinTaskTest extends JS { long startTime = System.nanoTime(); a.quietlyJoin(); // should be no-op - assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } try { @@ -193,7 +188,7 @@ public class ForkJoinTaskTest extends JS } catch (Throwable fail) { threadUnexpectedException(fail); } try { - a.get(5L, SECONDS); + a.get(randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (ExecutionException success) { assertSame(t.getClass(), success.getCause().getClass()); @@ -340,15 +335,12 @@ public class ForkJoinTaskTest extends JS public final boolean exec() { AsyncFib f = this; int n = f.number; - if (n > 1) { - while (n > 1) { - AsyncFib p = f; - AsyncFib r = new AsyncFib(n - 2); - f = new AsyncFib(--n); - p.linkSubtasks(r, f); - r.fork(); - } - f.number = n; + while (n > 1) { + AsyncFib p = f; + AsyncFib r = new AsyncFib(n - 2); + f = new AsyncFib(--n); + p.linkSubtasks(r, f); + r.fork(); } f.complete(); return false; @@ -368,15 +360,12 @@ public class ForkJoinTaskTest extends JS public final boolean exec() { FailingAsyncFib f = this; int n = f.number; - if (n > 1) { - while (n > 1) { - FailingAsyncFib p = f; - FailingAsyncFib r = new FailingAsyncFib(n - 2); - f = new FailingAsyncFib(--n); - p.linkSubtasks(r, f); - r.fork(); - } - f.number = n; + while (n > 1) { + FailingAsyncFib p = f; + FailingAsyncFib r = new FailingAsyncFib(n - 2); + f = new FailingAsyncFib(--n); + p.linkSubtasks(r, f); + r.fork(); } f.complete(); return false; @@ -473,7 +462,7 @@ public class ForkJoinTaskTest extends JS AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); try { - f.get(5L, null); + f.get(randomTimeout(), null); shouldThrow(); } catch (NullPointerException success) {} }}; @@ -782,6 +771,27 @@ public class ForkJoinTaskTest extends JS } /** + * completeExceptionally(null) surprisingly has the same effect as + * completeExceptionally(new RuntimeException()) + */ + public void testCompleteExceptionally_null() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + AsyncFib f = new AsyncFib(8); + f.completeExceptionally(null); + try { + f.invoke(); + shouldThrow(); + } catch (RuntimeException success) { + assertSame(success.getClass(), RuntimeException.class); + assertNull(success.getCause()); + checkCompletedAbnormally(f, success); + } + }}; + testInvokeOnPool(mainPool(), a); + } + + /** * invokeAll(t1, t2) invokes all task arguments */ public void testInvokeAll2() { @@ -882,7 +892,7 @@ public class ForkJoinTaskTest extends JS AsyncFib f = new AsyncFib(8); FailingAsyncFib g = new FailingAsyncFib(9); ForkJoinTask[] tasks = { f, g }; - Collections.shuffle(Arrays.asList(tasks)); + shuffle(tasks); try { invokeAll(tasks); shouldThrow(); @@ -920,7 +930,7 @@ public class ForkJoinTaskTest extends JS FailingAsyncFib g = new FailingAsyncFib(9); AsyncFib h = new AsyncFib(7); ForkJoinTask[] tasks = { f, g, h }; - Collections.shuffle(Arrays.asList(tasks)); + shuffle(tasks); try { invokeAll(tasks); shouldThrow(); @@ -941,10 +951,9 @@ public class ForkJoinTaskTest extends JS AsyncFib g = new AsyncFib(9); AsyncFib h = new AsyncFib(7); ForkJoinTask[] tasks = { f, g, h }; - List taskList = Arrays.asList(tasks); - Collections.shuffle(taskList); + shuffle(tasks); try { - invokeAll(taskList); + invokeAll(Arrays.asList(tasks)); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(f, success); @@ -1200,7 +1209,7 @@ public class ForkJoinTaskTest extends JS AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); try { - f.get(5L, null); + f.get(randomTimeout(), null); shouldThrow(); } catch (NullPointerException success) {} }}; @@ -1552,7 +1561,7 @@ public class ForkJoinTaskTest extends JS AsyncFib f = new AsyncFib(8); FailingAsyncFib g = new FailingAsyncFib(9); ForkJoinTask[] tasks = { f, g }; - Collections.shuffle(Arrays.asList(tasks)); + shuffle(tasks); try { invokeAll(tasks); shouldThrow(); @@ -1590,7 +1599,7 @@ public class ForkJoinTaskTest extends JS FailingAsyncFib g = new FailingAsyncFib(9); AsyncFib h = new AsyncFib(7); ForkJoinTask[] tasks = { f, g, h }; - Collections.shuffle(Arrays.asList(tasks)); + shuffle(tasks); try { invokeAll(tasks); shouldThrow(); @@ -1611,10 +1620,9 @@ public class ForkJoinTaskTest extends JS AsyncFib g = new AsyncFib(9); AsyncFib h = new AsyncFib(7); ForkJoinTask[] tasks = { f, g, h }; - List taskList = Arrays.asList(tasks); - Collections.shuffle(taskList); + shuffle(tasks); try { - invokeAll(taskList); + invokeAll(Arrays.asList(tasks)); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(f, success);