--- jsr166/src/test/tck/ScheduledExecutorTest.java 2015/10/04 02:15:08 1.61 +++ jsr166/src/test/tck/ScheduledExecutorTest.java 2015/10/04 08:07:31 1.62 @@ -43,16 +43,12 @@ public class ScheduledExecutorTest exten */ public void testExecute() throws InterruptedException { ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - final CountDownLatch done = new CountDownLatch(1); - final Runnable task = new CheckedRunnable() { - public void realRun() { - done.countDown(); - }}; - try { + try (PoolCleaner cleaner = cleaner(p)) { + final CountDownLatch done = new CountDownLatch(1); + final Runnable task = new CheckedRunnable() { + public void realRun() { done.countDown(); }}; p.execute(task); assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); - } finally { - joinPool(p); } } @@ -61,9 +57,9 @@ public class ScheduledExecutorTest exten */ public void testSchedule1() throws Exception { ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - final long startTime = System.nanoTime(); - final CountDownLatch done = new CountDownLatch(1); - try { + try (PoolCleaner cleaner = cleaner(p)) { + final long startTime = System.nanoTime(); + final CountDownLatch done = new CountDownLatch(1); Callable task = new CheckedCallable() { public Boolean realCall() { done.countDown(); @@ -74,8 +70,6 @@ public class ScheduledExecutorTest exten assertSame(Boolean.TRUE, f.get()); assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); assertTrue(done.await(0L, MILLISECONDS)); - } finally { - joinPool(p); } } @@ -84,9 +78,9 @@ public class ScheduledExecutorTest exten */ public void testSchedule3() throws Exception { ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - final long startTime = System.nanoTime(); - final CountDownLatch done = new CountDownLatch(1); - try { + try (PoolCleaner cleaner = cleaner(p)) { + final long startTime = System.nanoTime(); + final CountDownLatch done = new CountDownLatch(1); Runnable task = new CheckedRunnable() { public void realRun() { done.countDown(); @@ -96,8 +90,6 @@ public class ScheduledExecutorTest exten await(done); assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); - } finally { - joinPool(p); } } @@ -106,9 +98,9 @@ public class ScheduledExecutorTest exten */ public void testSchedule4() throws Exception { ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - final long startTime = System.nanoTime(); - final CountDownLatch done = new CountDownLatch(1); - try { + try (PoolCleaner cleaner = cleaner(p)) { + final long startTime = System.nanoTime(); + final CountDownLatch done = new CountDownLatch(1); Runnable task = new CheckedRunnable() { public void realRun() { done.countDown(); @@ -120,8 +112,6 @@ public class ScheduledExecutorTest exten await(done); assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); f.cancel(true); - } finally { - joinPool(p); } } @@ -130,9 +120,9 @@ public class ScheduledExecutorTest exten */ public void testSchedule5() throws Exception { ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - final long startTime = System.nanoTime(); - final CountDownLatch done = new CountDownLatch(1); - try { + try (PoolCleaner cleaner = cleaner(p)) { + final long startTime = System.nanoTime(); + final CountDownLatch done = new CountDownLatch(1); Runnable task = new CheckedRunnable() { public void realRun() { done.countDown(); @@ -144,8 +134,6 @@ public class ScheduledExecutorTest exten await(done); assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); f.cancel(true); - } finally { - joinPool(p); } } @@ -159,7 +147,7 @@ public class ScheduledExecutorTest exten */ public void testFixedRateSequence() throws InterruptedException { ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - try { + try (PoolCleaner cleaner = cleaner(p)) { for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { long startTime = System.nanoTime(); int cycles = 10; @@ -177,8 +165,6 @@ public class ScheduledExecutorTest exten return; } throw new AssertionError("unexpected execution rate"); - } finally { - joinPool(p); } } @@ -187,7 +173,7 @@ public class ScheduledExecutorTest exten */ public void testFixedDelaySequence() throws InterruptedException { ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - try { + try (PoolCleaner cleaner = cleaner(p)) { for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { long startTime = System.nanoTime(); int cycles = 10; @@ -205,8 +191,6 @@ public class ScheduledExecutorTest exten return; } throw new AssertionError("unexpected execution rate"); - } finally { - joinPool(p); } } @@ -214,108 +198,107 @@ public class ScheduledExecutorTest exten * execute(null) throws NPE */ public void testExecuteNull() throws InterruptedException { - ScheduledThreadPoolExecutor se = null; - try { - se = new ScheduledThreadPoolExecutor(1); - se.execute(null); - shouldThrow(); - } catch (NullPointerException success) {} - - joinPool(se); + ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); + try (PoolCleaner cleaner = cleaner(p)) { + try { + p.execute(null); + shouldThrow(); + } catch (NullPointerException success) {} + } } /** * schedule(null) throws NPE */ public void testScheduleNull() throws InterruptedException { - ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); - try { - TrackedCallable callable = null; - Future f = se.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (NullPointerException success) {} - joinPool(se); + final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); + try (PoolCleaner cleaner = cleaner(p)) { + try { + TrackedCallable callable = null; + Future f = p.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (NullPointerException success) {} + } } /** * execute throws RejectedExecutionException if shutdown */ public void testSchedule1_RejectedExecutionException() throws InterruptedException { - ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); - try { - se.shutdown(); - se.schedule(new NoOpRunnable(), - MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (RejectedExecutionException success) { - } catch (SecurityException ok) { + ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); + try (PoolCleaner cleaner = cleaner(p)) { + try { + p.shutdown(); + p.schedule(new NoOpRunnable(), + MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (RejectedExecutionException success) { + } catch (SecurityException ok) {} } - - joinPool(se); } /** * schedule throws RejectedExecutionException if shutdown */ public void testSchedule2_RejectedExecutionException() throws InterruptedException { - ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); - try { - se.shutdown(); - se.schedule(new NoOpCallable(), - MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (RejectedExecutionException success) { - } catch (SecurityException ok) { + ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); + try (PoolCleaner cleaner = cleaner(p)) { + try { + p.shutdown(); + p.schedule(new NoOpCallable(), + MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (RejectedExecutionException success) { + } catch (SecurityException ok) {} } - joinPool(se); } /** * schedule callable throws RejectedExecutionException if shutdown */ public void testSchedule3_RejectedExecutionException() throws InterruptedException { - ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); - try { - se.shutdown(); - se.schedule(new NoOpCallable(), - MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (RejectedExecutionException success) { - } catch (SecurityException ok) { + ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); + try (PoolCleaner cleaner = cleaner(p)) { + try { + p.shutdown(); + p.schedule(new NoOpCallable(), + MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (RejectedExecutionException success) { + } catch (SecurityException ok) {} } - joinPool(se); } /** * scheduleAtFixedRate throws RejectedExecutionException if shutdown */ public void testScheduleAtFixedRate1_RejectedExecutionException() throws InterruptedException { - ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); - try { - se.shutdown(); - se.scheduleAtFixedRate(new NoOpRunnable(), - MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (RejectedExecutionException success) { - } catch (SecurityException ok) { + ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); + try (PoolCleaner cleaner = cleaner(p)) { + try { + p.shutdown(); + p.scheduleAtFixedRate(new NoOpRunnable(), + MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (RejectedExecutionException success) { + } catch (SecurityException ok) {} } - joinPool(se); } /** * scheduleWithFixedDelay throws RejectedExecutionException if shutdown */ public void testScheduleWithFixedDelay1_RejectedExecutionException() throws InterruptedException { - ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); - try { - se.shutdown(); - se.scheduleWithFixedDelay(new NoOpRunnable(), - MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (RejectedExecutionException success) { - } catch (SecurityException ok) { + ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); + try (PoolCleaner cleaner = cleaner(p)) { + try { + p.shutdown(); + p.scheduleWithFixedDelay(new NoOpRunnable(), + MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (RejectedExecutionException success) { + } catch (SecurityException ok) {} } - joinPool(se); } /** @@ -324,9 +307,9 @@ public class ScheduledExecutorTest exten */ public void testGetActiveCount() throws InterruptedException { final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2); - final CountDownLatch threadStarted = new CountDownLatch(1); - final CountDownLatch done = new CountDownLatch(1); - try { + try (PoolCleaner cleaner = cleaner(p)) { + final CountDownLatch threadStarted = new CountDownLatch(1); + final CountDownLatch done = new CountDownLatch(1); assertEquals(0, p.getActiveCount()); p.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { @@ -336,9 +319,7 @@ public class ScheduledExecutorTest exten }}); assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); assertEquals(1, p.getActiveCount()); - } finally { done.countDown(); - joinPool(p); } } @@ -348,10 +329,10 @@ public class ScheduledExecutorTest exten */ public void testGetCompletedTaskCount() throws InterruptedException { final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2); - final CountDownLatch threadStarted = new CountDownLatch(1); - final CountDownLatch threadProceed = new CountDownLatch(1); - final CountDownLatch threadDone = new CountDownLatch(1); - try { + try (PoolCleaner cleaner = cleaner(p)) { + final CountDownLatch threadStarted = new CountDownLatch(1); + final CountDownLatch threadProceed = new CountDownLatch(1); + final CountDownLatch threadDone = new CountDownLatch(1); assertEquals(0, p.getCompletedTaskCount()); p.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { @@ -370,8 +351,6 @@ public class ScheduledExecutorTest exten fail("timed out"); Thread.yield(); } - } finally { - joinPool(p); } } @@ -380,8 +359,9 @@ public class ScheduledExecutorTest exten */ public void testGetCorePoolSize() throws InterruptedException { ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - assertEquals(1, p.getCorePoolSize()); - joinPool(p); + try (PoolCleaner cleaner = cleaner(p)) { + assertEquals(1, p.getCorePoolSize()); + } } /** @@ -393,7 +373,7 @@ public class ScheduledExecutorTest exten final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(THREADS); final CountDownLatch threadsStarted = new CountDownLatch(THREADS); final CountDownLatch done = new CountDownLatch(1); - try { + try (PoolCleaner cleaner = cleaner(p)) { assertEquals(0, p.getLargestPoolSize()); for (int i = 0; i < THREADS; i++) p.execute(new CheckedRunnable() { @@ -404,11 +384,9 @@ public class ScheduledExecutorTest exten }}); assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); assertEquals(THREADS, p.getLargestPoolSize()); - } finally { done.countDown(); - joinPool(p); - assertEquals(THREADS, p.getLargestPoolSize()); } + assertEquals(THREADS, p.getLargestPoolSize()); } /** @@ -419,7 +397,7 @@ public class ScheduledExecutorTest exten final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); final CountDownLatch threadStarted = new CountDownLatch(1); final CountDownLatch done = new CountDownLatch(1); - try { + try (PoolCleaner cleaner = cleaner(p)) { assertEquals(0, p.getPoolSize()); p.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { @@ -429,9 +407,7 @@ public class ScheduledExecutorTest exten }}); assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); assertEquals(1, p.getPoolSize()); - } finally { done.countDown(); - joinPool(p); } } @@ -441,10 +417,10 @@ public class ScheduledExecutorTest exten */ public void testGetTaskCount() throws InterruptedException { final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - final CountDownLatch threadStarted = new CountDownLatch(1); - final CountDownLatch done = new CountDownLatch(1); - final int TASKS = 5; - try { + try (PoolCleaner cleaner = cleaner(p)) { + final CountDownLatch threadStarted = new CountDownLatch(1); + final CountDownLatch done = new CountDownLatch(1); + final int TASKS = 5; assertEquals(0, p.getTaskCount()); for (int i = 0; i < TASKS; i++) p.execute(new CheckedRunnable() { @@ -454,9 +430,7 @@ public class ScheduledExecutorTest exten }}); assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); assertEquals(TASKS, p.getTaskCount()); - } finally { done.countDown(); - joinPool(p); } } @@ -464,9 +438,9 @@ public class ScheduledExecutorTest exten * getThreadFactory returns factory in constructor if not set */ public void testGetThreadFactory() throws InterruptedException { - ThreadFactory tf = new SimpleThreadFactory(); - ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf); - assertSame(tf, p.getThreadFactory()); + ThreadFactory threadFactory = new SimpleThreadFactory(); + ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, threadFactory); + assertSame(threadFactory, p.getThreadFactory()); joinPool(p); } @@ -474,11 +448,12 @@ public class ScheduledExecutorTest exten * setThreadFactory sets the thread factory returned by getThreadFactory */ public void testSetThreadFactory() throws InterruptedException { - ThreadFactory tf = new SimpleThreadFactory(); + ThreadFactory threadFactory = new SimpleThreadFactory(); ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - p.setThreadFactory(tf); - assertSame(tf, p.getThreadFactory()); - joinPool(p); + try (PoolCleaner cleaner = cleaner(p)) { + p.setThreadFactory(threadFactory); + assertSame(threadFactory, p.getThreadFactory()); + } } /** @@ -486,12 +461,11 @@ public class ScheduledExecutorTest exten */ public void testSetThreadFactoryNull() throws InterruptedException { ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - try { - p.setThreadFactory(null); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(p); + try (PoolCleaner cleaner = cleaner(p)) { + try { + p.setThreadFactory(null); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -515,10 +489,10 @@ public class ScheduledExecutorTest exten */ public void testIsTerminated() throws InterruptedException { final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - final CountDownLatch threadStarted = new CountDownLatch(1); - final CountDownLatch done = new CountDownLatch(1); - assertFalse(p.isTerminated()); - try { + try (PoolCleaner cleaner = cleaner(p)) { + final CountDownLatch threadStarted = new CountDownLatch(1); + final CountDownLatch done = new CountDownLatch(1); + assertFalse(p.isTerminated()); p.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { assertFalse(p.isTerminated()); @@ -528,11 +502,10 @@ public class ScheduledExecutorTest exten assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); assertFalse(p.isTerminating()); done.countDown(); - } finally { try { p.shutdown(); } catch (SecurityException ok) { return; } + assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(p.isTerminated()); } - assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); - assertTrue(p.isTerminated()); } /** @@ -542,7 +515,7 @@ public class ScheduledExecutorTest exten final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); final CountDownLatch threadStarted = new CountDownLatch(1); final CountDownLatch done = new CountDownLatch(1); - try { + try (PoolCleaner cleaner = cleaner(p)) { assertFalse(p.isTerminating()); p.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { @@ -553,12 +526,11 @@ public class ScheduledExecutorTest exten assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); assertFalse(p.isTerminating()); done.countDown(); - } finally { try { p.shutdown(); } catch (SecurityException ok) { return; } + assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(p.isTerminated()); + assertFalse(p.isTerminating()); } - assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); - assertTrue(p.isTerminated()); - assertFalse(p.isTerminating()); } /** @@ -566,9 +538,9 @@ public class ScheduledExecutorTest exten */ public void testGetQueue() throws InterruptedException { ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - final CountDownLatch threadStarted = new CountDownLatch(1); - final CountDownLatch done = new CountDownLatch(1); - try { + try (PoolCleaner cleaner = cleaner(p)) { + final CountDownLatch threadStarted = new CountDownLatch(1); + final CountDownLatch done = new CountDownLatch(1); ScheduledFuture[] tasks = new ScheduledFuture[5]; for (int i = 0; i < tasks.length; i++) { Runnable r = new CheckedRunnable() { @@ -582,9 +554,7 @@ public class ScheduledExecutorTest exten BlockingQueue q = p.getQueue(); assertTrue(q.contains(tasks[tasks.length - 1])); assertFalse(q.contains(tasks[0])); - } finally { done.countDown(); - joinPool(p); } } @@ -593,10 +563,10 @@ public class ScheduledExecutorTest exten */ public void testRemove() throws InterruptedException { final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - ScheduledFuture[] tasks = new ScheduledFuture[5]; - final CountDownLatch threadStarted = new CountDownLatch(1); - final CountDownLatch done = new CountDownLatch(1); - try { + try (PoolCleaner cleaner = cleaner(p)) { + ScheduledFuture[] tasks = new ScheduledFuture[5]; + final CountDownLatch threadStarted = new CountDownLatch(1); + final CountDownLatch done = new CountDownLatch(1); for (int i = 0; i < tasks.length; i++) { Runnable r = new CheckedRunnable() { public void realRun() throws InterruptedException { @@ -616,9 +586,7 @@ public class ScheduledExecutorTest exten assertTrue(q.contains((Runnable)tasks[3])); assertTrue(p.remove((Runnable)tasks[3])); assertFalse(q.contains((Runnable)tasks[3])); - } finally { done.countDown(); - joinPool(p); } } @@ -822,13 +790,11 @@ public class ScheduledExecutorTest exten * completed submit of callable returns result */ public void testSubmitCallable() throws Exception { - ExecutorService e = new ScheduledThreadPoolExecutor(2); - try { + final ExecutorService e = new ScheduledThreadPoolExecutor(2); + try (PoolCleaner cleaner = cleaner(e)) { Future future = e.submit(new StringTask()); String result = future.get(); assertSame(TEST_STRING, result); - } finally { - joinPool(e); } } @@ -836,13 +802,11 @@ public class ScheduledExecutorTest exten * completed submit of runnable returns successfully */ public void testSubmitRunnable() throws Exception { - ExecutorService e = new ScheduledThreadPoolExecutor(2); - try { + final ExecutorService e = new ScheduledThreadPoolExecutor(2); + try (PoolCleaner cleaner = cleaner(e)) { Future future = e.submit(new NoOpRunnable()); future.get(); assertTrue(future.isDone()); - } finally { - joinPool(e); } } @@ -850,13 +814,11 @@ public class ScheduledExecutorTest exten * completed submit of (runnable, result) returns result */ public void testSubmitRunnable2() throws Exception { - ExecutorService e = new ScheduledThreadPoolExecutor(2); - try { + final ExecutorService e = new ScheduledThreadPoolExecutor(2); + try (PoolCleaner cleaner = cleaner(e)) { Future future = e.submit(new NoOpRunnable(), TEST_STRING); String result = future.get(); assertSame(TEST_STRING, result); - } finally { - joinPool(e); } } @@ -864,13 +826,12 @@ public class ScheduledExecutorTest exten * invokeAny(null) throws NPE */ public void testInvokeAny1() throws Exception { - ExecutorService e = new ScheduledThreadPoolExecutor(2); - try { - e.invokeAny(null); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + final ExecutorService e = new ScheduledThreadPoolExecutor(2); + try (PoolCleaner cleaner = cleaner(e)) { + try { + e.invokeAny(null); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -878,13 +839,12 @@ public class ScheduledExecutorTest exten * invokeAny(empty collection) throws IAE */ public void testInvokeAny2() throws Exception { - ExecutorService e = new ScheduledThreadPoolExecutor(2); - try { - e.invokeAny(new ArrayList>()); - shouldThrow(); - } catch (IllegalArgumentException success) { - } finally { - joinPool(e); + final ExecutorService e = new ScheduledThreadPoolExecutor(2); + try (PoolCleaner cleaner = cleaner(e)) { + try { + e.invokeAny(new ArrayList>()); + shouldThrow(); + } catch (IllegalArgumentException success) {} } } @@ -893,17 +853,16 @@ public class ScheduledExecutorTest exten */ public void testInvokeAny3() throws Exception { CountDownLatch latch = new CountDownLatch(1); - ExecutorService e = new ScheduledThreadPoolExecutor(2); - List> l = new ArrayList>(); - l.add(latchAwaitingStringTask(latch)); - l.add(null); - try { - e.invokeAny(l); - shouldThrow(); - } catch (NullPointerException success) { - } finally { + final ExecutorService e = new ScheduledThreadPoolExecutor(2); + try (PoolCleaner cleaner = cleaner(e)) { + List> l = new ArrayList>(); + l.add(latchAwaitingStringTask(latch)); + l.add(null); + try { + e.invokeAny(l); + shouldThrow(); + } catch (NullPointerException success) {} latch.countDown(); - joinPool(e); } } @@ -911,16 +870,16 @@ public class ScheduledExecutorTest exten * invokeAny(c) throws ExecutionException if no task completes */ public void testInvokeAny4() throws Exception { - ExecutorService e = new ScheduledThreadPoolExecutor(2); - 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 ScheduledThreadPoolExecutor(2); + 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); + } } } @@ -928,15 +887,13 @@ public class ScheduledExecutorTest exten * invokeAny(c) returns result of some task */ public void testInvokeAny5() throws Exception { - ExecutorService e = new ScheduledThreadPoolExecutor(2); - try { + final ExecutorService e = new ScheduledThreadPoolExecutor(2); + 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); } } @@ -944,13 +901,12 @@ public class ScheduledExecutorTest exten * invokeAll(null) throws NPE */ public void testInvokeAll1() throws Exception { - ExecutorService e = new ScheduledThreadPoolExecutor(2); - try { - e.invokeAll(null); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + final ExecutorService e = new ScheduledThreadPoolExecutor(2); + try (PoolCleaner cleaner = cleaner(e)) { + try { + e.invokeAll(null); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -958,12 +914,10 @@ public class ScheduledExecutorTest exten * invokeAll(empty collection) returns empty collection */ public void testInvokeAll2() throws Exception { - ExecutorService e = new ScheduledThreadPoolExecutor(2); - try { + final ExecutorService e = new ScheduledThreadPoolExecutor(2); + try (PoolCleaner cleaner = cleaner(e)) { List> r = e.invokeAll(new ArrayList>()); assertTrue(r.isEmpty()); - } finally { - joinPool(e); } } @@ -971,16 +925,15 @@ public class ScheduledExecutorTest exten * invokeAll(c) throws NPE if c has null elements */ public void testInvokeAll3() throws Exception { - ExecutorService e = new ScheduledThreadPoolExecutor(2); - 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 ScheduledThreadPoolExecutor(2); + try (PoolCleaner cleaner = cleaner(e)) { + List> l = new ArrayList>(); + l.add(new StringTask()); + l.add(null); + try { + e.invokeAll(l); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -988,18 +941,18 @@ public class ScheduledExecutorTest exten * get of invokeAll(c) throws exception on failed task */ public void testInvokeAll4() throws Exception { - ExecutorService e = new ScheduledThreadPoolExecutor(2); - List> l = new ArrayList>(); - l.add(new NPETask()); - List> futures = e.invokeAll(l); - assertEquals(1, futures.size()); - try { - futures.get(0).get(); - shouldThrow(); - } catch (ExecutionException success) { - assertTrue(success.getCause() instanceof NullPointerException); - } finally { - joinPool(e); + final ExecutorService e = new ScheduledThreadPoolExecutor(2); + try (PoolCleaner cleaner = cleaner(e)) { + List> l = new ArrayList>(); + l.add(new NPETask()); + List> futures = e.invokeAll(l); + assertEquals(1, futures.size()); + try { + futures.get(0).get(); + shouldThrow(); + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof NullPointerException); + } } } @@ -1007,8 +960,8 @@ public class ScheduledExecutorTest exten * invokeAll(c) returns results of all completed tasks */ public void testInvokeAll5() throws Exception { - ExecutorService e = new ScheduledThreadPoolExecutor(2); - try { + final ExecutorService e = new ScheduledThreadPoolExecutor(2); + try (PoolCleaner cleaner = cleaner(e)) { List> l = new ArrayList>(); l.add(new StringTask()); l.add(new StringTask()); @@ -1016,8 +969,6 @@ public class ScheduledExecutorTest exten assertEquals(2, futures.size()); for (Future future : futures) assertSame(TEST_STRING, future.get()); - } finally { - joinPool(e); } } @@ -1025,13 +976,12 @@ public class ScheduledExecutorTest exten * timed invokeAny(null) throws NPE */ public void testTimedInvokeAny1() throws Exception { - ExecutorService e = new ScheduledThreadPoolExecutor(2); - try { - e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + final ExecutorService e = new ScheduledThreadPoolExecutor(2); + try (PoolCleaner cleaner = cleaner(e)) { + try { + e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -1039,15 +989,14 @@ public class ScheduledExecutorTest exten * timed invokeAny(,,null) throws NPE */ public void testTimedInvokeAnyNullTimeUnit() throws Exception { - ExecutorService e = new ScheduledThreadPoolExecutor(2); - 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 ScheduledThreadPoolExecutor(2); + 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) {} } } @@ -1055,13 +1004,12 @@ public class ScheduledExecutorTest exten * timed invokeAny(empty collection) throws IAE */ public void testTimedInvokeAny2() throws Exception { - ExecutorService e = new ScheduledThreadPoolExecutor(2); - try { - e.invokeAny(new ArrayList>(), MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (IllegalArgumentException success) { - } finally { - joinPool(e); + final ExecutorService e = new ScheduledThreadPoolExecutor(2); + try (PoolCleaner cleaner = cleaner(e)) { + try { + e.invokeAny(new ArrayList>(), MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (IllegalArgumentException success) {} } } @@ -1070,17 +1018,16 @@ public class ScheduledExecutorTest exten */ public void testTimedInvokeAny3() throws Exception { CountDownLatch latch = new CountDownLatch(1); - ExecutorService e = new ScheduledThreadPoolExecutor(2); - List> l = new ArrayList>(); - l.add(latchAwaitingStringTask(latch)); - l.add(null); - try { - e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (NullPointerException success) { - } finally { + final ExecutorService e = new ScheduledThreadPoolExecutor(2); + try (PoolCleaner cleaner = cleaner(e)) { + List> l = new ArrayList>(); + l.add(latchAwaitingStringTask(latch)); + l.add(null); + try { + e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (NullPointerException success) {} latch.countDown(); - joinPool(e); } } @@ -1088,16 +1035,16 @@ public class ScheduledExecutorTest exten * timed invokeAny(c) throws ExecutionException if no task completes */ public void testTimedInvokeAny4() throws Exception { - ExecutorService e = new ScheduledThreadPoolExecutor(2); - 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 ScheduledThreadPoolExecutor(2); + try (PoolCleaner cleaner = cleaner(e)) { + 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); + } } } @@ -1105,15 +1052,13 @@ public class ScheduledExecutorTest exten * timed invokeAny(c) returns result of some task */ public void testTimedInvokeAny5() throws Exception { - ExecutorService e = new ScheduledThreadPoolExecutor(2); - try { + final ExecutorService e = new ScheduledThreadPoolExecutor(2); + try (PoolCleaner cleaner = cleaner(e)) { List> l = new ArrayList>(); l.add(new StringTask()); l.add(new StringTask()); String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); assertSame(TEST_STRING, result); - } finally { - joinPool(e); } } @@ -1121,13 +1066,12 @@ public class ScheduledExecutorTest exten * timed invokeAll(null) throws NPE */ public void testTimedInvokeAll1() throws Exception { - ExecutorService e = new ScheduledThreadPoolExecutor(2); - try { - e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + final ExecutorService e = new ScheduledThreadPoolExecutor(2); + try (PoolCleaner cleaner = cleaner(e)) { + try { + e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -1135,15 +1079,14 @@ public class ScheduledExecutorTest exten * timed invokeAll(,,null) throws NPE */ public void testTimedInvokeAllNullTimeUnit() throws Exception { - ExecutorService e = new ScheduledThreadPoolExecutor(2); - 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 ScheduledThreadPoolExecutor(2); + 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) {} } } @@ -1151,12 +1094,11 @@ public class ScheduledExecutorTest exten * timed invokeAll(empty collection) returns empty collection */ public void testTimedInvokeAll2() throws Exception { - ExecutorService e = new ScheduledThreadPoolExecutor(2); - try { - List> r = e.invokeAll(new ArrayList>(), MEDIUM_DELAY_MS, MILLISECONDS); + final ExecutorService e = new ScheduledThreadPoolExecutor(2); + try (PoolCleaner cleaner = cleaner(e)) { + List> r = e.invokeAll(new ArrayList>(), + MEDIUM_DELAY_MS, MILLISECONDS); assertTrue(r.isEmpty()); - } finally { - joinPool(e); } } @@ -1164,16 +1106,15 @@ public class ScheduledExecutorTest exten * timed invokeAll(c) throws NPE if c has null elements */ public void testTimedInvokeAll3() throws Exception { - ExecutorService e = new ScheduledThreadPoolExecutor(2); - 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 ScheduledThreadPoolExecutor(2); + 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) {} } } @@ -1181,19 +1122,19 @@ public class ScheduledExecutorTest exten * get of element of invokeAll(c) throws exception on failed task */ public void testTimedInvokeAll4() throws Exception { - ExecutorService e = new ScheduledThreadPoolExecutor(2); - List> l = new ArrayList>(); - l.add(new NPETask()); - List> futures = - e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); - assertEquals(1, futures.size()); - try { - futures.get(0).get(); - shouldThrow(); - } catch (ExecutionException success) { - assertTrue(success.getCause() instanceof NullPointerException); - } finally { - joinPool(e); + final ExecutorService e = new ScheduledThreadPoolExecutor(2); + try (PoolCleaner cleaner = cleaner(e)) { + List> l = new ArrayList>(); + l.add(new NPETask()); + List> futures = + e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); + assertEquals(1, futures.size()); + try { + futures.get(0).get(); + shouldThrow(); + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof NullPointerException); + } } } @@ -1201,8 +1142,8 @@ public class ScheduledExecutorTest exten * timed invokeAll(c) returns results of all completed tasks */ public void testTimedInvokeAll5() throws Exception { - ExecutorService e = new ScheduledThreadPoolExecutor(2); - try { + final ExecutorService e = new ScheduledThreadPoolExecutor(2); + try (PoolCleaner cleaner = cleaner(e)) { List> l = new ArrayList>(); l.add(new StringTask()); l.add(new StringTask()); @@ -1211,8 +1152,6 @@ public class ScheduledExecutorTest exten assertEquals(2, futures.size()); for (Future future : futures) assertSame(TEST_STRING, future.get()); - } finally { - joinPool(e); } } @@ -1220,8 +1159,8 @@ public class ScheduledExecutorTest exten * timed invokeAll(c) cancels tasks not completed by timeout */ public void testTimedInvokeAll6() throws Exception { - ExecutorService e = new ScheduledThreadPoolExecutor(2); - try { + final ExecutorService e = new ScheduledThreadPoolExecutor(2); + try (PoolCleaner cleaner = cleaner(e)) { for (long timeout = timeoutMillis();;) { List> tasks = new ArrayList<>(); tasks.add(new StringTask("0")); @@ -1245,8 +1184,6 @@ public class ScheduledExecutorTest exten fail("expected exactly one task to be cancelled"); } } - } finally { - joinPool(e); } }