ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.70
Committed: Sun Oct 4 01:30:27 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.69: +3 -2 lines
Log Message:
improve testGetKeepAliveTime

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