--- jsr166/src/test/tck/ForkJoinTaskTest.java 2010/10/24 13:41:17 1.20 +++ jsr166/src/test/tck/ForkJoinTaskTest.java 2021/01/26 13:33:06 1.60 @@ -1,24 +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 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() { @@ -26,7 +31,7 @@ public class ForkJoinTaskTest extends JS } // Runs with "mainPool" use > 1 thread. singletonPool tests use 1 - static final int mainPoolSize = + static final int mainPoolSize = Math.max(2, Runtime.getRuntime().availableProcessors()); private static ForkJoinPool mainPool() { @@ -44,12 +49,13 @@ 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()); assertFalse(a.isCancelled()); assertNull(a.getException()); + assertNull(a.getRawResult()); assertNull(pool.invoke(a)); @@ -58,11 +64,142 @@ public class ForkJoinTaskTest extends JS assertFalse(a.isCompletedAbnormally()); assertFalse(a.isCancelled()); assertNull(a.getException()); - } finally { - joinPool(pool); + assertNull(a.getRawResult()); } } + void checkNotDone(ForkJoinTask a) { + assertFalse(a.isDone()); + assertFalse(a.isCompletedNormally()); + assertFalse(a.isCompletedAbnormally()); + assertFalse(a.isCancelled()); + assertNull(a.getException()); + assertNull(a.getRawResult()); + + try { + a.get(randomExpiredTimeout(), randomTimeUnit()); + shouldThrow(); + } catch (TimeoutException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + } + + void checkCompletedNormally(ForkJoinTask a) { + checkCompletedNormally(a, null); + } + + void checkCompletedNormally(ForkJoinTask a, T expectedValue) { + assertTrue(a.isDone()); + assertFalse(a.isCancelled()); + assertTrue(a.isCompletedNormally()); + assertFalse(a.isCompletedAbnormally()); + assertNull(a.getException()); + assertSame(expectedValue, a.getRawResult()); + + { + Thread.currentThread().interrupt(); + long startTime = System.nanoTime(); + assertSame(expectedValue, a.join()); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + Thread.interrupted(); + } + + { + Thread.currentThread().interrupt(); + long startTime = System.nanoTime(); + a.quietlyJoin(); // should be no-op + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + Thread.interrupted(); + } + + assertFalse(a.cancel(false)); + assertFalse(a.cancel(true)); + + T v1 = null, v2 = null; + try { + v1 = a.get(); + v2 = a.get(randomTimeout(), randomTimeUnit()); + } catch (Throwable fail) { threadUnexpectedException(fail); } + assertSame(expectedValue, v1); + assertSame(expectedValue, v2); + } + + void checkCancelled(ForkJoinTask a) { + assertTrue(a.isDone()); + assertTrue(a.isCancelled()); + assertFalse(a.isCompletedNormally()); + assertTrue(a.isCompletedAbnormally()); + assertTrue(a.getException() instanceof CancellationException); + assertNull(a.getRawResult()); + assertTrue(a.cancel(false)); + assertTrue(a.cancel(true)); + + try { + Thread.currentThread().interrupt(); + a.join(); + shouldThrow(); + } catch (CancellationException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + Thread.interrupted(); + + { + long startTime = System.nanoTime(); + a.quietlyJoin(); // should be no-op + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + } + + try { + a.get(); + shouldThrow(); + } catch (CancellationException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + + try { + a.get(randomTimeout(), randomTimeUnit()); + shouldThrow(); + } catch (CancellationException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + } + + void checkCompletedAbnormally(ForkJoinTask a, Throwable t) { + assertTrue(a.isDone()); + assertFalse(a.isCancelled()); + assertFalse(a.isCompletedNormally()); + assertTrue(a.isCompletedAbnormally()); + assertSame(t.getClass(), a.getException().getClass()); + assertNull(a.getRawResult()); + assertFalse(a.cancel(false)); + assertFalse(a.cancel(true)); + + try { + Thread.currentThread().interrupt(); + a.join(); + shouldThrow(); + } catch (Throwable expected) { + assertSame(t.getClass(), expected.getClass()); + } + Thread.interrupted(); + + { + long startTime = System.nanoTime(); + a.quietlyJoin(); // should be no-op + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + } + + try { + a.get(); + shouldThrow(); + } catch (ExecutionException success) { + assertSame(t.getClass(), success.getCause().getClass()); + } catch (Throwable fail) { threadUnexpectedException(fail); } + + try { + a.get(randomTimeout(), randomTimeUnit()); + shouldThrow(); + } catch (ExecutionException success) { + assertSame(t.getClass(), success.getCause().getClass()); + } catch (Throwable fail) { threadUnexpectedException(fail); } + } + /* * Testing coverage notes: * @@ -71,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(); } } @@ -82,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() { } @@ -119,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 (;;) { @@ -140,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; } } @@ -196,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; @@ -215,7 +356,6 @@ public class ForkJoinTaskTest extends JS } } - static final class FailingAsyncFib extends BinaryAsyncAction { int number; public FailingAsyncFib(int n) { @@ -225,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; @@ -247,18 +384,15 @@ public class ForkJoinTaskTest extends JS /** * invoke returns when task completes normally. * isCompletedAbnormally and isCancelled return false for normally - * completed tasks. getRawResult of a RecursiveAction returns null; + * completed tasks; getRawResult returns null. */ 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); - assertTrue(f.isDone()); - assertFalse(f.isCancelled()); - assertFalse(f.isCompletedAbnormally()); - assertNull(f.getRawResult()); + checkCompletedNormally(f); }}; testInvokeOnPool(mainPool(), a); } @@ -270,14 +404,11 @@ 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); - assertTrue(f.isDone()); - assertFalse(f.isCancelled()); - assertFalse(f.isCompletedAbnormally()); - assertNull(f.getRawResult()); + checkCompletedNormally(f); }}; testInvokeOnPool(mainPool(), a); } @@ -287,13 +418,12 @@ 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()); assertEquals(21, f.number); - assertTrue(f.isDone()); - assertNull(f.getRawResult()); + checkCompletedNormally(f); }}; testInvokeOnPool(mainPool(), a); } @@ -303,12 +433,12 @@ 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()); assertEquals(21, f.number); - assertTrue(f.isDone()); + checkCompletedNormally(f); }}; testInvokeOnPool(mainPool(), a); } @@ -318,12 +448,12 @@ 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)); assertEquals(21, f.number); - assertTrue(f.isDone()); + checkCompletedNormally(f); }}; testInvokeOnPool(mainPool(), a); } @@ -333,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) {} }}; @@ -349,46 +479,48 @@ 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(); assertEquals(21, f.number); - assertTrue(f.isDone()); + checkCompletedNormally(f); }}; 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); - assertTrue(f.isDone()); assertEquals(0, getQueuedTaskCount()); + checkCompletedNormally(f); }}; 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(); shouldThrow(); - } catch (FJException success) {} + } catch (FJException success) { + checkCompletedAbnormally(f, success); + } }}; testInvokeOnPool(mainPool(), a); } @@ -398,10 +530,11 @@ 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.isDone()); + assertTrue(f.getException() instanceof FJException); + checkCompletedAbnormally(f, f.getException()); }}; testInvokeOnPool(mainPool(), a); } @@ -411,13 +544,15 @@ 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 { f.join(); shouldThrow(); - } catch (FJException success) {} + } catch (FJException success) { + checkCompletedAbnormally(f, success); + } }}; testInvokeOnPool(mainPool(), a); } @@ -427,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 { @@ -436,9 +571,7 @@ public class ForkJoinTaskTest extends JS } catch (ExecutionException success) { Throwable cause = success.getCause(); assertTrue(cause instanceof FJException); - assertTrue(f.isDone()); - assertTrue(f.isCompletedAbnormally()); - assertSame(cause, f.getException()); + checkCompletedAbnormally(f, cause); } }}; testInvokeOnPool(mainPool(), a); @@ -449,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 { @@ -458,9 +591,7 @@ public class ForkJoinTaskTest extends JS } catch (ExecutionException success) { Throwable cause = success.getCause(); assertTrue(cause instanceof FJException); - assertTrue(f.isDone()); - assertTrue(f.isCompletedAbnormally()); - assertSame(cause, f.getException()); + checkCompletedAbnormally(f, cause); } }}; testInvokeOnPool(mainPool(), a); @@ -471,13 +602,12 @@ 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(); - assertTrue(f.isDone()); - assertTrue(f.isCompletedAbnormally()); assertTrue(f.getException() instanceof FJException); + checkCompletedAbnormally(f, f.getException()); }}; testInvokeOnPool(mainPool(), a); } @@ -487,17 +617,14 @@ 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 { f.invoke(); shouldThrow(); } catch (CancellationException success) { - assertTrue(f.isDone()); - assertTrue(f.isCancelled()); - assertTrue(f.isCompletedAbnormally()); - assertTrue(f.getException() instanceof CancellationException); + checkCancelled(f); } }}; testInvokeOnPool(mainPool(), a); @@ -508,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()); @@ -516,10 +643,7 @@ public class ForkJoinTaskTest extends JS f.join(); shouldThrow(); } catch (CancellationException success) { - assertTrue(f.isDone()); - assertTrue(f.isCancelled()); - assertTrue(f.isCompletedAbnormally()); - assertTrue(f.getException() instanceof CancellationException); + checkCancelled(f); } }}; testInvokeOnPool(mainPool(), a); @@ -530,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()); @@ -538,10 +662,7 @@ public class ForkJoinTaskTest extends JS f.get(); shouldThrow(); } catch (CancellationException success) { - assertTrue(f.isDone()); - assertTrue(f.isCancelled()); - assertTrue(f.isCompletedAbnormally()); - assertTrue(f.getException() instanceof CancellationException); + checkCancelled(f); } }}; testInvokeOnPool(mainPool(), a); @@ -552,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()); @@ -560,10 +681,7 @@ public class ForkJoinTaskTest extends JS f.get(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (CancellationException success) { - assertTrue(f.isDone()); - assertTrue(f.isCancelled()); - assertTrue(f.isCompletedAbnormally()); - assertTrue(f.getException() instanceof CancellationException); + checkCancelled(f); } }}; testInvokeOnPool(mainPool(), a); @@ -574,15 +692,12 @@ 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()); f.quietlyJoin(); - assertTrue(f.isDone()); - assertTrue(f.isCompletedAbnormally()); - assertTrue(f.isCancelled()); - assertTrue(f.getException() instanceof CancellationException); + checkCancelled(f); }}; testInvokeOnPool(mainPool(), a); } @@ -593,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); @@ -604,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()); @@ -615,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); @@ -626,8 +741,8 @@ public class ForkJoinTaskTest extends JS */ public void testInForkJoinPool2() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { - assertTrue(!inForkJoinPool()); + protected void realCompute() { + assertFalse(inForkJoinPool()); }}; assertNull(a.invoke()); } @@ -637,8 +752,9 @@ public class ForkJoinTaskTest extends JS */ public void testSetRawResult() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { setRawResult(null); + assertNull(getRawResult()); }}; assertNull(a.invoke()); } @@ -648,13 +764,36 @@ 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 { f.invoke(); shouldThrow(); - } catch (FJException success) {} + } catch (FJException success) { + checkCompletedAbnormally(f, success); + } + }}; + testInvokeOnPool(mainPool(), a); + } + + /** + * 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); } @@ -664,14 +803,14 @@ public class ForkJoinTaskTest extends JS */ 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); - assertTrue(f.isDone()); assertEquals(21, f.number); - assertTrue(g.isDone()); assertEquals(34, g.number); + checkCompletedNormally(f); + checkCompletedNormally(g); }}; testInvokeOnPool(mainPool(), a); } @@ -681,10 +820,10 @@ 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); - assertTrue(f.isDone()); + checkCompletedNormally(f); assertEquals(21, f.number); }}; testInvokeOnPool(mainPool(), a); @@ -695,17 +834,17 @@ 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); invokeAll(f, g, h); - assertTrue(f.isDone()); assertEquals(21, f.number); - assertTrue(g.isDone()); assertEquals(34, g.number); - assertTrue(h.isDone()); assertEquals(13, h.number); + checkCompletedNormally(f); + checkCompletedNormally(g); + checkCompletedNormally(h); }}; testInvokeOnPool(mainPool(), a); } @@ -715,32 +854,31 @@ 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); - HashSet set = new HashSet(); + HashSet> set = new HashSet>(); set.add(f); set.add(g); set.add(h); invokeAll(set); - assertTrue(f.isDone()); assertEquals(21, f.number); - assertTrue(g.isDone()); assertEquals(34, g.number); - assertTrue(h.isDone()); assertEquals(13, h.number); + checkCompletedNormally(f); + checkCompletedNormally(g); + checkCompletedNormally(h); }}; 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; @@ -757,13 +895,17 @@ 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) {} + } catch (FJException success) { + checkCompletedAbnormally(g, success); + } }}; testInvokeOnPool(mainPool(), a); } @@ -773,12 +915,14 @@ 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); shouldThrow(); - } catch (FJException success) {} + } catch (FJException success) { + checkCompletedAbnormally(g, success); + } }}; testInvokeOnPool(mainPool(), a); } @@ -788,35 +932,39 @@ 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) {} + } catch (FJException success) { + checkCompletedAbnormally(g, success); + } }}; testInvokeOnPool(mainPool(), a); } /** - * 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) {} + } catch (FJException success) { + checkCompletedAbnormally(f, success); + } }}; testInvokeOnPool(mainPool(), a); } @@ -827,15 +975,15 @@ 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); assertSame(f, f.fork()); assertTrue(f.tryUnfork()); helpQuiesce(); - assertFalse(f.isDone()); - assertTrue(g.isDone()); + checkNotDone(f); + checkCompletedNormally(g); }}; testInvokeOnPool(singletonPool(), a); } @@ -846,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); @@ -855,6 +1003,10 @@ public class ForkJoinTaskTest extends JS assertSame(f, f.fork()); assertTrue(getSurplusQueuedTaskCount() > 0); helpQuiesce(); + assertEquals(0, getSurplusQueuedTaskCount()); + checkCompletedNormally(f); + checkCompletedNormally(g); + checkCompletedNormally(h); }}; testInvokeOnPool(singletonPool(), a); } @@ -864,52 +1016,54 @@ 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); assertSame(f, f.fork()); assertSame(f, peekNextLocalTask()); assertNull(f.join()); - assertTrue(f.isDone()); + checkCompletedNormally(f); helpQuiesce(); + checkCompletedNormally(g); }}; testInvokeOnPool(singletonPool(), a); } /** - * pollNextLocalTask returns most recent unexecuted task - * without executing it + * pollNextLocalTask returns most recent unexecuted task without + * executing it */ 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); assertSame(f, f.fork()); assertSame(f, pollNextLocalTask()); helpQuiesce(); - assertFalse(f.isDone()); + checkNotDone(f); + assertEquals(34, g.number); + checkCompletedNormally(g); }}; testInvokeOnPool(singletonPool(), a); } /** - * pollTask returns an unexecuted task - * without executing it + * pollTask returns an unexecuted task without executing it */ 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); assertSame(f, f.fork()); assertSame(f, pollTask()); helpQuiesce(); - assertFalse(f.isDone()); - assertTrue(g.isDone()); + checkNotDone(f); + checkCompletedNormally(g); }}; testInvokeOnPool(singletonPool(), a); } @@ -919,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); @@ -927,45 +1081,49 @@ public class ForkJoinTaskTest extends JS assertSame(g, peekNextLocalTask()); assertNull(f.join()); helpQuiesce(); - assertTrue(f.isDone()); + checkCompletedNormally(f); + assertEquals(34, g.number); + checkCompletedNormally(g); }}; testInvokeOnPool(asyncSingletonPool(), a); } /** - * pollNextLocalTask returns least recent unexecuted task - * without executing it, in async mode + * pollNextLocalTask returns least recent unexecuted task without + * executing it, in async mode */ 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); assertSame(f, f.fork()); assertSame(g, pollNextLocalTask()); helpQuiesce(); - assertTrue(f.isDone()); - assertFalse(g.isDone()); + assertEquals(21, f.number); + checkCompletedNormally(f); + checkNotDone(g); }}; testInvokeOnPool(asyncSingletonPool(), a); } /** - * pollTask returns an unexecuted task - * without executing it, in async mode + * pollTask returns an unexecuted task without executing it, in + * async mode */ 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); assertSame(f, f.fork()); assertSame(g, pollTask()); helpQuiesce(); - assertTrue(f.isDone()); - assertFalse(g.isDone()); + assertEquals(21, f.number); + checkCompletedNormally(f); + checkNotDone(g); }}; testInvokeOnPool(asyncSingletonPool(), a); } @@ -975,18 +1133,15 @@ public class ForkJoinTaskTest extends JS /** * invoke returns when task completes normally. * isCompletedAbnormally and isCancelled return false for normally - * completed tasks. getRawResult of a RecursiveAction returns null; + * completed tasks; getRawResult returns null. */ 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); - assertTrue(f.isDone()); - assertFalse(f.isCancelled()); - assertFalse(f.isCompletedAbnormally()); - assertNull(f.getRawResult()); + checkCompletedNormally(f); }}; testInvokeOnPool(singletonPool(), a); } @@ -998,14 +1153,11 @@ 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); - assertTrue(f.isDone()); - assertFalse(f.isCancelled()); - assertFalse(f.isCompletedAbnormally()); - assertNull(f.getRawResult()); + checkCompletedNormally(f); }}; testInvokeOnPool(singletonPool(), a); } @@ -1015,13 +1167,12 @@ 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()); assertEquals(21, f.number); - assertTrue(f.isDone()); - assertNull(f.getRawResult()); + checkCompletedNormally(f); }}; testInvokeOnPool(singletonPool(), a); } @@ -1031,12 +1182,12 @@ 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()); assertEquals(21, f.number); - assertTrue(f.isDone()); + checkCompletedNormally(f); }}; testInvokeOnPool(singletonPool(), a); } @@ -1046,12 +1197,12 @@ 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)); assertEquals(21, f.number); - assertTrue(f.isDone()); + checkCompletedNormally(f); }}; testInvokeOnPool(singletonPool(), a); } @@ -1061,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) {} }}; @@ -1077,46 +1228,46 @@ 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(); assertEquals(21, f.number); - assertTrue(f.isDone()); + checkCompletedNormally(f); }}; 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(); - assertEquals(21, f.number); - assertTrue(f.isDone()); + helpQuiesce(); assertEquals(0, getQueuedTaskCount()); + assertEquals(21, f.number); + checkCompletedNormally(f); }}; 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(); shouldThrow(); - } catch (FJException success) {} + } catch (FJException success) { + checkCompletedAbnormally(f, success); + } }}; testInvokeOnPool(singletonPool(), a); } @@ -1126,10 +1277,11 @@ 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.isDone()); + assertTrue(f.getException() instanceof FJException); + checkCompletedAbnormally(f, f.getException()); }}; testInvokeOnPool(singletonPool(), a); } @@ -1139,13 +1291,15 @@ 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 { f.join(); shouldThrow(); - } catch (FJException success) {} + } catch (FJException success) { + checkCompletedAbnormally(f, success); + } }}; testInvokeOnPool(singletonPool(), a); } @@ -1155,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 { @@ -1164,9 +1318,7 @@ public class ForkJoinTaskTest extends JS } catch (ExecutionException success) { Throwable cause = success.getCause(); assertTrue(cause instanceof FJException); - assertTrue(f.isDone()); - assertTrue(f.isCompletedAbnormally()); - assertSame(cause, f.getException()); + checkCompletedAbnormally(f, cause); } }}; testInvokeOnPool(singletonPool(), a); @@ -1177,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 { @@ -1186,9 +1338,7 @@ public class ForkJoinTaskTest extends JS } catch (ExecutionException success) { Throwable cause = success.getCause(); assertTrue(cause instanceof FJException); - assertTrue(f.isDone()); - assertTrue(f.isCompletedAbnormally()); - assertSame(cause, f.getException()); + checkCompletedAbnormally(f, cause); } }}; testInvokeOnPool(singletonPool(), a); @@ -1199,13 +1349,12 @@ 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(); - assertTrue(f.isDone()); - assertTrue(f.isCompletedAbnormally()); assertTrue(f.getException() instanceof FJException); + checkCompletedAbnormally(f, f.getException()); }}; testInvokeOnPool(singletonPool(), a); } @@ -1215,17 +1364,14 @@ 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 { f.invoke(); shouldThrow(); } catch (CancellationException success) { - assertTrue(f.isDone()); - assertTrue(f.isCancelled()); - assertTrue(f.isCompletedAbnormally()); - assertTrue(f.getException() instanceof CancellationException); + checkCancelled(f); } }}; testInvokeOnPool(singletonPool(), a); @@ -1236,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()); @@ -1244,10 +1390,7 @@ public class ForkJoinTaskTest extends JS f.join(); shouldThrow(); } catch (CancellationException success) { - assertTrue(f.isDone()); - assertTrue(f.isCancelled()); - assertTrue(f.isCompletedAbnormally()); - assertTrue(f.getException() instanceof CancellationException); + checkCancelled(f); } }}; testInvokeOnPool(singletonPool(), a); @@ -1258,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()); @@ -1266,10 +1409,7 @@ public class ForkJoinTaskTest extends JS f.get(); shouldThrow(); } catch (CancellationException success) { - assertTrue(f.isDone()); - assertTrue(f.isCancelled()); - assertTrue(f.isCompletedAbnormally()); - assertTrue(f.getException() instanceof CancellationException); + checkCancelled(f); } }}; testInvokeOnPool(singletonPool(), a); @@ -1280,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()); @@ -1288,10 +1428,7 @@ public class ForkJoinTaskTest extends JS f.get(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (CancellationException success) { - assertTrue(f.isDone()); - assertTrue(f.isCancelled()); - assertTrue(f.isCompletedAbnormally()); - assertTrue(f.getException() instanceof CancellationException); + checkCancelled(f); } }}; testInvokeOnPool(singletonPool(), a); @@ -1302,15 +1439,12 @@ 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()); f.quietlyJoin(); - assertTrue(f.isDone()); - assertTrue(f.isCompletedAbnormally()); - assertTrue(f.isCancelled()); - assertTrue(f.getException() instanceof CancellationException); + checkCancelled(f); }}; testInvokeOnPool(singletonPool(), a); } @@ -1320,13 +1454,15 @@ 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 { f.invoke(); shouldThrow(); - } catch (FJException success) {} + } catch (FJException success) { + checkCompletedAbnormally(f, success); + } }}; testInvokeOnPool(singletonPool(), a); } @@ -1336,14 +1472,14 @@ 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); - assertTrue(f.isDone()); assertEquals(21, f.number); - assertTrue(g.isDone()); assertEquals(34, g.number); + checkCompletedNormally(f); + checkCompletedNormally(g); }}; testInvokeOnPool(singletonPool(), a); } @@ -1353,10 +1489,10 @@ 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); - assertTrue(f.isDone()); + checkCompletedNormally(f); assertEquals(21, f.number); }}; testInvokeOnPool(singletonPool(), a); @@ -1367,17 +1503,17 @@ 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); invokeAll(f, g, h); - assertTrue(f.isDone()); assertEquals(21, f.number); - assertTrue(g.isDone()); assertEquals(34, g.number); - assertTrue(h.isDone()); assertEquals(13, h.number); + checkCompletedNormally(f); + checkCompletedNormally(g); + checkCompletedNormally(h); }}; testInvokeOnPool(singletonPool(), a); } @@ -1387,32 +1523,31 @@ 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); - HashSet set = new HashSet(); + HashSet> set = new HashSet>(); set.add(f); set.add(g); set.add(h); invokeAll(set); - assertTrue(f.isDone()); assertEquals(21, f.number); - assertTrue(g.isDone()); assertEquals(34, g.number); - assertTrue(h.isDone()); assertEquals(13, h.number); + checkCompletedNormally(f); + checkCompletedNormally(g); + checkCompletedNormally(h); }}; 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; @@ -1429,13 +1564,17 @@ 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) {} + } catch (FJException success) { + checkCompletedAbnormally(g, success); + } }}; testInvokeOnPool(singletonPool(), a); } @@ -1445,12 +1584,14 @@ 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); shouldThrow(); - } catch (FJException success) {} + } catch (FJException success) { + checkCompletedAbnormally(g, success); + } }}; testInvokeOnPool(singletonPool(), a); } @@ -1460,37 +1601,112 @@ 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) {} + } catch (FJException success) { + checkCompletedAbnormally(g, success); + } }}; testInvokeOnPool(singletonPool(), a); } /** - * 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) {} + } catch (FJException success) { + checkCompletedAbnormally(f, success); + } }}; 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()); +// } +// } }