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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines