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

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