ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.100
Committed: Mon Oct 5 21:42:49 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.99: +19 -5 lines
Log Message:
improve testGetTaskCount

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