--- jsr166/src/test/tck/RecursiveTaskTest.java 2009/08/04 10:00:15 1.5 +++ 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,22 +84,21 @@ 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() { - public Integer compute() { - FibTask f = new FibTask(8); - Integer r = f.invoke(); - threadAssertTrue(r == 21); - threadAssertTrue(f.isDone()); - threadAssertFalse(f.isCancelled()); - threadAssertFalse(f.isCompletedAbnormally()); - threadAssertTrue(f.getRawResult() == 21); - return r; - } - }; - assertTrue(mainPool.invoke(a) == 21); + public Integer compute() { + FibTask f = new FibTask(8); + Integer r = f.invoke(); + threadAssertTrue(r == 21); + threadAssertTrue(f.isDone()); + threadAssertFalse(f.isCancelled()); + threadAssertFalse(f.isCompletedAbnormally()); + threadAssertTrue(f.getRawResult() == 21); + return r; + } + }; + assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } /** @@ -90,18 +108,18 @@ public class RecursiveTaskTest extends J */ public void testQuietlyInvoke() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - FibTask f = new FibTask(8); - f.quietlyInvoke(); - Integer r = f.getRawResult(); - threadAssertTrue(r == 21); - threadAssertTrue(f.isDone()); - threadAssertFalse(f.isCancelled()); - threadAssertFalse(f.isCompletedAbnormally()); - return r; - } - }; - assertTrue(mainPool.invoke(a) == 21); + public Integer compute() { + FibTask f = new FibTask(8); + f.quietlyInvoke(); + Integer r = f.getRawResult(); + threadAssertTrue(r == 21); + threadAssertTrue(f.isDone()); + threadAssertFalse(f.isCancelled()); + threadAssertFalse(f.isCompletedAbnormally()); + return r; + } + }; + assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } /** @@ -109,16 +127,16 @@ public class RecursiveTaskTest extends J */ public void testForkJoin() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - FibTask f = new FibTask(8); - f.fork(); - Integer r = f.join(); - threadAssertTrue(r == 21); - threadAssertTrue(f.isDone()); - return r; - } - }; - assertTrue(mainPool.invoke(a) == 21); + public Integer compute() { + FibTask f = new FibTask(8); + threadAssertSame(f, f.fork()); + Integer r = f.join(); + threadAssertTrue(r == 21); + threadAssertTrue(f.isDone()); + return r; + } + }; + assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } /** @@ -126,97 +144,61 @@ public class RecursiveTaskTest extends J */ public void testForkGet() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - try { - FibTask f = new FibTask(8); - f.fork(); - Integer r = f.get(); - threadAssertTrue(r == 21); - threadAssertTrue(f.isDone()); - return r; - } catch (Exception ex) { - unexpectedException(); - } - return NoResult; - } - }; - assertTrue(mainPool.invoke(a) == 21); - } - - /** - * timed get of a forked task returns when task completes - */ - public void testForkTimedGet() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { - try { - FibTask f = new FibTask(8); - f.fork(); - Integer r = f.get(5L, TimeUnit.SECONDS); - threadAssertTrue(r == 21); - threadAssertTrue(f.isDone()); - return r; - } catch (Exception ex) { - unexpectedException(); - } - 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() { + public Integer compute() { + try { FibTask f = new FibTask(8); - f.fork(); - Integer r = f.helpJoin(); + threadAssertSame(f, f.fork()); + Integer r = f.get(); threadAssertTrue(r == 21); threadAssertTrue(f.isDone()); return r; + } catch (Exception ex) { + unexpectedException(ex); } - }; - assertTrue(mainPool.invoke(a) == 21); + return NoResult; + } + }; + assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } /** - * quietlyJoin of a forked task returns when task completes + * timed get of a forked task returns when task completes */ - public void testForkQuietlyJoin() { + public void testForkTimedGet() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { + public Integer compute() { + try { FibTask f = new FibTask(8); - f.fork(); - f.quietlyJoin(); - Integer r = f.getRawResult(); + threadAssertSame(f, f.fork()); + Integer r = f.get(5L, TimeUnit.SECONDS); threadAssertTrue(r == 21); threadAssertTrue(f.isDone()); return r; + } catch (Exception ex) { + unexpectedException(ex); } - }; - assertTrue(mainPool.invoke(a) == 21); + return NoResult; + } + }; + assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } - /** - * quietlyHelpJoin of a forked task returns when task completes + * quietlyJoin of a forked task returns when task completes */ - public void testForkQuietlyHelpJoin() { + public void testForkQuietlyJoin() { 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); + public Integer compute() { + FibTask f = new FibTask(8); + threadAssertSame(f, f.fork()); + f.quietlyJoin(); + Integer r = f.getRawResult(); + threadAssertTrue(r == 21); + threadAssertTrue(f.isDone()); + return r; + } + }; + assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } @@ -226,18 +208,18 @@ public class RecursiveTaskTest extends J */ public void testForkHelpQuiesce() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - FibTask f = new FibTask(8); - f.fork(); - f.helpQuiesce(); - Integer r = f.getRawResult(); - threadAssertTrue(r == 21); - threadAssertTrue(f.isDone()); - threadAssertTrue(getQueuedTaskCount() == 0); - return r; - } - }; - assertTrue(mainPool.invoke(a) == 21); + public Integer compute() { + FibTask f = new FibTask(8); + threadAssertSame(f, f.fork()); + f.helpQuiesce(); + Integer r = f.getRawResult(); + threadAssertTrue(r == 21); + threadAssertTrue(f.isDone()); + threadAssertTrue(getQueuedTaskCount() == 0); + return r; + } + }; + assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } @@ -246,18 +228,18 @@ public class RecursiveTaskTest extends J */ public void testAbnormalInvoke() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - try { - FailingFibTask f = new FailingFibTask(8); - f.invoke(); - shouldThrow(); - return NoResult; - } catch (FJException success) { - } + public Integer compute() { + try { + FailingFibTask f = new FailingFibTask(8); + f.invoke(); + shouldThrow(); return NoResult; + } catch (FJException success) { } - }; - mainPool.invoke(a); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -265,14 +247,14 @@ public class RecursiveTaskTest extends J */ public void testAbnormalQuietlyInvoke() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - FailingFibTask f = new FailingFibTask(8); - f.quietlyInvoke(); - threadAssertTrue(f.isDone()); - return NoResult; - } - }; - mainPool.invoke(a); + public Integer compute() { + FailingFibTask f = new FailingFibTask(8); + f.quietlyInvoke(); + threadAssertTrue(f.isDone()); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -280,19 +262,19 @@ public class RecursiveTaskTest extends J */ public void testAbnormalForkJoin() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - try { - FailingFibTask f = new FailingFibTask(8); - f.fork(); - Integer r = f.join(); - shouldThrow(); - return r; - } catch (FJException success) { - } - return NoResult; + public Integer compute() { + try { + FailingFibTask f = new FailingFibTask(8); + threadAssertSame(f, f.fork()); + Integer r = f.join(); + shouldThrow(); + return r; + } catch (FJException success) { } - }; - mainPool.invoke(a); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -300,21 +282,21 @@ public class RecursiveTaskTest extends J */ public void testAbnormalForkGet() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - try { - FailingFibTask f = new FailingFibTask(8); - f.fork(); - Integer r = f.get(); - shouldThrow(); - return r; - } catch (ExecutionException success) { - } catch (Exception ex) { - unexpectedException(ex); - } - return NoResult; + public Integer compute() { + try { + FailingFibTask f = new FailingFibTask(8); + threadAssertSame(f, f.fork()); + Integer r = f.get(); + shouldThrow(); + return r; + } catch (ExecutionException success) { + } catch (Exception ex) { + unexpectedException(ex); } - }; - mainPool.invoke(a); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -322,63 +304,21 @@ public class RecursiveTaskTest extends J */ public void testAbnormalForkTimedGet() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - try { - FailingFibTask f = new FailingFibTask(8); - f.fork(); - Integer r = f.get(5L, TimeUnit.SECONDS); - shouldThrow(); - return r; - } catch (ExecutionException success) { - } catch (Exception ex) { - unexpectedException(ex); - } - 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() { + public Integer compute() { + try { 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; + threadAssertSame(f, f.fork()); + Integer r = f.get(5L, TimeUnit.SECONDS); + shouldThrow(); + return r; + } catch (ExecutionException success) { + } catch (Exception ex) { + unexpectedException(ex); } - }; - mainPool.invoke(a); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -386,17 +326,17 @@ public class RecursiveTaskTest extends J */ public void testAbnormalForkQuietlyJoin() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - FailingFibTask f = new FailingFibTask(8); - f.fork(); - f.quietlyJoin(); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.isCompletedAbnormally()); - threadAssertTrue(f.getException() instanceof FJException); - return NoResult; - } - }; - mainPool.invoke(a); + public Integer compute() { + FailingFibTask f = new FailingFibTask(8); + threadAssertSame(f, f.fork()); + f.quietlyJoin(); + threadAssertTrue(f.isDone()); + threadAssertTrue(f.isCompletedAbnormally()); + threadAssertTrue(f.getException() instanceof FJException); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -404,19 +344,19 @@ public class RecursiveTaskTest extends J */ public void testCancelledInvoke() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - try { - FibTask f = new FibTask(8); - f.cancel(true); - Integer r = f.invoke(); - shouldThrow(); - return r; - } catch (CancellationException success) { - } - return NoResult; + public Integer compute() { + try { + FibTask f = new FibTask(8); + threadAssertTrue(f.cancel(true)); + Integer r = f.invoke(); + shouldThrow(); + return r; + } catch (CancellationException success) { } - }; - mainPool.invoke(a); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -424,20 +364,20 @@ public class RecursiveTaskTest extends J */ public void testCancelledForkJoin() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - try { - FibTask f = new FibTask(8); - f.cancel(true); - f.fork(); - Integer r = f.join(); - shouldThrow(); - return r; - } catch (CancellationException success) { - } - return NoResult; + public Integer compute() { + try { + FibTask f = new FibTask(8); + threadAssertTrue(f.cancel(true)); + threadAssertSame(f, f.fork()); + Integer r = f.join(); + shouldThrow(); + return r; + } catch (CancellationException success) { } - }; - mainPool.invoke(a); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -445,22 +385,22 @@ public class RecursiveTaskTest extends J */ public void testCancelledForkGet() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - try { - FibTask f = new FibTask(8); - f.cancel(true); - f.fork(); - Integer r = f.get(); - shouldThrow(); - return r; - } catch (CancellationException success) { - } catch (Exception ex) { - unexpectedException(ex); - } - return NoResult; + public Integer compute() { + try { + FibTask f = new FibTask(8); + threadAssertTrue(f.cancel(true)); + threadAssertSame(f, f.fork()); + Integer r = f.get(); + shouldThrow(); + return r; + } catch (CancellationException success) { + } catch (Exception ex) { + unexpectedException(ex); } - }; - mainPool.invoke(a); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -468,66 +408,22 @@ public class RecursiveTaskTest extends J */ public void testCancelledForkTimedGet() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - try { - FibTask f = new FibTask(8); - f.cancel(true); - f.fork(); - Integer r = f.get(5L, TimeUnit.SECONDS); - shouldThrow(); - return r; - } catch (CancellationException success) { - } catch (Exception ex) { - unexpectedException(ex); - } - 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() { + public Integer compute() { + try { 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; + threadAssertTrue(f.cancel(true)); + threadAssertSame(f, f.fork()); + Integer r = f.get(5L, TimeUnit.SECONDS); + shouldThrow(); + return r; + } catch (CancellationException success) { + } catch (Exception ex) { + unexpectedException(ex); } - }; - mainPool.invoke(a); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -535,31 +431,32 @@ public class RecursiveTaskTest extends J */ public void testCancelledForkQuietlyJoin() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - FibTask f = new FibTask(8); - f.cancel(true); - f.fork(); - f.quietlyJoin(); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.isCompletedAbnormally()); - threadAssertTrue(f.getException() instanceof CancellationException); - return NoResult; - } - }; - mainPool.invoke(a); + public Integer compute() { + FibTask f = new FibTask(8); + threadAssertTrue(f.cancel(true)); + threadAssertSame(f, f.fork()); + f.quietlyJoin(); + threadAssertTrue(f.isDone()); + threadAssertTrue(f.isCompletedAbnormally()); + threadAssertTrue(f.getException() instanceof CancellationException); + return NoResult; + } + }; + 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); + public Integer compute() { + threadAssertTrue(getPool() == mainPool); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(mainPool, a)); } /** @@ -572,7 +469,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - a.invoke(); + assertSame(NoResult, a.invoke()); } /** @@ -580,12 +477,12 @@ public class RecursiveTaskTest extends J */ public void testInForkJoinPool() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - threadAssertTrue(inForkJoinPool()); - return NoResult; - } - }; - mainPool.invoke(a); + public Integer compute() { + threadAssertTrue(inForkJoinPool()); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -593,25 +490,26 @@ public class RecursiveTaskTest extends J */ public void testInForkJoinPool2() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - threadAssertTrue(!inForkJoinPool()); - return NoResult; - } - }; - a.invoke(); + public Integer compute() { + threadAssertTrue(!inForkJoinPool()); + return NoResult; + } + }; + 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); - return NoResult; - } - }; - assertEquals(a.invoke(), NoResult); + public Integer compute() { + setRawResult(NoResult); + threadAssertSame(getRawResult(), NoResult); + return NoResult; + } + }; + a.invoke(); } /** @@ -619,20 +517,20 @@ public class RecursiveTaskTest extends J */ public void testReinitialize() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - FibTask f = new FibTask(8); - Integer r = f.invoke(); - threadAssertTrue(r == 21); - threadAssertTrue(f.isDone()); - threadAssertFalse(f.isCancelled()); - threadAssertFalse(f.isCompletedAbnormally()); - f.reinitialize(); - r = f.invoke(); - threadAssertTrue(r == 21); - return NoResult; - } - }; - mainPool.invoke(a); + public Integer compute() { + FibTask f = new FibTask(8); + Integer r = f.invoke(); + threadAssertTrue(r == 21); + threadAssertTrue(f.isDone()); + threadAssertFalse(f.isCancelled()); + threadAssertFalse(f.isCompletedAbnormally()); + f.reinitialize(); + r = f.invoke(); + threadAssertTrue(r == 21); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -640,19 +538,19 @@ public class RecursiveTaskTest extends J */ public void testCompleteExceptionally() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - try { - FibTask f = new FibTask(8); - f.completeExceptionally(new FJException()); - Integer r = f.invoke(); - shouldThrow(); - return r; - } catch (FJException success) { - } - return NoResult; + public Integer compute() { + try { + FibTask f = new FibTask(8); + f.completeExceptionally(new FJException()); + Integer r = f.invoke(); + shouldThrow(); + return r; + } catch (FJException success) { } - }; - mainPool.invoke(a); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -660,16 +558,16 @@ public class RecursiveTaskTest extends J */ public void testComplete() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - FibTask f = new FibTask(8); - f.complete(NoResult); - Integer r = f.invoke(); - threadAssertTrue(f.isDone()); - threadAssertTrue(r == NoResult); - return r; - } - }; - mainPool.invoke(a); + public Integer compute() { + FibTask f = new FibTask(8); + f.complete(NoResult); + Integer r = f.invoke(); + threadAssertTrue(f.isDone()); + threadAssertTrue(r == NoResult); + return r; + } + }; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -677,18 +575,18 @@ public class RecursiveTaskTest extends J */ public void testInvokeAll2() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - FibTask f = new FibTask(8); - FibTask g = new FibTask(9); - invokeAll(f, g); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.join() == 21); - threadAssertTrue(g.isDone()); - threadAssertTrue(g.join() == 34); - return NoResult; - } - }; - mainPool.invoke(a); + public Integer compute() { + FibTask f = new FibTask(8); + FibTask g = new FibTask(9); + invokeAll(f, g); + threadAssertTrue(f.isDone()); + threadAssertTrue(f.join() == 21); + threadAssertTrue(g.isDone()); + threadAssertTrue(g.join() == 34); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -696,15 +594,15 @@ public class RecursiveTaskTest extends J */ public void testInvokeAll1() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - FibTask f = new FibTask(8); - invokeAll(f); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.join() == 21); - return NoResult; - } - }; - mainPool.invoke(a); + public Integer compute() { + FibTask f = new FibTask(8); + invokeAll(f); + threadAssertTrue(f.isDone()); + threadAssertTrue(f.join() == 21); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -712,21 +610,21 @@ public class RecursiveTaskTest extends J */ public void testInvokeAll3() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - FibTask f = new FibTask(8); - FibTask g = new FibTask(9); - FibTask h = new FibTask(7); - invokeAll(f, g, h); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.join() == 21); - threadAssertTrue(g.isDone()); - threadAssertTrue(g.join() == 34); - threadAssertTrue(h.isDone()); - threadAssertTrue(h.join() == 13); - return NoResult; - } - }; - mainPool.invoke(a); + public Integer compute() { + FibTask f = new FibTask(8); + FibTask g = new FibTask(9); + FibTask h = new FibTask(7); + invokeAll(f, g, h); + threadAssertTrue(f.isDone()); + threadAssertTrue(f.join() == 21); + threadAssertTrue(g.isDone()); + threadAssertTrue(g.join() == 34); + threadAssertTrue(h.isDone()); + threadAssertTrue(h.join() == 13); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -734,25 +632,25 @@ public class RecursiveTaskTest extends J */ public void testInvokeAllCollection() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - FibTask f = new FibTask(8); - FibTask g = new FibTask(9); - FibTask h = new FibTask(7); - HashSet set = new HashSet(); - set.add(f); - set.add(g); - set.add(h); - invokeAll(set); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.join() == 21); - threadAssertTrue(g.isDone()); - threadAssertTrue(g.join() == 34); - threadAssertTrue(h.isDone()); - threadAssertTrue(h.join() == 13); - return NoResult; - } - }; - mainPool.invoke(a); + public Integer compute() { + FibTask f = new FibTask(8); + FibTask g = new FibTask(9); + FibTask h = new FibTask(7); + HashSet set = new HashSet(); + set.add(f); + set.add(g); + set.add(h); + invokeAll(set); + threadAssertTrue(f.isDone()); + threadAssertTrue(f.join() == 21); + threadAssertTrue(g.isDone()); + threadAssertTrue(g.join() == 34); + threadAssertTrue(h.isDone()); + threadAssertTrue(h.join() == 13); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -761,19 +659,19 @@ public class RecursiveTaskTest extends J */ public void testAbnormalInvokeAll2() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - try { - FibTask f = new FibTask(8); - FailingFibTask g = new FailingFibTask(9); - invokeAll(f, g); - shouldThrow(); - return NoResult; - } catch (FJException success) { - } + public Integer compute() { + try { + FibTask f = new FibTask(8); + FailingFibTask g = new FailingFibTask(9); + invokeAll(f, g); + shouldThrow(); return NoResult; + } catch (FJException success) { } - }; - mainPool.invoke(a); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -781,18 +679,18 @@ public class RecursiveTaskTest extends J */ public void testAbnormalInvokeAll1() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - try { - FailingFibTask g = new FailingFibTask(9); - invokeAll(g); - shouldThrow(); - return NoResult; - } catch (FJException success) { - } + public Integer compute() { + try { + FailingFibTask g = new FailingFibTask(9); + invokeAll(g); + shouldThrow(); return NoResult; + } catch (FJException success) { } - }; - mainPool.invoke(a); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -800,20 +698,20 @@ public class RecursiveTaskTest extends J */ public void testAbnormalInvokeAll3() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - try { - FibTask f = new FibTask(8); - FailingFibTask g = new FailingFibTask(9); - FibTask h = new FibTask(7); - invokeAll(f, g, h); - shouldThrow(); - return NoResult; - } catch (FJException success) { - } + public Integer compute() { + try { + FibTask f = new FibTask(8); + FailingFibTask g = new FailingFibTask(9); + FibTask h = new FibTask(7); + invokeAll(f, g, h); + shouldThrow(); return NoResult; + } catch (FJException success) { } - }; - mainPool.invoke(a); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -821,24 +719,24 @@ public class RecursiveTaskTest extends J */ public void testAbnormalInvokeAllCollection() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - try { - FailingFibTask f = new FailingFibTask(8); - FibTask g = new FibTask(9); - FibTask h = new FibTask(7); - HashSet set = new HashSet(); - set.add(f); - set.add(g); - set.add(h); - invokeAll(set); - shouldThrow(); - return NoResult; - } catch (FJException success) { - } + public Integer compute() { + try { + FailingFibTask f = new FailingFibTask(8); + FibTask g = new FibTask(9); + FibTask h = new FibTask(7); + HashSet set = new HashSet(); + set.add(f); + set.add(g); + set.add(h); + invokeAll(set); + shouldThrow(); return NoResult; + } catch (FJException success) { } - }; - mainPool.invoke(a); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -847,19 +745,19 @@ public class RecursiveTaskTest extends J */ public void testTryUnfork() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - FibTask g = new FibTask(9); - g.fork(); - FibTask f = new FibTask(8); - f.fork(); - threadAssertTrue(f.tryUnfork()); - helpQuiesce(); - threadAssertFalse(f.isDone()); - threadAssertTrue(g.isDone()); - return NoResult; - } - }; - singletonPool.invoke(a); + public Integer compute() { + FibTask g = new FibTask(9); + threadAssertSame(g, g.fork()); + FibTask f = new FibTask(8); + threadAssertSame(f, f.fork()); + threadAssertTrue(f.tryUnfork()); + helpQuiesce(); + threadAssertFalse(f.isDone()); + threadAssertTrue(g.isDone()); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); } /** @@ -868,19 +766,19 @@ public class RecursiveTaskTest extends J */ public void testGetSurplusQueuedTaskCount() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - FibTask h = new FibTask(7); - h.fork(); - FibTask g = new FibTask(9); - g.fork(); - FibTask f = new FibTask(8); - f.fork(); - threadAssertTrue(getSurplusQueuedTaskCount() > 0); - helpQuiesce(); - return NoResult; - } - }; - singletonPool.invoke(a); + public Integer compute() { + FibTask h = new FibTask(7); + threadAssertSame(h, h.fork()); + FibTask g = new FibTask(9); + threadAssertSame(g, g.fork()); + FibTask f = new FibTask(8); + threadAssertSame(f, f.fork()); + threadAssertTrue(getSurplusQueuedTaskCount() > 0); + helpQuiesce(); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); } /** @@ -888,19 +786,19 @@ public class RecursiveTaskTest extends J */ public void testPeekNextLocalTask() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - FibTask g = new FibTask(9); - g.fork(); - FibTask f = new FibTask(8); - f.fork(); - threadAssertTrue(peekNextLocalTask() == f); - f.join(); - threadAssertTrue(f.isDone()); - helpQuiesce(); - return NoResult; - } - }; - singletonPool.invoke(a); + public Integer compute() { + FibTask g = new FibTask(9); + threadAssertSame(g, g.fork()); + FibTask f = new FibTask(8); + threadAssertSame(f, f.fork()); + threadAssertTrue(peekNextLocalTask() == f); + threadAssertTrue(f.join() == 21); + threadAssertTrue(f.isDone()); + helpQuiesce(); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); } /** @@ -909,39 +807,38 @@ public class RecursiveTaskTest extends J */ public void testPollNextLocalTask() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - FibTask g = new FibTask(9); - g.fork(); - FibTask f = new FibTask(8); - f.fork(); - threadAssertTrue(pollNextLocalTask() == f); - helpQuiesce(); - threadAssertFalse(f.isDone()); - return NoResult; - } - }; - singletonPool.invoke(a); + public Integer compute() { + FibTask g = new FibTask(9); + threadAssertSame(g, g.fork()); + FibTask f = new FibTask(8); + threadAssertSame(f, f.fork()); + threadAssertTrue(pollNextLocalTask() == f); + helpQuiesce(); + threadAssertFalse(f.isDone()); + return NoResult; + } + }; + 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(); - FibTask f = new FibTask(8); - f.fork(); - threadAssertTrue(pollTask() == f); - helpQuiesce(); - threadAssertFalse(f.isDone()); - threadAssertTrue(g.isDone()); - return NoResult; - } - }; - singletonPool.invoke(a); + public Integer compute() { + FibTask g = new FibTask(9); + threadAssertSame(g, g.fork()); + FibTask f = new FibTask(8); + threadAssertSame(f, f.fork()); + threadAssertTrue(pollTask() == f); + helpQuiesce(); + threadAssertFalse(f.isDone()); + threadAssertTrue(g.isDone()); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); } /** @@ -949,19 +846,19 @@ public class RecursiveTaskTest extends J */ public void testPeekNextLocalTaskAsync() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - FibTask g = new FibTask(9); - g.fork(); - FibTask f = new FibTask(8); - f.fork(); - threadAssertTrue(peekNextLocalTask() == g); - f.join(); - helpQuiesce(); - threadAssertTrue(f.isDone()); - return NoResult; - } - }; - asyncSingletonPool.invoke(a); + public Integer compute() { + FibTask g = new FibTask(9); + threadAssertSame(g, g.fork()); + FibTask f = new FibTask(8); + threadAssertSame(f, f.fork()); + threadAssertTrue(peekNextLocalTask() == g); + threadAssertEquals(21, (int) f.join()); + helpQuiesce(); + threadAssertTrue(f.isDone()); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); } /** @@ -970,19 +867,19 @@ public class RecursiveTaskTest extends J */ public void testPollNextLocalTaskAsync() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - FibTask g = new FibTask(9); - g.fork(); - FibTask f = new FibTask(8); - f.fork(); - threadAssertTrue(pollNextLocalTask() == g); - helpQuiesce(); - threadAssertTrue(f.isDone()); - threadAssertFalse(g.isDone()); - return NoResult; - } - }; - asyncSingletonPool.invoke(a); + public Integer compute() { + FibTask g = new FibTask(9); + threadAssertSame(g, g.fork()); + FibTask f = new FibTask(8); + threadAssertSame(f, f.fork()); + threadAssertTrue(pollNextLocalTask() == g); + helpQuiesce(); + threadAssertTrue(f.isDone()); + threadAssertFalse(g.isDone()); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); } /** @@ -991,19 +888,19 @@ public class RecursiveTaskTest extends J */ public void testPollTaskAsync() { RecursiveTask a = new RecursiveTask() { - public Integer compute() { - FibTask g = new FibTask(9); - g.fork(); - FibTask f = new FibTask(8); - f.fork(); - threadAssertTrue(pollTask() == g); - helpQuiesce(); - threadAssertTrue(f.isDone()); - threadAssertFalse(g.isDone()); - return NoResult; - } - }; - asyncSingletonPool.invoke(a); + public Integer compute() { + FibTask g = new FibTask(9); + threadAssertSame(g, g.fork()); + FibTask f = new FibTask(8); + threadAssertSame(f, f.fork()); + threadAssertTrue(pollTask() == g); + helpQuiesce(); + threadAssertTrue(f.isDone()); + threadAssertFalse(g.isDone()); + return NoResult; + } + }; + assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); } }