--- jsr166/src/test/tck/RecursiveTaskTest.java 2010/09/13 23:23:45 1.16 +++ jsr166/src/test/tck/RecursiveTaskTest.java 2021/01/26 13:33:06 1.42 @@ -1,21 +1,26 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ */ -import junit.framework.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; + +import java.util.HashSet; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.ForkJoinTask; import java.util.concurrent.RecursiveTask; -import java.util.concurrent.TimeUnit; -import java.util.HashSet; +import java.util.concurrent.TimeoutException; + +import junit.framework.Test; +import junit.framework.TestSuite; public class RecursiveTaskTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(RecursiveTaskTest.class); @@ -36,36 +41,173 @@ public class RecursiveTaskTest extends J } private T testInvokeOnPool(ForkJoinPool pool, RecursiveTask a) { + try (PoolCleaner cleaner = cleaner(pool)) { + checkNotDone(a); + + T result = pool.invoke(a); + + checkCompletedNormally(a, result); + return result; + } + } + + void checkNotDone(RecursiveTask a) { + assertFalse(a.isDone()); + assertFalse(a.isCompletedNormally()); + assertFalse(a.isCompletedAbnormally()); + assertFalse(a.isCancelled()); + assertNull(a.getException()); + assertNull(a.getRawResult()); + + if (! ForkJoinTask.inForkJoinPool()) { + Thread.currentThread().interrupt(); + try { + a.get(); + shouldThrow(); + } catch (InterruptedException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + + Thread.currentThread().interrupt(); + try { + a.get(randomTimeout(), randomTimeUnit()); + shouldThrow(); + } catch (InterruptedException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + } + + try { + a.get(randomExpiredTimeout(), randomTimeUnit()); + shouldThrow(); + } catch (TimeoutException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + } + + void checkCompletedNormally(RecursiveTask a, T expectedValue) { + assertTrue(a.isDone()); + assertFalse(a.isCancelled()); + assertTrue(a.isCompletedNormally()); + assertFalse(a.isCompletedAbnormally()); + assertNull(a.getException()); + assertSame(expectedValue, a.getRawResult()); + assertSame(expectedValue, a.join()); + assertFalse(a.cancel(false)); + assertFalse(a.cancel(true)); + + T v1 = null, v2 = null; + try { + v1 = a.get(); + v2 = a.get(randomTimeout(), randomTimeUnit()); + } catch (Throwable fail) { threadUnexpectedException(fail); } + assertSame(expectedValue, v1); + assertSame(expectedValue, v2); + } + + /** + * 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 expectedValue) { + Integer r = a.join(); + assertEquals(expectedValue, (int) r); + checkCompletedNormally(a, r); + } + + /** + * Like checkCompletesNormally, but verifies that the task has + * already completed. + */ + void checkCompletedNormally(RecursiveTask a, int expectedValue) { + Integer r = a.getRawResult(); + assertEquals(expectedValue, (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(randomTimeout(), randomTimeUnit()); + 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.getClass(), a.getException().getClass()); + assertNull(a.getRawResult()); + assertFalse(a.cancel(false)); + assertFalse(a.cancel(true)); + try { - return pool.invoke(a); - } finally { - joinPool(pool); + a.join(); + shouldThrow(); + } catch (Throwable expected) { + assertSame(t.getClass(), expected.getClass()); } + + try { + a.get(); + shouldThrow(); + } catch (ExecutionException success) { + assertSame(t.getClass(), success.getCause().getClass()); + } catch (Throwable fail) { threadUnexpectedException(fail); } + + try { + a.get(randomTimeout(), randomTimeUnit()); + shouldThrow(); + } catch (ExecutionException success) { + assertSame(t.getClass(), success.getCause().getClass()); + } catch (Throwable fail) { threadUnexpectedException(fail); } } - static final class FJException extends RuntimeException { - FJException() { super(); } + public static final class FJException extends RuntimeException { + public FJException() { super(); } } - // An invalid return value for Fib + /** An invalid return value for Fib. */ static final Integer NoResult = Integer.valueOf(-17); - // A simple recursive task for testing - static final class FibTask extends RecursiveTask { + /** A simple recursive task for testing. */ + 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; FibTask f1 = new FibTask(n - 1); f1.fork(); - return (new FibTask(n - 2)).compute() + f1.join(); + 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 { + /** A recursive action failing in base case. */ + final class FailingFibTask extends RecursiveTask { final int number; int result; FailingFibTask(int n) { number = n; } @@ -75,7 +217,7 @@ public class RecursiveTaskTest extends J throw new FJException(); FailingFibTask f1 = new FailingFibTask(n - 1); f1.fork(); - return (new FibTask(n - 2)).compute() + f1.join(); + return new FibTask(n - 2).compute() + f1.join(); } } @@ -86,18 +228,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 +245,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); - threadAssertSame(f, 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 +275,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); - threadAssertSame(f, 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 +291,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); - threadAssertSame(f, 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(LONG_DELAY_MS, MILLISECONDS); + assertEquals(21, (int) r); + checkCompletedNormally(f, r); + return r; + }}; assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } @@ -187,58 +307,53 @@ 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); - threadAssertSame(f, 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)); } - /** * helpQuiesce returns when tasks are complete. * 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); - 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)); + assertSame(f, f.fork()); + helpQuiesce(); + while (!f.isDone()) // wait out race + ; + assertEquals(0, getQueuedTaskCount()); + checkCompletedNormally(f, 21); + return NoResult; + }}; + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } - /** * 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 +361,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 +376,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); - threadAssertSame(f, f.fork()); - Integer r = f.join(); + f.join(); shouldThrow(); - return r; } catch (FJException success) { + checkCompletedAbnormally(f, success); } return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -281,21 +395,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); - threadAssertSame(f, f.fork()); - Integer r = f.get(); + 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 +416,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); - threadAssertSame(f, f.fork()); - Integer r = f.get(5L, TimeUnit.SECONDS); + f.get(LONG_DELAY_MS, MILLISECONDS); 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 +437,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); - threadAssertSame(f, 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 +453,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); - threadAssertTrue(f.cancel(true)); - Integer r = f.invoke(); + f.invoke(); shouldThrow(); - return r; } catch (CancellationException success) { + checkCancelled(f); } return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -363,20 +472,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); - threadAssertTrue(f.cancel(true)); - threadAssertSame(f, f.fork()); - Integer r = f.join(); + f.join(); shouldThrow(); - return r; } catch (CancellationException success) { + checkCancelled(f); } return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -384,22 +492,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); - threadAssertTrue(f.cancel(true)); - threadAssertSame(f, f.fork()); - Integer r = f.get(); + f.get(); shouldThrow(); - return r; } catch (CancellationException success) { - } catch (Exception ex) { - unexpectedException(ex); + checkCancelled(f); } return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -407,22 +512,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); - threadAssertTrue(f.cancel(true)); - threadAssertSame(f, f.fork()); - Integer r = f.get(5L, TimeUnit.SECONDS); + f.get(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); - return r; } catch (CancellationException success) { - } catch (Exception ex) { - unexpectedException(ex); + checkCancelled(f); } return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -430,18 +532,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); - threadAssertTrue(f.cancel(true)); - threadAssertSame(f, 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 +549,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,12 +561,11 @@ 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; - } - }; + }}; assertSame(NoResult, a.invoke()); } @@ -476,12 +573,11 @@ public class RecursiveTaskTest extends J * 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,22 +585,22 @@ 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; - } - }; + }}; 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; } }; @@ -512,23 +608,48 @@ public class RecursiveTaskTest extends J } /** - * 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 +657,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(); + f.invoke(); shouldThrow(); - return r; } catch (FJException success) { + checkCompletedAbnormally(f, success); } return NoResult; - } - }; + }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -556,16 +676,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 +692,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 +708,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 +722,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,46 +743,61 @@ 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); - HashSet set = new HashSet(); + 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); + 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 +805,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 +823,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 +843,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 +868,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); - threadAssertSame(g, g.fork()); + assertSame(g, g.fork()); FibTask f = new FibTask(8); - threadAssertSame(f, 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 +888,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); - threadAssertSame(h, h.fork()); + assertSame(h, h.fork()); FibTask g = new FibTask(9); - threadAssertSame(g, g.fork()); + assertSame(g, g.fork()); FibTask f = new FibTask(8); - threadAssertSame(f, 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 +911,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); - threadAssertSame(g, g.fork()); + assertSame(g, g.fork()); FibTask f = new FibTask(8); - threadAssertSame(f, f.fork()); - threadAssertTrue(peekNextLocalTask() == f); - threadAssertTrue(f.join() == 21); - 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 +931,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); - threadAssertSame(g, g.fork()); + assertSame(g, g.fork()); FibTask f = new FibTask(8); - threadAssertSame(f, 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 +950,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); - threadAssertSame(g, g.fork()); + assertSame(g, g.fork()); FibTask f = new FibTask(8); - threadAssertSame(f, 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 +969,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); - threadAssertSame(g, g.fork()); + assertSame(g, g.fork()); FibTask f = new FibTask(8); - threadAssertSame(f, f.fork()); - threadAssertTrue(peekNextLocalTask() == g); - threadAssertEquals(21, (int) 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); - threadAssertSame(g, g.fork()); + assertSame(g, g.fork()); FibTask f = new FibTask(8); - threadAssertSame(f, 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); - threadAssertSame(g, g.fork()); + assertSame(g, g.fork()); FibTask f = new FibTask(8); - threadAssertSame(f, 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)); }