--- jsr166/src/test/tck/RecursiveActionTest.java 2009/11/21 02:07:27 1.9 +++ jsr166/src/test/tck/RecursiveActionTest.java 2010/09/13 23:23:45 1.16 @@ -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); } - 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 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); + } } static final class FJException extends RuntimeException { @@ -68,20 +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(); + threadAssertNull(f.invoke()); threadAssertTrue(f.result == 21); threadAssertTrue(f.isDone()); threadAssertFalse(f.isCancelled()); threadAssertFalse(f.isCompletedAbnormally()); threadAssertTrue(f.getRawResult() == null); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -100,7 +120,7 @@ public class RecursiveActionTest extends threadAssertFalse(f.isCompletedAbnormally()); threadAssertTrue(f.getRawResult() == null); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -110,13 +130,13 @@ public class RecursiveActionTest extends RecursiveAction a = new RecursiveAction() { public void compute() { FibAction f = new FibAction(8); - f.fork(); - f.join(); + threadAssertSame(f, f.fork()); + threadAssertNull(f.join()); threadAssertTrue(f.result == 21); threadAssertTrue(f.isDone()); threadAssertTrue(f.getRawResult() == null); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -127,15 +147,15 @@ public class RecursiveActionTest extends public void compute() { try { FibAction f = new FibAction(8); - f.fork(); - f.get(); + 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); } /** @@ -146,15 +166,15 @@ public class RecursiveActionTest extends public void compute() { try { FibAction f = new FibAction(8); - f.fork(); - f.get(5L, TimeUnit.SECONDS); + 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); } /** @@ -165,7 +185,7 @@ public class RecursiveActionTest extends public void compute() { try { FibAction f = new FibAction(8); - f.fork(); + threadAssertSame(f, f.fork()); f.get(5L, null); shouldThrow(); } catch (NullPointerException success) { @@ -173,22 +193,7 @@ public class RecursiveActionTest extends unexpectedException(ex); } }}; - mainPool.invoke(a); - } - - /** - * helpJoin of a forked task returns when task completes - */ - public void testForkHelpJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - FibAction f = new FibAction(8); - f.fork(); - f.helpJoin(); - threadAssertTrue(f.result == 21); - threadAssertTrue(f.isDone()); - }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -198,28 +203,12 @@ public class RecursiveActionTest extends RecursiveAction a = new RecursiveAction() { public void compute() { FibAction f = new FibAction(8); - f.fork(); + threadAssertSame(f, 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); + testInvokeOnPool(mainPool(), a); } @@ -231,13 +220,13 @@ public class RecursiveActionTest extends RecursiveAction a = new RecursiveAction() { public void compute() { FibAction f = new FibAction(8); - f.fork(); + threadAssertSame(f, f.fork()); f.helpQuiesce(); threadAssertTrue(f.result == 21); threadAssertTrue(f.isDone()); threadAssertTrue(getQueuedTaskCount() == 0); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } @@ -254,7 +243,7 @@ public class RecursiveActionTest extends } catch (FJException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -267,7 +256,7 @@ public class RecursiveActionTest extends f.quietlyInvoke(); threadAssertTrue(f.isDone()); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -278,13 +267,13 @@ public class RecursiveActionTest extends public void compute() { try { FailingFibAction f = new FailingFibAction(8); - f.fork(); + threadAssertSame(f, f.fork()); f.join(); shouldThrow(); } catch (FJException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -295,7 +284,7 @@ public class RecursiveActionTest extends public void compute() { try { FailingFibAction f = new FailingFibAction(8); - f.fork(); + threadAssertSame(f, f.fork()); f.get(); shouldThrow(); } catch (ExecutionException success) { @@ -303,7 +292,7 @@ public class RecursiveActionTest extends unexpectedException(ex); } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -314,7 +303,7 @@ public class RecursiveActionTest extends public void compute() { try { FailingFibAction f = new FailingFibAction(8); - f.fork(); + threadAssertSame(f, f.fork()); f.get(5L, TimeUnit.SECONDS); shouldThrow(); } catch (ExecutionException success) { @@ -322,44 +311,7 @@ public class RecursiveActionTest extends 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() { - FailingFibAction f = new FailingFibAction(8); - f.fork(); - f.quietlyHelpJoin(); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.isCompletedAbnormally()); - threadAssertFalse(f.isCancelled()); - threadAssertTrue(f.getException() instanceof FJException); - }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -369,13 +321,13 @@ public class RecursiveActionTest extends RecursiveAction a = new RecursiveAction() { public void compute() { FailingFibAction f = new FailingFibAction(8); - f.fork(); + threadAssertSame(f, f.fork()); f.quietlyJoin(); threadAssertTrue(f.isDone()); threadAssertTrue(f.isCompletedAbnormally()); threadAssertTrue(f.getException() instanceof FJException); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -386,13 +338,13 @@ public class RecursiveActionTest extends public void compute() { try { FibAction f = new FibAction(8); - f.cancel(true); + threadAssertTrue(f.cancel(true)); f.invoke(); shouldThrow(); } catch (CancellationException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -403,14 +355,14 @@ public class RecursiveActionTest extends public void compute() { try { FibAction f = new FibAction(8); - f.cancel(true); - f.fork(); + threadAssertTrue(f.cancel(true)); + threadAssertSame(f, f.fork()); f.join(); shouldThrow(); } catch (CancellationException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -421,8 +373,8 @@ public class RecursiveActionTest extends public void compute() { try { FibAction f = new FibAction(8); - f.cancel(true); - f.fork(); + threadAssertTrue(f.cancel(true)); + threadAssertSame(f, f.fork()); f.get(); shouldThrow(); } catch (CancellationException success) { @@ -430,7 +382,7 @@ public class RecursiveActionTest extends unexpectedException(ex); } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -441,8 +393,8 @@ public class RecursiveActionTest extends public void compute() { try { FibAction f = new FibAction(8); - f.cancel(true); - f.fork(); + threadAssertTrue(f.cancel(true)); + threadAssertSame(f, f.fork()); f.get(5L, TimeUnit.SECONDS); shouldThrow(); } catch (CancellationException success) { @@ -450,46 +402,7 @@ public class RecursiveActionTest extends 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() { - 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); - }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -499,25 +412,26 @@ public class RecursiveActionTest extends RecursiveAction a = new RecursiveAction() { public void compute() { FibAction f = new FibAction(8); - f.cancel(true); - f.fork(); + threadAssertTrue(f.cancel(true)); + threadAssertSame(f, f.fork()); f.quietlyJoin(); threadAssertTrue(f.isDone()); threadAssertTrue(f.isCompletedAbnormally()); threadAssertTrue(f.getException() instanceof CancellationException); }}; - mainPool.invoke(a); + 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); + testInvokeOnPool(mainPool, a); } /** @@ -528,7 +442,7 @@ public class RecursiveActionTest extends public void compute() { threadAssertTrue(getPool() == null); }}; - a.invoke(); + assertNull(a.invoke()); } /** @@ -539,7 +453,7 @@ public class RecursiveActionTest extends public void compute() { threadAssertTrue(inForkJoinPool()); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -550,27 +464,28 @@ public class RecursiveActionTest extends public void compute() { threadAssertTrue(!inForkJoinPool()); }}; - a.invoke(); + 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()); + (ForkJoinWorkerThread) Thread.currentThread(); threadAssertTrue(w.getPool() == mainPool); }}; - mainPool.invoke(a); + 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 = @@ -579,7 +494,7 @@ public class RecursiveActionTest extends threadAssertTrue(idx >= 0); threadAssertTrue(idx < mainPool.getPoolSize()); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool, a); } @@ -591,7 +506,7 @@ public class RecursiveActionTest extends public void compute() { setRawResult(null); }}; - a.invoke(); + assertNull(a.invoke()); } /** @@ -601,16 +516,16 @@ public class RecursiveActionTest extends RecursiveAction a = new RecursiveAction() { public void compute() { FibAction f = new FibAction(8); - f.invoke(); + threadAssertNull(f.invoke()); threadAssertTrue(f.result == 21); threadAssertTrue(f.isDone()); threadAssertFalse(f.isCancelled()); threadAssertFalse(f.isCompletedAbnormally()); f.reinitialize(); - f.invoke(); + threadAssertNull(f.invoke()); threadAssertTrue(f.result == 21); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -627,7 +542,7 @@ public class RecursiveActionTest extends } catch (FJException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -638,11 +553,11 @@ public class RecursiveActionTest extends public void compute() { FibAction f = new FibAction(8); f.complete(null); - f.invoke(); + threadAssertNull(f.invoke()); threadAssertTrue(f.isDone()); threadAssertTrue(f.result == 0); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -659,7 +574,7 @@ public class RecursiveActionTest extends threadAssertTrue(g.isDone()); threadAssertTrue(g.result == 34); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -673,7 +588,7 @@ public class RecursiveActionTest extends threadAssertTrue(f.isDone()); threadAssertTrue(f.result == 21); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -693,7 +608,7 @@ public class RecursiveActionTest extends threadAssertTrue(h.isDone()); threadAssertTrue(h.result == 13); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -717,7 +632,7 @@ public class RecursiveActionTest extends threadAssertTrue(h.isDone()); threadAssertTrue(h.result == 13); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } @@ -736,7 +651,7 @@ public class RecursiveActionTest extends } catch (NullPointerException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -753,7 +668,7 @@ public class RecursiveActionTest extends } catch (FJException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -769,7 +684,7 @@ public class RecursiveActionTest extends } catch (FJException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -787,7 +702,7 @@ public class RecursiveActionTest extends } catch (FJException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -809,7 +724,7 @@ public class RecursiveActionTest extends } catch (FJException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -820,15 +735,15 @@ public class RecursiveActionTest extends RecursiveAction a = new RecursiveAction() { public void compute() { FibAction g = new FibAction(9); - g.fork(); + threadAssertSame(g, g.fork()); FibAction f = new FibAction(8); - f.fork(); + threadAssertSame(f, f.fork()); threadAssertTrue(f.tryUnfork()); helpQuiesce(); threadAssertFalse(f.isDone()); threadAssertTrue(g.isDone()); }}; - singletonPool.invoke(a); + testInvokeOnPool(singletonPool(), a); } /** @@ -839,15 +754,15 @@ public class RecursiveActionTest extends RecursiveAction a = new RecursiveAction() { public void compute() { FibAction h = new FibAction(7); - h.fork(); + threadAssertSame(h, h.fork()); FibAction g = new FibAction(9); - g.fork(); + threadAssertSame(g, g.fork()); FibAction f = new FibAction(8); - f.fork(); + threadAssertSame(f, f.fork()); threadAssertTrue(getSurplusQueuedTaskCount() > 0); helpQuiesce(); }}; - singletonPool.invoke(a); + testInvokeOnPool(singletonPool(), a); } /** @@ -857,15 +772,15 @@ public class RecursiveActionTest extends RecursiveAction a = new RecursiveAction() { public void compute() { FibAction g = new FibAction(9); - g.fork(); + threadAssertSame(g, g.fork()); FibAction f = new FibAction(8); - f.fork(); + threadAssertSame(f, f.fork()); threadAssertTrue(peekNextLocalTask() == f); - f.join(); + threadAssertNull(f.join()); threadAssertTrue(f.isDone()); helpQuiesce(); }}; - singletonPool.invoke(a); + testInvokeOnPool(singletonPool(), a); } /** @@ -876,14 +791,14 @@ public class RecursiveActionTest extends RecursiveAction a = new RecursiveAction() { public void compute() { FibAction g = new FibAction(9); - g.fork(); + threadAssertSame(g, g.fork()); FibAction f = new FibAction(8); - f.fork(); + threadAssertSame(f, f.fork()); threadAssertTrue(pollNextLocalTask() == f); helpQuiesce(); threadAssertFalse(f.isDone()); }}; - singletonPool.invoke(a); + testInvokeOnPool(singletonPool(), a); } /** @@ -894,15 +809,15 @@ public class RecursiveActionTest extends RecursiveAction a = new RecursiveAction() { public void compute() { FibAction g = new FibAction(9); - g.fork(); + threadAssertSame(g, g.fork()); FibAction f = new FibAction(8); - f.fork(); + threadAssertSame(f, f.fork()); threadAssertTrue(pollTask() == f); helpQuiesce(); threadAssertFalse(f.isDone()); threadAssertTrue(g.isDone()); }}; - singletonPool.invoke(a); + testInvokeOnPool(singletonPool(), a); } /** @@ -912,15 +827,15 @@ public class RecursiveActionTest extends RecursiveAction a = new RecursiveAction() { public void compute() { FibAction g = new FibAction(9); - g.fork(); + threadAssertSame(g, g.fork()); FibAction f = new FibAction(8); - f.fork(); + threadAssertSame(f, f.fork()); threadAssertTrue(peekNextLocalTask() == g); - f.join(); + threadAssertNull(f.join()); helpQuiesce(); threadAssertTrue(f.isDone()); }}; - asyncSingletonPool.invoke(a); + testInvokeOnPool(asyncSingletonPool(), a); } /** @@ -931,15 +846,15 @@ public class RecursiveActionTest extends RecursiveAction a = new RecursiveAction() { public void compute() { FibAction g = new FibAction(9); - g.fork(); + threadAssertSame(g, g.fork()); FibAction f = new FibAction(8); - f.fork(); + threadAssertSame(f, f.fork()); threadAssertTrue(pollNextLocalTask() == g); helpQuiesce(); threadAssertTrue(f.isDone()); threadAssertFalse(g.isDone()); }}; - asyncSingletonPool.invoke(a); + testInvokeOnPool(asyncSingletonPool(), a); } /** @@ -950,15 +865,15 @@ public class RecursiveActionTest extends RecursiveAction a = new RecursiveAction() { public void compute() { FibAction g = new FibAction(9); - g.fork(); + threadAssertSame(g, g.fork()); FibAction f = new FibAction(8); - f.fork(); + threadAssertSame(f, f.fork()); threadAssertTrue(pollTask() == g); helpQuiesce(); threadAssertTrue(f.isDone()); threadAssertFalse(g.isDone()); }}; - asyncSingletonPool.invoke(a); + testInvokeOnPool(asyncSingletonPool(), a); } }