ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.84
Committed: Sun Oct 4 02:31:15 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.83: +22 -20 lines
Log Message:
improve testAwaitTermination_timesOut

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