--- jsr166/src/test/tck/ForkJoinTaskTest.java 2009/08/04 09:35:31 1.4 +++ jsr166/src/test/tck/ForkJoinTaskTest.java 2010/09/13 23:23:44 1.16 @@ -3,21 +3,50 @@ * Expert Group and released to the public domain, as explained at * http://creativecommons.org/licenses/publicdomain */ +import java.util.concurrent.ExecutionException; +import java.util.concurrent.CancellationException; +import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.ForkJoinTask; +import java.util.concurrent.ForkJoinWorkerThread; +import java.util.concurrent.RecursiveAction; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; +import java.util.HashSet; import junit.framework.*; -import java.util.concurrent.*; -import java.util.concurrent.atomic.*; -import java.util.*; public class ForkJoinTaskTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run(suite()); } + public static Test suite() { - return new TestSuite(ForkJoinTaskTest.class); + return new TestSuite(ForkJoinTaskTest.class); } - /** + private static ForkJoinPool mainPool() { + return new ForkJoinPool(); + } + + private static ForkJoinPool singletonPool() { + return new ForkJoinPool(1); + } + + private static ForkJoinPool asyncSingletonPool() { + return new ForkJoinPool(1, + ForkJoinPool.defaultForkJoinWorkerThreadFactory, + null, true); + } + + private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { + try { + assertTrue(pool.invoke(a) == null); + } finally { + joinPool(pool); + } + } + + /* * Testing coverage notes: * * To test extension methods and overrides, most tests use @@ -25,13 +54,6 @@ public class ForkJoinTaskTest extends JS * differently than supplied Recursive forms. */ - static final ForkJoinPool mainPool = new ForkJoinPool(); - static final ForkJoinPool singletonPool = new ForkJoinPool(1); - static final ForkJoinPool asyncSingletonPool = new ForkJoinPool(1); - static { - asyncSingletonPool.setAsyncMode(true); - } - static final class FJException extends RuntimeException { FJException() { super(); } } @@ -148,7 +170,7 @@ public class ForkJoinTaskTest extends JS } - static final class AsyncFib extends BinaryAsyncAction { + static final class AsyncFib extends BinaryAsyncAction { int number; public AsyncFib(int n) { this.number = n; @@ -177,7 +199,7 @@ public class ForkJoinTaskTest extends JS } - static final class FailingAsyncFib extends BinaryAsyncAction { + static final class FailingAsyncFib extends BinaryAsyncAction { int number; public FailingAsyncFib(int n) { this.number = n; @@ -209,21 +231,19 @@ public class ForkJoinTaskTest extends JS * invoke returns when task completes normally. * isCompletedAbnormally and isCancelled return false for normally * completed tasks. getRawResult of a RecursiveAction returns null; - * */ public void testInvoke() { RecursiveAction a = new RecursiveAction() { - public void compute() { - AsyncFib f = new AsyncFib(8); - f.invoke(); - threadAssertTrue(f.number == 21); - threadAssertTrue(f.isDone()); - threadAssertFalse(f.isCancelled()); - threadAssertFalse(f.isCompletedAbnormally()); - threadAssertTrue(f.getRawResult() == null); - } - }; - mainPool.invoke(a); + public void compute() { + AsyncFib f = new AsyncFib(8); + threadAssertNull(f.invoke()); + threadAssertTrue(f.number == 21); + threadAssertTrue(f.isDone()); + threadAssertFalse(f.isCancelled()); + threadAssertFalse(f.isCompletedAbnormally()); + threadAssertTrue(f.getRawResult() == null); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -233,17 +253,16 @@ public class ForkJoinTaskTest extends JS */ public void testQuietlyInvoke() { RecursiveAction a = new RecursiveAction() { - public void compute() { - AsyncFib f = new AsyncFib(8); - f.quietlyInvoke(); - threadAssertTrue(f.number == 21); - threadAssertTrue(f.isDone()); - threadAssertFalse(f.isCancelled()); - threadAssertFalse(f.isCompletedAbnormally()); - threadAssertTrue(f.getRawResult() == null); - } - }; - mainPool.invoke(a); + public void compute() { + AsyncFib f = new AsyncFib(8); + f.quietlyInvoke(); + threadAssertTrue(f.number == 21); + threadAssertTrue(f.isDone()); + threadAssertFalse(f.isCancelled()); + threadAssertFalse(f.isCompletedAbnormally()); + threadAssertTrue(f.getRawResult() == null); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -251,16 +270,15 @@ public class ForkJoinTaskTest extends JS */ public void testForkJoin() { RecursiveAction a = new RecursiveAction() { - public void compute() { - AsyncFib f = new AsyncFib(8); - f.fork(); - f.join(); - threadAssertTrue(f.number == 21); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.getRawResult() == null); - } - }; - mainPool.invoke(a); + public void compute() { + AsyncFib f = new AsyncFib(8); + threadAssertSame(f, f.fork()); + threadAssertNull(f.join()); + threadAssertTrue(f.number == 21); + threadAssertTrue(f.isDone()); + threadAssertTrue(f.getRawResult() == null); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -268,19 +286,18 @@ public class ForkJoinTaskTest extends JS */ 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); - } + public void compute() { + try { + AsyncFib f = new AsyncFib(8); + threadAssertSame(f, f.fork()); + threadAssertNull(f.get()); + threadAssertTrue(f.number == 21); + threadAssertTrue(f.isDone()); + } catch (Exception ex) { + unexpectedException(ex); } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -288,19 +305,18 @@ public class ForkJoinTaskTest extends JS */ public void testForkTimedGet() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - AsyncFib f = new AsyncFib(8); - f.fork(); - f.get(5L, TimeUnit.SECONDS); - threadAssertTrue(f.number == 21); - threadAssertTrue(f.isDone()); - } catch (Exception ex) { - unexpectedException(ex); - } + public void compute() { + try { + AsyncFib f = new AsyncFib(8); + threadAssertSame(f, f.fork()); + threadAssertNull(f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); + threadAssertTrue(f.number == 21); + threadAssertTrue(f.isDone()); + } catch (Exception ex) { + unexpectedException(ex); } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -308,35 +324,18 @@ public class ForkJoinTaskTest extends JS */ public void testForkTimedGetNPE() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - AsyncFib f = new AsyncFib(8); - f.fork(); - f.get(5L, null); - shouldThrow(); - } catch (NullPointerException success) { - } catch (Exception ex) { - unexpectedException(ex); - } - } - }; - mainPool.invoke(a); - } - - /** - * helpJoin of a forked task returns when task completes - */ - public void testForkHelpJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + public void compute() { + try { AsyncFib f = new AsyncFib(8); - f.fork(); - f.helpJoin(); - threadAssertTrue(f.number == 21); - threadAssertTrue(f.isDone()); + threadAssertSame(f, f.fork()); + f.get(5L, null); + shouldThrow(); + } catch (NullPointerException success) { + } catch (Exception ex) { + unexpectedException(ex); } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -344,32 +343,14 @@ public class ForkJoinTaskTest extends JS */ public void testForkQuietlyJoin() { RecursiveAction a = new RecursiveAction() { - public void compute() { - AsyncFib f = new AsyncFib(8); - f.fork(); - f.quietlyJoin(); - threadAssertTrue(f.number == 21); - threadAssertTrue(f.isDone()); - } - }; - mainPool.invoke(a); - } - - - /** - * quietlyHelpJoin of a forked task returns when task completes - */ - public void testForkQuietlyHelpJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - AsyncFib f = new AsyncFib(8); - f.fork(); - f.quietlyHelpJoin(); - threadAssertTrue(f.number == 21); - threadAssertTrue(f.isDone()); - } - }; - mainPool.invoke(a); + public void compute() { + AsyncFib f = new AsyncFib(8); + threadAssertSame(f, f.fork()); + f.quietlyJoin(); + threadAssertTrue(f.number == 21); + threadAssertTrue(f.isDone()); + }}; + testInvokeOnPool(mainPool(), a); } @@ -379,16 +360,15 @@ public class ForkJoinTaskTest extends JS */ public void testForkHelpQuiesce() { RecursiveAction a = new RecursiveAction() { - public void compute() { - AsyncFib f = new AsyncFib(8); - f.fork(); - f.helpQuiesce(); - threadAssertTrue(f.number == 21); - threadAssertTrue(f.isDone()); - threadAssertTrue(getQueuedTaskCount() == 0); - } - }; - mainPool.invoke(a); + public void compute() { + AsyncFib f = new AsyncFib(8); + threadAssertSame(f, f.fork()); + f.helpQuiesce(); + threadAssertTrue(f.number == 21); + threadAssertTrue(f.isDone()); + threadAssertTrue(getQueuedTaskCount() == 0); + }}; + testInvokeOnPool(mainPool(), a); } @@ -397,16 +377,15 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalInvoke() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - FailingAsyncFib f = new FailingAsyncFib(8); - f.invoke(); - shouldThrow(); - } catch (FJException success) { - } + public void compute() { + try { + FailingAsyncFib f = new FailingAsyncFib(8); + f.invoke(); + shouldThrow(); + } catch (FJException success) { } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -414,13 +393,12 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalQuietlyInvoke() { RecursiveAction a = new RecursiveAction() { - public void compute() { - FailingAsyncFib f = new FailingAsyncFib(8); - f.quietlyInvoke(); - threadAssertTrue(f.isDone()); - } - }; - mainPool.invoke(a); + public void compute() { + FailingAsyncFib f = new FailingAsyncFib(8); + f.quietlyInvoke(); + threadAssertTrue(f.isDone()); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -428,17 +406,16 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalForkJoin() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - FailingAsyncFib f = new FailingAsyncFib(8); - f.fork(); - f.join(); - shouldThrow(); - } catch (FJException success) { - } + public void compute() { + try { + FailingAsyncFib f = new FailingAsyncFib(8); + threadAssertSame(f, f.fork()); + f.join(); + shouldThrow(); + } catch (FJException success) { } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -446,19 +423,18 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalForkGet() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - FailingAsyncFib f = new FailingAsyncFib(8); - f.fork(); - f.get(); - shouldThrow(); - } catch (ExecutionException success) { - } catch (Exception ex) { - unexpectedException(ex); - } + public void compute() { + try { + FailingAsyncFib f = new FailingAsyncFib(8); + threadAssertSame(f, f.fork()); + f.get(); + shouldThrow(); + } catch (ExecutionException success) { + } catch (Exception ex) { + unexpectedException(ex); } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -466,58 +442,18 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalForkTimedGet() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - FailingAsyncFib f = new FailingAsyncFib(8); - f.fork(); - f.get(5L, TimeUnit.SECONDS); - shouldThrow(); - } catch (ExecutionException success) { - } catch (Exception ex) { - unexpectedException(ex); - } - } - }; - mainPool.invoke(a); - } - - /** - * join of a forked task throws exception when task completes abnormally - */ - public void testAbnormalForkHelpJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - FailingAsyncFib f = new FailingAsyncFib(8); - f.fork(); - f.helpJoin(); - shouldThrow(); - } catch (FJException success) { - } - } - }; - mainPool.invoke(a); - } - - /** - * quietlyHelpJoin of a forked task returns when task completes abnormally. - * getException of failed task returns its exception. - * isCompletedAbnormally of a failed task returns true. - * isCancelled of a failed uncancelled task returns false - */ - public void testAbnormalForkQuietlyHelpJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + public void compute() { + try { FailingAsyncFib f = new FailingAsyncFib(8); - f.fork(); - f.quietlyHelpJoin(); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.isCompletedAbnormally()); - threadAssertFalse(f.isCancelled()); - threadAssertTrue(f.getException() instanceof FJException); + threadAssertSame(f, f.fork()); + f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS); + shouldThrow(); + } catch (ExecutionException success) { + } catch (Exception ex) { + unexpectedException(ex); } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -525,16 +461,15 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalForkQuietlyJoin() { RecursiveAction a = new RecursiveAction() { - public void compute() { - FailingAsyncFib f = new FailingAsyncFib(8); - f.fork(); - f.quietlyJoin(); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.isCompletedAbnormally()); - threadAssertTrue(f.getException() instanceof FJException); - } - }; - mainPool.invoke(a); + public void compute() { + FailingAsyncFib f = new FailingAsyncFib(8); + threadAssertSame(f, f.fork()); + f.quietlyJoin(); + threadAssertTrue(f.isDone()); + threadAssertTrue(f.isCompletedAbnormally()); + threadAssertTrue(f.getException() instanceof FJException); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -542,17 +477,16 @@ public class ForkJoinTaskTest extends JS */ public void testCancelledInvoke() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - AsyncFib f = new AsyncFib(8); - f.cancel(true); - f.invoke(); - shouldThrow(); - } catch (CancellationException success) { - } + public void compute() { + try { + AsyncFib f = new AsyncFib(8); + threadAssertTrue(f.cancel(true)); + f.invoke(); + shouldThrow(); + } catch (CancellationException success) { } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -560,18 +494,17 @@ public class ForkJoinTaskTest extends JS */ public void testCancelledForkJoin() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - AsyncFib f = new AsyncFib(8); - f.cancel(true); - f.fork(); - f.join(); - shouldThrow(); - } catch (CancellationException success) { - } + public void compute() { + try { + AsyncFib f = new AsyncFib(8); + threadAssertTrue(f.cancel(true)); + threadAssertSame(f, f.fork()); + f.join(); + shouldThrow(); + } catch (CancellationException success) { } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -579,20 +512,19 @@ public class ForkJoinTaskTest extends JS */ public void testCancelledForkGet() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - AsyncFib f = new AsyncFib(8); - f.cancel(true); - f.fork(); - f.get(); - shouldThrow(); - } catch (CancellationException success) { - } catch (Exception ex) { - unexpectedException(ex); - } + public void compute() { + try { + AsyncFib f = new AsyncFib(8); + threadAssertTrue(f.cancel(true)); + threadAssertSame(f, f.fork()); + f.get(); + shouldThrow(); + } catch (CancellationException success) { + } catch (Exception ex) { + unexpectedException(ex); } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -600,61 +532,19 @@ public class ForkJoinTaskTest extends JS */ public void testCancelledForkTimedGet() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - AsyncFib f = new AsyncFib(8); - f.cancel(true); - f.fork(); - f.get(5L, TimeUnit.SECONDS); - shouldThrow(); - } catch (CancellationException success) { - } catch (Exception ex) { - unexpectedException(ex); - } - } - }; - mainPool.invoke(a); - } - - /** - * join of a forked task throws exception when task cancelled - */ - public void testCancelledForkHelpJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - AsyncFib f = new AsyncFib(8); - f.cancel(true); - f.fork(); - f.helpJoin(); - shouldThrow(); - } catch (CancellationException success) { - } - } - }; - mainPool.invoke(a); - } - - /** - * quietlyHelpJoin of a forked task returns when task cancelled. - * getException of cancelled task returns its exception. - * isCompletedAbnormally of a cancelled task returns true. - * isCancelled of a cancelled task returns true - */ - public void testCancelledForkQuietlyHelpJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + public void compute() { + try { AsyncFib f = new AsyncFib(8); - f.cancel(true); - f.fork(); - f.quietlyHelpJoin(); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.isCompletedAbnormally()); - threadAssertTrue(f.isCancelled()); - threadAssertTrue(f.getException() instanceof CancellationException); + threadAssertTrue(f.cancel(true)); + threadAssertSame(f, f.fork()); + f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS); + shouldThrow(); + } catch (CancellationException success) { + } catch (Exception ex) { + unexpectedException(ex); } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -662,29 +552,28 @@ public class ForkJoinTaskTest extends JS */ public void testCancelledForkQuietlyJoin() { RecursiveAction a = new RecursiveAction() { - public void compute() { - AsyncFib f = new AsyncFib(8); - f.cancel(true); - f.fork(); - f.quietlyJoin(); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.isCompletedAbnormally()); - threadAssertTrue(f.getException() instanceof CancellationException); - } - }; - mainPool.invoke(a); + public void compute() { + AsyncFib f = new AsyncFib(8); + threadAssertTrue(f.cancel(true)); + threadAssertSame(f, f.fork()); + f.quietlyJoin(); + threadAssertTrue(f.isDone()); + threadAssertTrue(f.isCompletedAbnormally()); + threadAssertTrue(f.getException() instanceof CancellationException); + }}; + testInvokeOnPool(mainPool(), a); } /** * getPool of executing task returns its pool */ public void testGetPool() { + final ForkJoinPool mainPool = mainPool(); RecursiveAction a = new RecursiveAction() { - public void compute() { - threadAssertTrue(getPool() == mainPool); - } - }; - mainPool.invoke(a); + public void compute() { + threadAssertTrue(getPool() == mainPool); + }}; + testInvokeOnPool(mainPool, a); } /** @@ -692,11 +581,10 @@ public class ForkJoinTaskTest extends JS */ public void testGetPool2() { RecursiveAction a = new RecursiveAction() { - public void compute() { - threadAssertTrue(getPool() == null); - } - }; - a.invoke(); + public void compute() { + threadAssertTrue(getPool() == null); + }}; + assertNull(a.invoke()); } /** @@ -704,11 +592,10 @@ public class ForkJoinTaskTest extends JS */ public void testInForkJoinPool() { RecursiveAction a = new RecursiveAction() { - public void compute() { - threadAssertTrue(inForkJoinPool()); - } - }; - mainPool.invoke(a); + public void compute() { + threadAssertTrue(inForkJoinPool()); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -716,11 +603,10 @@ public class ForkJoinTaskTest extends JS */ public void testInForkJoinPool2() { RecursiveAction a = new RecursiveAction() { - public void compute() { - threadAssertTrue(!inForkJoinPool()); - } - }; - a.invoke(); + public void compute() { + threadAssertTrue(!inForkJoinPool()); + }}; + assertNull(a.invoke()); } /** @@ -728,11 +614,10 @@ public class ForkJoinTaskTest extends JS */ public void testSetRawResult() { RecursiveAction a = new RecursiveAction() { - public void compute() { - setRawResult(null); - } - }; - a.invoke(); + public void compute() { + setRawResult(null); + }}; + assertNull(a.invoke()); } /** @@ -740,17 +625,16 @@ public class ForkJoinTaskTest extends JS */ public void testCompleteExceptionally() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - AsyncFib f = new AsyncFib(8); - f.completeExceptionally(new FJException()); - f.invoke(); - shouldThrow(); - } catch (FJException success) { - } + public void compute() { + try { + AsyncFib f = new AsyncFib(8); + f.completeExceptionally(new FJException()); + f.invoke(); + shouldThrow(); + } catch (FJException success) { } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -758,17 +642,16 @@ public class ForkJoinTaskTest extends JS */ public void testInvokeAll2() { RecursiveAction a = new RecursiveAction() { - public void compute() { - 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); - } - }; - mainPool.invoke(a); + public void compute() { + 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); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -776,14 +659,13 @@ public class ForkJoinTaskTest extends JS */ public void testInvokeAll1() { RecursiveAction a = new RecursiveAction() { - public void compute() { - AsyncFib f = new AsyncFib(8); - invokeAll(f); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.number == 21); - } - }; - mainPool.invoke(a); + public void compute() { + AsyncFib f = new AsyncFib(8); + invokeAll(f); + threadAssertTrue(f.isDone()); + threadAssertTrue(f.number == 21); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -791,20 +673,19 @@ public class ForkJoinTaskTest extends JS */ public void testInvokeAll3() { RecursiveAction a = new RecursiveAction() { - public void compute() { - 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); - } - }; - mainPool.invoke(a); + public void compute() { + 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); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -812,24 +693,23 @@ public class ForkJoinTaskTest extends JS */ public void testInvokeAllCollection() { RecursiveAction a = new RecursiveAction() { - public void compute() { - 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); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.number == 21); - threadAssertTrue(g.isDone()); - threadAssertTrue(g.number == 34); - threadAssertTrue(h.isDone()); - threadAssertTrue(h.number == 13); - } - }; - mainPool.invoke(a); + public void compute() { + 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); + threadAssertTrue(f.isDone()); + threadAssertTrue(f.number == 21); + threadAssertTrue(g.isDone()); + threadAssertTrue(g.number == 34); + threadAssertTrue(h.isDone()); + threadAssertTrue(h.number == 13); + }}; + testInvokeOnPool(mainPool(), a); } @@ -838,18 +718,17 @@ public class ForkJoinTaskTest extends JS */ public void testInvokeAllNPE() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - AsyncFib f = new AsyncFib(8); - AsyncFib g = new AsyncFib(9); - AsyncFib h = null; - invokeAll(f, g, h); - shouldThrow(); - } catch (NullPointerException success) { - } + public void compute() { + try { + AsyncFib f = new AsyncFib(8); + AsyncFib g = new AsyncFib(9); + AsyncFib h = null; + invokeAll(f, g, h); + shouldThrow(); + } catch (NullPointerException success) { } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -857,17 +736,16 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalInvokeAll2() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - AsyncFib f = new AsyncFib(8); - FailingAsyncFib g = new FailingAsyncFib(9); - invokeAll(f, g); - shouldThrow(); - } catch (FJException success) { - } + public void compute() { + try { + AsyncFib f = new AsyncFib(8); + FailingAsyncFib g = new FailingAsyncFib(9); + invokeAll(f, g); + shouldThrow(); + } catch (FJException success) { } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -875,16 +753,15 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalInvokeAll1() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - FailingAsyncFib g = new FailingAsyncFib(9); - invokeAll(g); - shouldThrow(); - } catch (FJException success) { - } + public void compute() { + try { + FailingAsyncFib g = new FailingAsyncFib(9); + invokeAll(g); + shouldThrow(); + } catch (FJException success) { } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -892,18 +769,17 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalInvokeAll3() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - AsyncFib f = new AsyncFib(8); - FailingAsyncFib g = new FailingAsyncFib(9); - AsyncFib h = new AsyncFib(7); - invokeAll(f, g, h); - shouldThrow(); - } catch (FJException success) { - } + public void compute() { + try { + AsyncFib f = new AsyncFib(8); + FailingAsyncFib g = new FailingAsyncFib(9); + AsyncFib h = new AsyncFib(7); + invokeAll(f, g, h); + shouldThrow(); + } catch (FJException success) { } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -911,22 +787,21 @@ public class ForkJoinTaskTest extends JS */ public void testAbnormalInvokeAllCollection() { RecursiveAction a = new RecursiveAction() { - public void compute() { - 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) { - } + public void compute() { + 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) { } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -935,18 +810,17 @@ public class ForkJoinTaskTest extends JS */ public void testTryUnfork() { RecursiveAction a = new RecursiveAction() { - public void compute() { - AsyncFib g = new AsyncFib(9); - g.fork(); - AsyncFib f = new AsyncFib(8); - f.fork(); - threadAssertTrue(f.tryUnfork()); - helpQuiesce(); - threadAssertFalse(f.isDone()); - threadAssertTrue(g.isDone()); - } - }; - singletonPool.invoke(a); + public void compute() { + AsyncFib g = new AsyncFib(9); + threadAssertSame(g, g.fork()); + AsyncFib f = new AsyncFib(8); + threadAssertSame(f, f.fork()); + threadAssertTrue(f.tryUnfork()); + helpQuiesce(); + threadAssertFalse(f.isDone()); + threadAssertTrue(g.isDone()); + }}; + testInvokeOnPool(singletonPool(), a); } /** @@ -955,18 +829,17 @@ public class ForkJoinTaskTest extends JS */ public void testGetSurplusQueuedTaskCount() { RecursiveAction a = new RecursiveAction() { - public void compute() { - AsyncFib h = new AsyncFib(7); - h.fork(); - AsyncFib g = new AsyncFib(9); - g.fork(); - AsyncFib f = new AsyncFib(8); - f.fork(); - threadAssertTrue(getSurplusQueuedTaskCount() > 0); - helpQuiesce(); - } - }; - singletonPool.invoke(a); + public void compute() { + AsyncFib h = new AsyncFib(7); + threadAssertSame(h, h.fork()); + AsyncFib g = new AsyncFib(9); + threadAssertSame(g, g.fork()); + AsyncFib f = new AsyncFib(8); + threadAssertSame(f, f.fork()); + threadAssertTrue(getSurplusQueuedTaskCount() > 0); + helpQuiesce(); + }}; + testInvokeOnPool(singletonPool(), a); } /** @@ -974,18 +847,17 @@ public class ForkJoinTaskTest extends JS */ public void testPeekNextLocalTask() { RecursiveAction a = new RecursiveAction() { - public void compute() { - AsyncFib g = new AsyncFib(9); - g.fork(); - AsyncFib f = new AsyncFib(8); - f.fork(); - threadAssertTrue(peekNextLocalTask() == f); - f.join(); - threadAssertTrue(f.isDone()); - helpQuiesce(); - } - }; - singletonPool.invoke(a); + public void compute() { + AsyncFib g = new AsyncFib(9); + threadAssertSame(g, g.fork()); + AsyncFib f = new AsyncFib(8); + threadAssertSame(f, f.fork()); + threadAssertTrue(peekNextLocalTask() == f); + threadAssertNull(f.join()); + threadAssertTrue(f.isDone()); + helpQuiesce(); + }}; + testInvokeOnPool(singletonPool(), a); } /** @@ -994,17 +866,16 @@ public class ForkJoinTaskTest extends JS */ public void testPollNextLocalTask() { RecursiveAction a = new RecursiveAction() { - public void compute() { - AsyncFib g = new AsyncFib(9); - g.fork(); - AsyncFib f = new AsyncFib(8); - f.fork(); - threadAssertTrue(pollNextLocalTask() == f); - helpQuiesce(); - threadAssertFalse(f.isDone()); - } - }; - singletonPool.invoke(a); + public void compute() { + AsyncFib g = new AsyncFib(9); + threadAssertSame(g, g.fork()); + AsyncFib f = new AsyncFib(8); + threadAssertSame(f, f.fork()); + threadAssertTrue(pollNextLocalTask() == f); + helpQuiesce(); + threadAssertFalse(f.isDone()); + }}; + testInvokeOnPool(singletonPool(), a); } /** @@ -1013,18 +884,17 @@ public class ForkJoinTaskTest extends JS */ public void testPollTask() { RecursiveAction a = new RecursiveAction() { - public void compute() { - AsyncFib g = new AsyncFib(9); - g.fork(); - AsyncFib f = new AsyncFib(8); - f.fork(); - threadAssertTrue(pollTask() == f); - helpQuiesce(); - threadAssertFalse(f.isDone()); - threadAssertTrue(g.isDone()); - } - }; - singletonPool.invoke(a); + public void compute() { + AsyncFib g = new AsyncFib(9); + threadAssertSame(g, g.fork()); + AsyncFib f = new AsyncFib(8); + threadAssertSame(f, f.fork()); + threadAssertTrue(pollTask() == f); + helpQuiesce(); + threadAssertFalse(f.isDone()); + threadAssertTrue(g.isDone()); + }}; + testInvokeOnPool(singletonPool(), a); } /** @@ -1032,18 +902,17 @@ public class ForkJoinTaskTest extends JS */ public void testPeekNextLocalTaskAsync() { RecursiveAction a = new RecursiveAction() { - public void compute() { - AsyncFib g = new AsyncFib(9); - g.fork(); - AsyncFib f = new AsyncFib(8); - f.fork(); - threadAssertTrue(peekNextLocalTask() == g); - f.join(); - helpQuiesce(); - threadAssertTrue(f.isDone()); - } - }; - asyncSingletonPool.invoke(a); + public void compute() { + AsyncFib g = new AsyncFib(9); + threadAssertSame(g, g.fork()); + AsyncFib f = new AsyncFib(8); + threadAssertSame(f, f.fork()); + threadAssertTrue(peekNextLocalTask() == g); + threadAssertNull(f.join()); + helpQuiesce(); + threadAssertTrue(f.isDone()); + }}; + testInvokeOnPool(asyncSingletonPool(), a); } /** @@ -1052,18 +921,17 @@ public class ForkJoinTaskTest extends JS */ public void testPollNextLocalTaskAsync() { RecursiveAction a = new RecursiveAction() { - public void compute() { - AsyncFib g = new AsyncFib(9); - g.fork(); - AsyncFib f = new AsyncFib(8); - f.fork(); - threadAssertTrue(pollNextLocalTask() == g); - helpQuiesce(); - threadAssertTrue(f.isDone()); - threadAssertFalse(g.isDone()); - } - }; - asyncSingletonPool.invoke(a); + public void compute() { + AsyncFib g = new AsyncFib(9); + threadAssertSame(g, g.fork()); + AsyncFib f = new AsyncFib(8); + threadAssertSame(f, f.fork()); + threadAssertTrue(pollNextLocalTask() == g); + helpQuiesce(); + threadAssertTrue(f.isDone()); + threadAssertFalse(g.isDone()); + }}; + testInvokeOnPool(asyncSingletonPool(), a); } /** @@ -1072,17 +940,16 @@ public class ForkJoinTaskTest extends JS */ public void testPollTaskAsync() { RecursiveAction a = new RecursiveAction() { - public void compute() { - AsyncFib g = new AsyncFib(9); - g.fork(); - AsyncFib f = new AsyncFib(8); - f.fork(); - threadAssertTrue(pollTask() == g); - helpQuiesce(); - threadAssertTrue(f.isDone()); - threadAssertFalse(g.isDone()); - } - }; - asyncSingletonPool.invoke(a); + public void compute() { + AsyncFib g = new AsyncFib(9); + threadAssertSame(g, g.fork()); + AsyncFib f = new AsyncFib(8); + threadAssertSame(f, f.fork()); + threadAssertTrue(pollTask() == g); + helpQuiesce(); + threadAssertTrue(f.isDone()); + threadAssertFalse(g.isDone()); + }}; + testInvokeOnPool(asyncSingletonPool(), a); } }