--- jsr166/src/test/tck/ForkJoinTaskTest.java 2015/10/18 16:40:57 1.49 +++ jsr166/src/test/tck/ForkJoinTaskTest.java 2017/10/21 06:52:36 1.54 @@ -5,12 +5,10 @@ */ 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.Callable; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; @@ -79,7 +77,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 +99,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 +107,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,10 +115,8 @@ 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)); - } catch (Throwable fail) { threadUnexpectedException(fail); } + assertSame(expected, a.get(randomTimeout(), randomTimeUnit())); + } catch (Exception fail) { threadUnexpectedException(fail); } } void checkCancelled(ForkJoinTask a) { @@ -144,7 +140,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 +150,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 +178,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 +189,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()); @@ -467,7 +463,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) {} }}; @@ -897,7 +893,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(); @@ -935,7 +931,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(); @@ -956,10 +952,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); @@ -1215,7 +1210,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) {} }}; @@ -1567,7 +1562,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(); @@ -1605,7 +1600,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(); @@ -1626,10 +1621,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); @@ -1655,4 +1649,42 @@ public class ForkJoinTaskTest extends JS testInvokeOnPool(mainPool(), a); } + /** + * adapt(runnable).toString() contains toString of wrapped task + */ + public void testAdapt_Runnable_toString() { + if (testImplementationDetails) { + Runnable r = () -> {}; + ForkJoinTask task = ForkJoinTask.adapt(r); + assertEquals( + identityString(task) + "[Wrapped task = " + r.toString() + "]", + task.toString()); + } + } + + /** + * adapt(runnable, x).toString() contains toString of wrapped task + */ + public void testAdapt_Runnable_withResult_toString() { + if (testImplementationDetails) { + Runnable r = () -> {}; + ForkJoinTask task = ForkJoinTask.adapt(r, ""); + assertEquals( + identityString(task) + "[Wrapped task = " + r.toString() + "]", + task.toString()); + } + } + + /** + * adapt(callable).toString() contains toString of wrapped task + */ + public void testAdapt_Callable_toString() { + if (testImplementationDetails) { + Callable c = () -> ""; + ForkJoinTask task = ForkJoinTask.adapt(c); + assertEquals( + identityString(task) + "[Wrapped task = " + c.toString() + "]", + task.toString()); + } + } }