--- jsr166/src/test/tck/ForkJoinTask8Test.java 2015/02/07 22:32:48 1.12 +++ jsr166/src/test/tck/ForkJoinTask8Test.java 2015/10/05 22:59:29 1.19 @@ -8,9 +8,12 @@ import static java.util.concurrent.TimeU import static java.util.concurrent.TimeUnit.SECONDS; import java.util.Arrays; +import java.util.Collections; +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 +37,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 +74,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,8 +90,6 @@ public class ForkJoinTask8Test extends J assertFalse(a.isCancelled()); assertNull(a.getException()); assertNull(a.getRawResult()); - } finally { - joinPool(pool); } } @@ -197,9 +198,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); @@ -873,21 +874,20 @@ public class ForkJoinTask8Test extends J } /** - * 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 +897,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 }; + Collections.shuffle(Arrays.asList(tasks)); try { - invokeAll(g); + invokeAll(tasks[0], tasks[1]); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(g, success); @@ -934,8 +937,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 }; + Collections.shuffle(Arrays.asList(tasks)); try { - invokeAll(f, g, h); + invokeAll(tasks[0], tasks[1], tasks[2]); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(g, success); @@ -960,6 +965,7 @@ public class ForkJoinTask8Test extends J AsyncFib g = new AsyncFib(9); AsyncFib h = new AsyncFib(7); ForkJoinTask[] tasks = { f, g, h }; + Collections.shuffle(Arrays.asList(tasks)); try { invokeAll(Arrays.asList(tasks)); shouldThrow(); @@ -1146,4 +1152,41 @@ 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)) { + 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); + done.countDown(); + } + } + }