--- jsr166/src/test/tck/RecursiveTaskTest.java 2009/08/05 01:17:31 1.7 +++ jsr166/src/test/tck/RecursiveTaskTest.java 2010/09/14 10:24:07 1.17 @@ -3,25 +3,44 @@ * 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.RecursiveTask; +import java.util.concurrent.TimeUnit; +import java.util.HashSet; public class RecursiveTaskTest 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(RecursiveTaskTest.class); + return new TestSuite(RecursiveTaskTest.class); + } + + private static ForkJoinPool mainPool() { + return new ForkJoinPool(); } - 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 singletonPool() { + return new ForkJoinPool(1); + } + + private static ForkJoinPool asyncSingletonPool() { + return new ForkJoinPool(1, + ForkJoinPool.defaultForkJoinWorkerThreadFactory, + null, true); + } + + private T testInvokeOnPool(ForkJoinPool pool, RecursiveTask a) { + try { + return pool.invoke(a); + } finally { + joinPool(pool); + } } static final class FJException extends RuntimeException { @@ -65,7 +84,6 @@ public class RecursiveTaskTest extends J * isCompletedAbnormally and isCancelled return false for normally * completed tasks. getRawResult of a completed non-null task * returns value; - * */ public void testInvoke() { RecursiveTask a = new RecursiveTask() { @@ -80,7 +98,7 @@ public class RecursiveTaskTest extends J return r; } }; - assertTrue(mainPool.invoke(a) == 21); + assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } /** @@ -101,7 +119,7 @@ public class RecursiveTaskTest extends J return r; } }; - assertTrue(mainPool.invoke(a) == 21); + assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } /** @@ -111,14 +129,14 @@ public class RecursiveTaskTest extends J RecursiveTask a = new RecursiveTask() { public Integer compute() { FibTask f = new FibTask(8); - f.fork(); + threadAssertSame(f, f.fork()); Integer r = f.join(); threadAssertTrue(r == 21); threadAssertTrue(f.isDone()); return r; } }; - assertTrue(mainPool.invoke(a) == 21); + assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } /** @@ -129,7 +147,7 @@ public class RecursiveTaskTest extends J public Integer compute() { try { FibTask f = new FibTask(8); - f.fork(); + threadAssertSame(f, f.fork()); Integer r = f.get(); threadAssertTrue(r == 21); threadAssertTrue(f.isDone()); @@ -140,7 +158,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - assertTrue(mainPool.invoke(a) == 21); + assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } /** @@ -151,7 +169,7 @@ public class RecursiveTaskTest extends J public Integer compute() { try { FibTask f = new FibTask(8); - f.fork(); + threadAssertSame(f, f.fork()); Integer r = f.get(5L, TimeUnit.SECONDS); threadAssertTrue(r == 21); threadAssertTrue(f.isDone()); @@ -162,24 +180,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - assertTrue(mainPool.invoke(a) == 21); - } - - /** - * helpJoin of a forked task returns when task completes - */ - public void testForkHelpJoin() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { - FibTask f = new FibTask(8); - f.fork(); - Integer r = f.helpJoin(); - threadAssertTrue(r == 21); - threadAssertTrue(f.isDone()); - return r; - } - }; - assertTrue(mainPool.invoke(a) == 21); + assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } /** @@ -189,7 +190,7 @@ public class RecursiveTaskTest extends J RecursiveTask a = new RecursiveTask() { public Integer compute() { FibTask f = new FibTask(8); - f.fork(); + threadAssertSame(f, f.fork()); f.quietlyJoin(); Integer r = f.getRawResult(); threadAssertTrue(r == 21); @@ -197,26 +198,7 @@ public class RecursiveTaskTest extends J return r; } }; - assertTrue(mainPool.invoke(a) == 21); - } - - - /** - * quietlyHelpJoin of a forked task returns when task completes - */ - public void testForkQuietlyHelpJoin() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { - FibTask f = new FibTask(8); - f.fork(); - f.quietlyHelpJoin(); - Integer r = f.getRawResult(); - threadAssertTrue(r == 21); - threadAssertTrue(f.isDone()); - return r; - } - }; - assertTrue(mainPool.invoke(a) == 21); + assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } @@ -228,7 +210,7 @@ public class RecursiveTaskTest extends J RecursiveTask a = new RecursiveTask() { public Integer compute() { FibTask f = new FibTask(8); - f.fork(); + threadAssertSame(f, f.fork()); f.helpQuiesce(); Integer r = f.getRawResult(); threadAssertTrue(r == 21); @@ -237,7 +219,7 @@ public class RecursiveTaskTest extends J return r; } }; - assertTrue(mainPool.invoke(a) == 21); + assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } @@ -257,7 +239,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - mainPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -272,7 +254,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - mainPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -283,7 +265,7 @@ public class RecursiveTaskTest extends J public Integer compute() { try { FailingFibTask f = new FailingFibTask(8); - f.fork(); + threadAssertSame(f, f.fork()); Integer r = f.join(); shouldThrow(); return r; @@ -292,7 +274,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - mainPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -303,7 +285,7 @@ public class RecursiveTaskTest extends J public Integer compute() { try { FailingFibTask f = new FailingFibTask(8); - f.fork(); + threadAssertSame(f, f.fork()); Integer r = f.get(); shouldThrow(); return r; @@ -314,7 +296,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - mainPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -325,7 +307,7 @@ public class RecursiveTaskTest extends J public Integer compute() { try { FailingFibTask f = new FailingFibTask(8); - f.fork(); + threadAssertSame(f, f.fork()); Integer r = f.get(5L, TimeUnit.SECONDS); shouldThrow(); return r; @@ -336,49 +318,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - mainPool.invoke(a); - } - - /** - * join of a forked task throws exception when task completes abnormally - */ - public void testAbnormalForkHelpJoin() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { - try { - FailingFibTask f = new FailingFibTask(8); - f.fork(); - Integer r = f.helpJoin(); - shouldThrow(); - return r; - } catch (FJException success) { - } - return NoResult; - } - }; - 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() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { - FailingFibTask f = new FailingFibTask(8); - f.fork(); - f.quietlyHelpJoin(); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.isCompletedAbnormally()); - threadAssertFalse(f.isCancelled()); - threadAssertTrue(f.getException() instanceof FJException); - return NoResult; - } - }; - mainPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -388,7 +328,7 @@ public class RecursiveTaskTest extends J RecursiveTask a = new RecursiveTask() { public Integer compute() { FailingFibTask f = new FailingFibTask(8); - f.fork(); + threadAssertSame(f, f.fork()); f.quietlyJoin(); threadAssertTrue(f.isDone()); threadAssertTrue(f.isCompletedAbnormally()); @@ -396,7 +336,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - mainPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -407,7 +347,7 @@ public class RecursiveTaskTest extends J public Integer compute() { try { FibTask f = new FibTask(8); - f.cancel(true); + threadAssertTrue(f.cancel(true)); Integer r = f.invoke(); shouldThrow(); return r; @@ -416,7 +356,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - mainPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -427,8 +367,8 @@ public class RecursiveTaskTest extends J public Integer compute() { try { FibTask f = new FibTask(8); - f.cancel(true); - f.fork(); + threadAssertTrue(f.cancel(true)); + threadAssertSame(f, f.fork()); Integer r = f.join(); shouldThrow(); return r; @@ -437,7 +377,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - mainPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -448,8 +388,8 @@ public class RecursiveTaskTest extends J public Integer compute() { try { FibTask f = new FibTask(8); - f.cancel(true); - f.fork(); + threadAssertTrue(f.cancel(true)); + threadAssertSame(f, f.fork()); Integer r = f.get(); shouldThrow(); return r; @@ -460,7 +400,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - mainPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -471,8 +411,8 @@ public class RecursiveTaskTest extends J public Integer compute() { try { FibTask f = new FibTask(8); - f.cancel(true); - f.fork(); + threadAssertTrue(f.cancel(true)); + threadAssertSame(f, f.fork()); Integer r = f.get(5L, TimeUnit.SECONDS); shouldThrow(); return r; @@ -483,51 +423,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - mainPool.invoke(a); - } - - /** - * join of a forked task throws exception when task cancelled - */ - public void testCancelledForkHelpJoin() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { - try { - FibTask f = new FibTask(8); - f.cancel(true); - f.fork(); - Integer r = f.helpJoin(); - shouldThrow(); - return r; - } catch (CancellationException success) { - } - return NoResult; - } - }; - 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() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { - FibTask f = new FibTask(8); - f.cancel(true); - f.fork(); - f.quietlyHelpJoin(); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.isCompletedAbnormally()); - threadAssertTrue(f.isCancelled()); - threadAssertTrue(f.getException() instanceof CancellationException); - return NoResult; - } - }; - mainPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -537,8 +433,8 @@ public class RecursiveTaskTest extends J RecursiveTask a = new RecursiveTask() { public Integer compute() { FibTask f = new FibTask(8); - f.cancel(true); - f.fork(); + threadAssertTrue(f.cancel(true)); + threadAssertSame(f, f.fork()); f.quietlyJoin(); threadAssertTrue(f.isDone()); threadAssertTrue(f.isCompletedAbnormally()); @@ -546,20 +442,21 @@ public class RecursiveTaskTest extends J return NoResult; } }; - mainPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** * getPool of executing task returns its pool */ public void testGetPool() { + final ForkJoinPool mainPool = mainPool(); RecursiveTask a = new RecursiveTask() { public Integer compute() { threadAssertTrue(getPool() == mainPool); return NoResult; } }; - mainPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(mainPool, a)); } /** @@ -572,7 +469,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - a.invoke(); + assertSame(NoResult, a.invoke()); } /** @@ -585,7 +482,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - mainPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -598,20 +495,21 @@ public class RecursiveTaskTest extends J return NoResult; } }; - a.invoke(); + assertSame(NoResult, a.invoke()); } /** - * setRawResult(null) succeeds + * The value set by setRawResult is returned by getRawResult */ public void testSetRawResult() { RecursiveTask a = new RecursiveTask() { public Integer compute() { setRawResult(NoResult); + threadAssertSame(getRawResult(), NoResult); return NoResult; } }; - assertEquals(a.invoke(), NoResult); + a.invoke(); } /** @@ -632,7 +530,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - mainPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -652,7 +550,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - mainPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -669,7 +567,7 @@ public class RecursiveTaskTest extends J return r; } }; - mainPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -688,7 +586,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - mainPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -704,7 +602,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - mainPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -726,7 +624,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - mainPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -752,7 +650,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - mainPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -773,7 +671,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - mainPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -792,7 +690,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - mainPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -813,7 +711,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - mainPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -838,7 +736,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - mainPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -849,9 +747,9 @@ public class RecursiveTaskTest extends J RecursiveTask a = new RecursiveTask() { public Integer compute() { FibTask g = new FibTask(9); - g.fork(); + threadAssertSame(g, g.fork()); FibTask f = new FibTask(8); - f.fork(); + threadAssertSame(f, f.fork()); threadAssertTrue(f.tryUnfork()); helpQuiesce(); threadAssertFalse(f.isDone()); @@ -859,7 +757,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - singletonPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); } /** @@ -870,17 +768,17 @@ public class RecursiveTaskTest extends J RecursiveTask a = new RecursiveTask() { public Integer compute() { FibTask h = new FibTask(7); - h.fork(); + threadAssertSame(h, h.fork()); FibTask g = new FibTask(9); - g.fork(); + threadAssertSame(g, g.fork()); FibTask f = new FibTask(8); - f.fork(); + threadAssertSame(f, f.fork()); threadAssertTrue(getSurplusQueuedTaskCount() > 0); helpQuiesce(); return NoResult; } }; - singletonPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); } /** @@ -890,17 +788,17 @@ public class RecursiveTaskTest extends J RecursiveTask a = new RecursiveTask() { public Integer compute() { FibTask g = new FibTask(9); - g.fork(); + threadAssertSame(g, g.fork()); FibTask f = new FibTask(8); - f.fork(); + threadAssertSame(f, f.fork()); threadAssertTrue(peekNextLocalTask() == f); - f.join(); + threadAssertTrue(f.join() == 21); threadAssertTrue(f.isDone()); helpQuiesce(); return NoResult; } }; - singletonPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); } /** @@ -911,29 +809,28 @@ public class RecursiveTaskTest extends J RecursiveTask a = new RecursiveTask() { public Integer compute() { FibTask g = new FibTask(9); - g.fork(); + threadAssertSame(g, g.fork()); FibTask f = new FibTask(8); - f.fork(); + threadAssertSame(f, f.fork()); threadAssertTrue(pollNextLocalTask() == f); helpQuiesce(); threadAssertFalse(f.isDone()); return NoResult; } }; - singletonPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); } /** - * pollTask returns an unexecuted task - * without executing it + * pollTask returns an unexecuted task without executing it */ public void testPollTask() { RecursiveTask a = new RecursiveTask() { public Integer compute() { FibTask g = new FibTask(9); - g.fork(); + threadAssertSame(g, g.fork()); FibTask f = new FibTask(8); - f.fork(); + threadAssertSame(f, f.fork()); threadAssertTrue(pollTask() == f); helpQuiesce(); threadAssertFalse(f.isDone()); @@ -941,7 +838,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - singletonPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); } /** @@ -951,17 +848,17 @@ public class RecursiveTaskTest extends J RecursiveTask a = new RecursiveTask() { public Integer compute() { FibTask g = new FibTask(9); - g.fork(); + threadAssertSame(g, g.fork()); FibTask f = new FibTask(8); - f.fork(); + threadAssertSame(f, f.fork()); threadAssertTrue(peekNextLocalTask() == g); - f.join(); + threadAssertEquals(21, (int) f.join()); helpQuiesce(); threadAssertTrue(f.isDone()); return NoResult; } }; - asyncSingletonPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); } /** @@ -972,9 +869,9 @@ public class RecursiveTaskTest extends J RecursiveTask a = new RecursiveTask() { public Integer compute() { FibTask g = new FibTask(9); - g.fork(); + threadAssertSame(g, g.fork()); FibTask f = new FibTask(8); - f.fork(); + threadAssertSame(f, f.fork()); threadAssertTrue(pollNextLocalTask() == g); helpQuiesce(); threadAssertTrue(f.isDone()); @@ -982,7 +879,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - asyncSingletonPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); } /** @@ -993,9 +890,9 @@ public class RecursiveTaskTest extends J RecursiveTask a = new RecursiveTask() { public Integer compute() { FibTask g = new FibTask(9); - g.fork(); + threadAssertSame(g, g.fork()); FibTask f = new FibTask(8); - f.fork(); + threadAssertSame(f, f.fork()); threadAssertTrue(pollTask() == g); helpQuiesce(); threadAssertTrue(f.isDone()); @@ -1003,7 +900,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - asyncSingletonPool.invoke(a); + assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); } }