--- jsr166/src/test/tck/ForkJoinTask8Test.java 2015/02/07 22:32:48 1.12 +++ jsr166/src/test/tck/ForkJoinTask8Test.java 2021/01/26 13:33:06 1.35 @@ -5,12 +5,13 @@ */ import static java.util.concurrent.TimeUnit.MILLISECONDS; -import static java.util.concurrent.TimeUnit.SECONDS; import java.util.Arrays; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; +import java.util.concurrent.ForkJoinWorkerThread; import java.util.concurrent.RecursiveAction; import java.util.concurrent.TimeoutException; @@ -34,7 +35,7 @@ public class ForkJoinTask8Test extends J static final short EXCEPTION_STATE = 1; public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { @@ -71,7 +72,7 @@ public class ForkJoinTask8Test extends J } private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { - try { + try (PoolCleaner cleaner = cleaner(pool)) { assertFalse(a.isDone()); assertFalse(a.isCompletedNormally()); assertFalse(a.isCompletedAbnormally()); @@ -87,12 +88,10 @@ public class ForkJoinTask8Test extends J assertFalse(a.isCancelled()); assertNull(a.getException()); assertNull(a.getRawResult()); - } finally { - joinPool(pool); } } - void checkNotDone(ForkJoinTask a) { + void checkNotDone(ForkJoinTask a) { assertFalse(a.isDone()); assertFalse(a.isCompletedNormally()); assertFalse(a.isCompletedAbnormally()); @@ -100,10 +99,11 @@ public class ForkJoinTask8Test extends J assertNull(a.getException()); assertNull(a.getRawResult()); if (a instanceof BinaryAsyncAction) - assertTrue(((BinaryAsyncAction)a).getForkJoinTaskTag() == INITIAL_STATE); + assertEquals(INITIAL_STATE, + ((BinaryAsyncAction)a).getForkJoinTaskTag()); try { - a.get(0L, SECONDS); + a.get(randomExpiredTimeout(), randomTimeUnit()); shouldThrow(); } catch (TimeoutException success) { } catch (Throwable fail) { threadUnexpectedException(fail); } @@ -113,43 +113,46 @@ public class ForkJoinTask8Test extends J checkCompletedNormally(a, null); } - void checkCompletedNormally(ForkJoinTask a, T expected) { + void checkCompletedNormally(ForkJoinTask a, T expectedValue) { assertTrue(a.isDone()); assertFalse(a.isCancelled()); assertTrue(a.isCompletedNormally()); assertFalse(a.isCompletedAbnormally()); assertNull(a.getException()); - assertSame(expected, a.getRawResult()); + assertSame(expectedValue, a.getRawResult()); if (a instanceof BinaryAsyncAction) - assertTrue(((BinaryAsyncAction)a).getForkJoinTaskTag() == COMPLETE_STATE); + assertEquals(COMPLETE_STATE, + ((BinaryAsyncAction)a).getForkJoinTaskTag()); { Thread.currentThread().interrupt(); - long t0 = System.nanoTime(); - assertSame(expected, a.join()); - assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); + long startTime = System.nanoTime(); + assertSame(expectedValue, a.join()); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); Thread.interrupted(); } { Thread.currentThread().interrupt(); - long t0 = System.nanoTime(); + long startTime = System.nanoTime(); a.quietlyJoin(); // should be no-op - assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); Thread.interrupted(); } 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); } - void checkCompletedAbnormally(ForkJoinTask a, Throwable t) { + void checkCompletedAbnormally(ForkJoinTask a, Throwable t) { assertTrue(a.isDone()); assertFalse(a.isCancelled()); assertFalse(a.isCompletedNormally()); @@ -171,9 +174,9 @@ public class ForkJoinTask8Test extends J Thread.interrupted(); { - long t0 = System.nanoTime(); + long startTime = System.nanoTime(); a.quietlyJoin(); // should be no-op - assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } try { @@ -184,7 +187,7 @@ public class ForkJoinTask8Test 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()); @@ -197,9 +200,9 @@ public class ForkJoinTask8Test extends J abstract static class BinaryAsyncAction extends ForkJoinTask { - private BinaryAsyncAction parent; + private volatile BinaryAsyncAction parent; - private BinaryAsyncAction sibling; + private volatile BinaryAsyncAction sibling; protected BinaryAsyncAction() { setForkJoinTaskTag(INITIAL_STATE); @@ -242,6 +245,14 @@ public class ForkJoinTask8Test extends J super.completeExceptionally(ex); } + public boolean cancel(boolean mayInterruptIfRunning) { + if (super.cancel(mayInterruptIfRunning)) { + completeExceptionally(new FJException()); + return true; + } + return false; + } + public final void complete() { BinaryAsyncAction a = this; for (;;) { @@ -264,13 +275,12 @@ public class ForkJoinTask8Test extends J } public final void completeExceptionally(Throwable ex) { - BinaryAsyncAction a = this; - while (!a.isCompletedAbnormally()) { + for (BinaryAsyncAction a = this;;) { a.completeThisExceptionally(ex); BinaryAsyncAction s = a.sibling; - if (s != null) - s.cancel(false); - if (!a.onException() || (a = a.parent) == null) + if (s != null && !s.isDone()) + s.completeExceptionally(ex); + if ((a = a.parent) == null) break; } } @@ -302,21 +312,20 @@ public class ForkJoinTask8Test extends J try { AsyncFib f = this; int n = f.number; - if (n > 1) { - while (n > 1) { - AsyncFib p = f; - AsyncFib r = new AsyncFib(n - 2); - f = new AsyncFib(--n); - p.linkSubtasks(r, f); - r.fork(); - } - f.number = n; + while (n > 1) { + AsyncFib p = f; + AsyncFib r = new AsyncFib(n - 2); + f = new AsyncFib(--n); + p.linkSubtasks(r, f); + r.fork(); } f.complete(); } catch (Throwable ex) { compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE); } + if (getForkJoinTaskTag() == EXCEPTION_STATE) + throw new FJException(); return false; } @@ -338,9 +347,9 @@ public class ForkJoinTask8Test extends J } public final boolean exec() { - FailingAsyncFib f = this; - int n = f.number; - if (n > 1) { + try { + FailingAsyncFib f = this; + int n = f.number; while (n > 1) { FailingAsyncFib p = f; FailingAsyncFib r = new FailingAsyncFib(n - 2); @@ -348,9 +357,13 @@ public class ForkJoinTask8Test extends J p.linkSubtasks(r, f); r.fork(); } - f.number = n; + f.complete(); + } + catch (Throwable ex) { + compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE); } - f.complete(); + if (getForkJoinTaskTag() == EXCEPTION_STATE) + throw new FJException(); return false; } @@ -476,7 +489,7 @@ public class ForkJoinTask8Test extends J AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); try { - f.get(5L, null); + f.get(randomTimeout(), null); shouldThrow(); } catch (NullPointerException success) {} }}; @@ -519,6 +532,8 @@ public class ForkJoinTask8Test extends J AsyncFib f = new AsyncFib(8); assertSame(f, f.fork()); helpQuiesce(); + while (!f.isDone()) // wait out race + ; assertEquals(0, getQueuedTaskCount()); f.checkCompletedNormally(); }}; @@ -860,34 +875,32 @@ public class ForkJoinTask8Test extends J RecursiveAction a = new CheckedRecursiveAction() { protected void realCompute() { AsyncFib nul = null; - Runnable[] throwingActions = { + assertThrows( + NullPointerException.class, () -> invokeAll(nul), () -> invokeAll(nul, nul), () -> invokeAll(new AsyncFib(8), new AsyncFib(9), nul), () -> invokeAll(new AsyncFib(8), nul, new AsyncFib(9)), - () -> invokeAll(nul, new AsyncFib(8), new AsyncFib(9)), - }; - assertThrows(NullPointerException.class, throwingActions); + () -> invokeAll(nul, new AsyncFib(8), new AsyncFib(9))); }}; testInvokeOnPool(pool, a); } /** - * invokeAll(t1, t2) throw exception if any task does + * invokeAll(tasks) with 1 argument throws exception if task does */ - public void testAbnormalInvokeAll2() { - testAbnormalInvokeAll2(mainPool()); + public void testAbnormalInvokeAll1() { + testAbnormalInvokeAll1(mainPool()); } - public void testAbnormalInvokeAll2_Singleton() { - testAbnormalInvokeAll2(singletonPool()); + public void testAbnormalInvokeAll1_Singleton() { + testAbnormalInvokeAll1(singletonPool()); } - public void testAbnormalInvokeAll2(ForkJoinPool pool) { + public void testAbnormalInvokeAll1(ForkJoinPool pool) { RecursiveAction a = new CheckedRecursiveAction() { protected void realCompute() { - AsyncFib f = new AsyncFib(8); FailingAsyncFib g = new FailingAsyncFib(9); try { - invokeAll(f, g); + invokeAll(g); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(g, success); @@ -897,20 +910,23 @@ public class ForkJoinTask8Test extends J } /** - * invokeAll(tasks) with 1 argument throws exception if task does + * invokeAll(t1, t2) throw exception if any task does */ - public void testAbnormalInvokeAll1() { - testAbnormalInvokeAll1(mainPool()); + public void testAbnormalInvokeAll2() { + testAbnormalInvokeAll2(mainPool()); } - public void testAbnormalInvokeAll1_Singleton() { - testAbnormalInvokeAll1(singletonPool()); + public void testAbnormalInvokeAll2_Singleton() { + testAbnormalInvokeAll2(singletonPool()); } - public void testAbnormalInvokeAll1(ForkJoinPool pool) { + public void testAbnormalInvokeAll2(ForkJoinPool pool) { RecursiveAction a = new CheckedRecursiveAction() { protected void realCompute() { + AsyncFib f = new AsyncFib(8); FailingAsyncFib g = new FailingAsyncFib(9); + ForkJoinTask[] tasks = { f, g }; + shuffle(tasks); try { - invokeAll(g); + invokeAll(tasks[0], tasks[1]); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(g, success); @@ -934,8 +950,10 @@ public class ForkJoinTask8Test extends J AsyncFib f = new AsyncFib(8); FailingAsyncFib g = new FailingAsyncFib(9); AsyncFib h = new AsyncFib(7); + ForkJoinTask[] tasks = { f, g, h }; + shuffle(tasks); try { - invokeAll(f, g, h); + invokeAll(tasks[0], tasks[1], tasks[2]); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(g, success); @@ -959,7 +977,8 @@ public class ForkJoinTask8Test extends J FailingAsyncFib f = new FailingAsyncFib(8); AsyncFib g = new AsyncFib(9); AsyncFib h = new AsyncFib(7); - ForkJoinTask[] tasks = { f, g, h }; + ForkJoinTask[] tasks = { f, g, h }; + shuffle(tasks); try { invokeAll(Arrays.asList(tasks)); shouldThrow(); @@ -1146,4 +1165,40 @@ public class ForkJoinTask8Test extends J testInvokeOnPool(mainPool(), a); } + // jdk9 + + /** + * pollSubmission returns unexecuted submitted task, if present + */ + public void testPollSubmission() { + final CountDownLatch done = new CountDownLatch(1); + final ForkJoinTask a = ForkJoinTask.adapt(awaiter(done)); + final ForkJoinTask b = ForkJoinTask.adapt(awaiter(done)); + final ForkJoinTask c = ForkJoinTask.adapt(awaiter(done)); + final ForkJoinPool p = singletonPool(); + try (PoolCleaner cleaner = cleaner(p, done)) { + Thread external = new Thread(new CheckedRunnable() { + public void realRun() { + p.execute(a); + p.execute(b); + p.execute(c); + }}); + RecursiveAction s = new CheckedRecursiveAction() { + protected void realCompute() { + external.start(); + try { + external.join(); + } catch (Exception ex) { + threadUnexpectedException(ex); + } + assertTrue(p.hasQueuedSubmissions()); + assertTrue(Thread.currentThread() instanceof ForkJoinWorkerThread); + ForkJoinTask r = ForkJoinTask.pollSubmission(); + assertTrue(r == a || r == b || r == c); + assertFalse(r.isDone()); + }}; + p.invoke(s); + } + } + }