--- jsr166/src/test/tck/RecursiveTaskTest.java 2015/10/07 00:15:23 1.34 +++ jsr166/src/test/tck/RecursiveTaskTest.java 2019/12/16 22:55:54 1.41 @@ -4,7 +4,7 @@ * http://creativecommons.org/publicdomain/zero/1.0/ */ -import static java.util.concurrent.TimeUnit.SECONDS; +import static java.util.concurrent.TimeUnit.MILLISECONDS; import java.util.HashSet; import java.util.concurrent.CancellationException; @@ -69,44 +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()); + 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()); - } catch (Throwable fail) { threadUnexpectedException(fail); } - 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); } @@ -114,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); } @@ -141,7 +143,7 @@ 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); } @@ -172,7 +174,7 @@ public class RecursiveTaskTest extends J } catch (Throwable fail) { threadUnexpectedException(fail); } try { - a.get(5L, SECONDS); + a.get(randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (ExecutionException success) { assertSame(t.getClass(), success.getCause().getClass()); @@ -183,10 +185,10 @@ public class RecursiveTaskTest extends J 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; } @@ -196,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) { @@ -204,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; @@ -215,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(); } } @@ -293,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; @@ -379,7 +381,7 @@ 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) { checkCompletedAbnormally(f, success); @@ -398,7 +400,7 @@ 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) { Throwable cause = success.getCause(); @@ -419,7 +421,7 @@ 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) { Throwable cause = success.getCause(); @@ -456,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); @@ -476,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); @@ -496,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); @@ -516,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); @@ -660,7 +662,7 @@ 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) { checkCompletedAbnormally(f, success);