ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.73
Committed: Sun Oct 4 01:56:51 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.72: +5 -4 lines
Log Message:
improve testGetRejectedExecutionHandler

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