ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.37
Committed: Mon Oct 11 07:21:32 2010 UTC (13 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.36: +787 -286 lines
Log Message:
remove timing dependencies and optimize runtimes; descriptions of testShutdown3 and testShutdown4 were reversed; testShutDown2 never tested its assertion

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