ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java
Revision: 1.38
Committed: Fri Sep 4 20:08:27 2015 UTC (8 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.37: +1 -1 lines
Log Message:
use TimeUnit static imports

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