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

File Contents

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