ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.47
Committed: Tue May 31 16:16:24 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.46: +1 -2 lines
Log Message:
use serialClone in serialization tests; update imports

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