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.1 by dl, Sun Aug 31 19:24:56 2003 UTC vs.
Revision 1.31 by jsr166, Sat Nov 21 02:07:27 2009 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/licenses/publicdomain
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 TestCase{
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      }
16
17
19      public static Test suite() {
20          return new TestSuite(ThreadPoolExecutorTest.class);
21      }
22 <    
23 <    private static long SHORT_DELAY_MS = 100;
24 <    private static long MEDIUM_DELAY_MS = 1000;
25 <    private static long LONG_DELAY_MS = 10000;
26 <
27 < //---- testThread class to implement ThreadFactory for use in constructors
28 <    static class testThread implements ThreadFactory{
29 <        public Thread newThread(Runnable r){
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 <        }  
46 >        }
47 >    }
48 >
49 >
50 >    /**
51 >     *  execute successfully executes a runnable
52 >     */
53 >    public void testExecute() throws InterruptedException {
54 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
55 >        try {
56 >            p1.execute(new ShortRunnable());
57 >            Thread.sleep(SMALL_DELAY_MS);
58 >        } finally {
59 >            joinPool(p1);
60 >        }
61      }
62  
63 < //---- testReject class to implement RejectedExecutionHandler for use in the constructors
64 <    static class testReject implements RejectedExecutionHandler{
65 <        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){}
63 >    /**
64 >     *  getActiveCount increases but doesn't overestimate, when a
65 >     *  thread becomes active
66 >     */
67 >    public void testGetActiveCount() throws InterruptedException {
68 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
69 >        assertEquals(0, p2.getActiveCount());
70 >        p2.execute(new MediumRunnable());
71 >        Thread.sleep(SHORT_DELAY_MS);
72 >        assertEquals(1, p2.getActiveCount());
73 >        joinPool(p2);
74      }
75  
76 <    public Runnable newRunnable(){
77 <        return new Runnable(){
78 <                public void run(){
79 <                    try{Thread.sleep(MEDIUM_DELAY_MS);
80 <                    } catch(Exception e){
81 <                    }
82 <                }
83 <            };
76 >    /**
77 >     *  prestartCoreThread starts a thread if under corePoolSize, else doesn't
78 >     */
79 >    public void testPrestartCoreThread() {
80 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
81 >        assertEquals(0, p2.getPoolSize());
82 >        assertTrue(p2.prestartCoreThread());
83 >        assertEquals(1, p2.getPoolSize());
84 >        assertTrue(p2.prestartCoreThread());
85 >        assertEquals(2, p2.getPoolSize());
86 >        assertFalse(p2.prestartCoreThread());
87 >        assertEquals(2, p2.getPoolSize());
88 >        joinPool(p2);
89 >    }
90 >
91 >    /**
92 >     *  prestartAllCoreThreads starts all corePoolSize threads
93 >     */
94 >    public void testPrestartAllCoreThreads() {
95 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
96 >        assertEquals(0, p2.getPoolSize());
97 >        p2.prestartAllCoreThreads();
98 >        assertEquals(2, p2.getPoolSize());
99 >        p2.prestartAllCoreThreads();
100 >        assertEquals(2, p2.getPoolSize());
101 >        joinPool(p2);
102 >    }
103 >
104 >    /**
105 >     *   getCompletedTaskCount increases, but doesn't overestimate,
106 >     *   when tasks complete
107 >     */
108 >    public void testGetCompletedTaskCount() throws InterruptedException {
109 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
110 >        assertEquals(0, p2.getCompletedTaskCount());
111 >        p2.execute(new ShortRunnable());
112 >        Thread.sleep(SMALL_DELAY_MS);
113 >        assertEquals(1, p2.getCompletedTaskCount());
114 >        try { p2.shutdown(); } catch (SecurityException ok) { return; }
115 >        joinPool(p2);
116 >    }
117 >
118 >    /**
119 >     *   getCorePoolSize returns size given in constructor if not otherwise set
120 >     */
121 >    public void testGetCorePoolSize() {
122 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
123 >        assertEquals(1, p1.getCorePoolSize());
124 >        joinPool(p1);
125 >    }
126 >
127 >    /**
128 >     *   getKeepAliveTime returns value given in constructor if not otherwise set
129 >     */
130 >    public void testGetKeepAliveTime() {
131 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
132 >        assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS));
133 >        joinPool(p2);
134 >    }
135 >
136 >
137 >    /**
138 >     * getThreadFactory returns factory in constructor if not set
139 >     */
140 >    public void testGetThreadFactory() {
141 >        ThreadFactory tf = new SimpleThreadFactory();
142 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
143 >        assertSame(tf, p.getThreadFactory());
144 >        joinPool(p);
145 >    }
146 >
147 >    /**
148 >     * setThreadFactory sets the thread factory returned by getThreadFactory
149 >     */
150 >    public void testSetThreadFactory() {
151 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
152 >        ThreadFactory tf = new SimpleThreadFactory();
153 >        p.setThreadFactory(tf);
154 >        assertSame(tf, p.getThreadFactory());
155 >        joinPool(p);
156      }
157  
158 <
158 >
159      /**
160 <     *  Test to verify that execute successfully executes a runnable
160 >     * setThreadFactory(null) throws NPE
161       */
162 <    public void testExecute(){
163 <        ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
162 >    public void testSetThreadFactoryNull() {
163 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
164          try {
165 <            one.execute(new Runnable(){
166 <                    public void run(){
167 <                        try{
58 <                            Thread.sleep(SHORT_DELAY_MS);
59 <                        }catch(InterruptedException e){
60 <                            fail("unexpected exception");
61 <                        }
62 <                    }
63 <                });
64 <            Thread.sleep(SHORT_DELAY_MS * 2);
65 <        }catch(InterruptedException e){
66 <            fail("unexpected exception");
165 >            p.setThreadFactory(null);
166 >            shouldThrow();
167 >        } catch (NullPointerException success) {
168          } finally {
169 <            one.shutdown();
169 >            joinPool(p);
170          }
171      }
172  
173      /**
174 <     *  Test to verify getActiveCount gives correct values
174 >     * getRejectedExecutionHandler returns handler in constructor if not set
175 >     */
176 >    public void testGetRejectedExecutionHandler() {
177 >        RejectedExecutionHandler h = new NoOpREHandler();
178 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
179 >        assertSame(h, p.getRejectedExecutionHandler());
180 >        joinPool(p);
181 >    }
182 >
183 >    /**
184 >     * setRejectedExecutionHandler sets the handler returned by
185 >     * getRejectedExecutionHandler
186 >     */
187 >    public void testSetRejectedExecutionHandler() {
188 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
189 >        RejectedExecutionHandler h = new NoOpREHandler();
190 >        p.setRejectedExecutionHandler(h);
191 >        assertSame(h, p.getRejectedExecutionHandler());
192 >        joinPool(p);
193 >    }
194 >
195 >
196 >    /**
197 >     * setRejectedExecutionHandler(null) throws NPE
198       */
199 <    public void testGetActiveCount(){
200 <        ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
199 >    public void testSetRejectedExecutionHandlerNull() {
200 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
201          try {
202 <            assertEquals(0, two.getActiveCount());
203 <            two.execute(newRunnable());
204 <            try{Thread.sleep(10);}catch(Exception e){}
81 <            assertEquals(1, two.getActiveCount());
202 >            p.setRejectedExecutionHandler(null);
203 >            shouldThrow();
204 >        } catch (NullPointerException success) {
205          } finally {
206 <            two.shutdown();
206 >            joinPool(p);
207          }
208      }
209 <    
209 >
210 >
211 >    /**
212 >     *   getLargestPoolSize increases, but doesn't overestimate, when
213 >     *   multiple threads active
214 >     */
215 >    public void testGetLargestPoolSize() throws InterruptedException {
216 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
217 >        assertEquals(0, p2.getLargestPoolSize());
218 >        p2.execute(new MediumRunnable());
219 >        p2.execute(new MediumRunnable());
220 >        Thread.sleep(SHORT_DELAY_MS);
221 >        assertEquals(2, p2.getLargestPoolSize());
222 >        joinPool(p2);
223 >    }
224 >
225 >    /**
226 >     *   getMaximumPoolSize returns value given in constructor if not
227 >     *   otherwise set
228 >     */
229 >    public void testGetMaximumPoolSize() {
230 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
231 >        assertEquals(2, p2.getMaximumPoolSize());
232 >        joinPool(p2);
233 >    }
234 >
235 >    /**
236 >     *   getPoolSize increases, but doesn't overestimate, when threads
237 >     *   become active
238 >     */
239 >    public void testGetPoolSize() {
240 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
241 >        assertEquals(0, p1.getPoolSize());
242 >        p1.execute(new MediumRunnable());
243 >        assertEquals(1, p1.getPoolSize());
244 >        joinPool(p1);
245 >    }
246 >
247 >    /**
248 >     *  getTaskCount increases, but doesn't overestimate, when tasks submitted
249 >     */
250 >    public void testGetTaskCount() throws InterruptedException {
251 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
252 >        assertEquals(0, p1.getTaskCount());
253 >        p1.execute(new MediumRunnable());
254 >        Thread.sleep(SHORT_DELAY_MS);
255 >        assertEquals(1, p1.getTaskCount());
256 >        joinPool(p1);
257 >    }
258 >
259 >    /**
260 >     *   isShutDown is false before shutdown, true after
261 >     */
262 >    public void testIsShutdown() {
263 >
264 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
265 >        assertFalse(p1.isShutdown());
266 >        try { p1.shutdown(); } catch (SecurityException ok) { return; }
267 >        assertTrue(p1.isShutdown());
268 >        joinPool(p1);
269 >    }
270 >
271 >
272      /**
273 <     *  Test to verify getCompleteTaskCount gives correct values
273 >     *  isTerminated is false before termination, true after
274       */
275 <    public void testGetCompletedTaskCount(){
276 <        ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
275 >    public void testIsTerminated() throws InterruptedException {
276 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
277 >        assertFalse(p1.isTerminated());
278          try {
279 <            assertEquals(0, two.getCompletedTaskCount());
94 <            two.execute(newRunnable());
95 <            try{Thread.sleep(2000);}catch(Exception e){}
96 <            assertEquals(1, two.getCompletedTaskCount());
279 >            p1.execute(new MediumRunnable());
280          } finally {
281 <            two.shutdown();
281 >            try { p1.shutdown(); } catch (SecurityException ok) { return; }
282          }
283 +        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
284 +        assertTrue(p1.isTerminated());
285      }
286 <    
286 >
287      /**
288 <     *  Test to verify getCorePoolSize gives correct values
288 >     *  isTerminating is not true when running or when terminated
289       */
290 <    public void testGetCorePoolSize(){
291 <        ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
290 >    public void testIsTerminating() throws InterruptedException {
291 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
292 >        assertFalse(p1.isTerminating());
293          try {
294 <            assertEquals(1, one.getCorePoolSize());
294 >            p1.execute(new SmallRunnable());
295 >            assertFalse(p1.isTerminating());
296          } finally {
297 <            one.shutdown();
297 >            try { p1.shutdown(); } catch (SecurityException ok) { return; }
298          }
299 +        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
300 +        assertTrue(p1.isTerminated());
301 +        assertFalse(p1.isTerminating());
302      }
303 <    
303 >
304      /**
305 <     *  Test to verify getKeepAliveTime gives correct values
305 >     * getQueue returns the work queue, which contains queued tasks
306       */
307 <    public void testGetKeepAliveTime(){
308 <        ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
307 >    public void testGetQueue() throws InterruptedException {
308 >        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
309 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
310 >        FutureTask[] tasks = new FutureTask[5];
311 >        for (int i = 0; i < 5; i++) {
312 >            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
313 >            p1.execute(tasks[i]);
314 >        }
315          try {
316 <            assertEquals(1, two.getKeepAliveTime(TimeUnit.SECONDS));
316 >            Thread.sleep(SHORT_DELAY_MS);
317 >            BlockingQueue<Runnable> wq = p1.getQueue();
318 >            assertSame(q, wq);
319 >            assertFalse(wq.contains(tasks[0]));
320 >            assertTrue(wq.contains(tasks[4]));
321 >            for (int i = 1; i < 5; ++i)
322 >                tasks[i].cancel(true);
323 >            p1.shutdownNow();
324          } finally {
325 <            two.shutdown();
325 >            joinPool(p1);
326          }
327      }
328 <    
328 >
329      /**
330 <     *  Test to verify getLargestPoolSize gives correct values
330 >     * remove(task) removes queued task, and fails to remove active task
331       */
332 <    public void testGetLargestPoolSize(){
333 <        ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
332 >    public void testRemove() throws InterruptedException {
333 >        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
334 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
335 >        FutureTask[] tasks = new FutureTask[5];
336 >        for (int i = 0; i < 5; i++) {
337 >            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
338 >            p1.execute(tasks[i]);
339 >        }
340          try {
341 <            assertEquals(0, two.getLargestPoolSize());
342 <            two.execute(newRunnable());
343 <            two.execute(newRunnable());
344 <            try{Thread.sleep(SHORT_DELAY_MS);} catch(Exception e){}
345 <            assertEquals(2, two.getLargestPoolSize());
341 >            Thread.sleep(SHORT_DELAY_MS);
342 >            assertFalse(p1.remove(tasks[0]));
343 >            assertTrue(q.contains(tasks[4]));
344 >            assertTrue(q.contains(tasks[3]));
345 >            assertTrue(p1.remove(tasks[4]));
346 >            assertFalse(p1.remove(tasks[4]));
347 >            assertFalse(q.contains(tasks[4]));
348 >            assertTrue(q.contains(tasks[3]));
349 >            assertTrue(p1.remove(tasks[3]));
350 >            assertFalse(q.contains(tasks[3]));
351          } finally {
352 <            two.shutdown();
352 >            joinPool(p1);
353 >        }
354 >    }
355 >
356 >    /**
357 >     *   purge removes cancelled tasks from the queue
358 >     */
359 >    public void testPurge() {
360 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
361 >        FutureTask[] tasks = new FutureTask[5];
362 >        for (int i = 0; i < 5; i++) {
363 >            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
364 >            p1.execute(tasks[i]);
365 >        }
366 >        tasks[4].cancel(true);
367 >        tasks[3].cancel(true);
368 >        p1.purge();
369 >        long count = p1.getTaskCount();
370 >        assertTrue(count >= 2 && count < 5);
371 >        joinPool(p1);
372 >    }
373 >
374 >    /**
375 >     *  shutDownNow returns a list containing tasks that were not run
376 >     */
377 >    public void testShutDownNow() {
378 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
379 >        List l;
380 >        try {
381 >            for (int i = 0; i < 5; i++)
382 >                p1.execute(new MediumPossiblyInterruptedRunnable());
383 >        }
384 >        finally {
385 >            try {
386 >                l = p1.shutdownNow();
387 >            } catch (SecurityException ok) { return; }
388 >
389          }
390 +        assertTrue(p1.isShutdown());
391 +        assertTrue(l.size() <= 4);
392 +    }
393 +
394 +    // Exception Tests
395 +
396 +
397 +    /**
398 +     * Constructor throws if corePoolSize argument is less than zero
399 +     */
400 +    public void testConstructor1() {
401 +        try {
402 +            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
403 +            shouldThrow();
404 +        } catch (IllegalArgumentException success) {}
405 +    }
406 +
407 +    /**
408 +     * Constructor throws if maximumPoolSize is less than zero
409 +     */
410 +    public void testConstructor2() {
411 +        try {
412 +            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
413 +            shouldThrow();
414 +        } catch (IllegalArgumentException success) {}
415 +    }
416 +
417 +    /**
418 +     * Constructor throws if maximumPoolSize is equal to zero
419 +     */
420 +    public void testConstructor3() {
421 +        try {
422 +            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
423 +            shouldThrow();
424 +        } catch (IllegalArgumentException success) {}
425 +    }
426 +
427 +    /**
428 +     * Constructor throws if keepAliveTime is less than zero
429 +     */
430 +    public void testConstructor4() {
431 +        try {
432 +            new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
433 +            shouldThrow();
434 +        } catch (IllegalArgumentException success) {}
435 +    }
436 +
437 +    /**
438 +     * Constructor throws if corePoolSize is greater than the maximumPoolSize
439 +     */
440 +    public void testConstructor5() {
441 +        try {
442 +            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
443 +            shouldThrow();
444 +        } catch (IllegalArgumentException success) {}
445 +    }
446 +
447 +    /**
448 +     * Constructor throws if workQueue is set to null
449 +     */
450 +    public void testConstructorNullPointerException() {
451 +        try {
452 +            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null);
453 +            shouldThrow();
454 +        } catch (NullPointerException success) {}
455 +    }
456 +
457 +
458 +
459 +    /**
460 +     * Constructor throws if corePoolSize argument is less than zero
461 +     */
462 +    public void testConstructor6() {
463 +        try {
464 +            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
465 +            shouldThrow();
466 +        } catch (IllegalArgumentException success) {}
467 +    }
468 +
469 +    /**
470 +     * Constructor throws if maximumPoolSize is less than zero
471 +     */
472 +    public void testConstructor7() {
473 +        try {
474 +            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
475 +            shouldThrow();
476 +        } catch (IllegalArgumentException success) {}
477 +    }
478 +
479 +    /**
480 +     * Constructor throws if maximumPoolSize is equal to zero
481 +     */
482 +    public void testConstructor8() {
483 +        try {
484 +            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
485 +            shouldThrow();
486 +        } catch (IllegalArgumentException success) {}
487 +    }
488 +
489 +    /**
490 +     * Constructor throws if keepAliveTime is less than zero
491 +     */
492 +    public void testConstructor9() {
493 +        try {
494 +            new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
495 +            shouldThrow();
496 +        } catch (IllegalArgumentException success) {}
497 +    }
498 +
499 +    /**
500 +     * Constructor throws if corePoolSize is greater than the maximumPoolSize
501 +     */
502 +    public void testConstructor10() {
503 +        try {
504 +            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
505 +            shouldThrow();
506 +        } catch (IllegalArgumentException success) {}
507 +    }
508 +
509 +    /**
510 +     * Constructor throws if workQueue is set to null
511 +     */
512 +    public void testConstructorNullPointerException2() {
513 +        try {
514 +            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
515 +            shouldThrow();
516 +        } catch (NullPointerException success) {}
517 +    }
518 +
519 +    /**
520 +     * Constructor throws if threadFactory is set to null
521 +     */
522 +    public void testConstructorNullPointerException3() {
523 +        try {
524 +            ThreadFactory f = null;
525 +            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
526 +            shouldThrow();
527 +        } catch (NullPointerException success) {}
528 +    }
529 +
530 +
531 +    /**
532 +     * Constructor throws if corePoolSize argument is less than zero
533 +     */
534 +    public void testConstructor11() {
535 +        try {
536 +            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
537 +            shouldThrow();
538 +        } catch (IllegalArgumentException success) {}
539 +    }
540 +
541 +    /**
542 +     * Constructor throws if maximumPoolSize is less than zero
543 +     */
544 +    public void testConstructor12() {
545 +        try {
546 +            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
547 +            shouldThrow();
548 +        } catch (IllegalArgumentException success) {}
549 +    }
550 +
551 +    /**
552 +     * Constructor throws if maximumPoolSize is equal to zero
553 +     */
554 +    public void testConstructor13() {
555 +        try {
556 +            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
557 +            shouldThrow();
558 +        } catch (IllegalArgumentException success) {}
559 +    }
560 +
561 +    /**
562 +     * Constructor throws if keepAliveTime is less than zero
563 +     */
564 +    public void testConstructor14() {
565 +        try {
566 +            new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
567 +            shouldThrow();
568 +        } catch (IllegalArgumentException success) {}
569 +    }
570 +
571 +    /**
572 +     * Constructor throws if corePoolSize is greater than the maximumPoolSize
573 +     */
574 +    public void testConstructor15() {
575 +        try {
576 +            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
577 +            shouldThrow();
578 +        } catch (IllegalArgumentException success) {}
579 +    }
580 +
581 +    /**
582 +     * Constructor throws if workQueue is set to null
583 +     */
584 +    public void testConstructorNullPointerException4() {
585 +        try {
586 +            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
587 +            shouldThrow();
588 +        } catch (NullPointerException success) {}
589 +    }
590 +
591 +    /**
592 +     * Constructor throws if handler is set to null
593 +     */
594 +    public void testConstructorNullPointerException5() {
595 +        try {
596 +            RejectedExecutionHandler r = null;
597 +            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
598 +            shouldThrow();
599 +        } catch (NullPointerException success) {}
600 +    }
601 +
602 +
603 +    /**
604 +     * Constructor throws if corePoolSize argument is less than zero
605 +     */
606 +    public void testConstructor16() {
607 +        try {
608 +            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
609 +            shouldThrow();
610 +        } catch (IllegalArgumentException success) {}
611 +    }
612 +
613 +    /**
614 +     * Constructor throws if maximumPoolSize is less than zero
615 +     */
616 +    public void testConstructor17() {
617 +        try {
618 +            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
619 +            shouldThrow();
620 +        } catch (IllegalArgumentException success) {}
621 +    }
622 +
623 +    /**
624 +     * Constructor throws if maximumPoolSize is equal to zero
625 +     */
626 +    public void testConstructor18() {
627 +        try {
628 +            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
629 +            shouldThrow();
630 +        } catch (IllegalArgumentException success) {}
631 +    }
632 +
633 +    /**
634 +     * Constructor throws if keepAliveTime is less than zero
635 +     */
636 +    public void testConstructor19() {
637 +        try {
638 +            new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
639 +            shouldThrow();
640 +        } catch (IllegalArgumentException success) {}
641      }
642 <    
642 >
643      /**
644 <     *  Test to verify getMaximumPoolSize gives correct values
644 >     * Constructor throws if corePoolSize is greater than the maximumPoolSize
645       */
646 <    public void testGetMaximumPoolSize(){
146 <        ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
646 >    public void testConstructor20() {
647          try {
648 <            assertEquals(2, two.getMaximumPoolSize());
648 >            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
649 >            shouldThrow();
650 >        } catch (IllegalArgumentException success) {}
651 >    }
652 >
653 >    /**
654 >     * Constructor throws if workQueue is set to null
655 >     */
656 >    public void testConstructorNullPointerException6() {
657 >        try {
658 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
659 >            shouldThrow();
660 >        } catch (NullPointerException success) {}
661 >    }
662 >
663 >    /**
664 >     * Constructor throws if handler is set to null
665 >     */
666 >    public void testConstructorNullPointerException7() {
667 >        try {
668 >            RejectedExecutionHandler r = null;
669 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
670 >            shouldThrow();
671 >        } catch (NullPointerException success) {}
672 >    }
673 >
674 >    /**
675 >     * Constructor throws if ThreadFactory is set top null
676 >     */
677 >    public void testConstructorNullPointerException8() {
678 >        try {
679 >            ThreadFactory f = null;
680 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
681 >            shouldThrow();
682 >        } catch (NullPointerException success) {}
683 >    }
684 >
685 >
686 >    /**
687 >     *  execute throws RejectedExecutionException
688 >     *  if saturated.
689 >     */
690 >    public void testSaturatedExecute() {
691 >        ThreadPoolExecutor p =
692 >            new ThreadPoolExecutor(1, 1,
693 >                                   LONG_DELAY_MS, MILLISECONDS,
694 >                                   new ArrayBlockingQueue<Runnable>(1));
695 >        try {
696 >            for (int i = 0; i < 2; ++i)
697 >                p.execute(new MediumRunnable());
698 >            for (int i = 0; i < 2; ++i) {
699 >                try {
700 >                    p.execute(new MediumRunnable());
701 >                    shouldThrow();
702 >                } catch (RejectedExecutionException success) {}
703 >            }
704          } finally {
705 <            two.shutdown();
705 >            joinPool(p);
706          }
707      }
708 <    
708 >
709      /**
710 <     *  Test to verify getPoolSize gives correct values
710 >     *  executor using CallerRunsPolicy runs task if saturated.
711       */
712 <    public void testGetPoolSize(){
713 <        ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
714 <        try {
715 <            assertEquals(0, one.getPoolSize());
716 <            one.execute(newRunnable());
717 <            assertEquals(1, one.getPoolSize());
712 >    public void testSaturatedExecute2() {
713 >        RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
714 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
715 >        try {
716 >
717 >            TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
718 >            for (int i = 0; i < 5; ++i) {
719 >                tasks[i] = new TrackedNoOpRunnable();
720 >            }
721 >            TrackedLongRunnable mr = new TrackedLongRunnable();
722 >            p.execute(mr);
723 >            for (int i = 0; i < 5; ++i) {
724 >                p.execute(tasks[i]);
725 >            }
726 >            for (int i = 1; i < 5; ++i) {
727 >                assertTrue(tasks[i].done);
728 >            }
729 >            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
730          } finally {
731 <            one.shutdown();
731 >            joinPool(p);
732          }
733      }
734 <    
734 >
735      /**
736 <     *  Test to verify getTaskCount gives correct values
736 >     *  executor using DiscardPolicy drops task if saturated.
737       */
738 <    public void testGetTaskCount(){
739 <        ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
738 >    public void testSaturatedExecute3() {
739 >        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
740 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
741          try {
742 <            assertEquals(0, one.getTaskCount());
743 <            for(int i = 0; i < 5; i++)
744 <                one.execute(newRunnable());
745 <            try{Thread.sleep(SHORT_DELAY_MS);}catch(Exception e){}
746 <            assertEquals(5, one.getTaskCount());
742 >
743 >            TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
744 >            for (int i = 0; i < 5; ++i) {
745 >                tasks[i] = new TrackedNoOpRunnable();
746 >            }
747 >            p.execute(new TrackedLongRunnable());
748 >            for (int i = 0; i < 5; ++i) {
749 >                p.execute(tasks[i]);
750 >            }
751 >            for (int i = 0; i < 5; ++i) {
752 >                assertFalse(tasks[i].done);
753 >            }
754 >            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
755          } finally {
756 <            one.shutdown();
756 >            joinPool(p);
757          }
758      }
759 <    
759 >
760      /**
761 <     *  Test to verify isShutDown gives correct values
761 >     *  executor using DiscardOldestPolicy drops oldest task if saturated.
762       */
763 <    public void testIsShutdown(){
764 <        
765 <        ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
763 >    public void testSaturatedExecute4() {
764 >        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
765 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
766          try {
767 <            assertFalse(one.isShutdown());
767 >            p.execute(new TrackedLongRunnable());
768 >            TrackedLongRunnable r2 = new TrackedLongRunnable();
769 >            p.execute(r2);
770 >            assertTrue(p.getQueue().contains(r2));
771 >            TrackedNoOpRunnable r3 = new TrackedNoOpRunnable();
772 >            p.execute(r3);
773 >            assertFalse(p.getQueue().contains(r2));
774 >            assertTrue(p.getQueue().contains(r3));
775 >            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
776 >        } finally {
777 >            joinPool(p);
778          }
779 <        finally {
780 <            one.shutdown();
779 >    }
780 >
781 >    /**
782 >     *  execute throws RejectedExecutionException if shutdown
783 >     */
784 >    public void testRejectedExecutionExceptionOnShutdown() {
785 >        ThreadPoolExecutor tpe =
786 >            new ThreadPoolExecutor(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
787 >        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
788 >        try {
789 >            tpe.execute(new NoOpRunnable());
790 >            shouldThrow();
791 >        } catch (RejectedExecutionException success) {}
792 >
793 >        joinPool(tpe);
794 >    }
795 >
796 >    /**
797 >     *  execute using CallerRunsPolicy drops task on shutdown
798 >     */
799 >    public void testCallerRunsOnShutdown() {
800 >        RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
801 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
802 >
803 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
804 >        try {
805 >            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
806 >            p.execute(r);
807 >            assertFalse(r.done);
808 >        } finally {
809 >            joinPool(p);
810          }
196        assertTrue(one.isShutdown());
811      }
812  
199        
813      /**
814 <     *  Test to verify isTerminated gives correct values
202 <     *  Makes sure termination does not take an innapropriate
203 <     *  amount of time
814 >     *  execute using DiscardPolicy drops task on shutdown
815       */
816 <    public void testIsTerminated(){
817 <        ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
816 >    public void testDiscardOnShutdown() {
817 >        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
818 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
819 >
820 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
821          try {
822 <            one.execute(newRunnable());
822 >            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
823 >            p.execute(r);
824 >            assertFalse(r.done);
825          } finally {
826 <            one.shutdown();
826 >            joinPool(p);
827          }
212        boolean flag = false;
213        try{
214            flag = one.awaitTermination(10, TimeUnit.SECONDS);
215        }catch(Exception e){}  
216        assertTrue(one.isTerminated());
217        if(!flag)
218            fail("ThreadPoolExecutor - thread pool did not terminate within suitable timeframe");
828      }
829  
830 +
831      /**
832 <     *  Test to verify that purge correctly removes cancelled tasks
223 <     *  from the queue
832 >     *  execute using DiscardOldestPolicy drops task on shutdown
833       */
834 <    public void testPurge(){
835 <        ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
834 >    public void testDiscardOldestOnShutdown() {
835 >        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
836 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
837 >
838 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
839          try {
840 <            CancellableTask[] tasks = new CancellableTask[5];
841 <            for(int i = 0; i < 5; i++){
842 <                tasks[i] = new CancellableTask(newRunnable());
231 <                one.execute(tasks[i]);
232 <            }
233 <            tasks[4].cancel(true);
234 <            tasks[3].cancel(true);
235 <            one.purge();
236 <            long count = one.getTaskCount();
237 <            assertTrue(count >= 2 && count < 5);
840 >            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
841 >            p.execute(r);
842 >            assertFalse(r.done);
843          } finally {
844 <            one.shutdown();
844 >            joinPool(p);
845          }
846      }
847  
848 +
849      /**
850 <     *  Test to verify shutDownNow returns a list
245 <     *  containing the correct number of elements
850 >     *  execute (null) throws NPE
851       */
852 <    public void testShutDownNow(){
853 <        ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
854 <        List l;
852 >    public void testExecuteNull() {
853 >        ThreadPoolExecutor tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
854 >        try {
855 >            tpe.execute(null);
856 >            shouldThrow();
857 >        } catch (NullPointerException success) {}
858 >
859 >        joinPool(tpe);
860 >    }
861 >
862 >    /**
863 >     *  setCorePoolSize of negative value throws IllegalArgumentException
864 >     */
865 >    public void testCorePoolSizeIllegalArgumentException() {
866 >        ThreadPoolExecutor tpe =
867 >            new ThreadPoolExecutor(1, 2,
868 >                                   LONG_DELAY_MS, MILLISECONDS,
869 >                                   new ArrayBlockingQueue<Runnable>(10));
870          try {
871 <            for(int i = 0; i < 5; i++)
872 <                one.execute(newRunnable());
871 >            tpe.setCorePoolSize(-1);
872 >            shouldThrow();
873 >        } catch (IllegalArgumentException success) {
874 >        } finally {
875 >            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
876          }
877 <        finally {
878 <            l = one.shutdownNow();
877 >        joinPool(tpe);
878 >    }
879 >
880 >    /**
881 >     *  setMaximumPoolSize(int) throws IllegalArgumentException if
882 >     *  given a value less the core pool size
883 >     */
884 >    public void testMaximumPoolSizeIllegalArgumentException() {
885 >        ThreadPoolExecutor tpe =
886 >            new ThreadPoolExecutor(2, 3,
887 >                                   LONG_DELAY_MS, MILLISECONDS,
888 >                                   new ArrayBlockingQueue<Runnable>(10));
889 >        try {
890 >            tpe.setMaximumPoolSize(1);
891 >            shouldThrow();
892 >        } catch (IllegalArgumentException success) {
893 >        } finally {
894 >            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
895          }
896 <        assertTrue(one.isShutdown());
258 <        assertTrue(l.size() <= 4);
896 >        joinPool(tpe);
897      }
898  
899 <    
900 <
901 <    
902 <    
903 <      
904 <    // Exception Tests
905 <    
899 >    /**
900 >     *  setMaximumPoolSize throws IllegalArgumentException
901 >     *  if given a negative value
902 >     */
903 >    public void testMaximumPoolSizeIllegalArgumentException2() {
904 >        ThreadPoolExecutor tpe =
905 >            new ThreadPoolExecutor(2, 3,
906 >                                   LONG_DELAY_MS, MILLISECONDS,
907 >                                   new ArrayBlockingQueue<Runnable>(10));
908 >        try {
909 >            tpe.setMaximumPoolSize(-1);
910 >            shouldThrow();
911 >        } catch (IllegalArgumentException success) {
912 >        } finally {
913 >            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
914 >        }
915 >        joinPool(tpe);
916 >    }
917  
918 <    //---- Tests if corePoolSize argument is less than zero
919 <    public void testConstructor1() {
920 <        try{
921 <            new ThreadPoolExecutor(-1,1,100L,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
922 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
918 >
919 >    /**
920 >     *  setKeepAliveTime  throws IllegalArgumentException
921 >     *  when given a negative value
922 >     */
923 >    public void testKeepAliveTimeIllegalArgumentException() {
924 >        ThreadPoolExecutor tpe =
925 >            new ThreadPoolExecutor(2, 3,
926 >                                   LONG_DELAY_MS, MILLISECONDS,
927 >                                   new ArrayBlockingQueue<Runnable>(10));
928 >        try {
929 >            tpe.setKeepAliveTime(-1,MILLISECONDS);
930 >            shouldThrow();
931 >        } catch (IllegalArgumentException success) {
932 >        } finally {
933 >            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
934          }
935 <        catch (IllegalArgumentException i){}
935 >        joinPool(tpe);
936      }
937 <    
938 <    //---- Tests if maximumPoolSize is less than zero
939 <    public void testConstructor2() {
940 <        try{
941 <            new ThreadPoolExecutor(1,-1,100L,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
942 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
937 >
938 >    /**
939 >     * terminated() is called on termination
940 >     */
941 >    public void testTerminated() {
942 >        ExtendedTPE tpe = new ExtendedTPE();
943 >        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
944 >        assertTrue(tpe.terminatedCalled);
945 >        joinPool(tpe);
946 >    }
947 >
948 >    /**
949 >     * beforeExecute and afterExecute are called when executing task
950 >     */
951 >    public void testBeforeAfter() throws InterruptedException {
952 >        ExtendedTPE tpe = new ExtendedTPE();
953 >        try {
954 >            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
955 >            tpe.execute(r);
956 >            Thread.sleep(SHORT_DELAY_MS);
957 >            assertTrue(r.done);
958 >            assertTrue(tpe.beforeCalled);
959 >            assertTrue(tpe.afterCalled);
960 >            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
961 >        } finally {
962 >            joinPool(tpe);
963          }
284        catch (IllegalArgumentException i2){}
964      }
965 <    
966 <    //---- Tests if maximumPoolSize is equal to zero
967 <    public void testConstructor3() {
968 <        try{
969 <            new ThreadPoolExecutor(1,0,100L,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
970 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
965 >
966 >    /**
967 >     * completed submit of callable returns result
968 >     */
969 >    public void testSubmitCallable() throws Exception {
970 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
971 >        try {
972 >            Future<String> future = e.submit(new StringTask());
973 >            String result = future.get();
974 >            assertSame(TEST_STRING, result);
975 >        } finally {
976 >            joinPool(e);
977          }
293        catch (IllegalArgumentException i3){}
978      }
979  
980 <    //---- Tests if keepAliveTime is less than zero
981 <    public void testConstructor4() {
982 <        try{
983 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
984 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
980 >    /**
981 >     * completed submit of runnable returns successfully
982 >     */
983 >    public void testSubmitRunnable() throws Exception {
984 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
985 >        try {
986 >            Future<?> future = e.submit(new NoOpRunnable());
987 >            future.get();
988 >            assertTrue(future.isDone());
989 >        } finally {
990 >            joinPool(e);
991          }
302        catch (IllegalArgumentException i4){}
992      }
993  
994 <    //---- Tests if corePoolSize is greater than the maximumPoolSize
995 <    public void testConstructor5() {
996 <        try{
997 <            new ThreadPoolExecutor(2,1,100L,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
998 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
994 >    /**
995 >     * completed submit of (runnable, result) returns result
996 >     */
997 >    public void testSubmitRunnable2() throws Exception {
998 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
999 >        try {
1000 >            Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1001 >            String result = future.get();
1002 >            assertSame(TEST_STRING, result);
1003 >        } finally {
1004 >            joinPool(e);
1005          }
311        catch (IllegalArgumentException i5){}
1006      }
1007 <        
1008 <    //---- Tests if workQueue is set to null
1009 <    public void testNullPointerException() {
1010 <        try{
1011 <            new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,null);
1012 <            fail("ThreadPoolExecutor constructor should throw a NullPointerException");        
1007 >
1008 >
1009 >    /**
1010 >     * invokeAny(null) throws NPE
1011 >     */
1012 >    public void testInvokeAny1() throws Exception {
1013 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1014 >        try {
1015 >            e.invokeAny(null);
1016 >            shouldThrow();
1017 >        } catch (NullPointerException success) {
1018 >        } finally {
1019 >            joinPool(e);
1020          }
320        catch (NullPointerException n){}  
1021      }
322    
1022  
1023 <    
1024 <    //---- Tests if corePoolSize argument is less than zero
1025 <    public void testConstructor6() {
1026 <        try{
1027 <            new ThreadPoolExecutor(-1,1,100L,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread());
1028 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
1029 <        }catch (IllegalArgumentException i6){}
1023 >    /**
1024 >     * invokeAny(empty collection) throws IAE
1025 >     */
1026 >    public void testInvokeAny2() throws Exception {
1027 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1028 >        try {
1029 >            e.invokeAny(new ArrayList<Callable<String>>());
1030 >            shouldThrow();
1031 >        } catch (IllegalArgumentException success) {
1032 >        } finally {
1033 >            joinPool(e);
1034 >        }
1035      }
1036 <    
1037 <    //---- Tests if maximumPoolSize is less than zero
1038 <    public void testConstructor7() {
1039 <        try{
1040 <            new ThreadPoolExecutor(1,-1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread());
1041 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
1036 >
1037 >    /**
1038 >     * invokeAny(c) throws NPE if c has null elements
1039 >     */
1040 >    public void testInvokeAny3() throws Exception {
1041 >        final CountDownLatch latch = new CountDownLatch(1);
1042 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1043 >        try {
1044 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1045 >            l.add(new Callable<String>() {
1046 >                      public String call() {
1047 >                          try {
1048 >                              latch.await();
1049 >                          } catch (InterruptedException ok) {}
1050 >                          return TEST_STRING;
1051 >                      }});
1052 >            l.add(null);
1053 >            e.invokeAny(l);
1054 >            shouldThrow();
1055 >        } catch (NullPointerException success) {
1056 >        } finally {
1057 >            latch.countDown();
1058 >            joinPool(e);
1059          }
339        catch (IllegalArgumentException i7){}
1060      }
1061  
1062 <    //---- Tests if maximumPoolSize is equal to zero
1063 <    public void testConstructor8() {
1064 <        try{
1065 <            new ThreadPoolExecutor(1,0,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread());
1066 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
1062 >    /**
1063 >     * invokeAny(c) throws ExecutionException if no task completes
1064 >     */
1065 >    public void testInvokeAny4() throws Exception {
1066 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1067 >        try {
1068 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1069 >            l.add(new NPETask());
1070 >            e.invokeAny(l);
1071 >            shouldThrow();
1072 >        } catch (ExecutionException success) {
1073 >            assertTrue(success.getCause() instanceof NullPointerException);
1074 >        } finally {
1075 >            joinPool(e);
1076          }
348        catch (IllegalArgumentException i8){}
1077      }
1078  
1079 <    //---- Tests if keepAliveTime is less than zero
1080 <    public void testConstructor9() {
1081 <        try{
1082 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread());
1083 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
1079 >    /**
1080 >     * invokeAny(c) returns result of some task
1081 >     */
1082 >    public void testInvokeAny5() throws Exception {
1083 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1084 >        try {
1085 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1086 >            l.add(new StringTask());
1087 >            l.add(new StringTask());
1088 >            String result = e.invokeAny(l);
1089 >            assertSame(TEST_STRING, result);
1090 >        } finally {
1091 >            joinPool(e);
1092          }
357        catch (IllegalArgumentException i9){}
1093      }
1094  
1095 <    //---- Tests if corePoolSize is greater than the maximumPoolSize
1096 <    public void testConstructor10() {
1097 <        try{
1098 <            new ThreadPoolExecutor(2,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread());
1099 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
1095 >    /**
1096 >     * invokeAll(null) throws NPE
1097 >     */
1098 >    public void testInvokeAll1() throws Exception {
1099 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1100 >        try {
1101 >            e.invokeAll(null);
1102 >            shouldThrow();
1103 >        } catch (NullPointerException success) {
1104 >        } finally {
1105 >            joinPool(e);
1106          }
366        catch (IllegalArgumentException i10){}
1107      }
1108  
1109 <    //---- Tests if workQueue is set to null
1110 <    public void testNullPointerException2() {
1111 <        try{
1112 <            new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,null,new testThread());
1113 <            fail("ThreadPoolExecutor constructor should throw a NullPointerException");        
1109 >    /**
1110 >     * invokeAll(empty collection) returns empty collection
1111 >     */
1112 >    public void testInvokeAll2() throws InterruptedException {
1113 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1114 >        try {
1115 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1116 >            assertTrue(r.isEmpty());
1117 >        } finally {
1118 >            joinPool(e);
1119          }
375        catch (NullPointerException n2){}  
1120      }
1121  
1122 <    //---- Tests if threadFactory is set to null
1123 <    public void testNullPointerException3() {
1124 <        try{
1125 <            ThreadFactory f = null;
1126 <            new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
1127 <            fail("ThreadPoolExecutor constructor should throw a NullPointerException");        
1122 >    /**
1123 >     * invokeAll(c) throws NPE if c has null elements
1124 >     */
1125 >    public void testInvokeAll3() throws Exception {
1126 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1127 >        try {
1128 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1129 >            l.add(new StringTask());
1130 >            l.add(null);
1131 >            e.invokeAll(l);
1132 >            shouldThrow();
1133 >        } catch (NullPointerException success) {
1134 >        } finally {
1135 >            joinPool(e);
1136          }
385        catch (NullPointerException n3){}  
1137      }
1138 <
1139 <    
1140 <    //---- Tests if corePoolSize argument is less than zero
1141 <    public void testConstructor11() {
1142 <        try{
1143 <            new ThreadPoolExecutor(-1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testReject());
1144 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
1138 >
1139 >    /**
1140 >     * get of element of invokeAll(c) throws exception on failed task
1141 >     */
1142 >    public void testInvokeAll4() throws Exception {
1143 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1144 >        try {
1145 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1146 >            l.add(new NPETask());
1147 >            List<Future<String>> result = e.invokeAll(l);
1148 >            assertEquals(1, result.size());
1149 >            for (Future<String> future : result) {
1150 >                try {
1151 >                    future.get();
1152 >                    shouldThrow();
1153 >                } catch (ExecutionException success) {
1154 >                    Throwable cause = success.getCause();
1155 >                    assertTrue(cause instanceof NullPointerException);
1156 >                }
1157 >            }
1158 >        } finally {
1159 >            joinPool(e);
1160          }
395        catch (IllegalArgumentException i11){}
1161      }
1162  
1163 <    //---- Tests if maximumPoolSize is less than zero
1164 <    public void testConstructor12() {
1165 <        try{
1166 <            new ThreadPoolExecutor(1,-1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testReject());
1167 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
1163 >    /**
1164 >     * invokeAll(c) returns results of all completed tasks
1165 >     */
1166 >    public void testInvokeAll5() throws Exception {
1167 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1168 >        try {
1169 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1170 >            l.add(new StringTask());
1171 >            l.add(new StringTask());
1172 >            List<Future<String>> result = e.invokeAll(l);
1173 >            assertEquals(2, result.size());
1174 >            for (Future<String> future : result)
1175 >                assertSame(TEST_STRING, future.get());
1176 >        } finally {
1177 >            joinPool(e);
1178          }
404        catch (IllegalArgumentException i12){}
1179      }
1180  
1181 <    //---- Tests if maximumPoolSize is equal to zero
1182 <    public void testConstructor13() {
1183 <        try{
1184 <            new ThreadPoolExecutor(1,0,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testReject());
1185 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
1181 >
1182 >
1183 >    /**
1184 >     * timed invokeAny(null) throws NPE
1185 >     */
1186 >    public void testTimedInvokeAny1() throws Exception {
1187 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1188 >        try {
1189 >            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1190 >            shouldThrow();
1191 >        } catch (NullPointerException success) {
1192 >        } finally {
1193 >            joinPool(e);
1194          }
413        catch (IllegalArgumentException i13){}
1195      }
1196  
1197 <    //---- Tests if keepAliveTime is less than zero
1198 <    public void testConstructor14() {
1199 <        try{
1200 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testReject());
1201 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
1197 >    /**
1198 >     * timed invokeAny(,,null) throws NPE
1199 >     */
1200 >    public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1201 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1202 >        try {
1203 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1204 >            l.add(new StringTask());
1205 >            e.invokeAny(l, MEDIUM_DELAY_MS, null);
1206 >            shouldThrow();
1207 >        } catch (NullPointerException success) {
1208 >        } finally {
1209 >            joinPool(e);
1210          }
422        catch (IllegalArgumentException i14){}
1211      }
1212  
1213 <    //---- Tests if corePoolSize is greater than the maximumPoolSize
1214 <    public void testConstructor15() {
1215 <        try{
1216 <            new ThreadPoolExecutor(2,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testReject());
1217 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
1213 >    /**
1214 >     * timed invokeAny(empty collection) throws IAE
1215 >     */
1216 >    public void testTimedInvokeAny2() throws Exception {
1217 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1218 >        try {
1219 >            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1220 >            shouldThrow();
1221 >        } catch (IllegalArgumentException success) {
1222 >        } finally {
1223 >            joinPool(e);
1224          }
431        catch (IllegalArgumentException i15){}
1225      }
1226  
1227 <    //---- Tests if workQueue is set to null
1228 <    public void testNullPointerException4() {
1229 <        try{
1230 <            new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,null,new testReject());
1231 <            fail("ThreadPoolExecutor constructor should throw a NullPointerException");        
1227 >    /**
1228 >     * timed invokeAny(c) throws NPE if c has null elements
1229 >     */
1230 >    public void testTimedInvokeAny3() throws Exception {
1231 >        final CountDownLatch latch = new CountDownLatch(1);
1232 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1233 >        try {
1234 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1235 >            l.add(new Callable<String>() {
1236 >                      public String call() {
1237 >                          try {
1238 >                              latch.await();
1239 >                          } catch (InterruptedException ok) {}
1240 >                          return TEST_STRING;
1241 >                      }});
1242 >            l.add(null);
1243 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1244 >            shouldThrow();
1245 >        } catch (NullPointerException success) {
1246 >        } finally {
1247 >            latch.countDown();
1248 >            joinPool(e);
1249          }
440        catch (NullPointerException n4){}  
1250      }
1251  
1252 <    //---- Tests if handler is set to null
1253 <    public void testNullPointerException5() {
1254 <        try{
1255 <            RejectedExecutionHandler r = null;
1256 <            new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
1257 <            fail("ThreadPoolExecutor constructor should throw a NullPointerException");        
1252 >    /**
1253 >     * timed invokeAny(c) throws ExecutionException if no task completes
1254 >     */
1255 >    public void testTimedInvokeAny4() throws Exception {
1256 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1257 >        try {
1258 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1259 >            l.add(new NPETask());
1260 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1261 >            shouldThrow();
1262 >        } catch (ExecutionException success) {
1263 >            assertTrue(success.getCause() instanceof NullPointerException);
1264 >        } finally {
1265 >            joinPool(e);
1266          }
450        catch (NullPointerException n5){}  
1267      }
1268  
1269 <    
1270 <    //---- Tests if corePoolSize argument is less than zero
1271 <    public void testConstructor16() {
1272 <        try{
1273 <            new ThreadPoolExecutor(-1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread(),new testReject());
1274 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
1269 >    /**
1270 >     * timed invokeAny(c) returns result of some task
1271 >     */
1272 >    public void testTimedInvokeAny5() throws Exception {
1273 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1274 >        try {
1275 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1276 >            l.add(new StringTask());
1277 >            l.add(new StringTask());
1278 >            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1279 >            assertSame(TEST_STRING, result);
1280 >        } finally {
1281 >            joinPool(e);
1282          }
460        catch (IllegalArgumentException i16){}
1283      }
1284  
1285 <    //---- Tests if maximumPoolSize is less than zero
1286 <    public void testConstructor17() {
1287 <        try{
1288 <            new ThreadPoolExecutor(1,-1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread(),new testReject());
1289 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
1285 >    /**
1286 >     * timed invokeAll(null) throws NPE
1287 >     */
1288 >    public void testTimedInvokeAll1() throws Exception {
1289 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1290 >        try {
1291 >            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1292 >            shouldThrow();
1293 >        } catch (NullPointerException success) {
1294 >        } finally {
1295 >            joinPool(e);
1296          }
469        catch (IllegalArgumentException i17){}
1297      }
1298  
1299 <    //---- Tests if maximumPoolSize is equal to zero
1300 <    public void testConstructor18() {
1301 <        try{
1302 <            new ThreadPoolExecutor(1,0,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread(),new testReject());
1303 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
1299 >    /**
1300 >     * timed invokeAll(,,null) throws NPE
1301 >     */
1302 >    public void testTimedInvokeAllNullTimeUnit() throws Exception {
1303 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1304 >        try {
1305 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1306 >            l.add(new StringTask());
1307 >            e.invokeAll(l, MEDIUM_DELAY_MS, null);
1308 >            shouldThrow();
1309 >        } catch (NullPointerException success) {
1310 >        } finally {
1311 >            joinPool(e);
1312          }
478        catch (IllegalArgumentException i18){}
1313      }
1314  
1315 <    //---- Tests if keepAliveTime is less than zero
1316 <    public void testConstructor19() {
1317 <        try{
1318 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread(),new testReject());
1319 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
1315 >    /**
1316 >     * timed invokeAll(empty collection) returns empty collection
1317 >     */
1318 >    public void testTimedInvokeAll2() throws InterruptedException {
1319 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1320 >        try {
1321 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1322 >            assertTrue(r.isEmpty());
1323 >        } finally {
1324 >            joinPool(e);
1325          }
487        catch (IllegalArgumentException i19){}
1326      }
1327  
1328 <    //---- Tests if corePoolSize is greater than the maximumPoolSize
1329 <    public void testConstructor20() {
1330 <        try{
1331 <            new ThreadPoolExecutor(2,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread(),new testReject());
1332 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
1328 >    /**
1329 >     * timed invokeAll(c) throws NPE if c has null elements
1330 >     */
1331 >    public void testTimedInvokeAll3() throws Exception {
1332 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1333 >        try {
1334 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1335 >            l.add(new StringTask());
1336 >            l.add(null);
1337 >            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1338 >            shouldThrow();
1339 >        } catch (NullPointerException success) {
1340 >        } finally {
1341 >            joinPool(e);
1342          }
496        catch (IllegalArgumentException i20){}
1343      }
1344  
1345 <    //---- Tests if workQueue is set to null
1346 <    public void testNullPointerException6() {
1347 <        try{
1348 <            new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,null,new testThread(),new testReject());
1349 <            fail("ThreadPoolExecutor constructor should throw a NullPointerException");        
1345 >    /**
1346 >     * get of element of invokeAll(c) throws exception on failed task
1347 >     */
1348 >    public void testTimedInvokeAll4() throws Exception {
1349 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1350 >        try {
1351 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1352 >            l.add(new NPETask());
1353 >            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1354 >            assertEquals(1, result.size());
1355 >            for (Future<String> future : result)
1356 >                future.get();
1357 >            shouldThrow();
1358 >        } catch (ExecutionException success) {
1359 >            assertTrue(success.getCause() instanceof NullPointerException);
1360 >        } finally {
1361 >            joinPool(e);
1362          }
505        catch (NullPointerException n6){}  
1363      }
1364  
1365 <    //---- Tests if handler is set to null
1366 <    public void testNullPointerException7() {
1367 <        try{
1368 <            RejectedExecutionHandler r = null;
1369 <            new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new testThread(),r);
1370 <            fail("ThreadPoolExecutor constructor should throw a NullPointerException");        
1365 >    /**
1366 >     * timed invokeAll(c) returns results of all completed tasks
1367 >     */
1368 >    public void testTimedInvokeAll5() throws Exception {
1369 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1370 >        try {
1371 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1372 >            l.add(new StringTask());
1373 >            l.add(new StringTask());
1374 >            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1375 >            assertEquals(2, result.size());
1376 >            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1377 >                assertSame(TEST_STRING, it.next().get());
1378 >        } finally {
1379 >            joinPool(e);
1380          }
515        catch (NullPointerException n7){}  
1381      }
1382  
1383 <    //---- Tests if ThradFactory is set top null
1384 <    public void testNullPointerException8() {
1385 <        try{
1386 <            ThreadFactory f = null;
1387 <            new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new testReject());
1388 <            fail("ThreadPoolExecutor constructor should throw a NullPointerException");        
1383 >    /**
1384 >     * timed invokeAll(c) cancels tasks not completed by timeout
1385 >     */
1386 >    public void testTimedInvokeAll6() throws Exception {
1387 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1388 >        try {
1389 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1390 >            l.add(new StringTask());
1391 >            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1392 >            l.add(new StringTask());
1393 >            List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1394 >            assertEquals(3, result.size());
1395 >            Iterator<Future<String>> it = result.iterator();
1396 >            Future<String> f1 = it.next();
1397 >            Future<String> f2 = it.next();
1398 >            Future<String> f3 = it.next();
1399 >            assertTrue(f1.isDone());
1400 >            assertTrue(f2.isDone());
1401 >            assertTrue(f3.isDone());
1402 >            assertFalse(f1.isCancelled());
1403 >            assertTrue(f2.isCancelled());
1404 >        } finally {
1405 >            joinPool(e);
1406          }
525        catch (NullPointerException n8){}  
1407      }
527    
1408  
1409      /**
1410 <     *  Test to verify execute will throw RejectedExcutionException
1411 <     *  ThreadPoolExecutor will throw one when more runnables are
1412 <     *  executed then will fit in the Queue.
1413 <     */
1414 <    public void testRejectedExecutedException(){
1415 <        ThreadPoolExecutor tpe = null;
1416 <        try{
1417 <            tpe = new ThreadPoolExecutor(1,1,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1418 <        }catch(Exception e){}
1419 <        tpe.shutdown();
1420 <        try{
541 <            tpe.execute(new Runnable(){
542 <                    public void run(){
543 <                        try{
544 <                            Thread.sleep(1000);
545 <                        }catch(InterruptedException e){}
546 <                    }
547 <                });
548 <            fail("ThreadPoolExecutor - void execute(Runnable) should throw RejectedExecutionException");
549 <        }catch(RejectedExecutionException sucess){}
550 <        
551 <        
552 <    }
553 <    
554 <    /**
555 <     *  Test to verify setCorePoolSize will throw IllegalArgumentException
556 <     *  when given a negative
557 <     */
558 <    public void testIllegalArgumentException1(){
559 <        ThreadPoolExecutor tpe = null;
560 <        try{
561 <            tpe = new ThreadPoolExecutor(1,2,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
562 <        }catch(Exception e){}
563 <        try{
564 <            tpe.setCorePoolSize(-1);
565 <            fail("ThreadPoolExecutor - void setCorePoolSize(int) should throw IllegalArgumentException");
566 <        }catch(IllegalArgumentException sucess){
567 <        } finally {
568 <            tpe.shutdown();
569 <        }
570 <    }  
571 <
572 <    
573 <    /**
574 <     *  Test to verify setMaximumPoolSize(int) will throw IllegalArgumentException
575 <     *  if given a value less the it's actual core pool size
576 <     */  
577 <    public void testIllegalArgumentException2(){
578 <        ThreadPoolExecutor tpe = null;
579 <        try{
580 <            tpe = new ThreadPoolExecutor(2,3,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
581 <        }catch(Exception e){}
582 <        try{
583 <            tpe.setMaximumPoolSize(1);
584 <            fail("ThreadPoolExecutor - void setMaximumPoolSize(int) should throw IllegalArgumentException");
585 <        }catch(IllegalArgumentException sucess){
1410 >     * Execution continues if there is at least one thread even if
1411 >     * thread factory fails to create more
1412 >     */
1413 >    public void testFailingThreadFactory() throws InterruptedException {
1414 >        ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1415 >        try {
1416 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1417 >            for (int k = 0; k < 100; ++k) {
1418 >                e.execute(new NoOpRunnable());
1419 >            }
1420 >            Thread.sleep(LONG_DELAY_MS);
1421          } finally {
1422 <            tpe.shutdown();
1422 >            joinPool(e);
1423          }
1424      }
1425 <    
1425 >
1426      /**
1427 <     *  Test to verify that setMaximumPoolSize will throw IllegalArgumentException
593 <     *  if given a negative number
1427 >     * allowsCoreThreadTimeOut is by default false.
1428       */
1429 <    public void testIllegalArgumentException2SP(){
1430 <        ThreadPoolExecutor tpe = null;
1431 <        try{
1432 <            tpe = new ThreadPoolExecutor(2,3,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1433 <        }catch(Exception e){}
1434 <        try{
1435 <            tpe.setMaximumPoolSize(-1);
1436 <            fail("ThreadPoolExecutor - void setMaximumPoolSize(int) should throw IllegalArgumentException");
1437 <        }catch(IllegalArgumentException sucess){
1429 >    public void testAllowsCoreThreadTimeOut() {
1430 >        ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1431 >        assertFalse(tpe.allowsCoreThreadTimeOut());
1432 >        joinPool(tpe);
1433 >    }
1434 >
1435 >    /**
1436 >     * allowCoreThreadTimeOut(true) causes idle threads to time out
1437 >     */
1438 >    public void testAllowCoreThreadTimeOut_true() throws InterruptedException {
1439 >        ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1440 >        tpe.allowCoreThreadTimeOut(true);
1441 >        tpe.execute(new NoOpRunnable());
1442 >        try {
1443 >            Thread.sleep(MEDIUM_DELAY_MS);
1444 >            assertEquals(0, tpe.getPoolSize());
1445          } finally {
1446 <            tpe.shutdown();
1446 >            joinPool(tpe);
1447          }
1448      }
608    
1449  
1450      /**
1451 <     *  Test to verify setKeepAliveTime will throw IllegalArgumentException
612 <     *  when given a negative value
1451 >     * allowCoreThreadTimeOut(false) causes idle threads not to time out
1452       */
1453 <    public void testIllegalArgumentException3(){
1454 <        ThreadPoolExecutor tpe = null;
1455 <        try{
1456 <            tpe = new ThreadPoolExecutor(2,3,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1457 <        }catch(Exception e){}
1458 <        
1459 <        try{
1460 <            tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS);
1461 <            fail("ThreadPoolExecutor - void setKeepAliveTime(long, TimeUnit) should throw IllegalArgumentException");
1462 <        }catch(IllegalArgumentException sucess){
1463 <        } finally {
1464 <            tpe.shutdown();
1465 <        }
1466 <    }
1467 <  
1468 <    
1469 <  
1453 >    public void testAllowCoreThreadTimeOut_false() throws InterruptedException {
1454 >        ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1455 >        tpe.allowCoreThreadTimeOut(false);
1456 >        tpe.execute(new NoOpRunnable());
1457 >        try {
1458 >            Thread.sleep(MEDIUM_DELAY_MS);
1459 >            assertTrue(tpe.getPoolSize() >= 1);
1460 >        } finally {
1461 >            joinPool(tpe);
1462 >        }
1463 >    }
1464 >
1465 >    /**
1466 >     * execute allows the same task to be submitted multiple times, even
1467 >     * if rejected
1468 >     */
1469 >    public void testRejectedRecycledTask() throws InterruptedException {
1470 >        final int nTasks = 1000;
1471 >        final AtomicInteger nRun = new AtomicInteger(0);
1472 >        final Runnable recycledTask = new Runnable() {
1473 >                public void run() {
1474 >                    nRun.getAndIncrement();
1475 >                } };
1476 >        final ThreadPoolExecutor p =
1477 >            new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
1478 >                                   new ArrayBlockingQueue(30));
1479 >        try {
1480 >            for (int i = 0; i < nTasks; ++i) {
1481 >                for (;;) {
1482 >                    try {
1483 >                        p.execute(recycledTask);
1484 >                        break;
1485 >                    }
1486 >                    catch (RejectedExecutionException ignore) {
1487 >                    }
1488 >                }
1489 >            }
1490 >            Thread.sleep(5000); // enough time to run all tasks
1491 >            assertEquals(nRun.get(), nTasks);
1492 >        } finally {
1493 >            p.shutdown();
1494 >        }
1495 >    }
1496 >
1497   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines