--- jsr166/src/test/tck/RecursiveTaskTest.java 2010/09/16 00:52:49 1.19 +++ jsr166/src/test/tck/RecursiveTaskTest.java 2017/03/18 18:20:00 1.35 @@ -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.SECONDS; + +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,33 +41,152 @@ 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()); + try (PoolCleaner cleaner = cleaner(pool)) { + 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); } } - static final class FJException extends RuntimeException { - FJException() { super(); } + 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(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.getClass(), a.getException().getClass()); + assertNull(a.getRawResult()); + assertFalse(a.cancel(false)); + assertFalse(a.cancel(true)); + + try { + 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(5L, SECONDS); + shouldThrow(); + } catch (ExecutionException success) { + assertSame(t.getClass(), success.getCause().getClass()); + } catch (Throwable fail) { threadUnexpectedException(fail); } + } + + 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 + /** A simple recursive task for testing. */ final class FibTask extends CheckedRecursiveTask { final int number; FibTask(int n) { number = n; } @@ -74,9 +198,13 @@ 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 + /** A recursive action failing in base case. */ final class FailingFibTask extends RecursiveTask { final int number; int result; @@ -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,13 +312,12 @@ 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)); } - /** * helpQuiesce returns when tasks are complete. * getQueuedTaskCount returns 0 when quiescent @@ -207,17 +327,16 @@ public class RecursiveTaskTest extends J public Integer realCompute() { FibTask f = new FibTask(8); assertSame(f, f.fork()); - f.helpQuiesce(); - Integer r = f.getRawResult(); - assertEquals(21, (int) r); - assertTrue(f.isDone()); + helpQuiesce(); + while (!f.isDone()) // wait out race + ; assertEquals(0, getQueuedTaskCount()); - return r; + checkCompletedNormally(f, 21); + return NoResult; }}; - assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); + assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } - /** * invoke task throws exception when task completes abnormally */ @@ -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,16 +752,33 @@ 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)); } + /** + * 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 @@ -622,8 +791,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 +809,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 +829,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 +853,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 +874,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 +896,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 +916,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 +937,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 +956,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 +976,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 +996,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 +1016,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));