--- jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java 2013/09/25 07:39:17 1.32 +++ jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java 2015/09/14 03:27:11 1.39 @@ -6,16 +6,39 @@ * Pat Fisher, Mike Judd. */ -import junit.framework.*; -import java.util.concurrent.*; import static java.util.concurrent.TimeUnit.MILLISECONDS; +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.RunnableFuture; +import java.util.concurrent.SynchronousQueue; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; -import java.util.*; + +import junit.framework.Test; +import junit.framework.TestSuite; public class ThreadPoolExecutorSubclassTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(ThreadPoolExecutorSubclassTest.class); @@ -37,7 +60,7 @@ public class ThreadPoolExecutorSubclassT CustomTask(final Runnable r, final V res) { if (r == null) throw new NullPointerException(); callable = new Callable() { - public V call() throws Exception { r.run(); return res; }}; + public V call() throws Exception { r.run(); return res; }}; } public boolean isDone() { lock.lock(); try { return done; } finally { lock.unlock() ; } @@ -323,7 +346,7 @@ public class ThreadPoolExecutorSubclassT */ public void testGetKeepAliveTime() { ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue(10)); - assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS)); + assertEquals(1, p.getKeepAliveTime(SECONDS)); joinPool(p); } @@ -701,7 +724,8 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructor1() { try { - new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); + new CustomTPE(-1, 1, 1L, SECONDS, + new ArrayBlockingQueue(10)); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -711,7 +735,8 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructor2() { try { - new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); + new CustomTPE(1, -1, 1L, SECONDS, + new ArrayBlockingQueue(10)); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -721,7 +746,8 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructor3() { try { - new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); + new CustomTPE(1, 0, 1L, SECONDS, + new ArrayBlockingQueue(10)); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -731,7 +757,8 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructor4() { try { - new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue(10)); + new CustomTPE(1, 2, -1L, SECONDS, + new ArrayBlockingQueue(10)); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -741,7 +768,8 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructor5() { try { - new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); + new CustomTPE(2, 1, 1L, SECONDS, + new ArrayBlockingQueue(10)); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -751,7 +779,7 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructorNullPointerException() { try { - new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null); + new CustomTPE(1, 2, 1L, SECONDS, null); shouldThrow(); } catch (NullPointerException success) {} } @@ -761,7 +789,9 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructor6() { try { - new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory()); + new CustomTPE(-1, 1, 1L, SECONDS, + new ArrayBlockingQueue(10), + new SimpleThreadFactory()); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -771,7 +801,9 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructor7() { try { - new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory()); + new CustomTPE(1,-1, 1L, SECONDS, + new ArrayBlockingQueue(10), + new SimpleThreadFactory()); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -781,7 +813,9 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructor8() { try { - new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory()); + new CustomTPE(1, 0, 1L, SECONDS, + new ArrayBlockingQueue(10), + new SimpleThreadFactory()); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -791,7 +825,9 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructor9() { try { - new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory()); + new CustomTPE(1, 2, -1L, SECONDS, + new ArrayBlockingQueue(10), + new SimpleThreadFactory()); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -801,7 +837,9 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructor10() { try { - new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory()); + new CustomTPE(2, 1, 1L, SECONDS, + new ArrayBlockingQueue(10), + new SimpleThreadFactory()); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -811,7 +849,7 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructorNullPointerException2() { try { - new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory()); + new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory()); shouldThrow(); } catch (NullPointerException success) {} } @@ -821,8 +859,9 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructorNullPointerException3() { try { - ThreadFactory f = null; - new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue(10),f); + new CustomTPE(1, 2, 1L, SECONDS, + new ArrayBlockingQueue(10), + (ThreadFactory) null); shouldThrow(); } catch (NullPointerException success) {} } @@ -832,7 +871,9 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructor11() { try { - new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10),new NoOpREHandler()); + new CustomTPE(-1, 1, 1L, SECONDS, + new ArrayBlockingQueue(10), + new NoOpREHandler()); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -842,7 +883,9 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructor12() { try { - new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10),new NoOpREHandler()); + new CustomTPE(1, -1, 1L, SECONDS, + new ArrayBlockingQueue(10), + new NoOpREHandler()); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -852,7 +895,9 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructor13() { try { - new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10),new NoOpREHandler()); + new CustomTPE(1, 0, 1L, SECONDS, + new ArrayBlockingQueue(10), + new NoOpREHandler()); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -862,7 +907,9 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructor14() { try { - new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue(10),new NoOpREHandler()); + new CustomTPE(1, 2, -1L, SECONDS, + new ArrayBlockingQueue(10), + new NoOpREHandler()); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -872,7 +919,9 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructor15() { try { - new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10),new NoOpREHandler()); + new CustomTPE(2, 1, 1L, SECONDS, + new ArrayBlockingQueue(10), + new NoOpREHandler()); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -882,7 +931,9 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructorNullPointerException4() { try { - new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler()); + new CustomTPE(1, 2, 1L, SECONDS, + null, + new NoOpREHandler()); shouldThrow(); } catch (NullPointerException success) {} } @@ -892,8 +943,9 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructorNullPointerException5() { try { - RejectedExecutionHandler r = null; - new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue(10),r); + new CustomTPE(1, 2, 1L, SECONDS, + new ArrayBlockingQueue(10), + (RejectedExecutionHandler) null); shouldThrow(); } catch (NullPointerException success) {} } @@ -903,7 +955,10 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructor16() { try { - new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory(),new NoOpREHandler()); + new CustomTPE(-1, 1, 1L, SECONDS, + new ArrayBlockingQueue(10), + new SimpleThreadFactory(), + new NoOpREHandler()); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -913,7 +968,10 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructor17() { try { - new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory(),new NoOpREHandler()); + new CustomTPE(1, -1, 1L, SECONDS, + new ArrayBlockingQueue(10), + new SimpleThreadFactory(), + new NoOpREHandler()); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -923,7 +981,10 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructor18() { try { - new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory(),new NoOpREHandler()); + new CustomTPE(1, 0, 1L, SECONDS, + new ArrayBlockingQueue(10), + new SimpleThreadFactory(), + new NoOpREHandler()); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -933,7 +994,10 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructor19() { try { - new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory(),new NoOpREHandler()); + new CustomTPE(1, 2, -1L, SECONDS, + new ArrayBlockingQueue(10), + new SimpleThreadFactory(), + new NoOpREHandler()); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -943,7 +1007,10 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructor20() { try { - new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory(),new NoOpREHandler()); + new CustomTPE(2, 1, 1L, SECONDS, + new ArrayBlockingQueue(10), + new SimpleThreadFactory(), + new NoOpREHandler()); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -953,7 +1020,10 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructorNullPointerException6() { try { - new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); + new CustomTPE(1, 2, 1L, SECONDS, + null, + new SimpleThreadFactory(), + new NoOpREHandler()); shouldThrow(); } catch (NullPointerException success) {} } @@ -963,8 +1033,10 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructorNullPointerException7() { try { - RejectedExecutionHandler r = null; - new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue(10),new SimpleThreadFactory(),r); + new CustomTPE(1, 2, 1L, SECONDS, + new ArrayBlockingQueue(10), + new SimpleThreadFactory(), + (RejectedExecutionHandler) null); shouldThrow(); } catch (NullPointerException success) {} } @@ -974,8 +1046,7 @@ public class ThreadPoolExecutorSubclassT */ public void testConstructorNullPointerException8() { try { - new CustomTPE(1, 2, - LONG_DELAY_MS, MILLISECONDS, + new CustomTPE(1, 2, 1L, SECONDS, new ArrayBlockingQueue(10), (ThreadFactory) null, new NoOpREHandler()); @@ -1153,9 +1224,10 @@ public class ThreadPoolExecutorSubclassT * execute(null) throws NPE */ public void testExecuteNull() { - ThreadPoolExecutor p = null; + ThreadPoolExecutor p = + new CustomTPE(1, 2, 1L, SECONDS, + new ArrayBlockingQueue(10)); try { - p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue(10)); p.execute(null); shouldThrow(); } catch (NullPointerException success) {} @@ -1666,17 +1738,29 @@ public class ThreadPoolExecutorSubclassT public void testTimedInvokeAll6() throws Exception { ExecutorService e = new CustomTPE(2, 2, 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); } @@ -1719,21 +1803,21 @@ public class ThreadPoolExecutorSubclassT * 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 CustomTPE(2, 10, - coreThreadTimeOut, 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()); }}); await(threadStarted); - delay(coreThreadTimeOut); + delay(keepAliveTime); long startTime = System.nanoTime(); while (p.getPoolSize() > 0 && millisElapsedSince(startTime) < LONG_DELAY_MS) @@ -1749,10 +1833,10 @@ public class ThreadPoolExecutorSubclassT * 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 CustomTPE(2, 10, - coreThreadTimeOut, MILLISECONDS, + keepAliveTime, MILLISECONDS, new ArrayBlockingQueue(10)); final CountDownLatch threadStarted = new CountDownLatch(1); try { @@ -1762,7 +1846,7 @@ public class ThreadPoolExecutorSubclassT threadStarted.countDown(); assertTrue(p.getPoolSize() >= 1); }}); - delay(2 * coreThreadTimeOut); + delay(2 * keepAliveTime); assertTrue(p.getPoolSize() >= 1); } finally { joinPool(p);