--- jsr166/src/test/tck/ForkJoinTaskTest.java 2010/11/22 07:50:50 1.27 +++ jsr166/src/test/tck/ForkJoinTaskTest.java 2015/10/11 19:53:59 1.48 @@ -1,26 +1,31 @@ /* * 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.Collections; +import java.util.HashSet; +import java.util.List; 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 +51,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 +67,6 @@ public class ForkJoinTaskTest extends JS assertFalse(a.isCancelled()); assertNull(a.getException()); assertNull(a.getRawResult()); - } finally { - joinPool(pool); } } @@ -93,7 +96,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 +134,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 +165,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 +208,7 @@ public class ForkJoinTaskTest extends JS * differently than supplied Recursive forms. */ - static final class FJException extends RuntimeException { + public static final class FJException extends RuntimeException { FJException() { super(); } } @@ -184,9 +219,9 @@ public class ForkJoinTaskTest extends JS AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class, "controlState"); - private BinaryAsyncAction parent; + private volatile BinaryAsyncAction parent; - private BinaryAsyncAction sibling; + private volatile BinaryAsyncAction sibling; protected BinaryAsyncAction() { } @@ -221,6 +256,14 @@ public class ForkJoinTaskTest extends JS super.completeExceptionally(ex); } + public boolean cancel(boolean mayInterruptIfRunning) { + if (super.cancel(mayInterruptIfRunning)) { + completeExceptionally(new FJException()); + return true; + } + return false; + } + public final void complete() { BinaryAsyncAction a = this; for (;;) { @@ -242,13 +285,12 @@ public class ForkJoinTaskTest extends JS } public final void completeExceptionally(Throwable ex) { - BinaryAsyncAction a = this; - while (!a.isCompletedAbnormally()) { + for (BinaryAsyncAction a = this;;) { a.completeThisExceptionally(ex); BinaryAsyncAction s = a.sibling; - if (s != null) - s.cancel(false); - if (!a.onException() || (a = a.parent) == null) + if (s != null && !s.isDone()) + s.completeExceptionally(ex); + if ((a = a.parent) == null) break; } } @@ -298,15 +340,12 @@ public class ForkJoinTaskTest extends JS public final boolean exec() { AsyncFib f = this; int n = f.number; - if (n > 1) { - while (n > 1) { - AsyncFib p = f; - AsyncFib r = new AsyncFib(n - 2); - f = new AsyncFib(--n); - p.linkSubtasks(r, f); - r.fork(); - } - f.number = n; + while (n > 1) { + AsyncFib p = f; + AsyncFib r = new AsyncFib(n - 2); + f = new AsyncFib(--n); + p.linkSubtasks(r, f); + r.fork(); } f.complete(); return false; @@ -317,7 +356,6 @@ public class ForkJoinTaskTest extends JS } } - static final class FailingAsyncFib extends BinaryAsyncAction { int number; public FailingAsyncFib(int n) { @@ -327,15 +365,12 @@ public class ForkJoinTaskTest extends JS public final boolean exec() { FailingAsyncFib f = this; int n = f.number; - if (n > 1) { - while (n > 1) { - FailingAsyncFib p = f; - FailingAsyncFib r = new FailingAsyncFib(n - 2); - f = new FailingAsyncFib(--n); - p.linkSubtasks(r, f); - r.fork(); - } - f.number = n; + while (n > 1) { + FailingAsyncFib p = f; + FailingAsyncFib r = new FailingAsyncFib(n - 2); + f = new FailingAsyncFib(--n); + p.linkSubtasks(r, f); + r.fork(); } f.complete(); return false; @@ -353,7 +388,7 @@ public class ForkJoinTaskTest extends JS */ public void testInvoke() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertNull(f.invoke()); assertEquals(21, f.number); @@ -369,7 +404,7 @@ public class ForkJoinTaskTest extends JS */ public void testQuietlyInvoke() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); f.quietlyInvoke(); assertEquals(21, f.number); @@ -383,7 +418,7 @@ public class ForkJoinTaskTest extends JS */ public void testForkJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); assertNull(f.join()); @@ -398,7 +433,7 @@ public class ForkJoinTaskTest extends JS */ public void testForkGet() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); assertNull(f.get()); @@ -413,7 +448,7 @@ public class ForkJoinTaskTest extends JS */ public void testForkTimedGet() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); @@ -428,7 +463,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 +479,7 @@ public class ForkJoinTaskTest extends JS */ public void testForkQuietlyJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); f.quietlyJoin(); @@ -454,17 +489,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 +506,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 +528,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 +542,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 +560,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 +580,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 +600,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 +615,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 +633,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 +652,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 +671,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 +690,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 +706,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 +717,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 +728,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 +739,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,7 +750,7 @@ public class ForkJoinTaskTest extends JS */ public void testSetRawResult() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { setRawResult(null); assertNull(getRawResult()); }}; @@ -729,7 +762,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 { @@ -747,7 +780,7 @@ public class ForkJoinTaskTest extends JS */ public void testInvokeAll2() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { AsyncFib f = new AsyncFib(8); AsyncFib g = new AsyncFib(9); invokeAll(f, g); @@ -764,7 +797,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); @@ -778,7 +811,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); @@ -798,7 +831,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); @@ -817,13 +850,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; @@ -840,11 +872,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 }; + Collections.shuffle(Arrays.asList(tasks)); try { - invokeAll(f, g); + invokeAll(tasks); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(g, success); @@ -858,7 +892,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); @@ -875,12 +909,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 }; + Collections.shuffle(Arrays.asList(tasks)); try { - invokeAll(f, g, h); + invokeAll(tasks); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(g, success); @@ -890,20 +926,19 @@ 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 }; + List taskList = Arrays.asList(tasks); + Collections.shuffle(taskList); try { - invokeAll(set); + invokeAll(taskList); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(f, success); @@ -918,7 +953,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); @@ -937,7 +972,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); @@ -959,7 +994,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); @@ -979,7 +1014,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); @@ -998,7 +1033,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); @@ -1016,7 +1051,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); @@ -1037,7 +1072,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); @@ -1057,7 +1092,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); @@ -1080,7 +1115,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); @@ -1096,7 +1131,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); @@ -1110,7 +1145,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()); @@ -1125,7 +1160,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()); @@ -1140,7 +1175,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)); @@ -1155,7 +1190,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 { @@ -1171,7 +1206,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(); @@ -1181,17 +1216,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); @@ -1199,13 +1233,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(); @@ -1222,7 +1255,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); @@ -1236,7 +1269,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 { @@ -1254,7 +1287,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 { @@ -1274,7 +1307,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 { @@ -1294,7 +1327,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(); @@ -1309,7 +1342,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 { @@ -1327,7 +1360,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()); @@ -1346,7 +1379,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()); @@ -1365,7 +1398,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()); @@ -1384,7 +1417,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()); @@ -1399,7 +1432,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 { @@ -1417,7 +1450,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); @@ -1434,7 +1467,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); @@ -1448,7 +1481,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); @@ -1468,7 +1501,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); @@ -1487,13 +1520,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; @@ -1510,11 +1542,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 }; + Collections.shuffle(Arrays.asList(tasks)); try { - invokeAll(f, g); + invokeAll(tasks); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(g, success); @@ -1528,7 +1562,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); @@ -1545,12 +1579,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 }; + Collections.shuffle(Arrays.asList(tasks)); try { - invokeAll(f, g, h); + invokeAll(tasks); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(g, success); @@ -1560,20 +1596,19 @@ 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 }; + List taskList = Arrays.asList(tasks); + Collections.shuffle(taskList); try { - invokeAll(set); + invokeAll(taskList); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(f, success); @@ -1582,4 +1617,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); + } + }