ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.60
Committed: Sun Sep 27 18:50:50 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.59: +3 -1 lines
Log Message:
testShutdownNow: add queue-draining assertions

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