ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.109
Committed: Tue Oct 6 06:11:32 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.108: +17 -26 lines
Log Message:
improve test diagnosability

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 1.15 * 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 jsr166 1.40 * http://creativecommons.org/publicdomain/zero/1.0/
5 jsr166 1.24 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9 jsr166 1.27 import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 jsr166 1.48 import static java.util.concurrent.TimeUnit.NANOSECONDS;
11 jsr166 1.52 import static java.util.concurrent.TimeUnit.SECONDS;
12 jsr166 1.50
13     import java.util.ArrayList;
14     import java.util.List;
15     import java.util.concurrent.ArrayBlockingQueue;
16     import java.util.concurrent.BlockingQueue;
17 jsr166 1.58 import java.util.concurrent.Callable;
18 jsr166 1.56 import java.util.concurrent.CancellationException;
19 jsr166 1.50 import java.util.concurrent.CountDownLatch;
20     import java.util.concurrent.ExecutionException;
21     import java.util.concurrent.Executors;
22     import java.util.concurrent.ExecutorService;
23     import java.util.concurrent.Future;
24     import java.util.concurrent.FutureTask;
25     import java.util.concurrent.LinkedBlockingQueue;
26     import java.util.concurrent.RejectedExecutionException;
27     import java.util.concurrent.RejectedExecutionHandler;
28     import java.util.concurrent.SynchronousQueue;
29     import java.util.concurrent.ThreadFactory;
30     import java.util.concurrent.ThreadPoolExecutor;
31     import java.util.concurrent.TimeUnit;
32 jsr166 1.61 import java.util.concurrent.atomic.AtomicInteger;
33 jsr166 1.50
34     import junit.framework.Test;
35     import junit.framework.TestSuite;
36 dl 1.1
37 dl 1.3 public class ThreadPoolExecutorTest extends JSR166TestCase {
38 dl 1.1 public static void main(String[] args) {
39 jsr166 1.51 main(suite(), args);
40 dl 1.1 }
41     public static Test suite() {
42     return new TestSuite(ThreadPoolExecutorTest.class);
43     }
44 jsr166 1.24
45 dl 1.8 static class ExtendedTPE extends ThreadPoolExecutor {
46 jsr166 1.45 final CountDownLatch beforeCalled = new CountDownLatch(1);
47     final CountDownLatch afterCalled = new CountDownLatch(1);
48     final CountDownLatch terminatedCalled = new CountDownLatch(1);
49    
50 dl 1.8 public ExtendedTPE() {
51 jsr166 1.27 super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
52 dl 1.8 }
53     protected void beforeExecute(Thread t, Runnable r) {
54 jsr166 1.45 beforeCalled.countDown();
55 dl 1.8 }
56     protected void afterExecute(Runnable r, Throwable t) {
57 jsr166 1.45 afterCalled.countDown();
58 dl 1.8 }
59     protected void terminated() {
60 jsr166 1.45 terminatedCalled.countDown();
61     }
62    
63     public boolean beforeCalled() {
64     return beforeCalled.getCount() == 0;
65     }
66     public boolean afterCalled() {
67     return afterCalled.getCount() == 0;
68     }
69     public boolean terminatedCalled() {
70     return terminatedCalled.getCount() == 0;
71 dl 1.8 }
72     }
73 dl 1.1
74 jsr166 1.26 static class FailingThreadFactory implements ThreadFactory {
75 dl 1.19 int calls = 0;
76 jsr166 1.26 public Thread newThread(Runnable r) {
77 dl 1.20 if (++calls > 1) return null;
78 dl 1.19 return new Thread(r);
79 jsr166 1.24 }
80 dl 1.19 }
81 jsr166 1.24
82 dl 1.3 /**
83 jsr166 1.35 * execute successfully executes a runnable
84 dl 1.1 */
85 jsr166 1.27 public void testExecute() throws InterruptedException {
86 jsr166 1.37 final ThreadPoolExecutor p =
87     new ThreadPoolExecutor(1, 1,
88     LONG_DELAY_MS, MILLISECONDS,
89     new ArrayBlockingQueue<Runnable>(10));
90 jsr166 1.80 try (PoolCleaner cleaner = cleaner(p)) {
91     final CountDownLatch done = new CountDownLatch(1);
92     final Runnable task = new CheckedRunnable() {
93     public void realRun() { done.countDown(); }};
94 jsr166 1.37 p.execute(task);
95 jsr166 1.80 assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
96 jsr166 1.24 }
97 dl 1.1 }
98    
99     /**
100 jsr166 1.35 * getActiveCount increases but doesn't overestimate, when a
101     * thread becomes active
102 dl 1.1 */
103 jsr166 1.27 public void testGetActiveCount() throws InterruptedException {
104 jsr166 1.109 final CountDownLatch done = new CountDownLatch(1);
105 jsr166 1.37 final ThreadPoolExecutor p =
106     new ThreadPoolExecutor(2, 2,
107     LONG_DELAY_MS, MILLISECONDS,
108     new ArrayBlockingQueue<Runnable>(10));
109 jsr166 1.109 try (PoolCleaner cleaner = cleaner(p, done)) {
110 jsr166 1.80 final CountDownLatch threadStarted = new CountDownLatch(1);
111 jsr166 1.37 assertEquals(0, p.getActiveCount());
112     p.execute(new CheckedRunnable() {
113     public void realRun() throws InterruptedException {
114     threadStarted.countDown();
115     assertEquals(1, p.getActiveCount());
116 jsr166 1.106 await(done);
117 jsr166 1.37 }});
118 jsr166 1.108 await(threadStarted);
119 jsr166 1.37 assertEquals(1, p.getActiveCount());
120     }
121 dl 1.1 }
122 dl 1.8
123     /**
124 jsr166 1.35 * prestartCoreThread starts a thread if under corePoolSize, else doesn't
125 dl 1.8 */
126     public void testPrestartCoreThread() {
127 jsr166 1.37 final ThreadPoolExecutor p =
128 jsr166 1.66 new ThreadPoolExecutor(2, 6,
129 jsr166 1.37 LONG_DELAY_MS, MILLISECONDS,
130     new ArrayBlockingQueue<Runnable>(10));
131 jsr166 1.66 try (PoolCleaner cleaner = cleaner(p)) {
132     assertEquals(0, p.getPoolSize());
133     assertTrue(p.prestartCoreThread());
134     assertEquals(1, p.getPoolSize());
135     assertTrue(p.prestartCoreThread());
136     assertEquals(2, p.getPoolSize());
137     assertFalse(p.prestartCoreThread());
138     assertEquals(2, p.getPoolSize());
139     p.setCorePoolSize(4);
140     assertTrue(p.prestartCoreThread());
141     assertEquals(3, p.getPoolSize());
142     assertTrue(p.prestartCoreThread());
143     assertEquals(4, p.getPoolSize());
144     assertFalse(p.prestartCoreThread());
145     assertEquals(4, p.getPoolSize());
146     }
147 dl 1.8 }
148    
149     /**
150 jsr166 1.35 * prestartAllCoreThreads starts all corePoolSize threads
151 dl 1.8 */
152     public void testPrestartAllCoreThreads() {
153 jsr166 1.37 final ThreadPoolExecutor p =
154 jsr166 1.67 new ThreadPoolExecutor(2, 6,
155 jsr166 1.37 LONG_DELAY_MS, MILLISECONDS,
156     new ArrayBlockingQueue<Runnable>(10));
157 jsr166 1.67 try (PoolCleaner cleaner = cleaner(p)) {
158     assertEquals(0, p.getPoolSize());
159     p.prestartAllCoreThreads();
160     assertEquals(2, p.getPoolSize());
161     p.prestartAllCoreThreads();
162     assertEquals(2, p.getPoolSize());
163     p.setCorePoolSize(4);
164     p.prestartAllCoreThreads();
165     assertEquals(4, p.getPoolSize());
166     p.prestartAllCoreThreads();
167     assertEquals(4, p.getPoolSize());
168     }
169 dl 1.8 }
170 jsr166 1.24
171 dl 1.1 /**
172 jsr166 1.35 * getCompletedTaskCount increases, but doesn't overestimate,
173     * when tasks complete
174 dl 1.1 */
175 jsr166 1.27 public void testGetCompletedTaskCount() throws InterruptedException {
176 jsr166 1.37 final ThreadPoolExecutor p =
177     new ThreadPoolExecutor(2, 2,
178     LONG_DELAY_MS, MILLISECONDS,
179     new ArrayBlockingQueue<Runnable>(10));
180 jsr166 1.68 try (PoolCleaner cleaner = cleaner(p)) {
181     final CountDownLatch threadStarted = new CountDownLatch(1);
182     final CountDownLatch threadProceed = new CountDownLatch(1);
183     final CountDownLatch threadDone = new CountDownLatch(1);
184 jsr166 1.37 assertEquals(0, p.getCompletedTaskCount());
185     p.execute(new CheckedRunnable() {
186     public void realRun() throws InterruptedException {
187     threadStarted.countDown();
188     assertEquals(0, p.getCompletedTaskCount());
189     threadProceed.await();
190     threadDone.countDown();
191     }});
192 jsr166 1.45 await(threadStarted);
193 jsr166 1.37 assertEquals(0, p.getCompletedTaskCount());
194     threadProceed.countDown();
195     threadDone.await();
196 jsr166 1.45 long startTime = System.nanoTime();
197     while (p.getCompletedTaskCount() != 1) {
198     if (millisElapsedSince(startTime) > LONG_DELAY_MS)
199     fail("timed out");
200     Thread.yield();
201     }
202 jsr166 1.37 }
203 dl 1.1 }
204 jsr166 1.24
205 dl 1.1 /**
206 jsr166 1.35 * getCorePoolSize returns size given in constructor if not otherwise set
207 dl 1.1 */
208 dl 1.5 public void testGetCorePoolSize() {
209 jsr166 1.37 final ThreadPoolExecutor p =
210     new ThreadPoolExecutor(1, 1,
211     LONG_DELAY_MS, MILLISECONDS,
212     new ArrayBlockingQueue<Runnable>(10));
213 jsr166 1.69 try (PoolCleaner cleaner = cleaner(p)) {
214     assertEquals(1, p.getCorePoolSize());
215     }
216 dl 1.1 }
217 jsr166 1.24
218 dl 1.1 /**
219 jsr166 1.35 * getKeepAliveTime returns value given in constructor if not otherwise set
220 dl 1.1 */
221 dl 1.5 public void testGetKeepAliveTime() {
222 jsr166 1.37 final ThreadPoolExecutor p =
223     new ThreadPoolExecutor(2, 2,
224     1000, MILLISECONDS,
225     new ArrayBlockingQueue<Runnable>(10));
226 jsr166 1.70 try (PoolCleaner cleaner = cleaner(p)) {
227     assertEquals(1, p.getKeepAliveTime(SECONDS));
228     }
229 dl 1.1 }
230 dl 1.8
231 jsr166 1.24 /**
232 dl 1.8 * getThreadFactory returns factory in constructor if not set
233     */
234     public void testGetThreadFactory() {
235 jsr166 1.71 ThreadFactory threadFactory = new SimpleThreadFactory();
236 jsr166 1.37 final ThreadPoolExecutor p =
237     new ThreadPoolExecutor(1, 2,
238     LONG_DELAY_MS, MILLISECONDS,
239     new ArrayBlockingQueue<Runnable>(10),
240 jsr166 1.71 threadFactory,
241 jsr166 1.37 new NoOpREHandler());
242 jsr166 1.71 try (PoolCleaner cleaner = cleaner(p)) {
243     assertSame(threadFactory, p.getThreadFactory());
244     }
245 dl 1.8 }
246    
247 jsr166 1.24 /**
248 dl 1.8 * setThreadFactory sets the thread factory returned by getThreadFactory
249     */
250     public void testSetThreadFactory() {
251 jsr166 1.37 final ThreadPoolExecutor p =
252     new ThreadPoolExecutor(1, 2,
253     LONG_DELAY_MS, MILLISECONDS,
254     new ArrayBlockingQueue<Runnable>(10));
255 jsr166 1.71 try (PoolCleaner cleaner = cleaner(p)) {
256     ThreadFactory threadFactory = new SimpleThreadFactory();
257     p.setThreadFactory(threadFactory);
258     assertSame(threadFactory, p.getThreadFactory());
259     }
260 dl 1.8 }
261    
262 jsr166 1.24 /**
263 dl 1.8 * setThreadFactory(null) throws NPE
264     */
265     public void testSetThreadFactoryNull() {
266 jsr166 1.37 final ThreadPoolExecutor p =
267     new ThreadPoolExecutor(1, 2,
268     LONG_DELAY_MS, MILLISECONDS,
269     new ArrayBlockingQueue<Runnable>(10));
270 jsr166 1.72 try (PoolCleaner cleaner = cleaner(p)) {
271     try {
272     p.setThreadFactory(null);
273     shouldThrow();
274     } catch (NullPointerException success) {}
275 dl 1.8 }
276     }
277    
278 jsr166 1.24 /**
279 dl 1.9 * getRejectedExecutionHandler returns handler in constructor if not set
280     */
281     public void testGetRejectedExecutionHandler() {
282 jsr166 1.73 final RejectedExecutionHandler handler = new NoOpREHandler();
283 jsr166 1.37 final ThreadPoolExecutor p =
284     new ThreadPoolExecutor(1, 2,
285     LONG_DELAY_MS, MILLISECONDS,
286     new ArrayBlockingQueue<Runnable>(10),
287 jsr166 1.73 handler);
288     try (PoolCleaner cleaner = cleaner(p)) {
289     assertSame(handler, p.getRejectedExecutionHandler());
290     }
291 dl 1.9 }
292    
293 jsr166 1.24 /**
294 dl 1.9 * setRejectedExecutionHandler sets the handler returned by
295     * getRejectedExecutionHandler
296     */
297     public void testSetRejectedExecutionHandler() {
298 jsr166 1.37 final ThreadPoolExecutor p =
299     new ThreadPoolExecutor(1, 2,
300     LONG_DELAY_MS, MILLISECONDS,
301     new ArrayBlockingQueue<Runnable>(10));
302 jsr166 1.74 try (PoolCleaner cleaner = cleaner(p)) {
303     RejectedExecutionHandler handler = new NoOpREHandler();
304     p.setRejectedExecutionHandler(handler);
305     assertSame(handler, p.getRejectedExecutionHandler());
306     }
307 dl 1.9 }
308    
309 jsr166 1.24 /**
310 dl 1.9 * setRejectedExecutionHandler(null) throws NPE
311     */
312     public void testSetRejectedExecutionHandlerNull() {
313 jsr166 1.37 final ThreadPoolExecutor p =
314     new ThreadPoolExecutor(1, 2,
315     LONG_DELAY_MS, MILLISECONDS,
316     new ArrayBlockingQueue<Runnable>(10));
317 jsr166 1.75 try (PoolCleaner cleaner = cleaner(p)) {
318     try {
319     p.setRejectedExecutionHandler(null);
320     shouldThrow();
321     } catch (NullPointerException success) {}
322 dl 1.9 }
323     }
324    
325 dl 1.1 /**
326 jsr166 1.35 * getLargestPoolSize increases, but doesn't overestimate, when
327     * multiple threads active
328 dl 1.1 */
329 jsr166 1.27 public void testGetLargestPoolSize() throws InterruptedException {
330 jsr166 1.37 final int THREADS = 3;
331 jsr166 1.103 final CountDownLatch done = new CountDownLatch(1);
332 jsr166 1.37 final ThreadPoolExecutor p =
333     new ThreadPoolExecutor(THREADS, THREADS,
334     LONG_DELAY_MS, MILLISECONDS,
335     new ArrayBlockingQueue<Runnable>(10));
336 jsr166 1.103 try (PoolCleaner cleaner = cleaner(p, done)) {
337     assertEquals(0, p.getLargestPoolSize());
338 jsr166 1.76 final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
339 jsr166 1.37 for (int i = 0; i < THREADS; i++)
340     p.execute(new CheckedRunnable() {
341     public void realRun() throws InterruptedException {
342     threadsStarted.countDown();
343 jsr166 1.106 await(done);
344 jsr166 1.37 assertEquals(THREADS, p.getLargestPoolSize());
345     }});
346 jsr166 1.107 await(threadsStarted);
347 jsr166 1.37 assertEquals(THREADS, p.getLargestPoolSize());
348     }
349 jsr166 1.76 assertEquals(THREADS, p.getLargestPoolSize());
350 dl 1.1 }
351 jsr166 1.24
352 dl 1.1 /**
353 jsr166 1.35 * getMaximumPoolSize returns value given in constructor if not
354     * otherwise set
355 dl 1.1 */
356 dl 1.5 public void testGetMaximumPoolSize() {
357 jsr166 1.37 final ThreadPoolExecutor p =
358     new ThreadPoolExecutor(2, 3,
359     LONG_DELAY_MS, MILLISECONDS,
360     new ArrayBlockingQueue<Runnable>(10));
361 jsr166 1.77 try (PoolCleaner cleaner = cleaner(p)) {
362     assertEquals(3, p.getMaximumPoolSize());
363     p.setMaximumPoolSize(5);
364     assertEquals(5, p.getMaximumPoolSize());
365     p.setMaximumPoolSize(4);
366     assertEquals(4, p.getMaximumPoolSize());
367     }
368 dl 1.1 }
369 jsr166 1.24
370 dl 1.1 /**
371 jsr166 1.35 * getPoolSize increases, but doesn't overestimate, when threads
372     * become active
373 dl 1.1 */
374 jsr166 1.37 public void testGetPoolSize() throws InterruptedException {
375 jsr166 1.103 final CountDownLatch done = new CountDownLatch(1);
376 jsr166 1.37 final ThreadPoolExecutor p =
377     new ThreadPoolExecutor(1, 1,
378     LONG_DELAY_MS, MILLISECONDS,
379     new ArrayBlockingQueue<Runnable>(10));
380 jsr166 1.103 try (PoolCleaner cleaner = cleaner(p, done)) {
381     assertEquals(0, p.getPoolSize());
382 jsr166 1.78 final CountDownLatch threadStarted = new CountDownLatch(1);
383 jsr166 1.37 p.execute(new CheckedRunnable() {
384     public void realRun() throws InterruptedException {
385     threadStarted.countDown();
386     assertEquals(1, p.getPoolSize());
387 jsr166 1.106 await(done);
388 jsr166 1.37 }});
389 jsr166 1.108 await(threadStarted);
390 jsr166 1.37 assertEquals(1, p.getPoolSize());
391     }
392 dl 1.1 }
393 jsr166 1.24
394 dl 1.1 /**
395 jsr166 1.35 * getTaskCount increases, but doesn't overestimate, when tasks submitted
396 dl 1.1 */
397 jsr166 1.27 public void testGetTaskCount() throws InterruptedException {
398 jsr166 1.100 final int TASKS = 3;
399     final CountDownLatch done = new CountDownLatch(1);
400 jsr166 1.37 final ThreadPoolExecutor p =
401     new ThreadPoolExecutor(1, 1,
402     LONG_DELAY_MS, MILLISECONDS,
403     new ArrayBlockingQueue<Runnable>(10));
404 jsr166 1.100 try (PoolCleaner cleaner = cleaner(p, done)) {
405 jsr166 1.81 final CountDownLatch threadStarted = new CountDownLatch(1);
406 jsr166 1.37 assertEquals(0, p.getTaskCount());
407 jsr166 1.100 assertEquals(0, p.getCompletedTaskCount());
408 jsr166 1.37 p.execute(new CheckedRunnable() {
409     public void realRun() throws InterruptedException {
410     threadStarted.countDown();
411 jsr166 1.106 await(done);
412 jsr166 1.37 }});
413 jsr166 1.108 await(threadStarted);
414 jsr166 1.37 assertEquals(1, p.getTaskCount());
415 jsr166 1.100 assertEquals(0, p.getCompletedTaskCount());
416     for (int i = 0; i < TASKS; i++) {
417     assertEquals(1 + i, p.getTaskCount());
418     p.execute(new CheckedRunnable() {
419     public void realRun() throws InterruptedException {
420     threadStarted.countDown();
421     assertEquals(1 + TASKS, p.getTaskCount());
422 jsr166 1.106 await(done);
423 jsr166 1.100 }});
424     }
425     assertEquals(1 + TASKS, p.getTaskCount());
426     assertEquals(0, p.getCompletedTaskCount());
427 jsr166 1.37 }
428 jsr166 1.100 assertEquals(1 + TASKS, p.getTaskCount());
429     assertEquals(1 + TASKS, p.getCompletedTaskCount());
430 dl 1.1 }
431 jsr166 1.24
432 dl 1.1 /**
433 jsr166 1.43 * isShutdown is false before shutdown, true after
434 dl 1.1 */
435 dl 1.5 public void testIsShutdown() {
436 jsr166 1.37 final ThreadPoolExecutor p =
437     new ThreadPoolExecutor(1, 1,
438     LONG_DELAY_MS, MILLISECONDS,
439     new ArrayBlockingQueue<Runnable>(10));
440 jsr166 1.82 try (PoolCleaner cleaner = cleaner(p)) {
441     assertFalse(p.isShutdown());
442     try { p.shutdown(); } catch (SecurityException ok) { return; }
443     assertTrue(p.isShutdown());
444     }
445 dl 1.1 }
446    
447     /**
448 jsr166 1.48 * awaitTermination on a non-shutdown pool times out
449     */
450     public void testAwaitTermination_timesOut() throws InterruptedException {
451     final ThreadPoolExecutor p =
452     new ThreadPoolExecutor(1, 1,
453     LONG_DELAY_MS, MILLISECONDS,
454     new ArrayBlockingQueue<Runnable>(10));
455 jsr166 1.84 try (PoolCleaner cleaner = cleaner(p)) {
456     assertFalse(p.isTerminated());
457     assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
458     assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
459     assertFalse(p.awaitTermination(-1L, NANOSECONDS));
460     assertFalse(p.awaitTermination(-1L, MILLISECONDS));
461     assertFalse(p.awaitTermination(0L, NANOSECONDS));
462     assertFalse(p.awaitTermination(0L, MILLISECONDS));
463     long timeoutNanos = 999999L;
464     long startTime = System.nanoTime();
465     assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
466     assertTrue(System.nanoTime() - startTime >= timeoutNanos);
467     assertFalse(p.isTerminated());
468     startTime = System.nanoTime();
469     long timeoutMillis = timeoutMillis();
470     assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
471     assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
472     assertFalse(p.isTerminated());
473     try { p.shutdown(); } catch (SecurityException ok) { return; }
474     assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
475     assertTrue(p.isTerminated());
476     }
477 jsr166 1.48 }
478    
479     /**
480 jsr166 1.35 * isTerminated is false before termination, true after
481 dl 1.1 */
482 jsr166 1.27 public void testIsTerminated() throws InterruptedException {
483 jsr166 1.37 final ThreadPoolExecutor p =
484     new ThreadPoolExecutor(1, 1,
485     LONG_DELAY_MS, MILLISECONDS,
486     new ArrayBlockingQueue<Runnable>(10));
487 jsr166 1.83 try (PoolCleaner cleaner = cleaner(p)) {
488     final CountDownLatch threadStarted = new CountDownLatch(1);
489     final CountDownLatch done = new CountDownLatch(1);
490     assertFalse(p.isTerminating());
491 jsr166 1.37 p.execute(new CheckedRunnable() {
492     public void realRun() throws InterruptedException {
493 jsr166 1.83 assertFalse(p.isTerminating());
494 jsr166 1.37 threadStarted.countDown();
495 jsr166 1.106 await(done);
496 jsr166 1.37 }});
497 jsr166 1.108 await(threadStarted);
498 jsr166 1.39 assertFalse(p.isTerminating());
499 jsr166 1.37 done.countDown();
500     try { p.shutdown(); } catch (SecurityException ok) { return; }
501 jsr166 1.83 assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
502     assertTrue(p.isTerminated());
503     assertFalse(p.isTerminating());
504 dl 1.1 }
505 dl 1.5 }
506    
507     /**
508 jsr166 1.35 * isTerminating is not true when running or when terminated
509 dl 1.5 */
510 jsr166 1.27 public void testIsTerminating() throws InterruptedException {
511 jsr166 1.37 final ThreadPoolExecutor p =
512     new ThreadPoolExecutor(1, 1,
513     LONG_DELAY_MS, MILLISECONDS,
514     new ArrayBlockingQueue<Runnable>(10));
515 jsr166 1.85 try (PoolCleaner cleaner = cleaner(p)) {
516     final CountDownLatch threadStarted = new CountDownLatch(1);
517     final CountDownLatch done = new CountDownLatch(1);
518 jsr166 1.37 assertFalse(p.isTerminating());
519     p.execute(new CheckedRunnable() {
520     public void realRun() throws InterruptedException {
521 jsr166 1.38 assertFalse(p.isTerminating());
522 jsr166 1.37 threadStarted.countDown();
523 jsr166 1.106 await(done);
524 jsr166 1.37 }});
525 jsr166 1.108 await(threadStarted);
526 jsr166 1.37 assertFalse(p.isTerminating());
527     done.countDown();
528     try { p.shutdown(); } catch (SecurityException ok) { return; }
529 jsr166 1.85 assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
530     assertTrue(p.isTerminated());
531     assertFalse(p.isTerminating());
532 jsr166 1.37 }
533 dl 1.1 }
534    
535     /**
536 dl 1.8 * getQueue returns the work queue, which contains queued tasks
537     */
538 jsr166 1.27 public void testGetQueue() throws InterruptedException {
539 jsr166 1.109 final CountDownLatch done = new CountDownLatch(1);
540 jsr166 1.37 final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
541     final ThreadPoolExecutor p =
542     new ThreadPoolExecutor(1, 1,
543     LONG_DELAY_MS, MILLISECONDS,
544     q);
545 jsr166 1.109 try (PoolCleaner cleaner = cleaner(p, done)) {
546 jsr166 1.86 final CountDownLatch threadStarted = new CountDownLatch(1);
547 jsr166 1.37 FutureTask[] tasks = new FutureTask[5];
548     for (int i = 0; i < tasks.length; i++) {
549     Callable task = new CheckedCallable<Boolean>() {
550     public Boolean realCall() throws InterruptedException {
551     threadStarted.countDown();
552     assertSame(q, p.getQueue());
553 jsr166 1.106 await(done);
554 jsr166 1.37 return Boolean.TRUE;
555     }};
556     tasks[i] = new FutureTask(task);
557     p.execute(tasks[i]);
558     }
559 jsr166 1.108 await(threadStarted);
560 jsr166 1.37 assertSame(q, p.getQueue());
561     assertFalse(q.contains(tasks[0]));
562     assertTrue(q.contains(tasks[tasks.length - 1]));
563     assertEquals(tasks.length - 1, q.size());
564 dl 1.8 }
565     }
566    
567     /**
568     * remove(task) removes queued task, and fails to remove active task
569     */
570 jsr166 1.27 public void testRemove() throws InterruptedException {
571 jsr166 1.109 final CountDownLatch done = new CountDownLatch(1);
572 dl 1.8 BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
573 jsr166 1.37 final ThreadPoolExecutor p =
574     new ThreadPoolExecutor(1, 1,
575     LONG_DELAY_MS, MILLISECONDS,
576     q);
577 jsr166 1.109 try (PoolCleaner cleaner = cleaner(p, done)) {
578 jsr166 1.87 Runnable[] tasks = new Runnable[6];
579     final CountDownLatch threadStarted = new CountDownLatch(1);
580 jsr166 1.37 for (int i = 0; i < tasks.length; i++) {
581     tasks[i] = new CheckedRunnable() {
582     public void realRun() throws InterruptedException {
583     threadStarted.countDown();
584 jsr166 1.106 await(done);
585 jsr166 1.37 }};
586     p.execute(tasks[i]);
587     }
588 jsr166 1.108 await(threadStarted);
589 jsr166 1.37 assertFalse(p.remove(tasks[0]));
590 dl 1.8 assertTrue(q.contains(tasks[4]));
591     assertTrue(q.contains(tasks[3]));
592 jsr166 1.37 assertTrue(p.remove(tasks[4]));
593     assertFalse(p.remove(tasks[4]));
594 dl 1.8 assertFalse(q.contains(tasks[4]));
595     assertTrue(q.contains(tasks[3]));
596 jsr166 1.37 assertTrue(p.remove(tasks[3]));
597 dl 1.8 assertFalse(q.contains(tasks[3]));
598     }
599     }
600    
601     /**
602 jsr166 1.35 * purge removes cancelled tasks from the queue
603 dl 1.1 */
604 jsr166 1.37 public void testPurge() throws InterruptedException {
605     final CountDownLatch threadStarted = new CountDownLatch(1);
606     final CountDownLatch done = new CountDownLatch(1);
607     final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
608     final ThreadPoolExecutor p =
609     new ThreadPoolExecutor(1, 1,
610     LONG_DELAY_MS, MILLISECONDS,
611     q);
612 jsr166 1.101 try (PoolCleaner cleaner = cleaner(p, done)) {
613 jsr166 1.87 FutureTask[] tasks = new FutureTask[5];
614 jsr166 1.37 for (int i = 0; i < tasks.length; i++) {
615     Callable task = new CheckedCallable<Boolean>() {
616     public Boolean realCall() throws InterruptedException {
617     threadStarted.countDown();
618 jsr166 1.106 await(done);
619 jsr166 1.37 return Boolean.TRUE;
620     }};
621     tasks[i] = new FutureTask(task);
622     p.execute(tasks[i]);
623     }
624 jsr166 1.108 await(threadStarted);
625 jsr166 1.37 assertEquals(tasks.length, p.getTaskCount());
626     assertEquals(tasks.length - 1, q.size());
627     assertEquals(1L, p.getActiveCount());
628     assertEquals(0L, p.getCompletedTaskCount());
629     tasks[4].cancel(true);
630     tasks[3].cancel(false);
631     p.purge();
632     assertEquals(tasks.length - 3, q.size());
633     assertEquals(tasks.length - 2, p.getTaskCount());
634     p.purge(); // Nothing to do
635     assertEquals(tasks.length - 3, q.size());
636     assertEquals(tasks.length - 2, p.getTaskCount());
637     }
638 dl 1.1 }
639    
640     /**
641 jsr166 1.60 * shutdownNow returns a list containing tasks that were not run,
642     * and those tasks are drained from the queue
643 dl 1.1 */
644 jsr166 1.61 public void testShutdownNow() throws InterruptedException {
645     final int poolSize = 2;
646     final int count = 5;
647     final AtomicInteger ran = new AtomicInteger(0);
648 jsr166 1.37 final ThreadPoolExecutor p =
649 jsr166 1.61 new ThreadPoolExecutor(poolSize, poolSize,
650 jsr166 1.37 LONG_DELAY_MS, MILLISECONDS,
651     new ArrayBlockingQueue<Runnable>(10));
652 jsr166 1.99 final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
653 jsr166 1.63 Runnable waiter = new CheckedRunnable() { public void realRun() {
654 jsr166 1.61 threadsStarted.countDown();
655 dl 1.17 try {
656 jsr166 1.61 MILLISECONDS.sleep(2 * LONG_DELAY_MS);
657     } catch (InterruptedException success) {}
658     ran.getAndIncrement();
659     }};
660     for (int i = 0; i < count; i++)
661     p.execute(waiter);
662 jsr166 1.107 await(threadsStarted);
663 jsr166 1.62 assertEquals(poolSize, p.getActiveCount());
664     assertEquals(0, p.getCompletedTaskCount());
665 jsr166 1.61 final List<Runnable> queuedTasks;
666     try {
667     queuedTasks = p.shutdownNow();
668     } catch (SecurityException ok) {
669     return; // Allowed in case test doesn't have privs
670 dl 1.1 }
671 jsr166 1.37 assertTrue(p.isShutdown());
672 jsr166 1.60 assertTrue(p.getQueue().isEmpty());
673 jsr166 1.61 assertEquals(count - poolSize, queuedTasks.size());
674     assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
675     assertTrue(p.isTerminated());
676     assertEquals(poolSize, ran.get());
677 jsr166 1.62 assertEquals(poolSize, p.getCompletedTaskCount());
678 dl 1.1 }
679    
680     // Exception Tests
681    
682 jsr166 1.24 /**
683     * Constructor throws if corePoolSize argument is less than zero
684 dl 1.6 */
685 dl 1.1 public void testConstructor1() {
686 dl 1.5 try {
687 jsr166 1.52 new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
688 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10));
689 dl 1.5 shouldThrow();
690 jsr166 1.28 } catch (IllegalArgumentException success) {}
691 dl 1.1 }
692 jsr166 1.24
693     /**
694     * Constructor throws if maximumPoolSize is less than zero
695 dl 1.6 */
696 dl 1.1 public void testConstructor2() {
697 dl 1.5 try {
698 jsr166 1.52 new ThreadPoolExecutor(1, -1, 1L, SECONDS,
699 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10));
700 dl 1.5 shouldThrow();
701 jsr166 1.28 } catch (IllegalArgumentException success) {}
702 dl 1.1 }
703 jsr166 1.24
704     /**
705     * Constructor throws if maximumPoolSize is equal to zero
706 dl 1.6 */
707 dl 1.1 public void testConstructor3() {
708 dl 1.5 try {
709 jsr166 1.52 new ThreadPoolExecutor(1, 0, 1L, SECONDS,
710 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10));
711 dl 1.5 shouldThrow();
712 jsr166 1.28 } catch (IllegalArgumentException success) {}
713 dl 1.1 }
714    
715 jsr166 1.24 /**
716     * Constructor throws if keepAliveTime is less than zero
717 dl 1.6 */
718 dl 1.1 public void testConstructor4() {
719 dl 1.5 try {
720 jsr166 1.52 new ThreadPoolExecutor(1, 2, -1L, SECONDS,
721 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10));
722 dl 1.5 shouldThrow();
723 jsr166 1.28 } catch (IllegalArgumentException success) {}
724 dl 1.1 }
725    
726 jsr166 1.24 /**
727     * Constructor throws if corePoolSize is greater than the maximumPoolSize
728 dl 1.6 */
729 dl 1.1 public void testConstructor5() {
730 dl 1.5 try {
731 jsr166 1.52 new ThreadPoolExecutor(2, 1, 1L, SECONDS,
732 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10));
733 dl 1.5 shouldThrow();
734 jsr166 1.28 } catch (IllegalArgumentException success) {}
735 dl 1.1 }
736 jsr166 1.24
737     /**
738     * Constructor throws if workQueue is set to null
739 dl 1.6 */
740 dl 1.8 public void testConstructorNullPointerException() {
741 dl 1.5 try {
742 jsr166 1.52 new ThreadPoolExecutor(1, 2, 1L, SECONDS,
743 jsr166 1.37 (BlockingQueue) null);
744 dl 1.5 shouldThrow();
745 jsr166 1.28 } catch (NullPointerException success) {}
746 dl 1.1 }
747    
748 jsr166 1.24 /**
749     * Constructor throws if corePoolSize argument is less than zero
750 dl 1.6 */
751 dl 1.1 public void testConstructor6() {
752 dl 1.5 try {
753 jsr166 1.52 new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
754 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
755     new SimpleThreadFactory());
756 dl 1.5 shouldThrow();
757 jsr166 1.28 } catch (IllegalArgumentException success) {}
758 dl 1.1 }
759 jsr166 1.24
760     /**
761     * Constructor throws if maximumPoolSize is less than zero
762 dl 1.6 */
763 dl 1.1 public void testConstructor7() {
764 dl 1.5 try {
765 jsr166 1.52 new ThreadPoolExecutor(1, -1, 1L, SECONDS,
766 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
767     new SimpleThreadFactory());
768 dl 1.5 shouldThrow();
769 jsr166 1.28 } catch (IllegalArgumentException success) {}
770 dl 1.1 }
771    
772 jsr166 1.24 /**
773     * Constructor throws if maximumPoolSize is equal to zero
774 dl 1.6 */
775 dl 1.1 public void testConstructor8() {
776 dl 1.5 try {
777 jsr166 1.52 new ThreadPoolExecutor(1, 0, 1L, SECONDS,
778 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
779     new SimpleThreadFactory());
780 dl 1.5 shouldThrow();
781 jsr166 1.28 } catch (IllegalArgumentException success) {}
782 dl 1.1 }
783    
784 jsr166 1.24 /**
785     * Constructor throws if keepAliveTime is less than zero
786 dl 1.6 */
787 dl 1.1 public void testConstructor9() {
788 dl 1.5 try {
789 jsr166 1.52 new ThreadPoolExecutor(1, 2, -1L, SECONDS,
790 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
791     new SimpleThreadFactory());
792 dl 1.5 shouldThrow();
793 jsr166 1.28 } catch (IllegalArgumentException success) {}
794 dl 1.1 }
795    
796 jsr166 1.24 /**
797     * Constructor throws if corePoolSize is greater than the maximumPoolSize
798 dl 1.6 */
799 dl 1.1 public void testConstructor10() {
800 dl 1.5 try {
801 jsr166 1.52 new ThreadPoolExecutor(2, 1, 1L, SECONDS,
802 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
803     new SimpleThreadFactory());
804 dl 1.5 shouldThrow();
805 jsr166 1.28 } catch (IllegalArgumentException success) {}
806 dl 1.1 }
807    
808 jsr166 1.24 /**
809     * Constructor throws if workQueue is set to null
810 dl 1.6 */
811 dl 1.8 public void testConstructorNullPointerException2() {
812 dl 1.5 try {
813 jsr166 1.52 new ThreadPoolExecutor(1, 2, 1L, SECONDS,
814 jsr166 1.37 (BlockingQueue) null,
815     new SimpleThreadFactory());
816 dl 1.5 shouldThrow();
817 jsr166 1.28 } catch (NullPointerException success) {}
818 dl 1.1 }
819    
820 jsr166 1.24 /**
821     * Constructor throws if threadFactory is set to null
822 dl 1.6 */
823 dl 1.8 public void testConstructorNullPointerException3() {
824 dl 1.5 try {
825 jsr166 1.52 new ThreadPoolExecutor(1, 2, 1L, SECONDS,
826 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
827     (ThreadFactory) null);
828 dl 1.5 shouldThrow();
829 jsr166 1.28 } catch (NullPointerException success) {}
830 dl 1.1 }
831 jsr166 1.24
832     /**
833     * Constructor throws if corePoolSize argument is less than zero
834 dl 1.6 */
835 dl 1.1 public void testConstructor11() {
836 dl 1.5 try {
837 jsr166 1.52 new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
838 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
839     new NoOpREHandler());
840 dl 1.5 shouldThrow();
841 jsr166 1.28 } catch (IllegalArgumentException success) {}
842 dl 1.1 }
843    
844 jsr166 1.24 /**
845     * Constructor throws if maximumPoolSize is less than zero
846 dl 1.6 */
847 dl 1.1 public void testConstructor12() {
848 dl 1.5 try {
849 jsr166 1.52 new ThreadPoolExecutor(1, -1, 1L, SECONDS,
850 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
851     new NoOpREHandler());
852 dl 1.5 shouldThrow();
853 jsr166 1.28 } catch (IllegalArgumentException success) {}
854 dl 1.1 }
855    
856 jsr166 1.24 /**
857     * Constructor throws if maximumPoolSize is equal to zero
858 dl 1.6 */
859 dl 1.1 public void testConstructor13() {
860 dl 1.5 try {
861 jsr166 1.52 new ThreadPoolExecutor(1, 0, 1L, SECONDS,
862 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
863     new NoOpREHandler());
864 dl 1.5 shouldThrow();
865 jsr166 1.28 } catch (IllegalArgumentException success) {}
866 dl 1.1 }
867    
868 jsr166 1.24 /**
869     * Constructor throws if keepAliveTime is less than zero
870 dl 1.6 */
871 dl 1.1 public void testConstructor14() {
872 dl 1.5 try {
873 jsr166 1.52 new ThreadPoolExecutor(1, 2, -1L, SECONDS,
874 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
875     new NoOpREHandler());
876 dl 1.5 shouldThrow();
877 jsr166 1.28 } catch (IllegalArgumentException success) {}
878 dl 1.1 }
879    
880 jsr166 1.24 /**
881     * Constructor throws if corePoolSize is greater than the maximumPoolSize
882 dl 1.6 */
883 dl 1.1 public void testConstructor15() {
884 dl 1.5 try {
885 jsr166 1.52 new ThreadPoolExecutor(2, 1, 1L, SECONDS,
886 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
887     new NoOpREHandler());
888 dl 1.5 shouldThrow();
889 jsr166 1.28 } catch (IllegalArgumentException success) {}
890 dl 1.1 }
891    
892 jsr166 1.24 /**
893     * Constructor throws if workQueue is set to null
894 dl 1.6 */
895 dl 1.8 public void testConstructorNullPointerException4() {
896 dl 1.5 try {
897 jsr166 1.52 new ThreadPoolExecutor(1, 2, 1L, SECONDS,
898 jsr166 1.37 (BlockingQueue) null,
899     new NoOpREHandler());
900 dl 1.5 shouldThrow();
901 jsr166 1.28 } catch (NullPointerException success) {}
902 dl 1.1 }
903    
904 jsr166 1.24 /**
905     * Constructor throws if handler is set to null
906 dl 1.6 */
907 dl 1.8 public void testConstructorNullPointerException5() {
908 dl 1.5 try {
909 jsr166 1.52 new ThreadPoolExecutor(1, 2, 1L, SECONDS,
910 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
911     (RejectedExecutionHandler) null);
912 dl 1.5 shouldThrow();
913 jsr166 1.28 } catch (NullPointerException success) {}
914 dl 1.1 }
915    
916 jsr166 1.24 /**
917     * Constructor throws if corePoolSize argument is less than zero
918 dl 1.6 */
919 dl 1.1 public void testConstructor16() {
920 dl 1.5 try {
921 jsr166 1.52 new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
922 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
923     new SimpleThreadFactory(),
924     new NoOpREHandler());
925 dl 1.5 shouldThrow();
926 jsr166 1.28 } catch (IllegalArgumentException success) {}
927 dl 1.1 }
928    
929 jsr166 1.24 /**
930     * Constructor throws if maximumPoolSize is less than zero
931 dl 1.6 */
932 dl 1.1 public void testConstructor17() {
933 dl 1.5 try {
934 jsr166 1.52 new ThreadPoolExecutor(1, -1, 1L, SECONDS,
935 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
936     new SimpleThreadFactory(),
937     new NoOpREHandler());
938 dl 1.5 shouldThrow();
939 jsr166 1.28 } catch (IllegalArgumentException success) {}
940 dl 1.1 }
941    
942 jsr166 1.24 /**
943     * Constructor throws if maximumPoolSize is equal to zero
944 dl 1.6 */
945 dl 1.1 public void testConstructor18() {
946 dl 1.5 try {
947 jsr166 1.52 new ThreadPoolExecutor(1, 0, 1L, SECONDS,
948 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
949     new SimpleThreadFactory(),
950     new NoOpREHandler());
951 dl 1.5 shouldThrow();
952 jsr166 1.28 } catch (IllegalArgumentException success) {}
953 dl 1.1 }
954    
955 jsr166 1.24 /**
956     * Constructor throws if keepAliveTime is less than zero
957 dl 1.6 */
958 dl 1.1 public void testConstructor19() {
959 dl 1.5 try {
960 jsr166 1.52 new ThreadPoolExecutor(1, 2, -1L, SECONDS,
961 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
962     new SimpleThreadFactory(),
963     new NoOpREHandler());
964 dl 1.5 shouldThrow();
965 jsr166 1.28 } catch (IllegalArgumentException success) {}
966 dl 1.1 }
967    
968 jsr166 1.24 /**
969     * Constructor throws if corePoolSize is greater than the maximumPoolSize
970 dl 1.6 */
971 dl 1.1 public void testConstructor20() {
972 dl 1.5 try {
973 jsr166 1.52 new ThreadPoolExecutor(2, 1, 1L, SECONDS,
974 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
975     new SimpleThreadFactory(),
976     new NoOpREHandler());
977 dl 1.5 shouldThrow();
978 jsr166 1.28 } catch (IllegalArgumentException success) {}
979 dl 1.1 }
980    
981 jsr166 1.24 /**
982 jsr166 1.36 * Constructor throws if workQueue is null
983 dl 1.6 */
984 dl 1.8 public void testConstructorNullPointerException6() {
985 dl 1.5 try {
986 jsr166 1.52 new ThreadPoolExecutor(1, 2, 1L, SECONDS,
987 jsr166 1.37 (BlockingQueue) null,
988     new SimpleThreadFactory(),
989     new NoOpREHandler());
990 dl 1.5 shouldThrow();
991 jsr166 1.28 } catch (NullPointerException success) {}
992 dl 1.1 }
993    
994 jsr166 1.24 /**
995 jsr166 1.36 * Constructor throws if handler is null
996 dl 1.6 */
997 dl 1.8 public void testConstructorNullPointerException7() {
998 dl 1.5 try {
999 jsr166 1.52 new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1000 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
1001     new SimpleThreadFactory(),
1002     (RejectedExecutionHandler) null);
1003 dl 1.5 shouldThrow();
1004 jsr166 1.28 } catch (NullPointerException success) {}
1005 dl 1.1 }
1006    
1007 jsr166 1.24 /**
1008 jsr166 1.36 * Constructor throws if ThreadFactory is null
1009 dl 1.6 */
1010 dl 1.8 public void testConstructorNullPointerException8() {
1011 dl 1.5 try {
1012 jsr166 1.52 new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1013 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
1014     (ThreadFactory) null,
1015     new NoOpREHandler());
1016 dl 1.5 shouldThrow();
1017 jsr166 1.28 } catch (NullPointerException success) {}
1018 dl 1.1 }
1019 jsr166 1.24
1020 jsr166 1.37 /**
1021     * get of submitted callable throws InterruptedException if interrupted
1022     */
1023     public void testInterruptedSubmit() throws InterruptedException {
1024 jsr166 1.104 final CountDownLatch done = new CountDownLatch(1);
1025 jsr166 1.37 final ThreadPoolExecutor p =
1026     new ThreadPoolExecutor(1, 1,
1027 jsr166 1.55 60, SECONDS,
1028 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10));
1029    
1030 jsr166 1.104 try (PoolCleaner cleaner = cleaner(p, done)) {
1031 jsr166 1.88 final CountDownLatch threadStarted = new CountDownLatch(1);
1032 jsr166 1.37 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1033     public void realRun() throws Exception {
1034     Callable task = new CheckedCallable<Boolean>() {
1035     public Boolean realCall() throws InterruptedException {
1036     threadStarted.countDown();
1037 jsr166 1.106 await(done);
1038 jsr166 1.37 return Boolean.TRUE;
1039     }};
1040     p.submit(task).get();
1041     }});
1042    
1043 jsr166 1.104 await(threadStarted);
1044 jsr166 1.37 t.interrupt();
1045 jsr166 1.104 awaitTermination(t);
1046 jsr166 1.37 }
1047     }
1048 dl 1.1
1049     /**
1050 jsr166 1.35 * execute throws RejectedExecutionException if saturated.
1051 dl 1.8 */
1052     public void testSaturatedExecute() {
1053 jsr166 1.109 final CountDownLatch done = new CountDownLatch(1);
1054 jsr166 1.89 final ThreadPoolExecutor p =
1055 jsr166 1.27 new ThreadPoolExecutor(1, 1,
1056     LONG_DELAY_MS, MILLISECONDS,
1057     new ArrayBlockingQueue<Runnable>(1));
1058 jsr166 1.109 try (PoolCleaner cleaner = cleaner(p, done)) {
1059 jsr166 1.37 Runnable task = new CheckedRunnable() {
1060     public void realRun() throws InterruptedException {
1061 jsr166 1.106 await(done);
1062 jsr166 1.37 }};
1063     for (int i = 0; i < 2; ++i)
1064     p.execute(task);
1065     for (int i = 0; i < 2; ++i) {
1066     try {
1067     p.execute(task);
1068     shouldThrow();
1069     } catch (RejectedExecutionException success) {}
1070     assertTrue(p.getTaskCount() <= 2);
1071     }
1072     }
1073     }
1074    
1075     /**
1076     * submit(runnable) throws RejectedExecutionException if saturated.
1077     */
1078     public void testSaturatedSubmitRunnable() {
1079 jsr166 1.109 final CountDownLatch done = new CountDownLatch(1);
1080 jsr166 1.96 final ThreadPoolExecutor p =
1081 jsr166 1.37 new ThreadPoolExecutor(1, 1,
1082     LONG_DELAY_MS, MILLISECONDS,
1083     new ArrayBlockingQueue<Runnable>(1));
1084 jsr166 1.109 try (PoolCleaner cleaner = cleaner(p, done)) {
1085 jsr166 1.37 Runnable task = new CheckedRunnable() {
1086     public void realRun() throws InterruptedException {
1087 jsr166 1.106 await(done);
1088 jsr166 1.37 }};
1089     for (int i = 0; i < 2; ++i)
1090     p.submit(task);
1091     for (int i = 0; i < 2; ++i) {
1092     try {
1093     p.execute(task);
1094     shouldThrow();
1095     } catch (RejectedExecutionException success) {}
1096     assertTrue(p.getTaskCount() <= 2);
1097     }
1098     }
1099     }
1100    
1101     /**
1102     * submit(callable) throws RejectedExecutionException if saturated.
1103     */
1104     public void testSaturatedSubmitCallable() {
1105 jsr166 1.109 final CountDownLatch done = new CountDownLatch(1);
1106 jsr166 1.96 final ThreadPoolExecutor p =
1107 jsr166 1.37 new ThreadPoolExecutor(1, 1,
1108     LONG_DELAY_MS, MILLISECONDS,
1109     new ArrayBlockingQueue<Runnable>(1));
1110 jsr166 1.109 try (PoolCleaner cleaner = cleaner(p, done)) {
1111 jsr166 1.37 Runnable task = new CheckedRunnable() {
1112     public void realRun() throws InterruptedException {
1113 jsr166 1.106 await(done);
1114 jsr166 1.37 }};
1115 jsr166 1.27 for (int i = 0; i < 2; ++i)
1116 jsr166 1.37 p.submit(Executors.callable(task));
1117 jsr166 1.27 for (int i = 0; i < 2; ++i) {
1118     try {
1119 jsr166 1.37 p.execute(task);
1120 jsr166 1.27 shouldThrow();
1121     } catch (RejectedExecutionException success) {}
1122 jsr166 1.37 assertTrue(p.getTaskCount() <= 2);
1123 dl 1.8 }
1124 jsr166 1.27 }
1125 dl 1.8 }
1126    
1127     /**
1128 jsr166 1.35 * executor using CallerRunsPolicy runs task if saturated.
1129 dl 1.8 */
1130     public void testSaturatedExecute2() {
1131 jsr166 1.37 final ThreadPoolExecutor p =
1132     new ThreadPoolExecutor(1, 1,
1133     LONG_DELAY_MS,
1134     MILLISECONDS,
1135     new ArrayBlockingQueue<Runnable>(1),
1136 jsr166 1.90 new ThreadPoolExecutor.CallerRunsPolicy());
1137     try (PoolCleaner cleaner = cleaner(p)) {
1138     final CountDownLatch done = new CountDownLatch(1);
1139     Runnable blocker = new CheckedRunnable() {
1140     public void realRun() throws InterruptedException {
1141 jsr166 1.106 await(done);
1142 jsr166 1.90 }};
1143     p.execute(blocker);
1144 dl 1.8 TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1145 jsr166 1.90 for (int i = 0; i < tasks.length; i++)
1146 dl 1.8 tasks[i] = new TrackedNoOpRunnable();
1147 jsr166 1.90 for (int i = 0; i < tasks.length; i++)
1148 dl 1.8 p.execute(tasks[i]);
1149 jsr166 1.90 for (int i = 1; i < tasks.length; i++)
1150 dl 1.8 assertTrue(tasks[i].done);
1151 jsr166 1.91 assertFalse(tasks[0].done); // waiting in queue
1152 jsr166 1.90 done.countDown();
1153 dl 1.8 }
1154     }
1155    
1156     /**
1157 jsr166 1.35 * executor using DiscardPolicy drops task if saturated.
1158 dl 1.8 */
1159     public void testSaturatedExecute3() {
1160 jsr166 1.109 final CountDownLatch done = new CountDownLatch(1);
1161 jsr166 1.91 final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1162     for (int i = 0; i < tasks.length; ++i)
1163     tasks[i] = new TrackedNoOpRunnable();
1164 jsr166 1.37 final ThreadPoolExecutor p =
1165     new ThreadPoolExecutor(1, 1,
1166 jsr166 1.91 LONG_DELAY_MS, MILLISECONDS,
1167     new ArrayBlockingQueue<Runnable>(1),
1168     new ThreadPoolExecutor.DiscardPolicy());
1169 jsr166 1.109 try (PoolCleaner cleaner = cleaner(p, done)) {
1170 jsr166 1.91 p.execute(awaiter(done));
1171    
1172 jsr166 1.37 for (TrackedNoOpRunnable task : tasks)
1173     p.execute(task);
1174 jsr166 1.91 for (int i = 1; i < tasks.length; i++)
1175     assertFalse(tasks[i].done);
1176 dl 1.8 }
1177 jsr166 1.91 for (int i = 1; i < tasks.length; i++)
1178     assertFalse(tasks[i].done);
1179     assertTrue(tasks[0].done); // was waiting in queue
1180 dl 1.8 }
1181    
1182     /**
1183 jsr166 1.35 * executor using DiscardOldestPolicy drops oldest task if saturated.
1184 dl 1.8 */
1185     public void testSaturatedExecute4() {
1186 jsr166 1.92 final CountDownLatch done = new CountDownLatch(1);
1187     LatchAwaiter r1 = awaiter(done);
1188     LatchAwaiter r2 = awaiter(done);
1189     LatchAwaiter r3 = awaiter(done);
1190 jsr166 1.37 final ThreadPoolExecutor p =
1191     new ThreadPoolExecutor(1, 1,
1192     LONG_DELAY_MS, MILLISECONDS,
1193     new ArrayBlockingQueue<Runnable>(1),
1194 jsr166 1.92 new ThreadPoolExecutor.DiscardOldestPolicy());
1195 jsr166 1.109 try (PoolCleaner cleaner = cleaner(p, done)) {
1196 jsr166 1.92 assertEquals(LatchAwaiter.NEW, r1.state);
1197     assertEquals(LatchAwaiter.NEW, r2.state);
1198     assertEquals(LatchAwaiter.NEW, r3.state);
1199     p.execute(r1);
1200 dl 1.8 p.execute(r2);
1201     assertTrue(p.getQueue().contains(r2));
1202     p.execute(r3);
1203     assertFalse(p.getQueue().contains(r2));
1204     assertTrue(p.getQueue().contains(r3));
1205     }
1206 jsr166 1.92 assertEquals(LatchAwaiter.DONE, r1.state);
1207     assertEquals(LatchAwaiter.NEW, r2.state);
1208     assertEquals(LatchAwaiter.DONE, r3.state);
1209 dl 1.8 }
1210    
1211     /**
1212 jsr166 1.35 * execute throws RejectedExecutionException if shutdown
1213 dl 1.1 */
1214 dl 1.8 public void testRejectedExecutionExceptionOnShutdown() {
1215 jsr166 1.96 final ThreadPoolExecutor p =
1216 jsr166 1.37 new ThreadPoolExecutor(1, 1,
1217     LONG_DELAY_MS, MILLISECONDS,
1218     new ArrayBlockingQueue<Runnable>(1));
1219     try { p.shutdown(); } catch (SecurityException ok) { return; }
1220 jsr166 1.93 try (PoolCleaner cleaner = cleaner(p)) {
1221     try {
1222     p.execute(new NoOpRunnable());
1223     shouldThrow();
1224     } catch (RejectedExecutionException success) {}
1225     }
1226 dl 1.1 }
1227 dl 1.6
1228     /**
1229 jsr166 1.35 * execute using CallerRunsPolicy drops task on shutdown
1230 dl 1.8 */
1231     public void testCallerRunsOnShutdown() {
1232     RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
1233 jsr166 1.37 final ThreadPoolExecutor p =
1234     new ThreadPoolExecutor(1, 1,
1235     LONG_DELAY_MS, MILLISECONDS,
1236     new ArrayBlockingQueue<Runnable>(1), h);
1237 dl 1.8
1238 jsr166 1.25 try { p.shutdown(); } catch (SecurityException ok) { return; }
1239 jsr166 1.94 try (PoolCleaner cleaner = cleaner(p)) {
1240 dl 1.8 TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1241 jsr166 1.31 p.execute(r);
1242 dl 1.8 assertFalse(r.done);
1243     }
1244     }
1245    
1246     /**
1247 jsr166 1.35 * execute using DiscardPolicy drops task on shutdown
1248 dl 1.8 */
1249     public void testDiscardOnShutdown() {
1250 jsr166 1.95 final ThreadPoolExecutor p =
1251 jsr166 1.37 new ThreadPoolExecutor(1, 1,
1252     LONG_DELAY_MS, MILLISECONDS,
1253     new ArrayBlockingQueue<Runnable>(1),
1254 jsr166 1.95 new ThreadPoolExecutor.DiscardPolicy());
1255 dl 1.8
1256 jsr166 1.25 try { p.shutdown(); } catch (SecurityException ok) { return; }
1257 jsr166 1.95 try (PoolCleaner cleaner = cleaner(p)) {
1258 dl 1.8 TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1259 jsr166 1.31 p.execute(r);
1260 dl 1.8 assertFalse(r.done);
1261     }
1262     }
1263    
1264     /**
1265 jsr166 1.35 * execute using DiscardOldestPolicy drops task on shutdown
1266 dl 1.6 */
1267 dl 1.8 public void testDiscardOldestOnShutdown() {
1268 jsr166 1.95 final ThreadPoolExecutor p =
1269 jsr166 1.37 new ThreadPoolExecutor(1, 1,
1270     LONG_DELAY_MS, MILLISECONDS,
1271     new ArrayBlockingQueue<Runnable>(1),
1272 jsr166 1.95 new ThreadPoolExecutor.DiscardOldestPolicy());
1273 dl 1.8
1274 jsr166 1.25 try { p.shutdown(); } catch (SecurityException ok) { return; }
1275 jsr166 1.95 try (PoolCleaner cleaner = cleaner(p)) {
1276 dl 1.8 TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1277 jsr166 1.31 p.execute(r);
1278 dl 1.8 assertFalse(r.done);
1279     }
1280 dl 1.6 }
1281    
1282     /**
1283 jsr166 1.34 * execute(null) throws NPE
1284 dl 1.6 */
1285     public void testExecuteNull() {
1286 jsr166 1.95 final ThreadPoolExecutor p =
1287     new ThreadPoolExecutor(1, 2,
1288     1L, SECONDS,
1289 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10));
1290 jsr166 1.95 try (PoolCleaner cleaner = cleaner(p)) {
1291     try {
1292     p.execute(null);
1293     shouldThrow();
1294     } catch (NullPointerException success) {}
1295     }
1296 dl 1.6 }
1297 jsr166 1.24
1298 dl 1.1 /**
1299 jsr166 1.34 * setCorePoolSize of negative value throws IllegalArgumentException
1300 dl 1.1 */
1301 dl 1.5 public void testCorePoolSizeIllegalArgumentException() {
1302 jsr166 1.96 final ThreadPoolExecutor p =
1303 jsr166 1.27 new ThreadPoolExecutor(1, 2,
1304     LONG_DELAY_MS, MILLISECONDS,
1305     new ArrayBlockingQueue<Runnable>(10));
1306 jsr166 1.97 try (PoolCleaner cleaner = cleaner(p)) {
1307     try {
1308     p.setCorePoolSize(-1);
1309     shouldThrow();
1310     } catch (IllegalArgumentException success) {}
1311 dl 1.1 }
1312 jsr166 1.24 }
1313 dl 1.1
1314     /**
1315 jsr166 1.35 * setMaximumPoolSize(int) throws IllegalArgumentException if
1316     * given a value less the core pool size
1317 jsr166 1.24 */
1318 dl 1.5 public void testMaximumPoolSizeIllegalArgumentException() {
1319 jsr166 1.96 final ThreadPoolExecutor p =
1320 jsr166 1.27 new ThreadPoolExecutor(2, 3,
1321     LONG_DELAY_MS, MILLISECONDS,
1322     new ArrayBlockingQueue<Runnable>(10));
1323 jsr166 1.98 try (PoolCleaner cleaner = cleaner(p)) {
1324     try {
1325     p.setMaximumPoolSize(1);
1326     shouldThrow();
1327     } catch (IllegalArgumentException success) {}
1328 dl 1.1 }
1329     }
1330 jsr166 1.24
1331 dl 1.1 /**
1332 jsr166 1.35 * setMaximumPoolSize throws IllegalArgumentException
1333     * if given a negative value
1334 dl 1.1 */
1335 dl 1.5 public void testMaximumPoolSizeIllegalArgumentException2() {
1336 jsr166 1.96 final ThreadPoolExecutor p =
1337 jsr166 1.27 new ThreadPoolExecutor(2, 3,
1338     LONG_DELAY_MS, MILLISECONDS,
1339     new ArrayBlockingQueue<Runnable>(10));
1340 jsr166 1.98 try (PoolCleaner cleaner = cleaner(p)) {
1341     try {
1342     p.setMaximumPoolSize(-1);
1343     shouldThrow();
1344     } catch (IllegalArgumentException success) {}
1345 dl 1.1 }
1346     }
1347 jsr166 1.24
1348 dl 1.1 /**
1349 jsr166 1.54 * Configuration changes that allow core pool size greater than
1350     * max pool size result in IllegalArgumentException.
1351     */
1352     public void testPoolSizeInvariants() {
1353 jsr166 1.96 final ThreadPoolExecutor p =
1354 jsr166 1.54 new ThreadPoolExecutor(1, 1,
1355     LONG_DELAY_MS, MILLISECONDS,
1356     new ArrayBlockingQueue<Runnable>(10));
1357 jsr166 1.98 try (PoolCleaner cleaner = cleaner(p)) {
1358     for (int s = 1; s < 5; s++) {
1359     p.setMaximumPoolSize(s);
1360     p.setCorePoolSize(s);
1361     try {
1362     p.setMaximumPoolSize(s - 1);
1363     shouldThrow();
1364     } catch (IllegalArgumentException success) {}
1365     assertEquals(s, p.getCorePoolSize());
1366     assertEquals(s, p.getMaximumPoolSize());
1367     try {
1368     p.setCorePoolSize(s + 1);
1369     shouldThrow();
1370     } catch (IllegalArgumentException success) {}
1371     assertEquals(s, p.getCorePoolSize());
1372     assertEquals(s, p.getMaximumPoolSize());
1373     }
1374 jsr166 1.54 }
1375     }
1376    
1377     /**
1378 jsr166 1.35 * setKeepAliveTime throws IllegalArgumentException
1379     * when given a negative value
1380 dl 1.1 */
1381 dl 1.5 public void testKeepAliveTimeIllegalArgumentException() {
1382 jsr166 1.96 final ThreadPoolExecutor p =
1383 jsr166 1.27 new ThreadPoolExecutor(2, 3,
1384     LONG_DELAY_MS, MILLISECONDS,
1385     new ArrayBlockingQueue<Runnable>(10));
1386 jsr166 1.98 try (PoolCleaner cleaner = cleaner(p)) {
1387     try {
1388     p.setKeepAliveTime(-1, MILLISECONDS);
1389     shouldThrow();
1390     } catch (IllegalArgumentException success) {}
1391 dl 1.1 }
1392     }
1393 dl 1.8
1394     /**
1395     * terminated() is called on termination
1396     */
1397     public void testTerminated() {
1398 jsr166 1.37 ExtendedTPE p = new ExtendedTPE();
1399 jsr166 1.98 try (PoolCleaner cleaner = cleaner(p)) {
1400     try { p.shutdown(); } catch (SecurityException ok) { return; }
1401     assertTrue(p.terminatedCalled());
1402     assertTrue(p.isShutdown());
1403     }
1404 dl 1.8 }
1405    
1406     /**
1407     * beforeExecute and afterExecute are called when executing task
1408     */
1409 jsr166 1.27 public void testBeforeAfter() throws InterruptedException {
1410 jsr166 1.37 ExtendedTPE p = new ExtendedTPE();
1411 jsr166 1.98 try (PoolCleaner cleaner = cleaner(p)) {
1412 jsr166 1.45 final CountDownLatch done = new CountDownLatch(1);
1413 jsr166 1.49 p.execute(new CheckedRunnable() {
1414 jsr166 1.45 public void realRun() {
1415     done.countDown();
1416 jsr166 1.49 }});
1417 jsr166 1.45 await(p.afterCalled);
1418     assertEquals(0, done.getCount());
1419     assertTrue(p.afterCalled());
1420     assertTrue(p.beforeCalled());
1421 dl 1.8 }
1422     }
1423 dl 1.12
1424     /**
1425     * completed submit of callable returns result
1426     */
1427 jsr166 1.27 public void testSubmitCallable() throws Exception {
1428 jsr166 1.98 final ExecutorService e =
1429 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1430     LONG_DELAY_MS, MILLISECONDS,
1431     new ArrayBlockingQueue<Runnable>(10));
1432 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1433 dl 1.12 Future<String> future = e.submit(new StringTask());
1434     String result = future.get();
1435     assertSame(TEST_STRING, result);
1436     }
1437     }
1438    
1439     /**
1440     * completed submit of runnable returns successfully
1441     */
1442 jsr166 1.27 public void testSubmitRunnable() throws Exception {
1443 jsr166 1.98 final ExecutorService e =
1444 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1445     LONG_DELAY_MS, MILLISECONDS,
1446     new ArrayBlockingQueue<Runnable>(10));
1447 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1448 dl 1.12 Future<?> future = e.submit(new NoOpRunnable());
1449     future.get();
1450     assertTrue(future.isDone());
1451     }
1452     }
1453    
1454     /**
1455     * completed submit of (runnable, result) returns result
1456     */
1457 jsr166 1.27 public void testSubmitRunnable2() throws Exception {
1458 jsr166 1.98 final ExecutorService e =
1459 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1460     LONG_DELAY_MS, MILLISECONDS,
1461     new ArrayBlockingQueue<Runnable>(10));
1462 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1463 dl 1.12 Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1464     String result = future.get();
1465     assertSame(TEST_STRING, result);
1466     }
1467     }
1468    
1469     /**
1470     * invokeAny(null) throws NPE
1471     */
1472 jsr166 1.27 public void testInvokeAny1() throws Exception {
1473 jsr166 1.98 final ExecutorService e =
1474 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1475     LONG_DELAY_MS, MILLISECONDS,
1476     new ArrayBlockingQueue<Runnable>(10));
1477 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1478     try {
1479     e.invokeAny(null);
1480     shouldThrow();
1481     } catch (NullPointerException success) {}
1482 dl 1.12 }
1483     }
1484    
1485     /**
1486     * invokeAny(empty collection) throws IAE
1487     */
1488 jsr166 1.27 public void testInvokeAny2() throws Exception {
1489 jsr166 1.98 final ExecutorService e =
1490 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1491     LONG_DELAY_MS, MILLISECONDS,
1492     new ArrayBlockingQueue<Runnable>(10));
1493 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1494     try {
1495     e.invokeAny(new ArrayList<Callable<String>>());
1496     shouldThrow();
1497     } catch (IllegalArgumentException success) {}
1498 dl 1.12 }
1499     }
1500    
1501     /**
1502     * invokeAny(c) throws NPE if c has null elements
1503     */
1504 jsr166 1.27 public void testInvokeAny3() throws Exception {
1505 jsr166 1.37 final CountDownLatch latch = new CountDownLatch(1);
1506     final ExecutorService e =
1507     new ThreadPoolExecutor(2, 2,
1508     LONG_DELAY_MS, MILLISECONDS,
1509     new ArrayBlockingQueue<Runnable>(10));
1510 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1511     List<Callable<String>> l = new ArrayList<Callable<String>>();
1512     l.add(latchAwaitingStringTask(latch));
1513     l.add(null);
1514     try {
1515     e.invokeAny(l);
1516     shouldThrow();
1517     } catch (NullPointerException success) {}
1518 jsr166 1.27 latch.countDown();
1519 dl 1.12 }
1520     }
1521    
1522     /**
1523     * invokeAny(c) throws ExecutionException if no task completes
1524     */
1525 jsr166 1.27 public void testInvokeAny4() throws Exception {
1526 jsr166 1.98 final ExecutorService e =
1527 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1528     LONG_DELAY_MS, MILLISECONDS,
1529     new ArrayBlockingQueue<Runnable>(10));
1530 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1531     List<Callable<String>> l = new ArrayList<Callable<String>>();
1532     l.add(new NPETask());
1533     try {
1534     e.invokeAny(l);
1535     shouldThrow();
1536     } catch (ExecutionException success) {
1537     assertTrue(success.getCause() instanceof NullPointerException);
1538     }
1539 dl 1.12 }
1540     }
1541    
1542     /**
1543     * invokeAny(c) returns result of some task
1544     */
1545 jsr166 1.27 public void testInvokeAny5() throws Exception {
1546 jsr166 1.98 final ExecutorService e =
1547 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1548     LONG_DELAY_MS, MILLISECONDS,
1549     new ArrayBlockingQueue<Runnable>(10));
1550 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1551 jsr166 1.33 List<Callable<String>> l = new ArrayList<Callable<String>>();
1552 dl 1.12 l.add(new StringTask());
1553     l.add(new StringTask());
1554     String result = e.invokeAny(l);
1555     assertSame(TEST_STRING, result);
1556     }
1557     }
1558    
1559     /**
1560     * invokeAll(null) throws NPE
1561     */
1562 jsr166 1.27 public void testInvokeAll1() throws Exception {
1563 jsr166 1.98 final ExecutorService e =
1564 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1565     LONG_DELAY_MS, MILLISECONDS,
1566     new ArrayBlockingQueue<Runnable>(10));
1567 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1568     try {
1569     e.invokeAll(null);
1570     shouldThrow();
1571     } catch (NullPointerException success) {}
1572 dl 1.12 }
1573     }
1574    
1575     /**
1576     * invokeAll(empty collection) returns empty collection
1577     */
1578 jsr166 1.27 public void testInvokeAll2() throws InterruptedException {
1579 jsr166 1.98 final ExecutorService e =
1580 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1581     LONG_DELAY_MS, MILLISECONDS,
1582     new ArrayBlockingQueue<Runnable>(10));
1583 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1584 dl 1.12 List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1585     assertTrue(r.isEmpty());
1586     }
1587     }
1588    
1589     /**
1590     * invokeAll(c) throws NPE if c has null elements
1591     */
1592 jsr166 1.27 public void testInvokeAll3() throws Exception {
1593 jsr166 1.98 final ExecutorService e =
1594 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1595     LONG_DELAY_MS, MILLISECONDS,
1596     new ArrayBlockingQueue<Runnable>(10));
1597 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1598     List<Callable<String>> l = new ArrayList<Callable<String>>();
1599     l.add(new StringTask());
1600     l.add(null);
1601     try {
1602     e.invokeAll(l);
1603     shouldThrow();
1604     } catch (NullPointerException success) {}
1605 dl 1.12 }
1606     }
1607    
1608     /**
1609     * get of element of invokeAll(c) throws exception on failed task
1610     */
1611 jsr166 1.27 public void testInvokeAll4() throws Exception {
1612 jsr166 1.98 final ExecutorService e =
1613 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1614     LONG_DELAY_MS, MILLISECONDS,
1615     new ArrayBlockingQueue<Runnable>(10));
1616 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1617 jsr166 1.33 List<Callable<String>> l = new ArrayList<Callable<String>>();
1618 dl 1.12 l.add(new NPETask());
1619 jsr166 1.33 List<Future<String>> futures = e.invokeAll(l);
1620     assertEquals(1, futures.size());
1621     try {
1622     futures.get(0).get();
1623     shouldThrow();
1624     } catch (ExecutionException success) {
1625     assertTrue(success.getCause() instanceof NullPointerException);
1626 jsr166 1.27 }
1627 dl 1.12 }
1628     }
1629    
1630     /**
1631     * invokeAll(c) returns results of all completed tasks
1632     */
1633 jsr166 1.27 public void testInvokeAll5() throws Exception {
1634 jsr166 1.98 final ExecutorService e =
1635 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1636     LONG_DELAY_MS, MILLISECONDS,
1637     new ArrayBlockingQueue<Runnable>(10));
1638 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1639 jsr166 1.33 List<Callable<String>> l = new ArrayList<Callable<String>>();
1640 dl 1.12 l.add(new StringTask());
1641     l.add(new StringTask());
1642 jsr166 1.33 List<Future<String>> futures = e.invokeAll(l);
1643     assertEquals(2, futures.size());
1644     for (Future<String> future : futures)
1645 jsr166 1.27 assertSame(TEST_STRING, future.get());
1646 dl 1.12 }
1647     }
1648    
1649 dl 1.13 /**
1650     * timed invokeAny(null) throws NPE
1651     */
1652 jsr166 1.27 public void testTimedInvokeAny1() throws Exception {
1653 jsr166 1.98 final ExecutorService e =
1654 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1655     LONG_DELAY_MS, MILLISECONDS,
1656     new ArrayBlockingQueue<Runnable>(10));
1657 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1658     try {
1659     e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1660     shouldThrow();
1661     } catch (NullPointerException success) {}
1662 dl 1.13 }
1663     }
1664    
1665     /**
1666     * timed invokeAny(,,null) throws NPE
1667     */
1668 jsr166 1.27 public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1669 jsr166 1.98 final ExecutorService e =
1670 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1671     LONG_DELAY_MS, MILLISECONDS,
1672     new ArrayBlockingQueue<Runnable>(10));
1673 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1674     List<Callable<String>> l = new ArrayList<Callable<String>>();
1675     l.add(new StringTask());
1676     try {
1677     e.invokeAny(l, MEDIUM_DELAY_MS, null);
1678     shouldThrow();
1679     } catch (NullPointerException success) {}
1680 dl 1.13 }
1681     }
1682    
1683     /**
1684     * timed invokeAny(empty collection) throws IAE
1685     */
1686 jsr166 1.27 public void testTimedInvokeAny2() throws Exception {
1687 jsr166 1.98 final ExecutorService e =
1688 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1689     LONG_DELAY_MS, MILLISECONDS,
1690     new ArrayBlockingQueue<Runnable>(10));
1691 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1692     try {
1693     e.invokeAny(new ArrayList<Callable<String>>(),
1694     MEDIUM_DELAY_MS, MILLISECONDS);
1695     shouldThrow();
1696     } catch (IllegalArgumentException success) {}
1697 dl 1.13 }
1698     }
1699    
1700     /**
1701     * timed invokeAny(c) throws NPE if c has null elements
1702     */
1703 jsr166 1.27 public void testTimedInvokeAny3() throws Exception {
1704 jsr166 1.37 final CountDownLatch latch = new CountDownLatch(1);
1705     final ExecutorService e =
1706     new ThreadPoolExecutor(2, 2,
1707     LONG_DELAY_MS, MILLISECONDS,
1708     new ArrayBlockingQueue<Runnable>(10));
1709 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1710     List<Callable<String>> l = new ArrayList<Callable<String>>();
1711     l.add(latchAwaitingStringTask(latch));
1712     l.add(null);
1713     try {
1714     e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1715     shouldThrow();
1716     } catch (NullPointerException success) {}
1717 jsr166 1.30 latch.countDown();
1718 dl 1.13 }
1719     }
1720    
1721     /**
1722     * timed invokeAny(c) throws ExecutionException if no task completes
1723     */
1724 jsr166 1.27 public void testTimedInvokeAny4() throws Exception {
1725 jsr166 1.98 final ExecutorService e =
1726 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1727     LONG_DELAY_MS, MILLISECONDS,
1728     new ArrayBlockingQueue<Runnable>(10));
1729 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1730     List<Callable<String>> l = new ArrayList<Callable<String>>();
1731     l.add(new NPETask());
1732     try {
1733     e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1734     shouldThrow();
1735     } catch (ExecutionException success) {
1736     assertTrue(success.getCause() instanceof NullPointerException);
1737     }
1738 dl 1.13 }
1739     }
1740    
1741     /**
1742     * timed invokeAny(c) returns result of some task
1743     */
1744 jsr166 1.27 public void testTimedInvokeAny5() throws Exception {
1745 jsr166 1.98 final ExecutorService e =
1746 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1747     LONG_DELAY_MS, MILLISECONDS,
1748     new ArrayBlockingQueue<Runnable>(10));
1749 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1750 jsr166 1.33 List<Callable<String>> l = new ArrayList<Callable<String>>();
1751 dl 1.13 l.add(new StringTask());
1752     l.add(new StringTask());
1753 jsr166 1.27 String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1754 dl 1.13 assertSame(TEST_STRING, result);
1755     }
1756     }
1757    
1758     /**
1759     * timed invokeAll(null) throws NPE
1760     */
1761 jsr166 1.27 public void testTimedInvokeAll1() throws Exception {
1762 jsr166 1.98 final ExecutorService e =
1763 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1764     LONG_DELAY_MS, MILLISECONDS,
1765     new ArrayBlockingQueue<Runnable>(10));
1766 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1767     try {
1768     e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1769     shouldThrow();
1770     } catch (NullPointerException success) {}
1771 dl 1.13 }
1772     }
1773    
1774     /**
1775     * timed invokeAll(,,null) throws NPE
1776     */
1777 jsr166 1.27 public void testTimedInvokeAllNullTimeUnit() throws Exception {
1778 jsr166 1.98 final ExecutorService e =
1779 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1780     LONG_DELAY_MS, MILLISECONDS,
1781     new ArrayBlockingQueue<Runnable>(10));
1782 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1783     List<Callable<String>> l = new ArrayList<Callable<String>>();
1784     l.add(new StringTask());
1785     try {
1786     e.invokeAll(l, MEDIUM_DELAY_MS, null);
1787     shouldThrow();
1788     } catch (NullPointerException success) {}
1789 dl 1.13 }
1790     }
1791    
1792     /**
1793     * timed invokeAll(empty collection) returns empty collection
1794     */
1795 jsr166 1.27 public void testTimedInvokeAll2() throws InterruptedException {
1796 jsr166 1.98 final ExecutorService e =
1797 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1798     LONG_DELAY_MS, MILLISECONDS,
1799     new ArrayBlockingQueue<Runnable>(10));
1800 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1801     List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(),
1802     MEDIUM_DELAY_MS, MILLISECONDS);
1803 dl 1.13 assertTrue(r.isEmpty());
1804     }
1805     }
1806    
1807     /**
1808     * timed invokeAll(c) throws NPE if c has null elements
1809     */
1810 jsr166 1.27 public void testTimedInvokeAll3() throws Exception {
1811 jsr166 1.98 final ExecutorService e =
1812 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1813     LONG_DELAY_MS, MILLISECONDS,
1814     new ArrayBlockingQueue<Runnable>(10));
1815 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1816     List<Callable<String>> l = new ArrayList<Callable<String>>();
1817     l.add(new StringTask());
1818     l.add(null);
1819     try {
1820     e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1821     shouldThrow();
1822     } catch (NullPointerException success) {}
1823 dl 1.13 }
1824     }
1825    
1826     /**
1827     * get of element of invokeAll(c) throws exception on failed task
1828     */
1829 jsr166 1.27 public void testTimedInvokeAll4() throws Exception {
1830 jsr166 1.98 final ExecutorService e =
1831 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1832     LONG_DELAY_MS, MILLISECONDS,
1833     new ArrayBlockingQueue<Runnable>(10));
1834 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1835     List<Callable<String>> l = new ArrayList<Callable<String>>();
1836     l.add(new NPETask());
1837     List<Future<String>> futures =
1838 jsr166 1.105 e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1839 jsr166 1.98 assertEquals(1, futures.size());
1840     try {
1841     futures.get(0).get();
1842     shouldThrow();
1843     } catch (ExecutionException success) {
1844     assertTrue(success.getCause() instanceof NullPointerException);
1845     }
1846 dl 1.13 }
1847     }
1848    
1849     /**
1850     * timed invokeAll(c) returns results of all completed tasks
1851     */
1852 jsr166 1.27 public void testTimedInvokeAll5() throws Exception {
1853 jsr166 1.98 final ExecutorService e =
1854 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1855     LONG_DELAY_MS, MILLISECONDS,
1856     new ArrayBlockingQueue<Runnable>(10));
1857 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1858 jsr166 1.33 List<Callable<String>> l = new ArrayList<Callable<String>>();
1859 dl 1.13 l.add(new StringTask());
1860     l.add(new StringTask());
1861 jsr166 1.33 List<Future<String>> futures =
1862 jsr166 1.65 e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1863 jsr166 1.33 assertEquals(2, futures.size());
1864     for (Future<String> future : futures)
1865     assertSame(TEST_STRING, future.get());
1866 dl 1.13 }
1867     }
1868    
1869     /**
1870     * timed invokeAll(c) cancels tasks not completed by timeout
1871     */
1872 jsr166 1.27 public void testTimedInvokeAll6() throws Exception {
1873 jsr166 1.98 final ExecutorService e =
1874 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1875     LONG_DELAY_MS, MILLISECONDS,
1876     new ArrayBlockingQueue<Runnable>(10));
1877 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1878 jsr166 1.56 for (long timeout = timeoutMillis();;) {
1879     List<Callable<String>> tasks = new ArrayList<>();
1880 jsr166 1.59 tasks.add(new StringTask("0"));
1881 jsr166 1.56 tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1882 jsr166 1.59 tasks.add(new StringTask("2"));
1883 jsr166 1.56 long startTime = System.nanoTime();
1884     List<Future<String>> futures =
1885     e.invokeAll(tasks, timeout, MILLISECONDS);
1886     assertEquals(tasks.size(), futures.size());
1887     assertTrue(millisElapsedSince(startTime) >= timeout);
1888     for (Future future : futures)
1889     assertTrue(future.isDone());
1890     assertTrue(futures.get(1).isCancelled());
1891     try {
1892 jsr166 1.59 assertEquals("0", futures.get(0).get());
1893     assertEquals("2", futures.get(2).get());
1894 jsr166 1.56 break;
1895     } catch (CancellationException retryWithLongerTimeout) {
1896     timeout *= 2;
1897     if (timeout >= LONG_DELAY_MS / 2)
1898     fail("expected exactly one task to be cancelled");
1899     }
1900     }
1901 dl 1.13 }
1902     }
1903    
1904 dl 1.19 /**
1905     * Execution continues if there is at least one thread even if
1906     * thread factory fails to create more
1907     */
1908 jsr166 1.27 public void testFailingThreadFactory() throws InterruptedException {
1909 jsr166 1.37 final ExecutorService e =
1910     new ThreadPoolExecutor(100, 100,
1911     LONG_DELAY_MS, MILLISECONDS,
1912     new LinkedBlockingQueue<Runnable>(),
1913     new FailingThreadFactory());
1914 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1915 jsr166 1.37 final int TASKS = 100;
1916     final CountDownLatch done = new CountDownLatch(TASKS);
1917     for (int k = 0; k < TASKS; ++k)
1918     e.execute(new CheckedRunnable() {
1919     public void realRun() {
1920     done.countDown();
1921     }});
1922     assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1923 dl 1.19 }
1924     }
1925 dl 1.21
1926     /**
1927     * allowsCoreThreadTimeOut is by default false.
1928     */
1929     public void testAllowsCoreThreadTimeOut() {
1930 jsr166 1.37 final ThreadPoolExecutor p =
1931     new ThreadPoolExecutor(2, 2,
1932     1000, MILLISECONDS,
1933     new ArrayBlockingQueue<Runnable>(10));
1934 jsr166 1.98 try (PoolCleaner cleaner = cleaner(p)) {
1935     assertFalse(p.allowsCoreThreadTimeOut());
1936     }
1937 dl 1.21 }
1938    
1939     /**
1940     * allowCoreThreadTimeOut(true) causes idle threads to time out
1941     */
1942 jsr166 1.37 public void testAllowCoreThreadTimeOut_true() throws Exception {
1943 jsr166 1.57 long keepAliveTime = timeoutMillis();
1944 jsr166 1.37 final ThreadPoolExecutor p =
1945     new ThreadPoolExecutor(2, 10,
1946 jsr166 1.57 keepAliveTime, MILLISECONDS,
1947 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10));
1948 jsr166 1.98 try (PoolCleaner cleaner = cleaner(p)) {
1949     final CountDownLatch threadStarted = new CountDownLatch(1);
1950 jsr166 1.37 p.allowCoreThreadTimeOut(true);
1951     p.execute(new CheckedRunnable() {
1952 jsr166 1.44 public void realRun() {
1953 jsr166 1.37 threadStarted.countDown();
1954     assertEquals(1, p.getPoolSize());
1955     }});
1956 jsr166 1.44 await(threadStarted);
1957 jsr166 1.57 delay(keepAliveTime);
1958 jsr166 1.44 long startTime = System.nanoTime();
1959     while (p.getPoolSize() > 0
1960     && millisElapsedSince(startTime) < LONG_DELAY_MS)
1961     Thread.yield();
1962     assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1963 jsr166 1.37 assertEquals(0, p.getPoolSize());
1964 dl 1.21 }
1965     }
1966    
1967     /**
1968     * allowCoreThreadTimeOut(false) causes idle threads not to time out
1969     */
1970 jsr166 1.37 public void testAllowCoreThreadTimeOut_false() throws Exception {
1971 jsr166 1.57 long keepAliveTime = timeoutMillis();
1972 jsr166 1.37 final ThreadPoolExecutor p =
1973     new ThreadPoolExecutor(2, 10,
1974 jsr166 1.57 keepAliveTime, MILLISECONDS,
1975 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10));
1976 jsr166 1.98 try (PoolCleaner cleaner = cleaner(p)) {
1977     final CountDownLatch threadStarted = new CountDownLatch(1);
1978 jsr166 1.37 p.allowCoreThreadTimeOut(false);
1979     p.execute(new CheckedRunnable() {
1980     public void realRun() throws InterruptedException {
1981     threadStarted.countDown();
1982     assertTrue(p.getPoolSize() >= 1);
1983     }});
1984 jsr166 1.57 delay(2 * keepAliveTime);
1985 jsr166 1.37 assertTrue(p.getPoolSize() >= 1);
1986 dl 1.21 }
1987     }
1988    
1989 dl 1.23 /**
1990     * execute allows the same task to be submitted multiple times, even
1991     * if rejected
1992     */
1993 jsr166 1.27 public void testRejectedRecycledTask() throws InterruptedException {
1994 dl 1.23 final int nTasks = 1000;
1995 jsr166 1.37 final CountDownLatch done = new CountDownLatch(nTasks);
1996 dl 1.23 final Runnable recycledTask = new Runnable() {
1997 jsr166 1.37 public void run() {
1998     done.countDown();
1999     }};
2000 jsr166 1.24 final ThreadPoolExecutor p =
2001 jsr166 1.55 new ThreadPoolExecutor(1, 30,
2002     60, SECONDS,
2003 dl 1.23 new ArrayBlockingQueue(30));
2004 jsr166 1.98 try (PoolCleaner cleaner = cleaner(p)) {
2005 dl 1.23 for (int i = 0; i < nTasks; ++i) {
2006     for (;;) {
2007     try {
2008     p.execute(recycledTask);
2009     break;
2010     }
2011 jsr166 1.37 catch (RejectedExecutionException ignore) {}
2012 dl 1.23 }
2013     }
2014 jsr166 1.37 // enough time to run all tasks
2015     assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
2016 dl 1.23 }
2017     }
2018 jsr166 1.24
2019 jsr166 1.64 /**
2020     * get(cancelled task) throws CancellationException
2021     */
2022     public void testGet_cancelled() throws Exception {
2023 jsr166 1.109 final CountDownLatch done = new CountDownLatch(1);
2024 jsr166 1.64 final ExecutorService e =
2025     new ThreadPoolExecutor(1, 1,
2026     LONG_DELAY_MS, MILLISECONDS,
2027     new LinkedBlockingQueue<Runnable>());
2028 jsr166 1.109 try (PoolCleaner cleaner = cleaner(e, done)) {
2029 jsr166 1.64 final CountDownLatch blockerStarted = new CountDownLatch(1);
2030     final List<Future<?>> futures = new ArrayList<>();
2031     for (int i = 0; i < 2; i++) {
2032     Runnable r = new CheckedRunnable() { public void realRun()
2033     throws Throwable {
2034     blockerStarted.countDown();
2035     assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
2036     }};
2037     futures.add(e.submit(r));
2038     }
2039 jsr166 1.108 await(blockerStarted);
2040 jsr166 1.64 for (Future<?> future : futures) future.cancel(false);
2041     for (Future<?> future : futures) {
2042     try {
2043     future.get();
2044     shouldThrow();
2045     } catch (CancellationException success) {}
2046     try {
2047     future.get(LONG_DELAY_MS, MILLISECONDS);
2048     shouldThrow();
2049     } catch (CancellationException success) {}
2050     assertTrue(future.isCancelled());
2051     assertTrue(future.isDone());
2052     }
2053     }
2054     }
2055    
2056 dl 1.1 }