ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.45
Committed: Sun May 29 07:01:17 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.44: +40 -26 lines
Log Message:
various test case improvements

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