--- jsr166/src/test/tck/ForkJoinTaskTest.java 2010/09/13 07:22:29 1.12 +++ jsr166/src/test/tck/ForkJoinTaskTest.java 2016/08/24 22:22:39 1.51 @@ -1,41 +1,204 @@ /* * 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.*; -import java.util.concurrent.Executor; -import java.util.concurrent.Executors; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.AbstractExecutorService; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.Callable; -import java.util.concurrent.Future; -import java.util.concurrent.ExecutionException; + +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.SECONDS; + +import java.util.Arrays; +import java.util.HashSet; import java.util.concurrent.CancellationException; -import java.util.concurrent.RejectedExecutionException; +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.RecursiveTask; -import java.util.concurrent.TimeUnit; -import junit.framework.*; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.*; -import java.util.*; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; + +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() { return new TestSuite(ForkJoinTaskTest.class); } - /** + // Runs with "mainPool" use > 1 thread. singletonPool tests use 1 + static final int mainPoolSize = + Math.max(2, Runtime.getRuntime().availableProcessors()); + + private static ForkJoinPool mainPool() { + return new ForkJoinPool(mainPoolSize); + } + + private static ForkJoinPool singletonPool() { + return new ForkJoinPool(1); + } + + private static ForkJoinPool asyncSingletonPool() { + return new ForkJoinPool(1, + ForkJoinPool.defaultForkJoinWorkerThreadFactory, + null, true); + } + + private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { + 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)); + + assertTrue(a.isDone()); + assertTrue(a.isCompletedNormally()); + assertFalse(a.isCompletedAbnormally()); + assertFalse(a.isCancelled()); + assertNull(a.getException()); + 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(0L, SECONDS); + shouldThrow(); + } catch (TimeoutException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + } + + void checkCompletedNormally(ForkJoinTask a) { + checkCompletedNormally(a, null); + } + + void checkCompletedNormally(ForkJoinTask a, T expected) { + assertTrue(a.isDone()); + assertFalse(a.isCancelled()); + assertTrue(a.isCompletedNormally()); + assertFalse(a.isCompletedAbnormally()); + assertNull(a.getException()); + assertSame(expected, a.getRawResult()); + + { + Thread.currentThread().interrupt(); + long startTime = System.nanoTime(); + assertSame(expected, a.join()); + assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); + Thread.interrupted(); + } + + { + Thread.currentThread().interrupt(); + long startTime = System.nanoTime(); + a.quietlyJoin(); // should be no-op + assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); + Thread.interrupted(); + } + + assertFalse(a.cancel(false)); + assertFalse(a.cancel(true)); + try { + assertSame(expected, a.get()); + } catch (Throwable fail) { threadUnexpectedException(fail); } + try { + assertSame(expected, a.get(5L, SECONDS)); + } catch (Throwable fail) { threadUnexpectedException(fail); } + } + + 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) < SMALL_DELAY_MS); + } + + try { + a.get(); + shouldThrow(); + } catch (CancellationException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + + try { + a.get(5L, SECONDS); + 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) < SMALL_DELAY_MS); + } + + try { + a.get(); + shouldThrow(); + } catch (ExecutionException success) { + assertSame(t.getClass(), success.getCause().getClass()); + } catch (Throwable fail) { threadUnexpectedException(fail); } + + try { + a.get(5L, SECONDS); + shouldThrow(); + } catch (ExecutionException success) { + assertSame(t.getClass(), success.getCause().getClass()); + } catch (Throwable fail) { threadUnexpectedException(fail); } + } + + /* * Testing coverage notes: * * To test extension methods and overrides, most tests use @@ -43,25 +206,20 @@ public class ForkJoinTaskTest extends JS * differently than supplied Recursive forms. */ - static final ForkJoinPool mainPool = new ForkJoinPool(); - static final ForkJoinPool singletonPool = new ForkJoinPool(1); - static final ForkJoinPool asyncSingletonPool = - new ForkJoinPool(1, ForkJoinPool.defaultForkJoinWorkerThreadFactory, - null, true); - static final class FJException extends RuntimeException { + public static final class FJException extends RuntimeException { FJException() { super(); } } - static abstract class BinaryAsyncAction extends ForkJoinTask { + abstract static class BinaryAsyncAction extends ForkJoinTask { private volatile int controlState; static final AtomicIntegerFieldUpdater controlStateUpdater = AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class, "controlState"); - private BinaryAsyncAction parent; + private volatile BinaryAsyncAction parent; - private BinaryAsyncAction sibling; + private volatile BinaryAsyncAction sibling; protected BinaryAsyncAction() { } @@ -96,6 +254,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 (;;) { @@ -117,13 +283,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; } } @@ -173,15 +338,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; @@ -192,7 +354,6 @@ public class ForkJoinTaskTest extends JS } } - static final class FailingAsyncFib extends BinaryAsyncAction { int number; public FailingAsyncFib(int n) { @@ -202,15 +363,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; @@ -224,20 +382,17 @@ 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 RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); - f.invoke(); - threadAssertTrue(f.number == 21); - threadAssertTrue(f.isDone()); - threadAssertFalse(f.isCancelled()); - threadAssertFalse(f.isCompletedAbnormally()); - threadAssertTrue(f.getRawResult() == null); + assertNull(f.invoke()); + assertEquals(21, f.number); + checkCompletedNormally(f); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -246,447 +401,456 @@ public class ForkJoinTaskTest extends JS * completed tasks */ public void testQuietlyInvoke() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); f.quietlyInvoke(); - threadAssertTrue(f.number == 21); - threadAssertTrue(f.isDone()); - threadAssertFalse(f.isCancelled()); - threadAssertFalse(f.isCompletedAbnormally()); - threadAssertTrue(f.getRawResult() == null); + assertEquals(21, f.number); + checkCompletedNormally(f); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** * join of a forked task returns when task completes */ public void testForkJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); - f.fork(); - f.join(); - threadAssertTrue(f.number == 21); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.getRawResult() == null); + assertSame(f, f.fork()); + assertNull(f.join()); + assertEquals(21, f.number); + checkCompletedNormally(f); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** * get of a forked task returns when task completes */ public void testForkGet() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - AsyncFib f = new AsyncFib(8); - f.fork(); - f.get(); - threadAssertTrue(f.number == 21); - threadAssertTrue(f.isDone()); - } catch (Exception ex) { - unexpectedException(ex); - } + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() throws Exception { + AsyncFib f = new AsyncFib(8); + assertSame(f, f.fork()); + assertNull(f.get()); + assertEquals(21, f.number); + checkCompletedNormally(f); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** * timed get of a forked task returns when task completes */ public void testForkTimedGet() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - AsyncFib f = new AsyncFib(8); - f.fork(); - f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS); - threadAssertTrue(f.number == 21); - threadAssertTrue(f.isDone()); - } catch (Exception ex) { - unexpectedException(ex); - } + RecursiveAction a = new CheckedRecursiveAction() { + 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); + checkCompletedNormally(f); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** * timed get with null time unit throws NPE */ public void testForkTimedGetNPE() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() throws Exception { + AsyncFib f = new AsyncFib(8); + assertSame(f, f.fork()); try { - AsyncFib f = new AsyncFib(8); - f.fork(); f.get(5L, null); shouldThrow(); - } catch (NullPointerException success) { - } catch (Exception ex) { - unexpectedException(ex); - } + } catch (NullPointerException success) {} }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** * quietlyJoin of a forked task returns when task completes */ public void testForkQuietlyJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); - f.fork(); + assertSame(f, f.fork()); f.quietlyJoin(); - threadAssertTrue(f.number == 21); - threadAssertTrue(f.isDone()); + assertEquals(21, f.number); + checkCompletedNormally(f); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } - /** * helpQuiesce returns when tasks are complete. * getQueuedTaskCount returns 0 when quiescent */ public void testForkHelpQuiesce() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); - f.fork(); - f.helpQuiesce(); - threadAssertTrue(f.number == 21); - threadAssertTrue(f.isDone()); - threadAssertTrue(getQueuedTaskCount() == 0); + assertSame(f, f.fork()); + helpQuiesce(); + assertEquals(21, f.number); + assertEquals(0, getQueuedTaskCount()); + checkCompletedNormally(f); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } - /** * invoke task throws exception when task completes abnormally */ public void testAbnormalInvoke() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + FailingAsyncFib f = new FailingAsyncFib(8); try { - FailingAsyncFib f = new FailingAsyncFib(8); f.invoke(); shouldThrow(); } catch (FJException success) { + checkCompletedAbnormally(f, success); } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** * quietlyInvoke task returns when task completes abnormally */ public void testAbnormalQuietlyInvoke() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); f.quietlyInvoke(); - threadAssertTrue(f.isDone()); + assertTrue(f.getException() instanceof FJException); + checkCompletedAbnormally(f, f.getException()); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** * join of a forked task throws exception when task completes abnormally */ public void testAbnormalForkJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + FailingAsyncFib f = new FailingAsyncFib(8); + assertSame(f, f.fork()); try { - FailingAsyncFib f = new FailingAsyncFib(8); - f.fork(); f.join(); shouldThrow(); } catch (FJException success) { + checkCompletedAbnormally(f, success); } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** * get of a forked task throws exception when task completes abnormally */ public void testAbnormalForkGet() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() throws Exception { + FailingAsyncFib f = new FailingAsyncFib(8); + assertSame(f, f.fork()); try { - FailingAsyncFib f = new FailingAsyncFib(8); - f.fork(); f.get(); shouldThrow(); } catch (ExecutionException success) { - } catch (Exception ex) { - unexpectedException(ex); + Throwable cause = success.getCause(); + assertTrue(cause instanceof FJException); + checkCompletedAbnormally(f, cause); } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** * timed get of a forked task throws exception when task completes abnormally */ public void testAbnormalForkTimedGet() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() throws Exception { + FailingAsyncFib f = new FailingAsyncFib(8); + assertSame(f, f.fork()); try { - FailingAsyncFib f = new FailingAsyncFib(8); - f.fork(); - f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS); + f.get(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (ExecutionException success) { - } catch (Exception ex) { - unexpectedException(ex); + Throwable cause = success.getCause(); + assertTrue(cause instanceof FJException); + checkCompletedAbnormally(f, cause); } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** * quietlyJoin of a forked task returns when task completes abnormally */ public void testAbnormalForkQuietlyJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); - f.fork(); + assertSame(f, f.fork()); f.quietlyJoin(); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.isCompletedAbnormally()); - threadAssertTrue(f.getException() instanceof FJException); + assertTrue(f.getException() instanceof FJException); + checkCompletedAbnormally(f, f.getException()); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** * invoke task throws exception when task cancelled */ public void testCancelledInvoke() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + AsyncFib f = new AsyncFib(8); + assertTrue(f.cancel(true)); try { - AsyncFib f = new AsyncFib(8); - f.cancel(true); f.invoke(); shouldThrow(); } catch (CancellationException success) { + checkCancelled(f); } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** * join of a forked task throws exception when task cancelled */ public void testCancelledForkJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + AsyncFib f = new AsyncFib(8); + assertTrue(f.cancel(true)); + assertSame(f, f.fork()); try { - AsyncFib f = new AsyncFib(8); - f.cancel(true); - f.fork(); f.join(); shouldThrow(); } catch (CancellationException success) { + checkCancelled(f); } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** * get of a forked task throws exception when task cancelled */ public void testCancelledForkGet() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() throws Exception { + AsyncFib f = new AsyncFib(8); + assertTrue(f.cancel(true)); + assertSame(f, f.fork()); try { - AsyncFib f = new AsyncFib(8); - f.cancel(true); - f.fork(); f.get(); shouldThrow(); } catch (CancellationException success) { - } catch (Exception ex) { - unexpectedException(ex); + checkCancelled(f); } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** * timed get of a forked task throws exception when task cancelled */ - public void testCancelledForkTimedGet() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + public void testCancelledForkTimedGet() throws Exception { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() throws Exception { + AsyncFib f = new AsyncFib(8); + assertTrue(f.cancel(true)); + assertSame(f, f.fork()); try { - AsyncFib f = new AsyncFib(8); - f.cancel(true); - f.fork(); - f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS); + f.get(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (CancellationException success) { - } catch (Exception ex) { - unexpectedException(ex); + checkCancelled(f); } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** * quietlyJoin of a forked task returns when task cancelled */ public void testCancelledForkQuietlyJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); - f.cancel(true); - f.fork(); + assertTrue(f.cancel(true)); + assertSame(f, f.fork()); f.quietlyJoin(); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.isCompletedAbnormally()); - threadAssertTrue(f.getException() instanceof CancellationException); + checkCancelled(f); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** * getPool of executing task returns its pool */ public void testGetPool() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - threadAssertTrue(getPool() == mainPool); + final ForkJoinPool mainPool = mainPool(); + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + assertSame(mainPool, getPool()); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool, a); } /** * getPool of non-FJ task returns null */ public void testGetPool2() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - threadAssertTrue(getPool() == null); + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + assertNull(getPool()); }}; - a.invoke(); + assertNull(a.invoke()); } /** * inForkJoinPool of executing task returns true */ public void testInForkJoinPool() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - threadAssertTrue(inForkJoinPool()); + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + assertTrue(inForkJoinPool()); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** * inForkJoinPool of non-FJ task returns false */ public void testInForkJoinPool2() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - threadAssertTrue(!inForkJoinPool()); + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + assertFalse(inForkJoinPool()); }}; - a.invoke(); + assertNull(a.invoke()); } /** * setRawResult(null) succeeds */ public void testSetRawResult() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { setRawResult(null); + assertNull(getRawResult()); }}; - a.invoke(); + assertNull(a.invoke()); } /** * invoke task throws exception after invoking completeExceptionally */ public void testCompleteExceptionally() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + AsyncFib f = new AsyncFib(8); + f.completeExceptionally(new FJException()); try { - AsyncFib f = new AsyncFib(8); - f.completeExceptionally(new FJException()); f.invoke(); shouldThrow(); } catch (FJException success) { + checkCompletedAbnormally(f, success); } }}; - mainPool.invoke(a); + 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); } /** * invokeAll(t1, t2) invokes all task arguments */ public void testInvokeAll2() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); AsyncFib g = new AsyncFib(9); invokeAll(f, g); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.number == 21); - threadAssertTrue(g.isDone()); - threadAssertTrue(g.number == 34); + assertEquals(21, f.number); + assertEquals(34, g.number); + checkCompletedNormally(f); + checkCompletedNormally(g); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** * invokeAll(tasks) with 1 argument invokes task */ public void testInvokeAll1() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); invokeAll(f); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.number == 21); + checkCompletedNormally(f); + assertEquals(21, f.number); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** * invokeAll(tasks) with > 2 argument invokes tasks */ public void testInvokeAll3() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); AsyncFib g = new AsyncFib(9); AsyncFib h = new AsyncFib(7); invokeAll(f, g, h); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.number == 21); - threadAssertTrue(g.isDone()); - threadAssertTrue(g.number == 34); - threadAssertTrue(h.isDone()); - threadAssertTrue(h.number == 13); + assertEquals(21, f.number); + assertEquals(34, g.number); + assertEquals(13, h.number); + checkCompletedNormally(f); + checkCompletedNormally(g); + checkCompletedNormally(h); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** * invokeAll(collection) invokes all tasks in the collection */ public void testInvokeAllCollection() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); AsyncFib g = new AsyncFib(9); AsyncFib h = new AsyncFib(7); @@ -695,106 +859,110 @@ public class ForkJoinTaskTest extends JS set.add(g); set.add(h); invokeAll(set); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.number == 21); - threadAssertTrue(g.isDone()); - threadAssertTrue(g.number == 34); - threadAssertTrue(h.isDone()); - threadAssertTrue(h.number == 13); + assertEquals(21, f.number); + assertEquals(34, g.number); + assertEquals(13, h.number); + checkCompletedNormally(f); + checkCompletedNormally(g); + checkCompletedNormally(h); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } - /** * invokeAll(tasks) with any null task throws NPE */ public void testInvokeAllNPE() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + AsyncFib f = new AsyncFib(8); + AsyncFib g = new AsyncFib(9); + AsyncFib h = null; try { - AsyncFib f = new AsyncFib(8); - AsyncFib g = new AsyncFib(9); - AsyncFib h = null; invokeAll(f, g, h); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** * invokeAll(t1, t2) throw exception if any task does */ public void testAbnormalInvokeAll2() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + AsyncFib f = new AsyncFib(8); + FailingAsyncFib g = new FailingAsyncFib(9); + ForkJoinTask[] tasks = { f, g }; + shuffle(tasks); try { - AsyncFib f = new AsyncFib(8); - FailingAsyncFib g = new FailingAsyncFib(9); - invokeAll(f, g); + invokeAll(tasks); shouldThrow(); } catch (FJException success) { + checkCompletedAbnormally(g, success); } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** * invokeAll(tasks) with 1 argument throws exception if task does */ public void testAbnormalInvokeAll1() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + FailingAsyncFib g = new FailingAsyncFib(9); try { - FailingAsyncFib g = new FailingAsyncFib(9); invokeAll(g); shouldThrow(); } catch (FJException success) { + checkCompletedAbnormally(g, success); } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** * invokeAll(tasks) with > 2 argument throws exception if any task does */ public void testAbnormalInvokeAll3() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + 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 { - AsyncFib f = new AsyncFib(8); - FailingAsyncFib g = new FailingAsyncFib(9); - AsyncFib h = new AsyncFib(7); - invokeAll(f, g, h); + invokeAll(tasks); shouldThrow(); } catch (FJException success) { + checkCompletedAbnormally(g, success); } }}; - mainPool.invoke(a); + 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 RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + FailingAsyncFib f = new FailingAsyncFib(8); + AsyncFib g = new AsyncFib(9); + AsyncFib h = new AsyncFib(7); + ForkJoinTask[] tasks = { f, g, h }; + shuffle(tasks); try { - 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); - invokeAll(set); + invokeAll(Arrays.asList(tasks)); shouldThrow(); } catch (FJException success) { + checkCompletedAbnormally(f, success); } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -802,18 +970,18 @@ public class ForkJoinTaskTest extends JS * and suppresses execution */ public void testTryUnfork() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { AsyncFib g = new AsyncFib(9); - g.fork(); + assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); - f.fork(); - threadAssertTrue(f.tryUnfork()); + assertSame(f, f.fork()); + assertTrue(f.tryUnfork()); helpQuiesce(); - threadAssertFalse(f.isDone()); - threadAssertTrue(g.isDone()); + checkNotDone(f); + checkCompletedNormally(g); }}; - singletonPool.invoke(a); + testInvokeOnPool(singletonPool(), a); } /** @@ -821,128 +989,666 @@ public class ForkJoinTaskTest extends JS * there are more tasks than threads */ public void testGetSurplusQueuedTaskCount() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { AsyncFib h = new AsyncFib(7); - h.fork(); + assertSame(h, h.fork()); AsyncFib g = new AsyncFib(9); - g.fork(); + assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); - f.fork(); - threadAssertTrue(getSurplusQueuedTaskCount() > 0); + assertSame(f, f.fork()); + assertTrue(getSurplusQueuedTaskCount() > 0); helpQuiesce(); + assertEquals(0, getSurplusQueuedTaskCount()); + checkCompletedNormally(f); + checkCompletedNormally(g); + checkCompletedNormally(h); }}; - singletonPool.invoke(a); + testInvokeOnPool(singletonPool(), a); } /** * peekNextLocalTask returns most recent unexecuted task. */ public void testPeekNextLocalTask() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { AsyncFib g = new AsyncFib(9); - g.fork(); + assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); - f.fork(); - threadAssertTrue(peekNextLocalTask() == f); - f.join(); - threadAssertTrue(f.isDone()); + assertSame(f, f.fork()); + assertSame(f, peekNextLocalTask()); + assertNull(f.join()); + checkCompletedNormally(f); helpQuiesce(); + checkCompletedNormally(g); }}; - singletonPool.invoke(a); + 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 RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { AsyncFib g = new AsyncFib(9); - g.fork(); + assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); - f.fork(); - threadAssertTrue(pollNextLocalTask() == f); + assertSame(f, f.fork()); + assertSame(f, pollNextLocalTask()); helpQuiesce(); - threadAssertFalse(f.isDone()); + checkNotDone(f); + assertEquals(34, g.number); + checkCompletedNormally(g); }}; - singletonPool.invoke(a); + 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 RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { AsyncFib g = new AsyncFib(9); - g.fork(); + assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); - f.fork(); - threadAssertTrue(pollTask() == f); + assertSame(f, f.fork()); + assertSame(f, pollTask()); helpQuiesce(); - threadAssertFalse(f.isDone()); - threadAssertTrue(g.isDone()); + checkNotDone(f); + checkCompletedNormally(g); }}; - singletonPool.invoke(a); + testInvokeOnPool(singletonPool(), a); } /** * peekNextLocalTask returns least recent unexecuted task in async mode */ public void testPeekNextLocalTaskAsync() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { AsyncFib g = new AsyncFib(9); - g.fork(); + assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); - f.fork(); - threadAssertTrue(peekNextLocalTask() == g); - f.join(); + assertSame(f, f.fork()); + assertSame(g, peekNextLocalTask()); + assertNull(f.join()); helpQuiesce(); - threadAssertTrue(f.isDone()); + checkCompletedNormally(f); + assertEquals(34, g.number); + checkCompletedNormally(g); }}; - asyncSingletonPool.invoke(a); + 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 RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { AsyncFib g = new AsyncFib(9); - g.fork(); + assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); - f.fork(); - threadAssertTrue(pollNextLocalTask() == g); + assertSame(f, f.fork()); + assertSame(g, pollNextLocalTask()); helpQuiesce(); - threadAssertTrue(f.isDone()); - threadAssertFalse(g.isDone()); + assertEquals(21, f.number); + checkCompletedNormally(f); + checkNotDone(g); }}; - asyncSingletonPool.invoke(a); + 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 RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { AsyncFib g = new AsyncFib(9); - g.fork(); + assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); - f.fork(); - threadAssertTrue(pollTask() == g); + assertSame(f, f.fork()); + assertSame(g, pollTask()); helpQuiesce(); - threadAssertTrue(f.isDone()); - threadAssertFalse(g.isDone()); + assertEquals(21, f.number); + checkCompletedNormally(f); + checkNotDone(g); + }}; + testInvokeOnPool(asyncSingletonPool(), a); + } + + // versions for singleton pools + + /** + * invoke returns when task completes normally. + * isCompletedAbnormally and isCancelled return false for normally + * completed tasks; getRawResult returns null. + */ + public void testInvokeSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + AsyncFib f = new AsyncFib(8); + assertNull(f.invoke()); + assertEquals(21, f.number); + checkCompletedNormally(f); + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * quietlyInvoke task returns when task completes normally. + * isCompletedAbnormally and isCancelled return false for normally + * completed tasks + */ + public void testQuietlyInvokeSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + AsyncFib f = new AsyncFib(8); + f.quietlyInvoke(); + assertEquals(21, f.number); + checkCompletedNormally(f); + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * join of a forked task returns when task completes + */ + public void testForkJoinSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + AsyncFib f = new AsyncFib(8); + assertSame(f, f.fork()); + assertNull(f.join()); + assertEquals(21, f.number); + checkCompletedNormally(f); + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * get of a forked task returns when task completes + */ + public void testForkGetSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() throws Exception { + AsyncFib f = new AsyncFib(8); + assertSame(f, f.fork()); + assertNull(f.get()); + assertEquals(21, f.number); + checkCompletedNormally(f); + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * timed get of a forked task returns when task completes + */ + public void testForkTimedGetSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + 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); + checkCompletedNormally(f); + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * timed get with null time unit throws NPE + */ + public void testForkTimedGetNPESingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() throws Exception { + AsyncFib f = new AsyncFib(8); + assertSame(f, f.fork()); + try { + f.get(5L, null); + shouldThrow(); + } catch (NullPointerException success) {} + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * quietlyJoin of a forked task returns when task completes + */ + public void testForkQuietlyJoinSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + AsyncFib f = new AsyncFib(8); + assertSame(f, f.fork()); + f.quietlyJoin(); + assertEquals(21, f.number); + checkCompletedNormally(f); + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * helpQuiesce returns when tasks are complete. + * getQueuedTaskCount returns 0 when quiescent + */ + public void testForkHelpQuiesceSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + AsyncFib f = new AsyncFib(8); + assertSame(f, f.fork()); + 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() { + protected void realCompute() { + FailingAsyncFib f = new FailingAsyncFib(8); + try { + f.invoke(); + shouldThrow(); + } catch (FJException success) { + checkCompletedAbnormally(f, success); + } + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * quietlyInvoke task returns when task completes abnormally + */ + public void testAbnormalQuietlyInvokeSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + FailingAsyncFib f = new FailingAsyncFib(8); + f.quietlyInvoke(); + assertTrue(f.getException() instanceof FJException); + checkCompletedAbnormally(f, f.getException()); + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * join of a forked task throws exception when task completes abnormally + */ + public void testAbnormalForkJoinSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + FailingAsyncFib f = new FailingAsyncFib(8); + assertSame(f, f.fork()); + try { + f.join(); + shouldThrow(); + } catch (FJException success) { + checkCompletedAbnormally(f, success); + } + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * get of a forked task throws exception when task completes abnormally + */ + public void testAbnormalForkGetSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() throws Exception { + FailingAsyncFib f = new FailingAsyncFib(8); + assertSame(f, f.fork()); + try { + f.get(); + shouldThrow(); + } catch (ExecutionException success) { + Throwable cause = success.getCause(); + assertTrue(cause instanceof FJException); + checkCompletedAbnormally(f, cause); + } + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * timed get of a forked task throws exception when task completes abnormally + */ + public void testAbnormalForkTimedGetSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() throws Exception { + FailingAsyncFib f = new FailingAsyncFib(8); + assertSame(f, f.fork()); + try { + f.get(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (ExecutionException success) { + Throwable cause = success.getCause(); + assertTrue(cause instanceof FJException); + checkCompletedAbnormally(f, cause); + } + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * quietlyJoin of a forked task returns when task completes abnormally + */ + public void testAbnormalForkQuietlyJoinSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + FailingAsyncFib f = new FailingAsyncFib(8); + assertSame(f, f.fork()); + f.quietlyJoin(); + assertTrue(f.getException() instanceof FJException); + checkCompletedAbnormally(f, f.getException()); }}; - asyncSingletonPool.invoke(a); + testInvokeOnPool(singletonPool(), a); } + + /** + * invoke task throws exception when task cancelled + */ + public void testCancelledInvokeSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + AsyncFib f = new AsyncFib(8); + assertTrue(f.cancel(true)); + try { + f.invoke(); + shouldThrow(); + } catch (CancellationException success) { + checkCancelled(f); + } + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * join of a forked task throws exception when task cancelled + */ + public void testCancelledForkJoinSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + AsyncFib f = new AsyncFib(8); + assertTrue(f.cancel(true)); + assertSame(f, f.fork()); + try { + f.join(); + shouldThrow(); + } catch (CancellationException success) { + checkCancelled(f); + } + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * get of a forked task throws exception when task cancelled + */ + public void testCancelledForkGetSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() throws Exception { + AsyncFib f = new AsyncFib(8); + assertTrue(f.cancel(true)); + assertSame(f, f.fork()); + try { + f.get(); + shouldThrow(); + } catch (CancellationException success) { + checkCancelled(f); + } + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * timed get of a forked task throws exception when task cancelled + */ + public void testCancelledForkTimedGetSingleton() throws Exception { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() throws Exception { + AsyncFib f = new AsyncFib(8); + assertTrue(f.cancel(true)); + assertSame(f, f.fork()); + try { + f.get(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (CancellationException success) { + checkCancelled(f); + } + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * quietlyJoin of a forked task returns when task cancelled + */ + public void testCancelledForkQuietlyJoinSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + AsyncFib f = new AsyncFib(8); + assertTrue(f.cancel(true)); + assertSame(f, f.fork()); + f.quietlyJoin(); + checkCancelled(f); + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * invoke task throws exception after invoking completeExceptionally + */ + public void testCompleteExceptionallySingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + AsyncFib f = new AsyncFib(8); + f.completeExceptionally(new FJException()); + try { + f.invoke(); + shouldThrow(); + } catch (FJException success) { + checkCompletedAbnormally(f, success); + } + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * invokeAll(t1, t2) invokes all task arguments + */ + public void testInvokeAll2Singleton() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + AsyncFib f = new AsyncFib(8); + AsyncFib g = new AsyncFib(9); + invokeAll(f, g); + assertEquals(21, f.number); + assertEquals(34, g.number); + checkCompletedNormally(f); + checkCompletedNormally(g); + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * invokeAll(tasks) with 1 argument invokes task + */ + public void testInvokeAll1Singleton() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + AsyncFib f = new AsyncFib(8); + invokeAll(f); + checkCompletedNormally(f); + assertEquals(21, f.number); + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * invokeAll(tasks) with > 2 argument invokes tasks + */ + public void testInvokeAll3Singleton() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + AsyncFib f = new AsyncFib(8); + AsyncFib g = new AsyncFib(9); + AsyncFib h = new AsyncFib(7); + invokeAll(f, g, h); + assertEquals(21, f.number); + assertEquals(34, g.number); + assertEquals(13, h.number); + checkCompletedNormally(f); + checkCompletedNormally(g); + checkCompletedNormally(h); + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * invokeAll(collection) invokes all tasks in the collection + */ + public void testInvokeAllCollectionSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + AsyncFib f = new AsyncFib(8); + AsyncFib g = new AsyncFib(9); + AsyncFib h = new AsyncFib(7); + HashSet set = new HashSet(); + set.add(f); + set.add(g); + set.add(h); + invokeAll(set); + assertEquals(21, f.number); + assertEquals(34, g.number); + 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() { + protected void realCompute() { + AsyncFib f = new AsyncFib(8); + AsyncFib g = new AsyncFib(9); + AsyncFib h = null; + try { + invokeAll(f, g, h); + shouldThrow(); + } catch (NullPointerException success) {} + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * invokeAll(t1, t2) throw exception if any task does + */ + public void testAbnormalInvokeAll2Singleton() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + AsyncFib f = new AsyncFib(8); + FailingAsyncFib g = new FailingAsyncFib(9); + ForkJoinTask[] tasks = { f, g }; + shuffle(tasks); + try { + invokeAll(tasks); + shouldThrow(); + } catch (FJException success) { + checkCompletedAbnormally(g, success); + } + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * invokeAll(tasks) with 1 argument throws exception if task does + */ + public void testAbnormalInvokeAll1Singleton() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + FailingAsyncFib g = new FailingAsyncFib(9); + try { + invokeAll(g); + shouldThrow(); + } catch (FJException success) { + checkCompletedAbnormally(g, success); + } + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * invokeAll(tasks) with > 2 argument throws exception if any task does + */ + public void testAbnormalInvokeAll3Singleton() { + RecursiveAction a = new CheckedRecursiveAction() { + 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(tasks); + shouldThrow(); + } catch (FJException success) { + checkCompletedAbnormally(g, success); + } + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * invokeAll(collection) throws exception if any task does + */ + public void testAbnormalInvokeAllCollectionSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + FailingAsyncFib f = new FailingAsyncFib(8); + AsyncFib g = new AsyncFib(9); + AsyncFib h = new AsyncFib(7); + ForkJoinTask[] tasks = { f, g, h }; + shuffle(tasks); + try { + invokeAll(Arrays.asList(tasks)); + shouldThrow(); + } 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); + } + }