ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.81
Committed: Sun Oct 4 02:24:49 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.80: +3 -5 lines
Log Message:
improve testGetTaskCount

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