ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.51
Committed: Sat Apr 25 04:55:31 2015 UTC (9 years ago) by jsr166
Branch: MAIN
Changes since 1.50: +1 -1 lines
Log Message:
improve main methods; respect system properties; actually fail if a test fails

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