blob: 0865ed4f66d13a37ad437b94d099d6f6ae3be162 [file] [log] [blame]
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001/*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/publicdomain/zero/1.0/
5 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 * Pat Fisher, Mike Judd.
7 */
8
9package jsr166;
10
Calin Juravle8f0d92b2013-08-01 17:26:00 +010011import static java.util.concurrent.TimeUnit.MILLISECONDS;
12import static java.util.concurrent.TimeUnit.NANOSECONDS;
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +000013import static java.util.concurrent.TimeUnit.SECONDS;
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010014
15import java.util.ArrayList;
16import java.util.List;
17import java.util.concurrent.ArrayBlockingQueue;
18import java.util.concurrent.BlockingQueue;
19import java.util.concurrent.Callable;
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +000020import java.util.concurrent.CancellationException;
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010021import java.util.concurrent.CountDownLatch;
22import java.util.concurrent.ExecutionException;
23import java.util.concurrent.Executors;
24import java.util.concurrent.ExecutorService;
25import java.util.concurrent.Future;
26import java.util.concurrent.FutureTask;
27import java.util.concurrent.LinkedBlockingQueue;
28import java.util.concurrent.RejectedExecutionException;
29import java.util.concurrent.RejectedExecutionHandler;
30import java.util.concurrent.SynchronousQueue;
31import java.util.concurrent.ThreadFactory;
32import java.util.concurrent.ThreadPoolExecutor;
33import java.util.concurrent.TimeUnit;
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +000034import java.util.concurrent.atomic.AtomicInteger;
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010035
36import junit.framework.Test;
37import junit.framework.TestSuite;
Calin Juravle8f0d92b2013-08-01 17:26:00 +010038
39public class ThreadPoolExecutorTest extends JSR166TestCase {
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010040 // android-note: Removed because the CTS runner does a bad job of
41 // retrying tests that have suite() declarations.
42 //
43 // public static void main(String[] args) {
44 // main(suite(), args);
45 // }
46 // public static Test suite() {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +000047 // return new TestSuite(ThreadPoolExecutorTest.class);
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010048 // }
Calin Juravle8f0d92b2013-08-01 17:26:00 +010049
50 static class ExtendedTPE extends ThreadPoolExecutor {
51 final CountDownLatch beforeCalled = new CountDownLatch(1);
52 final CountDownLatch afterCalled = new CountDownLatch(1);
53 final CountDownLatch terminatedCalled = new CountDownLatch(1);
54
55 public ExtendedTPE() {
56 super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
57 }
58 protected void beforeExecute(Thread t, Runnable r) {
59 beforeCalled.countDown();
60 }
61 protected void afterExecute(Runnable r, Throwable t) {
62 afterCalled.countDown();
63 }
64 protected void terminated() {
65 terminatedCalled.countDown();
66 }
67
68 public boolean beforeCalled() {
69 return beforeCalled.getCount() == 0;
70 }
71 public boolean afterCalled() {
72 return afterCalled.getCount() == 0;
73 }
74 public boolean terminatedCalled() {
75 return terminatedCalled.getCount() == 0;
76 }
77 }
78
79 static class FailingThreadFactory implements ThreadFactory {
80 int calls = 0;
81 public Thread newThread(Runnable r) {
82 if (++calls > 1) return null;
83 return new Thread(r);
84 }
85 }
86
87 /**
88 * execute successfully executes a runnable
89 */
90 public void testExecute() throws InterruptedException {
91 final ThreadPoolExecutor p =
92 new ThreadPoolExecutor(1, 1,
93 LONG_DELAY_MS, MILLISECONDS,
94 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +000095 try (PoolCleaner cleaner = cleaner(p)) {
96 final CountDownLatch done = new CountDownLatch(1);
97 final Runnable task = new CheckedRunnable() {
98 public void realRun() { done.countDown(); }};
Calin Juravle8f0d92b2013-08-01 17:26:00 +010099 p.execute(task);
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000100 assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100101 }
102 }
103
104 /**
105 * getActiveCount increases but doesn't overestimate, when a
106 * thread becomes active
107 */
108 public void testGetActiveCount() throws InterruptedException {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000109 final CountDownLatch done = new CountDownLatch(1);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100110 final ThreadPoolExecutor p =
111 new ThreadPoolExecutor(2, 2,
112 LONG_DELAY_MS, MILLISECONDS,
113 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000114 try (PoolCleaner cleaner = cleaner(p, done)) {
115 final CountDownLatch threadStarted = new CountDownLatch(1);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100116 assertEquals(0, p.getActiveCount());
117 p.execute(new CheckedRunnable() {
118 public void realRun() throws InterruptedException {
119 threadStarted.countDown();
120 assertEquals(1, p.getActiveCount());
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000121 await(done);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100122 }});
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000123 await(threadStarted);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100124 assertEquals(1, p.getActiveCount());
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100125 }
126 }
127
128 /**
129 * prestartCoreThread starts a thread if under corePoolSize, else doesn't
130 */
131 public void testPrestartCoreThread() {
132 final ThreadPoolExecutor p =
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000133 new ThreadPoolExecutor(2, 6,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100134 LONG_DELAY_MS, MILLISECONDS,
135 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000136 try (PoolCleaner cleaner = cleaner(p)) {
137 assertEquals(0, p.getPoolSize());
138 assertTrue(p.prestartCoreThread());
139 assertEquals(1, p.getPoolSize());
140 assertTrue(p.prestartCoreThread());
141 assertEquals(2, p.getPoolSize());
142 assertFalse(p.prestartCoreThread());
143 assertEquals(2, p.getPoolSize());
144 p.setCorePoolSize(4);
145 assertTrue(p.prestartCoreThread());
146 assertEquals(3, p.getPoolSize());
147 assertTrue(p.prestartCoreThread());
148 assertEquals(4, p.getPoolSize());
149 assertFalse(p.prestartCoreThread());
150 assertEquals(4, p.getPoolSize());
151 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100152 }
153
154 /**
155 * prestartAllCoreThreads starts all corePoolSize threads
156 */
157 public void testPrestartAllCoreThreads() {
158 final ThreadPoolExecutor p =
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000159 new ThreadPoolExecutor(2, 6,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100160 LONG_DELAY_MS, MILLISECONDS,
161 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000162 try (PoolCleaner cleaner = cleaner(p)) {
163 assertEquals(0, p.getPoolSize());
164 p.prestartAllCoreThreads();
165 assertEquals(2, p.getPoolSize());
166 p.prestartAllCoreThreads();
167 assertEquals(2, p.getPoolSize());
168 p.setCorePoolSize(4);
169 p.prestartAllCoreThreads();
170 assertEquals(4, p.getPoolSize());
171 p.prestartAllCoreThreads();
172 assertEquals(4, p.getPoolSize());
173 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100174 }
175
176 /**
177 * getCompletedTaskCount increases, but doesn't overestimate,
178 * when tasks complete
179 */
180 public void testGetCompletedTaskCount() throws InterruptedException {
181 final ThreadPoolExecutor p =
182 new ThreadPoolExecutor(2, 2,
183 LONG_DELAY_MS, MILLISECONDS,
184 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000185 try (PoolCleaner cleaner = cleaner(p)) {
186 final CountDownLatch threadStarted = new CountDownLatch(1);
187 final CountDownLatch threadProceed = new CountDownLatch(1);
188 final CountDownLatch threadDone = new CountDownLatch(1);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100189 assertEquals(0, p.getCompletedTaskCount());
190 p.execute(new CheckedRunnable() {
191 public void realRun() throws InterruptedException {
192 threadStarted.countDown();
193 assertEquals(0, p.getCompletedTaskCount());
194 threadProceed.await();
195 threadDone.countDown();
196 }});
197 await(threadStarted);
198 assertEquals(0, p.getCompletedTaskCount());
199 threadProceed.countDown();
200 threadDone.await();
201 long startTime = System.nanoTime();
202 while (p.getCompletedTaskCount() != 1) {
203 if (millisElapsedSince(startTime) > LONG_DELAY_MS)
204 fail("timed out");
205 Thread.yield();
206 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100207 }
208 }
209
210 /**
211 * getCorePoolSize returns size given in constructor if not otherwise set
212 */
213 public void testGetCorePoolSize() {
214 final ThreadPoolExecutor p =
215 new ThreadPoolExecutor(1, 1,
216 LONG_DELAY_MS, MILLISECONDS,
217 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000218 try (PoolCleaner cleaner = cleaner(p)) {
219 assertEquals(1, p.getCorePoolSize());
220 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100221 }
222
223 /**
224 * getKeepAliveTime returns value given in constructor if not otherwise set
225 */
226 public void testGetKeepAliveTime() {
227 final ThreadPoolExecutor p =
228 new ThreadPoolExecutor(2, 2,
229 1000, MILLISECONDS,
230 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000231 try (PoolCleaner cleaner = cleaner(p)) {
232 assertEquals(1, p.getKeepAliveTime(SECONDS));
233 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100234 }
235
236 /**
237 * getThreadFactory returns factory in constructor if not set
238 */
239 public void testGetThreadFactory() {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000240 ThreadFactory threadFactory = new SimpleThreadFactory();
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100241 final ThreadPoolExecutor p =
242 new ThreadPoolExecutor(1, 2,
243 LONG_DELAY_MS, MILLISECONDS,
244 new ArrayBlockingQueue<Runnable>(10),
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000245 threadFactory,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100246 new NoOpREHandler());
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000247 try (PoolCleaner cleaner = cleaner(p)) {
248 assertSame(threadFactory, p.getThreadFactory());
249 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100250 }
251
252 /**
253 * setThreadFactory sets the thread factory returned by getThreadFactory
254 */
255 public void testSetThreadFactory() {
256 final ThreadPoolExecutor p =
257 new ThreadPoolExecutor(1, 2,
258 LONG_DELAY_MS, MILLISECONDS,
259 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000260 try (PoolCleaner cleaner = cleaner(p)) {
261 ThreadFactory threadFactory = new SimpleThreadFactory();
262 p.setThreadFactory(threadFactory);
263 assertSame(threadFactory, p.getThreadFactory());
264 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100265 }
266
267 /**
268 * setThreadFactory(null) throws NPE
269 */
270 public void testSetThreadFactoryNull() {
271 final ThreadPoolExecutor p =
272 new ThreadPoolExecutor(1, 2,
273 LONG_DELAY_MS, MILLISECONDS,
274 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000275 try (PoolCleaner cleaner = cleaner(p)) {
276 try {
277 p.setThreadFactory(null);
278 shouldThrow();
279 } catch (NullPointerException success) {}
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100280 }
281 }
282
283 /**
284 * getRejectedExecutionHandler returns handler in constructor if not set
285 */
286 public void testGetRejectedExecutionHandler() {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000287 final RejectedExecutionHandler handler = new NoOpREHandler();
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100288 final ThreadPoolExecutor p =
289 new ThreadPoolExecutor(1, 2,
290 LONG_DELAY_MS, MILLISECONDS,
291 new ArrayBlockingQueue<Runnable>(10),
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000292 handler);
293 try (PoolCleaner cleaner = cleaner(p)) {
294 assertSame(handler, p.getRejectedExecutionHandler());
295 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100296 }
297
298 /**
299 * setRejectedExecutionHandler sets the handler returned by
300 * getRejectedExecutionHandler
301 */
302 public void testSetRejectedExecutionHandler() {
303 final ThreadPoolExecutor p =
304 new ThreadPoolExecutor(1, 2,
305 LONG_DELAY_MS, MILLISECONDS,
306 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000307 try (PoolCleaner cleaner = cleaner(p)) {
308 RejectedExecutionHandler handler = new NoOpREHandler();
309 p.setRejectedExecutionHandler(handler);
310 assertSame(handler, p.getRejectedExecutionHandler());
311 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100312 }
313
314 /**
315 * setRejectedExecutionHandler(null) throws NPE
316 */
317 public void testSetRejectedExecutionHandlerNull() {
318 final ThreadPoolExecutor p =
319 new ThreadPoolExecutor(1, 2,
320 LONG_DELAY_MS, MILLISECONDS,
321 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000322 try (PoolCleaner cleaner = cleaner(p)) {
323 try {
324 p.setRejectedExecutionHandler(null);
325 shouldThrow();
326 } catch (NullPointerException success) {}
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100327 }
328 }
329
330 /**
331 * getLargestPoolSize increases, but doesn't overestimate, when
332 * multiple threads active
333 */
334 public void testGetLargestPoolSize() throws InterruptedException {
335 final int THREADS = 3;
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000336 final CountDownLatch done = new CountDownLatch(1);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100337 final ThreadPoolExecutor p =
338 new ThreadPoolExecutor(THREADS, THREADS,
339 LONG_DELAY_MS, MILLISECONDS,
340 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000341 try (PoolCleaner cleaner = cleaner(p, done)) {
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100342 assertEquals(0, p.getLargestPoolSize());
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000343 final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100344 for (int i = 0; i < THREADS; i++)
345 p.execute(new CheckedRunnable() {
346 public void realRun() throws InterruptedException {
347 threadsStarted.countDown();
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000348 await(done);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100349 assertEquals(THREADS, p.getLargestPoolSize());
350 }});
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000351 await(threadsStarted);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100352 assertEquals(THREADS, p.getLargestPoolSize());
353 }
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000354 assertEquals(THREADS, p.getLargestPoolSize());
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100355 }
356
357 /**
358 * getMaximumPoolSize returns value given in constructor if not
359 * otherwise set
360 */
361 public void testGetMaximumPoolSize() {
362 final ThreadPoolExecutor p =
363 new ThreadPoolExecutor(2, 3,
364 LONG_DELAY_MS, MILLISECONDS,
365 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000366 try (PoolCleaner cleaner = cleaner(p)) {
367 assertEquals(3, p.getMaximumPoolSize());
368 p.setMaximumPoolSize(5);
369 assertEquals(5, p.getMaximumPoolSize());
370 p.setMaximumPoolSize(4);
371 assertEquals(4, p.getMaximumPoolSize());
372 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100373 }
374
375 /**
376 * getPoolSize increases, but doesn't overestimate, when threads
377 * become active
378 */
379 public void testGetPoolSize() throws InterruptedException {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000380 final CountDownLatch done = new CountDownLatch(1);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100381 final ThreadPoolExecutor p =
382 new ThreadPoolExecutor(1, 1,
383 LONG_DELAY_MS, MILLISECONDS,
384 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000385 try (PoolCleaner cleaner = cleaner(p, done)) {
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100386 assertEquals(0, p.getPoolSize());
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000387 final CountDownLatch threadStarted = new CountDownLatch(1);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100388 p.execute(new CheckedRunnable() {
389 public void realRun() throws InterruptedException {
390 threadStarted.countDown();
391 assertEquals(1, p.getPoolSize());
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000392 await(done);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100393 }});
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000394 await(threadStarted);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100395 assertEquals(1, p.getPoolSize());
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100396 }
397 }
398
399 /**
400 * getTaskCount increases, but doesn't overestimate, when tasks submitted
401 */
402 public void testGetTaskCount() throws InterruptedException {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000403 final int TASKS = 3;
404 final CountDownLatch done = new CountDownLatch(1);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100405 final ThreadPoolExecutor p =
406 new ThreadPoolExecutor(1, 1,
407 LONG_DELAY_MS, MILLISECONDS,
408 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000409 try (PoolCleaner cleaner = cleaner(p, done)) {
410 final CountDownLatch threadStarted = new CountDownLatch(1);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100411 assertEquals(0, p.getTaskCount());
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000412 assertEquals(0, p.getCompletedTaskCount());
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100413 p.execute(new CheckedRunnable() {
414 public void realRun() throws InterruptedException {
415 threadStarted.countDown();
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000416 await(done);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100417 }});
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000418 await(threadStarted);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100419 assertEquals(1, p.getTaskCount());
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000420 assertEquals(0, p.getCompletedTaskCount());
421 for (int i = 0; i < TASKS; i++) {
422 assertEquals(1 + i, p.getTaskCount());
423 p.execute(new CheckedRunnable() {
424 public void realRun() throws InterruptedException {
425 threadStarted.countDown();
426 assertEquals(1 + TASKS, p.getTaskCount());
427 await(done);
428 }});
429 }
430 assertEquals(1 + TASKS, p.getTaskCount());
431 assertEquals(0, p.getCompletedTaskCount());
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100432 }
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000433 assertEquals(1 + TASKS, p.getTaskCount());
434 assertEquals(1 + TASKS, p.getCompletedTaskCount());
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100435 }
436
437 /**
438 * isShutdown is false before shutdown, true after
439 */
440 public void testIsShutdown() {
441 final ThreadPoolExecutor p =
442 new ThreadPoolExecutor(1, 1,
443 LONG_DELAY_MS, MILLISECONDS,
444 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000445 try (PoolCleaner cleaner = cleaner(p)) {
446 assertFalse(p.isShutdown());
447 try { p.shutdown(); } catch (SecurityException ok) { return; }
448 assertTrue(p.isShutdown());
449 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100450 }
451
452 /**
453 * awaitTermination on a non-shutdown pool times out
454 */
455 public void testAwaitTermination_timesOut() throws InterruptedException {
456 final ThreadPoolExecutor p =
457 new ThreadPoolExecutor(1, 1,
458 LONG_DELAY_MS, MILLISECONDS,
459 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000460 try (PoolCleaner cleaner = cleaner(p)) {
461 assertFalse(p.isTerminated());
462 assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
463 assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
464 assertFalse(p.awaitTermination(-1L, NANOSECONDS));
465 assertFalse(p.awaitTermination(-1L, MILLISECONDS));
466 assertFalse(p.awaitTermination(0L, NANOSECONDS));
467 assertFalse(p.awaitTermination(0L, MILLISECONDS));
468 long timeoutNanos = 999999L;
469 long startTime = System.nanoTime();
470 assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
471 assertTrue(System.nanoTime() - startTime >= timeoutNanos);
472 assertFalse(p.isTerminated());
473 startTime = System.nanoTime();
474 long timeoutMillis = timeoutMillis();
475 assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
476 assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
477 assertFalse(p.isTerminated());
478 try { p.shutdown(); } catch (SecurityException ok) { return; }
479 assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
480 assertTrue(p.isTerminated());
481 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100482 }
483
484 /**
485 * isTerminated is false before termination, true after
486 */
487 public void testIsTerminated() throws InterruptedException {
488 final ThreadPoolExecutor p =
489 new ThreadPoolExecutor(1, 1,
490 LONG_DELAY_MS, MILLISECONDS,
491 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000492 try (PoolCleaner cleaner = cleaner(p)) {
493 final CountDownLatch threadStarted = new CountDownLatch(1);
494 final CountDownLatch done = new CountDownLatch(1);
495 assertFalse(p.isTerminating());
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100496 p.execute(new CheckedRunnable() {
497 public void realRun() throws InterruptedException {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000498 assertFalse(p.isTerminating());
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100499 threadStarted.countDown();
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000500 await(done);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100501 }});
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000502 await(threadStarted);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100503 assertFalse(p.isTerminating());
504 done.countDown();
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100505 try { p.shutdown(); } catch (SecurityException ok) { return; }
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000506 assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
507 assertTrue(p.isTerminated());
508 assertFalse(p.isTerminating());
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100509 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100510 }
511
512 /**
513 * isTerminating is not true when running or when terminated
514 */
515 public void testIsTerminating() throws InterruptedException {
516 final ThreadPoolExecutor p =
517 new ThreadPoolExecutor(1, 1,
518 LONG_DELAY_MS, MILLISECONDS,
519 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000520 try (PoolCleaner cleaner = cleaner(p)) {
521 final CountDownLatch threadStarted = new CountDownLatch(1);
522 final CountDownLatch done = new CountDownLatch(1);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100523 assertFalse(p.isTerminating());
524 p.execute(new CheckedRunnable() {
525 public void realRun() throws InterruptedException {
526 assertFalse(p.isTerminating());
527 threadStarted.countDown();
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000528 await(done);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100529 }});
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000530 await(threadStarted);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100531 assertFalse(p.isTerminating());
532 done.countDown();
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100533 try { p.shutdown(); } catch (SecurityException ok) { return; }
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000534 assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
535 assertTrue(p.isTerminated());
536 assertFalse(p.isTerminating());
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100537 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100538 }
539
540 /**
541 * getQueue returns the work queue, which contains queued tasks
542 */
543 public void testGetQueue() throws InterruptedException {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000544 final CountDownLatch done = new CountDownLatch(1);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100545 final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
546 final ThreadPoolExecutor p =
547 new ThreadPoolExecutor(1, 1,
548 LONG_DELAY_MS, MILLISECONDS,
549 q);
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000550 try (PoolCleaner cleaner = cleaner(p, done)) {
551 final CountDownLatch threadStarted = new CountDownLatch(1);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100552 FutureTask[] tasks = new FutureTask[5];
553 for (int i = 0; i < tasks.length; i++) {
554 Callable task = new CheckedCallable<Boolean>() {
555 public Boolean realCall() throws InterruptedException {
556 threadStarted.countDown();
557 assertSame(q, p.getQueue());
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000558 await(done);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100559 return Boolean.TRUE;
560 }};
561 tasks[i] = new FutureTask(task);
562 p.execute(tasks[i]);
563 }
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000564 await(threadStarted);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100565 assertSame(q, p.getQueue());
566 assertFalse(q.contains(tasks[0]));
567 assertTrue(q.contains(tasks[tasks.length - 1]));
568 assertEquals(tasks.length - 1, q.size());
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100569 }
570 }
571
572 /**
573 * remove(task) removes queued task, and fails to remove active task
574 */
575 public void testRemove() throws InterruptedException {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000576 final CountDownLatch done = new CountDownLatch(1);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100577 BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
578 final ThreadPoolExecutor p =
579 new ThreadPoolExecutor(1, 1,
580 LONG_DELAY_MS, MILLISECONDS,
581 q);
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000582 try (PoolCleaner cleaner = cleaner(p, done)) {
583 Runnable[] tasks = new Runnable[6];
584 final CountDownLatch threadStarted = new CountDownLatch(1);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100585 for (int i = 0; i < tasks.length; i++) {
586 tasks[i] = new CheckedRunnable() {
587 public void realRun() throws InterruptedException {
588 threadStarted.countDown();
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000589 await(done);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100590 }};
591 p.execute(tasks[i]);
592 }
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000593 await(threadStarted);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100594 assertFalse(p.remove(tasks[0]));
595 assertTrue(q.contains(tasks[4]));
596 assertTrue(q.contains(tasks[3]));
597 assertTrue(p.remove(tasks[4]));
598 assertFalse(p.remove(tasks[4]));
599 assertFalse(q.contains(tasks[4]));
600 assertTrue(q.contains(tasks[3]));
601 assertTrue(p.remove(tasks[3]));
602 assertFalse(q.contains(tasks[3]));
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100603 }
604 }
605
606 /**
607 * purge removes cancelled tasks from the queue
608 */
609 public void testPurge() throws InterruptedException {
610 final CountDownLatch threadStarted = new CountDownLatch(1);
611 final CountDownLatch done = new CountDownLatch(1);
612 final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
613 final ThreadPoolExecutor p =
614 new ThreadPoolExecutor(1, 1,
615 LONG_DELAY_MS, MILLISECONDS,
616 q);
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000617 try (PoolCleaner cleaner = cleaner(p, done)) {
618 FutureTask[] tasks = new FutureTask[5];
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100619 for (int i = 0; i < tasks.length; i++) {
620 Callable task = new CheckedCallable<Boolean>() {
621 public Boolean realCall() throws InterruptedException {
622 threadStarted.countDown();
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000623 await(done);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100624 return Boolean.TRUE;
625 }};
626 tasks[i] = new FutureTask(task);
627 p.execute(tasks[i]);
628 }
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000629 await(threadStarted);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100630 assertEquals(tasks.length, p.getTaskCount());
631 assertEquals(tasks.length - 1, q.size());
632 assertEquals(1L, p.getActiveCount());
633 assertEquals(0L, p.getCompletedTaskCount());
634 tasks[4].cancel(true);
635 tasks[3].cancel(false);
636 p.purge();
637 assertEquals(tasks.length - 3, q.size());
638 assertEquals(tasks.length - 2, p.getTaskCount());
639 p.purge(); // Nothing to do
640 assertEquals(tasks.length - 3, q.size());
641 assertEquals(tasks.length - 2, p.getTaskCount());
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100642 }
643 }
644
645 /**
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000646 * shutdownNow returns a list containing tasks that were not run,
647 * and those tasks are drained from the queue
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100648 */
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000649 public void testShutdownNow() throws InterruptedException {
650 final int poolSize = 2;
651 final int count = 5;
652 final AtomicInteger ran = new AtomicInteger(0);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100653 final ThreadPoolExecutor p =
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000654 new ThreadPoolExecutor(poolSize, poolSize,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100655 LONG_DELAY_MS, MILLISECONDS,
656 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000657 final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
658 Runnable waiter = new CheckedRunnable() { public void realRun() {
659 threadsStarted.countDown();
Przemyslaw Szczepaniaked4f3652016-03-15 09:42:19 +0000660 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000661 MILLISECONDS.sleep(2 * LONG_DELAY_MS);
662 } catch (InterruptedException success) {}
663 ran.getAndIncrement();
664 }};
665 for (int i = 0; i < count; i++)
666 p.execute(waiter);
667 await(threadsStarted);
668 assertEquals(poolSize, p.getActiveCount());
669 assertEquals(0, p.getCompletedTaskCount());
670 final List<Runnable> queuedTasks;
671 try {
672 queuedTasks = p.shutdownNow();
673 } catch (SecurityException ok) {
674 return; // Allowed in case test doesn't have privs
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100675 }
676 assertTrue(p.isShutdown());
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000677 assertTrue(p.getQueue().isEmpty());
678 assertEquals(count - poolSize, queuedTasks.size());
679 assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
680 assertTrue(p.isTerminated());
681 assertEquals(poolSize, ran.get());
682 assertEquals(poolSize, p.getCompletedTaskCount());
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100683 }
684
685 // Exception Tests
686
687 /**
688 * Constructor throws if corePoolSize argument is less than zero
689 */
690 public void testConstructor1() {
691 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000692 new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100693 new ArrayBlockingQueue<Runnable>(10));
694 shouldThrow();
695 } catch (IllegalArgumentException success) {}
696 }
697
698 /**
699 * Constructor throws if maximumPoolSize is less than zero
700 */
701 public void testConstructor2() {
702 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000703 new ThreadPoolExecutor(1, -1, 1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100704 new ArrayBlockingQueue<Runnable>(10));
705 shouldThrow();
706 } catch (IllegalArgumentException success) {}
707 }
708
709 /**
710 * Constructor throws if maximumPoolSize is equal to zero
711 */
712 public void testConstructor3() {
713 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000714 new ThreadPoolExecutor(1, 0, 1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100715 new ArrayBlockingQueue<Runnable>(10));
716 shouldThrow();
717 } catch (IllegalArgumentException success) {}
718 }
719
720 /**
721 * Constructor throws if keepAliveTime is less than zero
722 */
723 public void testConstructor4() {
724 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000725 new ThreadPoolExecutor(1, 2, -1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100726 new ArrayBlockingQueue<Runnable>(10));
727 shouldThrow();
728 } catch (IllegalArgumentException success) {}
729 }
730
731 /**
732 * Constructor throws if corePoolSize is greater than the maximumPoolSize
733 */
734 public void testConstructor5() {
735 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000736 new ThreadPoolExecutor(2, 1, 1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100737 new ArrayBlockingQueue<Runnable>(10));
738 shouldThrow();
739 } catch (IllegalArgumentException success) {}
740 }
741
742 /**
743 * Constructor throws if workQueue is set to null
744 */
745 public void testConstructorNullPointerException() {
746 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000747 new ThreadPoolExecutor(1, 2, 1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100748 (BlockingQueue) null);
749 shouldThrow();
750 } catch (NullPointerException success) {}
751 }
752
753 /**
754 * Constructor throws if corePoolSize argument is less than zero
755 */
756 public void testConstructor6() {
757 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000758 new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100759 new ArrayBlockingQueue<Runnable>(10),
760 new SimpleThreadFactory());
761 shouldThrow();
762 } catch (IllegalArgumentException success) {}
763 }
764
765 /**
766 * Constructor throws if maximumPoolSize is less than zero
767 */
768 public void testConstructor7() {
769 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000770 new ThreadPoolExecutor(1, -1, 1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100771 new ArrayBlockingQueue<Runnable>(10),
772 new SimpleThreadFactory());
773 shouldThrow();
774 } catch (IllegalArgumentException success) {}
775 }
776
777 /**
778 * Constructor throws if maximumPoolSize is equal to zero
779 */
780 public void testConstructor8() {
781 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000782 new ThreadPoolExecutor(1, 0, 1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100783 new ArrayBlockingQueue<Runnable>(10),
784 new SimpleThreadFactory());
785 shouldThrow();
786 } catch (IllegalArgumentException success) {}
787 }
788
789 /**
790 * Constructor throws if keepAliveTime is less than zero
791 */
792 public void testConstructor9() {
793 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000794 new ThreadPoolExecutor(1, 2, -1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100795 new ArrayBlockingQueue<Runnable>(10),
796 new SimpleThreadFactory());
797 shouldThrow();
798 } catch (IllegalArgumentException success) {}
799 }
800
801 /**
802 * Constructor throws if corePoolSize is greater than the maximumPoolSize
803 */
804 public void testConstructor10() {
805 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000806 new ThreadPoolExecutor(2, 1, 1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100807 new ArrayBlockingQueue<Runnable>(10),
808 new SimpleThreadFactory());
809 shouldThrow();
810 } catch (IllegalArgumentException success) {}
811 }
812
813 /**
814 * Constructor throws if workQueue is set to null
815 */
816 public void testConstructorNullPointerException2() {
817 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000818 new ThreadPoolExecutor(1, 2, 1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100819 (BlockingQueue) null,
820 new SimpleThreadFactory());
821 shouldThrow();
822 } catch (NullPointerException success) {}
823 }
824
825 /**
826 * Constructor throws if threadFactory is set to null
827 */
828 public void testConstructorNullPointerException3() {
829 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000830 new ThreadPoolExecutor(1, 2, 1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100831 new ArrayBlockingQueue<Runnable>(10),
832 (ThreadFactory) null);
833 shouldThrow();
834 } catch (NullPointerException success) {}
835 }
836
837 /**
838 * Constructor throws if corePoolSize argument is less than zero
839 */
840 public void testConstructor11() {
841 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000842 new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100843 new ArrayBlockingQueue<Runnable>(10),
844 new NoOpREHandler());
845 shouldThrow();
846 } catch (IllegalArgumentException success) {}
847 }
848
849 /**
850 * Constructor throws if maximumPoolSize is less than zero
851 */
852 public void testConstructor12() {
853 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000854 new ThreadPoolExecutor(1, -1, 1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100855 new ArrayBlockingQueue<Runnable>(10),
856 new NoOpREHandler());
857 shouldThrow();
858 } catch (IllegalArgumentException success) {}
859 }
860
861 /**
862 * Constructor throws if maximumPoolSize is equal to zero
863 */
864 public void testConstructor13() {
865 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000866 new ThreadPoolExecutor(1, 0, 1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100867 new ArrayBlockingQueue<Runnable>(10),
868 new NoOpREHandler());
869 shouldThrow();
870 } catch (IllegalArgumentException success) {}
871 }
872
873 /**
874 * Constructor throws if keepAliveTime is less than zero
875 */
876 public void testConstructor14() {
877 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000878 new ThreadPoolExecutor(1, 2, -1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100879 new ArrayBlockingQueue<Runnable>(10),
880 new NoOpREHandler());
881 shouldThrow();
882 } catch (IllegalArgumentException success) {}
883 }
884
885 /**
886 * Constructor throws if corePoolSize is greater than the maximumPoolSize
887 */
888 public void testConstructor15() {
889 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000890 new ThreadPoolExecutor(2, 1, 1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100891 new ArrayBlockingQueue<Runnable>(10),
892 new NoOpREHandler());
893 shouldThrow();
894 } catch (IllegalArgumentException success) {}
895 }
896
897 /**
898 * Constructor throws if workQueue is set to null
899 */
900 public void testConstructorNullPointerException4() {
901 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000902 new ThreadPoolExecutor(1, 2, 1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100903 (BlockingQueue) null,
904 new NoOpREHandler());
905 shouldThrow();
906 } catch (NullPointerException success) {}
907 }
908
909 /**
910 * Constructor throws if handler is set to null
911 */
912 public void testConstructorNullPointerException5() {
913 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000914 new ThreadPoolExecutor(1, 2, 1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100915 new ArrayBlockingQueue<Runnable>(10),
916 (RejectedExecutionHandler) null);
917 shouldThrow();
918 } catch (NullPointerException success) {}
919 }
920
921 /**
922 * Constructor throws if corePoolSize argument is less than zero
923 */
924 public void testConstructor16() {
925 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000926 new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100927 new ArrayBlockingQueue<Runnable>(10),
928 new SimpleThreadFactory(),
929 new NoOpREHandler());
930 shouldThrow();
931 } catch (IllegalArgumentException success) {}
932 }
933
934 /**
935 * Constructor throws if maximumPoolSize is less than zero
936 */
937 public void testConstructor17() {
938 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000939 new ThreadPoolExecutor(1, -1, 1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100940 new ArrayBlockingQueue<Runnable>(10),
941 new SimpleThreadFactory(),
942 new NoOpREHandler());
943 shouldThrow();
944 } catch (IllegalArgumentException success) {}
945 }
946
947 /**
948 * Constructor throws if maximumPoolSize is equal to zero
949 */
950 public void testConstructor18() {
951 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000952 new ThreadPoolExecutor(1, 0, 1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100953 new ArrayBlockingQueue<Runnable>(10),
954 new SimpleThreadFactory(),
955 new NoOpREHandler());
956 shouldThrow();
957 } catch (IllegalArgumentException success) {}
958 }
959
960 /**
961 * Constructor throws if keepAliveTime is less than zero
962 */
963 public void testConstructor19() {
964 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000965 new ThreadPoolExecutor(1, 2, -1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100966 new ArrayBlockingQueue<Runnable>(10),
967 new SimpleThreadFactory(),
968 new NoOpREHandler());
969 shouldThrow();
970 } catch (IllegalArgumentException success) {}
971 }
972
973 /**
974 * Constructor throws if corePoolSize is greater than the maximumPoolSize
975 */
976 public void testConstructor20() {
977 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000978 new ThreadPoolExecutor(2, 1, 1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100979 new ArrayBlockingQueue<Runnable>(10),
980 new SimpleThreadFactory(),
981 new NoOpREHandler());
982 shouldThrow();
983 } catch (IllegalArgumentException success) {}
984 }
985
986 /**
987 * Constructor throws if workQueue is null
988 */
989 public void testConstructorNullPointerException6() {
990 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +0000991 new ThreadPoolExecutor(1, 2, 1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100992 (BlockingQueue) null,
993 new SimpleThreadFactory(),
994 new NoOpREHandler());
995 shouldThrow();
996 } catch (NullPointerException success) {}
997 }
998
999 /**
1000 * Constructor throws if handler is null
1001 */
1002 public void testConstructorNullPointerException7() {
1003 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001004 new ThreadPoolExecutor(1, 2, 1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001005 new ArrayBlockingQueue<Runnable>(10),
1006 new SimpleThreadFactory(),
1007 (RejectedExecutionHandler) null);
1008 shouldThrow();
1009 } catch (NullPointerException success) {}
1010 }
1011
1012 /**
1013 * Constructor throws if ThreadFactory is null
1014 */
1015 public void testConstructorNullPointerException8() {
1016 try {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001017 new ThreadPoolExecutor(1, 2, 1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001018 new ArrayBlockingQueue<Runnable>(10),
1019 (ThreadFactory) null,
1020 new NoOpREHandler());
1021 shouldThrow();
1022 } catch (NullPointerException success) {}
1023 }
1024
1025 /**
1026 * get of submitted callable throws InterruptedException if interrupted
1027 */
1028 public void testInterruptedSubmit() throws InterruptedException {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001029 final CountDownLatch done = new CountDownLatch(1);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001030 final ThreadPoolExecutor p =
1031 new ThreadPoolExecutor(1, 1,
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001032 60, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001033 new ArrayBlockingQueue<Runnable>(10));
1034
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001035 try (PoolCleaner cleaner = cleaner(p, done)) {
1036 final CountDownLatch threadStarted = new CountDownLatch(1);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001037 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1038 public void realRun() throws Exception {
1039 Callable task = new CheckedCallable<Boolean>() {
1040 public Boolean realCall() throws InterruptedException {
1041 threadStarted.countDown();
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001042 await(done);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001043 return Boolean.TRUE;
1044 }};
1045 p.submit(task).get();
1046 }});
1047
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001048 await(threadStarted);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001049 t.interrupt();
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001050 awaitTermination(t);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001051 }
1052 }
1053
1054 /**
1055 * execute throws RejectedExecutionException if saturated.
1056 */
1057 public void testSaturatedExecute() {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001058 final CountDownLatch done = new CountDownLatch(1);
1059 final ThreadPoolExecutor p =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001060 new ThreadPoolExecutor(1, 1,
1061 LONG_DELAY_MS, MILLISECONDS,
1062 new ArrayBlockingQueue<Runnable>(1));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001063 try (PoolCleaner cleaner = cleaner(p, done)) {
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001064 Runnable task = new CheckedRunnable() {
1065 public void realRun() throws InterruptedException {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001066 await(done);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001067 }};
1068 for (int i = 0; i < 2; ++i)
1069 p.execute(task);
1070 for (int i = 0; i < 2; ++i) {
1071 try {
1072 p.execute(task);
1073 shouldThrow();
1074 } catch (RejectedExecutionException success) {}
1075 assertTrue(p.getTaskCount() <= 2);
1076 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001077 }
1078 }
1079
1080 /**
1081 * submit(runnable) throws RejectedExecutionException if saturated.
1082 */
1083 public void testSaturatedSubmitRunnable() {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001084 final CountDownLatch done = new CountDownLatch(1);
1085 final ThreadPoolExecutor p =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001086 new ThreadPoolExecutor(1, 1,
1087 LONG_DELAY_MS, MILLISECONDS,
1088 new ArrayBlockingQueue<Runnable>(1));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001089 try (PoolCleaner cleaner = cleaner(p, done)) {
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001090 Runnable task = new CheckedRunnable() {
1091 public void realRun() throws InterruptedException {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001092 await(done);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001093 }};
1094 for (int i = 0; i < 2; ++i)
1095 p.submit(task);
1096 for (int i = 0; i < 2; ++i) {
1097 try {
1098 p.execute(task);
1099 shouldThrow();
1100 } catch (RejectedExecutionException success) {}
1101 assertTrue(p.getTaskCount() <= 2);
1102 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001103 }
1104 }
1105
1106 /**
1107 * submit(callable) throws RejectedExecutionException if saturated.
1108 */
1109 public void testSaturatedSubmitCallable() {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001110 final CountDownLatch done = new CountDownLatch(1);
1111 final ThreadPoolExecutor p =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001112 new ThreadPoolExecutor(1, 1,
1113 LONG_DELAY_MS, MILLISECONDS,
1114 new ArrayBlockingQueue<Runnable>(1));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001115 try (PoolCleaner cleaner = cleaner(p, done)) {
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001116 Runnable task = new CheckedRunnable() {
1117 public void realRun() throws InterruptedException {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001118 await(done);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001119 }};
1120 for (int i = 0; i < 2; ++i)
1121 p.submit(Executors.callable(task));
1122 for (int i = 0; i < 2; ++i) {
1123 try {
1124 p.execute(task);
1125 shouldThrow();
1126 } catch (RejectedExecutionException success) {}
1127 assertTrue(p.getTaskCount() <= 2);
1128 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001129 }
1130 }
1131
1132 /**
1133 * executor using CallerRunsPolicy runs task if saturated.
1134 */
1135 public void testSaturatedExecute2() {
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001136 final ThreadPoolExecutor p =
1137 new ThreadPoolExecutor(1, 1,
1138 LONG_DELAY_MS,
1139 MILLISECONDS,
1140 new ArrayBlockingQueue<Runnable>(1),
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001141 new ThreadPoolExecutor.CallerRunsPolicy());
1142 try (PoolCleaner cleaner = cleaner(p)) {
1143 final CountDownLatch done = new CountDownLatch(1);
1144 Runnable blocker = new CheckedRunnable() {
1145 public void realRun() throws InterruptedException {
1146 await(done);
1147 }};
1148 p.execute(blocker);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001149 TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001150 for (int i = 0; i < tasks.length; i++)
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001151 tasks[i] = new TrackedNoOpRunnable();
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001152 for (int i = 0; i < tasks.length; i++)
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001153 p.execute(tasks[i]);
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001154 for (int i = 1; i < tasks.length; i++)
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001155 assertTrue(tasks[i].done);
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001156 assertFalse(tasks[0].done); // waiting in queue
1157 done.countDown();
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001158 }
1159 }
1160
1161 /**
1162 * executor using DiscardPolicy drops task if saturated.
1163 */
1164 public void testSaturatedExecute3() {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001165 final CountDownLatch done = new CountDownLatch(1);
1166 final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1167 for (int i = 0; i < tasks.length; ++i)
1168 tasks[i] = new TrackedNoOpRunnable();
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001169 final ThreadPoolExecutor p =
1170 new ThreadPoolExecutor(1, 1,
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001171 LONG_DELAY_MS, MILLISECONDS,
1172 new ArrayBlockingQueue<Runnable>(1),
1173 new ThreadPoolExecutor.DiscardPolicy());
1174 try (PoolCleaner cleaner = cleaner(p, done)) {
1175 p.execute(awaiter(done));
1176
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001177 for (TrackedNoOpRunnable task : tasks)
1178 p.execute(task);
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001179 for (int i = 1; i < tasks.length; i++)
1180 assertFalse(tasks[i].done);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001181 }
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001182 for (int i = 1; i < tasks.length; i++)
1183 assertFalse(tasks[i].done);
1184 assertTrue(tasks[0].done); // was waiting in queue
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001185 }
1186
1187 /**
1188 * executor using DiscardOldestPolicy drops oldest task if saturated.
1189 */
1190 public void testSaturatedExecute4() {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001191 final CountDownLatch done = new CountDownLatch(1);
1192 LatchAwaiter r1 = awaiter(done);
1193 LatchAwaiter r2 = awaiter(done);
1194 LatchAwaiter r3 = awaiter(done);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001195 final ThreadPoolExecutor p =
1196 new ThreadPoolExecutor(1, 1,
1197 LONG_DELAY_MS, MILLISECONDS,
1198 new ArrayBlockingQueue<Runnable>(1),
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001199 new ThreadPoolExecutor.DiscardOldestPolicy());
1200 try (PoolCleaner cleaner = cleaner(p, done)) {
1201 assertEquals(LatchAwaiter.NEW, r1.state);
1202 assertEquals(LatchAwaiter.NEW, r2.state);
1203 assertEquals(LatchAwaiter.NEW, r3.state);
1204 p.execute(r1);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001205 p.execute(r2);
1206 assertTrue(p.getQueue().contains(r2));
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001207 p.execute(r3);
1208 assertFalse(p.getQueue().contains(r2));
1209 assertTrue(p.getQueue().contains(r3));
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001210 }
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001211 assertEquals(LatchAwaiter.DONE, r1.state);
1212 assertEquals(LatchAwaiter.NEW, r2.state);
1213 assertEquals(LatchAwaiter.DONE, r3.state);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001214 }
1215
1216 /**
1217 * execute throws RejectedExecutionException if shutdown
1218 */
1219 public void testRejectedExecutionExceptionOnShutdown() {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001220 final ThreadPoolExecutor p =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001221 new ThreadPoolExecutor(1, 1,
1222 LONG_DELAY_MS, MILLISECONDS,
1223 new ArrayBlockingQueue<Runnable>(1));
1224 try { p.shutdown(); } catch (SecurityException ok) { return; }
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001225 try (PoolCleaner cleaner = cleaner(p)) {
1226 try {
1227 p.execute(new NoOpRunnable());
1228 shouldThrow();
1229 } catch (RejectedExecutionException success) {}
1230 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001231 }
1232
1233 /**
1234 * execute using CallerRunsPolicy drops task on shutdown
1235 */
1236 public void testCallerRunsOnShutdown() {
1237 RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
1238 final ThreadPoolExecutor p =
1239 new ThreadPoolExecutor(1, 1,
1240 LONG_DELAY_MS, MILLISECONDS,
1241 new ArrayBlockingQueue<Runnable>(1), h);
1242
1243 try { p.shutdown(); } catch (SecurityException ok) { return; }
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001244 try (PoolCleaner cleaner = cleaner(p)) {
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001245 TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1246 p.execute(r);
1247 assertFalse(r.done);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001248 }
1249 }
1250
1251 /**
1252 * execute using DiscardPolicy drops task on shutdown
1253 */
1254 public void testDiscardOnShutdown() {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001255 final ThreadPoolExecutor p =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001256 new ThreadPoolExecutor(1, 1,
1257 LONG_DELAY_MS, MILLISECONDS,
1258 new ArrayBlockingQueue<Runnable>(1),
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001259 new ThreadPoolExecutor.DiscardPolicy());
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001260
1261 try { p.shutdown(); } catch (SecurityException ok) { return; }
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001262 try (PoolCleaner cleaner = cleaner(p)) {
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001263 TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1264 p.execute(r);
1265 assertFalse(r.done);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001266 }
1267 }
1268
1269 /**
1270 * execute using DiscardOldestPolicy drops task on shutdown
1271 */
1272 public void testDiscardOldestOnShutdown() {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001273 final ThreadPoolExecutor p =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001274 new ThreadPoolExecutor(1, 1,
1275 LONG_DELAY_MS, MILLISECONDS,
1276 new ArrayBlockingQueue<Runnable>(1),
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001277 new ThreadPoolExecutor.DiscardOldestPolicy());
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001278
1279 try { p.shutdown(); } catch (SecurityException ok) { return; }
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001280 try (PoolCleaner cleaner = cleaner(p)) {
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001281 TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1282 p.execute(r);
1283 assertFalse(r.done);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001284 }
1285 }
1286
1287 /**
1288 * execute(null) throws NPE
1289 */
1290 public void testExecuteNull() {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001291 final ThreadPoolExecutor p =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001292 new ThreadPoolExecutor(1, 2,
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001293 1L, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001294 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001295 try (PoolCleaner cleaner = cleaner(p)) {
1296 try {
1297 p.execute(null);
1298 shouldThrow();
1299 } catch (NullPointerException success) {}
1300 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001301 }
1302
1303 /**
1304 * setCorePoolSize of negative value throws IllegalArgumentException
1305 */
1306 public void testCorePoolSizeIllegalArgumentException() {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001307 final ThreadPoolExecutor p =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001308 new ThreadPoolExecutor(1, 2,
1309 LONG_DELAY_MS, MILLISECONDS,
1310 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001311 try (PoolCleaner cleaner = cleaner(p)) {
1312 try {
1313 p.setCorePoolSize(-1);
1314 shouldThrow();
1315 } catch (IllegalArgumentException success) {}
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001316 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001317 }
1318
1319 /**
1320 * setMaximumPoolSize(int) throws IllegalArgumentException if
1321 * given a value less the core pool size
1322 */
1323 public void testMaximumPoolSizeIllegalArgumentException() {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001324 final ThreadPoolExecutor p =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001325 new ThreadPoolExecutor(2, 3,
1326 LONG_DELAY_MS, MILLISECONDS,
1327 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001328 try (PoolCleaner cleaner = cleaner(p)) {
1329 try {
1330 p.setMaximumPoolSize(1);
1331 shouldThrow();
1332 } catch (IllegalArgumentException success) {}
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001333 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001334 }
1335
1336 /**
1337 * setMaximumPoolSize throws IllegalArgumentException
1338 * if given a negative value
1339 */
1340 public void testMaximumPoolSizeIllegalArgumentException2() {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001341 final ThreadPoolExecutor p =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001342 new ThreadPoolExecutor(2, 3,
1343 LONG_DELAY_MS, MILLISECONDS,
1344 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001345 try (PoolCleaner cleaner = cleaner(p)) {
1346 try {
1347 p.setMaximumPoolSize(-1);
1348 shouldThrow();
1349 } catch (IllegalArgumentException success) {}
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001350 }
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001351 }
1352
1353 /**
1354 * Configuration changes that allow core pool size greater than
1355 * max pool size result in IllegalArgumentException.
1356 */
1357 public void testPoolSizeInvariants() {
1358 final ThreadPoolExecutor p =
1359 new ThreadPoolExecutor(1, 1,
1360 LONG_DELAY_MS, MILLISECONDS,
1361 new ArrayBlockingQueue<Runnable>(10));
1362 try (PoolCleaner cleaner = cleaner(p)) {
1363 for (int s = 1; s < 5; s++) {
1364 p.setMaximumPoolSize(s);
1365 p.setCorePoolSize(s);
1366 try {
1367 p.setMaximumPoolSize(s - 1);
1368 shouldThrow();
1369 } catch (IllegalArgumentException success) {}
1370 assertEquals(s, p.getCorePoolSize());
1371 assertEquals(s, p.getMaximumPoolSize());
1372 try {
1373 p.setCorePoolSize(s + 1);
Tobias Thierer5d175dd2017-03-02 19:28:56 +00001374 // Android-changed: changeset dfec9b5386ca028cc1468f3e2717120ab6274702
Przemyslaw Szczepaniakd943f382016-03-17 14:05:28 +00001375 // disables this check for compatibility reason.
1376 // shouldThrow();
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001377 } catch (IllegalArgumentException success) {}
Tobias Thierer5d175dd2017-03-02 19:28:56 +00001378 // Android-changed: changeset dfec9b5386ca028cc1468f3e2717120ab6274702
Przemyslaw Szczepaniakd943f382016-03-17 14:05:28 +00001379 // disables maximumpoolsize check for compatibility reason.
1380 // assertEquals(s, p.getCorePoolSize());
1381 assertEquals(s + 1, p.getCorePoolSize());
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001382 assertEquals(s, p.getMaximumPoolSize());
1383 }
1384 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001385 }
1386
1387 /**
1388 * setKeepAliveTime throws IllegalArgumentException
1389 * when given a negative value
1390 */
1391 public void testKeepAliveTimeIllegalArgumentException() {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001392 final ThreadPoolExecutor p =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001393 new ThreadPoolExecutor(2, 3,
1394 LONG_DELAY_MS, MILLISECONDS,
1395 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001396 try (PoolCleaner cleaner = cleaner(p)) {
1397 try {
1398 p.setKeepAliveTime(-1, MILLISECONDS);
1399 shouldThrow();
1400 } catch (IllegalArgumentException success) {}
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001401 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001402 }
1403
1404 /**
1405 * terminated() is called on termination
1406 */
1407 public void testTerminated() {
1408 ExtendedTPE p = new ExtendedTPE();
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001409 try (PoolCleaner cleaner = cleaner(p)) {
1410 try { p.shutdown(); } catch (SecurityException ok) { return; }
1411 assertTrue(p.terminatedCalled());
1412 assertTrue(p.isShutdown());
1413 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001414 }
1415
1416 /**
1417 * beforeExecute and afterExecute are called when executing task
1418 */
1419 public void testBeforeAfter() throws InterruptedException {
1420 ExtendedTPE p = new ExtendedTPE();
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001421 try (PoolCleaner cleaner = cleaner(p)) {
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001422 final CountDownLatch done = new CountDownLatch(1);
Narayan Kamath8e9a0e92015-04-28 11:40:00 +01001423 p.execute(new CheckedRunnable() {
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001424 public void realRun() {
1425 done.countDown();
Narayan Kamath8e9a0e92015-04-28 11:40:00 +01001426 }});
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001427 await(p.afterCalled);
1428 assertEquals(0, done.getCount());
1429 assertTrue(p.afterCalled());
1430 assertTrue(p.beforeCalled());
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001431 }
1432 }
1433
1434 /**
1435 * completed submit of callable returns result
1436 */
1437 public void testSubmitCallable() throws Exception {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001438 final ExecutorService e =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001439 new ThreadPoolExecutor(2, 2,
1440 LONG_DELAY_MS, MILLISECONDS,
1441 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001442 try (PoolCleaner cleaner = cleaner(e)) {
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001443 Future<String> future = e.submit(new StringTask());
1444 String result = future.get();
1445 assertSame(TEST_STRING, result);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001446 }
1447 }
1448
1449 /**
1450 * completed submit of runnable returns successfully
1451 */
1452 public void testSubmitRunnable() throws Exception {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001453 final ExecutorService e =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001454 new ThreadPoolExecutor(2, 2,
1455 LONG_DELAY_MS, MILLISECONDS,
1456 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001457 try (PoolCleaner cleaner = cleaner(e)) {
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001458 Future<?> future = e.submit(new NoOpRunnable());
1459 future.get();
1460 assertTrue(future.isDone());
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001461 }
1462 }
1463
1464 /**
1465 * completed submit of (runnable, result) returns result
1466 */
1467 public void testSubmitRunnable2() throws Exception {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001468 final ExecutorService e =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001469 new ThreadPoolExecutor(2, 2,
1470 LONG_DELAY_MS, MILLISECONDS,
1471 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001472 try (PoolCleaner cleaner = cleaner(e)) {
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001473 Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1474 String result = future.get();
1475 assertSame(TEST_STRING, result);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001476 }
1477 }
1478
1479 /**
1480 * invokeAny(null) throws NPE
1481 */
1482 public void testInvokeAny1() throws Exception {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001483 final ExecutorService e =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001484 new ThreadPoolExecutor(2, 2,
1485 LONG_DELAY_MS, MILLISECONDS,
1486 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001487 try (PoolCleaner cleaner = cleaner(e)) {
1488 try {
1489 e.invokeAny(null);
1490 shouldThrow();
1491 } catch (NullPointerException success) {}
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001492 }
1493 }
1494
1495 /**
1496 * invokeAny(empty collection) throws IAE
1497 */
1498 public void testInvokeAny2() throws Exception {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001499 final ExecutorService e =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001500 new ThreadPoolExecutor(2, 2,
1501 LONG_DELAY_MS, MILLISECONDS,
1502 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001503 try (PoolCleaner cleaner = cleaner(e)) {
1504 try {
1505 e.invokeAny(new ArrayList<Callable<String>>());
1506 shouldThrow();
1507 } catch (IllegalArgumentException success) {}
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001508 }
1509 }
1510
1511 /**
1512 * invokeAny(c) throws NPE if c has null elements
1513 */
1514 public void testInvokeAny3() throws Exception {
1515 final CountDownLatch latch = new CountDownLatch(1);
1516 final ExecutorService e =
1517 new ThreadPoolExecutor(2, 2,
1518 LONG_DELAY_MS, MILLISECONDS,
1519 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001520 try (PoolCleaner cleaner = cleaner(e)) {
1521 List<Callable<String>> l = new ArrayList<Callable<String>>();
1522 l.add(latchAwaitingStringTask(latch));
1523 l.add(null);
1524 try {
1525 e.invokeAny(l);
1526 shouldThrow();
1527 } catch (NullPointerException success) {}
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001528 latch.countDown();
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001529 }
1530 }
1531
1532 /**
1533 * invokeAny(c) throws ExecutionException if no task completes
1534 */
1535 public void testInvokeAny4() throws Exception {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001536 final ExecutorService e =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001537 new ThreadPoolExecutor(2, 2,
1538 LONG_DELAY_MS, MILLISECONDS,
1539 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001540 try (PoolCleaner cleaner = cleaner(e)) {
1541 List<Callable<String>> l = new ArrayList<Callable<String>>();
1542 l.add(new NPETask());
1543 try {
1544 e.invokeAny(l);
1545 shouldThrow();
1546 } catch (ExecutionException success) {
1547 assertTrue(success.getCause() instanceof NullPointerException);
1548 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001549 }
1550 }
1551
1552 /**
1553 * invokeAny(c) returns result of some task
1554 */
1555 public void testInvokeAny5() throws Exception {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001556 final ExecutorService e =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001557 new ThreadPoolExecutor(2, 2,
1558 LONG_DELAY_MS, MILLISECONDS,
1559 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001560 try (PoolCleaner cleaner = cleaner(e)) {
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001561 List<Callable<String>> l = new ArrayList<Callable<String>>();
1562 l.add(new StringTask());
1563 l.add(new StringTask());
1564 String result = e.invokeAny(l);
1565 assertSame(TEST_STRING, result);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001566 }
1567 }
1568
1569 /**
1570 * invokeAll(null) throws NPE
1571 */
1572 public void testInvokeAll1() throws Exception {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001573 final ExecutorService e =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001574 new ThreadPoolExecutor(2, 2,
1575 LONG_DELAY_MS, MILLISECONDS,
1576 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001577 try (PoolCleaner cleaner = cleaner(e)) {
1578 try {
1579 e.invokeAll(null);
1580 shouldThrow();
1581 } catch (NullPointerException success) {}
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001582 }
1583 }
1584
1585 /**
1586 * invokeAll(empty collection) returns empty collection
1587 */
1588 public void testInvokeAll2() throws InterruptedException {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001589 final ExecutorService e =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001590 new ThreadPoolExecutor(2, 2,
1591 LONG_DELAY_MS, MILLISECONDS,
1592 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001593 try (PoolCleaner cleaner = cleaner(e)) {
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001594 List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1595 assertTrue(r.isEmpty());
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001596 }
1597 }
1598
1599 /**
1600 * invokeAll(c) throws NPE if c has null elements
1601 */
1602 public void testInvokeAll3() throws Exception {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001603 final ExecutorService e =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001604 new ThreadPoolExecutor(2, 2,
1605 LONG_DELAY_MS, MILLISECONDS,
1606 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001607 try (PoolCleaner cleaner = cleaner(e)) {
1608 List<Callable<String>> l = new ArrayList<Callable<String>>();
1609 l.add(new StringTask());
1610 l.add(null);
1611 try {
1612 e.invokeAll(l);
1613 shouldThrow();
1614 } catch (NullPointerException success) {}
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001615 }
1616 }
1617
1618 /**
1619 * get of element of invokeAll(c) throws exception on failed task
1620 */
1621 public void testInvokeAll4() throws Exception {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001622 final ExecutorService e =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001623 new ThreadPoolExecutor(2, 2,
1624 LONG_DELAY_MS, MILLISECONDS,
1625 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001626 try (PoolCleaner cleaner = cleaner(e)) {
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001627 List<Callable<String>> l = new ArrayList<Callable<String>>();
1628 l.add(new NPETask());
1629 List<Future<String>> futures = e.invokeAll(l);
1630 assertEquals(1, futures.size());
1631 try {
1632 futures.get(0).get();
1633 shouldThrow();
1634 } catch (ExecutionException success) {
1635 assertTrue(success.getCause() instanceof NullPointerException);
1636 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001637 }
1638 }
1639
1640 /**
1641 * invokeAll(c) returns results of all completed tasks
1642 */
1643 public void testInvokeAll5() throws Exception {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001644 final ExecutorService e =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001645 new ThreadPoolExecutor(2, 2,
1646 LONG_DELAY_MS, MILLISECONDS,
1647 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001648 try (PoolCleaner cleaner = cleaner(e)) {
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001649 List<Callable<String>> l = new ArrayList<Callable<String>>();
1650 l.add(new StringTask());
1651 l.add(new StringTask());
1652 List<Future<String>> futures = e.invokeAll(l);
1653 assertEquals(2, futures.size());
1654 for (Future<String> future : futures)
1655 assertSame(TEST_STRING, future.get());
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001656 }
1657 }
1658
1659 /**
1660 * timed invokeAny(null) throws NPE
1661 */
1662 public void testTimedInvokeAny1() throws Exception {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001663 final ExecutorService e =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001664 new ThreadPoolExecutor(2, 2,
1665 LONG_DELAY_MS, MILLISECONDS,
1666 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001667 try (PoolCleaner cleaner = cleaner(e)) {
1668 try {
1669 e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1670 shouldThrow();
1671 } catch (NullPointerException success) {}
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001672 }
1673 }
1674
1675 /**
1676 * timed invokeAny(,,null) throws NPE
1677 */
1678 public void testTimedInvokeAnyNullTimeUnit() throws Exception {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001679 final ExecutorService e =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001680 new ThreadPoolExecutor(2, 2,
1681 LONG_DELAY_MS, MILLISECONDS,
1682 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001683 try (PoolCleaner cleaner = cleaner(e)) {
1684 List<Callable<String>> l = new ArrayList<Callable<String>>();
1685 l.add(new StringTask());
1686 try {
1687 e.invokeAny(l, MEDIUM_DELAY_MS, null);
1688 shouldThrow();
1689 } catch (NullPointerException success) {}
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001690 }
1691 }
1692
1693 /**
1694 * timed invokeAny(empty collection) throws IAE
1695 */
1696 public void testTimedInvokeAny2() throws Exception {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001697 final ExecutorService e =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001698 new ThreadPoolExecutor(2, 2,
1699 LONG_DELAY_MS, MILLISECONDS,
1700 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001701 try (PoolCleaner cleaner = cleaner(e)) {
1702 try {
1703 e.invokeAny(new ArrayList<Callable<String>>(),
1704 MEDIUM_DELAY_MS, MILLISECONDS);
1705 shouldThrow();
1706 } catch (IllegalArgumentException success) {}
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001707 }
1708 }
1709
1710 /**
1711 * timed invokeAny(c) throws NPE if c has null elements
1712 */
1713 public void testTimedInvokeAny3() throws Exception {
1714 final CountDownLatch latch = new CountDownLatch(1);
1715 final ExecutorService e =
1716 new ThreadPoolExecutor(2, 2,
1717 LONG_DELAY_MS, MILLISECONDS,
1718 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001719 try (PoolCleaner cleaner = cleaner(e)) {
1720 List<Callable<String>> l = new ArrayList<Callable<String>>();
1721 l.add(latchAwaitingStringTask(latch));
1722 l.add(null);
1723 try {
1724 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1725 shouldThrow();
1726 } catch (NullPointerException success) {}
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001727 latch.countDown();
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001728 }
1729 }
1730
1731 /**
1732 * timed invokeAny(c) throws ExecutionException if no task completes
1733 */
1734 public void testTimedInvokeAny4() throws Exception {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001735 final ExecutorService e =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001736 new ThreadPoolExecutor(2, 2,
1737 LONG_DELAY_MS, MILLISECONDS,
1738 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001739 try (PoolCleaner cleaner = cleaner(e)) {
1740 long startTime = System.nanoTime();
1741 List<Callable<String>> l = new ArrayList<Callable<String>>();
1742 l.add(new NPETask());
1743 try {
1744 e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1745 shouldThrow();
1746 } catch (ExecutionException success) {
1747 assertTrue(success.getCause() instanceof NullPointerException);
1748 }
1749 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001750 }
1751 }
1752
1753 /**
1754 * timed invokeAny(c) returns result of some task
1755 */
1756 public void testTimedInvokeAny5() throws Exception {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001757 final ExecutorService e =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001758 new ThreadPoolExecutor(2, 2,
1759 LONG_DELAY_MS, MILLISECONDS,
1760 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001761 try (PoolCleaner cleaner = cleaner(e)) {
1762 long startTime = System.nanoTime();
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001763 List<Callable<String>> l = new ArrayList<Callable<String>>();
1764 l.add(new StringTask());
1765 l.add(new StringTask());
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001766 String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001767 assertSame(TEST_STRING, result);
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001768 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001769 }
1770 }
1771
1772 /**
1773 * timed invokeAll(null) throws NPE
1774 */
1775 public void testTimedInvokeAll1() throws Exception {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001776 final ExecutorService e =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001777 new ThreadPoolExecutor(2, 2,
1778 LONG_DELAY_MS, MILLISECONDS,
1779 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001780 try (PoolCleaner cleaner = cleaner(e)) {
1781 try {
1782 e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1783 shouldThrow();
1784 } catch (NullPointerException success) {}
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001785 }
1786 }
1787
1788 /**
1789 * timed invokeAll(,,null) throws NPE
1790 */
1791 public void testTimedInvokeAllNullTimeUnit() throws Exception {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001792 final ExecutorService e =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001793 new ThreadPoolExecutor(2, 2,
1794 LONG_DELAY_MS, MILLISECONDS,
1795 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001796 try (PoolCleaner cleaner = cleaner(e)) {
1797 List<Callable<String>> l = new ArrayList<Callable<String>>();
1798 l.add(new StringTask());
1799 try {
1800 e.invokeAll(l, MEDIUM_DELAY_MS, null);
1801 shouldThrow();
1802 } catch (NullPointerException success) {}
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001803 }
1804 }
1805
1806 /**
1807 * timed invokeAll(empty collection) returns empty collection
1808 */
1809 public void testTimedInvokeAll2() throws InterruptedException {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001810 final ExecutorService e =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001811 new ThreadPoolExecutor(2, 2,
1812 LONG_DELAY_MS, MILLISECONDS,
1813 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001814 try (PoolCleaner cleaner = cleaner(e)) {
1815 List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(),
1816 MEDIUM_DELAY_MS, MILLISECONDS);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001817 assertTrue(r.isEmpty());
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001818 }
1819 }
1820
1821 /**
1822 * timed invokeAll(c) throws NPE if c has null elements
1823 */
1824 public void testTimedInvokeAll3() throws Exception {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001825 final ExecutorService e =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001826 new ThreadPoolExecutor(2, 2,
1827 LONG_DELAY_MS, MILLISECONDS,
1828 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001829 try (PoolCleaner cleaner = cleaner(e)) {
1830 List<Callable<String>> l = new ArrayList<Callable<String>>();
1831 l.add(new StringTask());
1832 l.add(null);
1833 try {
1834 e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1835 shouldThrow();
1836 } catch (NullPointerException success) {}
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001837 }
1838 }
1839
1840 /**
1841 * get of element of invokeAll(c) throws exception on failed task
1842 */
1843 public void testTimedInvokeAll4() throws Exception {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001844 final ExecutorService e =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001845 new ThreadPoolExecutor(2, 2,
1846 LONG_DELAY_MS, MILLISECONDS,
1847 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001848 try (PoolCleaner cleaner = cleaner(e)) {
1849 List<Callable<String>> l = new ArrayList<Callable<String>>();
1850 l.add(new NPETask());
1851 List<Future<String>> futures =
1852 e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1853 assertEquals(1, futures.size());
1854 try {
1855 futures.get(0).get();
1856 shouldThrow();
1857 } catch (ExecutionException success) {
1858 assertTrue(success.getCause() instanceof NullPointerException);
1859 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001860 }
1861 }
1862
1863 /**
1864 * timed invokeAll(c) returns results of all completed tasks
1865 */
1866 public void testTimedInvokeAll5() throws Exception {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001867 final ExecutorService e =
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001868 new ThreadPoolExecutor(2, 2,
1869 LONG_DELAY_MS, MILLISECONDS,
1870 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001871 try (PoolCleaner cleaner = cleaner(e)) {
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001872 List<Callable<String>> l = new ArrayList<Callable<String>>();
1873 l.add(new StringTask());
1874 l.add(new StringTask());
1875 List<Future<String>> futures =
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001876 e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001877 assertEquals(2, futures.size());
1878 for (Future<String> future : futures)
1879 assertSame(TEST_STRING, future.get());
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001880 }
1881 }
1882
1883 /**
1884 * timed invokeAll(c) cancels tasks not completed by timeout
1885 */
1886 public void testTimedInvokeAll6() throws Exception {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001887 for (long timeout = timeoutMillis();;) {
1888 final CountDownLatch done = new CountDownLatch(1);
1889 final Callable<String> waiter = new CheckedCallable<String>() {
1890 public String realCall() {
1891 try { done.await(LONG_DELAY_MS, MILLISECONDS); }
1892 catch (InterruptedException ok) {}
1893 return "1"; }};
1894 final ExecutorService p =
1895 new ThreadPoolExecutor(2, 2,
1896 LONG_DELAY_MS, MILLISECONDS,
1897 new ArrayBlockingQueue<Runnable>(10));
1898 try (PoolCleaner cleaner = cleaner(p, done)) {
1899 List<Callable<String>> tasks = new ArrayList<>();
1900 tasks.add(new StringTask("0"));
1901 tasks.add(waiter);
1902 tasks.add(new StringTask("2"));
1903 long startTime = System.nanoTime();
1904 List<Future<String>> futures =
1905 p.invokeAll(tasks, timeout, MILLISECONDS);
1906 assertEquals(tasks.size(), futures.size());
1907 assertTrue(millisElapsedSince(startTime) >= timeout);
1908 for (Future future : futures)
1909 assertTrue(future.isDone());
1910 assertTrue(futures.get(1).isCancelled());
1911 try {
1912 assertEquals("0", futures.get(0).get());
1913 assertEquals("2", futures.get(2).get());
1914 break;
1915 } catch (CancellationException retryWithLongerTimeout) {
1916 timeout *= 2;
1917 if (timeout >= LONG_DELAY_MS / 2)
1918 fail("expected exactly one task to be cancelled");
1919 }
1920 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001921 }
1922 }
1923
1924 /**
1925 * Execution continues if there is at least one thread even if
1926 * thread factory fails to create more
1927 */
1928 public void testFailingThreadFactory() throws InterruptedException {
1929 final ExecutorService e =
1930 new ThreadPoolExecutor(100, 100,
1931 LONG_DELAY_MS, MILLISECONDS,
1932 new LinkedBlockingQueue<Runnable>(),
1933 new FailingThreadFactory());
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001934 try (PoolCleaner cleaner = cleaner(e)) {
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001935 final int TASKS = 100;
1936 final CountDownLatch done = new CountDownLatch(TASKS);
1937 for (int k = 0; k < TASKS; ++k)
1938 e.execute(new CheckedRunnable() {
1939 public void realRun() {
1940 done.countDown();
1941 }});
1942 assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001943 }
1944 }
1945
1946 /**
1947 * allowsCoreThreadTimeOut is by default false.
1948 */
1949 public void testAllowsCoreThreadTimeOut() {
1950 final ThreadPoolExecutor p =
1951 new ThreadPoolExecutor(2, 2,
1952 1000, MILLISECONDS,
1953 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001954 try (PoolCleaner cleaner = cleaner(p)) {
1955 assertFalse(p.allowsCoreThreadTimeOut());
1956 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001957 }
1958
1959 /**
1960 * allowCoreThreadTimeOut(true) causes idle threads to time out
1961 */
1962 public void testAllowCoreThreadTimeOut_true() throws Exception {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001963 long keepAliveTime = timeoutMillis();
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001964 final ThreadPoolExecutor p =
1965 new ThreadPoolExecutor(2, 10,
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001966 keepAliveTime, MILLISECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001967 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001968 try (PoolCleaner cleaner = cleaner(p)) {
1969 final CountDownLatch threadStarted = new CountDownLatch(1);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001970 p.allowCoreThreadTimeOut(true);
1971 p.execute(new CheckedRunnable() {
1972 public void realRun() {
1973 threadStarted.countDown();
1974 assertEquals(1, p.getPoolSize());
1975 }});
1976 await(threadStarted);
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001977 delay(keepAliveTime);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001978 long startTime = System.nanoTime();
1979 while (p.getPoolSize() > 0
1980 && millisElapsedSince(startTime) < LONG_DELAY_MS)
1981 Thread.yield();
1982 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1983 assertEquals(0, p.getPoolSize());
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001984 }
1985 }
1986
1987 /**
1988 * allowCoreThreadTimeOut(false) causes idle threads not to time out
1989 */
1990 public void testAllowCoreThreadTimeOut_false() throws Exception {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001991 long keepAliveTime = timeoutMillis();
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001992 final ThreadPoolExecutor p =
1993 new ThreadPoolExecutor(2, 10,
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001994 keepAliveTime, MILLISECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001995 new ArrayBlockingQueue<Runnable>(10));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00001996 try (PoolCleaner cleaner = cleaner(p)) {
1997 final CountDownLatch threadStarted = new CountDownLatch(1);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001998 p.allowCoreThreadTimeOut(false);
1999 p.execute(new CheckedRunnable() {
2000 public void realRun() throws InterruptedException {
2001 threadStarted.countDown();
2002 assertTrue(p.getPoolSize() >= 1);
2003 }});
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00002004 delay(2 * keepAliveTime);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01002005 assertTrue(p.getPoolSize() >= 1);
Calin Juravle8f0d92b2013-08-01 17:26:00 +01002006 }
2007 }
2008
2009 /**
2010 * execute allows the same task to be submitted multiple times, even
2011 * if rejected
2012 */
2013 public void testRejectedRecycledTask() throws InterruptedException {
2014 final int nTasks = 1000;
2015 final CountDownLatch done = new CountDownLatch(nTasks);
2016 final Runnable recycledTask = new Runnable() {
2017 public void run() {
2018 done.countDown();
2019 }};
2020 final ThreadPoolExecutor p =
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00002021 new ThreadPoolExecutor(1, 30,
2022 60, SECONDS,
Calin Juravle8f0d92b2013-08-01 17:26:00 +01002023 new ArrayBlockingQueue(30));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00002024 try (PoolCleaner cleaner = cleaner(p)) {
Calin Juravle8f0d92b2013-08-01 17:26:00 +01002025 for (int i = 0; i < nTasks; ++i) {
2026 for (;;) {
2027 try {
2028 p.execute(recycledTask);
2029 break;
2030 }
2031 catch (RejectedExecutionException ignore) {}
2032 }
2033 }
2034 // enough time to run all tasks
2035 assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +00002036 }
2037 }
2038
2039 /**
2040 * get(cancelled task) throws CancellationException
2041 */
2042 public void testGet_cancelled() throws Exception {
2043 final CountDownLatch done = new CountDownLatch(1);
2044 final ExecutorService e =
2045 new ThreadPoolExecutor(1, 1,
2046 LONG_DELAY_MS, MILLISECONDS,
2047 new LinkedBlockingQueue<Runnable>());
2048 try (PoolCleaner cleaner = cleaner(e, done)) {
2049 final CountDownLatch blockerStarted = new CountDownLatch(1);
2050 final List<Future<?>> futures = new ArrayList<>();
2051 for (int i = 0; i < 2; i++) {
2052 Runnable r = new CheckedRunnable() { public void realRun()
2053 throws Throwable {
2054 blockerStarted.countDown();
2055 assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
2056 }};
2057 futures.add(e.submit(r));
2058 }
2059 await(blockerStarted);
2060 for (Future<?> future : futures) future.cancel(false);
2061 for (Future<?> future : futures) {
2062 try {
2063 future.get();
2064 shouldThrow();
2065 } catch (CancellationException success) {}
2066 try {
2067 future.get(LONG_DELAY_MS, MILLISECONDS);
2068 shouldThrow();
2069 } catch (CancellationException success) {}
2070 assertTrue(future.isCancelled());
2071 assertTrue(future.isDone());
2072 }
Calin Juravle8f0d92b2013-08-01 17:26:00 +01002073 }
2074 }
2075
2076}