ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java
Revision: 1.93
Committed: Tue Oct 6 16:39:06 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.92: +14 -8 lines
Log Message:
testTimedInvokeAll6: fix very rare failure

File Contents

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