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.16 by dl, Sun Dec 28 21:56:18 2003 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
2 > * Written by Doug Lea with assistance from members of JCP JSR-166
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/licenses/publicdomain
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import java.util.concurrent.*;
10   import junit.framework.*;
11 < import java.util.List;
11 > import java.util.*;
12  
13   public class ThreadPoolExecutorTest extends JSR166TestCase {
14      public static void main(String[] args) {
# Line 17 | Line 18 | public class ThreadPoolExecutorTest exte
18          return new TestSuite(ThreadPoolExecutorTest.class);
19      }
20      
21 <    /**
22 <     * For use as ThreadFactory in constructors
23 <     */
24 <    static class MyThreadFactory implements ThreadFactory{
25 <        public Thread newThread(Runnable r){
26 <            return new Thread(r);
27 <        }  
21 >    static class ExtendedTPE extends ThreadPoolExecutor {
22 >        volatile boolean beforeCalled = false;
23 >        volatile boolean afterCalled = false;
24 >        volatile boolean terminatedCalled = false;
25 >        public ExtendedTPE() {
26 >            super(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>());
27 >        }
28 >        protected void beforeExecute(Thread t, Runnable r) {
29 >            beforeCalled = true;
30 >        }
31 >        protected void afterExecute(Runnable r, Throwable t) {
32 >            afterCalled = true;
33 >        }
34 >        protected void terminated() {
35 >            terminatedCalled = true;
36 >        }
37      }
38  
39      /**
40 <     * 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
40 >     *  execute successfully executes a runnable
41       */
42      public void testExecute() {
43 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
43 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
44          try {
45              p1.execute(new Runnable() {
46                      public void run() {
# Line 60 | Line 63 | public class ThreadPoolExecutorTest exte
63       *  thread becomes active
64       */
65      public void testGetActiveCount() {
66 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
66 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
67          assertEquals(0, p2.getActiveCount());
68          p2.execute(new MediumRunnable());
69          try {
# Line 71 | Line 74 | public class ThreadPoolExecutorTest exte
74          assertEquals(1, p2.getActiveCount());
75          joinPool(p2);
76      }
77 +
78 +    /**
79 +     *  prestartCoreThread starts a thread if under corePoolSize, else doesn't
80 +     */
81 +    public void testPrestartCoreThread() {
82 +        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
83 +        assertEquals(0, p2.getPoolSize());
84 +        assertTrue(p2.prestartCoreThread());
85 +        assertEquals(1, p2.getPoolSize());
86 +        assertTrue(p2.prestartCoreThread());
87 +        assertEquals(2, p2.getPoolSize());
88 +        assertFalse(p2.prestartCoreThread());
89 +        assertEquals(2, p2.getPoolSize());
90 +        joinPool(p2);
91 +    }
92 +
93 +    /**
94 +     *  prestartAllCoreThreads starts all corePoolSize threads
95 +     */
96 +    public void testPrestartAllCoreThreads() {
97 +        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
98 +        assertEquals(0, p2.getPoolSize());
99 +        p2.prestartAllCoreThreads();
100 +        assertEquals(2, p2.getPoolSize());
101 +        p2.prestartAllCoreThreads();
102 +        assertEquals(2, p2.getPoolSize());
103 +        joinPool(p2);
104 +    }
105      
106      /**
107       *   getCompletedTaskCount increases, but doesn't overestimate,
108       *   when tasks complete
109       */
110      public void testGetCompletedTaskCount() {
111 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
111 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
112          assertEquals(0, p2.getCompletedTaskCount());
113          p2.execute(new ShortRunnable());
114          try {
115 <            Thread.sleep(MEDIUM_DELAY_MS);
115 >            Thread.sleep(SMALL_DELAY_MS);
116          } catch(Exception e){
117              unexpectedException();
118          }
# Line 94 | Line 125 | public class ThreadPoolExecutorTest exte
125       *   getCorePoolSize returns size given in constructor if not otherwise set
126       */
127      public void testGetCorePoolSize() {
128 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
128 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
129          assertEquals(1, p1.getCorePoolSize());
130          joinPool(p1);
131      }
# Line 107 | Line 138 | public class ThreadPoolExecutorTest exte
138          assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS));
139          joinPool(p2);
140      }
141 +
142 +
143 +    /**
144 +     * getThreadFactory returns factory in constructor if not set
145 +     */
146 +    public void testGetThreadFactory() {
147 +        ThreadFactory tf = new SimpleThreadFactory();
148 +        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
149 +        assertSame(tf, p.getThreadFactory());
150 +        p.shutdown();
151 +        joinPool(p);
152 +    }
153 +
154 +    /**
155 +     * setThreadFactory sets the thread factory returned by getThreadFactory
156 +     */
157 +    public void testSetThreadFactory() {
158 +        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
159 +        ThreadFactory tf = new SimpleThreadFactory();
160 +        p.setThreadFactory(tf);
161 +        assertSame(tf, p.getThreadFactory());
162 +        p.shutdown();
163 +        joinPool(p);
164 +    }
165 +
166 +
167 +    /**
168 +     * setThreadFactory(null) throws NPE
169 +     */
170 +    public void testSetThreadFactoryNull() {
171 +        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
172 +        try {
173 +            p.setThreadFactory(null);
174 +            shouldThrow();
175 +        } catch (NullPointerException success) {
176 +        } finally {
177 +            joinPool(p);
178 +        }
179 +    }
180 +
181 +    /**
182 +     * getRejectedExecutionHandler returns handler in constructor if not set
183 +     */
184 +    public void testGetRejectedExecutionHandler() {
185 +        RejectedExecutionHandler h = new NoOpREHandler();
186 +        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
187 +        assertSame(h, p.getRejectedExecutionHandler());
188 +        p.shutdown();
189 +        joinPool(p);
190 +    }
191 +
192 +    /**
193 +     * setRejectedExecutionHandler sets the handler returned by
194 +     * getRejectedExecutionHandler
195 +     */
196 +    public void testSetRejectedExecutionHandler() {
197 +        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
198 +        RejectedExecutionHandler h = new NoOpREHandler();
199 +        p.setRejectedExecutionHandler(h);
200 +        assertSame(h, p.getRejectedExecutionHandler());
201 +        p.shutdown();
202 +        joinPool(p);
203 +    }
204 +
205 +
206 +    /**
207 +     * setRejectedExecutionHandler(null) throws NPE
208 +     */
209 +    public void testSetRejectedExecutionHandlerNull() {
210 +        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
211 +        try {
212 +            p.setRejectedExecutionHandler(null);
213 +            shouldThrow();
214 +        } catch (NullPointerException success) {
215 +        } finally {
216 +            joinPool(p);
217 +        }
218 +    }
219 +
220      
221      /**
222       *   getLargestPoolSize increases, but doesn't overestimate, when
223       *   multiple threads active
224       */
225      public void testGetLargestPoolSize() {
226 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
226 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
227          try {
228              assertEquals(0, p2.getLargestPoolSize());
229              p2.execute(new MediumRunnable());
# Line 131 | Line 241 | public class ThreadPoolExecutorTest exte
241       *   otherwise set
242       */
243      public void testGetMaximumPoolSize() {
244 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
244 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
245          assertEquals(2, p2.getMaximumPoolSize());
246          joinPool(p2);
247      }
# Line 141 | Line 251 | public class ThreadPoolExecutorTest exte
251       *   become active
252       */
253      public void testGetPoolSize() {
254 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
254 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
255          assertEquals(0, p1.getPoolSize());
256          p1.execute(new MediumRunnable());
257          assertEquals(1, p1.getPoolSize());
# Line 149 | Line 259 | public class ThreadPoolExecutorTest exte
259      }
260      
261      /**
262 <     *   getTaskCount increases, but doesn't overestimate, when tasks submitted
262 >     *  getTaskCount increases, but doesn't overestimate, when tasks submitted
263       */
264      public void testGetTaskCount() {
265 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
265 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
266          try {
267              assertEquals(0, p1.getTaskCount());
268              p1.execute(new MediumRunnable());
# Line 169 | Line 279 | public class ThreadPoolExecutorTest exte
279       */
280      public void testIsShutdown() {
281          
282 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
282 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
283          assertFalse(p1.isShutdown());
284          p1.shutdown();
285          assertTrue(p1.isShutdown());
# Line 181 | Line 291 | public class ThreadPoolExecutorTest exte
291       *  isTerminated is false before termination, true after
292       */
293      public void testIsTerminated() {
294 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
294 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
295          assertFalse(p1.isTerminated());
296          try {
297              p1.execute(new MediumRunnable());
# Line 200 | Line 310 | public class ThreadPoolExecutorTest exte
310       *  isTerminating is not true when running or when terminated
311       */
312      public void testIsTerminating() {
313 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
313 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
314          assertFalse(p1.isTerminating());
315          try {
316              p1.execute(new SmallRunnable());
# Line 218 | Line 328 | public class ThreadPoolExecutorTest exte
328      }
329  
330      /**
331 +     * getQueue returns the work queue, which contains queued tasks
332 +     */
333 +    public void testGetQueue() {
334 +        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
335 +        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q);
336 +        FutureTask[] tasks = new FutureTask[5];
337 +        for(int i = 0; i < 5; i++){
338 +            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
339 +            p1.execute(tasks[i]);
340 +        }
341 +        try {
342 +            Thread.sleep(SHORT_DELAY_MS);
343 +            BlockingQueue<Runnable> wq = p1.getQueue();
344 +            assertSame(q, wq);
345 +            assertFalse(wq.contains(tasks[0]));
346 +            assertTrue(wq.contains(tasks[4]));
347 +            p1.shutdownNow();
348 +        } catch(Exception e) {
349 +            unexpectedException();
350 +        } finally {
351 +            joinPool(p1);
352 +        }
353 +    }
354 +
355 +    /**
356 +     * remove(task) removes queued task, and fails to remove active task
357 +     */
358 +    public void testRemove() {
359 +        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
360 +        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q);
361 +        FutureTask[] tasks = new FutureTask[5];
362 +        for(int i = 0; i < 5; i++){
363 +            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
364 +            p1.execute(tasks[i]);
365 +        }
366 +        try {
367 +            Thread.sleep(SHORT_DELAY_MS);
368 +            assertFalse(p1.remove(tasks[0]));
369 +            assertTrue(q.contains(tasks[4]));
370 +            assertTrue(q.contains(tasks[3]));
371 +            assertTrue(p1.remove(tasks[4]));
372 +            assertFalse(p1.remove(tasks[4]));
373 +            assertFalse(q.contains(tasks[4]));
374 +            assertTrue(q.contains(tasks[3]));
375 +            assertTrue(p1.remove(tasks[3]));
376 +            assertFalse(q.contains(tasks[3]));
377 +            p1.shutdownNow();
378 +        } catch(Exception e) {
379 +            unexpectedException();
380 +        } finally {
381 +            joinPool(p1);
382 +        }
383 +    }
384 +
385 +    /**
386       *   purge removes cancelled tasks from the queue
387       */
388      public void testPurge() {
389 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
390 <        CancellableTask[] tasks = new CancellableTask[5];
389 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
390 >        FutureTask[] tasks = new FutureTask[5];
391          for(int i = 0; i < 5; i++){
392 <            tasks[i] = new CancellableTask(new MediumPossiblyInterruptedRunnable());
392 >            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
393              p1.execute(tasks[i]);
394          }
395          tasks[4].cancel(true);
# Line 232 | Line 397 | public class ThreadPoolExecutorTest exte
397          p1.purge();
398          long count = p1.getTaskCount();
399          assertTrue(count >= 2 && count < 5);
400 +        p1.shutdownNow();
401          joinPool(p1);
402      }
403  
# Line 239 | Line 405 | public class ThreadPoolExecutorTest exte
405       *  shutDownNow returns a list containing tasks that were not run
406       */
407      public void testShutDownNow() {
408 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
408 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
409          List l;
410          try {
411              for(int i = 0; i < 5; i++)
# Line 313 | Line 479 | public class ThreadPoolExecutorTest exte
479      /**
480       * Constructor throws if workQueue is set to null
481       */
482 <    public void testNullPointerException() {
482 >    public void testConstructorNullPointerException() {
483          try {
484 <            new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null);
484 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null);
485              shouldThrow();
486          }
487          catch (NullPointerException success){}  
# Line 328 | Line 494 | public class ThreadPoolExecutorTest exte
494       */
495      public void testConstructor6() {
496          try {
497 <            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory());
497 >            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
498              shouldThrow();
499          } catch (IllegalArgumentException success){}
500      }
# Line 338 | Line 504 | public class ThreadPoolExecutorTest exte
504       */
505      public void testConstructor7() {
506          try {
507 <            new ThreadPoolExecutor(1,-1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory());
507 >            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
508              shouldThrow();
509          }
510          catch (IllegalArgumentException success){}
# Line 349 | Line 515 | public class ThreadPoolExecutorTest exte
515       */
516      public void testConstructor8() {
517          try {
518 <            new ThreadPoolExecutor(1,0,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory());
518 >            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
519              shouldThrow();
520          }
521          catch (IllegalArgumentException success){}
# Line 360 | Line 526 | public class ThreadPoolExecutorTest exte
526       */
527      public void testConstructor9() {
528          try {
529 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory());
529 >            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
530              shouldThrow();
531          }
532          catch (IllegalArgumentException success){}
# Line 371 | Line 537 | public class ThreadPoolExecutorTest exte
537       */
538      public void testConstructor10() {
539          try {
540 <            new ThreadPoolExecutor(2,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory());
540 >            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
541              shouldThrow();
542          }
543          catch (IllegalArgumentException success){}
# Line 380 | Line 546 | public class ThreadPoolExecutorTest exte
546      /**
547       * Constructor throws if workQueue is set to null
548       */
549 <    public void testNullPointerException2() {
549 >    public void testConstructorNullPointerException2() {
550          try {
551 <            new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null,new MyThreadFactory());
551 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory());
552              shouldThrow();
553          }
554          catch (NullPointerException success){}  
# Line 391 | Line 557 | public class ThreadPoolExecutorTest exte
557      /**
558       * Constructor throws if threadFactory is set to null
559       */
560 <    public void testNullPointerException3() {
560 >    public void testConstructorNullPointerException3() {
561          try {
562              ThreadFactory f = null;
563 <            new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
563 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
564              shouldThrow();
565          }
566          catch (NullPointerException success){}  
# Line 406 | Line 572 | public class ThreadPoolExecutorTest exte
572       */
573      public void testConstructor11() {
574          try {
575 <            new ThreadPoolExecutor(-1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler());
575 >            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
576              shouldThrow();
577          }
578          catch (IllegalArgumentException success){}
# Line 417 | Line 583 | public class ThreadPoolExecutorTest exte
583       */
584      public void testConstructor12() {
585          try {
586 <            new ThreadPoolExecutor(1,-1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler());
586 >            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
587              shouldThrow();
588          }
589          catch (IllegalArgumentException success){}
# Line 428 | Line 594 | public class ThreadPoolExecutorTest exte
594       */
595      public void testConstructor13() {
596          try {
597 <            new ThreadPoolExecutor(1,0,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler());
597 >            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
598              shouldThrow();
599          }
600          catch (IllegalArgumentException success){}
# Line 439 | Line 605 | public class ThreadPoolExecutorTest exte
605       */
606      public void testConstructor14() {
607          try {
608 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler());
608 >            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
609              shouldThrow();
610          }
611          catch (IllegalArgumentException success){}
# Line 450 | Line 616 | public class ThreadPoolExecutorTest exte
616       */
617      public void testConstructor15() {
618          try {
619 <            new ThreadPoolExecutor(2,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler());
619 >            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
620              shouldThrow();
621          }
622          catch (IllegalArgumentException success){}
# Line 459 | Line 625 | public class ThreadPoolExecutorTest exte
625      /**
626       * Constructor throws if workQueue is set to null
627       */
628 <    public void testNullPointerException4() {
628 >    public void testConstructorNullPointerException4() {
629          try {
630 <            new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null,new MyREHandler());
630 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler());
631              shouldThrow();
632          }
633          catch (NullPointerException success){}  
# Line 470 | Line 636 | public class ThreadPoolExecutorTest exte
636      /**
637       * Constructor throws if handler is set to null
638       */
639 <    public void testNullPointerException5() {
639 >    public void testConstructorNullPointerException5() {
640          try {
641              RejectedExecutionHandler r = null;
642 <            new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
642 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
643              shouldThrow();
644          }
645          catch (NullPointerException success){}  
# Line 485 | Line 651 | public class ThreadPoolExecutorTest exte
651       */
652      public void testConstructor16() {
653          try {
654 <            new ThreadPoolExecutor(-1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),new MyREHandler());
654 >            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
655              shouldThrow();
656          }
657          catch (IllegalArgumentException success){}
# Line 496 | Line 662 | public class ThreadPoolExecutorTest exte
662       */
663      public void testConstructor17() {
664          try {
665 <            new ThreadPoolExecutor(1,-1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),new MyREHandler());
665 >            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
666              shouldThrow();
667          }
668          catch (IllegalArgumentException success){}
# Line 507 | Line 673 | public class ThreadPoolExecutorTest exte
673       */
674      public void testConstructor18() {
675          try {
676 <            new ThreadPoolExecutor(1,0,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),new MyREHandler());
676 >            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
677              shouldThrow();
678          }
679          catch (IllegalArgumentException success){}
# Line 518 | Line 684 | public class ThreadPoolExecutorTest exte
684       */
685      public void testConstructor19() {
686          try {
687 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),new MyREHandler());
687 >            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
688              shouldThrow();
689          }
690          catch (IllegalArgumentException success){}
# Line 529 | Line 695 | public class ThreadPoolExecutorTest exte
695       */
696      public void testConstructor20() {
697          try {
698 <            new ThreadPoolExecutor(2,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),new MyREHandler());
698 >            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
699              shouldThrow();
700          }
701          catch (IllegalArgumentException success){}
# Line 538 | Line 704 | public class ThreadPoolExecutorTest exte
704      /**
705       * Constructor throws if workQueue is set to null
706       */
707 <    public void testNullPointerException6() {
707 >    public void testConstructorNullPointerException6() {
708          try {
709 <            new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null,new MyThreadFactory(),new MyREHandler());
709 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
710              shouldThrow();
711          }
712          catch (NullPointerException success){}  
# Line 549 | Line 715 | public class ThreadPoolExecutorTest exte
715      /**
716       * Constructor throws if handler is set to null
717       */
718 <    public void testNullPointerException7() {
718 >    public void testConstructorNullPointerException7() {
719          try {
720              RejectedExecutionHandler r = null;
721 <            new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),r);
721 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
722              shouldThrow();
723          }
724          catch (NullPointerException success){}  
# Line 561 | Line 727 | public class ThreadPoolExecutorTest exte
727      /**
728       * Constructor throws if ThreadFactory is set top null
729       */
730 <    public void testNullPointerException8() {
730 >    public void testConstructorNullPointerException8() {
731          try {
732              ThreadFactory f = null;
733 <            new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new MyREHandler());
733 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
734              shouldThrow();
735          }
736          catch (NullPointerException successdn8){}  
# Line 572 | Line 738 | public class ThreadPoolExecutorTest exte
738      
739  
740      /**
741 <     *  execute throws RejectedExecutionException if shutdown
741 >     *  execute throws RejectedExecutionException
742 >     *  if saturated.
743       */
744 <    public void testRejectedExecutionException() {
745 <        ThreadPoolExecutor tpe = null;
744 >    public void testSaturatedExecute() {
745 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
746          try {
747 <            tpe = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
748 <        } catch(Exception e){}
747 >            
748 >            for(int i = 0; i < 5; ++i){
749 >                p.execute(new MediumRunnable());
750 >            }
751 >            shouldThrow();
752 >        } catch(RejectedExecutionException success){}
753 >        joinPool(p);
754 >    }
755 >
756 >    /**
757 >     *  executor using CallerRunsPolicy runs task if saturated.
758 >     */
759 >    public void testSaturatedExecute2() {
760 >        RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
761 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
762 >        try {
763 >            
764 >            TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
765 >            for(int i = 0; i < 5; ++i){
766 >                tasks[i] = new TrackedNoOpRunnable();
767 >            }
768 >            TrackedLongRunnable mr = new TrackedLongRunnable();
769 >            p.execute(mr);
770 >            for(int i = 0; i < 5; ++i){
771 >                p.execute(tasks[i]);
772 >            }
773 >            for(int i = 1; i < 5; ++i) {
774 >                assertTrue(tasks[i].done);
775 >            }
776 >            p.shutdownNow();
777 >        } catch(RejectedExecutionException ex){
778 >            unexpectedException();
779 >        } finally {
780 >            joinPool(p);
781 >        }
782 >    }
783 >
784 >    /**
785 >     *  executor using DiscardPolicy drops task if saturated.
786 >     */
787 >    public void testSaturatedExecute3() {
788 >        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
789 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
790 >        try {
791 >            
792 >            TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
793 >            for(int i = 0; i < 5; ++i){
794 >                tasks[i] = new TrackedNoOpRunnable();
795 >            }
796 >            p.execute(new TrackedLongRunnable());
797 >            for(int i = 0; i < 5; ++i){
798 >                p.execute(tasks[i]);
799 >            }
800 >            for(int i = 0; i < 5; ++i){
801 >                assertFalse(tasks[i].done);
802 >            }
803 >            p.shutdownNow();
804 >        } catch(RejectedExecutionException ex){
805 >            unexpectedException();
806 >        } finally {
807 >            joinPool(p);
808 >        }
809 >    }
810 >
811 >    /**
812 >     *  executor using DiscardOldestPolicy drops oldest task if saturated.
813 >     */
814 >    public void testSaturatedExecute4() {
815 >        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
816 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
817 >        try {
818 >            p.execute(new TrackedLongRunnable());
819 >            TrackedLongRunnable r2 = new TrackedLongRunnable();
820 >            p.execute(r2);
821 >            assertTrue(p.getQueue().contains(r2));
822 >            TrackedNoOpRunnable r3 = new TrackedNoOpRunnable();
823 >            p.execute(r3);
824 >            assertFalse(p.getQueue().contains(r2));
825 >            assertTrue(p.getQueue().contains(r3));
826 >            p.shutdownNow();
827 >        } catch(RejectedExecutionException ex){
828 >            unexpectedException();
829 >        } finally {
830 >            joinPool(p);
831 >        }
832 >    }
833 >
834 >    /**
835 >     *  execute throws RejectedExecutionException if shutdown
836 >     */
837 >    public void testRejectedExecutionExceptionOnShutdown() {
838 >        ThreadPoolExecutor tpe =
839 >            new ThreadPoolExecutor(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
840          tpe.shutdown();
841          try {
842              tpe.execute(new NoOpRunnable());
# Line 589 | Line 847 | public class ThreadPoolExecutorTest exte
847      }
848  
849      /**
850 <     *  execute throws RejectedExecutionException
593 <     *  if saturated.
850 >     *  execute using CallerRunsPolicy drops task on shutdown
851       */
852 <    public void testSaturatedExecute() {
853 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
854 <        try {
855 <            
856 <            for(int i = 0; i < 5; ++i){
857 <                p.execute(new MediumRunnable());
858 <            }
859 <            shouldThrow();
860 <        } catch(RejectedExecutionException success){}
861 <        joinPool(p);
852 >    public void testCallerRunsOnShutdown() {
853 >        RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
854 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
855 >
856 >        p.shutdown();
857 >        try {
858 >            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
859 >            p.execute(r);
860 >            assertFalse(r.done);
861 >        } catch(RejectedExecutionException success){
862 >            unexpectedException();
863 >        } finally {
864 >            joinPool(p);
865 >        }
866 >    }
867 >
868 >    /**
869 >     *  execute using DiscardPolicy drops task on shutdown
870 >     */
871 >    public void testDiscardOnShutdown() {
872 >        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
873 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
874 >
875 >        p.shutdown();
876 >        try {
877 >            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
878 >            p.execute(r);
879 >            assertFalse(r.done);
880 >        } catch(RejectedExecutionException success){
881 >            unexpectedException();
882 >        } finally {
883 >            joinPool(p);
884 >        }
885 >    }
886 >
887 >
888 >    /**
889 >     *  execute using DiscardOldestPolicy drops task on shutdown
890 >     */
891 >    public void testDiscardOldestOnShutdown() {
892 >        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
893 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
894 >
895 >        p.shutdown();
896 >        try {
897 >            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
898 >            p.execute(r);
899 >            assertFalse(r.done);
900 >        } catch(RejectedExecutionException success){
901 >            unexpectedException();
902 >        } finally {
903 >            joinPool(p);
904 >        }
905      }
906  
907 +
908      /**
909       *  execute (null) throws NPE
910       */
911      public void testExecuteNull() {
912          ThreadPoolExecutor tpe = null;
913          try {
914 <            tpe = new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
914 >            tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
915              tpe.execute(null);
916              shouldThrow();
917          } catch(NullPointerException success){}
# Line 619 | Line 920 | public class ThreadPoolExecutorTest exte
920      }
921      
922      /**
923 <     *  setCorePoolSize of netaitev value throws IllegalArgumentException
923 >     *  setCorePoolSize of negative value throws IllegalArgumentException
924       */
925      public void testCorePoolSizeIllegalArgumentException() {
926          ThreadPoolExecutor tpe = null;
927          try {
928 <            tpe = new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
928 >            tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
929          } catch(Exception e){}
930          try {
931              tpe.setCorePoolSize(-1);
# Line 643 | Line 944 | public class ThreadPoolExecutorTest exte
944      public void testMaximumPoolSizeIllegalArgumentException() {
945          ThreadPoolExecutor tpe = null;
946          try {
947 <            tpe = new ThreadPoolExecutor(2,3,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
947 >            tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
948          } catch(Exception e){}
949          try {
950              tpe.setMaximumPoolSize(1);
# Line 662 | Line 963 | public class ThreadPoolExecutorTest exte
963      public void testMaximumPoolSizeIllegalArgumentException2() {
964          ThreadPoolExecutor tpe = null;
965          try {
966 <            tpe = new ThreadPoolExecutor(2,3,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
966 >            tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
967          } catch(Exception e){}
968          try {
969              tpe.setMaximumPoolSize(-1);
# Line 682 | Line 983 | public class ThreadPoolExecutorTest exte
983      public void testKeepAliveTimeIllegalArgumentException() {
984          ThreadPoolExecutor tpe = null;
985          try {
986 <            tpe = new ThreadPoolExecutor(2,3,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
986 >            tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
987          } catch(Exception e){}
988          
989          try {
# Line 694 | Line 995 | public class ThreadPoolExecutorTest exte
995          }
996          joinPool(tpe);
997      }
998 <  
998 >
999 >    /**
1000 >     * terminated() is called on termination
1001 >     */
1002 >    public void testTerminated() {
1003 >        ExtendedTPE tpe = new ExtendedTPE();
1004 >        tpe.shutdown();
1005 >        assertTrue(tpe.terminatedCalled);
1006 >        joinPool(tpe);
1007 >    }
1008 >
1009 >    /**
1010 >     * beforeExecute and afterExecute are called when executing task
1011 >     */
1012 >    public void testBeforeAfter() {
1013 >        ExtendedTPE tpe = new ExtendedTPE();
1014 >        try {
1015 >            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1016 >            tpe.execute(r);
1017 >            Thread.sleep(SHORT_DELAY_MS);
1018 >            assertTrue(r.done);
1019 >            assertTrue(tpe.beforeCalled);
1020 >            assertTrue(tpe.afterCalled);
1021 >            tpe.shutdown();
1022 >        }
1023 >        catch(Exception ex) {
1024 >            unexpectedException();
1025 >        } finally {
1026 >            joinPool(tpe);
1027 >        }
1028 >    }
1029 >
1030 >    /**
1031 >     * completed submit of callable returns result
1032 >     */
1033 >    public void testSubmitCallable() {
1034 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1035 >        try {
1036 >            Future<String> future = e.submit(new StringTask());
1037 >            String result = future.get();
1038 >            assertSame(TEST_STRING, result);
1039 >        }
1040 >        catch (ExecutionException ex) {
1041 >            unexpectedException();
1042 >        }
1043 >        catch (InterruptedException ex) {
1044 >            unexpectedException();
1045 >        } finally {
1046 >            joinPool(e);
1047 >        }
1048 >    }
1049 >
1050 >    /**
1051 >     * completed submit of runnable returns successfully
1052 >     */
1053 >    public void testSubmitRunnable() {
1054 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1055 >        try {
1056 >            Future<?> future = e.submit(new NoOpRunnable());
1057 >            future.get();
1058 >            assertTrue(future.isDone());
1059 >        }
1060 >        catch (ExecutionException ex) {
1061 >            unexpectedException();
1062 >        }
1063 >        catch (InterruptedException ex) {
1064 >            unexpectedException();
1065 >        } finally {
1066 >            joinPool(e);
1067 >        }
1068 >    }
1069 >
1070 >    /**
1071 >     * completed submit of (runnable, result) returns result
1072 >     */
1073 >    public void testSubmitRunnable2() {
1074 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1075 >        try {
1076 >            Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1077 >            String result = future.get();
1078 >            assertSame(TEST_STRING, result);
1079 >        }
1080 >        catch (ExecutionException ex) {
1081 >            unexpectedException();
1082 >        }
1083 >        catch (InterruptedException ex) {
1084 >            unexpectedException();
1085 >        } finally {
1086 >            joinPool(e);
1087 >        }
1088 >    }
1089 >
1090 >
1091 >
1092 >
1093 >
1094 >    /**
1095 >     * invokeAny(null) throws NPE
1096 >     */
1097 >    public void testInvokeAny1() {
1098 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1099 >        try {
1100 >            e.invokeAny(null);
1101 >        } catch (NullPointerException success) {
1102 >        } catch(Exception ex) {
1103 >            unexpectedException();
1104 >        } finally {
1105 >            joinPool(e);
1106 >        }
1107 >    }
1108 >
1109 >    /**
1110 >     * invokeAny(empty collection) throws IAE
1111 >     */
1112 >    public void testInvokeAny2() {
1113 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1114 >        try {
1115 >            e.invokeAny(new ArrayList<Callable<String>>());
1116 >        } catch (IllegalArgumentException success) {
1117 >        } catch(Exception ex) {
1118 >            unexpectedException();
1119 >        } finally {
1120 >            joinPool(e);
1121 >        }
1122 >    }
1123 >
1124 >    /**
1125 >     * invokeAny(c) throws NPE if c has null elements
1126 >     */
1127 >    public void testInvokeAny3() {
1128 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1129 >        try {
1130 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1131 >            l.add(new StringTask());
1132 >            l.add(null);
1133 >            e.invokeAny(l);
1134 >        } catch (NullPointerException success) {
1135 >        } catch(Exception ex) {
1136 >            unexpectedException();
1137 >        } finally {
1138 >            joinPool(e);
1139 >        }
1140 >    }
1141 >
1142 >    /**
1143 >     * invokeAny(c) throws ExecutionException if no task completes
1144 >     */
1145 >    public void testInvokeAny4() {
1146 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1147 >        try {
1148 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1149 >            l.add(new NPETask());
1150 >            e.invokeAny(l);
1151 >        } catch (ExecutionException success) {
1152 >        } catch(Exception ex) {
1153 >            unexpectedException();
1154 >        } finally {
1155 >            joinPool(e);
1156 >        }
1157 >    }
1158 >
1159 >    /**
1160 >     * invokeAny(c) returns result of some task
1161 >     */
1162 >    public void testInvokeAny5() {
1163 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1164 >        try {
1165 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1166 >            l.add(new StringTask());
1167 >            l.add(new StringTask());
1168 >            String result = e.invokeAny(l);
1169 >            assertSame(TEST_STRING, result);
1170 >        } catch (ExecutionException success) {
1171 >        } catch(Exception ex) {
1172 >            unexpectedException();
1173 >        } finally {
1174 >            joinPool(e);
1175 >        }
1176 >    }
1177 >
1178 >    /**
1179 >     * invokeAll(null) throws NPE
1180 >     */
1181 >    public void testInvokeAll1() {
1182 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1183 >        try {
1184 >            e.invokeAll(null);
1185 >        } catch (NullPointerException success) {
1186 >        } catch(Exception ex) {
1187 >            unexpectedException();
1188 >        } finally {
1189 >            joinPool(e);
1190 >        }
1191 >    }
1192 >
1193 >    /**
1194 >     * invokeAll(empty collection) returns empty collection
1195 >     */
1196 >    public void testInvokeAll2() {
1197 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1198 >        try {
1199 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1200 >            assertTrue(r.isEmpty());
1201 >        } catch(Exception ex) {
1202 >            unexpectedException();
1203 >        } finally {
1204 >            joinPool(e);
1205 >        }
1206 >    }
1207 >
1208 >    /**
1209 >     * invokeAll(c) throws NPE if c has null elements
1210 >     */
1211 >    public void testInvokeAll3() {
1212 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1213 >        try {
1214 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1215 >            l.add(new StringTask());
1216 >            l.add(null);
1217 >            e.invokeAll(l);
1218 >        } catch (NullPointerException success) {
1219 >        } catch(Exception ex) {
1220 >            unexpectedException();
1221 >        } finally {
1222 >            joinPool(e);
1223 >        }
1224 >    }
1225 >
1226 >    /**
1227 >     * get of element of invokeAll(c) throws exception on failed task
1228 >     */
1229 >    public void testInvokeAll4() {
1230 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1231 >        try {
1232 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1233 >            l.add(new NPETask());
1234 >            List<Future<String>> result = e.invokeAll(l);
1235 >            assertEquals(1, result.size());
1236 >            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1237 >                it.next().get();
1238 >        } catch(ExecutionException success) {
1239 >        } catch(Exception ex) {
1240 >            unexpectedException();
1241 >        } finally {
1242 >            joinPool(e);
1243 >        }
1244 >    }
1245 >
1246 >    /**
1247 >     * invokeAll(c) returns results of all completed tasks
1248 >     */
1249 >    public void testInvokeAll5() {
1250 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1251 >        try {
1252 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1253 >            l.add(new StringTask());
1254 >            l.add(new StringTask());
1255 >            List<Future<String>> result = e.invokeAll(l);
1256 >            assertEquals(2, result.size());
1257 >            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1258 >                assertSame(TEST_STRING, it.next().get());
1259 >        } catch (ExecutionException success) {
1260 >        } catch(Exception ex) {
1261 >            unexpectedException();
1262 >        } finally {
1263 >            joinPool(e);
1264 >        }
1265 >    }
1266 >
1267 >
1268 >
1269 >    /**
1270 >     * timed invokeAny(null) throws NPE
1271 >     */
1272 >    public void testTimedInvokeAny1() {
1273 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1274 >        try {
1275 >            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1276 >        } catch (NullPointerException success) {
1277 >        } catch(Exception ex) {
1278 >            unexpectedException();
1279 >        } finally {
1280 >            joinPool(e);
1281 >        }
1282 >    }
1283 >
1284 >    /**
1285 >     * timed invokeAny(,,null) throws NPE
1286 >     */
1287 >    public void testTimedInvokeAnyNullTimeUnit() {
1288 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1289 >        try {
1290 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1291 >            l.add(new StringTask());
1292 >            e.invokeAny(l, MEDIUM_DELAY_MS, null);
1293 >        } catch (NullPointerException success) {
1294 >        } catch(Exception ex) {
1295 >            unexpectedException();
1296 >        } finally {
1297 >            joinPool(e);
1298 >        }
1299 >    }
1300 >
1301 >    /**
1302 >     * timed invokeAny(empty collection) throws IAE
1303 >     */
1304 >    public void testTimedInvokeAny2() {
1305 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1306 >        try {
1307 >            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1308 >        } catch (IllegalArgumentException success) {
1309 >        } catch(Exception ex) {
1310 >            unexpectedException();
1311 >        } finally {
1312 >            joinPool(e);
1313 >        }
1314 >    }
1315 >
1316 >    /**
1317 >     * timed invokeAny(c) throws NPE if c has null elements
1318 >     */
1319 >    public void testTimedInvokeAny3() {
1320 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1321 >        try {
1322 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1323 >            l.add(new StringTask());
1324 >            l.add(null);
1325 >            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1326 >        } catch (NullPointerException success) {
1327 >        } catch(Exception ex) {
1328 >            ex.printStackTrace();
1329 >            unexpectedException();
1330 >        } finally {
1331 >            joinPool(e);
1332 >        }
1333 >    }
1334 >
1335 >    /**
1336 >     * timed invokeAny(c) throws ExecutionException if no task completes
1337 >     */
1338 >    public void testTimedInvokeAny4() {
1339 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1340 >        try {
1341 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1342 >            l.add(new NPETask());
1343 >            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1344 >        } catch(ExecutionException success) {
1345 >        } catch(Exception ex) {
1346 >            unexpectedException();
1347 >        } finally {
1348 >            joinPool(e);
1349 >        }
1350 >    }
1351 >
1352 >    /**
1353 >     * timed invokeAny(c) returns result of some task
1354 >     */
1355 >    public void testTimedInvokeAny5() {
1356 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1357 >        try {
1358 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1359 >            l.add(new StringTask());
1360 >            l.add(new StringTask());
1361 >            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1362 >            assertSame(TEST_STRING, result);
1363 >        } catch (ExecutionException success) {
1364 >        } catch(Exception ex) {
1365 >            unexpectedException();
1366 >        } finally {
1367 >            joinPool(e);
1368 >        }
1369 >    }
1370 >
1371 >    /**
1372 >     * timed invokeAll(null) throws NPE
1373 >     */
1374 >    public void testTimedInvokeAll1() {
1375 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1376 >        try {
1377 >            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1378 >        } catch (NullPointerException success) {
1379 >        } catch(Exception ex) {
1380 >            unexpectedException();
1381 >        } finally {
1382 >            joinPool(e);
1383 >        }
1384 >    }
1385 >
1386 >    /**
1387 >     * timed invokeAll(,,null) throws NPE
1388 >     */
1389 >    public void testTimedInvokeAllNullTimeUnit() {
1390 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1391 >        try {
1392 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1393 >            l.add(new StringTask());
1394 >            e.invokeAll(l, MEDIUM_DELAY_MS, null);
1395 >        } catch (NullPointerException success) {
1396 >        } catch(Exception ex) {
1397 >            unexpectedException();
1398 >        } finally {
1399 >            joinPool(e);
1400 >        }
1401 >    }
1402 >
1403 >    /**
1404 >     * timed invokeAll(empty collection) returns empty collection
1405 >     */
1406 >    public void testTimedInvokeAll2() {
1407 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1408 >        try {
1409 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1410 >            assertTrue(r.isEmpty());
1411 >        } catch(Exception ex) {
1412 >            unexpectedException();
1413 >        } finally {
1414 >            joinPool(e);
1415 >        }
1416 >    }
1417 >
1418 >    /**
1419 >     * timed invokeAll(c) throws NPE if c has null elements
1420 >     */
1421 >    public void testTimedInvokeAll3() {
1422 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1423 >        try {
1424 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1425 >            l.add(new StringTask());
1426 >            l.add(null);
1427 >            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1428 >        } catch (NullPointerException success) {
1429 >        } catch(Exception ex) {
1430 >            unexpectedException();
1431 >        } finally {
1432 >            joinPool(e);
1433 >        }
1434 >    }
1435 >
1436 >    /**
1437 >     * get of element of invokeAll(c) throws exception on failed task
1438 >     */
1439 >    public void testTimedInvokeAll4() {
1440 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1441 >        try {
1442 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1443 >            l.add(new NPETask());
1444 >            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1445 >            assertEquals(1, result.size());
1446 >            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1447 >                it.next().get();
1448 >        } catch(ExecutionException success) {
1449 >        } catch(Exception ex) {
1450 >            unexpectedException();
1451 >        } finally {
1452 >            joinPool(e);
1453 >        }
1454 >    }
1455 >
1456 >    /**
1457 >     * timed invokeAll(c) returns results of all completed tasks
1458 >     */
1459 >    public void testTimedInvokeAll5() {
1460 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1461 >        try {
1462 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1463 >            l.add(new StringTask());
1464 >            l.add(new StringTask());
1465 >            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1466 >            assertEquals(2, result.size());
1467 >            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1468 >                assertSame(TEST_STRING, it.next().get());
1469 >        } catch (ExecutionException success) {
1470 >        } catch(Exception ex) {
1471 >            unexpectedException();
1472 >        } finally {
1473 >            joinPool(e);
1474 >        }
1475 >    }
1476 >
1477 >    /**
1478 >     * timed invokeAll(c) cancels tasks not completed by timeout
1479 >     */
1480 >    public void testTimedInvokeAll6() {
1481 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1482 >        try {
1483 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1484 >            l.add(new StringTask());
1485 >            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1486 >            l.add(new StringTask());
1487 >            List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
1488 >            assertEquals(3, result.size());
1489 >            Iterator<Future<String>> it = result.iterator();
1490 >            Future<String> f1 = it.next();
1491 >            Future<String> f2 = it.next();
1492 >            Future<String> f3 = it.next();
1493 >            assertTrue(f1.isDone());
1494 >            assertTrue(f2.isDone());
1495 >            assertTrue(f3.isDone());
1496 >            assertFalse(f1.isCancelled());
1497 >            assertTrue(f2.isCancelled());
1498 >        } catch(Exception ex) {
1499 >            unexpectedException();
1500 >        } finally {
1501 >            joinPool(e);
1502 >        }
1503 >    }
1504 >
1505 >
1506   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines