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.3 by dl, Sun Sep 14 20:42:41 2003 UTC vs.
Revision 1.10 by dl, Sat Nov 1 18:37:02 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines