ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.66
Committed: Sun Oct 4 01:18:25 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.65: +17 -9 lines
Log Message:
improve testPrestartCoreThread

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