--- jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java 2015/10/04 01:52:43 1.58 +++ jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java 2015/10/04 02:07:32 1.63 @@ -354,7 +354,7 @@ public class ThreadPoolExecutorSubclassT * getCorePoolSize returns size given in constructor if not otherwise set */ public void testGetCorePoolSize() { - ThreadPoolExecutor p = + final ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); @@ -367,7 +367,7 @@ public class ThreadPoolExecutorSubclassT * getKeepAliveTime returns value given in constructor if not otherwise set */ public void testGetKeepAliveTime() { - ThreadPoolExecutor p = + final ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue(10)); @@ -380,8 +380,8 @@ public class ThreadPoolExecutorSubclassT * getThreadFactory returns factory in constructor if not set */ public void testGetThreadFactory() { - ThreadFactory threadFactory = new SimpleThreadFactory(); - ThreadPoolExecutor p = + final ThreadFactory threadFactory = new SimpleThreadFactory(); + final ThreadPoolExecutor p = new CustomTPE(1, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10), @@ -396,7 +396,7 @@ public class ThreadPoolExecutorSubclassT * setThreadFactory sets the thread factory returned by getThreadFactory */ public void testSetThreadFactory() { - ThreadPoolExecutor p = + final ThreadPoolExecutor p = new CustomTPE(1, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); @@ -411,7 +411,7 @@ public class ThreadPoolExecutorSubclassT * setThreadFactory(null) throws NPE */ public void testSetThreadFactoryNull() { - ThreadPoolExecutor p = + final ThreadPoolExecutor p = new CustomTPE(1, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); @@ -427,10 +427,15 @@ public class ThreadPoolExecutorSubclassT * getRejectedExecutionHandler returns handler in constructor if not set */ public void testGetRejectedExecutionHandler() { - RejectedExecutionHandler h = new NoOpREHandler(); - ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10), h); - assertSame(h, p.getRejectedExecutionHandler()); - joinPool(p); + final RejectedExecutionHandler handler = new NoOpREHandler(); + final ThreadPoolExecutor p = + new CustomTPE(1, 2, + LONG_DELAY_MS, MILLISECONDS, + new ArrayBlockingQueue(10), + handler); + try (PoolCleaner cleaner = cleaner(p)) { + assertSame(handler, p.getRejectedExecutionHandler()); + } } /** @@ -438,24 +443,30 @@ public class ThreadPoolExecutorSubclassT * getRejectedExecutionHandler */ public void testSetRejectedExecutionHandler() { - ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); - RejectedExecutionHandler h = new NoOpREHandler(); - p.setRejectedExecutionHandler(h); - assertSame(h, p.getRejectedExecutionHandler()); - joinPool(p); + final ThreadPoolExecutor p = + new CustomTPE(1, 2, + LONG_DELAY_MS, MILLISECONDS, + new ArrayBlockingQueue(10)); + try (PoolCleaner cleaner = cleaner(p)) { + RejectedExecutionHandler handler = new NoOpREHandler(); + p.setRejectedExecutionHandler(handler); + assertSame(handler, p.getRejectedExecutionHandler()); + } } /** * setRejectedExecutionHandler(null) throws NPE */ public void testSetRejectedExecutionHandlerNull() { - ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); - try { - p.setRejectedExecutionHandler(null); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(p); + final ThreadPoolExecutor p = + new CustomTPE(1, 2, + LONG_DELAY_MS, MILLISECONDS, + new ArrayBlockingQueue(10)); + try (PoolCleaner cleaner = cleaner(p)) { + try { + p.setRejectedExecutionHandler(null); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -469,9 +480,9 @@ public class ThreadPoolExecutorSubclassT new CustomTPE(THREADS, THREADS, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); - final CountDownLatch threadsStarted = new CountDownLatch(THREADS); - final CountDownLatch done = new CountDownLatch(1); - try { + try (PoolCleaner cleaner = cleaner(p)) { + final CountDownLatch threadsStarted = new CountDownLatch(THREADS); + final CountDownLatch done = new CountDownLatch(1); assertEquals(0, p.getLargestPoolSize()); for (int i = 0; i < THREADS; i++) p.execute(new CheckedRunnable() { @@ -480,13 +491,11 @@ public class ThreadPoolExecutorSubclassT done.await(); assertEquals(THREADS, p.getLargestPoolSize()); }}); - assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); - assertEquals(THREADS, p.getLargestPoolSize()); - } finally { - done.countDown(); - joinPool(p); + assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); assertEquals(THREADS, p.getLargestPoolSize()); + done.countDown(); // release pool } + assertEquals(THREADS, p.getLargestPoolSize()); } /** @@ -494,9 +503,17 @@ public class ThreadPoolExecutorSubclassT * otherwise set */ public void testGetMaximumPoolSize() { - ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); - assertEquals(2, p.getMaximumPoolSize()); - joinPool(p); + final ThreadPoolExecutor p = + new CustomTPE(2, 3, + LONG_DELAY_MS, MILLISECONDS, + new ArrayBlockingQueue(10)); + try (PoolCleaner cleaner = cleaner(p)) { + assertEquals(3, p.getMaximumPoolSize()); + p.setMaximumPoolSize(5); + assertEquals(5, p.getMaximumPoolSize()); + p.setMaximumPoolSize(4); + assertEquals(4, p.getMaximumPoolSize()); + } } /**