--- jsr166/src/test/tck/ExecutorCompletionServiceTest.java 2016/05/21 22:30:16 1.24 +++ jsr166/src/test/tck/ExecutorCompletionServiceTest.java 2017/12/03 17:15:21 1.29 @@ -14,10 +14,7 @@ import java.util.concurrent.CompletionSe import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorCompletionService; -import java.util.concurrent.Executor; -import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; -import java.util.concurrent.ForkJoinPool; import java.util.concurrent.Future; import java.util.concurrent.FutureTask; import java.util.concurrent.RunnableFuture; @@ -50,9 +47,8 @@ public class ExecutorCompletionServiceTe * new ExecutorCompletionService(e, null) throws NullPointerException */ public void testConstructorNPE2() { - final Executor e = ForkJoinPool.commonPool(); try { - new ExecutorCompletionService(e, null); + new ExecutorCompletionService(cachedThreadPool, null); shouldThrow(); } catch (NullPointerException success) {} } @@ -61,10 +57,9 @@ public class ExecutorCompletionServiceTe * ecs.submit(null) throws NullPointerException */ public void testSubmitNullCallable() { - final ExecutorCompletionService ecs = - new ExecutorCompletionService(ForkJoinPool.commonPool()); + CompletionService cs = new ExecutorCompletionService(cachedThreadPool); try { - ecs.submit((Callable) null); + cs.submit((Callable) null); shouldThrow(); } catch (NullPointerException success) {} } @@ -73,10 +68,9 @@ public class ExecutorCompletionServiceTe * ecs.submit(null, val) throws NullPointerException */ public void testSubmitNullRunnable() { - final ExecutorCompletionService ecs = - new ExecutorCompletionService(ForkJoinPool.commonPool()); + CompletionService cs = new ExecutorCompletionService(cachedThreadPool); try { - ecs.submit((Runnable) null, Boolean.TRUE); + cs.submit((Runnable) null, Boolean.TRUE); shouldThrow(); } catch (NullPointerException success) {} } @@ -84,12 +78,10 @@ public class ExecutorCompletionServiceTe /** * A taken submitted task is completed */ - public void testTake() - throws InterruptedException, ExecutionException { - final ExecutorCompletionService ecs = - new ExecutorCompletionService(ForkJoinPool.commonPool()); - ecs.submit(new StringTask()); - Future f = ecs.take(); + public void testTake() throws Exception { + CompletionService cs = new ExecutorCompletionService(cachedThreadPool); + cs.submit(new StringTask()); + Future f = cs.take(); assertTrue(f.isDone()); assertSame(TEST_STRING, f.get()); } @@ -98,26 +90,23 @@ public class ExecutorCompletionServiceTe * Take returns the same future object returned by submit */ public void testTake2() throws InterruptedException { - final ExecutorCompletionService ecs = - new ExecutorCompletionService(ForkJoinPool.commonPool()); - Future f1 = ecs.submit(new StringTask()); - Future f2 = ecs.take(); + CompletionService cs = new ExecutorCompletionService(cachedThreadPool); + Future f1 = cs.submit(new StringTask()); + Future f2 = cs.take(); assertSame(f1, f2); } /** * poll returns non-null when the returned task is completed */ - public void testPoll1() - throws InterruptedException, ExecutionException { - final ExecutorCompletionService ecs = - new ExecutorCompletionService(ForkJoinPool.commonPool()); - assertNull(ecs.poll()); - ecs.submit(new StringTask()); + public void testPoll1() throws Exception { + CompletionService cs = new ExecutorCompletionService(cachedThreadPool); + assertNull(cs.poll()); + cs.submit(new StringTask()); long startTime = System.nanoTime(); Future f; - while ((f = ecs.poll()) == null) { + while ((f = cs.poll()) == null) { if (millisElapsedSince(startTime) > LONG_DELAY_MS) fail("timed out"); Thread.yield(); @@ -129,16 +118,15 @@ public class ExecutorCompletionServiceTe /** * timed poll returns non-null when the returned task is completed */ - public void testPoll2() - throws InterruptedException, ExecutionException { - final ExecutorCompletionService ecs = - new ExecutorCompletionService(ForkJoinPool.commonPool()); - assertNull(ecs.poll()); - ecs.submit(new StringTask()); + public void testPoll2() throws Exception { + CompletionService cs = new ExecutorCompletionService(cachedThreadPool); + assertNull(cs.poll()); + cs.submit(new StringTask()); long startTime = System.nanoTime(); Future f; - while ((f = ecs.poll(SHORT_DELAY_MS, MILLISECONDS)) == null) { + while ((f = cs.poll(timeoutMillis(), MILLISECONDS)) == null) { + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); if (millisElapsedSince(startTime) > LONG_DELAY_MS) fail("timed out"); Thread.yield(); @@ -150,55 +138,49 @@ public class ExecutorCompletionServiceTe /** * poll returns null before the returned task is completed */ - public void testPollReturnsNull() - throws InterruptedException, ExecutionException { - final ExecutorCompletionService ecs = - new ExecutorCompletionService(ForkJoinPool.commonPool()); + public void testPollReturnsNullBeforeCompletion() throws Exception { + CompletionService cs = new ExecutorCompletionService(cachedThreadPool); final CountDownLatch proceed = new CountDownLatch(1); - ecs.submit(new Callable() { public String call() throws Exception { - proceed.await(); + cs.submit(new Callable() { public String call() throws Exception { + await(proceed); return TEST_STRING; }}); - assertNull(ecs.poll()); - assertNull(ecs.poll(0L, MILLISECONDS)); - assertNull(ecs.poll(Long.MIN_VALUE, MILLISECONDS)); + assertNull(cs.poll()); + assertNull(cs.poll(0L, MILLISECONDS)); + assertNull(cs.poll(Long.MIN_VALUE, MILLISECONDS)); long startTime = System.nanoTime(); - assertNull(ecs.poll(timeoutMillis(), MILLISECONDS)); + assertNull(cs.poll(timeoutMillis(), MILLISECONDS)); assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); proceed.countDown(); - assertSame(TEST_STRING, ecs.take().get()); + assertSame(TEST_STRING, cs.take().get()); } /** * successful and failed tasks are both returned */ - public void testTaskAssortment() - throws InterruptedException, ExecutionException { - final ExecutorService e = Executors.newCachedThreadPool(); - final CompletionService cs = new ExecutorCompletionService(e); - final ArithmeticException ex = new ArithmeticException(); - try (PoolCleaner cleaner = cleaner(e)) { - for (int i = 0; i < 2; i++) { - cs.submit(new StringTask()); - cs.submit(callableThrowing(ex)); - cs.submit(runnableThrowing(ex), null); - } - int normalCompletions = 0; - int exceptionalCompletions = 0; - for (int i = 0; i < 3 * 2; i++) { - try { - if (cs.take().get() == TEST_STRING) - normalCompletions++; - } - catch (ExecutionException expected) { - assertTrue(expected.getCause() instanceof ArithmeticException); - exceptionalCompletions++; - } + public void testTaskAssortment() throws Exception { + CompletionService cs = new ExecutorCompletionService(cachedThreadPool); + ArithmeticException ex = new ArithmeticException(); + final int rounds = 2; + for (int i = rounds; i--> 0; ) { + cs.submit(new StringTask()); + cs.submit(callableThrowing(ex)); + cs.submit(runnableThrowing(ex), null); + } + int normalCompletions = 0; + int exceptionalCompletions = 0; + for (int i = 3 * rounds; i--> 0; ) { + try { + assertSame(TEST_STRING, cs.take().get()); + normalCompletions++; + } catch (ExecutionException expected) { + assertSame(ex, expected.getCause()); + exceptionalCompletions++; } - assertEquals(2 * 1, normalCompletions); - assertEquals(2 * 2, exceptionalCompletions); - assertNull(cs.poll()); } + assertEquals(1 * rounds, normalCompletions); + assertEquals(2 * rounds, exceptionalCompletions); + assertNull(cs.poll()); } /**