--- jsr166/src/test/tck/RecursiveActionTest.java 2009/08/05 01:17:30 1.7 +++ 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); + } + + private static ForkJoinPool asyncSingletonPool() { + return new ForkJoinPool(1, + ForkJoinPool.defaultForkJoinWorkerThreadFactory, + null, true); } - 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 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); } /** @@ -162,34 +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() { - FibAction f = new FibAction(8); - f.fork(); - f.helpJoin(); - threadAssertTrue(f.result == 21); - threadAssertTrue(f.isDone()); + try { + FibAction f = new FibAction(8); + threadAssertSame(f, f.fork()); + f.get(5L, null); + shouldThrow(); + } catch (NullPointerException success) { + } catch (Exception ex) { + unexpectedException(ex); + } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -199,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); } @@ -232,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); } @@ -255,7 +243,7 @@ public class RecursiveActionTest extends } catch (FJException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -268,7 +256,7 @@ public class RecursiveActionTest extends f.quietlyInvoke(); threadAssertTrue(f.isDone()); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -279,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); } /** @@ -296,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) { @@ -304,7 +292,7 @@ public class RecursiveActionTest extends unexpectedException(ex); } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -315,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) { @@ -323,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); } /** @@ -370,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); } /** @@ -387,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); } /** @@ -404,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); } /** @@ -422,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) { @@ -431,7 +382,7 @@ public class RecursiveActionTest extends unexpectedException(ex); } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -442,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) { @@ -451,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); } /** @@ -500,25 +412,27 @@ 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() { + final ForkJoinPool p = mainPool; public void compute() { - threadAssertTrue(getPool() == mainPool); + threadAssertTrue(getPool() == p); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool, a); } /** @@ -529,7 +443,7 @@ public class RecursiveActionTest extends public void compute() { threadAssertTrue(getPool() == null); }}; - a.invoke(); + assertNull(a.invoke()); } /** @@ -540,7 +454,7 @@ public class RecursiveActionTest extends public void compute() { threadAssertTrue(inForkJoinPool()); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -551,27 +465,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 = @@ -580,7 +495,7 @@ public class RecursiveActionTest extends threadAssertTrue(idx >= 0); threadAssertTrue(idx < mainPool.getPoolSize()); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool, a); } @@ -592,7 +507,7 @@ public class RecursiveActionTest extends public void compute() { setRawResult(null); }}; - a.invoke(); + assertNull(a.invoke()); } /** @@ -602,16 +517,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); } /** @@ -628,7 +543,7 @@ public class RecursiveActionTest extends } catch (FJException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -639,11 +554,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); } /** @@ -660,7 +575,7 @@ public class RecursiveActionTest extends threadAssertTrue(g.isDone()); threadAssertTrue(g.result == 34); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -674,7 +589,7 @@ public class RecursiveActionTest extends threadAssertTrue(f.isDone()); threadAssertTrue(f.result == 21); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -694,7 +609,7 @@ public class RecursiveActionTest extends threadAssertTrue(h.isDone()); threadAssertTrue(h.result == 13); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -718,7 +633,7 @@ public class RecursiveActionTest extends threadAssertTrue(h.isDone()); threadAssertTrue(h.result == 13); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } @@ -737,7 +652,7 @@ public class RecursiveActionTest extends } catch (NullPointerException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -754,7 +669,7 @@ public class RecursiveActionTest extends } catch (FJException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -770,7 +685,7 @@ public class RecursiveActionTest extends } catch (FJException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -788,7 +703,7 @@ public class RecursiveActionTest extends } catch (FJException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -810,7 +725,7 @@ public class RecursiveActionTest extends } catch (FJException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -821,15 +736,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); } /** @@ -840,15 +755,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); } /** @@ -858,15 +773,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); } /** @@ -877,14 +792,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); } /** @@ -895,15 +810,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); } /** @@ -913,15 +828,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); } /** @@ -932,15 +847,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); } /** @@ -951,15 +866,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); } }