--- jsr166/src/test/tck/RecursiveTaskTest.java 2010/11/21 08:25:10 1.20 +++ jsr166/src/test/tck/RecursiveTaskTest.java 2019/12/16 22:55:54 1.41 @@ -1,24 +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.ForkJoinWorkerThread; +import java.util.concurrent.ForkJoinTask; 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; + +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); @@ -39,15 +41,13 @@ public class RecursiveTaskTest extends J } private T testInvokeOnPool(ForkJoinPool pool, RecursiveTask a) { - try { + try (PoolCleaner cleaner = cleaner(pool)) { checkNotDone(a); T result = pool.invoke(a); checkCompletedNormally(a, result); return result; - } finally { - joinPool(pool); } } @@ -59,7 +59,7 @@ public class RecursiveTaskTest extends J assertNull(a.getException()); assertNull(a.getRawResult()); - if (! (Thread.currentThread() instanceof ForkJoinWorkerThread)) { + if (! ForkJoinTask.inForkJoinPool()) { Thread.currentThread().interrupt(); try { a.get(); @@ -69,42 +69,46 @@ public class RecursiveTaskTest extends J Thread.currentThread().interrupt(); try { - a.get(5L, SECONDS); + a.get(randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (InterruptedException success) { } catch (Throwable fail) { threadUnexpectedException(fail); } } try { - a.get(0L, SECONDS); + a.get(randomExpiredTimeout(), randomTimeUnit()); shouldThrow(); } catch (TimeoutException success) { } catch (Throwable fail) { threadUnexpectedException(fail); } } - void checkCompletedNormally(RecursiveTask a, T expected) { + void checkCompletedNormally(RecursiveTask a, T expectedValue) { 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); } + assertSame(expectedValue, a.getRawResult()); + assertSame(expectedValue, a.join()); + assertFalse(a.cancel(false)); + assertFalse(a.cancel(true)); + + T v1 = null, v2 = null; try { - assertSame(expected, a.get(5L, SECONDS)); + 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 expected) { + void checkCompletesNormally(RecursiveTask a, int expectedValue) { Integer r = a.join(); - assertEquals(expected, (int) r); + assertEquals(expectedValue, (int) r); checkCompletedNormally(a, r); } @@ -112,9 +116,9 @@ public class RecursiveTaskTest extends J * Like checkCompletesNormally, but verifies that the task has * already completed. */ - void checkCompletedNormally(RecursiveTask a, int expected) { + void checkCompletedNormally(RecursiveTask a, int expectedValue) { Integer r = a.getRawResult(); - assertEquals(expected, (int) r); + assertEquals(expectedValue, (int) r); checkCompletedNormally(a, r); } @@ -139,50 +143,52 @@ public class RecursiveTaskTest extends J } catch (Throwable fail) { threadUnexpectedException(fail); } try { - a.get(5L, SECONDS); + a.get(randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (CancellationException success) { } catch (Throwable fail) { threadUnexpectedException(fail); } } - void checkTaskThrew(RecursiveTask a, Throwable t) { + void checkCompletedAbnormally(RecursiveTask a, Throwable t) { assertTrue(a.isDone()); assertFalse(a.isCancelled()); assertFalse(a.isCompletedNormally()); assertTrue(a.isCompletedAbnormally()); - assertSame(t, a.getException()); + 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, expected); + assertSame(t.getClass(), expected.getClass()); } try { a.get(); shouldThrow(); } catch (ExecutionException success) { - assertSame(t, success.getCause()); + assertSame(t.getClass(), success.getCause().getClass()); } catch (Throwable fail) { threadUnexpectedException(fail); } try { - a.get(5L, SECONDS); + a.get(randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (ExecutionException success) { - assertSame(t, success.getCause()); + 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 + /** A simple recursive task for testing. */ final class FibTask extends CheckedRecursiveTask { final int number; FibTask(int n) { number = n; } @@ -192,7 +198,7 @@ public class RecursiveTaskTest extends J 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) { @@ -200,7 +206,7 @@ public class RecursiveTaskTest extends J } } - // A recursive action failing in base case + /** A recursive action failing in base case. */ final class FailingFibTask extends RecursiveTask { final int number; int result; @@ -211,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(); } } @@ -289,7 +295,7 @@ public class RecursiveTaskTest extends J public Integer realCompute() throws Exception { FibTask f = new FibTask(8); assertSame(f, f.fork()); - Integer r = f.get(5L, SECONDS); + Integer r = f.get(LONG_DELAY_MS, MILLISECONDS); assertEquals(21, (int) r); checkCompletedNormally(f, r); return r; @@ -314,7 +320,6 @@ public class RecursiveTaskTest extends J assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); } - /** * helpQuiesce returns when tasks are complete. * getQueuedTaskCount returns 0 when quiescent @@ -324,7 +329,9 @@ public class RecursiveTaskTest extends J public Integer realCompute() { FibTask f = new FibTask(8); assertSame(f, f.fork()); - f.helpQuiesce(); + helpQuiesce(); + while (!f.isDone()) // wait out race + ; assertEquals(0, getQueuedTaskCount()); checkCompletedNormally(f, 21); return NoResult; @@ -332,7 +339,6 @@ public class RecursiveTaskTest extends J assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } - /** * invoke task throws exception when task completes abnormally */ @@ -344,7 +350,7 @@ public class RecursiveTaskTest extends J f.invoke(); shouldThrow(); } catch (FJException success) { - checkTaskThrew(f, success); + checkCompletedAbnormally(f, success); } return NoResult; }}; @@ -360,7 +366,7 @@ public class RecursiveTaskTest extends J FailingFibTask f = new FailingFibTask(8); f.quietlyInvoke(); assertTrue(f.getException() instanceof FJException); - checkTaskThrew(f, f.getException()); + checkCompletedAbnormally(f, f.getException()); return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -375,10 +381,10 @@ public class RecursiveTaskTest extends J FailingFibTask f = new FailingFibTask(8); assertSame(f, f.fork()); try { - Integer r = f.join(); + f.join(); shouldThrow(); } catch (FJException success) { - checkTaskThrew(f, success); + checkCompletedAbnormally(f, success); } return NoResult; }}; @@ -394,10 +400,12 @@ public class RecursiveTaskTest extends J FailingFibTask f = new FailingFibTask(8); assertSame(f, f.fork()); try { - Integer r = f.get(); + f.get(); shouldThrow(); } catch (ExecutionException success) { - checkTaskThrew(f, success.getCause()); + Throwable cause = success.getCause(); + assertTrue(cause instanceof FJException); + checkCompletedAbnormally(f, cause); } return NoResult; }}; @@ -413,10 +421,12 @@ public class RecursiveTaskTest extends J FailingFibTask f = new FailingFibTask(8); assertSame(f, f.fork()); try { - Integer r = f.get(5L, SECONDS); + f.get(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (ExecutionException success) { - checkTaskThrew(f, success.getCause()); + Throwable cause = success.getCause(); + assertTrue(cause instanceof FJException); + checkCompletedAbnormally(f, cause); } return NoResult; }}; @@ -433,7 +443,7 @@ public class RecursiveTaskTest extends J assertSame(f, f.fork()); f.quietlyJoin(); assertTrue(f.getException() instanceof FJException); - checkTaskThrew(f, f.getException()); + checkCompletedAbnormally(f, f.getException()); return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -448,7 +458,7 @@ public class RecursiveTaskTest extends J FibTask f = new FibTask(8); assertTrue(f.cancel(true)); try { - Integer r = f.invoke(); + f.invoke(); shouldThrow(); } catch (CancellationException success) { checkCancelled(f); @@ -468,7 +478,7 @@ public class RecursiveTaskTest extends J assertTrue(f.cancel(true)); assertSame(f, f.fork()); try { - Integer r = f.join(); + f.join(); shouldThrow(); } catch (CancellationException success) { checkCancelled(f); @@ -488,7 +498,7 @@ public class RecursiveTaskTest extends J assertTrue(f.cancel(true)); assertSame(f, f.fork()); try { - Integer r = f.get(); + f.get(); shouldThrow(); } catch (CancellationException success) { checkCancelled(f); @@ -508,7 +518,7 @@ public class RecursiveTaskTest extends J assertTrue(f.cancel(true)); assertSame(f, f.fork()); try { - Integer r = f.get(5L, SECONDS); + f.get(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (CancellationException success) { checkCancelled(f); @@ -577,7 +587,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()); @@ -594,7 +604,7 @@ public class RecursiveTaskTest extends J return NoResult; } }; - a.invoke(); + assertSame(NoResult, a.invoke()); } /** @@ -633,7 +643,7 @@ public class RecursiveTaskTest extends J f.invoke(); shouldThrow(); } catch (FJException success) { - checkTaskThrew(f, success); + checkCompletedAbnormally(f, success); } f.reinitialize(); checkNotDone(f); @@ -652,10 +662,10 @@ public class RecursiveTaskTest extends J FibTask f = new FibTask(8); f.completeExceptionally(new FJException()); try { - Integer r = f.invoke(); + f.invoke(); shouldThrow(); } catch (FJException success) { - checkTaskThrew(f, success); + checkCompletedAbnormally(f, success); } return NoResult; }}; @@ -687,8 +697,8 @@ public class RecursiveTaskTest extends J FibTask f = new FibTask(8); FibTask g = new FibTask(9); invokeAll(f, g); - checkCompletesNormally(f, 21); - checkCompletesNormally(g, 34); + checkCompletedNormally(f, 21); + checkCompletedNormally(g, 34); return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -702,7 +712,7 @@ public class RecursiveTaskTest extends J public Integer realCompute() { FibTask f = new FibTask(8); invokeAll(f); - checkCompletesNormally(f, 21); + checkCompletedNormally(f, 21); return NoResult; }}; assertSame(NoResult, testInvokeOnPool(mainPool(), a)); @@ -718,9 +728,12 @@ public class RecursiveTaskTest extends J FibTask g = new FibTask(9); FibTask h = new FibTask(7); invokeAll(f, g, h); - checkCompletesNormally(f, 21); - checkCompletesNormally(g, 34); - checkCompletesNormally(h, 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)); @@ -740,15 +753,17 @@ public class RecursiveTaskTest extends J set.add(g); set.add(h); invokeAll(set); - checkCompletesNormally(f, 21); - checkCompletesNormally(g, 34); - checkCompletesNormally(h, 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 */ @@ -779,7 +794,7 @@ public class RecursiveTaskTest extends J invokeAll(f, g); shouldThrow(); } catch (FJException success) { - checkTaskThrew(g, success); + checkCompletedAbnormally(g, success); } return NoResult; }}; @@ -797,7 +812,7 @@ public class RecursiveTaskTest extends J invokeAll(g); shouldThrow(); } catch (FJException success) { - checkTaskThrew(g, success); + checkCompletedAbnormally(g, success); } return NoResult; }}; @@ -817,7 +832,7 @@ public class RecursiveTaskTest extends J invokeAll(f, g, h); shouldThrow(); } catch (FJException success) { - checkTaskThrew(g, success); + checkCompletedAbnormally(g, success); } return NoResult; }}; @@ -841,7 +856,7 @@ public class RecursiveTaskTest extends J invokeAll(set); shouldThrow(); } catch (FJException success) { - checkTaskThrew(f, success); + checkCompletedAbnormally(f, success); } return NoResult; }}; @@ -883,6 +898,7 @@ 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);