--- jsr166/src/test/tck/ForkJoinTaskTest.java 2010/11/22 07:40:24 1.26 +++ jsr166/src/test/tck/ForkJoinTaskTest.java 2016/08/24 22:22:39 1.51 @@ -1,26 +1,29 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ */ -import java.util.concurrent.ExecutionException; + +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.SECONDS; + +import java.util.Arrays; +import java.util.HashSet; import java.util.concurrent.CancellationException; +import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; -import java.util.concurrent.ForkJoinWorkerThread; import java.util.concurrent.RecursiveAction; -import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import static java.util.concurrent.TimeUnit.SECONDS; -import java.util.HashSet; -import junit.framework.*; + +import junit.framework.Test; +import junit.framework.TestSuite; public class ForkJoinTaskTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { @@ -46,7 +49,7 @@ public class ForkJoinTaskTest extends JS } private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { - try { + try (PoolCleaner cleaner = cleaner(pool)) { assertFalse(a.isDone()); assertFalse(a.isCompletedNormally()); assertFalse(a.isCompletedAbnormally()); @@ -62,8 +65,6 @@ public class ForkJoinTaskTest extends JS assertFalse(a.isCancelled()); assertNull(a.getException()); assertNull(a.getRawResult()); - } finally { - joinPool(pool); } } @@ -93,7 +94,23 @@ public class ForkJoinTaskTest extends JS assertFalse(a.isCompletedAbnormally()); assertNull(a.getException()); assertSame(expected, a.getRawResult()); - assertSame(expected, a.join()); + + { + 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 { @@ -115,10 +132,18 @@ public class ForkJoinTaskTest extends JS 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(); @@ -138,30 +163,38 @@ public class ForkJoinTaskTest extends JS assertFalse(a.isCancelled()); assertFalse(a.isCompletedNormally()); assertTrue(a.isCompletedAbnormally()); - assertSame(t, a.getException()); + assertSame(t.getClass(), a.getException().getClass()); assertNull(a.getRawResult()); assertFalse(a.cancel(false)); assertFalse(a.cancel(true)); try { + Thread.currentThread().interrupt(); a.join(); shouldThrow(); } catch (Throwable expected) { - assertSame(t, 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, success.getCause()); + assertSame(t.getClass(), success.getCause().getClass()); } catch (Throwable fail) { threadUnexpectedException(fail); } try { a.get(5L, SECONDS); shouldThrow(); } catch (ExecutionException success) { - assertSame(t, success.getCause()); + assertSame(t.getClass(), success.getCause().getClass()); } catch (Throwable fail) { threadUnexpectedException(fail); } } @@ -173,7 +206,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(); } } @@ -184,9 +217,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() { } @@ -221,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 (;;) { @@ -242,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; } } @@ -298,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; @@ -317,7 +354,6 @@ public class ForkJoinTaskTest extends JS } } - static final class FailingAsyncFib extends BinaryAsyncAction { int number; public FailingAsyncFib(int n) { @@ -327,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; @@ -353,7 +386,7 @@ public class ForkJoinTaskTest extends JS */ public void testInvoke() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertNull(f.invoke()); assertEquals(21, f.number); @@ -369,7 +402,7 @@ public class ForkJoinTaskTest extends JS */ public void testQuietlyInvoke() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); f.quietlyInvoke(); assertEquals(21, f.number); @@ -383,7 +416,7 @@ public class ForkJoinTaskTest extends JS */ public void testForkJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); assertNull(f.join()); @@ -398,7 +431,7 @@ public class ForkJoinTaskTest extends JS */ public void testForkGet() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); assertNull(f.get()); @@ -413,7 +446,7 @@ public class ForkJoinTaskTest extends JS */ public void testForkTimedGet() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); @@ -428,7 +461,7 @@ 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 { @@ -444,7 +477,7 @@ public class ForkJoinTaskTest extends JS */ public void testForkQuietlyJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); f.quietlyJoin(); @@ -454,17 +487,16 @@ public class ForkJoinTaskTest extends JS testInvokeOnPool(mainPool(), a); } - /** * helpQuiesce returns when tasks are complete. * getQueuedTaskCount returns 0 when quiescent */ public void testForkHelpQuiesce() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); - f.helpQuiesce(); + helpQuiesce(); assertEquals(21, f.number); assertEquals(0, getQueuedTaskCount()); checkCompletedNormally(f); @@ -472,13 +504,12 @@ public class ForkJoinTaskTest extends JS testInvokeOnPool(mainPool(), a); } - /** * invoke task throws exception when task completes abnormally */ public void testAbnormalInvoke() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); try { f.invoke(); @@ -495,7 +526,7 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalQuietlyInvoke() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); f.quietlyInvoke(); assertTrue(f.getException() instanceof FJException); @@ -509,7 +540,7 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalForkJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); assertSame(f, f.fork()); try { @@ -527,7 +558,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 { @@ -547,7 +578,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 { @@ -567,7 +598,7 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalForkQuietlyJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); assertSame(f, f.fork()); f.quietlyJoin(); @@ -582,7 +613,7 @@ public class ForkJoinTaskTest extends JS */ public void testCancelledInvoke() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertTrue(f.cancel(true)); try { @@ -600,7 +631,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()); @@ -619,7 +650,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()); @@ -638,7 +669,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()); @@ -657,7 +688,7 @@ public class ForkJoinTaskTest extends JS */ public void testCancelledForkQuietlyJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); @@ -673,7 +704,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); @@ -684,7 +715,7 @@ public class ForkJoinTaskTest extends JS */ public void testGetPool2() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { assertNull(getPool()); }}; assertNull(a.invoke()); @@ -695,7 +726,7 @@ public class ForkJoinTaskTest extends JS */ public void testInForkJoinPool() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { assertTrue(inForkJoinPool()); }}; testInvokeOnPool(mainPool(), a); @@ -706,7 +737,7 @@ public class ForkJoinTaskTest extends JS */ public void testInForkJoinPool2() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { assertFalse(inForkJoinPool()); }}; assertNull(a.invoke()); @@ -717,8 +748,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()); } @@ -728,7 +760,7 @@ public class ForkJoinTaskTest extends JS */ public void testCompleteExceptionally() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); f.completeExceptionally(new FJException()); try { @@ -742,11 +774,32 @@ public class ForkJoinTaskTest extends JS } /** + * completeExceptionally(null) surprisingly has the same effect as + * completeExceptionally(new RuntimeException()) + */ + public void testCompleteExceptionally_null() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + AsyncFib f = new AsyncFib(8); + f.completeExceptionally(null); + try { + f.invoke(); + shouldThrow(); + } catch (RuntimeException success) { + assertSame(success.getClass(), RuntimeException.class); + assertNull(success.getCause()); + checkCompletedAbnormally(f, success); + } + }}; + testInvokeOnPool(mainPool(), a); + } + + /** * invokeAll(t1, t2) invokes all task arguments */ public void testInvokeAll2() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); AsyncFib g = new AsyncFib(9); invokeAll(f, g); @@ -763,7 +816,7 @@ public class ForkJoinTaskTest extends JS */ public void testInvokeAll1() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); invokeAll(f); checkCompletedNormally(f); @@ -777,7 +830,7 @@ public class ForkJoinTaskTest extends JS */ public void testInvokeAll3() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); AsyncFib g = new AsyncFib(9); AsyncFib h = new AsyncFib(7); @@ -797,7 +850,7 @@ public class ForkJoinTaskTest extends JS */ public void testInvokeAllCollection() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); AsyncFib g = new AsyncFib(9); AsyncFib h = new AsyncFib(7); @@ -816,13 +869,12 @@ public class ForkJoinTaskTest extends JS testInvokeOnPool(mainPool(), a); } - /** * invokeAll(tasks) with any null task throws NPE */ public void testInvokeAllNPE() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); AsyncFib g = new AsyncFib(9); AsyncFib h = null; @@ -839,11 +891,13 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalInvokeAll2() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); FailingAsyncFib g = new FailingAsyncFib(9); + ForkJoinTask[] tasks = { f, g }; + shuffle(tasks); try { - invokeAll(f, g); + invokeAll(tasks); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(g, success); @@ -857,7 +911,7 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalInvokeAll1() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingAsyncFib g = new FailingAsyncFib(9); try { invokeAll(g); @@ -874,12 +928,14 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalInvokeAll3() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); FailingAsyncFib g = new FailingAsyncFib(9); AsyncFib h = new AsyncFib(7); + ForkJoinTask[] tasks = { f, g, h }; + shuffle(tasks); try { - invokeAll(f, g, h); + invokeAll(tasks); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(g, success); @@ -889,20 +945,18 @@ public class ForkJoinTaskTest extends JS } /** - * invokeAll(collection) throws exception if any task does + * invokeAll(collection) throws exception if any task does */ public void testAbnormalInvokeAllCollection() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); AsyncFib g = new AsyncFib(9); AsyncFib h = new AsyncFib(7); - HashSet set = new HashSet(); - set.add(f); - set.add(g); - set.add(h); + ForkJoinTask[] tasks = { f, g, h }; + shuffle(tasks); try { - invokeAll(set); + invokeAll(Arrays.asList(tasks)); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(f, success); @@ -917,7 +971,7 @@ public class ForkJoinTaskTest extends JS */ public void testTryUnfork() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib g = new AsyncFib(9); assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); @@ -936,7 +990,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); @@ -958,7 +1012,7 @@ public class ForkJoinTaskTest extends JS */ public void testPeekNextLocalTask() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib g = new AsyncFib(9); assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); @@ -978,7 +1032,7 @@ public class ForkJoinTaskTest extends JS */ public void testPollNextLocalTask() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib g = new AsyncFib(9); assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); @@ -997,7 +1051,7 @@ public class ForkJoinTaskTest extends JS */ public void testPollTask() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib g = new AsyncFib(9); assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); @@ -1015,7 +1069,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); @@ -1036,7 +1090,7 @@ public class ForkJoinTaskTest extends JS */ public void testPollNextLocalTaskAsync() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib g = new AsyncFib(9); assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); @@ -1056,7 +1110,7 @@ public class ForkJoinTaskTest extends JS */ public void testPollTaskAsync() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib g = new AsyncFib(9); assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); @@ -1079,7 +1133,7 @@ public class ForkJoinTaskTest extends JS */ public void testInvokeSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertNull(f.invoke()); assertEquals(21, f.number); @@ -1095,7 +1149,7 @@ public class ForkJoinTaskTest extends JS */ public void testQuietlyInvokeSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); f.quietlyInvoke(); assertEquals(21, f.number); @@ -1109,7 +1163,7 @@ public class ForkJoinTaskTest extends JS */ public void testForkJoinSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); assertNull(f.join()); @@ -1124,7 +1178,7 @@ public class ForkJoinTaskTest extends JS */ public void testForkGetSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); assertNull(f.get()); @@ -1139,7 +1193,7 @@ public class ForkJoinTaskTest extends JS */ public void testForkTimedGetSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); @@ -1154,7 +1208,7 @@ 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 { @@ -1170,7 +1224,7 @@ public class ForkJoinTaskTest extends JS */ public void testForkQuietlyJoinSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); f.quietlyJoin(); @@ -1180,17 +1234,16 @@ public class ForkJoinTaskTest extends JS testInvokeOnPool(singletonPool(), a); } - /** * helpQuiesce returns when tasks are complete. * getQueuedTaskCount returns 0 when quiescent */ public void testForkHelpQuiesceSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); - f.helpQuiesce(); + helpQuiesce(); assertEquals(0, getQueuedTaskCount()); assertEquals(21, f.number); checkCompletedNormally(f); @@ -1198,13 +1251,12 @@ public class ForkJoinTaskTest extends JS testInvokeOnPool(singletonPool(), a); } - /** * invoke task throws exception when task completes abnormally */ public void testAbnormalInvokeSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); try { f.invoke(); @@ -1221,7 +1273,7 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalQuietlyInvokeSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); f.quietlyInvoke(); assertTrue(f.getException() instanceof FJException); @@ -1235,7 +1287,7 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalForkJoinSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); assertSame(f, f.fork()); try { @@ -1253,7 +1305,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 { @@ -1273,7 +1325,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 { @@ -1293,7 +1345,7 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalForkQuietlyJoinSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); assertSame(f, f.fork()); f.quietlyJoin(); @@ -1308,7 +1360,7 @@ public class ForkJoinTaskTest extends JS */ public void testCancelledInvokeSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertTrue(f.cancel(true)); try { @@ -1326,7 +1378,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()); @@ -1345,7 +1397,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()); @@ -1364,7 +1416,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()); @@ -1383,7 +1435,7 @@ public class ForkJoinTaskTest extends JS */ public void testCancelledForkQuietlyJoinSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); @@ -1398,7 +1450,7 @@ public class ForkJoinTaskTest extends JS */ public void testCompleteExceptionallySingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); f.completeExceptionally(new FJException()); try { @@ -1416,7 +1468,7 @@ public class ForkJoinTaskTest extends JS */ public void testInvokeAll2Singleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); AsyncFib g = new AsyncFib(9); invokeAll(f, g); @@ -1433,7 +1485,7 @@ public class ForkJoinTaskTest extends JS */ public void testInvokeAll1Singleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); invokeAll(f); checkCompletedNormally(f); @@ -1447,7 +1499,7 @@ public class ForkJoinTaskTest extends JS */ public void testInvokeAll3Singleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); AsyncFib g = new AsyncFib(9); AsyncFib h = new AsyncFib(7); @@ -1467,7 +1519,7 @@ public class ForkJoinTaskTest extends JS */ public void testInvokeAllCollectionSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); AsyncFib g = new AsyncFib(9); AsyncFib h = new AsyncFib(7); @@ -1486,13 +1538,12 @@ public class ForkJoinTaskTest extends JS testInvokeOnPool(singletonPool(), a); } - /** * invokeAll(tasks) with any null task throws NPE */ public void testInvokeAllNPESingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); AsyncFib g = new AsyncFib(9); AsyncFib h = null; @@ -1509,11 +1560,13 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalInvokeAll2Singleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); FailingAsyncFib g = new FailingAsyncFib(9); + ForkJoinTask[] tasks = { f, g }; + shuffle(tasks); try { - invokeAll(f, g); + invokeAll(tasks); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(g, success); @@ -1527,7 +1580,7 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalInvokeAll1Singleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingAsyncFib g = new FailingAsyncFib(9); try { invokeAll(g); @@ -1544,12 +1597,14 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalInvokeAll3Singleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); FailingAsyncFib g = new FailingAsyncFib(9); AsyncFib h = new AsyncFib(7); + ForkJoinTask[] tasks = { f, g, h }; + shuffle(tasks); try { - invokeAll(f, g, h); + invokeAll(tasks); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(g, success); @@ -1559,20 +1614,18 @@ public class ForkJoinTaskTest extends JS } /** - * invokeAll(collection) throws exception if any task does + * invokeAll(collection) throws exception if any task does */ public void testAbnormalInvokeAllCollectionSingleton() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); AsyncFib g = new AsyncFib(9); AsyncFib h = new AsyncFib(7); - HashSet set = new HashSet(); - set.add(f); - set.add(g); - set.add(h); + ForkJoinTask[] tasks = { f, g, h }; + shuffle(tasks); try { - invokeAll(set); + invokeAll(Arrays.asList(tasks)); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(f, success); @@ -1581,4 +1634,21 @@ public class ForkJoinTaskTest extends JS testInvokeOnPool(singletonPool(), a); } + /** + * ForkJoinTask.quietlyComplete returns when task completes + * normally without setting a value. The most recent value + * established by setRawResult(V) (or null by default) is returned + * from invoke. + */ + public void testQuietlyComplete() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + AsyncFib f = new AsyncFib(8); + f.quietlyComplete(); + assertEquals(8, f.number); + checkCompletedNormally(f); + }}; + testInvokeOnPool(mainPool(), a); + } + }