--- jsr166/src/test/tck/ForkJoinTaskTest.java 2010/09/13 07:51:18 1.14 +++ jsr166/src/test/tck/ForkJoinTaskTest.java 2010/10/24 13:41:17 1.20 @@ -10,8 +10,9 @@ import java.util.concurrent.ForkJoinTask import java.util.concurrent.ForkJoinWorkerThread; import java.util.concurrent.RecursiveAction; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.*; -import java.util.*; +import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import java.util.HashSet; import junit.framework.*; public class ForkJoinTaskTest extends JSR166TestCase { @@ -24,8 +25,12 @@ public class ForkJoinTaskTest extends JS return new TestSuite(ForkJoinTaskTest.class); } + // Runs with "mainPool" use > 1 thread. singletonPool tests use 1 + static final int mainPoolSize = + Math.max(2, Runtime.getRuntime().availableProcessors()); + private static ForkJoinPool mainPool() { - return new ForkJoinPool(); + return new ForkJoinPool(mainPoolSize); } private static ForkJoinPool singletonPool() { @@ -40,7 +45,19 @@ public class ForkJoinTaskTest extends JS private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { try { - assertTrue(pool.invoke(a) == null); + assertFalse(a.isDone()); + assertFalse(a.isCompletedNormally()); + assertFalse(a.isCompletedAbnormally()); + assertFalse(a.isCancelled()); + assertNull(a.getException()); + + assertNull(pool.invoke(a)); + + assertTrue(a.isDone()); + assertTrue(a.isCompletedNormally()); + assertFalse(a.isCompletedAbnormally()); + assertFalse(a.isCancelled()); + assertNull(a.getException()); } finally { joinPool(pool); } @@ -58,7 +75,7 @@ public class ForkJoinTaskTest extends JS FJException() { super(); } } - static abstract class BinaryAsyncAction extends ForkJoinTask { + abstract static class BinaryAsyncAction extends ForkJoinTask { private volatile int controlState; static final AtomicIntegerFieldUpdater controlStateUpdater = @@ -233,15 +250,15 @@ public class ForkJoinTaskTest extends JS * completed tasks. getRawResult of a RecursiveAction returns null; */ public void testInvoke() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { AsyncFib f = new AsyncFib(8); - f.invoke(); - threadAssertTrue(f.number == 21); - threadAssertTrue(f.isDone()); - threadAssertFalse(f.isCancelled()); - threadAssertFalse(f.isCompletedAbnormally()); - threadAssertTrue(f.getRawResult() == null); + assertNull(f.invoke()); + assertEquals(21, f.number); + assertTrue(f.isDone()); + assertFalse(f.isCancelled()); + assertFalse(f.isCompletedAbnormally()); + assertNull(f.getRawResult()); }}; testInvokeOnPool(mainPool(), a); } @@ -252,15 +269,15 @@ public class ForkJoinTaskTest extends JS * completed tasks */ public void testQuietlyInvoke() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { AsyncFib f = new AsyncFib(8); f.quietlyInvoke(); - threadAssertTrue(f.number == 21); - threadAssertTrue(f.isDone()); - threadAssertFalse(f.isCancelled()); - threadAssertFalse(f.isCompletedAbnormally()); - threadAssertTrue(f.getRawResult() == null); + assertEquals(21, f.number); + assertTrue(f.isDone()); + assertFalse(f.isCancelled()); + assertFalse(f.isCompletedAbnormally()); + assertNull(f.getRawResult()); }}; testInvokeOnPool(mainPool(), a); } @@ -269,14 +286,14 @@ public class ForkJoinTaskTest extends JS * join of a forked task returns when task completes */ public void testForkJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { AsyncFib f = new AsyncFib(8); - f.fork(); - f.join(); - threadAssertTrue(f.number == 21); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.getRawResult() == null); + assertSame(f, f.fork()); + assertNull(f.join()); + assertEquals(21, f.number); + assertTrue(f.isDone()); + assertNull(f.getRawResult()); }}; testInvokeOnPool(mainPool(), a); } @@ -285,17 +302,13 @@ public class ForkJoinTaskTest extends JS * get of a forked task returns when task completes */ public void testForkGet() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - AsyncFib f = new AsyncFib(8); - f.fork(); - f.get(); - threadAssertTrue(f.number == 21); - threadAssertTrue(f.isDone()); - } catch (Exception ex) { - unexpectedException(ex); - } + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() throws Exception { + AsyncFib f = new AsyncFib(8); + assertSame(f, f.fork()); + assertNull(f.get()); + assertEquals(21, f.number); + assertTrue(f.isDone()); }}; testInvokeOnPool(mainPool(), a); } @@ -304,17 +317,13 @@ public class ForkJoinTaskTest extends JS * timed get of a forked task returns when task completes */ public void testForkTimedGet() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - AsyncFib f = new AsyncFib(8); - f.fork(); - f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS); - threadAssertTrue(f.number == 21); - threadAssertTrue(f.isDone()); - } catch (Exception ex) { - unexpectedException(ex); - } + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() throws Exception { + AsyncFib f = new AsyncFib(8); + assertSame(f, f.fork()); + assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); + assertEquals(21, f.number); + assertTrue(f.isDone()); }}; testInvokeOnPool(mainPool(), a); } @@ -323,17 +332,14 @@ public class ForkJoinTaskTest extends JS * timed get with null time unit throws NPE */ public void testForkTimedGetNPE() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() throws Exception { + AsyncFib f = new AsyncFib(8); + assertSame(f, f.fork()); try { - AsyncFib f = new AsyncFib(8); - f.fork(); f.get(5L, null); shouldThrow(); - } catch (NullPointerException success) { - } catch (Exception ex) { - unexpectedException(ex); - } + } catch (NullPointerException success) {} }}; testInvokeOnPool(mainPool(), a); } @@ -342,13 +348,13 @@ public class ForkJoinTaskTest extends JS * quietlyJoin of a forked task returns when task completes */ public void testForkQuietlyJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { AsyncFib f = new AsyncFib(8); - f.fork(); + assertSame(f, f.fork()); f.quietlyJoin(); - threadAssertTrue(f.number == 21); - threadAssertTrue(f.isDone()); + assertEquals(21, f.number); + assertTrue(f.isDone()); }}; testInvokeOnPool(mainPool(), a); } @@ -359,14 +365,14 @@ public class ForkJoinTaskTest extends JS * getQueuedTaskCount returns 0 when quiescent */ public void testForkHelpQuiesce() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { AsyncFib f = new AsyncFib(8); - f.fork(); + assertSame(f, f.fork()); f.helpQuiesce(); - threadAssertTrue(f.number == 21); - threadAssertTrue(f.isDone()); - threadAssertTrue(getQueuedTaskCount() == 0); + assertEquals(21, f.number); + assertTrue(f.isDone()); + assertEquals(0, getQueuedTaskCount()); }}; testInvokeOnPool(mainPool(), a); } @@ -376,14 +382,13 @@ public class ForkJoinTaskTest extends JS * invoke task throws exception when task completes abnormally */ public void testAbnormalInvoke() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + FailingAsyncFib f = new FailingAsyncFib(8); try { - FailingAsyncFib f = new FailingAsyncFib(8); f.invoke(); shouldThrow(); - } catch (FJException success) { - } + } catch (FJException success) {} }}; testInvokeOnPool(mainPool(), a); } @@ -392,11 +397,11 @@ public class ForkJoinTaskTest extends JS * quietlyInvoke task returns when task completes abnormally */ public void testAbnormalQuietlyInvoke() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); f.quietlyInvoke(); - threadAssertTrue(f.isDone()); + assertTrue(f.isDone()); }}; testInvokeOnPool(mainPool(), a); } @@ -405,15 +410,14 @@ public class ForkJoinTaskTest extends JS * join of a forked task throws exception when task completes abnormally */ public void testAbnormalForkJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + FailingAsyncFib f = new FailingAsyncFib(8); + assertSame(f, f.fork()); try { - FailingAsyncFib f = new FailingAsyncFib(8); - f.fork(); f.join(); shouldThrow(); - } catch (FJException success) { - } + } catch (FJException success) {} }}; testInvokeOnPool(mainPool(), a); } @@ -422,16 +426,19 @@ public class ForkJoinTaskTest extends JS * get of a forked task throws exception when task completes abnormally */ public void testAbnormalForkGet() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() throws Exception { + FailingAsyncFib f = new FailingAsyncFib(8); + assertSame(f, f.fork()); try { - FailingAsyncFib f = new FailingAsyncFib(8); - f.fork(); f.get(); shouldThrow(); } catch (ExecutionException success) { - } catch (Exception ex) { - unexpectedException(ex); + Throwable cause = success.getCause(); + assertTrue(cause instanceof FJException); + assertTrue(f.isDone()); + assertTrue(f.isCompletedAbnormally()); + assertSame(cause, f.getException()); } }}; testInvokeOnPool(mainPool(), a); @@ -441,16 +448,19 @@ public class ForkJoinTaskTest extends JS * timed get of a forked task throws exception when task completes abnormally */ public void testAbnormalForkTimedGet() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() throws Exception { + FailingAsyncFib f = new FailingAsyncFib(8); + assertSame(f, f.fork()); try { - FailingAsyncFib f = new FailingAsyncFib(8); - f.fork(); - f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS); + f.get(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (ExecutionException success) { - } catch (Exception ex) { - unexpectedException(ex); + Throwable cause = success.getCause(); + assertTrue(cause instanceof FJException); + assertTrue(f.isDone()); + assertTrue(f.isCompletedAbnormally()); + assertSame(cause, f.getException()); } }}; testInvokeOnPool(mainPool(), a); @@ -460,14 +470,14 @@ public class ForkJoinTaskTest extends JS * quietlyJoin of a forked task returns when task completes abnormally */ public void testAbnormalForkQuietlyJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); - f.fork(); + assertSame(f, f.fork()); f.quietlyJoin(); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.isCompletedAbnormally()); - threadAssertTrue(f.getException() instanceof FJException); + assertTrue(f.isDone()); + assertTrue(f.isCompletedAbnormally()); + assertTrue(f.getException() instanceof FJException); }}; testInvokeOnPool(mainPool(), a); } @@ -476,14 +486,18 @@ public class ForkJoinTaskTest extends JS * invoke task throws exception when task cancelled */ public void testCancelledInvoke() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + AsyncFib f = new AsyncFib(8); + assertTrue(f.cancel(true)); try { - AsyncFib f = new AsyncFib(8); - f.cancel(true); f.invoke(); shouldThrow(); } catch (CancellationException success) { + assertTrue(f.isDone()); + assertTrue(f.isCancelled()); + assertTrue(f.isCompletedAbnormally()); + assertTrue(f.getException() instanceof CancellationException); } }}; testInvokeOnPool(mainPool(), a); @@ -493,15 +507,19 @@ public class ForkJoinTaskTest extends JS * join of a forked task throws exception when task cancelled */ public void testCancelledForkJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + AsyncFib f = new AsyncFib(8); + assertTrue(f.cancel(true)); + assertSame(f, f.fork()); try { - AsyncFib f = new AsyncFib(8); - f.cancel(true); - f.fork(); f.join(); shouldThrow(); } catch (CancellationException success) { + assertTrue(f.isDone()); + assertTrue(f.isCancelled()); + assertTrue(f.isCompletedAbnormally()); + assertTrue(f.getException() instanceof CancellationException); } }}; testInvokeOnPool(mainPool(), a); @@ -511,17 +529,19 @@ public class ForkJoinTaskTest extends JS * get of a forked task throws exception when task cancelled */ public void testCancelledForkGet() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() throws Exception { + AsyncFib f = new AsyncFib(8); + assertTrue(f.cancel(true)); + assertSame(f, f.fork()); try { - AsyncFib f = new AsyncFib(8); - f.cancel(true); - f.fork(); f.get(); shouldThrow(); } catch (CancellationException success) { - } catch (Exception ex) { - unexpectedException(ex); + assertTrue(f.isDone()); + assertTrue(f.isCancelled()); + assertTrue(f.isCompletedAbnormally()); + assertTrue(f.getException() instanceof CancellationException); } }}; testInvokeOnPool(mainPool(), a); @@ -530,18 +550,20 @@ public class ForkJoinTaskTest extends JS /** * timed get of a forked task throws exception when task cancelled */ - public void testCancelledForkTimedGet() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - AsyncFib f = new AsyncFib(8); - f.cancel(true); - f.fork(); - f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS); + public void testCancelledForkTimedGet() throws Exception { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() throws Exception { + AsyncFib f = new AsyncFib(8); + assertTrue(f.cancel(true)); + assertSame(f, f.fork()); + try { + f.get(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (CancellationException success) { - } catch (Exception ex) { - unexpectedException(ex); + assertTrue(f.isDone()); + assertTrue(f.isCancelled()); + assertTrue(f.isCompletedAbnormally()); + assertTrue(f.getException() instanceof CancellationException); } }}; testInvokeOnPool(mainPool(), a); @@ -551,15 +573,16 @@ public class ForkJoinTaskTest extends JS * quietlyJoin of a forked task returns when task cancelled */ public void testCancelledForkQuietlyJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { AsyncFib f = new AsyncFib(8); - f.cancel(true); - f.fork(); + assertTrue(f.cancel(true)); + assertSame(f, f.fork()); f.quietlyJoin(); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.isCompletedAbnormally()); - threadAssertTrue(f.getException() instanceof CancellationException); + assertTrue(f.isDone()); + assertTrue(f.isCompletedAbnormally()); + assertTrue(f.isCancelled()); + assertTrue(f.getException() instanceof CancellationException); }}; testInvokeOnPool(mainPool(), a); } @@ -569,9 +592,9 @@ public class ForkJoinTaskTest extends JS */ public void testGetPool() { final ForkJoinPool mainPool = mainPool(); - RecursiveAction a = new RecursiveAction() { - public void compute() { - threadAssertTrue(getPool() == mainPool); + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + assertSame(mainPool, getPool()); }}; testInvokeOnPool(mainPool, a); } @@ -580,20 +603,20 @@ public class ForkJoinTaskTest extends JS * getPool of non-FJ task returns null */ public void testGetPool2() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - threadAssertTrue(getPool() == null); + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + assertNull(getPool()); }}; - a.invoke(); + assertNull(a.invoke()); } /** * inForkJoinPool of executing task returns true */ public void testInForkJoinPool() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - threadAssertTrue(inForkJoinPool()); + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + assertTrue(inForkJoinPool()); }}; testInvokeOnPool(mainPool(), a); } @@ -602,37 +625,36 @@ public class ForkJoinTaskTest extends JS * inForkJoinPool of non-FJ task returns false */ public void testInForkJoinPool2() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - threadAssertTrue(!inForkJoinPool()); + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + assertTrue(!inForkJoinPool()); }}; - a.invoke(); + assertNull(a.invoke()); } /** * setRawResult(null) succeeds */ public void testSetRawResult() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { setRawResult(null); }}; - a.invoke(); + assertNull(a.invoke()); } /** * invoke task throws exception after invoking completeExceptionally */ public void testCompleteExceptionally() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + AsyncFib f = new AsyncFib(8); + f.completeExceptionally(new FJException()); try { - AsyncFib f = new AsyncFib(8); - f.completeExceptionally(new FJException()); f.invoke(); shouldThrow(); - } catch (FJException success) { - } + } catch (FJException success) {} }}; testInvokeOnPool(mainPool(), a); } @@ -641,15 +663,15 @@ public class ForkJoinTaskTest extends JS * invokeAll(t1, t2) invokes all task arguments */ public void testInvokeAll2() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { AsyncFib f = new AsyncFib(8); AsyncFib g = new AsyncFib(9); invokeAll(f, g); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.number == 21); - threadAssertTrue(g.isDone()); - threadAssertTrue(g.number == 34); + assertTrue(f.isDone()); + assertEquals(21, f.number); + assertTrue(g.isDone()); + assertEquals(34, g.number); }}; testInvokeOnPool(mainPool(), a); } @@ -658,12 +680,12 @@ public class ForkJoinTaskTest extends JS * invokeAll(tasks) with 1 argument invokes task */ public void testInvokeAll1() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { AsyncFib f = new AsyncFib(8); invokeAll(f); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.number == 21); + assertTrue(f.isDone()); + assertEquals(21, f.number); }}; testInvokeOnPool(mainPool(), a); } @@ -672,18 +694,18 @@ public class ForkJoinTaskTest extends JS * invokeAll(tasks) with > 2 argument invokes tasks */ public void testInvokeAll3() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { AsyncFib f = new AsyncFib(8); AsyncFib g = new AsyncFib(9); AsyncFib h = new AsyncFib(7); invokeAll(f, g, h); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.number == 21); - threadAssertTrue(g.isDone()); - threadAssertTrue(g.number == 34); - threadAssertTrue(h.isDone()); - threadAssertTrue(h.number == 13); + assertTrue(f.isDone()); + assertEquals(21, f.number); + assertTrue(g.isDone()); + assertEquals(34, g.number); + assertTrue(h.isDone()); + assertEquals(13, h.number); }}; testInvokeOnPool(mainPool(), a); } @@ -692,8 +714,8 @@ public class ForkJoinTaskTest extends JS * invokeAll(collection) invokes all tasks in the collection */ public void testInvokeAllCollection() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { AsyncFib f = new AsyncFib(8); AsyncFib g = new AsyncFib(9); AsyncFib h = new AsyncFib(7); @@ -702,12 +724,12 @@ public class ForkJoinTaskTest extends JS set.add(g); set.add(h); invokeAll(set); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.number == 21); - threadAssertTrue(g.isDone()); - threadAssertTrue(g.number == 34); - threadAssertTrue(h.isDone()); - threadAssertTrue(h.number == 13); + assertTrue(f.isDone()); + assertEquals(21, f.number); + assertTrue(g.isDone()); + assertEquals(34, g.number); + assertTrue(h.isDone()); + assertEquals(13, h.number); }}; testInvokeOnPool(mainPool(), a); } @@ -717,16 +739,15 @@ public class ForkJoinTaskTest extends JS * invokeAll(tasks) with any null task throws NPE */ public void testInvokeAllNPE() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + AsyncFib f = new AsyncFib(8); + AsyncFib g = new AsyncFib(9); + AsyncFib h = null; try { - AsyncFib f = new AsyncFib(8); - AsyncFib g = new AsyncFib(9); - AsyncFib h = null; invokeAll(f, g, h); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} }}; testInvokeOnPool(mainPool(), a); } @@ -735,15 +756,14 @@ public class ForkJoinTaskTest extends JS * invokeAll(t1, t2) throw exception if any task does */ public void testAbnormalInvokeAll2() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + AsyncFib f = new AsyncFib(8); + FailingAsyncFib g = new FailingAsyncFib(9); try { - AsyncFib f = new AsyncFib(8); - FailingAsyncFib g = new FailingAsyncFib(9); invokeAll(f, g); shouldThrow(); - } catch (FJException success) { - } + } catch (FJException success) {} }}; testInvokeOnPool(mainPool(), a); } @@ -752,14 +772,13 @@ public class ForkJoinTaskTest extends JS * invokeAll(tasks) with 1 argument throws exception if task does */ public void testAbnormalInvokeAll1() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + FailingAsyncFib g = new FailingAsyncFib(9); try { - FailingAsyncFib g = new FailingAsyncFib(9); invokeAll(g); shouldThrow(); - } catch (FJException success) { - } + } catch (FJException success) {} }}; testInvokeOnPool(mainPool(), a); } @@ -768,16 +787,15 @@ public class ForkJoinTaskTest extends JS * invokeAll(tasks) with > 2 argument throws exception if any task does */ public void testAbnormalInvokeAll3() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + AsyncFib f = new AsyncFib(8); + FailingAsyncFib g = new FailingAsyncFib(9); + AsyncFib h = new AsyncFib(7); try { - AsyncFib f = new AsyncFib(8); - FailingAsyncFib g = new FailingAsyncFib(9); - AsyncFib h = new AsyncFib(7); invokeAll(f, g, h); shouldThrow(); - } catch (FJException success) { - } + } catch (FJException success) {} }}; testInvokeOnPool(mainPool(), a); } @@ -786,20 +804,19 @@ public class ForkJoinTaskTest extends JS * invokeAll(collection) throws exception if any task does */ public void testAbnormalInvokeAllCollection() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public 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 { - FailingAsyncFib f = new FailingAsyncFib(8); - AsyncFib g = new AsyncFib(9); - AsyncFib h = new AsyncFib(7); - HashSet set = new HashSet(); - set.add(f); - set.add(g); - set.add(h); invokeAll(set); shouldThrow(); - } catch (FJException success) { - } + } catch (FJException success) {} }}; testInvokeOnPool(mainPool(), a); } @@ -809,16 +826,16 @@ public class ForkJoinTaskTest extends JS * and suppresses execution */ public void testTryUnfork() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { AsyncFib g = new AsyncFib(9); - g.fork(); + assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); - f.fork(); - threadAssertTrue(f.tryUnfork()); + assertSame(f, f.fork()); + assertTrue(f.tryUnfork()); helpQuiesce(); - threadAssertFalse(f.isDone()); - threadAssertTrue(g.isDone()); + assertFalse(f.isDone()); + assertTrue(g.isDone()); }}; testInvokeOnPool(singletonPool(), a); } @@ -828,15 +845,15 @@ public class ForkJoinTaskTest extends JS * there are more tasks than threads */ public void testGetSurplusQueuedTaskCount() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { AsyncFib h = new AsyncFib(7); - h.fork(); + assertSame(h, h.fork()); AsyncFib g = new AsyncFib(9); - g.fork(); + assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); - f.fork(); - threadAssertTrue(getSurplusQueuedTaskCount() > 0); + assertSame(f, f.fork()); + assertTrue(getSurplusQueuedTaskCount() > 0); helpQuiesce(); }}; testInvokeOnPool(singletonPool(), a); @@ -846,15 +863,15 @@ public class ForkJoinTaskTest extends JS * peekNextLocalTask returns most recent unexecuted task. */ public void testPeekNextLocalTask() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { AsyncFib g = new AsyncFib(9); - g.fork(); + assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); - f.fork(); - threadAssertTrue(peekNextLocalTask() == f); - f.join(); - threadAssertTrue(f.isDone()); + assertSame(f, f.fork()); + assertSame(f, peekNextLocalTask()); + assertNull(f.join()); + assertTrue(f.isDone()); helpQuiesce(); }}; testInvokeOnPool(singletonPool(), a); @@ -865,15 +882,15 @@ public class ForkJoinTaskTest extends JS * without executing it */ public void testPollNextLocalTask() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { AsyncFib g = new AsyncFib(9); - g.fork(); + assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); - f.fork(); - threadAssertTrue(pollNextLocalTask() == f); + assertSame(f, f.fork()); + assertSame(f, pollNextLocalTask()); helpQuiesce(); - threadAssertFalse(f.isDone()); + assertFalse(f.isDone()); }}; testInvokeOnPool(singletonPool(), a); } @@ -883,16 +900,16 @@ public class ForkJoinTaskTest extends JS * without executing it */ public void testPollTask() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { AsyncFib g = new AsyncFib(9); - g.fork(); + assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); - f.fork(); - threadAssertTrue(pollTask() == f); + assertSame(f, f.fork()); + assertSame(f, pollTask()); helpQuiesce(); - threadAssertFalse(f.isDone()); - threadAssertTrue(g.isDone()); + assertFalse(f.isDone()); + assertTrue(g.isDone()); }}; testInvokeOnPool(singletonPool(), a); } @@ -901,16 +918,16 @@ public class ForkJoinTaskTest extends JS * peekNextLocalTask returns least recent unexecuted task in async mode */ public void testPeekNextLocalTaskAsync() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { AsyncFib g = new AsyncFib(9); - g.fork(); + assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); - f.fork(); - threadAssertTrue(peekNextLocalTask() == g); - f.join(); + assertSame(f, f.fork()); + assertSame(g, peekNextLocalTask()); + assertNull(f.join()); helpQuiesce(); - threadAssertTrue(f.isDone()); + assertTrue(f.isDone()); }}; testInvokeOnPool(asyncSingletonPool(), a); } @@ -920,16 +937,16 @@ public class ForkJoinTaskTest extends JS * without executing it, in async mode */ public void testPollNextLocalTaskAsync() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { AsyncFib g = new AsyncFib(9); - g.fork(); + assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); - f.fork(); - threadAssertTrue(pollNextLocalTask() == g); + assertSame(f, f.fork()); + assertSame(g, pollNextLocalTask()); helpQuiesce(); - threadAssertTrue(f.isDone()); - threadAssertFalse(g.isDone()); + assertTrue(f.isDone()); + assertFalse(g.isDone()); }}; testInvokeOnPool(asyncSingletonPool(), a); } @@ -939,17 +956,541 @@ public class ForkJoinTaskTest extends JS * without executing it, in async mode */ public void testPollTaskAsync() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { AsyncFib g = new AsyncFib(9); - g.fork(); + assertSame(g, g.fork()); AsyncFib f = new AsyncFib(8); - f.fork(); - threadAssertTrue(pollTask() == g); + assertSame(f, f.fork()); + assertSame(g, pollTask()); helpQuiesce(); - threadAssertTrue(f.isDone()); - threadAssertFalse(g.isDone()); + assertTrue(f.isDone()); + assertFalse(g.isDone()); }}; testInvokeOnPool(asyncSingletonPool(), a); } + + // versions for singleton pools + + /** + * invoke returns when task completes normally. + * isCompletedAbnormally and isCancelled return false for normally + * completed tasks. getRawResult of a RecursiveAction returns null; + */ + public void testInvokeSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + AsyncFib f = new AsyncFib(8); + assertNull(f.invoke()); + assertEquals(21, f.number); + assertTrue(f.isDone()); + assertFalse(f.isCancelled()); + assertFalse(f.isCompletedAbnormally()); + assertNull(f.getRawResult()); + }}; + 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() { + public void realCompute() { + AsyncFib f = new AsyncFib(8); + f.quietlyInvoke(); + assertEquals(21, f.number); + assertTrue(f.isDone()); + assertFalse(f.isCancelled()); + assertFalse(f.isCompletedAbnormally()); + assertNull(f.getRawResult()); + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * join of a forked task returns when task completes + */ + public void testForkJoinSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + AsyncFib f = new AsyncFib(8); + assertSame(f, f.fork()); + assertNull(f.join()); + assertEquals(21, f.number); + assertTrue(f.isDone()); + assertNull(f.getRawResult()); + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * get of a forked task returns when task completes + */ + public void testForkGetSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() throws Exception { + AsyncFib f = new AsyncFib(8); + assertSame(f, f.fork()); + assertNull(f.get()); + assertEquals(21, f.number); + assertTrue(f.isDone()); + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * timed get of a forked task returns when task completes + */ + public void testForkTimedGetSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() throws Exception { + AsyncFib f = new AsyncFib(8); + assertSame(f, f.fork()); + assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); + assertEquals(21, f.number); + assertTrue(f.isDone()); + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * timed get with null time unit throws NPE + */ + public void testForkTimedGetNPESingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + public 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() { + public void realCompute() { + AsyncFib f = new AsyncFib(8); + assertSame(f, f.fork()); + f.quietlyJoin(); + assertEquals(21, f.number); + assertTrue(f.isDone()); + }}; + testInvokeOnPool(singletonPool(), a); + } + + + /** + * helpQuiesce returns when tasks are complete. + * getQueuedTaskCount returns 0 when quiescent + */ + public void testForkHelpQuiesceSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + AsyncFib f = new AsyncFib(8); + assertSame(f, f.fork()); + f.helpQuiesce(); + assertEquals(21, f.number); + assertTrue(f.isDone()); + assertEquals(0, getQueuedTaskCount()); + }}; + testInvokeOnPool(singletonPool(), a); + } + + + /** + * invoke task throws exception when task completes abnormally + */ + public void testAbnormalInvokeSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + FailingAsyncFib f = new FailingAsyncFib(8); + try { + f.invoke(); + shouldThrow(); + } catch (FJException success) {} + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * quietlyInvoke task returns when task completes abnormally + */ + public void testAbnormalQuietlyInvokeSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + FailingAsyncFib f = new FailingAsyncFib(8); + f.quietlyInvoke(); + assertTrue(f.isDone()); + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * join of a forked task throws exception when task completes abnormally + */ + public void testAbnormalForkJoinSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + FailingAsyncFib f = new FailingAsyncFib(8); + assertSame(f, f.fork()); + try { + f.join(); + shouldThrow(); + } catch (FJException success) {} + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * get of a forked task throws exception when task completes abnormally + */ + public void testAbnormalForkGetSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + public 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); + assertTrue(f.isDone()); + assertTrue(f.isCompletedAbnormally()); + assertSame(cause, f.getException()); + } + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * timed get of a forked task throws exception when task completes abnormally + */ + public void testAbnormalForkTimedGetSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + public 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); + assertTrue(f.isDone()); + assertTrue(f.isCompletedAbnormally()); + assertSame(cause, f.getException()); + } + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * quietlyJoin of a forked task returns when task completes abnormally + */ + public void testAbnormalForkQuietlyJoinSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + FailingAsyncFib f = new FailingAsyncFib(8); + assertSame(f, f.fork()); + f.quietlyJoin(); + assertTrue(f.isDone()); + assertTrue(f.isCompletedAbnormally()); + assertTrue(f.getException() instanceof FJException); + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * invoke task throws exception when task cancelled + */ + public void testCancelledInvokeSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + AsyncFib f = new AsyncFib(8); + assertTrue(f.cancel(true)); + try { + f.invoke(); + shouldThrow(); + } catch (CancellationException success) { + assertTrue(f.isDone()); + assertTrue(f.isCancelled()); + assertTrue(f.isCompletedAbnormally()); + assertTrue(f.getException() instanceof CancellationException); + } + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * join of a forked task throws exception when task cancelled + */ + public void testCancelledForkJoinSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + AsyncFib f = new AsyncFib(8); + assertTrue(f.cancel(true)); + assertSame(f, f.fork()); + try { + f.join(); + shouldThrow(); + } catch (CancellationException success) { + assertTrue(f.isDone()); + assertTrue(f.isCancelled()); + assertTrue(f.isCompletedAbnormally()); + assertTrue(f.getException() instanceof CancellationException); + } + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * get of a forked task throws exception when task cancelled + */ + public void testCancelledForkGetSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() throws Exception { + AsyncFib f = new AsyncFib(8); + assertTrue(f.cancel(true)); + assertSame(f, f.fork()); + try { + f.get(); + shouldThrow(); + } catch (CancellationException success) { + assertTrue(f.isDone()); + assertTrue(f.isCancelled()); + assertTrue(f.isCompletedAbnormally()); + assertTrue(f.getException() instanceof CancellationException); + } + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * timed get of a forked task throws exception when task cancelled + */ + public void testCancelledForkTimedGetSingleton() throws Exception { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() throws Exception { + AsyncFib f = new AsyncFib(8); + assertTrue(f.cancel(true)); + assertSame(f, f.fork()); + try { + f.get(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (CancellationException success) { + assertTrue(f.isDone()); + assertTrue(f.isCancelled()); + assertTrue(f.isCompletedAbnormally()); + assertTrue(f.getException() instanceof CancellationException); + } + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * quietlyJoin of a forked task returns when task cancelled + */ + public void testCancelledForkQuietlyJoinSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + AsyncFib f = new AsyncFib(8); + assertTrue(f.cancel(true)); + assertSame(f, f.fork()); + f.quietlyJoin(); + assertTrue(f.isDone()); + assertTrue(f.isCompletedAbnormally()); + assertTrue(f.isCancelled()); + assertTrue(f.getException() instanceof CancellationException); + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * invoke task throws exception after invoking completeExceptionally + */ + public void testCompleteExceptionallySingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + AsyncFib f = new AsyncFib(8); + f.completeExceptionally(new FJException()); + try { + f.invoke(); + shouldThrow(); + } catch (FJException success) {} + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * invokeAll(t1, t2) invokes all task arguments + */ + public void testInvokeAll2Singleton() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + AsyncFib f = new AsyncFib(8); + AsyncFib g = new AsyncFib(9); + invokeAll(f, g); + assertTrue(f.isDone()); + assertEquals(21, f.number); + assertTrue(g.isDone()); + assertEquals(34, g.number); + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * invokeAll(tasks) with 1 argument invokes task + */ + public void testInvokeAll1Singleton() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + AsyncFib f = new AsyncFib(8); + invokeAll(f); + assertTrue(f.isDone()); + assertEquals(21, f.number); + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * invokeAll(tasks) with > 2 argument invokes tasks + */ + public void testInvokeAll3Singleton() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + AsyncFib f = new AsyncFib(8); + AsyncFib g = new AsyncFib(9); + AsyncFib h = new AsyncFib(7); + invokeAll(f, g, h); + assertTrue(f.isDone()); + assertEquals(21, f.number); + assertTrue(g.isDone()); + assertEquals(34, g.number); + assertTrue(h.isDone()); + assertEquals(13, h.number); + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * invokeAll(collection) invokes all tasks in the collection + */ + public void testInvokeAllCollectionSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + public 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); + assertTrue(f.isDone()); + assertEquals(21, f.number); + assertTrue(g.isDone()); + assertEquals(34, g.number); + assertTrue(h.isDone()); + assertEquals(13, h.number); + }}; + testInvokeOnPool(singletonPool(), a); + } + + + /** + * invokeAll(tasks) with any null task throws NPE + */ + public void testInvokeAllNPESingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + public 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() { + public void realCompute() { + AsyncFib f = new AsyncFib(8); + FailingAsyncFib g = new FailingAsyncFib(9); + try { + invokeAll(f, g); + shouldThrow(); + } catch (FJException success) {} + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * invokeAll(tasks) with 1 argument throws exception if task does + */ + public void testAbnormalInvokeAll1Singleton() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + FailingAsyncFib g = new FailingAsyncFib(9); + try { + invokeAll(g); + shouldThrow(); + } catch (FJException success) {} + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * invokeAll(tasks) with > 2 argument throws exception if any task does + */ + public void testAbnormalInvokeAll3Singleton() { + RecursiveAction a = new CheckedRecursiveAction() { + public 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) {} + }}; + testInvokeOnPool(singletonPool(), a); + } + + /** + * invokeAll(collection) throws exception if any task does + */ + public void testAbnormalInvokeAllCollectionSingleton() { + RecursiveAction a = new CheckedRecursiveAction() { + public 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) {} + }}; + testInvokeOnPool(singletonPool(), a); + } + }