ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.44
Committed: Fri May 27 19:35:24 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.43: +13 -27 lines
Log Message:
Improve testAllowCoreThreadTimeOut tests

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