--- jsr166/src/test/tck/ThreadPoolExecutorTest.java 2010/11/17 23:12:31 1.38 +++ jsr166/src/test/tck/ThreadPoolExecutorTest.java 2015/10/04 02:09:57 1.78 @@ -1,40 +1,73 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ * Other contributors include Andrew Wright, Jeffrey Hayes, * Pat Fisher, Mike Judd. */ -import java.util.concurrent.*; import static java.util.concurrent.TimeUnit.MILLISECONDS; -import java.util.concurrent.atomic.*; -import junit.framework.*; -import java.util.*; +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; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.concurrent.FutureTask; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.RejectedExecutionHandler; +import java.util.concurrent.SynchronousQueue; +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; public class ThreadPoolExecutorTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(ThreadPoolExecutorTest.class); } static class ExtendedTPE extends ThreadPoolExecutor { - volatile boolean beforeCalled = false; - volatile boolean afterCalled = false; - volatile boolean terminatedCalled = false; + final CountDownLatch beforeCalled = new CountDownLatch(1); + final CountDownLatch afterCalled = new CountDownLatch(1); + final CountDownLatch terminatedCalled = new CountDownLatch(1); + public ExtendedTPE() { super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue()); } protected void beforeExecute(Thread t, Runnable r) { - beforeCalled = true; + beforeCalled.countDown(); } protected void afterExecute(Runnable r, Throwable t) { - afterCalled = true; + afterCalled.countDown(); } protected void terminated() { - terminatedCalled = true; + terminatedCalled.countDown(); + } + + public boolean beforeCalled() { + return beforeCalled.getCount() == 0; + } + public boolean afterCalled() { + return afterCalled.getCount() == 0; + } + public boolean terminatedCalled() { + return terminatedCalled.getCount() == 0; } } @@ -46,7 +79,6 @@ public class ThreadPoolExecutorTest exte } } - /** * execute successfully executes a runnable */ @@ -100,17 +132,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()); + } } /** @@ -118,15 +158,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()); + } } /** @@ -138,10 +184,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 { @@ -150,14 +196,16 @@ public class ThreadPoolExecutorTest exte threadProceed.await(); threadDone.countDown(); }}); - assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); + await(threadStarted); assertEquals(0, p.getCompletedTaskCount()); threadProceed.countDown(); threadDone.await(); - Thread.sleep(SHORT_DELAY_MS); - assertEquals(1, p.getCompletedTaskCount()); - } finally { - joinPool(p); + long startTime = System.nanoTime(); + while (p.getCompletedTaskCount() != 1) { + if (millisElapsedSince(startTime) > LONG_DELAY_MS) + fail("timed out"); + Thread.yield(); + } } } @@ -169,8 +217,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()); + } } /** @@ -181,24 +230,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()); + } } /** @@ -209,13 +259,13 @@ 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()); + } } - /** * setThreadFactory(null) throws NPE */ @@ -224,12 +274,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) {} } } @@ -237,14 +286,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()); + } } /** @@ -256,13 +306,13 @@ 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()); + } } - /** * setRejectedExecutionHandler(null) throws NPE */ @@ -271,16 +321,14 @@ 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) {} } } - /** * getLargestPoolSize increases, but doesn't overestimate, when * multiple threads active @@ -291,9 +339,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() { @@ -302,13 +350,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()); } /** @@ -320,8 +366,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()); + } } /** @@ -333,9 +384,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 { @@ -343,11 +394,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 } } @@ -378,7 +427,7 @@ public class ThreadPoolExecutorTest exte } /** - * isShutDown is false before shutdown, true after + * isShutdown is false before shutdown, true after */ public void testIsShutdown() { final ThreadPoolExecutor p = @@ -391,6 +440,35 @@ public class ThreadPoolExecutorTest exte joinPool(p); } + /** + * awaitTermination on a non-shutdown pool times out + */ + public void testAwaitTermination_timesOut() throws InterruptedException { + final ThreadPoolExecutor p = + new ThreadPoolExecutor(1, 1, + LONG_DELAY_MS, MILLISECONDS, + new ArrayBlockingQueue(10)); + assertFalse(p.isTerminated()); + assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); + assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); + assertFalse(p.awaitTermination(-1L, NANOSECONDS)); + assertFalse(p.awaitTermination(-1L, MILLISECONDS)); + assertFalse(p.awaitTermination(0L, NANOSECONDS)); + assertFalse(p.awaitTermination(0L, MILLISECONDS)); + long timeoutNanos = 999999L; + long startTime = System.nanoTime(); + assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); + assertTrue(System.nanoTime() - startTime >= timeoutNanos); + assertFalse(p.isTerminated()); + startTime = System.nanoTime(); + long timeoutMillis = timeoutMillis(); + assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis); + assertFalse(p.isTerminated()); + p.shutdown(); + assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(p.isTerminated()); + } /** * isTerminated is false before termination, true after @@ -406,11 +484,12 @@ public class ThreadPoolExecutorTest exte try { p.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadStarted.countDown(); assertFalse(p.isTerminated()); + threadStarted.countDown(); done.await(); }}); assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); + assertFalse(p.isTerminating()); done.countDown(); } finally { try { p.shutdown(); } catch (SecurityException ok) { return; } @@ -563,37 +642,53 @@ 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 - /** * Constructor throws if corePoolSize argument is less than zero */ 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) {} @@ -604,8 +699,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) {} @@ -616,8 +710,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) {} @@ -628,8 +721,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) {} @@ -640,8 +732,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) {} @@ -652,22 +743,18 @@ 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) {} } - - /** * Constructor throws if corePoolSize argument is less than zero */ public void testConstructor6() { try { - new ThreadPoolExecutor(-1, 1, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(-1, 1, 1L, SECONDS, new ArrayBlockingQueue(10), new SimpleThreadFactory()); shouldThrow(); @@ -679,8 +766,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(); @@ -692,8 +778,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(); @@ -705,8 +790,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(); @@ -718,8 +802,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(); @@ -731,8 +814,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(); @@ -744,22 +826,19 @@ 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(); } catch (NullPointerException success) {} } - /** * Constructor throws if corePoolSize argument is less than zero */ public void testConstructor11() { try { - new ThreadPoolExecutor(-1, 1, - LONG_DELAY_MS, MILLISECONDS, + new ThreadPoolExecutor(-1, 1, 1L, SECONDS, new ArrayBlockingQueue(10), new NoOpREHandler()); shouldThrow(); @@ -771,8 +850,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(); @@ -784,8 +862,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(); @@ -797,8 +874,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(); @@ -810,8 +886,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(); @@ -823,8 +898,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(); @@ -836,22 +910,19 @@ 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(); } catch (NullPointerException success) {} } - /** * Constructor throws if corePoolSize argument is less than zero */ 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()); @@ -864,8 +935,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()); @@ -878,8 +948,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()); @@ -892,8 +961,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()); @@ -906,8 +974,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()); @@ -920,8 +987,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()); @@ -934,8 +1000,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); @@ -948,8 +1013,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()); @@ -963,7 +1027,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); @@ -1211,7 +1275,6 @@ public class ThreadPoolExecutorTest exte } } - /** * execute using DiscardOldestPolicy drops task on shutdown */ @@ -1233,14 +1296,12 @@ public class ThreadPoolExecutorTest exte } } - /** * execute(null) throws NPE */ 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); @@ -1306,6 +1367,33 @@ public class ThreadPoolExecutorTest exte joinPool(p); } + /** + * 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 @@ -1332,7 +1420,7 @@ public class ThreadPoolExecutorTest exte public void testTerminated() { ExtendedTPE p = new ExtendedTPE(); try { p.shutdown(); } catch (SecurityException ok) { return; } - assertTrue(p.terminatedCalled); + assertTrue(p.terminatedCalled()); joinPool(p); } @@ -1342,12 +1430,15 @@ public class ThreadPoolExecutorTest exte public void testBeforeAfter() throws InterruptedException { ExtendedTPE p = new ExtendedTPE(); try { - TrackedNoOpRunnable r = new TrackedNoOpRunnable(); - p.execute(r); - Thread.sleep(SHORT_DELAY_MS); - assertTrue(r.done); - assertTrue(p.beforeCalled); - assertTrue(p.afterCalled); + final CountDownLatch done = new CountDownLatch(1); + p.execute(new CheckedRunnable() { + public void realRun() { + done.countDown(); + }}); + await(p.afterCalled); + assertEquals(0, done.getCount()); + assertTrue(p.afterCalled()); + assertTrue(p.beforeCalled()); try { p.shutdown(); } catch (SecurityException ok) { return; } } finally { joinPool(p); @@ -1405,7 +1496,6 @@ public class ThreadPoolExecutorTest exte } } - /** * invokeAny(null) throws NPE */ @@ -1599,8 +1689,6 @@ public class ThreadPoolExecutorTest exte } } - - /** * timed invokeAny(null) throws NPE */ @@ -1823,7 +1911,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()); @@ -1841,22 +1929,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(3, futures.size()); - Iterator> it = futures.iterator(); - Future f1 = it.next(); - Future f2 = it.next(); - Future f3 = it.next(); - assertTrue(f1.isDone()); - assertTrue(f2.isDone()); - assertTrue(f3.isDone()); - assertFalse(f1.isCancelled()); - assertTrue(f2.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); } @@ -1902,24 +1997,26 @@ public class ThreadPoolExecutorTest exte * allowCoreThreadTimeOut(true) causes idle threads to time out */ public void testAllowCoreThreadTimeOut_true() throws Exception { + long keepAliveTime = timeoutMillis(); final ThreadPoolExecutor p = new ThreadPoolExecutor(2, 10, - SHORT_DELAY_MS, MILLISECONDS, + keepAliveTime, MILLISECONDS, new ArrayBlockingQueue(10)); final CountDownLatch threadStarted = new CountDownLatch(1); try { p.allowCoreThreadTimeOut(true); p.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { + public void realRun() { threadStarted.countDown(); assertEquals(1, p.getPoolSize()); }}); - assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); - for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) { - if (p.getPoolSize() == 0) - break; - Thread.sleep(10); - } + await(threadStarted); + delay(keepAliveTime); + long startTime = System.nanoTime(); + while (p.getPoolSize() > 0 + && millisElapsedSince(startTime) < LONG_DELAY_MS) + Thread.yield(); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); assertEquals(0, p.getPoolSize()); } finally { joinPool(p); @@ -1930,9 +2027,10 @@ public class ThreadPoolExecutorTest exte * allowCoreThreadTimeOut(false) causes idle threads not to time out */ public void testAllowCoreThreadTimeOut_false() throws Exception { + long keepAliveTime = timeoutMillis(); final ThreadPoolExecutor p = new ThreadPoolExecutor(2, 10, - SHORT_DELAY_MS, MILLISECONDS, + keepAliveTime, MILLISECONDS, new ArrayBlockingQueue(10)); final CountDownLatch threadStarted = new CountDownLatch(1); try { @@ -1942,7 +2040,7 @@ public class ThreadPoolExecutorTest exte threadStarted.countDown(); assertTrue(p.getPoolSize() >= 1); }}); - Thread.sleep(SMALL_DELAY_MS); + delay(2 * keepAliveTime); assertTrue(p.getPoolSize() >= 1); } finally { joinPool(p); @@ -1961,7 +2059,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) { @@ -1976,7 +2075,47 @@ public class ThreadPoolExecutorTest exte // enough time to run all tasks assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); } finally { - p.shutdown(); + joinPool(p); + } + } + + /** + * 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); } }