--- jsr166/src/test/tck/ForkJoinTaskTest.java 2010/11/22 22:45:49 1.28 +++ jsr166/src/test/tck/ForkJoinTaskTest.java 2020/12/04 20:29:22 1.59 @@ -1,26 +1,29 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * 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); } @@ -86,38 +87,40 @@ public class ForkJoinTaskTest extends JS checkCompletedNormally(a, null); } - void checkCompletedNormally(ForkJoinTask a, T expected) { + void checkCompletedNormally(ForkJoinTask a, T expectedValue) { assertTrue(a.isDone()); assertFalse(a.isCancelled()); assertTrue(a.isCompletedNormally()); assertFalse(a.isCompletedAbnormally()); assertNull(a.getException()); - assertSame(expected, a.getRawResult()); + assertSame(expectedValue, a.getRawResult()); { Thread.currentThread().interrupt(); - long t0 = System.nanoTime(); - assertSame(expected, a.join()); - assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); - assertTrue(Thread.interrupted()); + long startTime = System.nanoTime(); + assertSame(expectedValue, a.join()); + 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(Thread.interrupted()); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + Thread.interrupted(); } assertFalse(a.cancel(false)); assertFalse(a.cancel(true)); + + T v1 = null, v2 = null; try { - assertSame(expected, a.get()); - } catch (Throwable fail) { threadUnexpectedException(fail); } - try { - assertSame(expected, a.get(5L, SECONDS)); + v1 = a.get(); + v2 = a.get(randomTimeout(), randomTimeUnit()); } catch (Throwable fail) { threadUnexpectedException(fail); } + assertSame(expectedValue, v1); + assertSame(expectedValue, v2); } void checkCancelled(ForkJoinTask a) { @@ -135,13 +138,13 @@ public class ForkJoinTaskTest extends JS a.join(); shouldThrow(); } catch (CancellationException success) { - assertTrue(Thread.interrupted()); } catch (Throwable fail) { threadUnexpectedException(fail); } + 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 +154,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); } @@ -162,7 +165,7 @@ public class ForkJoinTaskTest extends JS assertFalse(a.isCancelled()); assertFalse(a.isCompletedNormally()); assertTrue(a.isCompletedAbnormally()); - assertSame(t, a.getException()); + assertSame(t.getClass(), a.getException().getClass()); assertNull(a.getRawResult()); assertFalse(a.cancel(false)); assertFalse(a.cancel(true)); @@ -172,28 +175,28 @@ public class ForkJoinTaskTest extends JS a.join(); shouldThrow(); } catch (Throwable expected) { - assertTrue(Thread.interrupted()); - assertSame(t, expected); + assertSame(t.getClass(), expected.getClass()); } + 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 { a.get(); shouldThrow(); } catch (ExecutionException success) { - assertSame(t, success.getCause()); + assertSame(t.getClass(), success.getCause().getClass()); } catch (Throwable fail) { threadUnexpectedException(fail); } try { - a.get(5L, SECONDS); + a.get(randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (ExecutionException success) { - assertSame(t, success.getCause()); + assertSame(t.getClass(), success.getCause().getClass()); } catch (Throwable fail) { threadUnexpectedException(fail); } } @@ -205,7 +208,7 @@ public class ForkJoinTaskTest extends JS * differently than supplied Recursive forms. */ - static final class FJException extends RuntimeException { + public static final class FJException extends RuntimeException { FJException() { super(); } } @@ -216,9 +219,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 +256,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 +285,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 +340,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; @@ -349,7 +356,6 @@ public class ForkJoinTaskTest extends JS } } - static final class FailingAsyncFib extends BinaryAsyncAction { int number; public FailingAsyncFib(int n) { @@ -359,15 +365,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; @@ -385,7 +388,7 @@ public class ForkJoinTaskTest extends JS */ public void testInvoke() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertNull(f.invoke()); assertEquals(21, f.number); @@ -401,7 +404,7 @@ public class ForkJoinTaskTest extends JS */ public void testQuietlyInvoke() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); f.quietlyInvoke(); assertEquals(21, f.number); @@ -415,7 +418,7 @@ public class ForkJoinTaskTest extends JS */ public void testForkJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); assertNull(f.join()); @@ -430,7 +433,7 @@ public class ForkJoinTaskTest extends JS */ public void testForkGet() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); assertNull(f.get()); @@ -445,7 +448,7 @@ public class ForkJoinTaskTest extends JS */ public void testForkTimedGet() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); @@ -460,11 +463,11 @@ public class ForkJoinTaskTest extends JS */ public void testForkTimedGetNPE() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); try { - f.get(5L, null); + f.get(randomTimeout(), null); shouldThrow(); } catch (NullPointerException success) {} }}; @@ -476,7 +479,7 @@ public class ForkJoinTaskTest extends JS */ public void testForkQuietlyJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); f.quietlyJoin(); @@ -486,17 +489,18 @@ public class ForkJoinTaskTest extends JS testInvokeOnPool(mainPool(), a); } - /** * helpQuiesce returns when tasks are complete. * getQueuedTaskCount returns 0 when quiescent */ public void testForkHelpQuiesce() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); - f.helpQuiesce(); + helpQuiesce(); + while (!f.isDone()) // wait out race + ; assertEquals(21, f.number); assertEquals(0, getQueuedTaskCount()); checkCompletedNormally(f); @@ -504,13 +508,12 @@ public class ForkJoinTaskTest extends JS testInvokeOnPool(mainPool(), a); } - /** * invoke task throws exception when task completes abnormally */ public void testAbnormalInvoke() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); try { f.invoke(); @@ -527,7 +530,7 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalQuietlyInvoke() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); f.quietlyInvoke(); assertTrue(f.getException() instanceof FJException); @@ -541,7 +544,7 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalForkJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); assertSame(f, f.fork()); try { @@ -559,7 +562,7 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalForkGet() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { FailingAsyncFib f = new FailingAsyncFib(8); assertSame(f, f.fork()); try { @@ -579,7 +582,7 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalForkTimedGet() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { FailingAsyncFib f = new FailingAsyncFib(8); assertSame(f, f.fork()); try { @@ -599,7 +602,7 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalForkQuietlyJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); assertSame(f, f.fork()); f.quietlyJoin(); @@ -614,7 +617,7 @@ public class ForkJoinTaskTest extends JS */ public void testCancelledInvoke() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertTrue(f.cancel(true)); try { @@ -632,7 +635,7 @@ public class ForkJoinTaskTest extends JS */ public void testCancelledForkJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); @@ -651,7 +654,7 @@ public class ForkJoinTaskTest extends JS */ public void testCancelledForkGet() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { AsyncFib f = new AsyncFib(8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); @@ -670,7 +673,7 @@ public class ForkJoinTaskTest extends JS */ public void testCancelledForkTimedGet() throws Exception { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { AsyncFib f = new AsyncFib(8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); @@ -689,7 +692,7 @@ public class ForkJoinTaskTest extends JS */ public void testCancelledForkQuietlyJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); @@ -705,7 +708,7 @@ public class ForkJoinTaskTest extends JS public void testGetPool() { final ForkJoinPool mainPool = mainPool(); RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { assertSame(mainPool, getPool()); }}; testInvokeOnPool(mainPool, a); @@ -716,7 +719,7 @@ public class ForkJoinTaskTest extends JS */ public void testGetPool2() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { assertNull(getPool()); }}; assertNull(a.invoke()); @@ -727,7 +730,7 @@ public class ForkJoinTaskTest extends JS */ public void testInForkJoinPool() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { assertTrue(inForkJoinPool()); }}; testInvokeOnPool(mainPool(), a); @@ -738,7 +741,7 @@ public class ForkJoinTaskTest extends JS */ public void testInForkJoinPool2() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { assertFalse(inForkJoinPool()); }}; assertNull(a.invoke()); @@ -749,7 +752,7 @@ public class ForkJoinTaskTest extends JS */ public void testSetRawResult() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { setRawResult(null); assertNull(getRawResult()); }}; @@ -761,7 +764,7 @@ public class ForkJoinTaskTest extends JS */ public void testCompleteExceptionally() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); f.completeExceptionally(new FJException()); try { @@ -775,11 +778,32 @@ 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() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); AsyncFib g = new AsyncFib(9); invokeAll(f, g); @@ -796,7 +820,7 @@ public class ForkJoinTaskTest extends JS */ public void testInvokeAll1() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); invokeAll(f); checkCompletedNormally(f); @@ -810,7 +834,7 @@ public class ForkJoinTaskTest extends JS */ public void testInvokeAll3() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); AsyncFib g = new AsyncFib(9); AsyncFib h = new AsyncFib(7); @@ -830,7 +854,7 @@ public class ForkJoinTaskTest extends JS */ public void testInvokeAllCollection() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); AsyncFib g = new AsyncFib(9); AsyncFib h = new AsyncFib(7); @@ -849,13 +873,12 @@ public class ForkJoinTaskTest extends JS testInvokeOnPool(mainPool(), a); } - /** * invokeAll(tasks) with any null task throws NPE */ public void testInvokeAllNPE() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); AsyncFib g = new AsyncFib(9); AsyncFib h = null; @@ -872,11 +895,13 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalInvokeAll2() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + 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); @@ -890,7 +915,7 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalInvokeAll1() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingAsyncFib g = new FailingAsyncFib(9); try { invokeAll(g); @@ -907,12 +932,14 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalInvokeAll3() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { 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); @@ -922,20 +949,18 @@ 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() { - public void realCompute() { + protected void realCompute() { 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); @@ -950,7 +975,7 @@ public class ForkJoinTaskTest extends JS */ public void testTryUnfork() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib g = new AsyncFib(9); assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); @@ -969,7 +994,7 @@ public class ForkJoinTaskTest extends JS */ public void testGetSurplusQueuedTaskCount() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib h = new AsyncFib(7); assertSame(h, h.fork()); AsyncFib g = new AsyncFib(9); @@ -991,7 +1016,7 @@ public class ForkJoinTaskTest extends JS */ public void testPeekNextLocalTask() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib g = new AsyncFib(9); assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); @@ -1011,7 +1036,7 @@ public class ForkJoinTaskTest extends JS */ public void testPollNextLocalTask() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib g = new AsyncFib(9); assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); @@ -1030,7 +1055,7 @@ public class ForkJoinTaskTest extends JS */ public void testPollTask() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib g = new AsyncFib(9); assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); @@ -1048,7 +1073,7 @@ public class ForkJoinTaskTest extends JS */ public void testPeekNextLocalTaskAsync() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib g = new AsyncFib(9); assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); @@ -1069,7 +1094,7 @@ public class ForkJoinTaskTest extends JS */ public void testPollNextLocalTaskAsync() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib g = new AsyncFib(9); assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); @@ -1089,7 +1114,7 @@ public class ForkJoinTaskTest extends JS */ public void testPollTaskAsync() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib g = new AsyncFib(9); assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); @@ -1112,7 +1137,7 @@ public class ForkJoinTaskTest extends JS */ public void testInvokeSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertNull(f.invoke()); assertEquals(21, f.number); @@ -1128,7 +1153,7 @@ public class ForkJoinTaskTest extends JS */ public void testQuietlyInvokeSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); f.quietlyInvoke(); assertEquals(21, f.number); @@ -1142,7 +1167,7 @@ public class ForkJoinTaskTest extends JS */ public void testForkJoinSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); assertNull(f.join()); @@ -1157,7 +1182,7 @@ public class ForkJoinTaskTest extends JS */ public void testForkGetSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); assertNull(f.get()); @@ -1172,7 +1197,7 @@ public class ForkJoinTaskTest extends JS */ public void testForkTimedGetSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); @@ -1187,11 +1212,11 @@ public class ForkJoinTaskTest extends JS */ public void testForkTimedGetNPESingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); try { - f.get(5L, null); + f.get(randomTimeout(), null); shouldThrow(); } catch (NullPointerException success) {} }}; @@ -1203,7 +1228,7 @@ public class ForkJoinTaskTest extends JS */ public void testForkQuietlyJoinSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); f.quietlyJoin(); @@ -1213,17 +1238,16 @@ public class ForkJoinTaskTest extends JS testInvokeOnPool(singletonPool(), a); } - /** * helpQuiesce returns when tasks are complete. * getQueuedTaskCount returns 0 when quiescent */ public void testForkHelpQuiesceSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); - f.helpQuiesce(); + helpQuiesce(); assertEquals(0, getQueuedTaskCount()); assertEquals(21, f.number); checkCompletedNormally(f); @@ -1231,13 +1255,12 @@ public class ForkJoinTaskTest extends JS testInvokeOnPool(singletonPool(), a); } - /** * invoke task throws exception when task completes abnormally */ public void testAbnormalInvokeSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); try { f.invoke(); @@ -1254,7 +1277,7 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalQuietlyInvokeSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); f.quietlyInvoke(); assertTrue(f.getException() instanceof FJException); @@ -1268,7 +1291,7 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalForkJoinSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); assertSame(f, f.fork()); try { @@ -1286,7 +1309,7 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalForkGetSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { FailingAsyncFib f = new FailingAsyncFib(8); assertSame(f, f.fork()); try { @@ -1306,7 +1329,7 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalForkTimedGetSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { FailingAsyncFib f = new FailingAsyncFib(8); assertSame(f, f.fork()); try { @@ -1326,7 +1349,7 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalForkQuietlyJoinSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); assertSame(f, f.fork()); f.quietlyJoin(); @@ -1341,7 +1364,7 @@ public class ForkJoinTaskTest extends JS */ public void testCancelledInvokeSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertTrue(f.cancel(true)); try { @@ -1359,7 +1382,7 @@ public class ForkJoinTaskTest extends JS */ public void testCancelledForkJoinSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); @@ -1378,7 +1401,7 @@ public class ForkJoinTaskTest extends JS */ public void testCancelledForkGetSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { AsyncFib f = new AsyncFib(8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); @@ -1397,7 +1420,7 @@ public class ForkJoinTaskTest extends JS */ public void testCancelledForkTimedGetSingleton() throws Exception { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { AsyncFib f = new AsyncFib(8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); @@ -1416,7 +1439,7 @@ public class ForkJoinTaskTest extends JS */ public void testCancelledForkQuietlyJoinSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); @@ -1431,7 +1454,7 @@ public class ForkJoinTaskTest extends JS */ public void testCompleteExceptionallySingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); f.completeExceptionally(new FJException()); try { @@ -1449,7 +1472,7 @@ public class ForkJoinTaskTest extends JS */ public void testInvokeAll2Singleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); AsyncFib g = new AsyncFib(9); invokeAll(f, g); @@ -1466,7 +1489,7 @@ public class ForkJoinTaskTest extends JS */ public void testInvokeAll1Singleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); invokeAll(f); checkCompletedNormally(f); @@ -1480,7 +1503,7 @@ public class ForkJoinTaskTest extends JS */ public void testInvokeAll3Singleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); AsyncFib g = new AsyncFib(9); AsyncFib h = new AsyncFib(7); @@ -1500,7 +1523,7 @@ public class ForkJoinTaskTest extends JS */ public void testInvokeAllCollectionSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); AsyncFib g = new AsyncFib(9); AsyncFib h = new AsyncFib(7); @@ -1519,13 +1542,12 @@ public class ForkJoinTaskTest extends JS testInvokeOnPool(singletonPool(), a); } - /** * invokeAll(tasks) with any null task throws NPE */ public void testInvokeAllNPESingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); AsyncFib g = new AsyncFib(9); AsyncFib h = null; @@ -1542,11 +1564,13 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalInvokeAll2Singleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + 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); @@ -1560,7 +1584,7 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalInvokeAll1Singleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingAsyncFib g = new FailingAsyncFib(9); try { invokeAll(g); @@ -1577,12 +1601,14 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalInvokeAll3Singleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { 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); @@ -1592,20 +1618,18 @@ 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() { - public void realCompute() { + protected void realCompute() { 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); @@ -1614,4 +1638,75 @@ public class ForkJoinTaskTest extends JS testInvokeOnPool(singletonPool(), a); } + /** + * ForkJoinTask.quietlyComplete returns when task completes + * normally without setting a value. The most recent value + * established by setRawResult(V) (or null by default) is returned + * from invoke. + */ + public void testQuietlyComplete() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + AsyncFib f = new AsyncFib(8); + f.quietlyComplete(); + assertEquals(8, f.number); + checkCompletedNormally(f); + }}; + 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()); + } + } + + // adaptInterruptible deferred to its own independent change + // https://bugs.openjdk.java.net/browse/JDK-8246587 + +// /** +// * adaptInterruptible(callable).toString() contains toString of wrapped task +// */ +// public void testAdaptInterruptible_Callable_toString() { +// if (testImplementationDetails) { +// Callable c = () -> ""; +// ForkJoinTask task = ForkJoinTask.adaptInterruptible(c); +// assertEquals( +// identityString(task) + "[Wrapped task = " + c.toString() + "]", +// task.toString()); +// } +// } }