--- jsr166/src/test/tck/RecursiveTaskTest.java 2010/09/16 00:52:49 1.19 +++ jsr166/src/test/tck/RecursiveTaskTest.java 2010/11/22 07:45:50 1.24 @@ -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,24 +40,145 @@ public class RecursiveTaskTest extends J private T testInvokeOnPool(ForkJoinPool pool, RecursiveTask a) { try { - assertFalse(a.isDone()); - assertFalse(a.isCompletedNormally()); - assertFalse(a.isCompletedAbnormally()); - assertFalse(a.isCancelled()); + checkNotDone(a); T result = pool.invoke(a); - assertTrue(a.isDone()); - assertTrue(a.isCompletedNormally()); - assertFalse(a.isCompletedAbnormally()); - assertFalse(a.isCancelled()); - assertNull(a.getException()); + 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(); } } @@ -74,6 +198,10 @@ 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 @@ -103,10 +231,7 @@ public class RecursiveTaskTest extends J FibTask f = new FibTask(8); Integer r = f.invoke(); assertEquals(21, (int) r); - assertTrue(f.isDone()); - assertFalse(f.isCancelled()); - assertFalse(f.isCompletedAbnormally()); - assertEquals(21, (int) f.getRawResult()); + checkCompletedNormally(f, r); return r; }}; assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); @@ -122,14 +247,10 @@ public class RecursiveTaskTest extends J public Integer realCompute() { FibTask f = new FibTask(8); f.quietlyInvoke(); - Integer r = f.getRawResult(); - assertEquals(21, (int) r); - assertTrue(f.isDone()); - assertFalse(f.isCancelled()); - assertFalse(f.isCompletedAbnormally()); - return r; + checkCompletedNormally(f, 21); + return NoResult; }}; - assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } /** @@ -142,7 +263,7 @@ public class RecursiveTaskTest extends J assertSame(f, f.fork()); Integer r = f.join(); assertEquals(21, (int) r); - assertTrue(f.isDone()); + checkCompletedNormally(f, r); return r; }}; assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); @@ -158,7 +279,7 @@ public class RecursiveTaskTest extends J assertSame(f, f.fork()); Integer r = f.get(); assertEquals(21, (int) r); - assertTrue(f.isDone()); + checkCompletedNormally(f, r); return r; }}; assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); @@ -172,9 +293,9 @@ public class RecursiveTaskTest extends J public Integer realCompute() throws Exception { FibTask f = new FibTask(8); assertSame(f, f.fork()); - Integer r = f.get(5L, TimeUnit.SECONDS); + Integer r = f.get(5L, SECONDS); assertEquals(21, (int) r); - assertTrue(f.isDone()); + checkCompletedNormally(f, r); return r; }}; assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); @@ -191,7 +312,7 @@ public class RecursiveTaskTest extends J f.quietlyJoin(); Integer r = f.getRawResult(); assertEquals(21, (int) r); - assertTrue(f.isDone()); + checkCompletedNormally(f, r); return r; }}; assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); @@ -208,13 +329,11 @@ public class RecursiveTaskTest extends J FibTask f = new FibTask(8); assertSame(f, f.fork()); f.helpQuiesce(); - Integer r = f.getRawResult(); - assertEquals(21, (int) r); - assertTrue(f.isDone()); assertEquals(0, getQueuedTaskCount()); - return r; + checkCompletedNormally(f, 21); + return NoResult; }}; - assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } @@ -228,8 +347,9 @@ public class RecursiveTaskTest extends J try { f.invoke(); shouldThrow(); - return NoResult; - } catch (FJException success) {} + } catch (FJException success) { + checkCompletedAbnormally(f, success); + } return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -243,7 +363,8 @@ public class RecursiveTaskTest extends J public Integer realCompute() { FailingFibTask f = new FailingFibTask(8); f.quietlyInvoke(); - assertTrue(f.isDone()); + assertTrue(f.getException() instanceof FJException); + checkCompletedAbnormally(f, f.getException()); return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -260,8 +381,9 @@ public class RecursiveTaskTest extends J try { Integer r = f.join(); shouldThrow(); - return r; - } catch (FJException success) {} + } catch (FJException success) { + checkCompletedAbnormally(f, success); + } return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -278,8 +400,11 @@ public class RecursiveTaskTest extends J try { Integer r = f.get(); shouldThrow(); - return r; - } catch (ExecutionException success) {} + } catch (ExecutionException success) { + Throwable cause = success.getCause(); + assertTrue(cause instanceof FJException); + checkCompletedAbnormally(f, cause); + } return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -294,10 +419,13 @@ public class RecursiveTaskTest extends J FailingFibTask f = new FailingFibTask(8); assertSame(f, f.fork()); try { - Integer r = f.get(5L, TimeUnit.SECONDS); + Integer r = f.get(5L, SECONDS); shouldThrow(); - return r; - } catch (ExecutionException success) {} + } catch (ExecutionException success) { + Throwable cause = success.getCause(); + assertTrue(cause instanceof FJException); + checkCompletedAbnormally(f, cause); + } return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -312,9 +440,8 @@ public class RecursiveTaskTest extends J FailingFibTask f = new FailingFibTask(8); assertSame(f, f.fork()); f.quietlyJoin(); - assertTrue(f.isDone()); - assertTrue(f.isCompletedAbnormally()); assertTrue(f.getException() instanceof FJException); + checkCompletedAbnormally(f, f.getException()); return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -331,8 +458,9 @@ public class RecursiveTaskTest extends J try { Integer r = f.invoke(); shouldThrow(); - return r; - } catch (CancellationException success) {} + } catch (CancellationException success) { + checkCancelled(f); + } return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -350,8 +478,9 @@ public class RecursiveTaskTest extends J try { Integer r = f.join(); shouldThrow(); - return r; - } catch (CancellationException success) {} + } catch (CancellationException success) { + checkCancelled(f); + } return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -369,8 +498,9 @@ public class RecursiveTaskTest extends J try { Integer r = f.get(); shouldThrow(); - return r; - } catch (CancellationException success) {} + } catch (CancellationException success) { + checkCancelled(f); + } return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -386,10 +516,11 @@ public class RecursiveTaskTest extends J assertTrue(f.cancel(true)); assertSame(f, f.fork()); try { - Integer r = f.get(5L, TimeUnit.SECONDS); + Integer r = f.get(5L, SECONDS); shouldThrow(); - return r; - } catch (CancellationException success) {} + } catch (CancellationException success) { + checkCancelled(f); + } return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -405,10 +536,7 @@ public class RecursiveTaskTest extends J assertTrue(f.cancel(true)); assertSame(f, f.fork()); f.quietlyJoin(); - assertTrue(f.isDone()); - assertTrue(f.isCompletedAbnormally()); - assertTrue(f.isCancelled()); - assertTrue(f.getException() instanceof CancellationException); + checkCancelled(f); return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -457,7 +585,7 @@ public class RecursiveTaskTest extends J public void testInForkJoinPool2() { RecursiveTask a = new CheckedRecursiveTask() { public Integer realCompute() { - assertTrue(!inForkJoinPool()); + assertFalse(inForkJoinPool()); return NoResult; }}; assertSame(NoResult, a.invoke()); @@ -474,24 +602,50 @@ public class RecursiveTaskTest extends J return NoResult; } }; - a.invoke(); + assertSame(NoResult, a.invoke()); } /** - * A reinitialized task may be re-invoked + * A reinitialized normally completed task may be re-invoked */ public void testReinitialize() { RecursiveTask a = new CheckedRecursiveTask() { public Integer realCompute() { FibTask f = new FibTask(8); - Integer r = f.invoke(); - assertEquals(21, (int) r); - assertTrue(f.isDone()); - assertFalse(f.isCancelled()); - assertFalse(f.isCompletedAbnormally()); - f.reinitialize(); - r = f.invoke(); - assertEquals(21, (int) r); + 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)); @@ -508,8 +662,9 @@ public class RecursiveTaskTest extends J try { Integer r = f.invoke(); shouldThrow(); - return r; - } catch (FJException success) {} + } catch (FJException success) { + checkCompletedAbnormally(f, success); + } return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -524,8 +679,8 @@ public class RecursiveTaskTest extends J FibTask f = new FibTask(8); f.complete(NoResult); Integer r = f.invoke(); - assertTrue(f.isDone()); assertSame(NoResult, r); + checkCompletedNormally(f, NoResult); return r; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -540,10 +695,8 @@ public class RecursiveTaskTest extends J FibTask f = new FibTask(8); FibTask g = new FibTask(9); invokeAll(f, g); - assertTrue(f.isDone()); - assertEquals(21, (int) f.join()); - assertTrue(g.isDone()); - assertEquals(34, (int) g.join()); + checkCompletedNormally(f, 21); + checkCompletedNormally(g, 34); return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -557,8 +710,7 @@ public class RecursiveTaskTest extends J public Integer realCompute() { FibTask f = new FibTask(8); invokeAll(f); - assertTrue(f.isDone()); - assertEquals(21, (int) f.join()); + checkCompletedNormally(f, 21); return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -575,11 +727,11 @@ public class RecursiveTaskTest extends J FibTask h = new FibTask(7); invokeAll(f, g, h); assertTrue(f.isDone()); - assertEquals(21, (int) f.join()); assertTrue(g.isDone()); - assertEquals(34, (int) g.join()); assertTrue(h.isDone()); - assertEquals(13, (int) h.join()); + checkCompletedNormally(f, 21); + checkCompletedNormally(g, 34); + checkCompletedNormally(h, 13); return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -600,11 +752,11 @@ public class RecursiveTaskTest extends J set.add(h); invokeAll(set); assertTrue(f.isDone()); - assertEquals(21, (int) f.join()); assertTrue(g.isDone()); - assertEquals(34, (int) g.join()); assertTrue(h.isDone()); - assertEquals(13, (int) h.join()); + checkCompletedNormally(f, 21); + checkCompletedNormally(g, 34); + checkCompletedNormally(h, 13); return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -612,6 +764,24 @@ public class RecursiveTaskTest extends J /** + * 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() { @@ -622,8 +792,9 @@ public class RecursiveTaskTest extends J try { invokeAll(f, g); shouldThrow(); - return NoResult; - } catch (FJException success) {} + } catch (FJException success) { + checkCompletedAbnormally(g, success); + } return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -639,8 +810,9 @@ public class RecursiveTaskTest extends J try { invokeAll(g); shouldThrow(); - return NoResult; - } catch (FJException success) {} + } catch (FJException success) { + checkCompletedAbnormally(g, success); + } return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -658,8 +830,9 @@ public class RecursiveTaskTest extends J try { invokeAll(f, g, h); shouldThrow(); - return NoResult; - } catch (FJException success) {} + } catch (FJException success) { + checkCompletedAbnormally(g, success); + } return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -681,8 +854,9 @@ public class RecursiveTaskTest extends J try { invokeAll(set); shouldThrow(); - return NoResult; - } catch (FJException success) {} + } catch (FJException success) { + checkCompletedAbnormally(f, success); + } return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -701,8 +875,8 @@ public class RecursiveTaskTest extends J assertSame(f, f.fork()); assertTrue(f.tryUnfork()); helpQuiesce(); - assertFalse(f.isDone()); - assertTrue(g.isDone()); + checkNotDone(f); + checkCompletedNormally(g, 34); return NoResult; }}; assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); @@ -723,6 +897,10 @@ public class RecursiveTaskTest extends J 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)); @@ -739,9 +917,9 @@ public class RecursiveTaskTest extends J FibTask f = new FibTask(8); assertSame(f, f.fork()); assertSame(f, peekNextLocalTask()); - assertEquals(21, (int) f.join()); - assertTrue(f.isDone()); + checkCompletesNormally(f, 21); helpQuiesce(); + checkCompletedNormally(g, 34); return NoResult; }}; assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); @@ -760,7 +938,8 @@ public class RecursiveTaskTest extends J assertSame(f, f.fork()); assertSame(f, pollNextLocalTask()); helpQuiesce(); - assertFalse(f.isDone()); + checkNotDone(f); + checkCompletedNormally(g, 34); return NoResult; }}; assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); @@ -778,8 +957,8 @@ public class RecursiveTaskTest extends J assertSame(f, f.fork()); assertSame(f, pollTask()); helpQuiesce(); - assertFalse(f.isDone()); - assertTrue(g.isDone()); + checkNotDone(f); + checkCompletedNormally(g, 34); return NoResult; }}; assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); @@ -798,15 +977,16 @@ public class RecursiveTaskTest extends J assertSame(g, peekNextLocalTask()); assertEquals(21, (int) f.join()); helpQuiesce(); - assertTrue(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 CheckedRecursiveTask() { @@ -817,16 +997,16 @@ public class RecursiveTaskTest extends J assertSame(f, f.fork()); assertSame(g, pollNextLocalTask()); helpQuiesce(); - assertTrue(f.isDone()); - assertFalse(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 CheckedRecursiveTask() { @@ -837,8 +1017,8 @@ public class RecursiveTaskTest extends J assertSame(f, f.fork()); assertSame(g, pollTask()); helpQuiesce(); - assertTrue(f.isDone()); - assertFalse(g.isDone()); + checkCompletedNormally(f, 21); + checkNotDone(g); return NoResult; }}; assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a));