--- jsr166/src/test/tck/RecursiveTaskTest.java 2010/09/16 00:52:49 1.19 +++ jsr166/src/test/tck/RecursiveTaskTest.java 2010/11/21 08:25:10 1.20 @@ -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,141 @@ 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()); + 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 checkTaskThrew(RecursiveTask a, Throwable t) { + assertTrue(a.isDone()); + assertFalse(a.isCancelled()); + assertFalse(a.isCompletedNormally()); + assertTrue(a.isCompletedAbnormally()); + assertSame(t, a.getException()); + assertNull(a.getRawResult()); + + 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 +194,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 +227,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 +243,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 +259,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 +275,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 +289,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 +308,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 +325,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 +343,9 @@ public class RecursiveTaskTest extends J try { f.invoke(); shouldThrow(); - return NoResult; - } catch (FJException success) {} + } catch (FJException success) { + checkTaskThrew(f, success); + } return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -243,7 +359,8 @@ public class RecursiveTaskTest extends J public Integer realCompute() { FailingFibTask f = new FailingFibTask(8); f.quietlyInvoke(); - assertTrue(f.isDone()); + assertTrue(f.getException() instanceof FJException); + checkTaskThrew(f, f.getException()); return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -260,8 +377,9 @@ public class RecursiveTaskTest extends J try { Integer r = f.join(); shouldThrow(); - return r; - } catch (FJException success) {} + } catch (FJException success) { + checkTaskThrew(f, success); + } return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -278,8 +396,9 @@ public class RecursiveTaskTest extends J try { Integer r = f.get(); shouldThrow(); - return r; - } catch (ExecutionException success) {} + } catch (ExecutionException success) { + checkTaskThrew(f, success.getCause()); + } return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -294,10 +413,11 @@ 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) { + checkTaskThrew(f, success.getCause()); + } return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -312,9 +432,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); + checkTaskThrew(f, f.getException()); return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -331,8 +450,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 +470,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 +490,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 +508,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 +528,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)); @@ -478,20 +598,46 @@ 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 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) { + checkTaskThrew(f, success); + } + f.reinitialize(); + checkNotDone(f); + } return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -508,8 +654,9 @@ public class RecursiveTaskTest extends J try { Integer r = f.invoke(); shouldThrow(); - return r; - } catch (FJException success) {} + } catch (FJException success) { + checkTaskThrew(f, success); + } return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -524,8 +671,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 +687,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()); + checkCompletesNormally(f, 21); + checkCompletesNormally(g, 34); return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -557,8 +702,7 @@ public class RecursiveTaskTest extends J public Integer realCompute() { FibTask f = new FibTask(8); invokeAll(f); - assertTrue(f.isDone()); - assertEquals(21, (int) f.join()); + checkCompletesNormally(f, 21); return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -574,12 +718,9 @@ public class RecursiveTaskTest extends J FibTask g = new FibTask(9); 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()); + checkCompletesNormally(f, 21); + checkCompletesNormally(g, 34); + checkCompletesNormally(h, 13); return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -599,12 +740,9 @@ public class RecursiveTaskTest extends J set.add(g); 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()); + checkCompletesNormally(f, 21); + checkCompletesNormally(g, 34); + checkCompletesNormally(h, 13); return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -612,6 +750,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 +778,9 @@ public class RecursiveTaskTest extends J try { invokeAll(f, g); shouldThrow(); - return NoResult; - } catch (FJException success) {} + } catch (FJException success) { + checkTaskThrew(g, success); + } return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -639,8 +796,9 @@ public class RecursiveTaskTest extends J try { invokeAll(g); shouldThrow(); - return NoResult; - } catch (FJException success) {} + } catch (FJException success) { + checkTaskThrew(g, success); + } return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -658,8 +816,9 @@ public class RecursiveTaskTest extends J try { invokeAll(f, g, h); shouldThrow(); - return NoResult; - } catch (FJException success) {} + } catch (FJException success) { + checkTaskThrew(g, success); + } return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -681,8 +840,9 @@ public class RecursiveTaskTest extends J try { invokeAll(set); shouldThrow(); - return NoResult; - } catch (FJException success) {} + } catch (FJException success) { + checkTaskThrew(f, success); + } return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -701,8 +861,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 +883,9 @@ public class RecursiveTaskTest extends J assertSame(f, f.fork()); assertTrue(getSurplusQueuedTaskCount() > 0); helpQuiesce(); + checkCompletedNormally(f, 21); + checkCompletedNormally(g, 34); + checkCompletedNormally(h, 13); return NoResult; }}; assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); @@ -739,9 +902,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 +923,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 +942,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 +962,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 +982,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 +1002,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));