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

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