--- jsr166/src/test/tck/RecursiveTaskTest.java 2010/09/13 20:48:58 1.15 +++ jsr166/src/test/tck/RecursiveTaskTest.java 2010/11/21 20:32:15 1.23 @@ -8,8 +8,11 @@ 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.RecursiveTask; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import static java.util.concurrent.TimeUnit.SECONDS; import java.util.HashSet; public class RecursiveTaskTest extends JSR166TestCase { @@ -37,12 +40,145 @@ public class RecursiveTaskTest extends J private T testInvokeOnPool(ForkJoinPool pool, RecursiveTask a) { try { - return pool.invoke(a); + checkNotDone(a); + + T result = pool.invoke(a); + + checkCompletedNormally(a, result); + return result; } finally { joinPool(pool); } } + void checkNotDone(RecursiveTask a) { + assertFalse(a.isDone()); + assertFalse(a.isCompletedNormally()); + assertFalse(a.isCompletedAbnormally()); + assertFalse(a.isCancelled()); + assertNull(a.getException()); + assertNull(a.getRawResult()); + + if (! (Thread.currentThread() instanceof ForkJoinWorkerThread)) { + Thread.currentThread().interrupt(); + try { + a.get(); + shouldThrow(); + } catch (InterruptedException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + + Thread.currentThread().interrupt(); + try { + a.get(5L, SECONDS); + shouldThrow(); + } catch (InterruptedException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + } + + try { + a.get(0L, SECONDS); + shouldThrow(); + } catch (TimeoutException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + } + + void checkCompletedNormally(RecursiveTask a, T expected) { + assertTrue(a.isDone()); + assertFalse(a.isCancelled()); + assertTrue(a.isCompletedNormally()); + assertFalse(a.isCompletedAbnormally()); + assertNull(a.getException()); + assertSame(expected, a.getRawResult()); + assertSame(expected, a.join()); + assertFalse(a.cancel(false)); + assertFalse(a.cancel(true)); + try { + assertSame(expected, a.get()); + } catch (Throwable fail) { threadUnexpectedException(fail); } + try { + assertSame(expected, a.get(5L, SECONDS)); + } catch (Throwable fail) { threadUnexpectedException(fail); } + } + + /** + * Waits for the task to complete, and checks that when it does, + * it will have an Integer result equals to the given int. + */ + void checkCompletesNormally(RecursiveTask a, int expected) { + Integer r = a.join(); + assertEquals(expected, (int) r); + checkCompletedNormally(a, r); + } + + /** + * Like checkCompletesNormally, but verifies that the task has + * already completed. + */ + void checkCompletedNormally(RecursiveTask a, int expected) { + Integer r = a.getRawResult(); + assertEquals(expected, (int) r); + checkCompletedNormally(a, r); + } + + void checkCancelled(RecursiveTask a) { + assertTrue(a.isDone()); + assertTrue(a.isCancelled()); + assertFalse(a.isCompletedNormally()); + assertTrue(a.isCompletedAbnormally()); + assertTrue(a.getException() instanceof CancellationException); + assertNull(a.getRawResult()); + + try { + a.join(); + shouldThrow(); + } catch (CancellationException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + + try { + a.get(); + shouldThrow(); + } catch (CancellationException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + + try { + a.get(5L, SECONDS); + shouldThrow(); + } catch (CancellationException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + } + + void checkCompletedAbnormally(RecursiveTask a, Throwable t) { + assertTrue(a.isDone()); + assertFalse(a.isCancelled()); + assertFalse(a.isCompletedNormally()); + assertTrue(a.isCompletedAbnormally()); + assertSame(t, a.getException()); + assertNull(a.getRawResult()); + assertFalse(a.cancel(false)); + assertFalse(a.cancel(true)); + + try { + a.join(); + shouldThrow(); + } catch (Throwable expected) { + assertSame(t, expected); + } + + try { + a.get(); + shouldThrow(); + } catch (ExecutionException success) { + assertSame(t, success.getCause()); + } catch (Throwable fail) { threadUnexpectedException(fail); } + + try { + a.get(5L, SECONDS); + shouldThrow(); + } catch (ExecutionException success) { + assertSame(t, success.getCause()); + } catch (Throwable fail) { threadUnexpectedException(fail); } + } + static final class FJException extends RuntimeException { FJException() { super(); } } @@ -51,10 +187,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; @@ -62,10 +198,14 @@ public class RecursiveTaskTest extends J f1.fork(); return (new FibTask(n - 2)).compute() + f1.join(); } + + public void publicSetRawResult(Integer result) { + setRawResult(result); + } } // 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; } @@ -86,18 +226,14 @@ public class RecursiveTaskTest extends J * 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); + checkCompletedNormally(f, r); return r; - } - }; + }}; assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } @@ -107,35 +243,29 @@ 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()); - return r; - } - }; - assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); + checkCompletedNormally(f, 21); + return NoResult; + }}; + assertSame(NoResult, 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); + checkCompletedNormally(f, r); return r; - } - }; + }}; assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } @@ -143,21 +273,15 @@ public class RecursiveTaskTest extends J * 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; - } - }; + 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); + checkCompletedNormally(f, r); + return r; + }}; assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } @@ -165,21 +289,15 @@ public class RecursiveTaskTest extends J * 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; - } - }; + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() throws Exception { + FibTask f = new FibTask(8); + assertSame(f, f.fork()); + Integer r = f.get(5L, SECONDS); + assertEquals(21, (int) r); + checkCompletedNormally(f, r); + return r; + }}; assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } @@ -187,17 +305,16 @@ public class RecursiveTaskTest extends J * 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); + checkCompletedNormally(f, r); return r; - } - }; + }}; assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } @@ -207,19 +324,16 @@ 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); - return r; - } - }; - assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); + assertEquals(0, getQueuedTaskCount()); + checkCompletedNormally(f, 21); + return NoResult; + }}; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -227,18 +341,17 @@ 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) { + checkCompletedAbnormally(f, success); } return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -246,14 +359,14 @@ public class RecursiveTaskTest extends J * 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.getException() instanceof FJException); + checkCompletedAbnormally(f, f.getException()); return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -261,19 +374,18 @@ public class RecursiveTaskTest extends J * 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) { + checkCompletedAbnormally(f, success); } return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -281,21 +393,20 @@ public class RecursiveTaskTest extends J * 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); + Throwable cause = success.getCause(); + assertTrue(cause instanceof FJException); + checkCompletedAbnormally(f, cause); } return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -303,21 +414,20 @@ public class RecursiveTaskTest extends J * 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); + Integer r = f.get(5L, SECONDS); shouldThrow(); - return r; } catch (ExecutionException success) { - } catch (Exception ex) { - unexpectedException(ex); + Throwable cause = success.getCause(); + assertTrue(cause instanceof FJException); + checkCompletedAbnormally(f, cause); } return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -325,17 +435,15 @@ public class RecursiveTaskTest extends J * 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.getException() instanceof FJException); + checkCompletedAbnormally(f, f.getException()); return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -343,19 +451,18 @@ public class RecursiveTaskTest extends J * 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) { + checkCancelled(f); } return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -363,20 +470,19 @@ public class RecursiveTaskTest extends J * 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) { + checkCancelled(f); } return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -384,22 +490,19 @@ public class RecursiveTaskTest extends J * 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); + checkCancelled(f); } return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -407,22 +510,19 @@ public class RecursiveTaskTest extends J * 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); + Integer r = f.get(5L, SECONDS); shouldThrow(); - return r; } catch (CancellationException success) { - } catch (Exception ex) { - unexpectedException(ex); + checkCancelled(f); } return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -430,18 +530,15 @@ public class RecursiveTaskTest extends J * 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); + checkCancelled(f); return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -450,12 +547,11 @@ public class RecursiveTaskTest extends J */ public void testGetPool() { final ForkJoinPool mainPool = mainPool(); - RecursiveTask a = new RecursiveTask() { - public Integer compute() { - threadAssertTrue(getPool() == mainPool); + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { + assertSame(mainPool, getPool()); return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool, a)); } @@ -463,25 +559,23 @@ public class RecursiveTaskTest extends J * 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; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -489,46 +583,71 @@ public class RecursiveTaskTest extends J * 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() { + assertFalse(inForkJoinPool()); return NoResult; - } - }; - a.invoke(); + }}; + assertSame(NoResult, a.invoke()); } /** - * The value set by setRawResult is returned by invoke + * 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; } }; - assertSame(NoResult, a.invoke()); + a.invoke(); } /** - * A reinitialized task may be re-invoked + * A reinitialized normally completed 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()); - f.reinitialize(); - r = f.invoke(); - threadAssertTrue(r == 21); + checkNotDone(f); + + for (int i = 0; i < 3; i++) { + Integer r = f.invoke(); + assertEquals(21, (int) r); + checkCompletedNormally(f, r); + f.reinitialize(); + f.publicSetRawResult(null); + checkNotDone(f); + } return NoResult; - } - }; + }}; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); + } + + /** + * A reinitialized abnormally completed task may be re-invoked + */ + public void testReinitializeAbnormal() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { + FailingFibTask f = new FailingFibTask(8); + checkNotDone(f); + + for (int i = 0; i < 3; i++) { + try { + f.invoke(); + shouldThrow(); + } catch (FJException success) { + checkCompletedAbnormally(f, success); + } + f.reinitialize(); + checkNotDone(f); + } + return NoResult; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -536,19 +655,18 @@ public class RecursiveTaskTest extends J * 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) { + checkCompletedAbnormally(f, success); } return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -556,16 +674,15 @@ public class RecursiveTaskTest extends J * 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); + assertSame(NoResult, r); + checkCompletedNormally(f, NoResult); return r; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -573,18 +690,15 @@ public class RecursiveTaskTest extends J * 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); + checkCompletedNormally(f, 21); + checkCompletedNormally(g, 34); return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -592,15 +706,13 @@ public class RecursiveTaskTest extends J * 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); + checkCompletedNormally(f, 21); return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -608,21 +720,20 @@ public class RecursiveTaskTest extends J * 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()); + assertTrue(g.isDone()); + assertTrue(h.isDone()); + checkCompletedNormally(f, 21); + checkCompletedNormally(g, 34); + checkCompletedNormally(h, 13); return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -630,8 +741,8 @@ public class RecursiveTaskTest extends J * 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); @@ -640,36 +751,52 @@ 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()); + assertTrue(g.isDone()); + assertTrue(h.isDone()); + checkCompletedNormally(f, 21); + checkCompletedNormally(g, 34); + checkCompletedNormally(h, 13); return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** + * invokeAll(tasks) with any null task throws NPE + */ + public void testInvokeAllNPE() { + RecursiveTask a = new CheckedRecursiveTask() { + public Integer realCompute() { + FibTask f = new FibTask(8); + FibTask g = new FibTask(9); + FibTask h = null; + try { + invokeAll(f, g, h); + shouldThrow(); + } catch (NullPointerException success) {} + return NoResult; + }}; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); + } + + /** * 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) { + checkCompletedAbnormally(g, success); } return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -677,18 +804,17 @@ public class RecursiveTaskTest extends J * 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) { + checkCompletedAbnormally(g, success); } return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -696,20 +822,19 @@ public class RecursiveTaskTest extends J * 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) { + checkCompletedAbnormally(g, success); } return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -717,24 +842,23 @@ public class RecursiveTaskTest extends J * 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) { + checkCompletedAbnormally(f, success); } return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -743,19 +867,18 @@ 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()); + checkNotDone(f); + checkCompletedNormally(g, 34); return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); } @@ -764,19 +887,22 @@ 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(); + assertEquals(0, getSurplusQueuedTaskCount()); + checkCompletedNormally(f, 21); + checkCompletedNormally(g, 34); + checkCompletedNormally(h, 13); return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); } @@ -784,19 +910,18 @@ public class RecursiveTaskTest extends J * 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()); + checkCompletesNormally(f, 21); helpQuiesce(); + checkCompletedNormally(g, 34); return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); } @@ -805,18 +930,18 @@ 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()); + checkNotDone(f); + checkCompletedNormally(g, 34); return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); } @@ -824,19 +949,18 @@ public class RecursiveTaskTest extends J * 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()); + checkNotDone(f); + checkCompletedNormally(g, 34); return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); } @@ -844,61 +968,59 @@ public class RecursiveTaskTest extends J * 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()); + checkCompletedNormally(f, 21); + checkCompletedNormally(g, 34); return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); } /** - * pollNextLocalTask returns least recent unexecuted task - * without executing it, in async mode + * pollNextLocalTask returns least recent unexecuted task 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()); + checkCompletedNormally(f, 21); + checkNotDone(g); return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); } /** - * pollTask returns an unexecuted task - * without executing it, in async mode + * pollTask returns an unexecuted task 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()); + checkCompletedNormally(f, 21); + checkNotDone(g); return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); }