--- jsr166/src/test/tck/ForkJoinTask8Test.java 2013/07/21 22:24:18 1.1 +++ jsr166/src/test/tck/ForkJoinTask8Test.java 2015/10/04 18:40:57 1.18 @@ -3,19 +3,22 @@ * Expert Group and released to the public domain, as explained at * http://creativecommons.org/publicdomain/zero/1.0/ */ + +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.SECONDS; + +import java.util.Arrays; +import java.util.Collections; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; -import java.util.concurrent.CancellationException; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; import java.util.concurrent.ForkJoinWorkerThread; import java.util.concurrent.RecursiveAction; -import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import static java.util.concurrent.TimeUnit.SECONDS; -import java.util.HashSet; -import junit.framework.*; + +import junit.framework.Test; +import junit.framework.TestSuite; public class ForkJoinTask8Test extends JSR166TestCase { @@ -32,10 +35,9 @@ public class ForkJoinTask8Test extends J static final short INITIAL_STATE = -1; static final short COMPLETE_STATE = 0; static final short EXCEPTION_STATE = 1; - public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { @@ -60,8 +62,19 @@ public class ForkJoinTask8Test extends J null, true); } + // Compute fib naively and efficiently + final int[] fib; + { + int[] fib = new int[10]; + fib[0] = 0; + fib[1] = 1; + for (int i = 2; i < fib.length; i++) + fib[i] = fib[i - 1] + fib[i - 2]; + this.fib = fib; + } + private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { - try { + try (PoolCleaner cleaner = cleaner(pool)) { assertFalse(a.isDone()); assertFalse(a.isCompletedNormally()); assertFalse(a.isCompletedAbnormally()); @@ -77,8 +90,6 @@ public class ForkJoinTask8Test extends J assertFalse(a.isCancelled()); assertNull(a.getException()); assertNull(a.getRawResult()); - } finally { - joinPool(pool); } } @@ -181,7 +192,6 @@ public class ForkJoinTask8Test extends J } catch (Throwable fail) { threadUnexpectedException(fail); } } - public static final class FJException extends RuntimeException { FJException() { super(); } } @@ -206,8 +216,8 @@ public class ForkJoinTask8Test extends J } protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) { - if (this.getForkJoinTaskTag() != COMPLETE_STATE || - x.getForkJoinTaskTag() != COMPLETE_STATE || + if (this.getForkJoinTaskTag() != COMPLETE_STATE || + x.getForkJoinTaskTag() != COMPLETE_STATE || y.getForkJoinTaskTag() != COMPLETE_STATE) { completeThisExceptionally(new FJException()); } @@ -241,7 +251,7 @@ public class ForkJoinTask8Test extends J a.sibling = null; a.parent = null; a.completeThis(); - if (p == null || + if (p == null || p.compareAndSetForkJoinTaskTag(INITIAL_STATE, COMPLETE_STATE)) break; try { @@ -279,13 +289,14 @@ public class ForkJoinTask8Test extends J super.reinitialize(); } - } - static final class AsyncFib extends BinaryAsyncAction { + final class AsyncFib extends BinaryAsyncAction { int number; - public AsyncFib(int n) { - this.number = n; + int expectedResult; + public AsyncFib(int number) { + this.number = number; + this.expectedResult = fib[number]; } public final boolean exec() { @@ -304,7 +315,7 @@ public class ForkJoinTask8Test extends J } f.complete(); } - catch(Throwable ex) { + catch (Throwable ex) { compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE); } return false; @@ -314,6 +325,11 @@ public class ForkJoinTask8Test extends J number = ((AsyncFib)x).number + ((AsyncFib)y).number; super.onComplete(x, y); } + + public void checkCompletedNormally() { + assertEquals(expectedResult, number); + ForkJoinTask8Test.this.checkCompletedNormally(this); + } } static final class FailingAsyncFib extends BinaryAsyncAction { @@ -350,14 +366,19 @@ public class ForkJoinTask8Test extends J * completed tasks; getRawResult returns null. */ public void testInvoke() { + testInvoke(mainPool()); + } + public void testInvoke_Singleton() { + testInvoke(singletonPool()); + } + public void testInvoke(ForkJoinPool pool) { RecursiveAction a = new CheckedRecursiveAction() { protected void realCompute() { AsyncFib f = new AsyncFib(8); assertNull(f.invoke()); - assertEquals(21, f.number); - checkCompletedNormally(f); + f.checkCompletedNormally(); }}; - testInvokeOnPool(mainPool(), a); + testInvokeOnPool(pool, a); } /** @@ -366,65 +387,91 @@ public class ForkJoinTask8Test extends J * completed tasks */ public void testQuietlyInvoke() { + testQuietlyInvoke(mainPool()); + } + public void testQuietlyInvoke_Singleton() { + testQuietlyInvoke(singletonPool()); + } + public void testQuietlyInvoke(ForkJoinPool pool) { RecursiveAction a = new CheckedRecursiveAction() { protected void realCompute() { AsyncFib f = new AsyncFib(8); f.quietlyInvoke(); - assertEquals(21, f.number); - checkCompletedNormally(f); + f.checkCompletedNormally(); }}; - testInvokeOnPool(mainPool(), a); + testInvokeOnPool(pool, a); } /** * join of a forked task returns when task completes */ public void testForkJoin() { + testForkJoin(mainPool()); + } + public void testForkJoin_Singleton() { + testForkJoin(singletonPool()); + } + public void testForkJoin(ForkJoinPool pool) { 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); + f.checkCompletedNormally(); }}; - testInvokeOnPool(mainPool(), a); + testInvokeOnPool(pool, a); } /** * get of a forked task returns when task completes */ public void testForkGet() { + testForkGet(mainPool()); + } + public void testForkGet_Singleton() { + testForkGet(singletonPool()); + } + public void testForkGet(ForkJoinPool pool) { 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); + f.checkCompletedNormally(); }}; - testInvokeOnPool(mainPool(), a); + testInvokeOnPool(pool, a); } /** * timed get of a forked task returns when task completes */ public void testForkTimedGet() { + testForkTimedGet(mainPool()); + } + public void testForkTimedGet_Singleton() { + testForkTimedGet(singletonPool()); + } + public void testForkTimedGet(ForkJoinPool pool) { 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); + f.checkCompletedNormally(); }}; - testInvokeOnPool(mainPool(), a); + testInvokeOnPool(pool, a); } /** - * timed get with null time unit throws NPE + * timed get with null time unit throws NullPointerException */ - public void testForkTimedGetNPE() { + public void testForkTimedGetNullTimeUnit() { + testForkTimedGetNullTimeUnit(mainPool()); + } + public void testForkTimedGetNullTimeUnit_Singleton() { + testForkTimedGet(singletonPool()); + } + public void testForkTimedGetNullTimeUnit(ForkJoinPool pool) { RecursiveAction a = new CheckedRecursiveAction() { protected void realCompute() throws Exception { AsyncFib f = new AsyncFib(8); @@ -434,22 +481,27 @@ public class ForkJoinTask8Test extends J shouldThrow(); } catch (NullPointerException success) {} }}; - testInvokeOnPool(mainPool(), a); + testInvokeOnPool(pool, a); } /** * quietlyJoin of a forked task returns when task completes */ public void testForkQuietlyJoin() { + testForkQuietlyJoin(mainPool()); + } + public void testForkQuietlyJoin_Singleton() { + testForkQuietlyJoin(singletonPool()); + } + public void testForkQuietlyJoin(ForkJoinPool pool) { RecursiveAction a = new CheckedRecursiveAction() { protected void realCompute() { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); f.quietlyJoin(); - assertEquals(21, f.number); - checkCompletedNormally(f); + f.checkCompletedNormally(); }}; - testInvokeOnPool(mainPool(), a); + testInvokeOnPool(pool, a); } /** @@ -457,22 +509,33 @@ public class ForkJoinTask8Test extends J * getQueuedTaskCount returns 0 when quiescent */ public void testForkHelpQuiesce() { + testForkHelpQuiesce(mainPool()); + } + public void testForkHelpQuiesce_Singleton() { + testForkHelpQuiesce(singletonPool()); + } + public void testForkHelpQuiesce(ForkJoinPool pool) { RecursiveAction a = new CheckedRecursiveAction() { protected void realCompute() { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); helpQuiesce(); - assertEquals(21, f.number); assertEquals(0, getQueuedTaskCount()); - checkCompletedNormally(f); + f.checkCompletedNormally(); }}; - testInvokeOnPool(mainPool(), a); + testInvokeOnPool(pool, a); } /** * invoke task throws exception when task completes abnormally */ public void testAbnormalInvoke() { + testAbnormalInvoke(mainPool()); + } + public void testAbnormalInvoke_Singleton() { + testAbnormalInvoke(singletonPool()); + } + public void testAbnormalInvoke(ForkJoinPool pool) { RecursiveAction a = new CheckedRecursiveAction() { protected void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); @@ -483,13 +546,19 @@ public class ForkJoinTask8Test extends J checkCompletedAbnormally(f, success); } }}; - testInvokeOnPool(mainPool(), a); + testInvokeOnPool(pool, a); } /** * quietlyInvoke task returns when task completes abnormally */ public void testAbnormalQuietlyInvoke() { + testAbnormalQuietlyInvoke(mainPool()); + } + public void testAbnormalQuietlyInvoke_Singleton() { + testAbnormalQuietlyInvoke(singletonPool()); + } + public void testAbnormalQuietlyInvoke(ForkJoinPool pool) { RecursiveAction a = new CheckedRecursiveAction() { protected void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); @@ -497,13 +566,19 @@ public class ForkJoinTask8Test extends J assertTrue(f.getException() instanceof FJException); checkCompletedAbnormally(f, f.getException()); }}; - testInvokeOnPool(mainPool(), a); + testInvokeOnPool(pool, a); } /** * join of a forked task throws exception when task completes abnormally */ public void testAbnormalForkJoin() { + testAbnormalForkJoin(mainPool()); + } + public void testAbnormalForkJoin_Singleton() { + testAbnormalForkJoin(singletonPool()); + } + public void testAbnormalForkJoin(ForkJoinPool pool) { RecursiveAction a = new CheckedRecursiveAction() { protected void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); @@ -515,13 +590,19 @@ public class ForkJoinTask8Test extends J checkCompletedAbnormally(f, success); } }}; - testInvokeOnPool(mainPool(), a); + testInvokeOnPool(pool, a); } /** * get of a forked task throws exception when task completes abnormally */ public void testAbnormalForkGet() { + testAbnormalForkGet(mainPool()); + } + public void testAbnormalForkGet_Singleton() { + testAbnormalForkJoin(singletonPool()); + } + public void testAbnormalForkGet(ForkJoinPool pool) { RecursiveAction a = new CheckedRecursiveAction() { protected void realCompute() throws Exception { FailingAsyncFib f = new FailingAsyncFib(8); @@ -535,13 +616,19 @@ public class ForkJoinTask8Test extends J checkCompletedAbnormally(f, cause); } }}; - testInvokeOnPool(mainPool(), a); + testInvokeOnPool(pool, a); } /** * timed get of a forked task throws exception when task completes abnormally */ public void testAbnormalForkTimedGet() { + testAbnormalForkTimedGet(mainPool()); + } + public void testAbnormalForkTimedGet_Singleton() { + testAbnormalForkTimedGet(singletonPool()); + } + public void testAbnormalForkTimedGet(ForkJoinPool pool) { RecursiveAction a = new CheckedRecursiveAction() { protected void realCompute() throws Exception { FailingAsyncFib f = new FailingAsyncFib(8); @@ -555,13 +642,19 @@ public class ForkJoinTask8Test extends J checkCompletedAbnormally(f, cause); } }}; - testInvokeOnPool(mainPool(), a); + testInvokeOnPool(pool, a); } /** * quietlyJoin of a forked task returns when task completes abnormally */ public void testAbnormalForkQuietlyJoin() { + testAbnormalForkQuietlyJoin(mainPool()); + } + public void testAbnormalForkQuietlyJoin_Singleton() { + testAbnormalForkQuietlyJoin(singletonPool()); + } + public void testAbnormalForkQuietlyJoin(ForkJoinPool pool) { RecursiveAction a = new CheckedRecursiveAction() { protected void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); @@ -570,20 +663,24 @@ public class ForkJoinTask8Test extends J assertTrue(f.getException() instanceof FJException); checkCompletedAbnormally(f, f.getException()); }}; - testInvokeOnPool(mainPool(), a); + testInvokeOnPool(pool, a); } - /** * getPool of executing task returns its pool */ public void testGetPool() { - final ForkJoinPool mainPool = mainPool(); + testGetPool(mainPool()); + } + public void testGetPool_Singleton() { + testGetPool(singletonPool()); + } + public void testGetPool(ForkJoinPool pool) { RecursiveAction a = new CheckedRecursiveAction() { protected void realCompute() { - assertSame(mainPool, getPool()); + assertSame(pool, getPool()); }}; - testInvokeOnPool(mainPool, a); + testInvokeOnPool(pool, a); } /** @@ -601,11 +698,17 @@ public class ForkJoinTask8Test extends J * inForkJoinPool of executing task returns true */ public void testInForkJoinPool() { + testInForkJoinPool(mainPool()); + } + public void testInForkJoinPool_Singleton() { + testInForkJoinPool(singletonPool()); + } + public void testInForkJoinPool(ForkJoinPool pool) { RecursiveAction a = new CheckedRecursiveAction() { protected void realCompute() { assertTrue(inForkJoinPool()); }}; - testInvokeOnPool(mainPool(), a); + testInvokeOnPool(pool, a); } /** @@ -635,6 +738,12 @@ public class ForkJoinTask8Test extends J * invoke task throws exception after invoking completeExceptionally */ public void testCompleteExceptionally() { + testCompleteExceptionally(mainPool()); + } + public void testCompleteExceptionally_Singleton() { + testCompleteExceptionally(singletonPool()); + } + public void testCompleteExceptionally(ForkJoinPool pool) { RecursiveAction a = new CheckedRecursiveAction() { protected void realCompute() { AsyncFib f = new AsyncFib(8); @@ -646,176 +755,225 @@ public class ForkJoinTask8Test extends J checkCompletedAbnormally(f, success); } }}; - testInvokeOnPool(mainPool(), a); + testInvokeOnPool(pool, a); } /** - * invokeAll(t1, t2) invokes all task arguments + * invokeAll(tasks) with 1 argument invokes task */ - public void testInvokeAll2() { + public void testInvokeAll1() { + testInvokeAll1(mainPool()); + } + public void testInvokeAll1_Singleton() { + testInvokeAll1(singletonPool()); + } + public void testInvokeAll1(ForkJoinPool pool) { 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); + invokeAll(f); + f.checkCompletedNormally(); }}; - testInvokeOnPool(mainPool(), a); + testInvokeOnPool(pool, a); } /** - * invokeAll(tasks) with 1 argument invokes task + * invokeAll(t1, t2) invokes all task arguments */ - public void testInvokeAll1() { + public void testInvokeAll2() { + testInvokeAll2(mainPool()); + } + public void testInvokeAll2_Singleton() { + testInvokeAll2(singletonPool()); + } + public void testInvokeAll2(ForkJoinPool pool) { RecursiveAction a = new CheckedRecursiveAction() { protected void realCompute() { - AsyncFib f = new AsyncFib(8); - invokeAll(f); - checkCompletedNormally(f); - assertEquals(21, f.number); + AsyncFib[] tasks = { + new AsyncFib(8), + new AsyncFib(9), + }; + invokeAll(tasks[0], tasks[1]); + for (AsyncFib task : tasks) assertTrue(task.isDone()); + for (AsyncFib task : tasks) task.checkCompletedNormally(); }}; - testInvokeOnPool(mainPool(), a); + testInvokeOnPool(pool, a); } /** * invokeAll(tasks) with > 2 argument invokes tasks */ public void testInvokeAll3() { + testInvokeAll3(mainPool()); + } + public void testInvokeAll3_Singleton() { + testInvokeAll3(singletonPool()); + } + public void testInvokeAll3(ForkJoinPool pool) { 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); + AsyncFib[] tasks = { + new AsyncFib(8), + new AsyncFib(9), + new AsyncFib(7), + }; + invokeAll(tasks[0], tasks[1], tasks[2]); + for (AsyncFib task : tasks) assertTrue(task.isDone()); + for (AsyncFib task : tasks) task.checkCompletedNormally(); }}; - testInvokeOnPool(mainPool(), a); + testInvokeOnPool(pool, a); } /** * invokeAll(collection) invokes all tasks in the collection */ public void testInvokeAllCollection() { + testInvokeAllCollection(mainPool()); + } + public void testInvokeAllCollection_Singleton() { + testInvokeAllCollection(singletonPool()); + } + public void testInvokeAllCollection(ForkJoinPool pool) { 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); + AsyncFib[] tasks = { + new AsyncFib(8), + new AsyncFib(9), + new AsyncFib(7), + }; + invokeAll(Arrays.asList(tasks)); + for (AsyncFib task : tasks) assertTrue(task.isDone()); + for (AsyncFib task : tasks) task.checkCompletedNormally(); }}; - testInvokeOnPool(mainPool(), a); + testInvokeOnPool(pool, a); } /** - * invokeAll(tasks) with any null task throws NPE + * invokeAll(tasks) with any null task throws NullPointerException */ - public void testInvokeAllNPE() { + public void testInvokeAllNullTask() { + testInvokeAllNullTask(mainPool()); + } + public void testInvokeAllNullTask_Singleton() { + testInvokeAllNullTask(singletonPool()); + } + public void testInvokeAllNullTask(ForkJoinPool pool) { 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) {} + AsyncFib nul = null; + Runnable[] throwingActions = { + () -> invokeAll(nul), + () -> invokeAll(nul, nul), + () -> invokeAll(new AsyncFib(8), new AsyncFib(9), nul), + () -> invokeAll(new AsyncFib(8), nul, new AsyncFib(9)), + () -> invokeAll(nul, new AsyncFib(8), new AsyncFib(9)), + }; + assertThrows(NullPointerException.class, throwingActions); }}; - testInvokeOnPool(mainPool(), a); + testInvokeOnPool(pool, a); } /** - * invokeAll(t1, t2) throw exception if any task does + * invokeAll(tasks) with 1 argument throws exception if task does */ - public void testAbnormalInvokeAll2() { + public void testAbnormalInvokeAll1() { + testAbnormalInvokeAll1(mainPool()); + } + public void testAbnormalInvokeAll1_Singleton() { + testAbnormalInvokeAll1(singletonPool()); + } + public void testAbnormalInvokeAll1(ForkJoinPool pool) { RecursiveAction a = new CheckedRecursiveAction() { protected void realCompute() { - AsyncFib f = new AsyncFib(8); FailingAsyncFib g = new FailingAsyncFib(9); try { - invokeAll(f, g); + invokeAll(g); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(g, success); } }}; - testInvokeOnPool(mainPool(), a); + testInvokeOnPool(pool, a); } /** - * invokeAll(tasks) with 1 argument throws exception if task does + * invokeAll(t1, t2) throw exception if any task does */ - public void testAbnormalInvokeAll1() { + public void testAbnormalInvokeAll2() { + testAbnormalInvokeAll2(mainPool()); + } + public void testAbnormalInvokeAll2_Singleton() { + testAbnormalInvokeAll2(singletonPool()); + } + public void testAbnormalInvokeAll2(ForkJoinPool pool) { RecursiveAction a = new CheckedRecursiveAction() { protected void realCompute() { + AsyncFib f = new AsyncFib(8); FailingAsyncFib g = new FailingAsyncFib(9); + ForkJoinTask[] tasks = { f, g }; + Collections.shuffle(Arrays.asList(tasks)); try { - invokeAll(g); + invokeAll(tasks[0], tasks[1]); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(g, success); } }}; - testInvokeOnPool(mainPool(), a); + testInvokeOnPool(pool, a); } /** * invokeAll(tasks) with > 2 argument throws exception if any task does */ public void testAbnormalInvokeAll3() { + testAbnormalInvokeAll3(mainPool()); + } + public void testAbnormalInvokeAll3_Singleton() { + testAbnormalInvokeAll3(singletonPool()); + } + public void testAbnormalInvokeAll3(ForkJoinPool pool) { 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 }; + Collections.shuffle(Arrays.asList(tasks)); try { - invokeAll(f, g, h); + invokeAll(tasks[0], tasks[1], tasks[2]); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(g, success); } }}; - testInvokeOnPool(mainPool(), a); + testInvokeOnPool(pool, a); } /** - * invokeAll(collection) throws exception if any task does + * invokeAll(collection) throws exception if any task does */ public void testAbnormalInvokeAllCollection() { + testAbnormalInvokeAllCollection(mainPool()); + } + public void testAbnormalInvokeAllCollection_Singleton() { + testAbnormalInvokeAllCollection(singletonPool()); + } + public void testAbnormalInvokeAllCollection(ForkJoinPool pool) { RecursiveAction a = new CheckedRecursiveAction() { 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 }; + Collections.shuffle(Arrays.asList(tasks)); try { - invokeAll(set); + invokeAll(Arrays.asList(tasks)); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(f, success); } }}; - testInvokeOnPool(mainPool(), a); + testInvokeOnPool(pool, a); } /** @@ -832,7 +990,7 @@ public class ForkJoinTask8Test extends J assertTrue(f.tryUnfork()); helpQuiesce(); checkNotDone(f); - checkCompletedNormally(g); + g.checkCompletedNormally(); }}; testInvokeOnPool(singletonPool(), a); } @@ -853,9 +1011,9 @@ public class ForkJoinTask8Test extends J assertTrue(getSurplusQueuedTaskCount() > 0); helpQuiesce(); assertEquals(0, getSurplusQueuedTaskCount()); - checkCompletedNormally(f); - checkCompletedNormally(g); - checkCompletedNormally(h); + f.checkCompletedNormally(); + g.checkCompletedNormally(); + h.checkCompletedNormally(); }}; testInvokeOnPool(singletonPool(), a); } @@ -872,9 +1030,9 @@ public class ForkJoinTask8Test extends J assertSame(f, f.fork()); assertSame(f, peekNextLocalTask()); assertNull(f.join()); - checkCompletedNormally(f); + f.checkCompletedNormally(); helpQuiesce(); - checkCompletedNormally(g); + g.checkCompletedNormally(); }}; testInvokeOnPool(singletonPool(), a); } @@ -893,8 +1051,7 @@ public class ForkJoinTask8Test extends J assertSame(f, pollNextLocalTask()); helpQuiesce(); checkNotDone(f); - assertEquals(34, g.number); - checkCompletedNormally(g); + g.checkCompletedNormally(); }}; testInvokeOnPool(singletonPool(), a); } @@ -912,7 +1069,7 @@ public class ForkJoinTask8Test extends J assertSame(f, pollTask()); helpQuiesce(); checkNotDone(f); - checkCompletedNormally(g); + g.checkCompletedNormally(); }}; testInvokeOnPool(singletonPool(), a); } @@ -930,9 +1087,8 @@ public class ForkJoinTask8Test extends J assertSame(g, peekNextLocalTask()); assertNull(f.join()); helpQuiesce(); - checkCompletedNormally(f); - assertEquals(34, g.number); - checkCompletedNormally(g); + f.checkCompletedNormally(); + g.checkCompletedNormally(); }}; testInvokeOnPool(asyncSingletonPool(), a); } @@ -950,8 +1106,7 @@ public class ForkJoinTask8Test extends J assertSame(f, f.fork()); assertSame(g, pollNextLocalTask()); helpQuiesce(); - assertEquals(21, f.number); - checkCompletedNormally(f); + f.checkCompletedNormally(); checkNotDone(g); }}; testInvokeOnPool(asyncSingletonPool(), a); @@ -970,432 +1125,12 @@ public class ForkJoinTask8Test extends J assertSame(f, f.fork()); assertSame(g, pollTask()); helpQuiesce(); - assertEquals(21, f.number); - checkCompletedNormally(f); + f.checkCompletedNormally(); 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()); - }}; - 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); - try { - invokeAll(f, g); - 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); - try { - invokeAll(f, g, h); - 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); - HashSet set = new HashSet(); - set.add(f); - set.add(g); - set.add(h); - try { - invokeAll(set); - 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 @@ -1417,4 +1152,41 @@ public class ForkJoinTask8Test extends J testInvokeOnPool(mainPool(), a); } + // jdk9 + + /** + * pollSubmission returns unexecuted submitted task, if present + */ + public void testPollSubmission() { + final CountDownLatch done = new CountDownLatch(1); + final ForkJoinTask a = ForkJoinTask.adapt(awaiter(done)); + final ForkJoinTask b = ForkJoinTask.adapt(awaiter(done)); + final ForkJoinTask c = ForkJoinTask.adapt(awaiter(done)); + final ForkJoinPool p = singletonPool(); + try (PoolCleaner cleaner = cleaner(p)) { + Thread external = new Thread(new CheckedRunnable() { + public void realRun() { + p.execute(a); + p.execute(b); + p.execute(c); + }}); + RecursiveAction s = new CheckedRecursiveAction() { + protected void realCompute() { + external.start(); + try { + external.join(); + } catch (Exception ex) { + threadUnexpectedException(ex); + } + assertTrue(p.hasQueuedSubmissions()); + assertTrue(Thread.currentThread() instanceof ForkJoinWorkerThread); + ForkJoinTask r = ForkJoinTask.pollSubmission(); + assertTrue(r == a || r == b || r == c); + assertFalse(r.isDone()); + }}; + p.invoke(s); + done.countDown(); + } + } + }