ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.106
Committed: Tue Oct 6 05:12:16 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.105: +15 -15 lines
Log Message:
improve test diagnosability

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