ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java
Revision: 1.42
Committed: Mon Sep 28 03:05:23 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.41: +3 -0 lines
Log Message:
improve tests for shutdownNow

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