--- jsr166/src/test/tck/RecursiveTaskTest.java 2011/02/22 01:18:59 1.26 +++ jsr166/src/test/tck/RecursiveTaskTest.java 2021/01/26 13:33:06 1.42 @@ -1,25 +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.ForkJoinTask; -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; + +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); @@ -40,19 +41,17 @@ 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); } } - void checkNotDone(RecursiveTask a) { + void checkNotDone(RecursiveTask a) { assertFalse(a.isDone()); assertFalse(a.isCompletedNormally()); assertFalse(a.isCompletedAbnormally()); @@ -70,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); } @@ -115,13 +116,13 @@ 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); } - void checkCancelled(RecursiveTask a) { + void checkCancelled(RecursiveTask a) { assertTrue(a.isDone()); assertTrue(a.isCancelled()); assertFalse(a.isCompletedNormally()); @@ -142,13 +143,13 @@ 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 checkCompletedAbnormally(RecursiveTask a, Throwable t) { + void checkCompletedAbnormally(RecursiveTask a, Throwable t) { assertTrue(a.isDone()); assertFalse(a.isCancelled()); assertFalse(a.isCompletedNormally()); @@ -173,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()); @@ -184,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; } @@ -197,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) { @@ -205,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; @@ -216,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(); } } @@ -294,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; @@ -319,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 @@ -329,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; @@ -337,7 +339,6 @@ public class RecursiveTaskTest extends J assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } - /** * invoke task throws exception when task completes abnormally */ @@ -380,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); @@ -399,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(); @@ -420,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(); @@ -457,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); @@ -477,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); @@ -497,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); @@ -517,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); @@ -661,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); @@ -747,7 +748,7 @@ public class RecursiveTaskTest extends J FibTask f = new FibTask(8); FibTask g = new FibTask(9); FibTask h = new FibTask(7); - HashSet set = new HashSet(); + HashSet> set = new HashSet>(); set.add(f); set.add(g); set.add(h); @@ -763,7 +764,6 @@ public class RecursiveTaskTest extends J assertSame(NoResult, testInvokeOnPool(mainPool(), a)); } - /** * invokeAll(tasks) with any null task throws NPE */ @@ -848,7 +848,7 @@ public class RecursiveTaskTest extends J FailingFibTask f = new FailingFibTask(8); FibTask g = new FibTask(9); FibTask h = new FibTask(7); - HashSet set = new HashSet(); + HashSet> set = new HashSet>(); set.add(f); set.add(g); set.add(h);