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.6 by dl, Thu Sep 25 11:02:41 2003 UTC vs.
Revision 1.11 by dl, Thu Dec 4 20:54:46 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
31 <     */
32 <    static class MyREHandler implements RejectedExecutionHandler{
33 <        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){}
34 <    }
35 <
36 <    /**
37 <     *   execute successfully executes a runnable
39 >     *  execute successfully executes a runnable
40       */
41      public void testExecute() {
42 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
42 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
43          try {
44              p1.execute(new Runnable() {
45                      public void run() {
# Line 60 | Line 62 | public class ThreadPoolExecutorTest exte
62       *  thread becomes active
63       */
64      public void testGetActiveCount() {
65 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
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 {
# Line 71 | Line 73 | public class ThreadPoolExecutorTest exte
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       *   getCompletedTaskCount increases, but doesn't overestimate,
107       *   when tasks complete
108       */
109      public void testGetCompletedTaskCount() {
110 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
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(MEDIUM_DELAY_MS);
114 >            Thread.sleep(SMALL_DELAY_MS);
115          } catch(Exception e){
116              unexpectedException();
117          }
# Line 94 | Line 124 | public class ThreadPoolExecutorTest exte
124       *   getCorePoolSize returns size given in constructor if not otherwise set
125       */
126      public void testGetCorePoolSize() {
127 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
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      }
# Line 107 | Line 137 | public class ThreadPoolExecutorTest exte
137          assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS));
138          joinPool(p2);
139      }
140 +
141 +
142 +    /**
143 +     * getThreadFactory returns factory in constructor if not set
144 +     */
145 +    public void testGetThreadFactory() {
146 +        ThreadFactory tf = new SimpleThreadFactory();
147 +        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
148 +        assertSame(tf, p.getThreadFactory());
149 +        p.shutdown();
150 +        joinPool(p);
151 +    }
152 +
153 +    /**
154 +     * setThreadFactory sets the thread factory returned by getThreadFactory
155 +     */
156 +    public void 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 increases, but doesn't overestimate, when
222       *   multiple threads active
223       */
224      public void testGetLargestPoolSize() {
225 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
225 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
226          try {
227              assertEquals(0, p2.getLargestPoolSize());
228              p2.execute(new MediumRunnable());
# Line 131 | Line 240 | public class ThreadPoolExecutorTest exte
240       *   otherwise set
241       */
242      public void testGetMaximumPoolSize() {
243 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
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      }
# Line 141 | Line 250 | public class ThreadPoolExecutorTest exte
250       *   become active
251       */
252      public void testGetPoolSize() {
253 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
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());
# Line 149 | Line 258 | public class ThreadPoolExecutorTest exte
258      }
259      
260      /**
261 <     *   getTaskCount increases, but doesn't overestimate, when tasks submitted
261 >     *  getTaskCount increases, but doesn't overestimate, when tasks submitted
262       */
263      public void testGetTaskCount() {
264 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
264 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
265          try {
266              assertEquals(0, p1.getTaskCount());
267              p1.execute(new MediumRunnable());
# Line 169 | Line 278 | public class ThreadPoolExecutorTest exte
278       */
279      public void testIsShutdown() {
280          
281 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
281 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
282          assertFalse(p1.isShutdown());
283          p1.shutdown();
284          assertTrue(p1.isShutdown());
# Line 181 | Line 290 | public class ThreadPoolExecutorTest exte
290       *  isTerminated is false before termination, true after
291       */
292      public void testIsTerminated() {
293 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
293 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
294          assertFalse(p1.isTerminated());
295          try {
296              p1.execute(new MediumRunnable());
# Line 200 | Line 309 | public class ThreadPoolExecutorTest exte
309       *  isTerminating is not true when running or when terminated
310       */
311      public void testIsTerminating() {
312 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
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());
# Line 218 | Line 327 | public class ThreadPoolExecutorTest exte
327      }
328  
329      /**
330 +     * getQueue returns the work queue, which contains queued tasks
331 +     */
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 +        FutureTask[] tasks = new FutureTask[5];
336 +        for(int i = 0; i < 5; i++){
337 +            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
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 +        FutureTask[] tasks = new FutureTask[5];
361 +        for(int i = 0; i < 5; i++){
362 +            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
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, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
389 <        CancellableTask[] tasks = new CancellableTask[5];
388 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
389 >        FutureTask[] tasks = new FutureTask[5];
390          for(int i = 0; i < 5; i++){
391 <            tasks[i] = new CancellableTask(new MediumPossiblyInterruptedRunnable());
391 >            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
392              p1.execute(tasks[i]);
393          }
394          tasks[4].cancel(true);
# Line 232 | Line 396 | public class ThreadPoolExecutorTest exte
396          p1.purge();
397          long count = p1.getTaskCount();
398          assertTrue(count >= 2 && count < 5);
399 +        p1.shutdownNow();
400          joinPool(p1);
401      }
402  
# Line 239 | Line 404 | public class ThreadPoolExecutorTest exte
404       *  shutDownNow returns a list containing tasks that were not run
405       */
406      public void testShutDownNow() {
407 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
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++)
# Line 313 | Line 478 | public class ThreadPoolExecutorTest exte
478      /**
479       * Constructor throws if workQueue is set to null
480       */
481 <    public void testNullPointerException() {
481 >    public void testConstructorNullPointerException() {
482          try {
483 <            new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null);
483 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null);
484              shouldThrow();
485          }
486          catch (NullPointerException success){}  
# Line 328 | Line 493 | public class ThreadPoolExecutorTest exte
493       */
494      public void testConstructor6() {
495          try {
496 <            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory());
496 >            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
497              shouldThrow();
498          } catch (IllegalArgumentException success){}
499      }
# Line 338 | Line 503 | public class ThreadPoolExecutorTest exte
503       */
504      public void testConstructor7() {
505          try {
506 <            new ThreadPoolExecutor(1,-1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory());
506 >            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
507              shouldThrow();
508          }
509          catch (IllegalArgumentException success){}
# Line 349 | Line 514 | public class ThreadPoolExecutorTest exte
514       */
515      public void testConstructor8() {
516          try {
517 <            new ThreadPoolExecutor(1,0,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory());
517 >            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
518              shouldThrow();
519          }
520          catch (IllegalArgumentException success){}
# Line 360 | Line 525 | public class ThreadPoolExecutorTest exte
525       */
526      public void testConstructor9() {
527          try {
528 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory());
528 >            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
529              shouldThrow();
530          }
531          catch (IllegalArgumentException success){}
# Line 371 | Line 536 | public class ThreadPoolExecutorTest exte
536       */
537      public void testConstructor10() {
538          try {
539 <            new ThreadPoolExecutor(2,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory());
539 >            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
540              shouldThrow();
541          }
542          catch (IllegalArgumentException success){}
# Line 380 | Line 545 | public class ThreadPoolExecutorTest exte
545      /**
546       * Constructor throws if workQueue is set to null
547       */
548 <    public void testNullPointerException2() {
548 >    public void testConstructorNullPointerException2() {
549          try {
550 <            new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null,new MyThreadFactory());
550 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory());
551              shouldThrow();
552          }
553          catch (NullPointerException success){}  
# Line 391 | Line 556 | public class ThreadPoolExecutorTest exte
556      /**
557       * Constructor throws if threadFactory is set to null
558       */
559 <    public void testNullPointerException3() {
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);
562 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
563              shouldThrow();
564          }
565          catch (NullPointerException success){}  
# Line 406 | Line 571 | public class ThreadPoolExecutorTest exte
571       */
572      public void testConstructor11() {
573          try {
574 <            new ThreadPoolExecutor(-1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler());
574 >            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
575              shouldThrow();
576          }
577          catch (IllegalArgumentException success){}
# Line 417 | Line 582 | public class ThreadPoolExecutorTest exte
582       */
583      public void testConstructor12() {
584          try {
585 <            new ThreadPoolExecutor(1,-1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler());
585 >            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
586              shouldThrow();
587          }
588          catch (IllegalArgumentException success){}
# Line 428 | Line 593 | public class ThreadPoolExecutorTest exte
593       */
594      public void testConstructor13() {
595          try {
596 <            new ThreadPoolExecutor(1,0,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler());
596 >            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
597              shouldThrow();
598          }
599          catch (IllegalArgumentException success){}
# Line 439 | Line 604 | public class ThreadPoolExecutorTest exte
604       */
605      public void testConstructor14() {
606          try {
607 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler());
607 >            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
608              shouldThrow();
609          }
610          catch (IllegalArgumentException success){}
# Line 450 | Line 615 | public class ThreadPoolExecutorTest exte
615       */
616      public void testConstructor15() {
617          try {
618 <            new ThreadPoolExecutor(2,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler());
618 >            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
619              shouldThrow();
620          }
621          catch (IllegalArgumentException success){}
# Line 459 | Line 624 | public class ThreadPoolExecutorTest exte
624      /**
625       * Constructor throws if workQueue is set to null
626       */
627 <    public void testNullPointerException4() {
627 >    public void testConstructorNullPointerException4() {
628          try {
629 <            new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null,new MyREHandler());
629 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler());
630              shouldThrow();
631          }
632          catch (NullPointerException success){}  
# Line 470 | Line 635 | public class ThreadPoolExecutorTest exte
635      /**
636       * Constructor throws if handler is set to null
637       */
638 <    public void testNullPointerException5() {
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);
641 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
642              shouldThrow();
643          }
644          catch (NullPointerException success){}  
# Line 485 | Line 650 | public class ThreadPoolExecutorTest exte
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());
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){}
# Line 496 | Line 661 | public class ThreadPoolExecutorTest exte
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());
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){}
# Line 507 | Line 672 | public class ThreadPoolExecutorTest exte
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());
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){}
# Line 518 | Line 683 | public class ThreadPoolExecutorTest exte
683       */
684      public void testConstructor19() {
685          try {
686 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),new MyREHandler());
686 >            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
687              shouldThrow();
688          }
689          catch (IllegalArgumentException success){}
# Line 529 | Line 694 | public class ThreadPoolExecutorTest exte
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());
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){}
# Line 538 | Line 703 | public class ThreadPoolExecutorTest exte
703      /**
704       * Constructor throws if workQueue is set to null
705       */
706 <    public void testNullPointerException6() {
706 >    public void testConstructorNullPointerException6() {
707          try {
708 <            new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null,new MyThreadFactory(),new MyREHandler());
708 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
709              shouldThrow();
710          }
711          catch (NullPointerException success){}  
# Line 549 | Line 714 | public class ThreadPoolExecutorTest exte
714      /**
715       * Constructor throws if handler is set to null
716       */
717 <    public void testNullPointerException7() {
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);
720 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
721              shouldThrow();
722          }
723          catch (NullPointerException success){}  
# Line 561 | Line 726 | public class ThreadPoolExecutorTest exte
726      /**
727       * Constructor throws if ThreadFactory is set top null
728       */
729 <    public void testNullPointerException8() {
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());
732 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
733              shouldThrow();
734          }
735          catch (NullPointerException successdn8){}  
# Line 572 | Line 737 | public class ThreadPoolExecutorTest exte
737      
738  
739      /**
740 <     *  execute throws RejectedExecutionException if shutdown
740 >     *  execute throws RejectedExecutionException
741 >     *  if saturated.
742       */
743 <    public void testRejectedExecutionException() {
744 <        ThreadPoolExecutor tpe = null;
743 >    public void testSaturatedExecute() {
744 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
745          try {
746 <            tpe = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
747 <        } catch(Exception e){}
746 >            
747 >            for(int i = 0; i < 5; ++i){
748 >                p.execute(new MediumRunnable());
749 >            }
750 >            shouldThrow();
751 >        } catch(RejectedExecutionException success){}
752 >        joinPool(p);
753 >    }
754 >
755 >    /**
756 >     *  executor using CallerRunsPolicy runs task if saturated.
757 >     */
758 >    public void testSaturatedExecute2() {
759 >        RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
760 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
761 >        try {
762 >            
763 >            TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
764 >            for(int i = 0; i < 5; ++i){
765 >                tasks[i] = new TrackedNoOpRunnable();
766 >            }
767 >            TrackedLongRunnable mr = new TrackedLongRunnable();
768 >            p.execute(mr);
769 >            for(int i = 0; i < 5; ++i){
770 >                p.execute(tasks[i]);
771 >            }
772 >            for(int i = 1; i < 5; ++i) {
773 >                assertTrue(tasks[i].done);
774 >            }
775 >            p.shutdownNow();
776 >        } catch(RejectedExecutionException ex){
777 >            unexpectedException();
778 >        } finally {
779 >            joinPool(p);
780 >        }
781 >    }
782 >
783 >    /**
784 >     *  executor using DiscardPolicy drops task if saturated.
785 >     */
786 >    public void testSaturatedExecute3() {
787 >        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
788 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
789 >        try {
790 >            
791 >            TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
792 >            for(int i = 0; i < 5; ++i){
793 >                tasks[i] = new TrackedNoOpRunnable();
794 >            }
795 >            p.execute(new TrackedLongRunnable());
796 >            for(int i = 0; i < 5; ++i){
797 >                p.execute(tasks[i]);
798 >            }
799 >            for(int i = 0; i < 5; ++i){
800 >                assertFalse(tasks[i].done);
801 >            }
802 >            p.shutdownNow();
803 >        } catch(RejectedExecutionException ex){
804 >            unexpectedException();
805 >        } finally {
806 >            joinPool(p);
807 >        }
808 >    }
809 >
810 >    /**
811 >     *  executor using DiscardOldestPolicy drops oldest task if saturated.
812 >     */
813 >    public void testSaturatedExecute4() {
814 >        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
815 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
816 >        try {
817 >            p.execute(new TrackedLongRunnable());
818 >            TrackedLongRunnable r2 = new TrackedLongRunnable();
819 >            p.execute(r2);
820 >            assertTrue(p.getQueue().contains(r2));
821 >            TrackedNoOpRunnable r3 = new TrackedNoOpRunnable();
822 >            p.execute(r3);
823 >            assertFalse(p.getQueue().contains(r2));
824 >            assertTrue(p.getQueue().contains(r3));
825 >            p.shutdownNow();
826 >        } catch(RejectedExecutionException ex){
827 >            unexpectedException();
828 >        } finally {
829 >            joinPool(p);
830 >        }
831 >    }
832 >
833 >    /**
834 >     *  execute throws RejectedExecutionException if shutdown
835 >     */
836 >    public void testRejectedExecutionExceptionOnShutdown() {
837 >        ThreadPoolExecutor tpe =
838 >            new ThreadPoolExecutor(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
839          tpe.shutdown();
840          try {
841              tpe.execute(new NoOpRunnable());
# Line 589 | Line 846 | public class ThreadPoolExecutorTest exte
846      }
847  
848      /**
849 <     *  execute throws RejectedExecutionException
593 <     *  if saturated.
849 >     *  execute using CallerRunsPolicy drops task on shutdown
850       */
851 <    public void testSaturatedExecute() {
852 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
853 <        try {
854 <            
855 <            for(int i = 0; i < 5; ++i){
856 <                p.execute(new MediumRunnable());
857 <            }
858 <            shouldThrow();
859 <        } catch(RejectedExecutionException success){}
860 <        joinPool(p);
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,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
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){}
# Line 619 | Line 919 | public class ThreadPoolExecutorTest exte
919      }
920      
921      /**
922 <     *  setCorePoolSize of netaitev value throws IllegalArgumentException
922 >     *  setCorePoolSize of negative value throws IllegalArgumentException
923       */
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));
927 >            tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
928          } catch(Exception e){}
929          try {
930              tpe.setCorePoolSize(-1);
# Line 643 | Line 943 | public class ThreadPoolExecutorTest exte
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));
946 >            tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
947          } catch(Exception e){}
948          try {
949              tpe.setMaximumPoolSize(1);
# Line 662 | Line 962 | public class ThreadPoolExecutorTest exte
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));
965 >            tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
966          } catch(Exception e){}
967          try {
968              tpe.setMaximumPoolSize(-1);
# Line 682 | Line 982 | public class ThreadPoolExecutorTest exte
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));
985 >            tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
986          } catch(Exception e){}
987          
988          try {
# Line 694 | Line 994 | public class ThreadPoolExecutorTest exte
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