--- jsr166/src/test/tck/ThreadPoolExecutorTest.java 2015/10/05 20:45:41 1.99 +++ jsr166/src/test/tck/ThreadPoolExecutorTest.java 2017/05/29 22:44:27 1.119 @@ -11,6 +11,8 @@ import static java.util.concurrent.TimeU import static java.util.concurrent.TimeUnit.SECONDS; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; @@ -18,7 +20,6 @@ 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; @@ -28,7 +29,6 @@ import java.util.concurrent.RejectedExec 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; @@ -92,7 +92,7 @@ public class ThreadPoolExecutorTest exte final Runnable task = new CheckedRunnable() { public void realRun() { done.countDown(); }}; p.execute(task); - assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); + await(done); } } @@ -101,23 +101,22 @@ public class ThreadPoolExecutorTest exte * thread becomes active */ public void testGetActiveCount() throws InterruptedException { + final CountDownLatch done = new CountDownLatch(1); final ThreadPoolExecutor p = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); - try (PoolCleaner cleaner = cleaner(p)) { + try (PoolCleaner cleaner = cleaner(p, done)) { 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 { threadStarted.countDown(); assertEquals(1, p.getActiveCount()); - done.await(); + await(done); }}); - assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); + await(threadStarted); assertEquals(1, p.getActiveCount()); - done.countDown(); } } @@ -187,13 +186,13 @@ public class ThreadPoolExecutorTest exte public void realRun() throws InterruptedException { threadStarted.countDown(); assertEquals(0, p.getCompletedTaskCount()); - threadProceed.await(); + await(threadProceed); threadDone.countDown(); }}); await(threadStarted); assertEquals(0, p.getCompletedTaskCount()); threadProceed.countDown(); - threadDone.await(); + await(threadDone); long startTime = System.nanoTime(); while (p.getCompletedTaskCount() != 1) { if (millisElapsedSince(startTime) > LONG_DELAY_MS) @@ -277,6 +276,20 @@ public class ThreadPoolExecutorTest exte } /** + * The default rejected execution handler is AbortPolicy. + */ + public void testDefaultRejectedExecutionHandler() { + final ThreadPoolExecutor p = + new ThreadPoolExecutor(1, 2, + LONG_DELAY_MS, MILLISECONDS, + new ArrayBlockingQueue(10)); + try (PoolCleaner cleaner = cleaner(p)) { + assertTrue(p.getRejectedExecutionHandler() + instanceof ThreadPoolExecutor.AbortPolicy); + } + } + + /** * getRejectedExecutionHandler returns handler in constructor if not set */ public void testGetRejectedExecutionHandler() { @@ -329,24 +342,23 @@ public class ThreadPoolExecutorTest exte */ public void testGetLargestPoolSize() throws InterruptedException { final int THREADS = 3; + final CountDownLatch done = new CountDownLatch(1); final ThreadPoolExecutor p = new ThreadPoolExecutor(THREADS, THREADS, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); - try (PoolCleaner cleaner = cleaner(p)) { - final CountDownLatch threadsStarted = new CountDownLatch(THREADS); - final CountDownLatch done = new CountDownLatch(1); + try (PoolCleaner cleaner = cleaner(p, done)) { assertEquals(0, p.getLargestPoolSize()); + final CountDownLatch threadsStarted = new CountDownLatch(THREADS); for (int i = 0; i < THREADS; i++) p.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { threadsStarted.countDown(); - done.await(); + await(done); assertEquals(THREADS, p.getLargestPoolSize()); }}); - assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); + await(threadsStarted); assertEquals(THREADS, p.getLargestPoolSize()); - done.countDown(); // release pool } assertEquals(THREADS, p.getLargestPoolSize()); } @@ -374,23 +386,22 @@ public class ThreadPoolExecutorTest exte * become active */ public void testGetPoolSize() throws InterruptedException { + final CountDownLatch done = new CountDownLatch(1); final ThreadPoolExecutor p = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); - try (PoolCleaner cleaner = cleaner(p)) { - final CountDownLatch threadStarted = new CountDownLatch(1); - final CountDownLatch done = new CountDownLatch(1); + try (PoolCleaner cleaner = cleaner(p, done)) { assertEquals(0, p.getPoolSize()); + final CountDownLatch threadStarted = new CountDownLatch(1); p.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { threadStarted.countDown(); assertEquals(1, p.getPoolSize()); - done.await(); + await(done); }}); - assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); + await(threadStarted); assertEquals(1, p.getPoolSize()); - done.countDown(); // release pool } } @@ -398,24 +409,38 @@ public class ThreadPoolExecutorTest exte * getTaskCount increases, but doesn't overestimate, when tasks submitted */ public void testGetTaskCount() throws InterruptedException { + final int TASKS = 3; + final CountDownLatch done = new CountDownLatch(1); final ThreadPoolExecutor p = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); - try (PoolCleaner cleaner = cleaner(p)) { + try (PoolCleaner cleaner = cleaner(p, done)) { final CountDownLatch threadStarted = new CountDownLatch(1); - final CountDownLatch done = new CountDownLatch(1); assertEquals(0, p.getTaskCount()); + assertEquals(0, p.getCompletedTaskCount()); p.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { threadStarted.countDown(); - assertEquals(1, p.getTaskCount()); - done.await(); + await(done); }}); - assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); + await(threadStarted); assertEquals(1, p.getTaskCount()); - done.countDown(); + assertEquals(0, p.getCompletedTaskCount()); + for (int i = 0; i < TASKS; i++) { + assertEquals(1 + i, p.getTaskCount()); + p.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadStarted.countDown(); + assertEquals(1 + TASKS, p.getTaskCount()); + await(done); + }}); + } + assertEquals(1 + TASKS, p.getTaskCount()); + assertEquals(0, p.getCompletedTaskCount()); } + assertEquals(1 + TASKS, p.getTaskCount()); + assertEquals(1 + TASKS, p.getCompletedTaskCount()); } /** @@ -447,8 +472,8 @@ public class ThreadPoolExecutorTest exte 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)); + assertFalse(p.awaitTermination(randomExpiredTimeout(), + randomTimeUnit())); long timeoutNanos = 999999L; long startTime = System.nanoTime(); assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); @@ -481,9 +506,9 @@ public class ThreadPoolExecutorTest exte public void realRun() throws InterruptedException { assertFalse(p.isTerminating()); threadStarted.countDown(); - done.await(); + await(done); }}); - assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); + await(threadStarted); assertFalse(p.isTerminating()); done.countDown(); try { p.shutdown(); } catch (SecurityException ok) { return; } @@ -509,9 +534,9 @@ public class ThreadPoolExecutorTest exte public void realRun() throws InterruptedException { assertFalse(p.isTerminating()); threadStarted.countDown(); - done.await(); + await(done); }}); - assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); + await(threadStarted); assertFalse(p.isTerminating()); done.countDown(); try { p.shutdown(); } catch (SecurityException ok) { return; } @@ -525,32 +550,31 @@ public class ThreadPoolExecutorTest exte * getQueue returns the work queue, which contains queued tasks */ public void testGetQueue() throws InterruptedException { - final BlockingQueue q = new ArrayBlockingQueue(10); + final CountDownLatch done = new CountDownLatch(1); + final BlockingQueue q = new ArrayBlockingQueue<>(10); final ThreadPoolExecutor p = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, q); - try (PoolCleaner cleaner = cleaner(p)) { + try (PoolCleaner cleaner = cleaner(p, done)) { final CountDownLatch threadStarted = new CountDownLatch(1); - final CountDownLatch done = new CountDownLatch(1); FutureTask[] tasks = new FutureTask[5]; for (int i = 0; i < tasks.length; i++) { Callable task = new CheckedCallable() { public Boolean realCall() throws InterruptedException { threadStarted.countDown(); assertSame(q, p.getQueue()); - done.await(); + await(done); return Boolean.TRUE; }}; tasks[i] = new FutureTask(task); p.execute(tasks[i]); } - assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); + await(threadStarted); assertSame(q, p.getQueue()); assertFalse(q.contains(tasks[0])); assertTrue(q.contains(tasks[tasks.length - 1])); assertEquals(tasks.length - 1, q.size()); - done.countDown(); } } @@ -558,24 +582,24 @@ public class ThreadPoolExecutorTest exte * remove(task) removes queued task, and fails to remove active task */ public void testRemove() throws InterruptedException { - BlockingQueue q = new ArrayBlockingQueue(10); + final CountDownLatch done = new CountDownLatch(1); + BlockingQueue q = new ArrayBlockingQueue<>(10); final ThreadPoolExecutor p = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, q); - try (PoolCleaner cleaner = cleaner(p)) { + try (PoolCleaner cleaner = cleaner(p, done)) { Runnable[] tasks = new Runnable[6]; final CountDownLatch threadStarted = new CountDownLatch(1); - final CountDownLatch done = new CountDownLatch(1); for (int i = 0; i < tasks.length; i++) { tasks[i] = new CheckedRunnable() { public void realRun() throws InterruptedException { threadStarted.countDown(); - done.await(); + await(done); }}; p.execute(tasks[i]); } - assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); + await(threadStarted); assertFalse(p.remove(tasks[0])); assertTrue(q.contains(tasks[4])); assertTrue(q.contains(tasks[3])); @@ -585,7 +609,6 @@ public class ThreadPoolExecutorTest exte assertTrue(q.contains(tasks[3])); assertTrue(p.remove(tasks[3])); assertFalse(q.contains(tasks[3])); - done.countDown(); } } @@ -595,24 +618,24 @@ public class ThreadPoolExecutorTest exte public void testPurge() throws InterruptedException { final CountDownLatch threadStarted = new CountDownLatch(1); final CountDownLatch done = new CountDownLatch(1); - final BlockingQueue q = new ArrayBlockingQueue(10); + final BlockingQueue q = new ArrayBlockingQueue<>(10); final ThreadPoolExecutor p = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, q); - try (PoolCleaner cleaner = cleaner(p)) { + try (PoolCleaner cleaner = cleaner(p, done)) { FutureTask[] tasks = new FutureTask[5]; for (int i = 0; i < tasks.length; i++) { Callable task = new CheckedCallable() { public Boolean realCall() throws InterruptedException { threadStarted.countDown(); - done.await(); + await(done); return Boolean.TRUE; }}; tasks[i] = new FutureTask(task); p.execute(tasks[i]); } - assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); + await(threadStarted); assertEquals(tasks.length, p.getTaskCount()); assertEquals(tasks.length - 1, q.size()); assertEquals(1L, p.getActiveCount()); @@ -625,7 +648,6 @@ public class ThreadPoolExecutorTest exte p.purge(); // Nothing to do assertEquals(tasks.length - 3, q.size()); assertEquals(tasks.length - 2, p.getTaskCount()); - done.countDown(); } } @@ -651,7 +673,7 @@ public class ThreadPoolExecutorTest exte }}; for (int i = 0; i < count; i++) p.execute(waiter); - assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); + await(threadsStarted); assertEquals(poolSize, p.getActiveCount()); assertEquals(0, p.getCompletedTaskCount()); final List queuedTasks; @@ -1013,29 +1035,28 @@ public class ThreadPoolExecutorTest exte * get of submitted callable throws InterruptedException if interrupted */ public void testInterruptedSubmit() throws InterruptedException { + final CountDownLatch done = new CountDownLatch(1); final ThreadPoolExecutor p = new ThreadPoolExecutor(1, 1, 60, SECONDS, new ArrayBlockingQueue(10)); - try (PoolCleaner cleaner = cleaner(p)) { + try (PoolCleaner cleaner = cleaner(p, done)) { final CountDownLatch threadStarted = new CountDownLatch(1); - final CountDownLatch done = new CountDownLatch(1); Thread t = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws Exception { Callable task = new CheckedCallable() { public Boolean realCall() throws InterruptedException { threadStarted.countDown(); - done.await(); + await(done); return Boolean.TRUE; }}; p.submit(task).get(); }}); - assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); + await(threadStarted); t.interrupt(); - awaitTermination(t, MEDIUM_DELAY_MS); - done.countDown(); + awaitTermination(t); } } @@ -1043,15 +1064,15 @@ public class ThreadPoolExecutorTest exte * execute throws RejectedExecutionException if saturated. */ public void testSaturatedExecute() { + final CountDownLatch done = new CountDownLatch(1); final ThreadPoolExecutor p = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(1)); - try (PoolCleaner cleaner = cleaner(p)) { - final CountDownLatch done = new CountDownLatch(1); + try (PoolCleaner cleaner = cleaner(p, done)) { Runnable task = new CheckedRunnable() { public void realRun() throws InterruptedException { - done.await(); + await(done); }}; for (int i = 0; i < 2; ++i) p.execute(task); @@ -1062,7 +1083,6 @@ public class ThreadPoolExecutorTest exte } catch (RejectedExecutionException success) {} assertTrue(p.getTaskCount() <= 2); } - done.countDown(); } } @@ -1070,15 +1090,15 @@ public class ThreadPoolExecutorTest exte * submit(runnable) throws RejectedExecutionException if saturated. */ public void testSaturatedSubmitRunnable() { + final CountDownLatch done = new CountDownLatch(1); final ThreadPoolExecutor p = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(1)); - try (PoolCleaner cleaner = cleaner(p)) { - final CountDownLatch done = new CountDownLatch(1); + try (PoolCleaner cleaner = cleaner(p, done)) { Runnable task = new CheckedRunnable() { public void realRun() throws InterruptedException { - done.await(); + await(done); }}; for (int i = 0; i < 2; ++i) p.submit(task); @@ -1089,7 +1109,6 @@ public class ThreadPoolExecutorTest exte } catch (RejectedExecutionException success) {} assertTrue(p.getTaskCount() <= 2); } - done.countDown(); } } @@ -1097,18 +1116,18 @@ public class ThreadPoolExecutorTest exte * submit(callable) throws RejectedExecutionException if saturated. */ public void testSaturatedSubmitCallable() { + final CountDownLatch done = new CountDownLatch(1); final ThreadPoolExecutor p = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(1)); - try (PoolCleaner cleaner = cleaner(p)) { - final CountDownLatch done = new CountDownLatch(1); + try (PoolCleaner cleaner = cleaner(p, done)) { Runnable task = new CheckedRunnable() { public void realRun() throws InterruptedException { - done.await(); + await(done); }}; for (int i = 0; i < 2; ++i) - p.submit(Executors.callable(task)); + p.execute(task); for (int i = 0; i < 2; ++i) { try { p.execute(task); @@ -1116,7 +1135,6 @@ public class ThreadPoolExecutorTest exte } catch (RejectedExecutionException success) {} assertTrue(p.getTaskCount() <= 2); } - done.countDown(); } } @@ -1134,7 +1152,7 @@ public class ThreadPoolExecutorTest exte final CountDownLatch done = new CountDownLatch(1); Runnable blocker = new CheckedRunnable() { public void realRun() throws InterruptedException { - done.await(); + await(done); }}; p.execute(blocker); TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; @@ -1153,6 +1171,7 @@ public class ThreadPoolExecutorTest exte * executor using DiscardPolicy drops task if saturated. */ public void testSaturatedExecute3() { + final CountDownLatch done = new CountDownLatch(1); final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; for (int i = 0; i < tasks.length; ++i) tasks[i] = new TrackedNoOpRunnable(); @@ -1161,15 +1180,13 @@ public class ThreadPoolExecutorTest exte LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(1), new ThreadPoolExecutor.DiscardPolicy()); - try (PoolCleaner cleaner = cleaner(p)) { - final CountDownLatch done = new CountDownLatch(1); + try (PoolCleaner cleaner = cleaner(p, done)) { p.execute(awaiter(done)); for (TrackedNoOpRunnable task : tasks) p.execute(task); for (int i = 1; i < tasks.length; i++) assertFalse(tasks[i].done); - done.countDown(); } for (int i = 1; i < tasks.length; i++) assertFalse(tasks[i].done); @@ -1189,7 +1206,7 @@ public class ThreadPoolExecutorTest exte LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(1), new ThreadPoolExecutor.DiscardOldestPolicy()); - try (PoolCleaner cleaner = cleaner(p)) { + try (PoolCleaner cleaner = cleaner(p, done)) { assertEquals(LatchAwaiter.NEW, r1.state); assertEquals(LatchAwaiter.NEW, r2.state); assertEquals(LatchAwaiter.NEW, r3.state); @@ -1199,7 +1216,6 @@ public class ThreadPoolExecutorTest exte p.execute(r3); assertFalse(p.getQueue().contains(r2)); assertTrue(p.getQueue().contains(r3)); - done.countDown(); } assertEquals(LatchAwaiter.DONE, r1.state); assertEquals(LatchAwaiter.NEW, r2.state); @@ -1481,7 +1497,7 @@ public class ThreadPoolExecutorTest exte } /** - * invokeAny(empty collection) throws IAE + * invokeAny(empty collection) throws IllegalArgumentException */ public void testInvokeAny2() throws Exception { final ExecutorService e = @@ -1506,7 +1522,7 @@ public class ThreadPoolExecutorTest exte LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(latchAwaitingStringTask(latch)); l.add(null); try { @@ -1526,7 +1542,7 @@ public class ThreadPoolExecutorTest exte LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new NPETask()); try { e.invokeAny(l); @@ -1546,7 +1562,7 @@ public class ThreadPoolExecutorTest exte LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(new StringTask()); String result = e.invokeAny(l); @@ -1571,15 +1587,17 @@ public class ThreadPoolExecutorTest exte } /** - * invokeAll(empty collection) returns empty collection + * invokeAll(empty collection) returns empty list */ public void testInvokeAll2() throws InterruptedException { final ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); + final Collection> emptyCollection + = Collections.emptyList(); try (PoolCleaner cleaner = cleaner(e)) { - List> r = e.invokeAll(new ArrayList>()); + List> r = e.invokeAll(emptyCollection); assertTrue(r.isEmpty()); } } @@ -1593,7 +1611,7 @@ public class ThreadPoolExecutorTest exte LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(null); try { @@ -1612,7 +1630,7 @@ public class ThreadPoolExecutorTest exte LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new NPETask()); List> futures = e.invokeAll(l); assertEquals(1, futures.size()); @@ -1634,7 +1652,7 @@ public class ThreadPoolExecutorTest exte LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(new StringTask()); List> futures = e.invokeAll(l); @@ -1654,7 +1672,7 @@ public class ThreadPoolExecutorTest exte new ArrayBlockingQueue(10)); try (PoolCleaner cleaner = cleaner(e)) { try { - e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); + e.invokeAny(null, randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (NullPointerException success) {} } @@ -1669,17 +1687,17 @@ public class ThreadPoolExecutorTest exte LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); try { - e.invokeAny(l, MEDIUM_DELAY_MS, null); + e.invokeAny(l, randomTimeout(), null); shouldThrow(); } catch (NullPointerException success) {} } } /** - * timed invokeAny(empty collection) throws IAE + * timed invokeAny(empty collection) throws IllegalArgumentException */ public void testTimedInvokeAny2() throws Exception { final ExecutorService e = @@ -1689,14 +1707,14 @@ public class ThreadPoolExecutorTest exte try (PoolCleaner cleaner = cleaner(e)) { try { e.invokeAny(new ArrayList>(), - MEDIUM_DELAY_MS, MILLISECONDS); + randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (IllegalArgumentException success) {} } } /** - * timed invokeAny(c) throws NPE if c has null elements + * timed invokeAny(c) throws NullPointerException if c has null elements */ public void testTimedInvokeAny3() throws Exception { final CountDownLatch latch = new CountDownLatch(1); @@ -1705,11 +1723,11 @@ public class ThreadPoolExecutorTest exte LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(latchAwaitingStringTask(latch)); l.add(null); try { - e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); + e.invokeAny(l, randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (NullPointerException success) {} latch.countDown(); @@ -1725,14 +1743,16 @@ public class ThreadPoolExecutorTest exte LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + long startTime = System.nanoTime(); + List> l = new ArrayList<>(); l.add(new NPETask()); try { - e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); + e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (ExecutionException success) { assertTrue(success.getCause() instanceof NullPointerException); } + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } } @@ -1745,11 +1765,13 @@ public class ThreadPoolExecutorTest exte LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + long startTime = System.nanoTime(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(new StringTask()); - String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); + String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); assertSame(TEST_STRING, result); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } } @@ -1763,7 +1785,7 @@ public class ThreadPoolExecutorTest exte new ArrayBlockingQueue(10)); try (PoolCleaner cleaner = cleaner(e)) { try { - e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); + e.invokeAll(null, randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (NullPointerException success) {} } @@ -1778,26 +1800,28 @@ public class ThreadPoolExecutorTest exte LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); try { - e.invokeAll(l, MEDIUM_DELAY_MS, null); + e.invokeAll(l, randomTimeout(), null); shouldThrow(); } catch (NullPointerException success) {} } } /** - * timed invokeAll(empty collection) returns empty collection + * timed invokeAll(empty collection) returns empty list */ public void testTimedInvokeAll2() throws InterruptedException { final ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); + final Collection> emptyCollection + = Collections.emptyList(); try (PoolCleaner cleaner = cleaner(e)) { - List> r = e.invokeAll(new ArrayList>(), - MEDIUM_DELAY_MS, MILLISECONDS); + List> r = + e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit()); assertTrue(r.isEmpty()); } } @@ -1811,11 +1835,11 @@ public class ThreadPoolExecutorTest exte LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(null); try { - e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); + e.invokeAll(l, randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (NullPointerException success) {} } @@ -1830,10 +1854,10 @@ public class ThreadPoolExecutorTest exte LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new NPETask()); List> futures = - e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); + e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); assertEquals(1, futures.size()); try { futures.get(0).get(); @@ -1853,7 +1877,7 @@ public class ThreadPoolExecutorTest exte LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(new StringTask()); List> futures = @@ -1868,19 +1892,25 @@ public class ThreadPoolExecutorTest exte * timed invokeAll(c) cancels tasks not completed by timeout */ public void testTimedInvokeAll6() throws Exception { - final ExecutorService e = - new ThreadPoolExecutor(2, 2, - LONG_DELAY_MS, MILLISECONDS, - new ArrayBlockingQueue(10)); - try (PoolCleaner cleaner = cleaner(e)) { - for (long timeout = timeoutMillis();;) { + for (long timeout = timeoutMillis();;) { + final CountDownLatch done = new CountDownLatch(1); + final Callable waiter = new CheckedCallable() { + public String realCall() { + try { done.await(LONG_DELAY_MS, MILLISECONDS); } + catch (InterruptedException ok) {} + return "1"; }}; + final ExecutorService p = + new ThreadPoolExecutor(2, 2, + LONG_DELAY_MS, MILLISECONDS, + new ArrayBlockingQueue(10)); + try (PoolCleaner cleaner = cleaner(p, done)) { List> tasks = new ArrayList<>(); tasks.add(new StringTask("0")); - tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); + tasks.add(waiter); tasks.add(new StringTask("2")); long startTime = System.nanoTime(); List> futures = - e.invokeAll(tasks, timeout, MILLISECONDS); + p.invokeAll(tasks, timeout, MILLISECONDS); assertEquals(tasks.size(), futures.size()); assertTrue(millisElapsedSince(startTime) >= timeout); for (Future future : futures) @@ -1917,7 +1947,7 @@ public class ThreadPoolExecutorTest exte public void realRun() { done.countDown(); }}); - assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); + await(done); } } @@ -2010,7 +2040,7 @@ public class ThreadPoolExecutorTest exte } } // enough time to run all tasks - assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); + await(done, nTasks * SHORT_DELAY_MS); } } @@ -2018,13 +2048,13 @@ public class ThreadPoolExecutorTest exte * get(cancelled task) throws CancellationException */ public void testGet_cancelled() throws Exception { + final CountDownLatch done = new CountDownLatch(1); final ExecutorService e = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue()); - try (PoolCleaner cleaner = cleaner(e)) { + try (PoolCleaner cleaner = cleaner(e, done)) { 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() @@ -2034,7 +2064,7 @@ public class ThreadPoolExecutorTest exte }}; futures.add(e.submit(r)); } - assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); + await(blockerStarted); for (Future future : futures) future.cancel(false); for (Future future : futures) { try { @@ -2048,7 +2078,6 @@ public class ThreadPoolExecutorTest exte assertTrue(future.isCancelled()); assertTrue(future.isDone()); } - done.countDown(); } }