--- jsr166/src/test/tck/RecursiveActionTest.java 2009/08/04 10:13:48 1.6 +++ jsr166/src/test/tck/RecursiveActionTest.java 2010/09/15 12:46:57 1.17 @@ -3,25 +3,46 @@ * Expert Group and released to the public domain, as explained at * http://creativecommons.org/licenses/publicdomain */ -import junit.framework.*; -import java.util.concurrent.*; -import java.util.*; +import junit.framework.*; +import java.util.concurrent.CancellationException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.ForkJoinWorkerThread; +import java.util.concurrent.RecursiveAction; +import java.util.concurrent.TimeUnit; +import java.util.HashSet; public class RecursiveActionTest 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(RecursiveActionTest.class); + return new TestSuite(RecursiveActionTest.class); + } + + private static ForkJoinPool mainPool() { + return new ForkJoinPool(); + } + + private static ForkJoinPool singletonPool() { + return new ForkJoinPool(1); } - static final ForkJoinPool mainPool = new ForkJoinPool(); - static final ForkJoinPool singletonPool = new ForkJoinPool(1); - static final ForkJoinPool asyncSingletonPool = new ForkJoinPool(1); - static { - asyncSingletonPool.setAsyncMode(true); + 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); + } } static final class FJException extends RuntimeException { @@ -68,21 +89,19 @@ public class RecursiveActionTest extends * 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() { - FibAction f = new FibAction(8); - f.invoke(); - threadAssertTrue(f.result == 21); - threadAssertTrue(f.isDone()); - threadAssertFalse(f.isCancelled()); - threadAssertFalse(f.isCompletedAbnormally()); - threadAssertTrue(f.getRawResult() == null); - } - }; - mainPool.invoke(a); + public void compute() { + FibAction f = new FibAction(8); + threadAssertNull(f.invoke()); + threadAssertTrue(f.result == 21); + threadAssertTrue(f.isDone()); + threadAssertFalse(f.isCancelled()); + threadAssertFalse(f.isCompletedAbnormally()); + threadAssertTrue(f.getRawResult() == null); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -92,17 +111,16 @@ public class RecursiveActionTest extends */ public void testQuietlyInvoke() { RecursiveAction a = new RecursiveAction() { - public void compute() { - FibAction f = new FibAction(8); - f.quietlyInvoke(); - threadAssertTrue(f.result == 21); - threadAssertTrue(f.isDone()); - threadAssertFalse(f.isCancelled()); - threadAssertFalse(f.isCompletedAbnormally()); - threadAssertTrue(f.getRawResult() == null); - } - }; - mainPool.invoke(a); + public void compute() { + FibAction f = new FibAction(8); + f.quietlyInvoke(); + threadAssertTrue(f.result == 21); + threadAssertTrue(f.isDone()); + threadAssertFalse(f.isCancelled()); + threadAssertFalse(f.isCompletedAbnormally()); + threadAssertTrue(f.getRawResult() == null); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -110,16 +128,15 @@ public class RecursiveActionTest extends */ public void testForkJoin() { RecursiveAction a = new RecursiveAction() { - public void compute() { - FibAction f = new FibAction(8); - f.fork(); - f.join(); - threadAssertTrue(f.result == 21); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.getRawResult() == null); - } - }; - mainPool.invoke(a); + public void compute() { + FibAction f = new FibAction(8); + threadAssertSame(f, f.fork()); + threadAssertNull(f.join()); + threadAssertTrue(f.result == 21); + threadAssertTrue(f.isDone()); + threadAssertTrue(f.getRawResult() == null); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -127,19 +144,18 @@ public class RecursiveActionTest extends */ public void testForkGet() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - FibAction f = new FibAction(8); - f.fork(); - f.get(); - threadAssertTrue(f.result == 21); - threadAssertTrue(f.isDone()); - } catch (Exception ex) { - unexpectedException(ex); - } + public void compute() { + try { + FibAction f = new FibAction(8); + threadAssertSame(f, f.fork()); + threadAssertNull(f.get()); + threadAssertTrue(f.result == 21); + threadAssertTrue(f.isDone()); + } catch (Exception ex) { + unexpectedException(ex); } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -147,19 +163,18 @@ public class RecursiveActionTest extends */ public void testForkTimedGet() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - FibAction f = new FibAction(8); - f.fork(); - f.get(5L, TimeUnit.SECONDS); - threadAssertTrue(f.result == 21); - threadAssertTrue(f.isDone()); - } catch (Exception ex) { - unexpectedException(ex); - } + public void compute() { + try { + FibAction f = new FibAction(8); + threadAssertSame(f, f.fork()); + threadAssertNull(f.get(5L, TimeUnit.SECONDS)); + threadAssertTrue(f.result == 21); + threadAssertTrue(f.isDone()); + } catch (Exception ex) { + unexpectedException(ex); } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -167,35 +182,18 @@ public class RecursiveActionTest extends */ public void testForkTimedGetNPE() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - FibAction f = new FibAction(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 { FibAction f = new FibAction(8); - f.fork(); - f.helpJoin(); - threadAssertTrue(f.result == 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); } /** @@ -203,32 +201,14 @@ public class RecursiveActionTest extends */ public void testForkQuietlyJoin() { RecursiveAction a = new RecursiveAction() { - public void compute() { - FibAction f = new FibAction(8); - f.fork(); - f.quietlyJoin(); - threadAssertTrue(f.result == 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() { - FibAction f = new FibAction(8); - f.fork(); - f.quietlyHelpJoin(); - threadAssertTrue(f.result == 21); - threadAssertTrue(f.isDone()); - } - }; - mainPool.invoke(a); + public void compute() { + FibAction f = new FibAction(8); + threadAssertSame(f, f.fork()); + f.quietlyJoin(); + threadAssertTrue(f.result == 21); + threadAssertTrue(f.isDone()); + }}; + testInvokeOnPool(mainPool(), a); } @@ -238,16 +218,15 @@ public class RecursiveActionTest extends */ public void testForkHelpQuiesce() { RecursiveAction a = new RecursiveAction() { - public void compute() { - FibAction f = new FibAction(8); - f.fork(); - f.helpQuiesce(); - threadAssertTrue(f.result == 21); - threadAssertTrue(f.isDone()); - threadAssertTrue(getQueuedTaskCount() == 0); - } - }; - mainPool.invoke(a); + public void compute() { + FibAction f = new FibAction(8); + threadAssertSame(f, f.fork()); + f.helpQuiesce(); + threadAssertTrue(f.result == 21); + threadAssertTrue(f.isDone()); + threadAssertTrue(getQueuedTaskCount() == 0); + }}; + testInvokeOnPool(mainPool(), a); } @@ -256,16 +235,15 @@ public class RecursiveActionTest extends */ public void testAbnormalInvoke() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - FailingFibAction f = new FailingFibAction(8); - f.invoke(); - shouldThrow(); - } catch (FJException success) { - } + public void compute() { + try { + FailingFibAction f = new FailingFibAction(8); + f.invoke(); + shouldThrow(); + } catch (FJException success) { } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -273,13 +251,12 @@ public class RecursiveActionTest extends */ public void testAbnormalQuietlyInvoke() { RecursiveAction a = new RecursiveAction() { - public void compute() { - FailingFibAction f = new FailingFibAction(8); - f.quietlyInvoke(); - threadAssertTrue(f.isDone()); - } - }; - mainPool.invoke(a); + public void compute() { + FailingFibAction f = new FailingFibAction(8); + f.quietlyInvoke(); + threadAssertTrue(f.isDone()); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -287,17 +264,16 @@ public class RecursiveActionTest extends */ public void testAbnormalForkJoin() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - FailingFibAction f = new FailingFibAction(8); - f.fork(); - f.join(); - shouldThrow(); - } catch (FJException success) { - } + public void compute() { + try { + FailingFibAction f = new FailingFibAction(8); + threadAssertSame(f, f.fork()); + f.join(); + shouldThrow(); + } catch (FJException success) { } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -305,19 +281,18 @@ public class RecursiveActionTest extends */ public void testAbnormalForkGet() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - FailingFibAction f = new FailingFibAction(8); - f.fork(); - f.get(); - shouldThrow(); - } catch (ExecutionException success) { - } catch (Exception ex) { - unexpectedException(ex); - } + public void compute() { + try { + FailingFibAction f = new FailingFibAction(8); + threadAssertSame(f, f.fork()); + f.get(); + shouldThrow(); + } catch (ExecutionException success) { + } catch (Exception ex) { + unexpectedException(ex); } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -325,58 +300,18 @@ public class RecursiveActionTest extends */ public void testAbnormalForkTimedGet() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - FailingFibAction f = new FailingFibAction(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 { - FailingFibAction f = new FailingFibAction(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 { FailingFibAction f = new FailingFibAction(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(5L, TimeUnit.SECONDS); + shouldThrow(); + } catch (ExecutionException success) { + } catch (Exception ex) { + unexpectedException(ex); } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -384,16 +319,15 @@ public class RecursiveActionTest extends */ public void testAbnormalForkQuietlyJoin() { RecursiveAction a = new RecursiveAction() { - public void compute() { - FailingFibAction f = new FailingFibAction(8); - f.fork(); - f.quietlyJoin(); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.isCompletedAbnormally()); - threadAssertTrue(f.getException() instanceof FJException); - } - }; - mainPool.invoke(a); + public void compute() { + FailingFibAction f = new FailingFibAction(8); + threadAssertSame(f, f.fork()); + f.quietlyJoin(); + threadAssertTrue(f.isDone()); + threadAssertTrue(f.isCompletedAbnormally()); + threadAssertTrue(f.getException() instanceof FJException); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -401,17 +335,16 @@ public class RecursiveActionTest extends */ public void testCancelledInvoke() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - FibAction f = new FibAction(8); - f.cancel(true); - f.invoke(); - shouldThrow(); - } catch (CancellationException success) { - } + public void compute() { + try { + FibAction f = new FibAction(8); + threadAssertTrue(f.cancel(true)); + f.invoke(); + shouldThrow(); + } catch (CancellationException success) { } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -419,18 +352,17 @@ public class RecursiveActionTest extends */ public void testCancelledForkJoin() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - FibAction f = new FibAction(8); - f.cancel(true); - f.fork(); - f.join(); - shouldThrow(); - } catch (CancellationException success) { - } + public void compute() { + try { + FibAction f = new FibAction(8); + threadAssertTrue(f.cancel(true)); + threadAssertSame(f, f.fork()); + f.join(); + shouldThrow(); + } catch (CancellationException success) { } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -438,20 +370,19 @@ public class RecursiveActionTest extends */ public void testCancelledForkGet() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - FibAction f = new FibAction(8); - f.cancel(true); - f.fork(); - f.get(); - shouldThrow(); - } catch (CancellationException success) { - } catch (Exception ex) { - unexpectedException(ex); - } + public void compute() { + try { + FibAction f = new FibAction(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); } /** @@ -459,61 +390,19 @@ public class RecursiveActionTest extends */ public void testCancelledForkTimedGet() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - FibAction f = new FibAction(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 { - FibAction f = new FibAction(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 { FibAction f = new FibAction(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(5L, TimeUnit.SECONDS); + shouldThrow(); + } catch (CancellationException success) { + } catch (Exception ex) { + unexpectedException(ex); } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -521,29 +410,29 @@ public class RecursiveActionTest extends */ public void testCancelledForkQuietlyJoin() { RecursiveAction a = new RecursiveAction() { - public void compute() { - FibAction f = new FibAction(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() { + FibAction f = new FibAction(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); + final ForkJoinPool p = mainPool; + public void compute() { + threadAssertTrue(getPool() == p); + }}; + testInvokeOnPool(mainPool, a); } /** @@ -551,11 +440,10 @@ public class RecursiveActionTest extends */ public void testGetPool2() { RecursiveAction a = new RecursiveAction() { - public void compute() { - threadAssertTrue(getPool() == null); - } - }; - a.invoke(); + public void compute() { + threadAssertTrue(getPool() == null); + }}; + assertNull(a.invoke()); } /** @@ -563,11 +451,10 @@ public class RecursiveActionTest extends */ public void testInForkJoinPool() { RecursiveAction a = new RecursiveAction() { - public void compute() { - threadAssertTrue(inForkJoinPool()); - } - }; - mainPool.invoke(a); + public void compute() { + threadAssertTrue(inForkJoinPool()); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -575,42 +462,40 @@ public class RecursiveActionTest extends */ public void testInForkJoinPool2() { RecursiveAction a = new RecursiveAction() { - public void compute() { - threadAssertTrue(!inForkJoinPool()); - } - }; - a.invoke(); + public void compute() { + threadAssertTrue(!inForkJoinPool()); + }}; + assertNull(a.invoke()); } /** * getPool of current thread in pool returns its pool */ public void testWorkerGetPool() { + final ForkJoinPool mainPool = mainPool(); RecursiveAction a = new RecursiveAction() { - public void compute() { - ForkJoinWorkerThread w = - (ForkJoinWorkerThread)(Thread.currentThread()); - threadAssertTrue(w.getPool() == mainPool); - } - }; - mainPool.invoke(a); + public void compute() { + ForkJoinWorkerThread w = + (ForkJoinWorkerThread) Thread.currentThread(); + threadAssertTrue(w.getPool() == mainPool); + }}; + testInvokeOnPool(mainPool, a); } /** * getPoolIndex of current thread in pool returns 0 <= value < poolSize - * */ public void testWorkerGetPoolIndex() { + final ForkJoinPool mainPool = mainPool(); RecursiveAction a = new RecursiveAction() { - public void compute() { - ForkJoinWorkerThread w = - (ForkJoinWorkerThread)(Thread.currentThread()); - int idx = w.getPoolIndex(); - threadAssertTrue(idx >= 0); - threadAssertTrue(idx < mainPool.getPoolSize()); - } - }; - mainPool.invoke(a); + public void compute() { + ForkJoinWorkerThread w = + (ForkJoinWorkerThread)(Thread.currentThread()); + int idx = w.getPoolIndex(); + threadAssertTrue(idx >= 0); + threadAssertTrue(idx < mainPool.getPoolSize()); + }}; + testInvokeOnPool(mainPool, a); } @@ -619,11 +504,10 @@ public class RecursiveActionTest extends */ public void testSetRawResult() { RecursiveAction a = new RecursiveAction() { - public void compute() { - setRawResult(null); - } - }; - a.invoke(); + public void compute() { + setRawResult(null); + }}; + assertNull(a.invoke()); } /** @@ -631,19 +515,18 @@ public class RecursiveActionTest extends */ public void testReinitialize() { RecursiveAction a = new RecursiveAction() { - public void compute() { - FibAction f = new FibAction(8); - f.invoke(); - threadAssertTrue(f.result == 21); - threadAssertTrue(f.isDone()); - threadAssertFalse(f.isCancelled()); - threadAssertFalse(f.isCompletedAbnormally()); - f.reinitialize(); - f.invoke(); - threadAssertTrue(f.result == 21); - } - }; - mainPool.invoke(a); + public void compute() { + FibAction f = new FibAction(8); + threadAssertNull(f.invoke()); + threadAssertTrue(f.result == 21); + threadAssertTrue(f.isDone()); + threadAssertFalse(f.isCancelled()); + threadAssertFalse(f.isCompletedAbnormally()); + f.reinitialize(); + threadAssertNull(f.invoke()); + threadAssertTrue(f.result == 21); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -651,17 +534,16 @@ public class RecursiveActionTest extends */ public void testCompleteExceptionally() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - FibAction f = new FibAction(8); - f.completeExceptionally(new FJException()); - f.invoke(); - shouldThrow(); - } catch (FJException success) { - } + public void compute() { + try { + FibAction f = new FibAction(8); + f.completeExceptionally(new FJException()); + f.invoke(); + shouldThrow(); + } catch (FJException success) { } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -669,15 +551,14 @@ public class RecursiveActionTest extends */ public void testComplete() { RecursiveAction a = new RecursiveAction() { - public void compute() { - FibAction f = new FibAction(8); - f.complete(null); - f.invoke(); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.result == 0); - } - }; - mainPool.invoke(a); + public void compute() { + FibAction f = new FibAction(8); + f.complete(null); + threadAssertNull(f.invoke()); + threadAssertTrue(f.isDone()); + threadAssertTrue(f.result == 0); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -685,17 +566,16 @@ public class RecursiveActionTest extends */ public void testInvokeAll2() { RecursiveAction a = new RecursiveAction() { - public void compute() { - FibAction f = new FibAction(8); - FibAction g = new FibAction(9); - invokeAll(f, g); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.result == 21); - threadAssertTrue(g.isDone()); - threadAssertTrue(g.result == 34); - } - }; - mainPool.invoke(a); + public void compute() { + FibAction f = new FibAction(8); + FibAction g = new FibAction(9); + invokeAll(f, g); + threadAssertTrue(f.isDone()); + threadAssertTrue(f.result == 21); + threadAssertTrue(g.isDone()); + threadAssertTrue(g.result == 34); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -703,14 +583,13 @@ public class RecursiveActionTest extends */ public void testInvokeAll1() { RecursiveAction a = new RecursiveAction() { - public void compute() { - FibAction f = new FibAction(8); - invokeAll(f); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.result == 21); - } - }; - mainPool.invoke(a); + public void compute() { + FibAction f = new FibAction(8); + invokeAll(f); + threadAssertTrue(f.isDone()); + threadAssertTrue(f.result == 21); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -718,20 +597,19 @@ public class RecursiveActionTest extends */ public void testInvokeAll3() { RecursiveAction a = new RecursiveAction() { - public void compute() { - FibAction f = new FibAction(8); - FibAction g = new FibAction(9); - FibAction h = new FibAction(7); - invokeAll(f, g, h); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.result == 21); - threadAssertTrue(g.isDone()); - threadAssertTrue(g.result == 34); - threadAssertTrue(h.isDone()); - threadAssertTrue(h.result == 13); - } - }; - mainPool.invoke(a); + public void compute() { + FibAction f = new FibAction(8); + FibAction g = new FibAction(9); + FibAction h = new FibAction(7); + invokeAll(f, g, h); + threadAssertTrue(f.isDone()); + threadAssertTrue(f.result == 21); + threadAssertTrue(g.isDone()); + threadAssertTrue(g.result == 34); + threadAssertTrue(h.isDone()); + threadAssertTrue(h.result == 13); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -739,24 +617,23 @@ public class RecursiveActionTest extends */ public void testInvokeAllCollection() { RecursiveAction a = new RecursiveAction() { - public void compute() { - FibAction f = new FibAction(8); - FibAction g = new FibAction(9); - FibAction h = new FibAction(7); - HashSet set = new HashSet(); - set.add(f); - set.add(g); - set.add(h); - invokeAll(set); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.result == 21); - threadAssertTrue(g.isDone()); - threadAssertTrue(g.result == 34); - threadAssertTrue(h.isDone()); - threadAssertTrue(h.result == 13); - } - }; - mainPool.invoke(a); + public void compute() { + FibAction f = new FibAction(8); + FibAction g = new FibAction(9); + FibAction h = new FibAction(7); + HashSet set = new HashSet(); + set.add(f); + set.add(g); + set.add(h); + invokeAll(set); + threadAssertTrue(f.isDone()); + threadAssertTrue(f.result == 21); + threadAssertTrue(g.isDone()); + threadAssertTrue(g.result == 34); + threadAssertTrue(h.isDone()); + threadAssertTrue(h.result == 13); + }}; + testInvokeOnPool(mainPool(), a); } @@ -765,18 +642,17 @@ public class RecursiveActionTest extends */ public void testInvokeAllNPE() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - FibAction f = new FibAction(8); - FibAction g = new FibAction(9); - FibAction h = null; - invokeAll(f, g, h); - shouldThrow(); - } catch (NullPointerException success) { - } + public void compute() { + try { + FibAction f = new FibAction(8); + FibAction g = new FibAction(9); + FibAction h = null; + invokeAll(f, g, h); + shouldThrow(); + } catch (NullPointerException success) { } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -784,17 +660,16 @@ public class RecursiveActionTest extends */ public void testAbnormalInvokeAll2() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - FibAction f = new FibAction(8); - FailingFibAction g = new FailingFibAction(9); - invokeAll(f, g); - shouldThrow(); - } catch (FJException success) { - } + public void compute() { + try { + FibAction f = new FibAction(8); + FailingFibAction g = new FailingFibAction(9); + invokeAll(f, g); + shouldThrow(); + } catch (FJException success) { } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -802,16 +677,15 @@ public class RecursiveActionTest extends */ public void testAbnormalInvokeAll1() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - FailingFibAction g = new FailingFibAction(9); - invokeAll(g); - shouldThrow(); - } catch (FJException success) { - } + public void compute() { + try { + FailingFibAction g = new FailingFibAction(9); + invokeAll(g); + shouldThrow(); + } catch (FJException success) { } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -819,18 +693,17 @@ public class RecursiveActionTest extends */ public void testAbnormalInvokeAll3() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - FibAction f = new FibAction(8); - FailingFibAction g = new FailingFibAction(9); - FibAction h = new FibAction(7); - invokeAll(f, g, h); - shouldThrow(); - } catch (FJException success) { - } + public void compute() { + try { + FibAction f = new FibAction(8); + FailingFibAction g = new FailingFibAction(9); + FibAction h = new FibAction(7); + invokeAll(f, g, h); + shouldThrow(); + } catch (FJException success) { } - }; - mainPool.invoke(a); + }}; + testInvokeOnPool(mainPool(), a); } /** @@ -838,22 +711,21 @@ public class RecursiveActionTest extends */ public void testAbnormalInvokeAllCollection() { RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - FailingFibAction f = new FailingFibAction(8); - FibAction g = new FibAction(9); - FibAction h = new FibAction(7); - HashSet set = new HashSet(); - set.add(f); - set.add(g); - set.add(h); - invokeAll(set); - shouldThrow(); - } catch (FJException success) { - } + public void compute() { + try { + FailingFibAction f = new FailingFibAction(8); + FibAction g = new FibAction(9); + FibAction h = new FibAction(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); } /** @@ -862,18 +734,17 @@ public class RecursiveActionTest extends */ public void testTryUnfork() { RecursiveAction a = new RecursiveAction() { - public void compute() { - FibAction g = new FibAction(9); - g.fork(); - FibAction f = new FibAction(8); - f.fork(); - threadAssertTrue(f.tryUnfork()); - helpQuiesce(); - threadAssertFalse(f.isDone()); - threadAssertTrue(g.isDone()); - } - }; - singletonPool.invoke(a); + public void compute() { + FibAction g = new FibAction(9); + threadAssertSame(g, g.fork()); + FibAction f = new FibAction(8); + threadAssertSame(f, f.fork()); + threadAssertTrue(f.tryUnfork()); + helpQuiesce(); + threadAssertFalse(f.isDone()); + threadAssertTrue(g.isDone()); + }}; + testInvokeOnPool(singletonPool(), a); } /** @@ -882,18 +753,17 @@ public class RecursiveActionTest extends */ public void testGetSurplusQueuedTaskCount() { RecursiveAction a = new RecursiveAction() { - public void compute() { - FibAction h = new FibAction(7); - h.fork(); - FibAction g = new FibAction(9); - g.fork(); - FibAction f = new FibAction(8); - f.fork(); - threadAssertTrue(getSurplusQueuedTaskCount() > 0); - helpQuiesce(); - } - }; - singletonPool.invoke(a); + public void compute() { + FibAction h = new FibAction(7); + threadAssertSame(h, h.fork()); + FibAction g = new FibAction(9); + threadAssertSame(g, g.fork()); + FibAction f = new FibAction(8); + threadAssertSame(f, f.fork()); + threadAssertTrue(getSurplusQueuedTaskCount() > 0); + helpQuiesce(); + }}; + testInvokeOnPool(singletonPool(), a); } /** @@ -901,18 +771,17 @@ public class RecursiveActionTest extends */ public void testPeekNextLocalTask() { RecursiveAction a = new RecursiveAction() { - public void compute() { - FibAction g = new FibAction(9); - g.fork(); - FibAction f = new FibAction(8); - f.fork(); - threadAssertTrue(peekNextLocalTask() == f); - f.join(); - threadAssertTrue(f.isDone()); - helpQuiesce(); - } - }; - singletonPool.invoke(a); + public void compute() { + FibAction g = new FibAction(9); + threadAssertSame(g, g.fork()); + FibAction f = new FibAction(8); + threadAssertSame(f, f.fork()); + threadAssertTrue(peekNextLocalTask() == f); + threadAssertNull(f.join()); + threadAssertTrue(f.isDone()); + helpQuiesce(); + }}; + testInvokeOnPool(singletonPool(), a); } /** @@ -921,17 +790,16 @@ public class RecursiveActionTest extends */ public void testPollNextLocalTask() { RecursiveAction a = new RecursiveAction() { - public void compute() { - FibAction g = new FibAction(9); - g.fork(); - FibAction f = new FibAction(8); - f.fork(); - threadAssertTrue(pollNextLocalTask() == f); - helpQuiesce(); - threadAssertFalse(f.isDone()); - } - }; - singletonPool.invoke(a); + public void compute() { + FibAction g = new FibAction(9); + threadAssertSame(g, g.fork()); + FibAction f = new FibAction(8); + threadAssertSame(f, f.fork()); + threadAssertTrue(pollNextLocalTask() == f); + helpQuiesce(); + threadAssertFalse(f.isDone()); + }}; + testInvokeOnPool(singletonPool(), a); } /** @@ -940,18 +808,17 @@ public class RecursiveActionTest extends */ public void testPollTask() { RecursiveAction a = new RecursiveAction() { - public void compute() { - FibAction g = new FibAction(9); - g.fork(); - FibAction f = new FibAction(8); - f.fork(); - threadAssertTrue(pollTask() == f); - helpQuiesce(); - threadAssertFalse(f.isDone()); - threadAssertTrue(g.isDone()); - } - }; - singletonPool.invoke(a); + public void compute() { + FibAction g = new FibAction(9); + threadAssertSame(g, g.fork()); + FibAction f = new FibAction(8); + threadAssertSame(f, f.fork()); + threadAssertTrue(pollTask() == f); + helpQuiesce(); + threadAssertFalse(f.isDone()); + threadAssertTrue(g.isDone()); + }}; + testInvokeOnPool(singletonPool(), a); } /** @@ -959,18 +826,17 @@ public class RecursiveActionTest extends */ public void testPeekNextLocalTaskAsync() { RecursiveAction a = new RecursiveAction() { - public void compute() { - FibAction g = new FibAction(9); - g.fork(); - FibAction f = new FibAction(8); - f.fork(); - threadAssertTrue(peekNextLocalTask() == g); - f.join(); - helpQuiesce(); - threadAssertTrue(f.isDone()); - } - }; - asyncSingletonPool.invoke(a); + public void compute() { + FibAction g = new FibAction(9); + threadAssertSame(g, g.fork()); + FibAction f = new FibAction(8); + threadAssertSame(f, f.fork()); + threadAssertTrue(peekNextLocalTask() == g); + threadAssertNull(f.join()); + helpQuiesce(); + threadAssertTrue(f.isDone()); + }}; + testInvokeOnPool(asyncSingletonPool(), a); } /** @@ -979,18 +845,17 @@ public class RecursiveActionTest extends */ public void testPollNextLocalTaskAsync() { RecursiveAction a = new RecursiveAction() { - public void compute() { - FibAction g = new FibAction(9); - g.fork(); - FibAction f = new FibAction(8); - f.fork(); - threadAssertTrue(pollNextLocalTask() == g); - helpQuiesce(); - threadAssertTrue(f.isDone()); - threadAssertFalse(g.isDone()); - } - }; - asyncSingletonPool.invoke(a); + public void compute() { + FibAction g = new FibAction(9); + threadAssertSame(g, g.fork()); + FibAction f = new FibAction(8); + threadAssertSame(f, f.fork()); + threadAssertTrue(pollNextLocalTask() == g); + helpQuiesce(); + threadAssertTrue(f.isDone()); + threadAssertFalse(g.isDone()); + }}; + testInvokeOnPool(asyncSingletonPool(), a); } /** @@ -999,18 +864,17 @@ public class RecursiveActionTest extends */ public void testPollTaskAsync() { RecursiveAction a = new RecursiveAction() { - public void compute() { - FibAction g = new FibAction(9); - g.fork(); - FibAction f = new FibAction(8); - f.fork(); - threadAssertTrue(pollTask() == g); - helpQuiesce(); - threadAssertTrue(f.isDone()); - threadAssertFalse(g.isDone()); - } - }; - asyncSingletonPool.invoke(a); + public void compute() { + FibAction g = new FibAction(9); + threadAssertSame(g, g.fork()); + FibAction f = new FibAction(8); + threadAssertSame(f, f.fork()); + threadAssertTrue(pollTask() == g); + helpQuiesce(); + threadAssertTrue(f.isDone()); + threadAssertFalse(g.isDone()); + }}; + testInvokeOnPool(asyncSingletonPool(), a); } }