ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.39
Committed: Tue Nov 30 02:12:52 2010 UTC (13 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.38: +2 -1 lines
Log Message:
fix rare failures in testIsTerminating

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