ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.62
Committed: Mon Sep 28 03:05:23 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.61: +3 -0 lines
Log Message:
improve tests for shutdownNow

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