ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.54
Committed: Fri Sep 4 19:35:46 2015 UTC (8 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.53: +28 -0 lines
Log Message:
move ThreadPoolExecutor9Test.java into ThreadPoolExecutorTest.java

File Contents

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