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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines