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.7 by dl, Fri Sep 26 15:33:14 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      
22    private static long SHORT_DELAY_MS = 100;
23    private static long MEDIUM_DELAY_MS = 1000;
24    private static long LONG_DELAY_MS = 10000;
25
26 //---- testThread class to implement ThreadFactory for use in constructors
27    static class testThread implements ThreadFactory{
28        public Thread newThread(Runnable r){
29            return new Thread(r);
30        }  
31    }
32
33 //---- testReject class to implement RejectedExecutionHandler for use in the constructors
34    static class testReject implements RejectedExecutionHandler{
35        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){}
36    }
37
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            };
46    }
20  
48
21      /**
22 <     *  Test to verify that execute successfully executes a runnable
22 >     *   execute successfully executes a runnable
23       */
24 <    public void testExecute(){
25 <        ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
24 >    public void testExecute() {
25 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
26          try {
27 <            one.execute(new Runnable(){
28 <                    public void run(){
29 <                        try{
27 >            p1.execute(new Runnable() {
28 >                    public void run() {
29 >                        try {
30                              Thread.sleep(SHORT_DELAY_MS);
31                          } catch(InterruptedException e){
32 <                            fail("unexpected exception");
32 >                            threadUnexpectedException();
33                          }
34                      }
35                  });
36 <            Thread.sleep(SHORT_DELAY_MS * 2);
36 >            Thread.sleep(SMALL_DELAY_MS);
37          } catch(InterruptedException e){
38 <            fail("unexpected exception");
39 <        } finally {
40 <            one.shutdown();
69 <        }
38 >            unexpectedException();
39 >        }
40 >        joinPool(p1);
41      }
42  
43      /**
44 <     *  Test to verify getActiveCount gives correct values
44 >     *  getActiveCount increases but doesn't overestimate, when a
45 >     *  thread becomes active
46       */
47 <    public void testGetActiveCount(){
48 <        ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
47 >    public void testGetActiveCount() {
48 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
49 >        assertEquals(0, p2.getActiveCount());
50 >        p2.execute(new MediumRunnable());
51          try {
52 <            assertEquals(0, two.getActiveCount());
53 <            two.execute(newRunnable());
54 <            try{Thread.sleep(10);} catch(Exception e){}
81 <            assertEquals(1, two.getActiveCount());
82 <        } finally {
83 <            two.shutdown();
52 >            Thread.sleep(SHORT_DELAY_MS);
53 >        } catch(Exception e){
54 >            unexpectedException();
55          }
56 +        assertEquals(1, p2.getActiveCount());
57 +        joinPool(p2);
58      }
59      
60      /**
61 <     *  Test to verify getCompleteTaskCount gives correct values
61 >     *   getCompletedTaskCount increases, but doesn't overestimate,
62 >     *   when tasks complete
63       */
64 <    public void testGetCompletedTaskCount(){
65 <        ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
64 >    public void testGetCompletedTaskCount() {
65 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
66 >        assertEquals(0, p2.getCompletedTaskCount());
67 >        p2.execute(new ShortRunnable());
68          try {
69 <            assertEquals(0, two.getCompletedTaskCount());
70 <            two.execute(newRunnable());
71 <            try{Thread.sleep(2000);} catch(Exception e){}
96 <            assertEquals(1, two.getCompletedTaskCount());
97 <        } finally {
98 <            two.shutdown();
69 >            Thread.sleep(MEDIUM_DELAY_MS);
70 >        } catch(Exception e){
71 >            unexpectedException();
72          }
73 +        assertEquals(1, p2.getCompletedTaskCount());
74 +        p2.shutdown();
75 +        joinPool(p2);
76      }
77      
78      /**
79 <     *  Test to verify getCorePoolSize gives correct values
79 >     *   getCorePoolSize returns size given in constructor if not otherwise set
80       */
81 <    public void testGetCorePoolSize(){
82 <        ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
83 <        try {
84 <            assertEquals(1, one.getCorePoolSize());
109 <        } finally {
110 <            one.shutdown();
111 <        }
81 >    public void testGetCorePoolSize() {
82 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
83 >        assertEquals(1, p1.getCorePoolSize());
84 >        joinPool(p1);
85      }
86      
87      /**
88 <     *  Test to verify getKeepAliveTime gives correct values
88 >     *   getKeepAliveTime returns value given in constructor if not otherwise set
89       */
90 <    public void testGetKeepAliveTime(){
91 <        ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
92 <        try {
93 <            assertEquals(1, two.getKeepAliveTime(TimeUnit.SECONDS));
121 <        } finally {
122 <            two.shutdown();
123 <        }
90 >    public void testGetKeepAliveTime() {
91 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
92 >        assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS));
93 >        joinPool(p2);
94      }
95      
96      /**
97 <     *  Test to verify getLargestPoolSize gives correct values
97 >     *   getLargestPoolSize increases, but doesn't overestimate, when
98 >     *   multiple threads active
99       */
100 <    public void testGetLargestPoolSize(){
101 <        ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
100 >    public void testGetLargestPoolSize() {
101 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
102          try {
103 <            assertEquals(0, two.getLargestPoolSize());
104 <            two.execute(newRunnable());
105 <            two.execute(newRunnable());
106 <            try{Thread.sleep(SHORT_DELAY_MS);} catch(Exception e){}
107 <            assertEquals(2, two.getLargestPoolSize());
108 <        } finally {
109 <            two.shutdown();
110 <        }
103 >            assertEquals(0, p2.getLargestPoolSize());
104 >            p2.execute(new MediumRunnable());
105 >            p2.execute(new MediumRunnable());
106 >            Thread.sleep(SHORT_DELAY_MS);
107 >            assertEquals(2, p2.getLargestPoolSize());
108 >        } catch(Exception e){
109 >            unexpectedException();
110 >        }
111 >        joinPool(p2);
112      }
113      
114      /**
115 <     *  Test to verify getMaximumPoolSize gives correct values
115 >     *   getMaximumPoolSize returns value given in constructor if not
116 >     *   otherwise set
117       */
118 <    public void testGetMaximumPoolSize(){
119 <        ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
120 <        try {
121 <            assertEquals(2, two.getMaximumPoolSize());
149 <        } finally {
150 <            two.shutdown();
151 <        }
118 >    public void testGetMaximumPoolSize() {
119 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
120 >        assertEquals(2, p2.getMaximumPoolSize());
121 >        joinPool(p2);
122      }
123      
124      /**
125 <     *  Test to verify getPoolSize gives correct values
125 >     *   getPoolSize increases, but doesn't overestimate, when threads
126 >     *   become active
127       */
128 <    public void testGetPoolSize(){
129 <        ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
130 <        try {
131 <            assertEquals(0, one.getPoolSize());
132 <            one.execute(newRunnable());
133 <            assertEquals(1, one.getPoolSize());
163 <        } finally {
164 <            one.shutdown();
165 <        }
128 >    public void testGetPoolSize() {
129 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
130 >        assertEquals(0, p1.getPoolSize());
131 >        p1.execute(new MediumRunnable());
132 >        assertEquals(1, p1.getPoolSize());
133 >        joinPool(p1);
134      }
135      
136      /**
137 <     *  Test to verify getTaskCount gives correct values
137 >     *   getTaskCount increases, but doesn't overestimate, when tasks submitted
138       */
139 <    public void testGetTaskCount(){
140 <        ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
139 >    public void testGetTaskCount() {
140 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
141          try {
142 <            assertEquals(0, one.getTaskCount());
143 <            for(int i = 0; i < 5; i++)
144 <                one.execute(newRunnable());
145 <            try{Thread.sleep(SHORT_DELAY_MS);} catch(Exception e){}
146 <            assertEquals(5, one.getTaskCount());
147 <        } finally {
148 <            one.shutdown();
149 <        }
142 >            assertEquals(0, p1.getTaskCount());
143 >            p1.execute(new MediumRunnable());
144 >            Thread.sleep(SHORT_DELAY_MS);
145 >            assertEquals(1, p1.getTaskCount());
146 >        } catch(Exception e){
147 >            unexpectedException();
148 >        }
149 >        joinPool(p1);
150      }
151      
152      /**
153 <     *  Test to verify isShutDown gives correct values
153 >     *   isShutDown is false before shutdown, true after
154       */
155 <    public void testIsShutdown(){
155 >    public void testIsShutdown() {
156          
157 <        ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
158 <        try {
159 <            assertFalse(one.isShutdown());
160 <        }
161 <        finally {
194 <            one.shutdown();
195 <        }
196 <        assertTrue(one.isShutdown());
157 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
158 >        assertFalse(p1.isShutdown());
159 >        p1.shutdown();
160 >        assertTrue(p1.isShutdown());
161 >        joinPool(p1);
162      }
163  
164          
165      /**
166 <     *  Test to verify isTerminated gives correct values
202 <     *  Makes sure termination does not take an innapropriate
203 <     *  amount of time
166 >     *  isTerminated is false before termination, true after
167       */
168 <    public void testIsTerminated(){
169 <        ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
168 >    public void testIsTerminated() {
169 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
170 >        assertFalse(p1.isTerminated());
171          try {
172 <            one.execute(newRunnable());
172 >            p1.execute(new MediumRunnable());
173          } finally {
174 <            one.shutdown();
174 >            p1.shutdown();
175          }
176 <        boolean flag = false;
177 <        try{
178 <            flag = one.awaitTermination(10, TimeUnit.SECONDS);
179 <        } catch(Exception e){}  
180 <        assertTrue(one.isTerminated());
181 <        if(!flag)
218 <            fail("ThreadPoolExecutor - thread pool did not terminate within suitable timeframe");
176 >        try {
177 >            assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
178 >            assertTrue(p1.isTerminated());
179 >        } catch(Exception e){
180 >            unexpectedException();
181 >        }      
182      }
183  
184      /**
185 <     *  Test to verify that purge correctly removes cancelled tasks
223 <     *  from the queue
185 >     *  isTerminating is not true when running or when terminated
186       */
187 <    public void testPurge(){
188 <        ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
187 >    public void testIsTerminating() {
188 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
189 >        assertFalse(p1.isTerminating());
190          try {
191 <            CancellableTask[] tasks = new CancellableTask[5];
192 <            for(int i = 0; i < 5; i++){
230 <                tasks[i] = new CancellableTask(newRunnable());
231 <                one.execute(tasks[i]);
232 <            }
233 <            tasks[4].cancel(true);
234 <            tasks[3].cancel(true);
235 <            one.purge();
236 <            long count = one.getTaskCount();
237 <            assertTrue(count >= 2 && count < 5);
191 >            p1.execute(new SmallRunnable());
192 >            assertFalse(p1.isTerminating());
193          } finally {
194 <            one.shutdown();
194 >            p1.shutdown();
195          }
196 +        try {
197 +            assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
198 +            assertTrue(p1.isTerminated());
199 +            assertFalse(p1.isTerminating());
200 +        } catch(Exception e){
201 +            unexpectedException();
202 +        }      
203 +    }
204 +
205 +    /**
206 +     *   purge removes cancelled tasks from the queue
207 +     */
208 +    public void testPurge() {
209 +        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
210 +        CancellableTask[] tasks = new CancellableTask[5];
211 +        for(int i = 0; i < 5; i++){
212 +            tasks[i] = new CancellableTask(new MediumPossiblyInterruptedRunnable());
213 +            p1.execute(tasks[i]);
214 +        }
215 +        tasks[4].cancel(true);
216 +        tasks[3].cancel(true);
217 +        p1.purge();
218 +        long count = p1.getTaskCount();
219 +        assertTrue(count >= 2 && count < 5);
220 +        joinPool(p1);
221      }
222  
223      /**
224 <     *  Test to verify shutDownNow returns a list
245 <     *  containing the correct number of elements
224 >     *  shutDownNow returns a list containing tasks that were not run
225       */
226 <    public void testShutDownNow(){
227 <        ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
226 >    public void testShutDownNow() {
227 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
228          List l;
229          try {
230              for(int i = 0; i < 5; i++)
231 <                one.execute(newRunnable());
231 >                p1.execute(new MediumPossiblyInterruptedRunnable());
232          }
233          finally {
234 <            l = one.shutdownNow();
234 >            l = p1.shutdownNow();
235          }
236 <        assertTrue(one.isShutdown());
236 >        assertTrue(p1.isShutdown());
237          assertTrue(l.size() <= 4);
238      }
239  
261    
262
263    
264    
265      
240      // Exception Tests
241      
242  
243 <    //---- Tests if corePoolSize argument is less than zero
243 >    /**
244 >     * Constructor throws if corePoolSize argument is less than zero
245 >     */
246      public void testConstructor1() {
247 <        try{
248 <            new ThreadPoolExecutor(-1,1,100L,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
249 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
247 >        try {
248 >            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
249 >            shouldThrow();
250          }
251 <        catch (IllegalArgumentException i){}
251 >        catch (IllegalArgumentException success){}
252      }
253      
254 <    //---- Tests if maximumPoolSize is less than zero
254 >    /**
255 >     * Constructor throws if maximumPoolSize is less than zero
256 >     */
257      public void testConstructor2() {
258 <        try{
259 <            new ThreadPoolExecutor(1,-1,100L,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
260 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
258 >        try {
259 >            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
260 >            shouldThrow();
261          }
262 <        catch (IllegalArgumentException i2){}
262 >        catch (IllegalArgumentException success){}
263      }
264      
265 <    //---- Tests if maximumPoolSize is equal to zero
265 >    /**
266 >     * Constructor throws if maximumPoolSize is equal to zero
267 >     */
268      public void testConstructor3() {
269 <        try{
270 <            new ThreadPoolExecutor(1,0,100L,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
271 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
269 >        try {
270 >            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
271 >            shouldThrow();
272          }
273 <        catch (IllegalArgumentException i3){}
273 >        catch (IllegalArgumentException success){}
274      }
275  
276 <    //---- Tests if keepAliveTime is less than zero
276 >    /**
277 >     * Constructor throws if keepAliveTime is less than zero
278 >     */
279      public void testConstructor4() {
280 <        try{
280 >        try {
281              new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
282 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
282 >            shouldThrow();
283          }
284 <        catch (IllegalArgumentException i4){}
284 >        catch (IllegalArgumentException success){}
285      }
286  
287 <    //---- Tests if corePoolSize is greater than the maximumPoolSize
287 >    /**
288 >     * Constructor throws if corePoolSize is greater than the maximumPoolSize
289 >     */
290      public void testConstructor5() {
291 <        try{
292 <            new ThreadPoolExecutor(2,1,100L,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
293 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
291 >        try {
292 >            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
293 >            shouldThrow();
294          }
295 <        catch (IllegalArgumentException i5){}
295 >        catch (IllegalArgumentException success){}
296      }
297          
298 <    //---- Tests if workQueue is set to null
298 >    /**
299 >     * Constructor throws if workQueue is set to null
300 >     */
301      public void testNullPointerException() {
302 <        try{
303 <            new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,null);
304 <            fail("ThreadPoolExecutor constructor should throw a NullPointerException");        
302 >        try {
303 >            new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null);
304 >            shouldThrow();
305          }
306 <        catch (NullPointerException n){}  
306 >        catch (NullPointerException success){}  
307      }
308      
309  
310      
311 <    //---- Tests if corePoolSize argument is less than zero
311 >    /**
312 >     * Constructor throws if corePoolSize argument is less than zero
313 >     */
314      public void testConstructor6() {
315 <        try{
316 <            new ThreadPoolExecutor(-1,1,100L,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread());
317 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
318 <        } catch (IllegalArgumentException i6){}
315 >        try {
316 >            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
317 >            shouldThrow();
318 >        } catch (IllegalArgumentException success){}
319      }
320      
321 <    //---- Tests if maximumPoolSize is less than zero
321 >    /**
322 >     * Constructor throws if maximumPoolSize is less than zero
323 >     */
324      public void testConstructor7() {
325 <        try{
326 <            new ThreadPoolExecutor(1,-1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread());
327 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
325 >        try {
326 >            new ThreadPoolExecutor(1,-1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
327 >            shouldThrow();
328          }
329 <        catch (IllegalArgumentException i7){}
329 >        catch (IllegalArgumentException success){}
330      }
331  
332 <    //---- Tests if maximumPoolSize is equal to zero
332 >    /**
333 >     * Constructor throws if maximumPoolSize is equal to zero
334 >     */
335      public void testConstructor8() {
336 <        try{
337 <            new ThreadPoolExecutor(1,0,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread());
338 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
336 >        try {
337 >            new ThreadPoolExecutor(1,0,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
338 >            shouldThrow();
339          }
340 <        catch (IllegalArgumentException i8){}
340 >        catch (IllegalArgumentException success){}
341      }
342  
343 <    //---- Tests if keepAliveTime is less than zero
343 >    /**
344 >     * Constructor throws if keepAliveTime is less than zero
345 >     */
346      public void testConstructor9() {
347 <        try{
348 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread());
349 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
347 >        try {
348 >            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
349 >            shouldThrow();
350          }
351 <        catch (IllegalArgumentException i9){}
351 >        catch (IllegalArgumentException success){}
352      }
353  
354 <    //---- Tests if corePoolSize is greater than the maximumPoolSize
354 >    /**
355 >     * Constructor throws if corePoolSize is greater than the maximumPoolSize
356 >     */
357      public void testConstructor10() {
358 <        try{
359 <            new ThreadPoolExecutor(2,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread());
360 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
358 >        try {
359 >            new ThreadPoolExecutor(2,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
360 >            shouldThrow();
361          }
362 <        catch (IllegalArgumentException i10){}
362 >        catch (IllegalArgumentException success){}
363      }
364  
365 <    //---- Tests if workQueue is set to null
365 >    /**
366 >     * Constructor throws if workQueue is set to null
367 >     */
368      public void testNullPointerException2() {
369 <        try{
370 <            new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,null,new testThread());
371 <            fail("ThreadPoolExecutor constructor should throw a NullPointerException");        
369 >        try {
370 >            new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory());
371 >            shouldThrow();
372          }
373 <        catch (NullPointerException n2){}  
373 >        catch (NullPointerException success){}  
374      }
375  
376 <    //---- Tests if threadFactory is set to null
376 >    /**
377 >     * Constructor throws if threadFactory is set to null
378 >     */
379      public void testNullPointerException3() {
380 <        try{
380 >        try {
381              ThreadFactory f = null;
382 <            new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
383 <            fail("ThreadPoolExecutor constructor should throw a NullPointerException");        
382 >            new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
383 >            shouldThrow();
384          }
385 <        catch (NullPointerException n3){}  
385 >        catch (NullPointerException success){}  
386      }
387  
388      
389 <    //---- Tests if corePoolSize argument is less than zero
389 >    /**
390 >     * Constructor throws if corePoolSize argument is less than zero
391 >     */
392      public void testConstructor11() {
393 <        try{
394 <            new ThreadPoolExecutor(-1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testReject());
395 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
393 >        try {
394 >            new ThreadPoolExecutor(-1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
395 >            shouldThrow();
396          }
397 <        catch (IllegalArgumentException i11){}
397 >        catch (IllegalArgumentException success){}
398      }
399  
400 <    //---- Tests if maximumPoolSize is less than zero
400 >    /**
401 >     * Constructor throws if maximumPoolSize is less than zero
402 >     */
403      public void testConstructor12() {
404 <        try{
405 <            new ThreadPoolExecutor(1,-1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testReject());
406 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
404 >        try {
405 >            new ThreadPoolExecutor(1,-1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
406 >            shouldThrow();
407          }
408 <        catch (IllegalArgumentException i12){}
408 >        catch (IllegalArgumentException success){}
409      }
410  
411 <    //---- Tests if maximumPoolSize is equal to zero
411 >    /**
412 >     * Constructor throws if maximumPoolSize is equal to zero
413 >     */
414      public void testConstructor13() {
415 <        try{
416 <            new ThreadPoolExecutor(1,0,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testReject());
417 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
415 >        try {
416 >            new ThreadPoolExecutor(1,0,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
417 >            shouldThrow();
418          }
419 <        catch (IllegalArgumentException i13){}
419 >        catch (IllegalArgumentException success){}
420      }
421  
422 <    //---- Tests if keepAliveTime is less than zero
422 >    /**
423 >     * Constructor throws if keepAliveTime is less than zero
424 >     */
425      public void testConstructor14() {
426 <        try{
427 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testReject());
428 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
426 >        try {
427 >            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
428 >            shouldThrow();
429          }
430 <        catch (IllegalArgumentException i14){}
430 >        catch (IllegalArgumentException success){}
431      }
432  
433 <    //---- Tests if corePoolSize is greater than the maximumPoolSize
433 >    /**
434 >     * Constructor throws if corePoolSize is greater than the maximumPoolSize
435 >     */
436      public void testConstructor15() {
437 <        try{
438 <            new ThreadPoolExecutor(2,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testReject());
439 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
437 >        try {
438 >            new ThreadPoolExecutor(2,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
439 >            shouldThrow();
440          }
441 <        catch (IllegalArgumentException i15){}
441 >        catch (IllegalArgumentException success){}
442      }
443  
444 <    //---- Tests if workQueue is set to null
444 >    /**
445 >     * Constructor throws if workQueue is set to null
446 >     */
447      public void testNullPointerException4() {
448 <        try{
449 <            new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,null,new testReject());
450 <            fail("ThreadPoolExecutor constructor should throw a NullPointerException");        
448 >        try {
449 >            new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler());
450 >            shouldThrow();
451          }
452 <        catch (NullPointerException n4){}  
452 >        catch (NullPointerException success){}  
453      }
454  
455 <    //---- Tests if handler is set to null
455 >    /**
456 >     * Constructor throws if handler is set to null
457 >     */
458      public void testNullPointerException5() {
459 <        try{
459 >        try {
460              RejectedExecutionHandler r = null;
461 <            new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
462 <            fail("ThreadPoolExecutor constructor should throw a NullPointerException");        
461 >            new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
462 >            shouldThrow();
463          }
464 <        catch (NullPointerException n5){}  
464 >        catch (NullPointerException success){}  
465      }
466  
467      
468 <    //---- Tests if corePoolSize argument is less than zero
468 >    /**
469 >     * Constructor throws if corePoolSize argument is less than zero
470 >     */
471      public void testConstructor16() {
472 <        try{
473 <            new ThreadPoolExecutor(-1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread(),new testReject());
474 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
472 >        try {
473 >            new ThreadPoolExecutor(-1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
474 >            shouldThrow();
475          }
476 <        catch (IllegalArgumentException i16){}
476 >        catch (IllegalArgumentException success){}
477      }
478  
479 <    //---- Tests if maximumPoolSize is less than zero
479 >    /**
480 >     * Constructor throws if maximumPoolSize is less than zero
481 >     */
482      public void testConstructor17() {
483 <        try{
484 <            new ThreadPoolExecutor(1,-1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread(),new testReject());
485 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
483 >        try {
484 >            new ThreadPoolExecutor(1,-1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
485 >            shouldThrow();
486          }
487 <        catch (IllegalArgumentException i17){}
487 >        catch (IllegalArgumentException success){}
488      }
489  
490 <    //---- Tests if maximumPoolSize is equal to zero
490 >    /**
491 >     * Constructor throws if maximumPoolSize is equal to zero
492 >     */
493      public void testConstructor18() {
494 <        try{
495 <            new ThreadPoolExecutor(1,0,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread(),new testReject());
496 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
494 >        try {
495 >            new ThreadPoolExecutor(1,0,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
496 >            shouldThrow();
497          }
498 <        catch (IllegalArgumentException i18){}
498 >        catch (IllegalArgumentException success){}
499      }
500  
501 <    //---- Tests if keepAliveTime is less than zero
501 >    /**
502 >     * Constructor throws if keepAliveTime is less than zero
503 >     */
504      public void testConstructor19() {
505 <        try{
506 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread(),new testReject());
507 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
505 >        try {
506 >            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
507 >            shouldThrow();
508          }
509 <        catch (IllegalArgumentException i19){}
509 >        catch (IllegalArgumentException success){}
510      }
511  
512 <    //---- Tests if corePoolSize is greater than the maximumPoolSize
512 >    /**
513 >     * Constructor throws if corePoolSize is greater than the maximumPoolSize
514 >     */
515      public void testConstructor20() {
516 <        try{
517 <            new ThreadPoolExecutor(2,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread(),new testReject());
518 <            fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");            
516 >        try {
517 >            new ThreadPoolExecutor(2,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
518 >            shouldThrow();
519          }
520 <        catch (IllegalArgumentException i20){}
520 >        catch (IllegalArgumentException success){}
521      }
522  
523 <    //---- Tests if workQueue is set to null
523 >    /**
524 >     * Constructor throws if workQueue is set to null
525 >     */
526      public void testNullPointerException6() {
527 <        try{
528 <            new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,null,new testThread(),new testReject());
529 <            fail("ThreadPoolExecutor constructor should throw a NullPointerException");        
527 >        try {
528 >            new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
529 >            shouldThrow();
530          }
531 <        catch (NullPointerException n6){}  
531 >        catch (NullPointerException success){}  
532      }
533  
534 <    //---- Tests if handler is set to null
534 >    /**
535 >     * Constructor throws if handler is set to null
536 >     */
537      public void testNullPointerException7() {
538 <        try{
538 >        try {
539              RejectedExecutionHandler r = null;
540 <            new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new testThread(),r);
541 <            fail("ThreadPoolExecutor constructor should throw a NullPointerException");        
540 >            new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
541 >            shouldThrow();
542          }
543 <        catch (NullPointerException n7){}  
543 >        catch (NullPointerException success){}  
544      }
545  
546 <    //---- Tests if ThradFactory is set top null
546 >    /**
547 >     * Constructor throws if ThreadFactory is set top null
548 >     */
549      public void testNullPointerException8() {
550 <        try{
550 >        try {
551              ThreadFactory f = null;
552 <            new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new testReject());
553 <            fail("ThreadPoolExecutor constructor should throw a NullPointerException");        
552 >            new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
553 >            shouldThrow();
554          }
555 <        catch (NullPointerException n8){}  
555 >        catch (NullPointerException successdn8){}  
556      }
557      
558  
559      /**
560 <     *  Test to verify execute will throw RejectedExcutionException
531 <     *  ThreadPoolExecutor will throw one when more runnables are
532 <     *  executed then will fit in the Queue.
560 >     *  execute throws RejectedExecutionException if shutdown
561       */
562 <    public void testRejectedExecutedException(){
562 >    public void testRejectedExecutionException() {
563          ThreadPoolExecutor tpe = null;
564 <        try{
565 <            tpe = new ThreadPoolExecutor(1,1,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
564 >        try {
565 >            tpe = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
566          } catch(Exception e){}
567          tpe.shutdown();
568 <        try{
569 <            tpe.execute(new Runnable(){
570 <                    public void run(){
543 <                        try{
544 <                            Thread.sleep(1000);
545 <                        } catch(InterruptedException e){}
546 <                    }
547 <                });
548 <            fail("ThreadPoolExecutor - void execute(Runnable) should throw RejectedExecutionException");
568 >        try {
569 >            tpe.execute(new NoOpRunnable());
570 >            shouldThrow();
571          } catch(RejectedExecutionException success){}
572          
573 +        joinPool(tpe);
574 +    }
575 +
576 +    /**
577 +     *  execute throws RejectedExecutionException
578 +     *  if saturated.
579 +     */
580 +    public void testSaturatedExecute() {
581 +        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
582 +        try {
583 +            
584 +            for(int i = 0; i < 5; ++i){
585 +                p.execute(new MediumRunnable());
586 +            }
587 +            shouldThrow();
588 +        } catch(RejectedExecutionException success){}
589 +        joinPool(p);
590 +    }
591 +
592 +    /**
593 +     *  execute (null) throws NPE
594 +     */
595 +    public void testExecuteNull() {
596 +        ThreadPoolExecutor tpe = null;
597 +        try {
598 +            tpe = new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
599 +            tpe.execute(null);
600 +            shouldThrow();
601 +        } catch(NullPointerException success){}
602          
603 +        joinPool(tpe);
604      }
605      
606      /**
607 <     *  Test to verify setCorePoolSize will throw IllegalArgumentException
556 <     *  when given a negative
607 >     *  setCorePoolSize of netaitev value throws IllegalArgumentException
608       */
609 <    public void testIllegalArgumentException1(){
609 >    public void testCorePoolSizeIllegalArgumentException() {
610          ThreadPoolExecutor tpe = null;
611 <        try{
612 <            tpe = new ThreadPoolExecutor(1,2,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
611 >        try {
612 >            tpe = new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
613          } catch(Exception e){}
614 <        try{
614 >        try {
615              tpe.setCorePoolSize(-1);
616 <            fail("ThreadPoolExecutor - void setCorePoolSize(int) should throw IllegalArgumentException");
616 >            shouldThrow();
617          } catch(IllegalArgumentException success){
618          } finally {
619              tpe.shutdown();
620          }
621 +        joinPool(tpe);
622      }  
623  
572    
624      /**
625 <     *  Test to verify setMaximumPoolSize(int) will throw IllegalArgumentException
626 <     *  if given a value less the it's actual core pool size
625 >     *  setMaximumPoolSize(int) throws IllegalArgumentException if
626 >     *  given a value less the core pool size
627       */  
628 <    public void testIllegalArgumentException2(){
628 >    public void testMaximumPoolSizeIllegalArgumentException() {
629          ThreadPoolExecutor tpe = null;
630 <        try{
631 <            tpe = new ThreadPoolExecutor(2,3,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
630 >        try {
631 >            tpe = new ThreadPoolExecutor(2,3,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
632          } catch(Exception e){}
633 <        try{
633 >        try {
634              tpe.setMaximumPoolSize(1);
635 <            fail("ThreadPoolExecutor - void setMaximumPoolSize(int) should throw IllegalArgumentException");
635 >            shouldThrow();
636          } catch(IllegalArgumentException success){
637          } finally {
638              tpe.shutdown();
639          }
640 +        joinPool(tpe);
641      }
642      
643      /**
644 <     *  Test to verify that setMaximumPoolSize will throw IllegalArgumentException
645 <     *  if given a negative number
644 >     *  setMaximumPoolSize throws IllegalArgumentException
645 >     *  if given a negative value
646       */
647 <    public void testIllegalArgumentException2SP(){
647 >    public void testMaximumPoolSizeIllegalArgumentException2() {
648          ThreadPoolExecutor tpe = null;
649 <        try{
650 <            tpe = new ThreadPoolExecutor(2,3,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
649 >        try {
650 >            tpe = new ThreadPoolExecutor(2,3,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
651          } catch(Exception e){}
652 <        try{
652 >        try {
653              tpe.setMaximumPoolSize(-1);
654 <            fail("ThreadPoolExecutor - void setMaximumPoolSize(int) should throw IllegalArgumentException");
654 >            shouldThrow();
655          } catch(IllegalArgumentException success){
656          } finally {
657              tpe.shutdown();
658          }
659 +        joinPool(tpe);
660      }
661      
662  
663      /**
664 <     *  Test to verify setKeepAliveTime will throw IllegalArgumentException
664 >     *  setKeepAliveTime  throws IllegalArgumentException
665       *  when given a negative value
666       */
667 <    public void testIllegalArgumentException3(){
667 >    public void testKeepAliveTimeIllegalArgumentException() {
668          ThreadPoolExecutor tpe = null;
669 <        try{
670 <            tpe = new ThreadPoolExecutor(2,3,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
669 >        try {
670 >            tpe = new ThreadPoolExecutor(2,3,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
671          } catch(Exception e){}
672          
673 <        try{
673 >        try {
674              tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS);
675 <            fail("ThreadPoolExecutor - void setKeepAliveTime(long, TimeUnit) should throw IllegalArgumentException");
675 >            shouldThrow();
676          } catch(IllegalArgumentException success){
677          } finally {
678              tpe.shutdown();
679          }
680 +        joinPool(tpe);
681      }
682    
629    
630  
683   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines