--- jsr166/src/test/tck/ThreadPoolExecutorTest.java 2015/04/25 04:55:31 1.51 +++ jsr166/src/test/tck/ThreadPoolExecutorTest.java 2015/10/04 02:26:47 1.82 @@ -8,12 +8,14 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.NANOSECONDS; +import static java.util.concurrent.TimeUnit.SECONDS; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; 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; @@ -27,6 +29,7 @@ import java.util.concurrent.SynchronousQ import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; import junit.framework.Test; import junit.framework.TestSuite; @@ -84,16 +87,12 @@ public class ThreadPoolExecutorTest exte new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); - 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); + assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); } } @@ -106,9 +105,9 @@ public class ThreadPoolExecutorTest exte new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); - 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 { @@ -116,11 +115,9 @@ public class ThreadPoolExecutorTest exte assertEquals(1, p.getActiveCount()); done.await(); }}); - assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); + assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); assertEquals(1, p.getActiveCount()); - } finally { done.countDown(); - joinPool(p); } } @@ -129,17 +126,25 @@ public class ThreadPoolExecutorTest exte */ public void testPrestartCoreThread() { final ThreadPoolExecutor p = - new ThreadPoolExecutor(2, 2, + new ThreadPoolExecutor(2, 6, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); - assertEquals(0, p.getPoolSize()); - assertTrue(p.prestartCoreThread()); - assertEquals(1, p.getPoolSize()); - assertTrue(p.prestartCoreThread()); - assertEquals(2, p.getPoolSize()); - assertFalse(p.prestartCoreThread()); - assertEquals(2, p.getPoolSize()); - joinPool(p); + try (PoolCleaner cleaner = cleaner(p)) { + assertEquals(0, p.getPoolSize()); + assertTrue(p.prestartCoreThread()); + assertEquals(1, p.getPoolSize()); + assertTrue(p.prestartCoreThread()); + assertEquals(2, p.getPoolSize()); + assertFalse(p.prestartCoreThread()); + assertEquals(2, p.getPoolSize()); + p.setCorePoolSize(4); + assertTrue(p.prestartCoreThread()); + assertEquals(3, p.getPoolSize()); + assertTrue(p.prestartCoreThread()); + assertEquals(4, p.getPoolSize()); + assertFalse(p.prestartCoreThread()); + assertEquals(4, p.getPoolSize()); + } } /** @@ -147,15 +152,21 @@ public class ThreadPoolExecutorTest exte */ public void testPrestartAllCoreThreads() { final ThreadPoolExecutor p = - new ThreadPoolExecutor(2, 2, + new ThreadPoolExecutor(2, 6, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); - assertEquals(0, p.getPoolSize()); - p.prestartAllCoreThreads(); - assertEquals(2, p.getPoolSize()); - p.prestartAllCoreThreads(); - assertEquals(2, p.getPoolSize()); - joinPool(p); + try (PoolCleaner cleaner = cleaner(p)) { + assertEquals(0, p.getPoolSize()); + p.prestartAllCoreThreads(); + assertEquals(2, p.getPoolSize()); + p.prestartAllCoreThreads(); + assertEquals(2, p.getPoolSize()); + p.setCorePoolSize(4); + p.prestartAllCoreThreads(); + assertEquals(4, p.getPoolSize()); + p.prestartAllCoreThreads(); + assertEquals(4, p.getPoolSize()); + } } /** @@ -167,10 +178,10 @@ public class ThreadPoolExecutorTest exte new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); - 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 { @@ -189,8 +200,6 @@ public class ThreadPoolExecutorTest exte fail("timed out"); Thread.yield(); } - } finally { - joinPool(p); } } @@ -202,8 +211,9 @@ public class ThreadPoolExecutorTest exte new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); - assertEquals(1, p.getCorePoolSize()); - joinPool(p); + try (PoolCleaner cleaner = cleaner(p)) { + assertEquals(1, p.getCorePoolSize()); + } } /** @@ -214,23 +224,25 @@ public class ThreadPoolExecutorTest exte new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue(10)); - assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS)); - joinPool(p); + try (PoolCleaner cleaner = cleaner(p)) { + assertEquals(1, p.getKeepAliveTime(SECONDS)); + } } /** * getThreadFactory returns factory in constructor if not set */ public void testGetThreadFactory() { - ThreadFactory tf = new SimpleThreadFactory(); + ThreadFactory threadFactory = new SimpleThreadFactory(); final ThreadPoolExecutor p = new ThreadPoolExecutor(1, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10), - tf, + threadFactory, new NoOpREHandler()); - assertSame(tf, p.getThreadFactory()); - joinPool(p); + try (PoolCleaner cleaner = cleaner(p)) { + assertSame(threadFactory, p.getThreadFactory()); + } } /** @@ -241,10 +253,11 @@ public class ThreadPoolExecutorTest exte new ThreadPoolExecutor(1, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); - ThreadFactory tf = new SimpleThreadFactory(); - p.setThreadFactory(tf); - assertSame(tf, p.getThreadFactory()); - joinPool(p); + try (PoolCleaner cleaner = cleaner(p)) { + ThreadFactory threadFactory = new SimpleThreadFactory(); + p.setThreadFactory(threadFactory); + assertSame(threadFactory, p.getThreadFactory()); + } } /** @@ -255,12 +268,11 @@ public class ThreadPoolExecutorTest exte new ThreadPoolExecutor(1, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); - try { - p.setThreadFactory(null); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(p); + try (PoolCleaner cleaner = cleaner(p)) { + try { + p.setThreadFactory(null); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -268,14 +280,15 @@ public class ThreadPoolExecutorTest exte * getRejectedExecutionHandler returns handler in constructor if not set */ public void testGetRejectedExecutionHandler() { - final RejectedExecutionHandler h = new NoOpREHandler(); + final RejectedExecutionHandler handler = new NoOpREHandler(); final ThreadPoolExecutor p = new ThreadPoolExecutor(1, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10), - h); - assertSame(h, p.getRejectedExecutionHandler()); - joinPool(p); + handler); + try (PoolCleaner cleaner = cleaner(p)) { + assertSame(handler, p.getRejectedExecutionHandler()); + } } /** @@ -287,10 +300,11 @@ public class ThreadPoolExecutorTest exte new ThreadPoolExecutor(1, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); - RejectedExecutionHandler h = new NoOpREHandler(); - p.setRejectedExecutionHandler(h); - assertSame(h, p.getRejectedExecutionHandler()); - joinPool(p); + try (PoolCleaner cleaner = cleaner(p)) { + RejectedExecutionHandler handler = new NoOpREHandler(); + p.setRejectedExecutionHandler(handler); + assertSame(handler, p.getRejectedExecutionHandler()); + } } /** @@ -301,12 +315,11 @@ public class ThreadPoolExecutorTest exte new ThreadPoolExecutor(1, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); - try { - p.setRejectedExecutionHandler(null); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(p); + try (PoolCleaner cleaner = cleaner(p)) { + try { + p.setRejectedExecutionHandler(null); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -320,9 +333,9 @@ public class ThreadPoolExecutorTest exte new ThreadPoolExecutor(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() { @@ -331,13 +344,11 @@ public class ThreadPoolExecutorTest exte 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()); } /** @@ -349,8 +360,13 @@ public class ThreadPoolExecutorTest exte new ThreadPoolExecutor(2, 3, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); - assertEquals(3, p.getMaximumPoolSize()); - joinPool(p); + try (PoolCleaner cleaner = cleaner(p)) { + assertEquals(3, p.getMaximumPoolSize()); + p.setMaximumPoolSize(5); + assertEquals(5, p.getMaximumPoolSize()); + p.setMaximumPoolSize(4); + assertEquals(4, p.getMaximumPoolSize()); + } } /** @@ -362,9 +378,9 @@ public class ThreadPoolExecutorTest exte new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); - 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.getPoolSize()); p.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { @@ -372,11 +388,9 @@ public class ThreadPoolExecutorTest exte assertEquals(1, p.getPoolSize()); done.await(); }}); - assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); + assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); assertEquals(1, p.getPoolSize()); - } finally { - done.countDown(); - joinPool(p); + done.countDown(); // release pool } } @@ -388,9 +402,9 @@ public class ThreadPoolExecutorTest exte new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); - 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.getTaskCount()); p.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { @@ -398,11 +412,9 @@ public class ThreadPoolExecutorTest exte assertEquals(1, p.getTaskCount()); done.await(); }}); - assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); + assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); assertEquals(1, p.getTaskCount()); - } finally { done.countDown(); - joinPool(p); } } @@ -414,10 +426,11 @@ public class ThreadPoolExecutorTest exte new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); - assertFalse(p.isShutdown()); - try { p.shutdown(); } catch (SecurityException ok) { return; } - assertTrue(p.isShutdown()); - joinPool(p); + try (PoolCleaner cleaner = cleaner(p)) { + assertFalse(p.isShutdown()); + try { p.shutdown(); } catch (SecurityException ok) { return; } + assertTrue(p.isShutdown()); + } } /** @@ -468,7 +481,7 @@ public class ThreadPoolExecutorTest exte threadStarted.countDown(); done.await(); }}); - assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); + assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); assertFalse(p.isTerminating()); done.countDown(); } finally { @@ -496,7 +509,7 @@ public class ThreadPoolExecutorTest exte threadStarted.countDown(); done.await(); }}); - assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); + assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); assertFalse(p.isTerminating()); done.countDown(); } finally { @@ -531,7 +544,7 @@ public class ThreadPoolExecutorTest exte tasks[i] = new FutureTask(task); p.execute(tasks[i]); } - assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); + assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); assertSame(q, p.getQueue()); assertFalse(q.contains(tasks[0])); assertTrue(q.contains(tasks[tasks.length - 1])); @@ -563,7 +576,7 @@ public class ThreadPoolExecutorTest exte }}; p.execute(tasks[i]); } - assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); + assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); assertFalse(p.remove(tasks[0])); assertTrue(q.contains(tasks[4])); assertTrue(q.contains(tasks[3])); @@ -602,7 +615,7 @@ public class ThreadPoolExecutorTest exte tasks[i] = new FutureTask(task); p.execute(tasks[i]); } - assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); + assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); assertEquals(tasks.length, p.getTaskCount()); assertEquals(tasks.length - 1, q.size()); assertEquals(1L, p.getActiveCount()); @@ -622,25 +635,43 @@ public class ThreadPoolExecutorTest exte } /** - * shutdownNow returns a list containing tasks that were not run + * shutdownNow returns a list containing tasks that were not run, + * and those tasks are drained from the queue */ - public void testShutdownNow() { + public void testShutdownNow() throws InterruptedException { + final int poolSize = 2; + final int count = 5; + final AtomicInteger ran = new AtomicInteger(0); final ThreadPoolExecutor p = - new ThreadPoolExecutor(1, 1, + new ThreadPoolExecutor(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); - List l; - try { - for (int i = 0; i < 5; i++) - p.execute(new MediumPossiblyInterruptedRunnable()); - } - finally { + CountDownLatch threadsStarted = new CountDownLatch(poolSize); + Runnable waiter = new CheckedRunnable() { public void realRun() { + threadsStarted.countDown(); try { - l = p.shutdownNow(); - } catch (SecurityException ok) { return; } + MILLISECONDS.sleep(2 * LONG_DELAY_MS); + } catch (InterruptedException success) {} + ran.getAndIncrement(); + }}; + for (int i = 0; i < count; i++) + p.execute(waiter); + assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); + assertEquals(poolSize, p.getActiveCount()); + assertEquals(0, p.getCompletedTaskCount()); + final List queuedTasks; + try { + queuedTasks = p.shutdownNow(); + } catch (SecurityException ok) { + return; // Allowed in case test doesn't have privs } assertTrue(p.isShutdown()); - assertTrue(l.size() <= 4); + assertTrue(p.getQueue().isEmpty()); + assertEquals(count - poolSize, queuedTasks.size()); + assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(p.isTerminated()); + assertEquals(poolSize, ran.get()); + assertEquals(poolSize, p.getCompletedTaskCount()); } // Exception Tests @@ -650,8 +681,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructor1() { try { - new ThreadPoolExecutor(-1, 1, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(-1, 1, 1L, SECONDS, new ArrayBlockingQueue(10)); shouldThrow(); } catch (IllegalArgumentException success) {} @@ -662,8 +692,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructor2() { try { - new ThreadPoolExecutor(1, -1, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(1, -1, 1L, SECONDS, new ArrayBlockingQueue(10)); shouldThrow(); } catch (IllegalArgumentException success) {} @@ -674,8 +703,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructor3() { try { - new ThreadPoolExecutor(1, 0, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(1, 0, 1L, SECONDS, new ArrayBlockingQueue(10)); shouldThrow(); } catch (IllegalArgumentException success) {} @@ -686,8 +714,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructor4() { try { - new ThreadPoolExecutor(1, 2, - -1L, MILLISECONDS, + new ThreadPoolExecutor(1, 2, -1L, SECONDS, new ArrayBlockingQueue(10)); shouldThrow(); } catch (IllegalArgumentException success) {} @@ -698,8 +725,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructor5() { try { - new ThreadPoolExecutor(2, 1, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(2, 1, 1L, SECONDS, new ArrayBlockingQueue(10)); shouldThrow(); } catch (IllegalArgumentException success) {} @@ -710,8 +736,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructorNullPointerException() { try { - new ThreadPoolExecutor(1, 2, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(1, 2, 1L, SECONDS, (BlockingQueue) null); shouldThrow(); } catch (NullPointerException success) {} @@ -722,8 +747,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructor6() { try { - new ThreadPoolExecutor(-1, 1, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(-1, 1, 1L, SECONDS, new ArrayBlockingQueue(10), new SimpleThreadFactory()); shouldThrow(); @@ -735,8 +759,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructor7() { try { - new ThreadPoolExecutor(1, -1, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(1, -1, 1L, SECONDS, new ArrayBlockingQueue(10), new SimpleThreadFactory()); shouldThrow(); @@ -748,8 +771,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructor8() { try { - new ThreadPoolExecutor(1, 0, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(1, 0, 1L, SECONDS, new ArrayBlockingQueue(10), new SimpleThreadFactory()); shouldThrow(); @@ -761,8 +783,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructor9() { try { - new ThreadPoolExecutor(1, 2, - -1L, MILLISECONDS, + new ThreadPoolExecutor(1, 2, -1L, SECONDS, new ArrayBlockingQueue(10), new SimpleThreadFactory()); shouldThrow(); @@ -774,8 +795,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructor10() { try { - new ThreadPoolExecutor(2, 1, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(2, 1, 1L, SECONDS, new ArrayBlockingQueue(10), new SimpleThreadFactory()); shouldThrow(); @@ -787,8 +807,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructorNullPointerException2() { try { - new ThreadPoolExecutor(1, 2, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(1, 2, 1L, SECONDS, (BlockingQueue) null, new SimpleThreadFactory()); shouldThrow(); @@ -800,8 +819,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructorNullPointerException3() { try { - new ThreadPoolExecutor(1, 2, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(1, 2, 1L, SECONDS, new ArrayBlockingQueue(10), (ThreadFactory) null); shouldThrow(); @@ -813,8 +831,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructor11() { try { - new ThreadPoolExecutor(-1, 1, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(-1, 1, 1L, SECONDS, new ArrayBlockingQueue(10), new NoOpREHandler()); shouldThrow(); @@ -826,8 +843,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructor12() { try { - new ThreadPoolExecutor(1, -1, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(1, -1, 1L, SECONDS, new ArrayBlockingQueue(10), new NoOpREHandler()); shouldThrow(); @@ -839,8 +855,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructor13() { try { - new ThreadPoolExecutor(1, 0, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(1, 0, 1L, SECONDS, new ArrayBlockingQueue(10), new NoOpREHandler()); shouldThrow(); @@ -852,8 +867,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructor14() { try { - new ThreadPoolExecutor(1, 2, - -1L, MILLISECONDS, + new ThreadPoolExecutor(1, 2, -1L, SECONDS, new ArrayBlockingQueue(10), new NoOpREHandler()); shouldThrow(); @@ -865,8 +879,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructor15() { try { - new ThreadPoolExecutor(2, 1, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(2, 1, 1L, SECONDS, new ArrayBlockingQueue(10), new NoOpREHandler()); shouldThrow(); @@ -878,8 +891,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructorNullPointerException4() { try { - new ThreadPoolExecutor(1, 2, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(1, 2, 1L, SECONDS, (BlockingQueue) null, new NoOpREHandler()); shouldThrow(); @@ -891,8 +903,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructorNullPointerException5() { try { - new ThreadPoolExecutor(1, 2, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(1, 2, 1L, SECONDS, new ArrayBlockingQueue(10), (RejectedExecutionHandler) null); shouldThrow(); @@ -904,8 +915,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructor16() { try { - new ThreadPoolExecutor(-1, 1, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(-1, 1, 1L, SECONDS, new ArrayBlockingQueue(10), new SimpleThreadFactory(), new NoOpREHandler()); @@ -918,8 +928,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructor17() { try { - new ThreadPoolExecutor(1, -1, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(1, -1, 1L, SECONDS, new ArrayBlockingQueue(10), new SimpleThreadFactory(), new NoOpREHandler()); @@ -932,8 +941,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructor18() { try { - new ThreadPoolExecutor(1, 0, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(1, 0, 1L, SECONDS, new ArrayBlockingQueue(10), new SimpleThreadFactory(), new NoOpREHandler()); @@ -946,8 +954,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructor19() { try { - new ThreadPoolExecutor(1, 2, - -1L, MILLISECONDS, + new ThreadPoolExecutor(1, 2, -1L, SECONDS, new ArrayBlockingQueue(10), new SimpleThreadFactory(), new NoOpREHandler()); @@ -960,8 +967,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructor20() { try { - new ThreadPoolExecutor(2, 1, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(2, 1, 1L, SECONDS, new ArrayBlockingQueue(10), new SimpleThreadFactory(), new NoOpREHandler()); @@ -974,8 +980,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructorNullPointerException6() { try { - new ThreadPoolExecutor(1, 2, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(1, 2, 1L, SECONDS, (BlockingQueue) null, new SimpleThreadFactory(), new NoOpREHandler()); @@ -988,8 +993,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructorNullPointerException7() { try { - new ThreadPoolExecutor(1, 2, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(1, 2, 1L, SECONDS, new ArrayBlockingQueue(10), new SimpleThreadFactory(), (RejectedExecutionHandler) null); @@ -1002,8 +1006,7 @@ public class ThreadPoolExecutorTest exte */ public void testConstructorNullPointerException8() { try { - new ThreadPoolExecutor(1, 2, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(1, 2, 1L, SECONDS, new ArrayBlockingQueue(10), (ThreadFactory) null, new NoOpREHandler()); @@ -1017,7 +1020,7 @@ public class ThreadPoolExecutorTest exte public void testInterruptedSubmit() throws InterruptedException { final ThreadPoolExecutor p = new ThreadPoolExecutor(1, 1, - 60, TimeUnit.SECONDS, + 60, SECONDS, new ArrayBlockingQueue(10)); final CountDownLatch threadStarted = new CountDownLatch(1); @@ -1034,7 +1037,7 @@ public class ThreadPoolExecutorTest exte p.submit(task).get(); }}); - assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); + assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); t.interrupt(); awaitTermination(t, MEDIUM_DELAY_MS); } finally { @@ -1291,8 +1294,7 @@ public class ThreadPoolExecutorTest exte */ public void testExecuteNull() { ThreadPoolExecutor p = - new ThreadPoolExecutor(1, 2, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(1, 2, 1L, SECONDS, new ArrayBlockingQueue(10)); try { p.execute(null); @@ -1359,6 +1361,34 @@ public class ThreadPoolExecutorTest exte } /** + * Configuration changes that allow core pool size greater than + * max pool size result in IllegalArgumentException. + */ + public void testPoolSizeInvariants() { + ThreadPoolExecutor p = + new ThreadPoolExecutor(1, 1, + LONG_DELAY_MS, MILLISECONDS, + new ArrayBlockingQueue(10)); + for (int s = 1; s < 5; s++) { + p.setMaximumPoolSize(s); + p.setCorePoolSize(s); + try { + p.setMaximumPoolSize(s - 1); + shouldThrow(); + } catch (IllegalArgumentException success) {} + assertEquals(s, p.getCorePoolSize()); + assertEquals(s, p.getMaximumPoolSize()); + try { + p.setCorePoolSize(s + 1); + shouldThrow(); + } catch (IllegalArgumentException success) {} + assertEquals(s, p.getCorePoolSize()); + assertEquals(s, p.getMaximumPoolSize()); + } + joinPool(p); + } + + /** * setKeepAliveTime throws IllegalArgumentException * when given a negative value */ @@ -1874,7 +1904,7 @@ public class ThreadPoolExecutorTest exte 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()); @@ -1892,17 +1922,29 @@ public class ThreadPoolExecutorTest exte LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); try { - List> l = new ArrayList>(); - l.add(new StringTask()); - l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), 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()); - assertTrue(futures.get(1).isCancelled()); + for (long timeout = timeoutMillis();;) { + List> tasks = new ArrayList<>(); + tasks.add(new StringTask("0")); + tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), 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()); + assertTrue(futures.get(1).isCancelled()); + try { + assertEquals("0", futures.get(0).get()); + assertEquals("2", futures.get(2).get()); + break; + } catch (CancellationException retryWithLongerTimeout) { + timeout *= 2; + if (timeout >= LONG_DELAY_MS / 2) + fail("expected exactly one task to be cancelled"); + } + } } finally { joinPool(e); } @@ -1948,10 +1990,10 @@ public class ThreadPoolExecutorTest exte * allowCoreThreadTimeOut(true) causes idle threads to time out */ public void testAllowCoreThreadTimeOut_true() throws Exception { - long coreThreadTimeOut = SHORT_DELAY_MS; + long keepAliveTime = timeoutMillis(); final ThreadPoolExecutor p = new ThreadPoolExecutor(2, 10, - coreThreadTimeOut, MILLISECONDS, + keepAliveTime, MILLISECONDS, new ArrayBlockingQueue(10)); final CountDownLatch threadStarted = new CountDownLatch(1); try { @@ -1962,7 +2004,7 @@ public class ThreadPoolExecutorTest exte assertEquals(1, p.getPoolSize()); }}); await(threadStarted); - delay(coreThreadTimeOut); + delay(keepAliveTime); long startTime = System.nanoTime(); while (p.getPoolSize() > 0 && millisElapsedSince(startTime) < LONG_DELAY_MS) @@ -1978,10 +2020,10 @@ public class ThreadPoolExecutorTest exte * allowCoreThreadTimeOut(false) causes idle threads not to time out */ public void testAllowCoreThreadTimeOut_false() throws Exception { - long coreThreadTimeOut = SHORT_DELAY_MS; + long keepAliveTime = timeoutMillis(); final ThreadPoolExecutor p = new ThreadPoolExecutor(2, 10, - coreThreadTimeOut, MILLISECONDS, + keepAliveTime, MILLISECONDS, new ArrayBlockingQueue(10)); final CountDownLatch threadStarted = new CountDownLatch(1); try { @@ -1991,7 +2033,7 @@ public class ThreadPoolExecutorTest exte threadStarted.countDown(); assertTrue(p.getPoolSize() >= 1); }}); - delay(2 * coreThreadTimeOut); + delay(2 * keepAliveTime); assertTrue(p.getPoolSize() >= 1); } finally { joinPool(p); @@ -2010,7 +2052,8 @@ public class ThreadPoolExecutorTest exte done.countDown(); }}; final ThreadPoolExecutor p = - new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS, + new ThreadPoolExecutor(1, 30, + 60, SECONDS, new ArrayBlockingQueue(30)); try { for (int i = 0; i < nTasks; ++i) { @@ -2029,4 +2072,44 @@ public class ThreadPoolExecutorTest exte } } + /** + * get(cancelled task) throws CancellationException + */ + public void testGet_cancelled() throws Exception { + final ExecutorService e = + new ThreadPoolExecutor(1, 1, + LONG_DELAY_MS, MILLISECONDS, + new LinkedBlockingQueue()); + try { + final CountDownLatch blockerStarted = new CountDownLatch(1); + final CountDownLatch done = new CountDownLatch(1); + final List> futures = new ArrayList<>(); + for (int i = 0; i < 2; i++) { + Runnable r = new CheckedRunnable() { public void realRun() + throws Throwable { + blockerStarted.countDown(); + assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS)); + }}; + futures.add(e.submit(r)); + } + assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); + for (Future future : futures) future.cancel(false); + for (Future future : futures) { + try { + future.get(); + shouldThrow(); + } catch (CancellationException success) {} + try { + future.get(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (CancellationException success) {} + assertTrue(future.isCancelled()); + assertTrue(future.isDone()); + } + done.countDown(); + } finally { + joinPool(e); + } + } + }