--- jsr166/src/test/tck/ForkJoinTask8Test.java 2015/02/07 22:32:48 1.12 +++ jsr166/src/test/tck/ForkJoinTask8Test.java 2015/10/08 23:02:21 1.22 @@ -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); } } @@ -125,17 +126,17 @@ public class ForkJoinTask8Test extends J { Thread.currentThread().interrupt(); - long t0 = System.nanoTime(); + long startTime = System.nanoTime(); assertSame(expected, a.join()); - assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); + assertTrue(millisElapsedSince(startTime) < SMALL_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) < SMALL_DELAY_MS); Thread.interrupted(); } @@ -171,9 +172,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) < SMALL_DELAY_MS); } try { @@ -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); @@ -316,6 +317,7 @@ public class ForkJoinTask8Test extends J } catch (Throwable ex) { compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE); + throw new Error(ex); } return false; } @@ -873,21 +875,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 +898,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 +938,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 +966,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 +1153,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); + } + } + }