--- jsr166/src/test/tck/ForkJoinTaskTest.java 2013/07/21 22:24:18 1.35 +++ jsr166/src/test/tck/ForkJoinTaskTest.java 2017/11/08 02:21:43 1.55 @@ -3,24 +3,27 @@ * Expert Group and released to the public domain, as explained at * http://creativecommons.org/publicdomain/zero/1.0/ */ -import java.util.concurrent.ExecutionException; + +import static java.util.concurrent.TimeUnit.MILLISECONDS; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.concurrent.Callable; import java.util.concurrent.CancellationException; +import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; -import java.util.concurrent.ForkJoinWorkerThread; import java.util.concurrent.RecursiveAction; -import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import static java.util.concurrent.TimeUnit.SECONDS; -import java.util.HashSet; -import junit.framework.*; + +import junit.framework.Test; +import junit.framework.TestSuite; public class ForkJoinTaskTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { @@ -46,7 +49,7 @@ public class ForkJoinTaskTest extends JS } private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { - try { + try (PoolCleaner cleaner = cleaner(pool)) { assertFalse(a.isDone()); assertFalse(a.isCompletedNormally()); assertFalse(a.isCompletedAbnormally()); @@ -62,8 +65,6 @@ public class ForkJoinTaskTest extends JS assertFalse(a.isCancelled()); assertNull(a.getException()); assertNull(a.getRawResult()); - } finally { - joinPool(pool); } } @@ -76,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); } @@ -96,17 +97,17 @@ public class ForkJoinTaskTest extends JS { Thread.currentThread().interrupt(); - long t0 = System.nanoTime(); + long startTime = System.nanoTime(); assertSame(expected, a.join()); - assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); Thread.interrupted(); } { Thread.currentThread().interrupt(); - long t0 = System.nanoTime(); + long startTime = System.nanoTime(); a.quietlyJoin(); // should be no-op - assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); Thread.interrupted(); } @@ -114,9 +115,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); } } @@ -139,9 +138,9 @@ public class ForkJoinTaskTest extends JS Thread.interrupted(); { - long t0 = System.nanoTime(); + long startTime = System.nanoTime(); a.quietlyJoin(); // should be no-op - assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } try { @@ -151,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); } @@ -177,9 +176,9 @@ public class ForkJoinTaskTest extends JS Thread.interrupted(); { - long t0 = System.nanoTime(); + long startTime = System.nanoTime(); a.quietlyJoin(); // should be no-op - assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } try { @@ -190,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()); @@ -216,9 +215,9 @@ public class ForkJoinTaskTest extends JS AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class, "controlState"); - private BinaryAsyncAction parent; + private volatile BinaryAsyncAction parent; - private BinaryAsyncAction sibling; + private volatile BinaryAsyncAction sibling; protected BinaryAsyncAction() { } @@ -253,6 +252,14 @@ public class ForkJoinTaskTest extends JS super.completeExceptionally(ex); } + public boolean cancel(boolean mayInterruptIfRunning) { + if (super.cancel(mayInterruptIfRunning)) { + completeExceptionally(new FJException()); + return true; + } + return false; + } + public final void complete() { BinaryAsyncAction a = this; for (;;) { @@ -274,13 +281,12 @@ public class ForkJoinTaskTest extends JS } public final void completeExceptionally(Throwable ex) { - BinaryAsyncAction a = this; - while (!a.isCompletedAbnormally()) { + for (BinaryAsyncAction a = this;;) { a.completeThisExceptionally(ex); BinaryAsyncAction s = a.sibling; - if (s != null) - s.cancel(false); - if (!a.onException() || (a = a.parent) == null) + if (s != null && !s.isDone()) + s.completeExceptionally(ex); + if ((a = a.parent) == null) break; } } @@ -330,15 +336,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; @@ -358,15 +361,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; @@ -463,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) {} }}; @@ -772,6 +772,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() { @@ -871,8 +892,10 @@ public class ForkJoinTaskTest extends JS protected void realCompute() { AsyncFib f = new AsyncFib(8); FailingAsyncFib g = new FailingAsyncFib(9); + ForkJoinTask[] tasks = { f, g }; + shuffle(tasks); try { - invokeAll(f, g); + invokeAll(tasks); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(g, success); @@ -907,8 +930,10 @@ public class ForkJoinTaskTest extends JS AsyncFib f = new AsyncFib(8); FailingAsyncFib g = new FailingAsyncFib(9); AsyncFib h = new AsyncFib(7); + ForkJoinTask[] tasks = { f, g, h }; + shuffle(tasks); try { - invokeAll(f, g, h); + invokeAll(tasks); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(g, success); @@ -918,7 +943,7 @@ public class ForkJoinTaskTest extends JS } /** - * invokeAll(collection) throws exception if any task does + * invokeAll(collection) throws exception if any task does */ public void testAbnormalInvokeAllCollection() { RecursiveAction a = new CheckedRecursiveAction() { @@ -926,12 +951,10 @@ public class ForkJoinTaskTest extends JS FailingAsyncFib f = new FailingAsyncFib(8); AsyncFib g = new AsyncFib(9); AsyncFib h = new AsyncFib(7); - HashSet set = new HashSet(); - set.add(f); - set.add(g); - set.add(h); + ForkJoinTask[] tasks = { f, g, h }; + shuffle(tasks); try { - invokeAll(set); + invokeAll(Arrays.asList(tasks)); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(f, success); @@ -1187,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) {} }}; @@ -1538,8 +1561,10 @@ public class ForkJoinTaskTest extends JS protected void realCompute() { AsyncFib f = new AsyncFib(8); FailingAsyncFib g = new FailingAsyncFib(9); + ForkJoinTask[] tasks = { f, g }; + shuffle(tasks); try { - invokeAll(f, g); + invokeAll(tasks); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(g, success); @@ -1574,8 +1599,10 @@ public class ForkJoinTaskTest extends JS AsyncFib f = new AsyncFib(8); FailingAsyncFib g = new FailingAsyncFib(9); AsyncFib h = new AsyncFib(7); + ForkJoinTask[] tasks = { f, g, h }; + shuffle(tasks); try { - invokeAll(f, g, h); + invokeAll(tasks); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(g, success); @@ -1585,7 +1612,7 @@ public class ForkJoinTaskTest extends JS } /** - * invokeAll(collection) throws exception if any task does + * invokeAll(collection) throws exception if any task does */ public void testAbnormalInvokeAllCollectionSingleton() { RecursiveAction a = new CheckedRecursiveAction() { @@ -1593,12 +1620,10 @@ public class ForkJoinTaskTest extends JS FailingAsyncFib f = new FailingAsyncFib(8); AsyncFib g = new AsyncFib(9); AsyncFib h = new AsyncFib(7); - HashSet set = new HashSet(); - set.add(f); - set.add(g); - set.add(h); + ForkJoinTask[] tasks = { f, g, h }; + shuffle(tasks); try { - invokeAll(set); + invokeAll(Arrays.asList(tasks)); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(f, success); @@ -1607,7 +1632,6 @@ public class ForkJoinTaskTest extends JS testInvokeOnPool(singletonPool(), a); } - /** * ForkJoinTask.quietlyComplete returns when task completes * normally without setting a value. The most recent value @@ -1625,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()); + } + } }