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.10 by dl, Sat Nov 1 18:37:02 2003 UTC

# Line 9 | Line 9 | import java.util.concurrent.*;
9   import junit.framework.*;
10   import java.util.List;
11  
12 < public class ThreadPoolExecutorTest extends TestCase{
12 > public class ThreadPoolExecutorTest extends JSR166TestCase {
13      public static void main(String[] args) {
14          junit.textui.TestRunner.run (suite());  
15      }
16
17
16      public static Test suite() {
17          return new TestSuite(ThreadPoolExecutorTest.class);
18      }
19      
20 <    private static long SHORT_DELAY_MS = 100;
21 <    private static long MEDIUM_DELAY_MS = 1000;
22 <    private static long LONG_DELAY_MS = 10000;
23 <
24 < //---- testThread class to implement ThreadFactory for use in constructors
25 <    static class testThread implements ThreadFactory{
26 <        public Thread newThread(Runnable r){
27 <            return new Thread(r);
28 <        }  
29 <    }
30 <
31 < //---- testReject class to implement RejectedExecutionHandler for use in the constructors
32 <    static class testReject implements RejectedExecutionHandler{
33 <        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){}
34 <    }
35 <
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 <            };
20 >    static class ExtendedTPE extends ThreadPoolExecutor {
21 >        volatile boolean beforeCalled = false;
22 >        volatile boolean afterCalled = false;
23 >        volatile boolean terminatedCalled = false;
24 >        public ExtendedTPE() {
25 >            super(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>());
26 >        }
27 >        protected void beforeExecute(Thread t, Runnable r) {
28 >            beforeCalled = true;
29 >        }
30 >        protected void afterExecute(Runnable r, Throwable t) {
31 >            afterCalled = true;
32 >        }
33 >        protected void terminated() {
34 >            terminatedCalled = true;
35 >        }
36      }
37  
48
38      /**
39 <     *  Test to verify that execute successfully executes a runnable
39 >     *  execute successfully executes a runnable
40       */
41 <    public void testExecute(){
42 <        ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
41 >    public void testExecute() {
42 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
43          try {
44 <            one.execute(new Runnable(){
45 <                    public void run(){
46 <                        try{
44 >            p1.execute(new Runnable() {
45 >                    public void run() {
46 >                        try {
47                              Thread.sleep(SHORT_DELAY_MS);
48                          } catch(InterruptedException e){
49 <                            fail("unexpected exception");
49 >                            threadUnexpectedException();
50                          }
51                      }
52                  });
53 <            Thread.sleep(SHORT_DELAY_MS * 2);
53 >            Thread.sleep(SMALL_DELAY_MS);
54          } catch(InterruptedException e){
55 <            fail("unexpected exception");
56 <        } finally {
57 <            one.shutdown();
69 <        }
55 >            unexpectedException();
56 >        }
57 >        joinPool(p1);
58      }
59  
60      /**
61 <     *  Test to verify getActiveCount gives correct values
61 >     *  getActiveCount increases but doesn't overestimate, when a
62 >     *  thread becomes active
63       */
64 <    public void testGetActiveCount(){
65 <        ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
64 >    public void testGetActiveCount() {
65 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
66 >        assertEquals(0, p2.getActiveCount());
67 >        p2.execute(new MediumRunnable());
68          try {
69 <            assertEquals(0, two.getActiveCount());
70 <            two.execute(newRunnable());
71 <            try{Thread.sleep(10);} catch(Exception e){}
81 <            assertEquals(1, two.getActiveCount());
82 <        } finally {
83 <            two.shutdown();
69 >            Thread.sleep(SHORT_DELAY_MS);
70 >        } catch(Exception e){
71 >            unexpectedException();
72          }
73 +        assertEquals(1, p2.getActiveCount());
74 +        joinPool(p2);
75 +    }
76 +
77 +    /**
78 +     *  prestartCoreThread starts a thread if under corePoolSize, else doesn't
79 +     */
80 +    public void testPrestartCoreThread() {
81 +        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
82 +        assertEquals(0, p2.getPoolSize());
83 +        assertTrue(p2.prestartCoreThread());
84 +        assertEquals(1, p2.getPoolSize());
85 +        assertTrue(p2.prestartCoreThread());
86 +        assertEquals(2, p2.getPoolSize());
87 +        assertFalse(p2.prestartCoreThread());
88 +        assertEquals(2, p2.getPoolSize());
89 +        joinPool(p2);
90 +    }
91 +
92 +    /**
93 +     *  prestartAllCoreThreads starts all corePoolSize threads
94 +     */
95 +    public void testPrestartAllCoreThreads() {
96 +        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
97 +        assertEquals(0, p2.getPoolSize());
98 +        p2.prestartAllCoreThreads();
99 +        assertEquals(2, p2.getPoolSize());
100 +        p2.prestartAllCoreThreads();
101 +        assertEquals(2, p2.getPoolSize());
102 +        joinPool(p2);
103      }
104      
105      /**
106 <     *  Test to verify getCompleteTaskCount gives correct values
106 >     *   getCompletedTaskCount increases, but doesn't overestimate,
107 >     *   when tasks complete
108       */
109 <    public void testGetCompletedTaskCount(){
110 <        ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
109 >    public void testGetCompletedTaskCount() {
110 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
111 >        assertEquals(0, p2.getCompletedTaskCount());
112 >        p2.execute(new ShortRunnable());
113          try {
114 <            assertEquals(0, two.getCompletedTaskCount());
115 <            two.execute(newRunnable());
116 <            try{Thread.sleep(2000);} catch(Exception e){}
96 <            assertEquals(1, two.getCompletedTaskCount());
97 <        } finally {
98 <            two.shutdown();
114 >            Thread.sleep(SMALL_DELAY_MS);
115 >        } catch(Exception e){
116 >            unexpectedException();
117          }
118 +        assertEquals(1, p2.getCompletedTaskCount());
119 +        p2.shutdown();
120 +        joinPool(p2);
121      }
122      
123      /**
124 <     *  Test to verify getCorePoolSize gives correct values
124 >     *   getCorePoolSize returns size given in constructor if not otherwise set
125       */
126 <    public void testGetCorePoolSize(){
127 <        ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
128 <        try {
129 <            assertEquals(1, one.getCorePoolSize());
109 <        } finally {
110 <            one.shutdown();
111 <        }
126 >    public void testGetCorePoolSize() {
127 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
128 >        assertEquals(1, p1.getCorePoolSize());
129 >        joinPool(p1);
130      }
131      
132      /**
133 <     *  Test to verify getKeepAliveTime gives correct values
133 >     *   getKeepAliveTime returns value given in constructor if not otherwise set
134 >     */
135 >    public void testGetKeepAliveTime() {
136 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
137 >        assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS));
138 >        joinPool(p2);
139 >    }
140 >
141 >
142 >    /**
143 >     * getThreadFactory returns factory in constructor if not set
144 >     */
145 >    public void testGetThreadFactory() {
146 >        ThreadFactory tf = new SimpleThreadFactory();
147 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
148 >        assertSame(tf, p.getThreadFactory());
149 >        p.shutdown();
150 >        joinPool(p);
151 >    }
152 >
153 >    /**
154 >     * setThreadFactory sets the thread factory returned by getThreadFactory
155       */
156 <    public void testGetKeepAliveTime(){
157 <        ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
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 >        p.shutdown();
162 >        joinPool(p);
163 >    }
164 >
165 >
166 >    /**
167 >     * setThreadFactory(null) throws NPE
168 >     */
169 >    public void testSetThreadFactoryNull() {
170 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
171          try {
172 <            assertEquals(1, two.getKeepAliveTime(TimeUnit.SECONDS));
172 >            p.setThreadFactory(null);
173 >            shouldThrow();
174 >        } catch (NullPointerException success) {
175          } finally {
176 <            two.shutdown();
176 >            joinPool(p);
177          }
178      }
179 <    
180 <    /**
181 <     *  Test to verify getLargestPoolSize gives correct values
179 >
180 >    /**
181 >     * getRejectedExecutionHandler returns handler in constructor if not set
182 >     */
183 >    public void testGetRejectedExecutionHandler() {
184 >        RejectedExecutionHandler h = new NoOpREHandler();
185 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
186 >        assertSame(h, p.getRejectedExecutionHandler());
187 >        p.shutdown();
188 >        joinPool(p);
189 >    }
190 >
191 >    /**
192 >     * setRejectedExecutionHandler sets the handler returned by
193 >     * getRejectedExecutionHandler
194 >     */
195 >    public void testSetRejectedExecutionHandler() {
196 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
197 >        RejectedExecutionHandler h = new NoOpREHandler();
198 >        p.setRejectedExecutionHandler(h);
199 >        assertSame(h, p.getRejectedExecutionHandler());
200 >        p.shutdown();
201 >        joinPool(p);
202 >    }
203 >
204 >
205 >    /**
206 >     * setRejectedExecutionHandler(null) throws NPE
207       */
208 <    public void testGetLargestPoolSize(){
209 <        ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
208 >    public void testSetRejectedExecutionHandlerNull() {
209 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
210          try {
211 <            assertEquals(0, two.getLargestPoolSize());
212 <            two.execute(newRunnable());
213 <            two.execute(newRunnable());
135 <            try{Thread.sleep(SHORT_DELAY_MS);} catch(Exception e){}
136 <            assertEquals(2, two.getLargestPoolSize());
211 >            p.setRejectedExecutionHandler(null);
212 >            shouldThrow();
213 >        } catch (NullPointerException success) {
214          } finally {
215 <            two.shutdown();
215 >            joinPool(p);
216          }
217      }
218 +
219      
220      /**
221 <     *  Test to verify getMaximumPoolSize gives correct values
221 >     *   getLargestPoolSize increases, but doesn't overestimate, when
222 >     *   multiple threads active
223       */
224 <    public void testGetMaximumPoolSize(){
225 <        ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
224 >    public void testGetLargestPoolSize() {
225 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
226          try {
227 <            assertEquals(2, two.getMaximumPoolSize());
228 <        } finally {
229 <            two.shutdown();
230 <        }
227 >            assertEquals(0, p2.getLargestPoolSize());
228 >            p2.execute(new MediumRunnable());
229 >            p2.execute(new MediumRunnable());
230 >            Thread.sleep(SHORT_DELAY_MS);
231 >            assertEquals(2, p2.getLargestPoolSize());
232 >        } catch(Exception e){
233 >            unexpectedException();
234 >        }
235 >        joinPool(p2);
236      }
237      
238      /**
239 <     *  Test to verify getPoolSize gives correct values
239 >     *   getMaximumPoolSize returns value given in constructor if not
240 >     *   otherwise set
241       */
242 <    public void testGetPoolSize(){
243 <        ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
244 <        try {
245 <            assertEquals(0, one.getPoolSize());
161 <            one.execute(newRunnable());
162 <            assertEquals(1, one.getPoolSize());
163 <        } finally {
164 <            one.shutdown();
165 <        }
242 >    public void testGetMaximumPoolSize() {
243 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
244 >        assertEquals(2, p2.getMaximumPoolSize());
245 >        joinPool(p2);
246      }
247      
248      /**
249 <     *  Test to verify getTaskCount gives correct values
249 >     *   getPoolSize increases, but doesn't overestimate, when threads
250 >     *   become active
251       */
252 <    public void testGetTaskCount(){
253 <        ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
252 >    public void testGetPoolSize() {
253 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
254 >        assertEquals(0, p1.getPoolSize());
255 >        p1.execute(new MediumRunnable());
256 >        assertEquals(1, p1.getPoolSize());
257 >        joinPool(p1);
258 >    }
259 >    
260 >    /**
261 >     *  getTaskCount increases, but doesn't overestimate, when tasks submitted
262 >     */
263 >    public void testGetTaskCount() {
264 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
265          try {
266 <            assertEquals(0, one.getTaskCount());
267 <            for(int i = 0; i < 5; i++)
268 <                one.execute(newRunnable());
269 <            try{Thread.sleep(SHORT_DELAY_MS);} catch(Exception e){}
270 <            assertEquals(5, one.getTaskCount());
271 <        } finally {
272 <            one.shutdown();
273 <        }
266 >            assertEquals(0, p1.getTaskCount());
267 >            p1.execute(new MediumRunnable());
268 >            Thread.sleep(SHORT_DELAY_MS);
269 >            assertEquals(1, p1.getTaskCount());
270 >        } catch(Exception e){
271 >            unexpectedException();
272 >        }
273 >        joinPool(p1);
274      }
275      
276      /**
277 <     *  Test to verify isShutDown gives correct values
277 >     *   isShutDown is false before shutdown, true after
278       */
279 <    public void testIsShutdown(){
279 >    public void testIsShutdown() {
280          
281 <        ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
281 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
282 >        assertFalse(p1.isShutdown());
283 >        p1.shutdown();
284 >        assertTrue(p1.isShutdown());
285 >        joinPool(p1);
286 >    }
287 >
288 >        
289 >    /**
290 >     *  isTerminated is false before termination, true after
291 >     */
292 >    public void testIsTerminated() {
293 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
294 >        assertFalse(p1.isTerminated());
295          try {
296 <            assertFalse(one.isShutdown());
296 >            p1.execute(new MediumRunnable());
297 >        } finally {
298 >            p1.shutdown();
299          }
300 <        finally {
301 <            one.shutdown();
300 >        try {
301 >            assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
302 >            assertTrue(p1.isTerminated());
303 >        } catch(Exception e){
304 >            unexpectedException();
305 >        }      
306 >    }
307 >
308 >    /**
309 >     *  isTerminating is not true when running or when terminated
310 >     */
311 >    public void testIsTerminating() {
312 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
313 >        assertFalse(p1.isTerminating());
314 >        try {
315 >            p1.execute(new SmallRunnable());
316 >            assertFalse(p1.isTerminating());
317 >        } finally {
318 >            p1.shutdown();
319          }
320 <        assertTrue(one.isShutdown());
320 >        try {
321 >            assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
322 >            assertTrue(p1.isTerminated());
323 >            assertFalse(p1.isTerminating());
324 >        } catch(Exception e){
325 >            unexpectedException();
326 >        }      
327      }
328  
199        
329      /**
330 <     *  Test to verify isTerminated gives correct values
202 <     *  Makes sure termination does not take an innapropriate
203 <     *  amount of time
330 >     * getQueue returns the work queue, which contains queued tasks
331       */
332 <    public void testIsTerminated(){
333 <        ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
332 >    public void testGetQueue() {
333 >        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
334 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q);
335 >        CancellableTask[] tasks = new CancellableTask[5];
336 >        for(int i = 0; i < 5; i++){
337 >            tasks[i] = new CancellableTask(new MediumPossiblyInterruptedRunnable());
338 >            p1.execute(tasks[i]);
339 >        }
340          try {
341 <            one.execute(newRunnable());
341 >            Thread.sleep(SHORT_DELAY_MS);
342 >            BlockingQueue<Runnable> wq = p1.getQueue();
343 >            assertSame(q, wq);
344 >            assertFalse(wq.contains(tasks[0]));
345 >            assertTrue(wq.contains(tasks[4]));
346 >            p1.shutdownNow();
347 >        } catch(Exception e) {
348 >            unexpectedException();
349          } finally {
350 <            one.shutdown();
350 >            joinPool(p1);
351          }
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");
352      }
353  
354      /**
355 <     *  Test to verify that purge correctly removes cancelled tasks
223 <     *  from the queue
355 >     * remove(task) removes queued task, and fails to remove active task
356       */
357 <    public void testPurge(){
358 <        ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
357 >    public void testRemove() {
358 >        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
359 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q);
360 >        CancellableTask[] tasks = new CancellableTask[5];
361 >        for(int i = 0; i < 5; i++){
362 >            tasks[i] = new CancellableTask(new MediumPossiblyInterruptedRunnable());
363 >            p1.execute(tasks[i]);
364 >        }
365          try {
366 <            CancellableTask[] tasks = new CancellableTask[5];
367 <            for(int i = 0; i < 5; i++){
368 <                tasks[i] = new CancellableTask(newRunnable());
369 <                one.execute(tasks[i]);
370 <            }
371 <            tasks[4].cancel(true);
372 <            tasks[3].cancel(true);
373 <            one.purge();
374 <            long count = one.getTaskCount();
375 <            assertTrue(count >= 2 && count < 5);
366 >            Thread.sleep(SHORT_DELAY_MS);
367 >            assertFalse(p1.remove(tasks[0]));
368 >            assertTrue(q.contains(tasks[4]));
369 >            assertTrue(q.contains(tasks[3]));
370 >            assertTrue(p1.remove(tasks[4]));
371 >            assertFalse(p1.remove(tasks[4]));
372 >            assertFalse(q.contains(tasks[4]));
373 >            assertTrue(q.contains(tasks[3]));
374 >            assertTrue(p1.remove(tasks[3]));
375 >            assertFalse(q.contains(tasks[3]));
376 >            p1.shutdownNow();
377 >        } catch(Exception e) {
378 >            unexpectedException();
379          } finally {
380 <            one.shutdown();
380 >            joinPool(p1);
381 >        }
382 >    }
383 >
384 >    /**
385 >     *   purge removes cancelled tasks from the queue
386 >     */
387 >    public void testPurge() {
388 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
389 >        CancellableTask[] tasks = new CancellableTask[5];
390 >        for(int i = 0; i < 5; i++){
391 >            tasks[i] = new CancellableTask(new MediumPossiblyInterruptedRunnable());
392 >            p1.execute(tasks[i]);
393          }
394 +        tasks[4].cancel(true);
395 +        tasks[3].cancel(true);
396 +        p1.purge();
397 +        long count = p1.getTaskCount();
398 +        assertTrue(count >= 2 && count < 5);
399 +        p1.shutdownNow();
400 +        joinPool(p1);
401      }
402  
403      /**
404 <     *  Test to verify shutDownNow returns a list
245 <     *  containing the correct number of elements
404 >     *  shutDownNow returns a list containing tasks that were not run
405       */
406 <    public void testShutDownNow(){
407 <        ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
406 >    public void testShutDownNow() {
407 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
408          List l;
409          try {
410              for(int i = 0; i < 5; i++)
411 <                one.execute(newRunnable());
411 >                p1.execute(new MediumPossiblyInterruptedRunnable());
412          }
413          finally {
414 <            l = one.shutdownNow();
414 >            l = p1.shutdownNow();
415          }
416 <        assertTrue(one.isShutdown());
416 >        assertTrue(p1.isShutdown());
417          assertTrue(l.size() <= 4);
418      }
419  
261    
262
263    
264    
265      
420      // Exception Tests
421      
422  
423 <    //---- Tests if corePoolSize argument is less than zero
423 >    /**
424 >     * Constructor throws if corePoolSize argument is less than zero
425 >     */
426      public void testConstructor1() {
427 <        try{
428 <            new ThreadPoolExecutor(-1,1,100L,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
429 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
427 >        try {
428 >            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
429 >            shouldThrow();
430          }
431 <        catch (IllegalArgumentException i){}
431 >        catch (IllegalArgumentException success){}
432      }
433      
434 <    //---- Tests if maximumPoolSize is less than zero
434 >    /**
435 >     * Constructor throws if maximumPoolSize is less than zero
436 >     */
437      public void testConstructor2() {
438 <        try{
439 <            new ThreadPoolExecutor(1,-1,100L,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
440 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
438 >        try {
439 >            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
440 >            shouldThrow();
441          }
442 <        catch (IllegalArgumentException i2){}
442 >        catch (IllegalArgumentException success){}
443      }
444      
445 <    //---- Tests if maximumPoolSize is equal to zero
445 >    /**
446 >     * Constructor throws if maximumPoolSize is equal to zero
447 >     */
448      public void testConstructor3() {
449 <        try{
450 <            new ThreadPoolExecutor(1,0,100L,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
451 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
449 >        try {
450 >            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
451 >            shouldThrow();
452          }
453 <        catch (IllegalArgumentException i3){}
453 >        catch (IllegalArgumentException success){}
454      }
455  
456 <    //---- Tests if keepAliveTime is less than zero
456 >    /**
457 >     * Constructor throws if keepAliveTime is less than zero
458 >     */
459      public void testConstructor4() {
460 <        try{
460 >        try {
461              new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
462 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
462 >            shouldThrow();
463          }
464 <        catch (IllegalArgumentException i4){}
464 >        catch (IllegalArgumentException success){}
465      }
466  
467 <    //---- Tests if corePoolSize is greater than the maximumPoolSize
467 >    /**
468 >     * Constructor throws if corePoolSize is greater than the maximumPoolSize
469 >     */
470      public void testConstructor5() {
471 <        try{
472 <            new ThreadPoolExecutor(2,1,100L,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
473 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
471 >        try {
472 >            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
473 >            shouldThrow();
474          }
475 <        catch (IllegalArgumentException i5){}
475 >        catch (IllegalArgumentException success){}
476      }
477          
478 <    //---- Tests if workQueue is set to null
479 <    public void testNullPointerException() {
480 <        try{
481 <            new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,null);
482 <            fail("ThreadPoolExecutor constructor should throw a NullPointerException");        
478 >    /**
479 >     * Constructor throws if workQueue is set to null
480 >     */
481 >    public void testConstructorNullPointerException() {
482 >        try {
483 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null);
484 >            shouldThrow();
485          }
486 <        catch (NullPointerException n){}  
486 >        catch (NullPointerException success){}  
487      }
488      
489  
490      
491 <    //---- Tests if corePoolSize argument is less than zero
491 >    /**
492 >     * Constructor throws if corePoolSize argument is less than zero
493 >     */
494      public void testConstructor6() {
495 <        try{
496 <            new ThreadPoolExecutor(-1,1,100L,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread());
497 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
498 <        } catch (IllegalArgumentException i6){}
495 >        try {
496 >            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
497 >            shouldThrow();
498 >        } catch (IllegalArgumentException success){}
499      }
500      
501 <    //---- Tests if maximumPoolSize is less than zero
501 >    /**
502 >     * Constructor throws if maximumPoolSize is less than zero
503 >     */
504      public void testConstructor7() {
505 <        try{
506 <            new ThreadPoolExecutor(1,-1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread());
507 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
505 >        try {
506 >            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
507 >            shouldThrow();
508          }
509 <        catch (IllegalArgumentException i7){}
509 >        catch (IllegalArgumentException success){}
510      }
511  
512 <    //---- Tests if maximumPoolSize is equal to zero
512 >    /**
513 >     * Constructor throws if maximumPoolSize is equal to zero
514 >     */
515      public void testConstructor8() {
516 <        try{
517 <            new ThreadPoolExecutor(1,0,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread());
518 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
516 >        try {
517 >            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
518 >            shouldThrow();
519          }
520 <        catch (IllegalArgumentException i8){}
520 >        catch (IllegalArgumentException success){}
521      }
522  
523 <    //---- Tests if keepAliveTime is less than zero
523 >    /**
524 >     * Constructor throws if keepAliveTime is less than zero
525 >     */
526      public void testConstructor9() {
527 <        try{
528 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread());
529 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
527 >        try {
528 >            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
529 >            shouldThrow();
530          }
531 <        catch (IllegalArgumentException i9){}
531 >        catch (IllegalArgumentException success){}
532      }
533  
534 <    //---- Tests if corePoolSize is greater than the maximumPoolSize
534 >    /**
535 >     * Constructor throws if corePoolSize is greater than the maximumPoolSize
536 >     */
537      public void testConstructor10() {
538 <        try{
539 <            new ThreadPoolExecutor(2,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread());
540 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
538 >        try {
539 >            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
540 >            shouldThrow();
541          }
542 <        catch (IllegalArgumentException i10){}
542 >        catch (IllegalArgumentException success){}
543      }
544  
545 <    //---- Tests if workQueue is set to null
546 <    public void testNullPointerException2() {
547 <        try{
548 <            new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,null,new testThread());
549 <            fail("ThreadPoolExecutor constructor should throw a NullPointerException");        
545 >    /**
546 >     * Constructor throws if workQueue is set to null
547 >     */
548 >    public void testConstructorNullPointerException2() {
549 >        try {
550 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory());
551 >            shouldThrow();
552          }
553 <        catch (NullPointerException n2){}  
553 >        catch (NullPointerException success){}  
554      }
555  
556 <    //---- Tests if threadFactory is set to null
557 <    public void testNullPointerException3() {
558 <        try{
556 >    /**
557 >     * Constructor throws if threadFactory is set to null
558 >     */
559 >    public void testConstructorNullPointerException3() {
560 >        try {
561              ThreadFactory f = null;
562 <            new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
563 <            fail("ThreadPoolExecutor constructor should throw a NullPointerException");        
562 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
563 >            shouldThrow();
564          }
565 <        catch (NullPointerException n3){}  
565 >        catch (NullPointerException success){}  
566      }
567  
568      
569 <    //---- Tests if corePoolSize argument is less than zero
569 >    /**
570 >     * Constructor throws if corePoolSize argument is less than zero
571 >     */
572      public void testConstructor11() {
573 <        try{
574 <            new ThreadPoolExecutor(-1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testReject());
575 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
573 >        try {
574 >            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
575 >            shouldThrow();
576          }
577 <        catch (IllegalArgumentException i11){}
577 >        catch (IllegalArgumentException success){}
578      }
579  
580 <    //---- Tests if maximumPoolSize is less than zero
580 >    /**
581 >     * Constructor throws if maximumPoolSize is less than zero
582 >     */
583      public void testConstructor12() {
584 <        try{
585 <            new ThreadPoolExecutor(1,-1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testReject());
586 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
584 >        try {
585 >            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
586 >            shouldThrow();
587          }
588 <        catch (IllegalArgumentException i12){}
588 >        catch (IllegalArgumentException success){}
589      }
590  
591 <    //---- Tests if maximumPoolSize is equal to zero
591 >    /**
592 >     * Constructor throws if maximumPoolSize is equal to zero
593 >     */
594      public void testConstructor13() {
595 <        try{
596 <            new ThreadPoolExecutor(1,0,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testReject());
597 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
595 >        try {
596 >            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
597 >            shouldThrow();
598          }
599 <        catch (IllegalArgumentException i13){}
599 >        catch (IllegalArgumentException success){}
600      }
601  
602 <    //---- Tests if keepAliveTime is less than zero
602 >    /**
603 >     * Constructor throws if keepAliveTime is less than zero
604 >     */
605      public void testConstructor14() {
606 <        try{
607 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testReject());
608 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
606 >        try {
607 >            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
608 >            shouldThrow();
609          }
610 <        catch (IllegalArgumentException i14){}
610 >        catch (IllegalArgumentException success){}
611      }
612  
613 <    //---- Tests if corePoolSize is greater than the maximumPoolSize
613 >    /**
614 >     * Constructor throws if corePoolSize is greater than the maximumPoolSize
615 >     */
616      public void testConstructor15() {
617 <        try{
618 <            new ThreadPoolExecutor(2,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testReject());
619 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
617 >        try {
618 >            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
619 >            shouldThrow();
620          }
621 <        catch (IllegalArgumentException i15){}
621 >        catch (IllegalArgumentException success){}
622      }
623  
624 <    //---- Tests if workQueue is set to null
625 <    public void testNullPointerException4() {
626 <        try{
627 <            new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,null,new testReject());
628 <            fail("ThreadPoolExecutor constructor should throw a NullPointerException");        
624 >    /**
625 >     * Constructor throws if workQueue is set to null
626 >     */
627 >    public void testConstructorNullPointerException4() {
628 >        try {
629 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler());
630 >            shouldThrow();
631          }
632 <        catch (NullPointerException n4){}  
632 >        catch (NullPointerException success){}  
633      }
634  
635 <    //---- Tests if handler is set to null
636 <    public void testNullPointerException5() {
637 <        try{
635 >    /**
636 >     * Constructor throws if handler is set to null
637 >     */
638 >    public void testConstructorNullPointerException5() {
639 >        try {
640              RejectedExecutionHandler r = null;
641 <            new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
642 <            fail("ThreadPoolExecutor constructor should throw a NullPointerException");        
641 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
642 >            shouldThrow();
643          }
644 <        catch (NullPointerException n5){}  
644 >        catch (NullPointerException success){}  
645      }
646  
647      
648 <    //---- Tests if corePoolSize argument is less than zero
648 >    /**
649 >     * Constructor throws if corePoolSize argument is less than zero
650 >     */
651      public void testConstructor16() {
652 <        try{
653 <            new ThreadPoolExecutor(-1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread(),new testReject());
654 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
652 >        try {
653 >            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
654 >            shouldThrow();
655          }
656 <        catch (IllegalArgumentException i16){}
656 >        catch (IllegalArgumentException success){}
657      }
658  
659 <    //---- Tests if maximumPoolSize is less than zero
659 >    /**
660 >     * Constructor throws if maximumPoolSize is less than zero
661 >     */
662      public void testConstructor17() {
663 <        try{
664 <            new ThreadPoolExecutor(1,-1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread(),new testReject());
665 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
663 >        try {
664 >            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
665 >            shouldThrow();
666          }
667 <        catch (IllegalArgumentException i17){}
667 >        catch (IllegalArgumentException success){}
668      }
669  
670 <    //---- Tests if maximumPoolSize is equal to zero
670 >    /**
671 >     * Constructor throws if maximumPoolSize is equal to zero
672 >     */
673      public void testConstructor18() {
674 <        try{
675 <            new ThreadPoolExecutor(1,0,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread(),new testReject());
676 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
674 >        try {
675 >            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
676 >            shouldThrow();
677          }
678 <        catch (IllegalArgumentException i18){}
678 >        catch (IllegalArgumentException success){}
679      }
680  
681 <    //---- Tests if keepAliveTime is less than zero
681 >    /**
682 >     * Constructor throws if keepAliveTime is less than zero
683 >     */
684      public void testConstructor19() {
685 <        try{
686 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread(),new testReject());
687 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
685 >        try {
686 >            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
687 >            shouldThrow();
688          }
689 <        catch (IllegalArgumentException i19){}
689 >        catch (IllegalArgumentException success){}
690      }
691  
692 <    //---- Tests if corePoolSize is greater than the maximumPoolSize
692 >    /**
693 >     * Constructor throws if corePoolSize is greater than the maximumPoolSize
694 >     */
695      public void testConstructor20() {
696 <        try{
697 <            new ThreadPoolExecutor(2,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread(),new testReject());
698 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
696 >        try {
697 >            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
698 >            shouldThrow();
699          }
700 <        catch (IllegalArgumentException i20){}
700 >        catch (IllegalArgumentException success){}
701      }
702  
703 <    //---- Tests if workQueue is set to null
704 <    public void testNullPointerException6() {
705 <        try{
706 <            new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,null,new testThread(),new testReject());
707 <            fail("ThreadPoolExecutor constructor should throw a NullPointerException");        
703 >    /**
704 >     * Constructor throws if workQueue is set to null
705 >     */
706 >    public void testConstructorNullPointerException6() {
707 >        try {
708 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
709 >            shouldThrow();
710          }
711 <        catch (NullPointerException n6){}  
711 >        catch (NullPointerException success){}  
712      }
713  
714 <    //---- Tests if handler is set to null
715 <    public void testNullPointerException7() {
716 <        try{
714 >    /**
715 >     * Constructor throws if handler is set to null
716 >     */
717 >    public void testConstructorNullPointerException7() {
718 >        try {
719              RejectedExecutionHandler r = null;
720 <            new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new testThread(),r);
721 <            fail("ThreadPoolExecutor constructor should throw a NullPointerException");        
720 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
721 >            shouldThrow();
722          }
723 <        catch (NullPointerException n7){}  
723 >        catch (NullPointerException success){}  
724      }
725  
726 <    //---- Tests if ThradFactory is set top null
727 <    public void testNullPointerException8() {
728 <        try{
726 >    /**
727 >     * Constructor throws if ThreadFactory is set top null
728 >     */
729 >    public void testConstructorNullPointerException8() {
730 >        try {
731              ThreadFactory f = null;
732 <            new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new testReject());
733 <            fail("ThreadPoolExecutor constructor should throw a NullPointerException");        
732 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
733 >            shouldThrow();
734          }
735 <        catch (NullPointerException n8){}  
735 >        catch (NullPointerException successdn8){}  
736      }
737      
738  
739      /**
740 <     *  Test to verify execute will throw RejectedExcutionException
741 <     *  ThreadPoolExecutor will throw one when more runnables are
532 <     *  executed then will fit in the Queue.
740 >     *  execute throws RejectedExecutionException
741 >     *  if saturated.
742       */
743 <    public void testRejectedExecutedException(){
744 <        ThreadPoolExecutor tpe = null;
745 <        try{
746 <            tpe = new ThreadPoolExecutor(1,1,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
747 <        } catch(Exception e){}
743 >    public void testSaturatedExecute() {
744 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
745 >        try {
746 >            
747 >            for(int i = 0; i < 5; ++i){
748 >                p.execute(new MediumRunnable());
749 >            }
750 >            shouldThrow();
751 >        } catch(RejectedExecutionException success){}
752 >        joinPool(p);
753 >    }
754 >
755 >    /**
756 >     *  executor using CallerRunsPolicy runs task if saturated.
757 >     */
758 >    public void testSaturatedExecute2() {
759 >        RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
760 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
761 >        try {
762 >            
763 >            TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
764 >            for(int i = 0; i < 5; ++i){
765 >                tasks[i] = new TrackedNoOpRunnable();
766 >            }
767 >            TrackedLongRunnable mr = new TrackedLongRunnable();
768 >            p.execute(mr);
769 >            for(int i = 0; i < 5; ++i){
770 >                p.execute(tasks[i]);
771 >            }
772 >            for(int i = 1; i < 5; ++i) {
773 >                assertTrue(tasks[i].done);
774 >            }
775 >            p.shutdownNow();
776 >        } catch(RejectedExecutionException ex){
777 >            unexpectedException();
778 >        } finally {
779 >            joinPool(p);
780 >        }
781 >    }
782 >
783 >    /**
784 >     *  executor using DiscardPolicy drops task if saturated.
785 >     */
786 >    public void testSaturatedExecute3() {
787 >        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
788 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
789 >        try {
790 >            
791 >            TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
792 >            for(int i = 0; i < 5; ++i){
793 >                tasks[i] = new TrackedNoOpRunnable();
794 >            }
795 >            p.execute(new TrackedLongRunnable());
796 >            for(int i = 0; i < 5; ++i){
797 >                p.execute(tasks[i]);
798 >            }
799 >            for(int i = 0; i < 5; ++i){
800 >                assertFalse(tasks[i].done);
801 >            }
802 >            p.shutdownNow();
803 >        } catch(RejectedExecutionException ex){
804 >            unexpectedException();
805 >        } finally {
806 >            joinPool(p);
807 >        }
808 >    }
809 >
810 >    /**
811 >     *  executor using DiscardOldestPolicy drops oldest task if saturated.
812 >     */
813 >    public void testSaturatedExecute4() {
814 >        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
815 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
816 >        try {
817 >            p.execute(new TrackedLongRunnable());
818 >            TrackedLongRunnable r2 = new TrackedLongRunnable();
819 >            p.execute(r2);
820 >            assertTrue(p.getQueue().contains(r2));
821 >            TrackedNoOpRunnable r3 = new TrackedNoOpRunnable();
822 >            p.execute(r3);
823 >            assertFalse(p.getQueue().contains(r2));
824 >            assertTrue(p.getQueue().contains(r3));
825 >            p.shutdownNow();
826 >        } catch(RejectedExecutionException ex){
827 >            unexpectedException();
828 >        } finally {
829 >            joinPool(p);
830 >        }
831 >    }
832 >
833 >    /**
834 >     *  execute throws RejectedExecutionException if shutdown
835 >     */
836 >    public void testRejectedExecutionExceptionOnShutdown() {
837 >        ThreadPoolExecutor tpe =
838 >            new ThreadPoolExecutor(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
839          tpe.shutdown();
840 <        try{
841 <            tpe.execute(new Runnable(){
842 <                    public void run(){
543 <                        try{
544 <                            Thread.sleep(1000);
545 <                        } catch(InterruptedException e){}
546 <                    }
547 <                });
548 <            fail("ThreadPoolExecutor - void execute(Runnable) should throw RejectedExecutionException");
840 >        try {
841 >            tpe.execute(new NoOpRunnable());
842 >            shouldThrow();
843          } catch(RejectedExecutionException success){}
844          
845 +        joinPool(tpe);
846 +    }
847 +
848 +    /**
849 +     *  execute using CallerRunsPolicy drops task on shutdown
850 +     */
851 +    public void testCallerRunsOnShutdown() {
852 +        RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
853 +        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
854 +
855 +        p.shutdown();
856 +        try {
857 +            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
858 +            p.execute(r);
859 +            assertFalse(r.done);
860 +        } catch(RejectedExecutionException success){
861 +            unexpectedException();
862 +        } finally {
863 +            joinPool(p);
864 +        }
865 +    }
866 +
867 +    /**
868 +     *  execute using DiscardPolicy drops task on shutdown
869 +     */
870 +    public void testDiscardOnShutdown() {
871 +        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
872 +        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
873 +
874 +        p.shutdown();
875 +        try {
876 +            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
877 +            p.execute(r);
878 +            assertFalse(r.done);
879 +        } catch(RejectedExecutionException success){
880 +            unexpectedException();
881 +        } finally {
882 +            joinPool(p);
883 +        }
884 +    }
885 +
886 +
887 +    /**
888 +     *  execute using DiscardOldestPolicy drops task on shutdown
889 +     */
890 +    public void testDiscardOldestOnShutdown() {
891 +        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
892 +        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
893 +
894 +        p.shutdown();
895 +        try {
896 +            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
897 +            p.execute(r);
898 +            assertFalse(r.done);
899 +        } catch(RejectedExecutionException success){
900 +            unexpectedException();
901 +        } finally {
902 +            joinPool(p);
903 +        }
904 +    }
905 +
906 +
907 +    /**
908 +     *  execute (null) throws NPE
909 +     */
910 +    public void testExecuteNull() {
911 +        ThreadPoolExecutor tpe = null;
912 +        try {
913 +            tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
914 +            tpe.execute(null);
915 +            shouldThrow();
916 +        } catch(NullPointerException success){}
917          
918 +        joinPool(tpe);
919      }
920      
921      /**
922 <     *  Test to verify setCorePoolSize will throw IllegalArgumentException
556 <     *  when given a negative
922 >     *  setCorePoolSize of negative value throws IllegalArgumentException
923       */
924 <    public void testIllegalArgumentException1(){
924 >    public void testCorePoolSizeIllegalArgumentException() {
925          ThreadPoolExecutor tpe = null;
926 <        try{
927 <            tpe = new ThreadPoolExecutor(1,2,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
926 >        try {
927 >            tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
928          } catch(Exception e){}
929 <        try{
929 >        try {
930              tpe.setCorePoolSize(-1);
931 <            fail("ThreadPoolExecutor - void setCorePoolSize(int) should throw IllegalArgumentException");
931 >            shouldThrow();
932          } catch(IllegalArgumentException success){
933          } finally {
934              tpe.shutdown();
935          }
936 +        joinPool(tpe);
937      }  
938  
572    
939      /**
940 <     *  Test to verify setMaximumPoolSize(int) will throw IllegalArgumentException
941 <     *  if given a value less the it's actual core pool size
940 >     *  setMaximumPoolSize(int) throws IllegalArgumentException if
941 >     *  given a value less the core pool size
942       */  
943 <    public void testIllegalArgumentException2(){
943 >    public void testMaximumPoolSizeIllegalArgumentException() {
944          ThreadPoolExecutor tpe = null;
945 <        try{
946 <            tpe = new ThreadPoolExecutor(2,3,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
945 >        try {
946 >            tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
947          } catch(Exception e){}
948 <        try{
948 >        try {
949              tpe.setMaximumPoolSize(1);
950 <            fail("ThreadPoolExecutor - void setMaximumPoolSize(int) should throw IllegalArgumentException");
950 >            shouldThrow();
951          } catch(IllegalArgumentException success){
952          } finally {
953              tpe.shutdown();
954          }
955 +        joinPool(tpe);
956      }
957      
958      /**
959 <     *  Test to verify that setMaximumPoolSize will throw IllegalArgumentException
960 <     *  if given a negative number
959 >     *  setMaximumPoolSize throws IllegalArgumentException
960 >     *  if given a negative value
961       */
962 <    public void testIllegalArgumentException2SP(){
962 >    public void testMaximumPoolSizeIllegalArgumentException2() {
963          ThreadPoolExecutor tpe = null;
964 <        try{
965 <            tpe = new ThreadPoolExecutor(2,3,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
964 >        try {
965 >            tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
966          } catch(Exception e){}
967 <        try{
967 >        try {
968              tpe.setMaximumPoolSize(-1);
969 <            fail("ThreadPoolExecutor - void setMaximumPoolSize(int) should throw IllegalArgumentException");
969 >            shouldThrow();
970          } catch(IllegalArgumentException success){
971          } finally {
972              tpe.shutdown();
973          }
974 +        joinPool(tpe);
975      }
976      
977  
978      /**
979 <     *  Test to verify setKeepAliveTime will throw IllegalArgumentException
979 >     *  setKeepAliveTime  throws IllegalArgumentException
980       *  when given a negative value
981       */
982 <    public void testIllegalArgumentException3(){
982 >    public void testKeepAliveTimeIllegalArgumentException() {
983          ThreadPoolExecutor tpe = null;
984 <        try{
985 <            tpe = new ThreadPoolExecutor(2,3,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
984 >        try {
985 >            tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
986          } catch(Exception e){}
987          
988 <        try{
988 >        try {
989              tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS);
990 <            fail("ThreadPoolExecutor - void setKeepAliveTime(long, TimeUnit) should throw IllegalArgumentException");
990 >            shouldThrow();
991          } catch(IllegalArgumentException success){
992          } finally {
993              tpe.shutdown();
994          }
995 +        joinPool(tpe);
996 +    }
997 +
998 +    /**
999 +     * terminated() is called on termination
1000 +     */
1001 +    public void testTerminated() {
1002 +        ExtendedTPE tpe = new ExtendedTPE();
1003 +        tpe.shutdown();
1004 +        assertTrue(tpe.terminatedCalled);
1005 +        joinPool(tpe);
1006 +    }
1007 +
1008 +    /**
1009 +     * beforeExecute and afterExecute are called when executing task
1010 +     */
1011 +    public void testBeforeAfter() {
1012 +        ExtendedTPE tpe = new ExtendedTPE();
1013 +        try {
1014 +            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1015 +            tpe.execute(r);
1016 +            Thread.sleep(SHORT_DELAY_MS);
1017 +            assertTrue(r.done);
1018 +            assertTrue(tpe.beforeCalled);
1019 +            assertTrue(tpe.afterCalled);
1020 +            tpe.shutdown();
1021 +        }
1022 +        catch(Exception ex) {
1023 +            unexpectedException();
1024 +        } finally {
1025 +            joinPool(tpe);
1026 +        }
1027      }
628  
629    
630  
1028   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines