--- jsr166/src/test/tck/AbstractExecutorServiceTest.java 2014/12/31 19:05:42 1.35 +++ jsr166/src/test/tck/AbstractExecutorServiceTest.java 2015/10/08 03:08:37 1.44 @@ -16,6 +16,7 @@ import java.util.List; import java.util.concurrent.AbstractExecutorService; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.Callable; +import java.util.concurrent.CancellationException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executors; @@ -30,7 +31,7 @@ import junit.framework.TestSuite; public class AbstractExecutorServiceTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(AbstractExecutorServiceTest.class); @@ -167,8 +168,8 @@ public class AbstractExecutorServiceTest * execute(null runnable) throws NPE */ public void testExecuteNullRunnable() { + ExecutorService e = new DirectExecutorService(); try { - ExecutorService e = new DirectExecutorService(); e.submit((Runnable) null); shouldThrow(); } catch (NullPointerException success) {} @@ -178,8 +179,8 @@ public class AbstractExecutorServiceTest * submit(null callable) throws NPE */ public void testSubmitNullCallable() { + ExecutorService e = new DirectExecutorService(); try { - ExecutorService e = new DirectExecutorService(); e.submit((Callable) null); shouldThrow(); } catch (NullPointerException success) {} @@ -191,28 +192,25 @@ public class AbstractExecutorServiceTest public void testInterruptedSubmit() throws InterruptedException { final CountDownLatch submitted = new CountDownLatch(1); final CountDownLatch quittingTime = new CountDownLatch(1); - final ExecutorService p - = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, - new ArrayBlockingQueue(10)); final Callable awaiter = new CheckedCallable() { public Void realCall() throws InterruptedException { - quittingTime.await(); + assertTrue(quittingTime.await(2*LONG_DELAY_MS, MILLISECONDS)); return null; }}; - try { - Thread t = new Thread(new CheckedInterruptedRunnable() { + final ExecutorService p + = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, + new ArrayBlockingQueue(10)); + try (PoolCleaner cleaner = cleaner(p, quittingTime)) { + Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws Exception { Future future = p.submit(awaiter); submitted.countDown(); future.get(); }}); - t.start(); - submitted.await(); + + await(submitted); t.interrupt(); - t.join(); - } finally { - quittingTime.countDown(); - joinPool(p); + awaitTermination(t); } } @@ -221,34 +219,32 @@ public class AbstractExecutorServiceTest * throws exception */ public void testSubmitEE() throws InterruptedException { - ThreadPoolExecutor p = + final ThreadPoolExecutor p = new ThreadPoolExecutor(1, 1, 60, TimeUnit.SECONDS, new ArrayBlockingQueue(10)); - - Callable c = new Callable() { - public Object call() { throw new ArithmeticException(); }}; - - try { - p.submit(c).get(); - shouldThrow(); - } catch (ExecutionException success) { - assertTrue(success.getCause() instanceof ArithmeticException); + try (PoolCleaner cleaner = cleaner(p)) { + Callable c = new Callable() { + public Object call() { throw new ArithmeticException(); }}; + try { + p.submit(c).get(); + shouldThrow(); + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof ArithmeticException); + } } - joinPool(p); } /** * invokeAny(null) throws NPE */ public void testInvokeAny1() throws Exception { - ExecutorService e = new DirectExecutorService(); - try { - e.invokeAny(null); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + final ExecutorService e = new DirectExecutorService(); + try (PoolCleaner cleaner = cleaner(e)) { + try { + e.invokeAny(null); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -256,13 +252,12 @@ public class AbstractExecutorServiceTest * invokeAny(empty collection) throws IAE */ public void testInvokeAny2() throws Exception { - ExecutorService e = new DirectExecutorService(); - try { - e.invokeAny(new ArrayList>()); - shouldThrow(); - } catch (IllegalArgumentException success) { - } finally { - joinPool(e); + final ExecutorService e = new DirectExecutorService(); + try (PoolCleaner cleaner = cleaner(e)) { + try { + e.invokeAny(new ArrayList>()); + shouldThrow(); + } catch (IllegalArgumentException success) {} } } @@ -270,17 +265,16 @@ public class AbstractExecutorServiceTest * invokeAny(c) throws NPE if c has null elements */ public void testInvokeAny3() throws Exception { - ExecutorService e = new DirectExecutorService(); - List> l = new ArrayList>(); - l.add(new Callable() { - public Long call() { throw new ArithmeticException(); }}); - l.add(null); - try { - e.invokeAny(l); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + final ExecutorService e = new DirectExecutorService(); + try (PoolCleaner cleaner = cleaner(e)) { + List> l = new ArrayList>(); + l.add(new Callable() { + public Long call() { throw new ArithmeticException(); }}); + l.add(null); + try { + e.invokeAny(l); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -288,16 +282,16 @@ public class AbstractExecutorServiceTest * invokeAny(c) throws ExecutionException if no task in c completes */ public void testInvokeAny4() throws InterruptedException { - ExecutorService e = new DirectExecutorService(); - List> l = new ArrayList>(); - l.add(new NPETask()); - try { - e.invokeAny(l); - shouldThrow(); - } catch (ExecutionException success) { - assertTrue(success.getCause() instanceof NullPointerException); - } finally { - joinPool(e); + final ExecutorService e = new DirectExecutorService(); + try (PoolCleaner cleaner = cleaner(e)) { + List> l = new ArrayList>(); + l.add(new NPETask()); + try { + e.invokeAny(l); + shouldThrow(); + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof NullPointerException); + } } } @@ -305,15 +299,13 @@ public class AbstractExecutorServiceTest * invokeAny(c) returns result of some task in c if at least one completes */ public void testInvokeAny5() throws Exception { - ExecutorService e = new DirectExecutorService(); - try { + final ExecutorService e = new DirectExecutorService(); + try (PoolCleaner cleaner = cleaner(e)) { List> l = new ArrayList>(); l.add(new StringTask()); l.add(new StringTask()); String result = e.invokeAny(l); assertSame(TEST_STRING, result); - } finally { - joinPool(e); } } @@ -321,13 +313,12 @@ public class AbstractExecutorServiceTest * invokeAll(null) throws NPE */ public void testInvokeAll1() throws InterruptedException { - ExecutorService e = new DirectExecutorService(); - try { - e.invokeAll(null); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + final ExecutorService e = new DirectExecutorService(); + try (PoolCleaner cleaner = cleaner(e)) { + try { + e.invokeAll(null); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -335,12 +326,10 @@ public class AbstractExecutorServiceTest * invokeAll(empty collection) returns empty collection */ public void testInvokeAll2() throws InterruptedException { - ExecutorService e = new DirectExecutorService(); - try { + final ExecutorService e = new DirectExecutorService(); + try (PoolCleaner cleaner = cleaner(e)) { List> r = e.invokeAll(new ArrayList>()); assertTrue(r.isEmpty()); - } finally { - joinPool(e); } } @@ -348,16 +337,15 @@ public class AbstractExecutorServiceTest * invokeAll(c) throws NPE if c has null elements */ public void testInvokeAll3() throws InterruptedException { - ExecutorService e = new DirectExecutorService(); - List> l = new ArrayList>(); - l.add(new StringTask()); - l.add(null); - try { - e.invokeAll(l); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + final ExecutorService e = new DirectExecutorService(); + try (PoolCleaner cleaner = cleaner(e)) { + List> l = new ArrayList>(); + l.add(new StringTask()); + l.add(null); + try { + e.invokeAll(l); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -365,8 +353,8 @@ public class AbstractExecutorServiceTest * get of returned element of invokeAll(c) throws exception on failed task */ public void testInvokeAll4() throws Exception { - ExecutorService e = new DirectExecutorService(); - try { + final ExecutorService e = new DirectExecutorService(); + try (PoolCleaner cleaner = cleaner(e)) { List> l = new ArrayList>(); l.add(new NPETask()); List> futures = e.invokeAll(l); @@ -377,8 +365,6 @@ public class AbstractExecutorServiceTest } catch (ExecutionException success) { assertTrue(success.getCause() instanceof NullPointerException); } - } finally { - joinPool(e); } } @@ -386,8 +372,8 @@ public class AbstractExecutorServiceTest * invokeAll(c) returns results of all completed tasks in c */ public void testInvokeAll5() throws Exception { - ExecutorService e = new DirectExecutorService(); - try { + final ExecutorService e = new DirectExecutorService(); + try (PoolCleaner cleaner = cleaner(e)) { List> l = new ArrayList>(); l.add(new StringTask()); l.add(new StringTask()); @@ -395,8 +381,6 @@ public class AbstractExecutorServiceTest assertEquals(2, futures.size()); for (Future future : futures) assertSame(TEST_STRING, future.get()); - } finally { - joinPool(e); } } @@ -404,13 +388,12 @@ public class AbstractExecutorServiceTest * timed invokeAny(null) throws NPE */ public void testTimedInvokeAny1() throws Exception { - ExecutorService e = new DirectExecutorService(); - try { - e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + final ExecutorService e = new DirectExecutorService(); + try (PoolCleaner cleaner = cleaner(e)) { + try { + e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -418,15 +401,14 @@ public class AbstractExecutorServiceTest * timed invokeAny(null time unit) throws NPE */ public void testTimedInvokeAnyNullTimeUnit() throws Exception { - ExecutorService e = new DirectExecutorService(); - List> l = new ArrayList>(); - l.add(new StringTask()); - try { - e.invokeAny(l, MEDIUM_DELAY_MS, null); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + final ExecutorService e = new DirectExecutorService(); + try (PoolCleaner cleaner = cleaner(e)) { + List> l = new ArrayList>(); + l.add(new StringTask()); + try { + e.invokeAny(l, MEDIUM_DELAY_MS, null); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -434,13 +416,13 @@ public class AbstractExecutorServiceTest * timed invokeAny(empty collection) throws IAE */ public void testTimedInvokeAny2() throws Exception { - ExecutorService e = new DirectExecutorService(); - try { - e.invokeAny(new ArrayList>(), MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (IllegalArgumentException success) { - } finally { - joinPool(e); + final ExecutorService e = new DirectExecutorService(); + try (PoolCleaner cleaner = cleaner(e)) { + try { + e.invokeAny(new ArrayList>(), + MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (IllegalArgumentException success) {} } } @@ -448,17 +430,16 @@ public class AbstractExecutorServiceTest * timed invokeAny(c) throws NPE if c has null elements */ public void testTimedInvokeAny3() throws Exception { - ExecutorService e = new DirectExecutorService(); - List> l = new ArrayList>(); - l.add(new Callable() { - public Long call() { throw new ArithmeticException(); }}); - l.add(null); - try { - e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + final ExecutorService e = new DirectExecutorService(); + try (PoolCleaner cleaner = cleaner(e)) { + List> l = new ArrayList>(); + l.add(new Callable() { + public Long call() { throw new ArithmeticException(); }}); + l.add(null); + try { + e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -466,16 +447,18 @@ public class AbstractExecutorServiceTest * timed invokeAny(c) throws ExecutionException if no task completes */ public void testTimedInvokeAny4() throws Exception { - ExecutorService e = new DirectExecutorService(); - List> l = new ArrayList>(); - l.add(new NPETask()); - try { - e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (ExecutionException success) { - assertTrue(success.getCause() instanceof NullPointerException); - } finally { - joinPool(e); + final ExecutorService e = new DirectExecutorService(); + try (PoolCleaner cleaner = cleaner(e)) { + long startTime = System.nanoTime(); + List> l = new ArrayList>(); + l.add(new NPETask()); + try { + e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof NullPointerException); + } + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } } @@ -483,15 +466,15 @@ public class AbstractExecutorServiceTest * timed invokeAny(c) returns result of some task in c */ public void testTimedInvokeAny5() throws Exception { - ExecutorService e = new DirectExecutorService(); - try { + final ExecutorService e = new DirectExecutorService(); + try (PoolCleaner cleaner = cleaner(e)) { + long startTime = System.nanoTime(); List> l = new ArrayList>(); l.add(new StringTask()); l.add(new StringTask()); - String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); + String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); assertSame(TEST_STRING, result); - } finally { - joinPool(e); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } } @@ -499,13 +482,12 @@ public class AbstractExecutorServiceTest * timed invokeAll(null) throws NPE */ public void testTimedInvokeAll1() throws InterruptedException { - ExecutorService e = new DirectExecutorService(); - try { - e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + final ExecutorService e = new DirectExecutorService(); + try (PoolCleaner cleaner = cleaner(e)) { + try { + e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -513,15 +495,14 @@ public class AbstractExecutorServiceTest * timed invokeAll(null time unit) throws NPE */ public void testTimedInvokeAllNullTimeUnit() throws InterruptedException { - ExecutorService e = new DirectExecutorService(); - List> l = new ArrayList>(); - l.add(new StringTask()); - try { - e.invokeAll(l, MEDIUM_DELAY_MS, null); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + final ExecutorService e = new DirectExecutorService(); + try (PoolCleaner cleaner = cleaner(e)) { + List> l = new ArrayList>(); + l.add(new StringTask()); + try { + e.invokeAll(l, MEDIUM_DELAY_MS, null); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -529,12 +510,10 @@ public class AbstractExecutorServiceTest * timed invokeAll(empty collection) returns empty collection */ public void testTimedInvokeAll2() throws InterruptedException { - ExecutorService e = new DirectExecutorService(); - try { + final ExecutorService e = new DirectExecutorService(); + try (PoolCleaner cleaner = cleaner(e)) { List> r = e.invokeAll(new ArrayList>(), MEDIUM_DELAY_MS, MILLISECONDS); assertTrue(r.isEmpty()); - } finally { - joinPool(e); } } @@ -542,16 +521,15 @@ public class AbstractExecutorServiceTest * timed invokeAll(c) throws NPE if c has null elements */ public void testTimedInvokeAll3() throws InterruptedException { - ExecutorService e = new DirectExecutorService(); - List> l = new ArrayList>(); - l.add(new StringTask()); - l.add(null); - try { - e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + final ExecutorService e = new DirectExecutorService(); + try (PoolCleaner cleaner = cleaner(e)) { + List> l = new ArrayList>(); + l.add(new StringTask()); + l.add(null); + try { + e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -559,12 +537,12 @@ public class AbstractExecutorServiceTest * get of returned element of invokeAll(c) throws exception on failed task */ public void testTimedInvokeAll4() throws Exception { - ExecutorService e = new DirectExecutorService(); - try { + final ExecutorService e = new DirectExecutorService(); + try (PoolCleaner cleaner = cleaner(e)) { List> l = new ArrayList>(); l.add(new NPETask()); List> futures = - e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); + e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); assertEquals(1, futures.size()); try { futures.get(0).get(); @@ -572,8 +550,6 @@ public class AbstractExecutorServiceTest } catch (ExecutionException success) { assertTrue(success.getCause() instanceof NullPointerException); } - } finally { - joinPool(e); } } @@ -581,41 +557,51 @@ public class AbstractExecutorServiceTest * timed invokeAll(c) returns results of all completed tasks in c */ public void testTimedInvokeAll5() throws Exception { - ExecutorService e = new DirectExecutorService(); - try { + final ExecutorService e = new DirectExecutorService(); + try (PoolCleaner cleaner = cleaner(e)) { List> l = new ArrayList>(); l.add(new StringTask()); l.add(new StringTask()); List> futures = - e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); + e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); assertEquals(2, futures.size()); for (Future future : futures) assertSame(TEST_STRING, future.get()); - } finally { - joinPool(e); } } /** * timed invokeAll cancels tasks not completed by timeout */ - public void testTimedInvokeAll6() throws InterruptedException { - ExecutorService e = new DirectExecutorService(); - try { - List> l = new ArrayList>(); - l.add(new StringTask()); - l.add(Executors.callable(possiblyInterruptedRunnable(2 * SHORT_DELAY_MS), TEST_STRING)); - l.add(new StringTask()); - List> futures = - e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); - assertEquals(l.size(), futures.size()); - for (Future future : futures) - assertTrue(future.isDone()); - assertFalse(futures.get(0).isCancelled()); - assertFalse(futures.get(1).isCancelled()); - assertTrue(futures.get(2).isCancelled()); - } finally { - joinPool(e); + public void testTimedInvokeAll6() throws Exception { + final ExecutorService e = new DirectExecutorService(); + try (PoolCleaner cleaner = cleaner(e)) { + for (long timeout = timeoutMillis();;) { + List> tasks = new ArrayList<>(); + tasks.add(new StringTask("0")); + tasks.add(Executors.callable(possiblyInterruptedRunnable(timeout), + TEST_STRING)); + tasks.add(new StringTask("2")); + long startTime = System.nanoTime(); + List> futures = + e.invokeAll(tasks, timeout, MILLISECONDS); + assertEquals(tasks.size(), futures.size()); + assertTrue(millisElapsedSince(startTime) >= timeout); + for (Future future : futures) + assertTrue(future.isDone()); + try { + assertEquals("0", futures.get(0).get()); + assertEquals(TEST_STRING, futures.get(1).get()); + } catch (CancellationException retryWithLongerTimeout) { + // unusual delay before starting second task + timeout *= 2; + if (timeout >= LONG_DELAY_MS / 2) + fail("expected exactly one task to be cancelled"); + continue; + } + assertTrue(futures.get(2).isCancelled()); + break; + } } }