--- jsr166/src/test/tck/RecursiveTaskTest.java 2009/11/21 02:07:27 1.8 +++ jsr166/src/test/tck/RecursiveTaskTest.java 2010/09/16 00:52:49 1.19 @@ -3,25 +3,56 @@ * 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); } - 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 T testInvokeOnPool(ForkJoinPool pool, RecursiveTask a) { + try { + assertFalse(a.isDone()); + assertFalse(a.isCompletedNormally()); + assertFalse(a.isCompletedAbnormally()); + assertFalse(a.isCancelled()); + + T result = pool.invoke(a); + + assertTrue(a.isDone()); + assertTrue(a.isCompletedNormally()); + assertFalse(a.isCompletedAbnormally()); + assertFalse(a.isCancelled()); + assertNull(a.getException()); + return result; + } finally { + joinPool(pool); + } } static final class FJException extends RuntimeException { @@ -32,10 +63,10 @@ public class RecursiveTaskTest extends J static final Integer NoResult = Integer.valueOf(-17); // A simple recursive task for testing - static final class FibTask extends RecursiveTask { + final class FibTask extends CheckedRecursiveTask { final int number; FibTask(int n) { number = n; } - public Integer compute() { + public Integer realCompute() { int n = number; if (n <= 1) return n; @@ -46,7 +77,7 @@ public class RecursiveTaskTest extends J } // A recursive action failing in base case - static final class FailingFibTask extends RecursiveTask { + final class FailingFibTask extends RecursiveTask { final int number; int result; FailingFibTask(int n) { number = n; } @@ -65,22 +96,20 @@ 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() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { 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); + assertEquals(21, (int) r); + assertTrue(f.isDone()); + assertFalse(f.isCancelled()); + assertFalse(f.isCompletedAbnormally()); + assertEquals(21, (int) f.getRawResult()); return r; - } - }; - assertTrue(mainPool.invoke(a) == 21); + }}; + assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } /** @@ -89,134 +118,83 @@ public class RecursiveTaskTest extends J * completed tasks */ public void testQuietlyInvoke() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { FibTask f = new FibTask(8); f.quietlyInvoke(); Integer r = f.getRawResult(); - threadAssertTrue(r == 21); - threadAssertTrue(f.isDone()); - threadAssertFalse(f.isCancelled()); - threadAssertFalse(f.isCompletedAbnormally()); + assertEquals(21, (int) r); + assertTrue(f.isDone()); + assertFalse(f.isCancelled()); + assertFalse(f.isCompletedAbnormally()); return r; - } - }; - assertTrue(mainPool.invoke(a) == 21); + }}; + assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } /** * join of a forked task returns when task completes */ public void testForkJoin() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { FibTask f = new FibTask(8); - f.fork(); + assertSame(f, f.fork()); Integer r = f.join(); - threadAssertTrue(r == 21); - threadAssertTrue(f.isDone()); + assertEquals(21, (int) r); + assertTrue(f.isDone()); return r; - } - }; - assertTrue(mainPool.invoke(a) == 21); + }}; + assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } /** * get of a forked task returns when task completes */ 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(ex); - } - return NoResult; - } - }; - assertTrue(mainPool.invoke(a) == 21); + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() throws Exception { + FibTask f = new FibTask(8); + assertSame(f, f.fork()); + Integer r = f.get(); + assertEquals(21, (int) r); + assertTrue(f.isDone()); + return r; + }}; + assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } /** * 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(ex); - } - 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() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() throws Exception { FibTask f = new FibTask(8); - f.fork(); - Integer r = f.helpJoin(); - threadAssertTrue(r == 21); - threadAssertTrue(f.isDone()); + assertSame(f, f.fork()); + Integer r = f.get(5L, TimeUnit.SECONDS); + assertEquals(21, (int) r); + assertTrue(f.isDone()); return r; - } - }; - assertTrue(mainPool.invoke(a) == 21); + }}; + assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } /** * quietlyJoin of a forked task returns when task completes */ public void testForkQuietlyJoin() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { FibTask f = new FibTask(8); - f.fork(); + assertSame(f, f.fork()); f.quietlyJoin(); Integer r = f.getRawResult(); - threadAssertTrue(r == 21); - threadAssertTrue(f.isDone()); + assertEquals(21, (int) r); + assertTrue(f.isDone()); 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)); } @@ -225,19 +203,18 @@ public class RecursiveTaskTest extends J * getQueuedTaskCount returns 0 when quiescent */ public void testForkHelpQuiesce() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { FibTask f = new FibTask(8); - f.fork(); + assertSame(f, f.fork()); f.helpQuiesce(); Integer r = f.getRawResult(); - threadAssertTrue(r == 21); - threadAssertTrue(f.isDone()); - threadAssertTrue(getQueuedTaskCount() == 0); + assertEquals(21, (int) r); + assertTrue(f.isDone()); + assertEquals(0, getQueuedTaskCount()); return r; - } - }; - assertTrue(mainPool.invoke(a) == 21); + }}; + assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } @@ -245,496 +222,375 @@ public class RecursiveTaskTest extends J * invoke task throws exception when task completes abnormally */ public void testAbnormalInvoke() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { + FailingFibTask f = new FailingFibTask(8); try { - FailingFibTask f = new FailingFibTask(8); f.invoke(); shouldThrow(); return NoResult; - } catch (FJException success) { - } + } catch (FJException success) {} return NoResult; - } - }; - mainPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** * quietlyInvoke task returns when task completes abnormally */ public void testAbnormalQuietlyInvoke() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { FailingFibTask f = new FailingFibTask(8); f.quietlyInvoke(); - threadAssertTrue(f.isDone()); + assertTrue(f.isDone()); return NoResult; - } - }; - mainPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** * join of a forked task throws exception when task completes abnormally */ public void testAbnormalForkJoin() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { + FailingFibTask f = new FailingFibTask(8); + assertSame(f, f.fork()); try { - FailingFibTask f = new FailingFibTask(8); - f.fork(); Integer r = f.join(); shouldThrow(); return r; - } catch (FJException success) { - } + } catch (FJException success) {} return NoResult; - } - }; - mainPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** * get of a forked task throws exception when task completes abnormally */ public void testAbnormalForkGet() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() throws Exception { + FailingFibTask f = new FailingFibTask(8); + assertSame(f, f.fork()); try { - FailingFibTask f = new FailingFibTask(8); - f.fork(); Integer r = f.get(); shouldThrow(); return r; - } catch (ExecutionException success) { - } catch (Exception ex) { - unexpectedException(ex); - } + } catch (ExecutionException success) {} return NoResult; - } - }; - mainPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** * timed get of a forked task throws exception when task completes abnormally */ public void testAbnormalForkTimedGet() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() throws Exception { + FailingFibTask f = new FailingFibTask(8); + assertSame(f, f.fork()); 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() { - FailingFibTask f = new FailingFibTask(8); - f.fork(); - f.quietlyHelpJoin(); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.isCompletedAbnormally()); - threadAssertFalse(f.isCancelled()); - threadAssertTrue(f.getException() instanceof FJException); + } catch (ExecutionException success) {} return NoResult; - } - }; - mainPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** * quietlyJoin of a forked task returns when task completes abnormally */ public void testAbnormalForkQuietlyJoin() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { FailingFibTask f = new FailingFibTask(8); - f.fork(); + assertSame(f, f.fork()); f.quietlyJoin(); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.isCompletedAbnormally()); - threadAssertTrue(f.getException() instanceof FJException); + assertTrue(f.isDone()); + assertTrue(f.isCompletedAbnormally()); + assertTrue(f.getException() instanceof FJException); return NoResult; - } - }; - mainPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** * invoke task throws exception when task cancelled */ public void testCancelledInvoke() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { + FibTask f = new FibTask(8); + assertTrue(f.cancel(true)); try { - FibTask f = new FibTask(8); - f.cancel(true); Integer r = f.invoke(); shouldThrow(); return r; - } catch (CancellationException success) { - } + } catch (CancellationException success) {} return NoResult; - } - }; - mainPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** * join of a forked task throws exception when task cancelled */ public void testCancelledForkJoin() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { + FibTask f = new FibTask(8); + assertTrue(f.cancel(true)); + assertSame(f, f.fork()); try { - FibTask f = new FibTask(8); - f.cancel(true); - f.fork(); Integer r = f.join(); shouldThrow(); return r; - } catch (CancellationException success) { - } + } catch (CancellationException success) {} return NoResult; - } - }; - mainPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** * get of a forked task throws exception when task cancelled */ public void testCancelledForkGet() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() throws Exception { + FibTask f = new FibTask(8); + assertTrue(f.cancel(true)); + assertSame(f, f.fork()); 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); - } + } catch (CancellationException success) {} return NoResult; - } - }; - mainPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** * timed get of a forked task throws exception when task cancelled */ public void testCancelledForkTimedGet() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() throws Exception { + FibTask f = new FibTask(8); + assertTrue(f.cancel(true)); + assertSame(f, f.fork()); 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); - } + } catch (CancellationException success) {} 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)); } /** * quietlyJoin of a forked task returns when task cancelled */ public void testCancelledForkQuietlyJoin() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { FibTask f = new FibTask(8); - f.cancel(true); - f.fork(); + assertTrue(f.cancel(true)); + assertSame(f, f.fork()); f.quietlyJoin(); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.isCompletedAbnormally()); - threadAssertTrue(f.getException() instanceof CancellationException); + assertTrue(f.isDone()); + assertTrue(f.isCompletedAbnormally()); + assertTrue(f.isCancelled()); + assertTrue(f.getException() instanceof CancellationException); return NoResult; - } - }; - mainPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** * getPool of executing task returns its pool */ public void testGetPool() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { - threadAssertTrue(getPool() == mainPool); + final ForkJoinPool mainPool = mainPool(); + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { + assertSame(mainPool, getPool()); return NoResult; - } - }; - mainPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(mainPool, a)); } /** * getPool of non-FJ task returns null */ public void testGetPool2() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { - threadAssertTrue(getPool() == null); + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { + assertNull(getPool()); return NoResult; - } - }; - a.invoke(); + }}; + assertSame(NoResult, a.invoke()); } /** * inForkJoinPool of executing task returns true */ public void testInForkJoinPool() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { - threadAssertTrue(inForkJoinPool()); + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { + assertTrue(inForkJoinPool()); return NoResult; - } - }; - mainPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** * inForkJoinPool of non-FJ task returns false */ public void testInForkJoinPool2() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { - threadAssertTrue(!inForkJoinPool()); + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { + assertTrue(!inForkJoinPool()); 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() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { setRawResult(NoResult); + assertSame(NoResult, getRawResult()); return NoResult; } }; - assertEquals(a.invoke(), NoResult); + a.invoke(); } /** * A reinitialized task may be re-invoked */ public void testReinitialize() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { FibTask f = new FibTask(8); Integer r = f.invoke(); - threadAssertTrue(r == 21); - threadAssertTrue(f.isDone()); - threadAssertFalse(f.isCancelled()); - threadAssertFalse(f.isCompletedAbnormally()); + assertEquals(21, (int) r); + assertTrue(f.isDone()); + assertFalse(f.isCancelled()); + assertFalse(f.isCompletedAbnormally()); f.reinitialize(); r = f.invoke(); - threadAssertTrue(r == 21); + assertEquals(21, (int) r); return NoResult; - } - }; - mainPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** * invoke task throws exception after invoking completeExceptionally */ public void testCompleteExceptionally() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { + FibTask f = new FibTask(8); + f.completeExceptionally(new FJException()); try { - FibTask f = new FibTask(8); - f.completeExceptionally(new FJException()); Integer r = f.invoke(); shouldThrow(); return r; - } catch (FJException success) { - } + } catch (FJException success) {} return NoResult; - } - }; - mainPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** * invoke task suppresses execution invoking complete */ public void testComplete() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { FibTask f = new FibTask(8); f.complete(NoResult); Integer r = f.invoke(); - threadAssertTrue(f.isDone()); - threadAssertTrue(r == NoResult); + assertTrue(f.isDone()); + assertSame(NoResult, r); return r; - } - }; - mainPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** * invokeAll(t1, t2) invokes all task arguments */ public void testInvokeAll2() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { 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); + assertTrue(f.isDone()); + assertEquals(21, (int) f.join()); + assertTrue(g.isDone()); + assertEquals(34, (int) g.join()); return NoResult; - } - }; - mainPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** * invokeAll(tasks) with 1 argument invokes task */ public void testInvokeAll1() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { FibTask f = new FibTask(8); invokeAll(f); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.join() == 21); + assertTrue(f.isDone()); + assertEquals(21, (int) f.join()); return NoResult; - } - }; - mainPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** * invokeAll(tasks) with > 2 argument invokes tasks */ public void testInvokeAll3() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { 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); + assertTrue(f.isDone()); + assertEquals(21, (int) f.join()); + assertTrue(g.isDone()); + assertEquals(34, (int) g.join()); + assertTrue(h.isDone()); + assertEquals(13, (int) h.join()); return NoResult; - } - }; - mainPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** * invokeAll(collection) invokes all tasks in the collection */ public void testInvokeAllCollection() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { FibTask f = new FibTask(8); FibTask g = new FibTask(9); FibTask h = new FibTask(7); @@ -743,16 +599,15 @@ public class RecursiveTaskTest extends J 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); + assertTrue(f.isDone()); + assertEquals(21, (int) f.join()); + assertTrue(g.isDone()); + assertEquals(34, (int) g.join()); + assertTrue(h.isDone()); + assertEquals(13, (int) h.join()); return NoResult; - } - }; - mainPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -760,85 +615,77 @@ public class RecursiveTaskTest extends J * invokeAll(t1, t2) throw exception if any task does */ public void testAbnormalInvokeAll2() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { + FibTask f = new FibTask(8); + FailingFibTask g = new FailingFibTask(9); try { - FibTask f = new FibTask(8); - FailingFibTask g = new FailingFibTask(9); invokeAll(f, g); shouldThrow(); return NoResult; - } catch (FJException success) { - } + } catch (FJException success) {} return NoResult; - } - }; - mainPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** * invokeAll(tasks) with 1 argument throws exception if task does */ public void testAbnormalInvokeAll1() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { + FailingFibTask g = new FailingFibTask(9); try { - FailingFibTask g = new FailingFibTask(9); invokeAll(g); shouldThrow(); return NoResult; - } catch (FJException success) { - } + } catch (FJException success) {} return NoResult; - } - }; - mainPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** * invokeAll(tasks) with > 2 argument throws exception if any task does */ public void testAbnormalInvokeAll3() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { + FibTask f = new FibTask(8); + FailingFibTask g = new FailingFibTask(9); + FibTask h = new FibTask(7); 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) { - } + } catch (FJException success) {} return NoResult; - } - }; - mainPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** * invokeAll(collection) throws exception if any task does */ public void testAbnormalInvokeAllCollection() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { + 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); 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) { - } + } catch (FJException success) {} return NoResult; - } - }; - mainPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -846,20 +693,19 @@ public class RecursiveTaskTest extends J * and suppresses execution */ public void testTryUnfork() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { FibTask g = new FibTask(9); - g.fork(); + assertSame(g, g.fork()); FibTask f = new FibTask(8); - f.fork(); - threadAssertTrue(f.tryUnfork()); + assertSame(f, f.fork()); + assertTrue(f.tryUnfork()); helpQuiesce(); - threadAssertFalse(f.isDone()); - threadAssertTrue(g.isDone()); + assertFalse(f.isDone()); + assertTrue(g.isDone()); return NoResult; - } - }; - singletonPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); } /** @@ -867,40 +713,38 @@ public class RecursiveTaskTest extends J * there are more tasks than threads */ public void testGetSurplusQueuedTaskCount() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { FibTask h = new FibTask(7); - h.fork(); + assertSame(h, h.fork()); FibTask g = new FibTask(9); - g.fork(); + assertSame(g, g.fork()); FibTask f = new FibTask(8); - f.fork(); - threadAssertTrue(getSurplusQueuedTaskCount() > 0); + assertSame(f, f.fork()); + assertTrue(getSurplusQueuedTaskCount() > 0); helpQuiesce(); return NoResult; - } - }; - singletonPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); } /** * peekNextLocalTask returns most recent unexecuted task. */ public void testPeekNextLocalTask() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { FibTask g = new FibTask(9); - g.fork(); + assertSame(g, g.fork()); FibTask f = new FibTask(8); - f.fork(); - threadAssertTrue(peekNextLocalTask() == f); - f.join(); - threadAssertTrue(f.isDone()); + assertSame(f, f.fork()); + assertSame(f, peekNextLocalTask()); + assertEquals(21, (int) f.join()); + assertTrue(f.isDone()); helpQuiesce(); return NoResult; - } - }; - singletonPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); } /** @@ -908,60 +752,56 @@ public class RecursiveTaskTest extends J * without executing it */ public void testPollNextLocalTask() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { FibTask g = new FibTask(9); - g.fork(); + assertSame(g, g.fork()); FibTask f = new FibTask(8); - f.fork(); - threadAssertTrue(pollNextLocalTask() == f); + assertSame(f, f.fork()); + assertSame(f, pollNextLocalTask()); helpQuiesce(); - threadAssertFalse(f.isDone()); + assertFalse(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() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { FibTask g = new FibTask(9); - g.fork(); + assertSame(g, g.fork()); FibTask f = new FibTask(8); - f.fork(); - threadAssertTrue(pollTask() == f); + assertSame(f, f.fork()); + assertSame(f, pollTask()); helpQuiesce(); - threadAssertFalse(f.isDone()); - threadAssertTrue(g.isDone()); + assertFalse(f.isDone()); + assertTrue(g.isDone()); return NoResult; - } - }; - singletonPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); } /** * peekNextLocalTask returns least recent unexecuted task in async mode */ public void testPeekNextLocalTaskAsync() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { FibTask g = new FibTask(9); - g.fork(); + assertSame(g, g.fork()); FibTask f = new FibTask(8); - f.fork(); - threadAssertTrue(peekNextLocalTask() == g); - f.join(); + assertSame(f, f.fork()); + assertSame(g, peekNextLocalTask()); + assertEquals(21, (int) f.join()); helpQuiesce(); - threadAssertTrue(f.isDone()); + assertTrue(f.isDone()); return NoResult; - } - }; - asyncSingletonPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); } /** @@ -969,20 +809,19 @@ public class RecursiveTaskTest extends J * without executing it, in async mode */ public void testPollNextLocalTaskAsync() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { FibTask g = new FibTask(9); - g.fork(); + assertSame(g, g.fork()); FibTask f = new FibTask(8); - f.fork(); - threadAssertTrue(pollNextLocalTask() == g); + assertSame(f, f.fork()); + assertSame(g, pollNextLocalTask()); helpQuiesce(); - threadAssertTrue(f.isDone()); - threadAssertFalse(g.isDone()); + assertTrue(f.isDone()); + assertFalse(g.isDone()); return NoResult; - } - }; - asyncSingletonPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); } /** @@ -990,20 +829,19 @@ public class RecursiveTaskTest extends J * without executing it, in async mode */ public void testPollTaskAsync() { - RecursiveTask a = new RecursiveTask() { - public Integer compute() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { FibTask g = new FibTask(9); - g.fork(); + assertSame(g, g.fork()); FibTask f = new FibTask(8); - f.fork(); - threadAssertTrue(pollTask() == g); + assertSame(f, f.fork()); + assertSame(g, pollTask()); helpQuiesce(); - threadAssertTrue(f.isDone()); - threadAssertFalse(g.isDone()); + assertTrue(f.isDone()); + assertFalse(g.isDone()); return NoResult; - } - }; - asyncSingletonPool.invoke(a); + }}; + assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); } }