--- jsr166/src/test/tck/ThreadPoolExecutorTest.java 2010/10/11 07:21:32 1.37 +++ jsr166/src/test/tck/ThreadPoolExecutorTest.java 2015/09/28 08:23:49 1.63 @@ -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 */ @@ -150,12 +182,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()); + long startTime = System.nanoTime(); + while (p.getCompletedTaskCount() != 1) { + if (millisElapsedSince(startTime) > LONG_DELAY_MS) + fail("timed out"); + Thread.yield(); + } } finally { joinPool(p); } @@ -181,11 +217,10 @@ public class ThreadPoolExecutorTest exte new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue(10)); - assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS)); + assertEquals(1, p.getKeepAliveTime(SECONDS)); joinPool(p); } - /** * getThreadFactory returns factory in constructor if not set */ @@ -215,7 +250,6 @@ public class ThreadPoolExecutorTest exte joinPool(p); } - /** * setThreadFactory(null) throws NPE */ @@ -262,7 +296,6 @@ public class ThreadPoolExecutorTest exte joinPool(p); } - /** * setRejectedExecutionHandler(null) throws NPE */ @@ -280,7 +313,6 @@ public class ThreadPoolExecutorTest exte } } - /** * getLargestPoolSize increases, but doesn't overestimate, when * multiple threads active @@ -378,7 +410,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 +423,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 +467,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; } @@ -433,8 +495,8 @@ public class ThreadPoolExecutorTest exte assertFalse(p.isTerminating()); p.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadStarted.countDown(); assertFalse(p.isTerminating()); + threadStarted.countDown(); done.await(); }}); assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); @@ -563,37 +625,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 +682,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 +693,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 +704,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 +715,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 +726,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 +749,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 +761,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 +773,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 +785,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 +797,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 +809,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 +833,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 +845,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 +857,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 +869,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 +881,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 +893,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 +918,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 +931,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 +944,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 +957,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 +970,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 +983,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 +996,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 +1010,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 +1258,6 @@ public class ThreadPoolExecutorTest exte } } - /** * execute using DiscardOldestPolicy drops task on shutdown */ @@ -1233,14 +1279,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 +1350,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 +1403,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 +1413,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 +1479,6 @@ public class ThreadPoolExecutorTest exte } } - /** * invokeAny(null) throws NPE */ @@ -1599,8 +1672,6 @@ public class ThreadPoolExecutorTest exte } } - - /** * timed invokeAny(null) throws NPE */ @@ -1841,22 +1912,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 +1980,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 +2010,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 +2023,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 +2042,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 +2058,7 @@ public class ThreadPoolExecutorTest exte // enough time to run all tasks assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); } finally { - p.shutdown(); + joinPool(p); } }