ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java
Revision: 1.21
Committed: Mon Oct 11 07:21:32 2010 UTC (13 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.20: +419 -219 lines
Log Message:
remove timing dependencies and optimize runtimes; descriptions of testShutdown3 and testShutdown4 were reversed; testShutDown2 never tested its assertion

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