ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.77
Committed: Sun Oct 4 02:07:32 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.76: +7 -2 lines
Log Message:
improve testGetMaximumPoolSize

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