ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.119
Committed: Mon May 29 22:44:27 2017 UTC (6 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.118: +21 -15 lines
Log Message:
more timeout handling rework; remove most uses of MEDIUM_DELAY_MS; randomize timeouts and TimeUnits; write out IAE and ISE

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