ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ThreadPoolExecutorTest.java (file contents):
Revision 1.3 by dl, Sun Sep 14 20:42:41 2003 UTC vs.
Revision 1.41 by dl, Fri May 6 11:22:08 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines