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

Comparing jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java (file contents):
Revision 1.2 by jsr166, Mon Nov 2 20:28:32 2009 UTC vs.
Revision 1.30 by jsr166, Sun May 29 07:01:17 2011 UTC

# Line 1 | Line 1
1   /*
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
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9   import java.util.concurrent.*;
10 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
11   import java.util.concurrent.locks.*;
12  
13   import junit.framework.*;
# Line 14 | Line 15 | import java.util.*;
15  
16   public class ThreadPoolExecutorSubclassTest extends JSR166TestCase {
17      public static void main(String[] args) {
18 <        junit.textui.TestRunner.run (suite());
18 >        junit.textui.TestRunner.run(suite());
19      }
20      public static Test suite() {
21 <        return new TestSuite(ThreadPoolExecutorTest.class);
21 >        return new TestSuite(ThreadPoolExecutorSubclassTest.class);
22      }
23  
24      static class CustomTask<V> implements RunnableFuture<V> {
# Line 29 | Line 30 | public class ThreadPoolExecutorSubclassT
30          V result;
31          Thread thread;
32          Exception exception;
33 <        CustomTask(Callable<V> c) { callable = c; }
34 <        CustomTask(final Runnable r, final V res) { callable = new Callable<V>() {
33 >        CustomTask(Callable<V> c) {
34 >            if (c == null) throw new NullPointerException();
35 >            callable = c;
36 >        }
37 >        CustomTask(final Runnable r, final V res) {
38 >            if (r == null) throw new NullPointerException();
39 >            callable = new Callable<V>() {
40              public V call() throws Exception { r.run(); return res; }};
41          }
42          public boolean isDone() {
# Line 54 | Line 60 | public class ThreadPoolExecutorSubclassT
60              finally { lock.unlock() ; }
61          }
62          public void run() {
57            boolean runme;
63              lock.lock();
64              try {
65 <                runme = !done;
66 <                if (!runme)
67 <                    thread = Thread.currentThread();
65 >                if (done)
66 >                    return;
67 >                thread = Thread.currentThread();
68              }
69              finally { lock.unlock() ; }
65            if (!runme) return;
70              V v = null;
71              Exception e = null;
72              try {
73                  v = callable.call();
74              }
75 <            catch(Exception ex) {
75 >            catch (Exception ex) {
76                  e = ex;
77              }
78              lock.lock();
# Line 93 | Line 97 | public class ThreadPoolExecutorSubclassT
97              finally { lock.unlock(); }
98          }
99          public V get(long timeout, TimeUnit unit)
100 <            throws InterruptedException, ExecutionException, TimeoutException{
100 >            throws InterruptedException, ExecutionException, TimeoutException {
101              long nanos = unit.toNanos(timeout);
102              lock.lock();
103              try {
# Line 111 | Line 115 | public class ThreadPoolExecutorSubclassT
115          }
116      }
117  
114
118      static class CustomTPE extends ThreadPoolExecutor {
119          protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
120              return new CustomTask<V>(c);
# Line 158 | Line 161 | public class ThreadPoolExecutorSubclassT
161                workQueue, threadFactory, handler);
162          }
163  
164 <        volatile boolean beforeCalled = false;
165 <        volatile boolean afterCalled = false;
166 <        volatile boolean terminatedCalled = false;
164 >        final CountDownLatch beforeCalled = new CountDownLatch(1);
165 >        final CountDownLatch afterCalled = new CountDownLatch(1);
166 >        final CountDownLatch terminatedCalled = new CountDownLatch(1);
167 >
168          public CustomTPE() {
169 <            super(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>());
169 >            super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
170          }
171          protected void beforeExecute(Thread t, Runnable r) {
172 <            beforeCalled = true;
172 >            beforeCalled.countDown();
173          }
174          protected void afterExecute(Runnable r, Throwable t) {
175 <            afterCalled = true;
175 >            afterCalled.countDown();
176          }
177          protected void terminated() {
178 <            terminatedCalled = true;
178 >            terminatedCalled.countDown();
179          }
180  
181 +        public boolean beforeCalled() {
182 +            return beforeCalled.getCount() == 0;
183 +        }
184 +        public boolean afterCalled() {
185 +            return afterCalled.getCount() == 0;
186 +        }
187 +        public boolean terminatedCalled() {
188 +            return terminatedCalled.getCount() == 0;
189 +        }
190      }
191  
192 <    static class FailingThreadFactory implements ThreadFactory{
192 >    static class FailingThreadFactory implements ThreadFactory {
193          int calls = 0;
194 <        public Thread newThread(Runnable r){
194 >        public Thread newThread(Runnable r) {
195              if (++calls > 1) return null;
196              return new Thread(r);
197          }
198      }
199  
187
200      /**
201 <     *  execute successfully executes a runnable
201 >     * execute successfully executes a runnable
202       */
203 <    public void testExecute() {
204 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
203 >    public void testExecute() throws InterruptedException {
204 >        final ThreadPoolExecutor p =
205 >            new CustomTPE(1, 1,
206 >                          LONG_DELAY_MS, MILLISECONDS,
207 >                          new ArrayBlockingQueue<Runnable>(10));
208 >        final CountDownLatch done = new CountDownLatch(1);
209 >        final Runnable task = new CheckedRunnable() {
210 >            public void realRun() {
211 >                done.countDown();
212 >            }};
213          try {
214 <            p1.execute(new Runnable() {
215 <                    public void run() {
216 <                        try {
217 <                            Thread.sleep(SHORT_DELAY_MS);
198 <                        } catch(InterruptedException e){
199 <                            threadUnexpectedException();
200 <                        }
201 <                    }
202 <                });
203 <            Thread.sleep(SMALL_DELAY_MS);
204 <        } catch(InterruptedException e){
205 <            unexpectedException();
214 >            p.execute(task);
215 >            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
216 >        } finally {
217 >            joinPool(p);
218          }
207        joinPool(p1);
219      }
220  
221      /**
222 <     *  getActiveCount increases but doesn't overestimate, when a
223 <     *  thread becomes active
222 >     * getActiveCount increases but doesn't overestimate, when a
223 >     * thread becomes active
224       */
225 <    public void testGetActiveCount() {
226 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
227 <        assertEquals(0, p2.getActiveCount());
228 <        p2.execute(new MediumRunnable());
229 <        try {
230 <            Thread.sleep(SHORT_DELAY_MS);
231 <        } catch(Exception e){
232 <            unexpectedException();
225 >    public void testGetActiveCount() throws InterruptedException {
226 >        final ThreadPoolExecutor p =
227 >            new CustomTPE(2, 2,
228 >                          LONG_DELAY_MS, MILLISECONDS,
229 >                          new ArrayBlockingQueue<Runnable>(10));
230 >        final CountDownLatch threadStarted = new CountDownLatch(1);
231 >        final CountDownLatch done = new CountDownLatch(1);
232 >        try {
233 >            assertEquals(0, p.getActiveCount());
234 >            p.execute(new CheckedRunnable() {
235 >                public void realRun() throws InterruptedException {
236 >                    threadStarted.countDown();
237 >                    assertEquals(1, p.getActiveCount());
238 >                    done.await();
239 >                }});
240 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
241 >            assertEquals(1, p.getActiveCount());
242 >        } finally {
243 >            done.countDown();
244 >            joinPool(p);
245          }
223        assertEquals(1, p2.getActiveCount());
224        joinPool(p2);
246      }
247  
248      /**
249 <     *  prestartCoreThread starts a thread if under corePoolSize, else doesn't
249 >     * prestartCoreThread starts a thread if under corePoolSize, else doesn't
250       */
251      public void testPrestartCoreThread() {
252 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
253 <        assertEquals(0, p2.getPoolSize());
254 <        assertTrue(p2.prestartCoreThread());
255 <        assertEquals(1, p2.getPoolSize());
256 <        assertTrue(p2.prestartCoreThread());
257 <        assertEquals(2, p2.getPoolSize());
258 <        assertFalse(p2.prestartCoreThread());
259 <        assertEquals(2, p2.getPoolSize());
260 <        joinPool(p2);
252 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
253 >        assertEquals(0, p.getPoolSize());
254 >        assertTrue(p.prestartCoreThread());
255 >        assertEquals(1, p.getPoolSize());
256 >        assertTrue(p.prestartCoreThread());
257 >        assertEquals(2, p.getPoolSize());
258 >        assertFalse(p.prestartCoreThread());
259 >        assertEquals(2, p.getPoolSize());
260 >        joinPool(p);
261      }
262  
263      /**
264 <     *  prestartAllCoreThreads starts all corePoolSize threads
264 >     * prestartAllCoreThreads starts all corePoolSize threads
265       */
266      public void testPrestartAllCoreThreads() {
267 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
268 <        assertEquals(0, p2.getPoolSize());
269 <        p2.prestartAllCoreThreads();
270 <        assertEquals(2, p2.getPoolSize());
271 <        p2.prestartAllCoreThreads();
272 <        assertEquals(2, p2.getPoolSize());
273 <        joinPool(p2);
267 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
268 >        assertEquals(0, p.getPoolSize());
269 >        p.prestartAllCoreThreads();
270 >        assertEquals(2, p.getPoolSize());
271 >        p.prestartAllCoreThreads();
272 >        assertEquals(2, p.getPoolSize());
273 >        joinPool(p);
274      }
275  
276      /**
277 <     *   getCompletedTaskCount increases, but doesn't overestimate,
278 <     *   when tasks complete
277 >     * getCompletedTaskCount increases, but doesn't overestimate,
278 >     * when tasks complete
279       */
280 <    public void testGetCompletedTaskCount() {
281 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
282 <        assertEquals(0, p2.getCompletedTaskCount());
283 <        p2.execute(new ShortRunnable());
284 <        try {
285 <            Thread.sleep(SMALL_DELAY_MS);
286 <        } catch(Exception e){
287 <            unexpectedException();
280 >    public void testGetCompletedTaskCount() throws InterruptedException {
281 >        final ThreadPoolExecutor p =
282 >            new CustomTPE(2, 2,
283 >                          LONG_DELAY_MS, MILLISECONDS,
284 >                          new ArrayBlockingQueue<Runnable>(10));
285 >        final CountDownLatch threadStarted = new CountDownLatch(1);
286 >        final CountDownLatch threadProceed = new CountDownLatch(1);
287 >        final CountDownLatch threadDone = new CountDownLatch(1);
288 >        try {
289 >            assertEquals(0, p.getCompletedTaskCount());
290 >            p.execute(new CheckedRunnable() {
291 >                public void realRun() throws InterruptedException {
292 >                    threadStarted.countDown();
293 >                    assertEquals(0, p.getCompletedTaskCount());
294 >                    threadProceed.await();
295 >                    threadDone.countDown();
296 >                }});
297 >            await(threadStarted);
298 >            assertEquals(0, p.getCompletedTaskCount());
299 >            threadProceed.countDown();
300 >            threadDone.await();
301 >            long startTime = System.nanoTime();
302 >            while (p.getCompletedTaskCount() != 1) {
303 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
304 >                    fail("timed out");
305 >                Thread.yield();
306 >            }
307 >        } finally {
308 >            joinPool(p);
309          }
268        assertEquals(1, p2.getCompletedTaskCount());
269        try { p2.shutdown(); } catch(SecurityException ok) { return; }
270        joinPool(p2);
310      }
311  
312      /**
313 <     *   getCorePoolSize returns size given in constructor if not otherwise set
313 >     * getCorePoolSize returns size given in constructor if not otherwise set
314       */
315      public void testGetCorePoolSize() {
316 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
317 <        assertEquals(1, p1.getCorePoolSize());
318 <        joinPool(p1);
316 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
317 >        assertEquals(1, p.getCorePoolSize());
318 >        joinPool(p);
319      }
320  
321      /**
322 <     *   getKeepAliveTime returns value given in constructor if not otherwise set
322 >     * getKeepAliveTime returns value given in constructor if not otherwise set
323       */
324      public void testGetKeepAliveTime() {
325 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
326 <        assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS));
327 <        joinPool(p2);
325 >        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
326 >        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
327 >        joinPool(p);
328      }
329  
291
330      /**
331       * getThreadFactory returns factory in constructor if not set
332       */
333      public void testGetThreadFactory() {
334          ThreadFactory tf = new SimpleThreadFactory();
335 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
335 >        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
336          assertSame(tf, p.getThreadFactory());
337          joinPool(p);
338      }
# Line 303 | Line 341 | public class ThreadPoolExecutorSubclassT
341       * setThreadFactory sets the thread factory returned by getThreadFactory
342       */
343      public void testSetThreadFactory() {
344 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
344 >        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
345          ThreadFactory tf = new SimpleThreadFactory();
346          p.setThreadFactory(tf);
347          assertSame(tf, p.getThreadFactory());
348          joinPool(p);
349      }
350  
313
351      /**
352       * setThreadFactory(null) throws NPE
353       */
354      public void testSetThreadFactoryNull() {
355 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
355 >        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
356          try {
357              p.setThreadFactory(null);
358              shouldThrow();
# Line 330 | Line 367 | public class ThreadPoolExecutorSubclassT
367       */
368      public void testGetRejectedExecutionHandler() {
369          RejectedExecutionHandler h = new NoOpREHandler();
370 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
370 >        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
371          assertSame(h, p.getRejectedExecutionHandler());
372          joinPool(p);
373      }
# Line 340 | Line 377 | public class ThreadPoolExecutorSubclassT
377       * getRejectedExecutionHandler
378       */
379      public void testSetRejectedExecutionHandler() {
380 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
380 >        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
381          RejectedExecutionHandler h = new NoOpREHandler();
382          p.setRejectedExecutionHandler(h);
383          assertSame(h, p.getRejectedExecutionHandler());
384          joinPool(p);
385      }
386  
350
387      /**
388       * setRejectedExecutionHandler(null) throws NPE
389       */
390      public void testSetRejectedExecutionHandlerNull() {
391 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
391 >        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
392          try {
393              p.setRejectedExecutionHandler(null);
394              shouldThrow();
# Line 362 | Line 398 | public class ThreadPoolExecutorSubclassT
398          }
399      }
400  
365
401      /**
402 <     *   getLargestPoolSize increases, but doesn't overestimate, when
403 <     *   multiple threads active
402 >     * getLargestPoolSize increases, but doesn't overestimate, when
403 >     * multiple threads active
404       */
405 <    public void testGetLargestPoolSize() {
406 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
407 <        try {
408 <            assertEquals(0, p2.getLargestPoolSize());
409 <            p2.execute(new MediumRunnable());
410 <            p2.execute(new MediumRunnable());
411 <            Thread.sleep(SHORT_DELAY_MS);
412 <            assertEquals(2, p2.getLargestPoolSize());
413 <        } catch(Exception e){
414 <            unexpectedException();
405 >    public void testGetLargestPoolSize() throws InterruptedException {
406 >        final int THREADS = 3;
407 >        final ThreadPoolExecutor p =
408 >            new CustomTPE(THREADS, THREADS,
409 >                          LONG_DELAY_MS, MILLISECONDS,
410 >                          new ArrayBlockingQueue<Runnable>(10));
411 >        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
412 >        final CountDownLatch done = new CountDownLatch(1);
413 >        try {
414 >            assertEquals(0, p.getLargestPoolSize());
415 >            for (int i = 0; i < THREADS; i++)
416 >                p.execute(new CheckedRunnable() {
417 >                    public void realRun() throws InterruptedException {
418 >                        threadsStarted.countDown();
419 >                        done.await();
420 >                        assertEquals(THREADS, p.getLargestPoolSize());
421 >                    }});
422 >            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
423 >            assertEquals(THREADS, p.getLargestPoolSize());
424 >        } finally {
425 >            done.countDown();
426 >            joinPool(p);
427 >            assertEquals(THREADS, p.getLargestPoolSize());
428          }
381        joinPool(p2);
429      }
430  
431      /**
432 <     *   getMaximumPoolSize returns value given in constructor if not
433 <     *   otherwise set
432 >     * getMaximumPoolSize returns value given in constructor if not
433 >     * otherwise set
434       */
435      public void testGetMaximumPoolSize() {
436 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
437 <        assertEquals(2, p2.getMaximumPoolSize());
438 <        joinPool(p2);
436 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
437 >        assertEquals(2, p.getMaximumPoolSize());
438 >        joinPool(p);
439      }
440  
441      /**
442 <     *   getPoolSize increases, but doesn't overestimate, when threads
443 <     *   become active
442 >     * getPoolSize increases, but doesn't overestimate, when threads
443 >     * become active
444       */
445 <    public void testGetPoolSize() {
446 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
447 <        assertEquals(0, p1.getPoolSize());
448 <        p1.execute(new MediumRunnable());
449 <        assertEquals(1, p1.getPoolSize());
450 <        joinPool(p1);
445 >    public void testGetPoolSize() throws InterruptedException {
446 >        final ThreadPoolExecutor p =
447 >            new CustomTPE(1, 1,
448 >                          LONG_DELAY_MS, MILLISECONDS,
449 >                          new ArrayBlockingQueue<Runnable>(10));
450 >        final CountDownLatch threadStarted = new CountDownLatch(1);
451 >        final CountDownLatch done = new CountDownLatch(1);
452 >        try {
453 >            assertEquals(0, p.getPoolSize());
454 >            p.execute(new CheckedRunnable() {
455 >                public void realRun() throws InterruptedException {
456 >                    threadStarted.countDown();
457 >                    assertEquals(1, p.getPoolSize());
458 >                    done.await();
459 >                }});
460 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
461 >            assertEquals(1, p.getPoolSize());
462 >        } finally {
463 >            done.countDown();
464 >            joinPool(p);
465 >        }
466      }
467  
468      /**
469 <     *  getTaskCount increases, but doesn't overestimate, when tasks submitted
469 >     * getTaskCount increases, but doesn't overestimate, when tasks submitted
470       */
471 <    public void testGetTaskCount() {
472 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
473 <        try {
474 <            assertEquals(0, p1.getTaskCount());
475 <            p1.execute(new MediumRunnable());
476 <            Thread.sleep(SHORT_DELAY_MS);
477 <            assertEquals(1, p1.getTaskCount());
478 <        } catch(Exception e){
479 <            unexpectedException();
471 >    public void testGetTaskCount() throws InterruptedException {
472 >        final ThreadPoolExecutor p =
473 >            new CustomTPE(1, 1,
474 >                          LONG_DELAY_MS, MILLISECONDS,
475 >                          new ArrayBlockingQueue<Runnable>(10));
476 >        final CountDownLatch threadStarted = new CountDownLatch(1);
477 >        final CountDownLatch done = new CountDownLatch(1);
478 >        try {
479 >            assertEquals(0, p.getTaskCount());
480 >            p.execute(new CheckedRunnable() {
481 >                public void realRun() throws InterruptedException {
482 >                    threadStarted.countDown();
483 >                    assertEquals(1, p.getTaskCount());
484 >                    done.await();
485 >                }});
486 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
487 >            assertEquals(1, p.getTaskCount());
488 >        } finally {
489 >            done.countDown();
490 >            joinPool(p);
491          }
419        joinPool(p1);
492      }
493  
494      /**
495 <     *   isShutDown is false before shutdown, true after
495 >     * isShutdown is false before shutdown, true after
496       */
497      public void testIsShutdown() {
498  
499 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
500 <        assertFalse(p1.isShutdown());
501 <        try { p1.shutdown(); } catch(SecurityException ok) { return; }
502 <        assertTrue(p1.isShutdown());
503 <        joinPool(p1);
499 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
500 >        assertFalse(p.isShutdown());
501 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
502 >        assertTrue(p.isShutdown());
503 >        joinPool(p);
504      }
505  
434
506      /**
507 <     *  isTerminated is false before termination, true after
507 >     * isTerminated is false before termination, true after
508       */
509 <    public void testIsTerminated() {
510 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
511 <        assertFalse(p1.isTerminated());
512 <        try {
513 <            p1.execute(new MediumRunnable());
514 <        } finally {
515 <            try { p1.shutdown(); } catch(SecurityException ok) { return; }
516 <        }
517 <        try {
518 <            assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
519 <            assertTrue(p1.isTerminated());
520 <        } catch(Exception e){
521 <            unexpectedException();
522 <        }
509 >    public void testIsTerminated() throws InterruptedException {
510 >        final ThreadPoolExecutor p =
511 >            new CustomTPE(1, 1,
512 >                          LONG_DELAY_MS, MILLISECONDS,
513 >                          new ArrayBlockingQueue<Runnable>(10));
514 >        final CountDownLatch threadStarted = new CountDownLatch(1);
515 >        final CountDownLatch done = new CountDownLatch(1);
516 >        try {
517 >            assertFalse(p.isTerminating());
518 >            p.execute(new CheckedRunnable() {
519 >                public void realRun() throws InterruptedException {
520 >                    assertFalse(p.isTerminating());
521 >                    threadStarted.countDown();
522 >                    done.await();
523 >                }});
524 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
525 >            assertFalse(p.isTerminating());
526 >            done.countDown();
527 >        } finally {
528 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
529 >        }
530 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
531 >        assertTrue(p.isTerminated());
532 >        assertFalse(p.isTerminating());
533      }
534  
535      /**
536 <     *  isTerminating is not true when running or when terminated
537 <     */
538 <    public void testIsTerminating() {
539 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
540 <        assertFalse(p1.isTerminating());
541 <        try {
542 <            p1.execute(new SmallRunnable());
543 <            assertFalse(p1.isTerminating());
544 <        } finally {
545 <            try { p1.shutdown(); } catch(SecurityException ok) { return; }
546 <        }
547 <        try {
548 <            assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
549 <            assertTrue(p1.isTerminated());
550 <            assertFalse(p1.isTerminating());
551 <        } catch(Exception e){
552 <            unexpectedException();
553 <        }
536 >     * isTerminating is not true when running or when terminated
537 >     */
538 >    public void testIsTerminating() throws InterruptedException {
539 >        final ThreadPoolExecutor p =
540 >            new CustomTPE(1, 1,
541 >                          LONG_DELAY_MS, MILLISECONDS,
542 >                          new ArrayBlockingQueue<Runnable>(10));
543 >        final CountDownLatch threadStarted = new CountDownLatch(1);
544 >        final CountDownLatch done = new CountDownLatch(1);
545 >        try {
546 >            assertFalse(p.isTerminating());
547 >            p.execute(new CheckedRunnable() {
548 >                public void realRun() throws InterruptedException {
549 >                    assertFalse(p.isTerminating());
550 >                    threadStarted.countDown();
551 >                    done.await();
552 >                }});
553 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
554 >            assertFalse(p.isTerminating());
555 >            done.countDown();
556 >        } finally {
557 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
558 >        }
559 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
560 >        assertTrue(p.isTerminated());
561 >        assertFalse(p.isTerminating());
562      }
563  
564      /**
565       * getQueue returns the work queue, which contains queued tasks
566       */
567 <    public void testGetQueue() {
568 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
569 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q);
570 <        FutureTask[] tasks = new FutureTask[5];
571 <        for(int i = 0; i < 5; i++){
572 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
573 <            p1.execute(tasks[i]);
574 <        }
575 <        try {
576 <            Thread.sleep(SHORT_DELAY_MS);
577 <            BlockingQueue<Runnable> wq = p1.getQueue();
578 <            assertSame(q, wq);
579 <            assertFalse(wq.contains(tasks[0]));
580 <            assertTrue(wq.contains(tasks[4]));
581 <            for (int i = 1; i < 5; ++i)
582 <                tasks[i].cancel(true);
583 <            p1.shutdownNow();
584 <        } catch(Exception e) {
585 <            unexpectedException();
567 >    public void testGetQueue() throws InterruptedException {
568 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
569 >        final ThreadPoolExecutor p =
570 >            new CustomTPE(1, 1,
571 >                          LONG_DELAY_MS, MILLISECONDS,
572 >                          q);
573 >        final CountDownLatch threadStarted = new CountDownLatch(1);
574 >        final CountDownLatch done = new CountDownLatch(1);
575 >        try {
576 >            FutureTask[] tasks = new FutureTask[5];
577 >            for (int i = 0; i < tasks.length; i++) {
578 >                Callable task = new CheckedCallable<Boolean>() {
579 >                    public Boolean realCall() throws InterruptedException {
580 >                        threadStarted.countDown();
581 >                        assertSame(q, p.getQueue());
582 >                        done.await();
583 >                        return Boolean.TRUE;
584 >                    }};
585 >                tasks[i] = new FutureTask(task);
586 >                p.execute(tasks[i]);
587 >            }
588 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
589 >            assertSame(q, p.getQueue());
590 >            assertFalse(q.contains(tasks[0]));
591 >            assertTrue(q.contains(tasks[tasks.length - 1]));
592 >            assertEquals(tasks.length - 1, q.size());
593          } finally {
594 <            joinPool(p1);
594 >            done.countDown();
595 >            joinPool(p);
596          }
597      }
598  
599      /**
600       * remove(task) removes queued task, and fails to remove active task
601       */
602 <    public void testRemove() {
602 >    public void testRemove() throws InterruptedException {
603          BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
604 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q);
605 <        FutureTask[] tasks = new FutureTask[5];
606 <        for(int i = 0; i < 5; i++){
607 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
608 <            p1.execute(tasks[i]);
609 <        }
610 <        try {
611 <            Thread.sleep(SHORT_DELAY_MS);
612 <            assertFalse(p1.remove(tasks[0]));
604 >        final ThreadPoolExecutor p =
605 >            new CustomTPE(1, 1,
606 >                          LONG_DELAY_MS, MILLISECONDS,
607 >                          q);
608 >        Runnable[] tasks = new Runnable[6];
609 >        final CountDownLatch threadStarted = new CountDownLatch(1);
610 >        final CountDownLatch done = new CountDownLatch(1);
611 >        try {
612 >            for (int i = 0; i < tasks.length; i++) {
613 >                tasks[i] = new CheckedRunnable() {
614 >                        public void realRun() throws InterruptedException {
615 >                            threadStarted.countDown();
616 >                            done.await();
617 >                        }};
618 >                p.execute(tasks[i]);
619 >            }
620 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
621 >            assertFalse(p.remove(tasks[0]));
622              assertTrue(q.contains(tasks[4]));
623              assertTrue(q.contains(tasks[3]));
624 <            assertTrue(p1.remove(tasks[4]));
625 <            assertFalse(p1.remove(tasks[4]));
624 >            assertTrue(p.remove(tasks[4]));
625 >            assertFalse(p.remove(tasks[4]));
626              assertFalse(q.contains(tasks[4]));
627              assertTrue(q.contains(tasks[3]));
628 <            assertTrue(p1.remove(tasks[3]));
628 >            assertTrue(p.remove(tasks[3]));
629              assertFalse(q.contains(tasks[3]));
524        } catch(Exception e) {
525            unexpectedException();
630          } finally {
631 <            joinPool(p1);
631 >            done.countDown();
632 >            joinPool(p);
633          }
634      }
635  
636      /**
637 <     *   purge removes cancelled tasks from the queue
637 >     * purge removes cancelled tasks from the queue
638       */
639 <    public void testPurge() {
640 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
639 >    public void testPurge() throws InterruptedException {
640 >        final CountDownLatch threadStarted = new CountDownLatch(1);
641 >        final CountDownLatch done = new CountDownLatch(1);
642 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
643 >        final ThreadPoolExecutor p =
644 >            new CustomTPE(1, 1,
645 >                          LONG_DELAY_MS, MILLISECONDS,
646 >                          q);
647          FutureTask[] tasks = new FutureTask[5];
648 <        for(int i = 0; i < 5; i++){
649 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
650 <            p1.execute(tasks[i]);
648 >        try {
649 >            for (int i = 0; i < tasks.length; i++) {
650 >                Callable task = new CheckedCallable<Boolean>() {
651 >                    public Boolean realCall() throws InterruptedException {
652 >                        threadStarted.countDown();
653 >                        done.await();
654 >                        return Boolean.TRUE;
655 >                    }};
656 >                tasks[i] = new FutureTask(task);
657 >                p.execute(tasks[i]);
658 >            }
659 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
660 >            assertEquals(tasks.length, p.getTaskCount());
661 >            assertEquals(tasks.length - 1, q.size());
662 >            assertEquals(1L, p.getActiveCount());
663 >            assertEquals(0L, p.getCompletedTaskCount());
664 >            tasks[4].cancel(true);
665 >            tasks[3].cancel(false);
666 >            p.purge();
667 >            assertEquals(tasks.length - 3, q.size());
668 >            assertEquals(tasks.length - 2, p.getTaskCount());
669 >            p.purge();         // Nothing to do
670 >            assertEquals(tasks.length - 3, q.size());
671 >            assertEquals(tasks.length - 2, p.getTaskCount());
672 >        } finally {
673 >            done.countDown();
674 >            joinPool(p);
675          }
541        tasks[4].cancel(true);
542        tasks[3].cancel(true);
543        p1.purge();
544        long count = p1.getTaskCount();
545        assertTrue(count >= 2 && count < 5);
546        joinPool(p1);
676      }
677  
678      /**
679 <     *  shutDownNow returns a list containing tasks that were not run
679 >     * shutdownNow returns a list containing tasks that were not run
680       */
681 <    public void testShutDownNow() {
682 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
681 >    public void testShutdownNow() {
682 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
683          List l;
684          try {
685 <            for(int i = 0; i < 5; i++)
686 <                p1.execute(new MediumPossiblyInterruptedRunnable());
685 >            for (int i = 0; i < 5; i++)
686 >                p.execute(new MediumPossiblyInterruptedRunnable());
687          }
688          finally {
689              try {
690 <                l = p1.shutdownNow();
690 >                l = p.shutdownNow();
691              } catch (SecurityException ok) { return; }
563
692          }
693 <        assertTrue(p1.isShutdown());
694 <        assertTrue(l.size() <= 4);
693 >        assertTrue(p.isShutdown());
694 >        assertTrue(l.size() <= 4);
695      }
696  
697      // Exception Tests
698  
571
699      /**
700       * Constructor throws if corePoolSize argument is less than zero
701       */
702      public void testConstructor1() {
703          try {
704 <            new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
704 >            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
705              shouldThrow();
706 <        }
580 <        catch (IllegalArgumentException success){}
706 >        } catch (IllegalArgumentException success) {}
707      }
708  
709      /**
# Line 585 | Line 711 | public class ThreadPoolExecutorSubclassT
711       */
712      public void testConstructor2() {
713          try {
714 <            new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
714 >            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
715              shouldThrow();
716 <        }
591 <        catch (IllegalArgumentException success){}
716 >        } catch (IllegalArgumentException success) {}
717      }
718  
719      /**
# Line 596 | Line 721 | public class ThreadPoolExecutorSubclassT
721       */
722      public void testConstructor3() {
723          try {
724 <            new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
724 >            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
725              shouldThrow();
726 <        }
602 <        catch (IllegalArgumentException success){}
726 >        } catch (IllegalArgumentException success) {}
727      }
728  
729      /**
# Line 607 | Line 731 | public class ThreadPoolExecutorSubclassT
731       */
732      public void testConstructor4() {
733          try {
734 <            new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
734 >            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
735              shouldThrow();
736 <        }
613 <        catch (IllegalArgumentException success){}
736 >        } catch (IllegalArgumentException success) {}
737      }
738  
739      /**
# Line 618 | Line 741 | public class ThreadPoolExecutorSubclassT
741       */
742      public void testConstructor5() {
743          try {
744 <            new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
744 >            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
745              shouldThrow();
746 <        }
624 <        catch (IllegalArgumentException success){}
746 >        } catch (IllegalArgumentException success) {}
747      }
748  
749      /**
# Line 629 | Line 751 | public class ThreadPoolExecutorSubclassT
751       */
752      public void testConstructorNullPointerException() {
753          try {
754 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null);
754 >            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
755              shouldThrow();
756 <        }
635 <        catch (NullPointerException success){}
756 >        } catch (NullPointerException success) {}
757      }
758  
638
639
759      /**
760       * Constructor throws if corePoolSize argument is less than zero
761       */
762      public void testConstructor6() {
763          try {
764 <            new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
764 >            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
765              shouldThrow();
766 <        } catch (IllegalArgumentException success){}
766 >        } catch (IllegalArgumentException success) {}
767      }
768  
769      /**
# Line 652 | Line 771 | public class ThreadPoolExecutorSubclassT
771       */
772      public void testConstructor7() {
773          try {
774 <            new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
774 >            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
775              shouldThrow();
776 <        }
658 <        catch (IllegalArgumentException success){}
776 >        } catch (IllegalArgumentException success) {}
777      }
778  
779      /**
# Line 663 | Line 781 | public class ThreadPoolExecutorSubclassT
781       */
782      public void testConstructor8() {
783          try {
784 <            new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
784 >            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
785              shouldThrow();
786 <        }
669 <        catch (IllegalArgumentException success){}
786 >        } catch (IllegalArgumentException success) {}
787      }
788  
789      /**
# Line 674 | Line 791 | public class ThreadPoolExecutorSubclassT
791       */
792      public void testConstructor9() {
793          try {
794 <            new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
794 >            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
795              shouldThrow();
796 <        }
680 <        catch (IllegalArgumentException success){}
796 >        } catch (IllegalArgumentException success) {}
797      }
798  
799      /**
# Line 685 | Line 801 | public class ThreadPoolExecutorSubclassT
801       */
802      public void testConstructor10() {
803          try {
804 <            new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
804 >            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
805              shouldThrow();
806 <        }
691 <        catch (IllegalArgumentException success){}
806 >        } catch (IllegalArgumentException success) {}
807      }
808  
809      /**
# Line 696 | Line 811 | public class ThreadPoolExecutorSubclassT
811       */
812      public void testConstructorNullPointerException2() {
813          try {
814 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory());
814 >            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
815              shouldThrow();
816 <        }
702 <        catch (NullPointerException success){}
816 >        } catch (NullPointerException success) {}
817      }
818  
819      /**
# Line 708 | Line 822 | public class ThreadPoolExecutorSubclassT
822      public void testConstructorNullPointerException3() {
823          try {
824              ThreadFactory f = null;
825 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
825 >            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
826              shouldThrow();
827 <        }
714 <        catch (NullPointerException success){}
827 >        } catch (NullPointerException success) {}
828      }
829  
717
830      /**
831       * Constructor throws if corePoolSize argument is less than zero
832       */
833      public void testConstructor11() {
834          try {
835 <            new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
835 >            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
836              shouldThrow();
837 <        }
726 <        catch (IllegalArgumentException success){}
837 >        } catch (IllegalArgumentException success) {}
838      }
839  
840      /**
# Line 731 | Line 842 | public class ThreadPoolExecutorSubclassT
842       */
843      public void testConstructor12() {
844          try {
845 <            new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
845 >            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
846              shouldThrow();
847 <        }
737 <        catch (IllegalArgumentException success){}
847 >        } catch (IllegalArgumentException success) {}
848      }
849  
850      /**
# Line 742 | Line 852 | public class ThreadPoolExecutorSubclassT
852       */
853      public void testConstructor13() {
854          try {
855 <            new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
855 >            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
856              shouldThrow();
857 <        }
748 <        catch (IllegalArgumentException success){}
857 >        } catch (IllegalArgumentException success) {}
858      }
859  
860      /**
# Line 753 | Line 862 | public class ThreadPoolExecutorSubclassT
862       */
863      public void testConstructor14() {
864          try {
865 <            new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
865 >            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
866              shouldThrow();
867 <        }
759 <        catch (IllegalArgumentException success){}
867 >        } catch (IllegalArgumentException success) {}
868      }
869  
870      /**
# Line 764 | Line 872 | public class ThreadPoolExecutorSubclassT
872       */
873      public void testConstructor15() {
874          try {
875 <            new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
875 >            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
876              shouldThrow();
877 <        }
770 <        catch (IllegalArgumentException success){}
877 >        } catch (IllegalArgumentException success) {}
878      }
879  
880      /**
# Line 775 | Line 882 | public class ThreadPoolExecutorSubclassT
882       */
883      public void testConstructorNullPointerException4() {
884          try {
885 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler());
885 >            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
886              shouldThrow();
887 <        }
781 <        catch (NullPointerException success){}
887 >        } catch (NullPointerException success) {}
888      }
889  
890      /**
# Line 787 | Line 893 | public class ThreadPoolExecutorSubclassT
893      public void testConstructorNullPointerException5() {
894          try {
895              RejectedExecutionHandler r = null;
896 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
896 >            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
897              shouldThrow();
898 <        }
793 <        catch (NullPointerException success){}
898 >        } catch (NullPointerException success) {}
899      }
900  
796
901      /**
902       * Constructor throws if corePoolSize argument is less than zero
903       */
904      public void testConstructor16() {
905          try {
906 <            new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
906 >            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
907              shouldThrow();
908 <        }
805 <        catch (IllegalArgumentException success){}
908 >        } catch (IllegalArgumentException success) {}
909      }
910  
911      /**
# Line 810 | Line 913 | public class ThreadPoolExecutorSubclassT
913       */
914      public void testConstructor17() {
915          try {
916 <            new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
916 >            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
917              shouldThrow();
918 <        }
816 <        catch (IllegalArgumentException success){}
918 >        } catch (IllegalArgumentException success) {}
919      }
920  
921      /**
# Line 821 | Line 923 | public class ThreadPoolExecutorSubclassT
923       */
924      public void testConstructor18() {
925          try {
926 <            new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
926 >            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
927              shouldThrow();
928 <        }
827 <        catch (IllegalArgumentException success){}
928 >        } catch (IllegalArgumentException success) {}
929      }
930  
931      /**
# Line 832 | Line 933 | public class ThreadPoolExecutorSubclassT
933       */
934      public void testConstructor19() {
935          try {
936 <            new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
936 >            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
937              shouldThrow();
938 <        }
838 <        catch (IllegalArgumentException success){}
938 >        } catch (IllegalArgumentException success) {}
939      }
940  
941      /**
# Line 843 | Line 943 | public class ThreadPoolExecutorSubclassT
943       */
944      public void testConstructor20() {
945          try {
946 <            new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
946 >            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
947              shouldThrow();
948 <        }
849 <        catch (IllegalArgumentException success){}
948 >        } catch (IllegalArgumentException success) {}
949      }
950  
951      /**
952 <     * Constructor throws if workQueue is set to null
952 >     * Constructor throws if workQueue is null
953       */
954      public void testConstructorNullPointerException6() {
955          try {
956 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
956 >            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
957              shouldThrow();
958 <        }
860 <        catch (NullPointerException success){}
958 >        } catch (NullPointerException success) {}
959      }
960  
961      /**
962 <     * Constructor throws if handler is set to null
962 >     * Constructor throws if handler is null
963       */
964      public void testConstructorNullPointerException7() {
965          try {
966              RejectedExecutionHandler r = null;
967 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
967 >            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
968              shouldThrow();
969 <        }
872 <        catch (NullPointerException success){}
969 >        } catch (NullPointerException success) {}
970      }
971  
972      /**
973 <     * Constructor throws if ThreadFactory is set top null
973 >     * Constructor throws if ThreadFactory is null
974       */
975      public void testConstructorNullPointerException8() {
976          try {
977 <            ThreadFactory f = null;
978 <            new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
977 >            new CustomTPE(1, 2,
978 >                          LONG_DELAY_MS, MILLISECONDS,
979 >                          new ArrayBlockingQueue<Runnable>(10),
980 >                          (ThreadFactory) null,
981 >                          new NoOpREHandler());
982              shouldThrow();
983 <        }
884 <        catch (NullPointerException successdn8){}
983 >        } catch (NullPointerException success) {}
984      }
985  
887
986      /**
987 <     *  execute throws RejectedExecutionException
890 <     *  if saturated.
987 >     * execute throws RejectedExecutionException if saturated.
988       */
989      public void testSaturatedExecute() {
990 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
991 <        try {
992 <
993 <            for(int i = 0; i < 5; ++i){
994 <                p.execute(new MediumRunnable());
990 >        ThreadPoolExecutor p =
991 >            new CustomTPE(1, 1,
992 >                          LONG_DELAY_MS, MILLISECONDS,
993 >                          new ArrayBlockingQueue<Runnable>(1));
994 >        final CountDownLatch done = new CountDownLatch(1);
995 >        try {
996 >            Runnable task = new CheckedRunnable() {
997 >                public void realRun() throws InterruptedException {
998 >                    done.await();
999 >                }};
1000 >            for (int i = 0; i < 2; ++i)
1001 >                p.execute(task);
1002 >            for (int i = 0; i < 2; ++i) {
1003 >                try {
1004 >                    p.execute(task);
1005 >                    shouldThrow();
1006 >                } catch (RejectedExecutionException success) {}
1007 >                assertTrue(p.getTaskCount() <= 2);
1008              }
1009 <            shouldThrow();
1010 <        } catch(RejectedExecutionException success){}
1011 <        joinPool(p);
1009 >        } finally {
1010 >            done.countDown();
1011 >            joinPool(p);
1012 >        }
1013      }
1014  
1015      /**
1016 <     *  executor using CallerRunsPolicy runs task if saturated.
1016 >     * executor using CallerRunsPolicy runs task if saturated.
1017       */
1018      public void testSaturatedExecute2() {
1019          RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1020 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1020 >        ThreadPoolExecutor p = new CustomTPE(1, 1,
1021 >                                             LONG_DELAY_MS, MILLISECONDS,
1022 >                                             new ArrayBlockingQueue<Runnable>(1),
1023 >                                             h);
1024          try {
911
1025              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1026 <            for(int i = 0; i < 5; ++i){
1026 >            for (int i = 0; i < tasks.length; ++i)
1027                  tasks[i] = new TrackedNoOpRunnable();
915            }
1028              TrackedLongRunnable mr = new TrackedLongRunnable();
1029              p.execute(mr);
1030 <            for(int i = 0; i < 5; ++i){
1030 >            for (int i = 0; i < tasks.length; ++i)
1031                  p.execute(tasks[i]);
1032 <            }
921 <            for(int i = 1; i < 5; ++i) {
1032 >            for (int i = 1; i < tasks.length; ++i)
1033                  assertTrue(tasks[i].done);
1034 <            }
924 <            try { p.shutdownNow(); } catch(SecurityException ok) { return; }
925 <        } catch(RejectedExecutionException ex){
926 <            unexpectedException();
1034 >            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1035          } finally {
1036              joinPool(p);
1037          }
1038      }
1039  
1040      /**
1041 <     *  executor using DiscardPolicy drops task if saturated.
1041 >     * executor using DiscardPolicy drops task if saturated.
1042       */
1043      public void testSaturatedExecute3() {
1044          RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1045 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1045 >        ThreadPoolExecutor p =
1046 >            new CustomTPE(1, 1,
1047 >                          LONG_DELAY_MS, MILLISECONDS,
1048 >                          new ArrayBlockingQueue<Runnable>(1),
1049 >                          h);
1050          try {
939
1051              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1052 <            for(int i = 0; i < 5; ++i){
1052 >            for (int i = 0; i < tasks.length; ++i)
1053                  tasks[i] = new TrackedNoOpRunnable();
943            }
1054              p.execute(new TrackedLongRunnable());
1055 <            for(int i = 0; i < 5; ++i){
1056 <                p.execute(tasks[i]);
1057 <            }
1058 <            for(int i = 0; i < 5; ++i){
1059 <                assertFalse(tasks[i].done);
950 <            }
951 <            try { p.shutdownNow(); } catch(SecurityException ok) { return; }
952 <        } catch(RejectedExecutionException ex){
953 <            unexpectedException();
1055 >            for (TrackedNoOpRunnable task : tasks)
1056 >                p.execute(task);
1057 >            for (TrackedNoOpRunnable task : tasks)
1058 >                assertFalse(task.done);
1059 >            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1060          } finally {
1061              joinPool(p);
1062          }
1063      }
1064  
1065      /**
1066 <     *  executor using DiscardOldestPolicy drops oldest task if saturated.
1066 >     * executor using DiscardOldestPolicy drops oldest task if saturated.
1067       */
1068      public void testSaturatedExecute4() {
1069          RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
1070 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1070 >        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1071          try {
1072              p.execute(new TrackedLongRunnable());
1073              TrackedLongRunnable r2 = new TrackedLongRunnable();
# Line 971 | Line 1077 | public class ThreadPoolExecutorSubclassT
1077              p.execute(r3);
1078              assertFalse(p.getQueue().contains(r2));
1079              assertTrue(p.getQueue().contains(r3));
1080 <            try { p.shutdownNow(); } catch(SecurityException ok) { return; }
975 <        } catch(RejectedExecutionException ex){
976 <            unexpectedException();
1080 >            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1081          } finally {
1082              joinPool(p);
1083          }
1084      }
1085  
1086      /**
1087 <     *  execute throws RejectedExecutionException if shutdown
1087 >     * execute throws RejectedExecutionException if shutdown
1088       */
1089      public void testRejectedExecutionExceptionOnShutdown() {
1090 <        ThreadPoolExecutor tpe =
1091 <            new CustomTPE(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1092 <        try { tpe.shutdown(); } catch(SecurityException ok) { return; }
1093 <        try {
1094 <            tpe.execute(new NoOpRunnable());
1095 <            shouldThrow();
1096 <        } catch(RejectedExecutionException success){}
1090 >        ThreadPoolExecutor p =
1091 >            new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1092 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1093 >        try {
1094 >            p.execute(new NoOpRunnable());
1095 >            shouldThrow();
1096 >        } catch (RejectedExecutionException success) {}
1097  
1098 <        joinPool(tpe);
1098 >        joinPool(p);
1099      }
1100  
1101      /**
1102 <     *  execute using CallerRunsPolicy drops task on shutdown
1102 >     * execute using CallerRunsPolicy drops task on shutdown
1103       */
1104      public void testCallerRunsOnShutdown() {
1105          RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1106 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1106 >        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1107  
1108 <        try { p.shutdown(); } catch(SecurityException ok) { return; }
1109 <        try {
1108 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1109 >        try {
1110              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1111 <            p.execute(r);
1111 >            p.execute(r);
1112              assertFalse(r.done);
1009        } catch(RejectedExecutionException success){
1010            unexpectedException();
1113          } finally {
1114              joinPool(p);
1115          }
1116      }
1117  
1118      /**
1119 <     *  execute using DiscardPolicy drops task on shutdown
1119 >     * execute using DiscardPolicy drops task on shutdown
1120       */
1121      public void testDiscardOnShutdown() {
1122          RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1123 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1123 >        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1124  
1125 <        try { p.shutdown(); } catch(SecurityException ok) { return; }
1126 <        try {
1125 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1126 >        try {
1127              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1128 <            p.execute(r);
1128 >            p.execute(r);
1129              assertFalse(r.done);
1028        } catch(RejectedExecutionException success){
1029            unexpectedException();
1130          } finally {
1131              joinPool(p);
1132          }
1133      }
1134  
1035
1135      /**
1136 <     *  execute using DiscardOldestPolicy drops task on shutdown
1136 >     * execute using DiscardOldestPolicy drops task on shutdown
1137       */
1138      public void testDiscardOldestOnShutdown() {
1139          RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
1140 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1140 >        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1141  
1142 <        try { p.shutdown(); } catch(SecurityException ok) { return; }
1143 <        try {
1142 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1143 >        try {
1144              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1145 <            p.execute(r);
1145 >            p.execute(r);
1146              assertFalse(r.done);
1048        } catch(RejectedExecutionException success){
1049            unexpectedException();
1147          } finally {
1148              joinPool(p);
1149          }
1150      }
1151  
1055
1152      /**
1153 <     *  execute (null) throws NPE
1153 >     * execute(null) throws NPE
1154       */
1155      public void testExecuteNull() {
1156 <        ThreadPoolExecutor tpe = null;
1156 >        ThreadPoolExecutor p = null;
1157          try {
1158 <            tpe = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1159 <            tpe.execute(null);
1158 >            p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1159 >            p.execute(null);
1160              shouldThrow();
1161 <        } catch(NullPointerException success){}
1161 >        } catch (NullPointerException success) {}
1162  
1163 <        joinPool(tpe);
1163 >        joinPool(p);
1164      }
1165  
1166      /**
1167 <     *  setCorePoolSize of negative value throws IllegalArgumentException
1167 >     * setCorePoolSize of negative value throws IllegalArgumentException
1168       */
1169      public void testCorePoolSizeIllegalArgumentException() {
1170 <        ThreadPoolExecutor tpe = null;
1171 <        try {
1172 <            tpe = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1173 <        } catch(Exception e){}
1174 <        try {
1175 <            tpe.setCorePoolSize(-1);
1080 <            shouldThrow();
1081 <        } catch(IllegalArgumentException success){
1170 >        ThreadPoolExecutor p =
1171 >            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1172 >        try {
1173 >            p.setCorePoolSize(-1);
1174 >            shouldThrow();
1175 >        } catch (IllegalArgumentException success) {
1176          } finally {
1177 <            try { tpe.shutdown(); } catch(SecurityException ok) { return; }
1177 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1178          }
1179 <        joinPool(tpe);
1179 >        joinPool(p);
1180      }
1181  
1182      /**
1183 <     *  setMaximumPoolSize(int) throws IllegalArgumentException if
1184 <     *  given a value less the core pool size
1183 >     * setMaximumPoolSize(int) throws IllegalArgumentException
1184 >     * if given a value less the core pool size
1185       */
1186      public void testMaximumPoolSizeIllegalArgumentException() {
1187 <        ThreadPoolExecutor tpe = null;
1187 >        ThreadPoolExecutor p =
1188 >            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1189          try {
1190 <            tpe = new CustomTPE(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1096 <        } catch(Exception e){}
1097 <        try {
1098 <            tpe.setMaximumPoolSize(1);
1190 >            p.setMaximumPoolSize(1);
1191              shouldThrow();
1192 <        } catch(IllegalArgumentException success){
1192 >        } catch (IllegalArgumentException success) {
1193          } finally {
1194 <            try { tpe.shutdown(); } catch(SecurityException ok) { return; }
1194 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1195          }
1196 <        joinPool(tpe);
1196 >        joinPool(p);
1197      }
1198  
1199      /**
1200 <     *  setMaximumPoolSize throws IllegalArgumentException
1201 <     *  if given a negative value
1200 >     * setMaximumPoolSize throws IllegalArgumentException
1201 >     * if given a negative value
1202       */
1203      public void testMaximumPoolSizeIllegalArgumentException2() {
1204 <        ThreadPoolExecutor tpe = null;
1204 >        ThreadPoolExecutor p =
1205 >            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1206          try {
1207 <            tpe = new CustomTPE(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1115 <        } catch(Exception e){}
1116 <        try {
1117 <            tpe.setMaximumPoolSize(-1);
1207 >            p.setMaximumPoolSize(-1);
1208              shouldThrow();
1209 <        } catch(IllegalArgumentException success){
1209 >        } catch (IllegalArgumentException success) {
1210          } finally {
1211 <            try { tpe.shutdown(); } catch(SecurityException ok) { return; }
1211 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1212          }
1213 <        joinPool(tpe);
1213 >        joinPool(p);
1214      }
1215  
1126
1216      /**
1217 <     *  setKeepAliveTime  throws IllegalArgumentException
1218 <     *  when given a negative value
1217 >     * setKeepAliveTime throws IllegalArgumentException
1218 >     * when given a negative value
1219       */
1220      public void testKeepAliveTimeIllegalArgumentException() {
1221 <        ThreadPoolExecutor tpe = null;
1222 <        try {
1134 <            tpe = new CustomTPE(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1135 <        } catch(Exception e){}
1221 >        ThreadPoolExecutor p =
1222 >            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1223  
1224 <        try {
1225 <            tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS);
1224 >        try {
1225 >            p.setKeepAliveTime(-1,MILLISECONDS);
1226              shouldThrow();
1227 <        } catch(IllegalArgumentException success){
1227 >        } catch (IllegalArgumentException success) {
1228          } finally {
1229 <            try { tpe.shutdown(); } catch(SecurityException ok) { return; }
1229 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1230          }
1231 <        joinPool(tpe);
1231 >        joinPool(p);
1232      }
1233  
1234      /**
1235       * terminated() is called on termination
1236       */
1237      public void testTerminated() {
1238 <        CustomTPE tpe = new CustomTPE();
1239 <        try { tpe.shutdown(); } catch(SecurityException ok) { return; }
1240 <        assertTrue(tpe.terminatedCalled);
1241 <        joinPool(tpe);
1238 >        CustomTPE p = new CustomTPE();
1239 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1240 >        assertTrue(p.terminatedCalled());
1241 >        joinPool(p);
1242      }
1243  
1244      /**
1245       * beforeExecute and afterExecute are called when executing task
1246       */
1247 <    public void testBeforeAfter() {
1248 <        CustomTPE tpe = new CustomTPE();
1247 >    public void testBeforeAfter() throws InterruptedException {
1248 >        CustomTPE p = new CustomTPE();
1249          try {
1250 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1251 <            tpe.execute(r);
1252 <            Thread.sleep(SHORT_DELAY_MS);
1253 <            assertTrue(r.done);
1254 <            assertTrue(tpe.beforeCalled);
1255 <            assertTrue(tpe.afterCalled);
1256 <            try { tpe.shutdown(); } catch(SecurityException ok) { return; }
1257 <        }
1258 <        catch(Exception ex) {
1259 <            unexpectedException();
1250 >            final CountDownLatch done = new CountDownLatch(1);
1251 >            final CheckedRunnable task = new CheckedRunnable() {
1252 >                public void realRun() {
1253 >                    done.countDown();
1254 >                }};
1255 >            p.execute(task);
1256 >            await(p.afterCalled);
1257 >            assertEquals(0, done.getCount());
1258 >            assertTrue(p.afterCalled());
1259 >            assertTrue(p.beforeCalled());
1260 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1261          } finally {
1262 <            joinPool(tpe);
1262 >            joinPool(p);
1263          }
1264      }
1265  
1266      /**
1267       * completed submit of callable returns result
1268       */
1269 <    public void testSubmitCallable() {
1270 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1269 >    public void testSubmitCallable() throws Exception {
1270 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1271          try {
1272              Future<String> future = e.submit(new StringTask());
1273              String result = future.get();
1274              assertSame(TEST_STRING, result);
1187        }
1188        catch (ExecutionException ex) {
1189            unexpectedException();
1190        }
1191        catch (InterruptedException ex) {
1192            unexpectedException();
1275          } finally {
1276              joinPool(e);
1277          }
# Line 1198 | Line 1280 | public class ThreadPoolExecutorSubclassT
1280      /**
1281       * completed submit of runnable returns successfully
1282       */
1283 <    public void testSubmitRunnable() {
1284 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1283 >    public void testSubmitRunnable() throws Exception {
1284 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1285          try {
1286              Future<?> future = e.submit(new NoOpRunnable());
1287              future.get();
1288              assertTrue(future.isDone());
1207        }
1208        catch (ExecutionException ex) {
1209            unexpectedException();
1210        }
1211        catch (InterruptedException ex) {
1212            unexpectedException();
1289          } finally {
1290              joinPool(e);
1291          }
# Line 1218 | Line 1294 | public class ThreadPoolExecutorSubclassT
1294      /**
1295       * completed submit of (runnable, result) returns result
1296       */
1297 <    public void testSubmitRunnable2() {
1298 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1297 >    public void testSubmitRunnable2() throws Exception {
1298 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1299          try {
1300              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1301              String result = future.get();
1302              assertSame(TEST_STRING, result);
1227        }
1228        catch (ExecutionException ex) {
1229            unexpectedException();
1230        }
1231        catch (InterruptedException ex) {
1232            unexpectedException();
1303          } finally {
1304              joinPool(e);
1305          }
1306      }
1307  
1238
1239
1240
1241
1308      /**
1309       * invokeAny(null) throws NPE
1310       */
1311 <    public void testInvokeAny1() {
1312 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1311 >    public void testInvokeAny1() throws Exception {
1312 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1313          try {
1314              e.invokeAny(null);
1315 +            shouldThrow();
1316          } catch (NullPointerException success) {
1250        } catch(Exception ex) {
1251            unexpectedException();
1317          } finally {
1318              joinPool(e);
1319          }
# Line 1257 | Line 1322 | public class ThreadPoolExecutorSubclassT
1322      /**
1323       * invokeAny(empty collection) throws IAE
1324       */
1325 <    public void testInvokeAny2() {
1326 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1325 >    public void testInvokeAny2() throws Exception {
1326 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1327          try {
1328              e.invokeAny(new ArrayList<Callable<String>>());
1329 +            shouldThrow();
1330          } catch (IllegalArgumentException success) {
1265        } catch(Exception ex) {
1266            unexpectedException();
1331          } finally {
1332              joinPool(e);
1333          }
# Line 1272 | Line 1336 | public class ThreadPoolExecutorSubclassT
1336      /**
1337       * invokeAny(c) throws NPE if c has null elements
1338       */
1339 <    public void testInvokeAny3() {
1340 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1339 >    public void testInvokeAny3() throws Exception {
1340 >        CountDownLatch latch = new CountDownLatch(1);
1341 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1342 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1343 >        l.add(latchAwaitingStringTask(latch));
1344 >        l.add(null);
1345          try {
1278            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1279            l.add(new StringTask());
1280            l.add(null);
1346              e.invokeAny(l);
1347 +            shouldThrow();
1348          } catch (NullPointerException success) {
1283        } catch(Exception ex) {
1284            unexpectedException();
1349          } finally {
1350 +            latch.countDown();
1351              joinPool(e);
1352          }
1353      }
# Line 1290 | Line 1355 | public class ThreadPoolExecutorSubclassT
1355      /**
1356       * invokeAny(c) throws ExecutionException if no task completes
1357       */
1358 <    public void testInvokeAny4() {
1359 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1358 >    public void testInvokeAny4() throws Exception {
1359 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1360 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1361 >        l.add(new NPETask());
1362          try {
1296            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1297            l.add(new NPETask());
1363              e.invokeAny(l);
1364 +            shouldThrow();
1365          } catch (ExecutionException success) {
1366 <        } catch(Exception ex) {
1301 <            unexpectedException();
1366 >            assertTrue(success.getCause() instanceof NullPointerException);
1367          } finally {
1368              joinPool(e);
1369          }
# Line 1307 | Line 1372 | public class ThreadPoolExecutorSubclassT
1372      /**
1373       * invokeAny(c) returns result of some task
1374       */
1375 <    public void testInvokeAny5() {
1376 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1375 >    public void testInvokeAny5() throws Exception {
1376 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1377          try {
1378 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1378 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1379              l.add(new StringTask());
1380              l.add(new StringTask());
1381              String result = e.invokeAny(l);
1382              assertSame(TEST_STRING, result);
1318        } catch (ExecutionException success) {
1319        } catch(Exception ex) {
1320            unexpectedException();
1383          } finally {
1384              joinPool(e);
1385          }
# Line 1326 | Line 1388 | public class ThreadPoolExecutorSubclassT
1388      /**
1389       * invokeAll(null) throws NPE
1390       */
1391 <    public void testInvokeAll1() {
1392 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1391 >    public void testInvokeAll1() throws Exception {
1392 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1393          try {
1394              e.invokeAll(null);
1395 +            shouldThrow();
1396          } catch (NullPointerException success) {
1334        } catch(Exception ex) {
1335            unexpectedException();
1397          } finally {
1398              joinPool(e);
1399          }
# Line 1341 | Line 1402 | public class ThreadPoolExecutorSubclassT
1402      /**
1403       * invokeAll(empty collection) returns empty collection
1404       */
1405 <    public void testInvokeAll2() {
1406 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1405 >    public void testInvokeAll2() throws Exception {
1406 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1407          try {
1408              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1409              assertTrue(r.isEmpty());
1349        } catch(Exception ex) {
1350            unexpectedException();
1410          } finally {
1411              joinPool(e);
1412          }
# Line 1356 | Line 1415 | public class ThreadPoolExecutorSubclassT
1415      /**
1416       * invokeAll(c) throws NPE if c has null elements
1417       */
1418 <    public void testInvokeAll3() {
1419 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1418 >    public void testInvokeAll3() throws Exception {
1419 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1420 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1421 >        l.add(new StringTask());
1422 >        l.add(null);
1423          try {
1362            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1363            l.add(new StringTask());
1364            l.add(null);
1424              e.invokeAll(l);
1425 +            shouldThrow();
1426          } catch (NullPointerException success) {
1367        } catch(Exception ex) {
1368            unexpectedException();
1427          } finally {
1428              joinPool(e);
1429          }
# Line 1374 | Line 1432 | public class ThreadPoolExecutorSubclassT
1432      /**
1433       * get of element of invokeAll(c) throws exception on failed task
1434       */
1435 <    public void testInvokeAll4() {
1436 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1435 >    public void testInvokeAll4() throws Exception {
1436 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1437 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1438 >        l.add(new NPETask());
1439 >        List<Future<String>> futures = e.invokeAll(l);
1440 >        assertEquals(1, futures.size());
1441          try {
1442 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1443 <            l.add(new NPETask());
1444 <            List<Future<String>> result = e.invokeAll(l);
1445 <            assertEquals(1, result.size());
1384 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1385 <                it.next().get();
1386 <        } catch(ExecutionException success) {
1387 <        } catch(Exception ex) {
1388 <            unexpectedException();
1442 >            futures.get(0).get();
1443 >            shouldThrow();
1444 >        } catch (ExecutionException success) {
1445 >            assertTrue(success.getCause() instanceof NullPointerException);
1446          } finally {
1447              joinPool(e);
1448          }
# Line 1394 | Line 1451 | public class ThreadPoolExecutorSubclassT
1451      /**
1452       * invokeAll(c) returns results of all completed tasks
1453       */
1454 <    public void testInvokeAll5() {
1455 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1454 >    public void testInvokeAll5() throws Exception {
1455 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1456          try {
1457 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1457 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1458              l.add(new StringTask());
1459              l.add(new StringTask());
1460 <            List<Future<String>> result = e.invokeAll(l);
1461 <            assertEquals(2, result.size());
1462 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1463 <                assertSame(TEST_STRING, it.next().get());
1407 <        } catch (ExecutionException success) {
1408 <        } catch(Exception ex) {
1409 <            unexpectedException();
1460 >            List<Future<String>> futures = e.invokeAll(l);
1461 >            assertEquals(2, futures.size());
1462 >            for (Future<String> future : futures)
1463 >                assertSame(TEST_STRING, future.get());
1464          } finally {
1465              joinPool(e);
1466          }
1467      }
1468  
1415
1416
1469      /**
1470       * timed invokeAny(null) throws NPE
1471       */
1472 <    public void testTimedInvokeAny1() {
1473 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1472 >    public void testTimedInvokeAny1() throws Exception {
1473 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1474          try {
1475 <            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1475 >            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1476 >            shouldThrow();
1477          } catch (NullPointerException success) {
1425        } catch(Exception ex) {
1426            unexpectedException();
1478          } finally {
1479              joinPool(e);
1480          }
# Line 1432 | Line 1483 | public class ThreadPoolExecutorSubclassT
1483      /**
1484       * timed invokeAny(,,null) throws NPE
1485       */
1486 <    public void testTimedInvokeAnyNullTimeUnit() {
1487 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1486 >    public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1487 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1488 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1489 >        l.add(new StringTask());
1490          try {
1438            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1439            l.add(new StringTask());
1491              e.invokeAny(l, MEDIUM_DELAY_MS, null);
1492 +            shouldThrow();
1493          } catch (NullPointerException success) {
1442        } catch(Exception ex) {
1443            unexpectedException();
1494          } finally {
1495              joinPool(e);
1496          }
# Line 1449 | Line 1499 | public class ThreadPoolExecutorSubclassT
1499      /**
1500       * timed invokeAny(empty collection) throws IAE
1501       */
1502 <    public void testTimedInvokeAny2() {
1503 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1502 >    public void testTimedInvokeAny2() throws Exception {
1503 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1504          try {
1505 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1505 >            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1506 >            shouldThrow();
1507          } catch (IllegalArgumentException success) {
1457        } catch(Exception ex) {
1458            unexpectedException();
1508          } finally {
1509              joinPool(e);
1510          }
# Line 1464 | Line 1513 | public class ThreadPoolExecutorSubclassT
1513      /**
1514       * timed invokeAny(c) throws NPE if c has null elements
1515       */
1516 <    public void testTimedInvokeAny3() {
1517 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1516 >    public void testTimedInvokeAny3() throws Exception {
1517 >        CountDownLatch latch = new CountDownLatch(1);
1518 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1519 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1520 >        l.add(latchAwaitingStringTask(latch));
1521 >        l.add(null);
1522          try {
1523 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1524 <            l.add(new StringTask());
1472 <            l.add(null);
1473 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1523 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1524 >            shouldThrow();
1525          } catch (NullPointerException success) {
1475        } catch(Exception ex) {
1476            ex.printStackTrace();
1477            unexpectedException();
1526          } finally {
1527 +            latch.countDown();
1528              joinPool(e);
1529          }
1530      }
# Line 1483 | Line 1532 | public class ThreadPoolExecutorSubclassT
1532      /**
1533       * timed invokeAny(c) throws ExecutionException if no task completes
1534       */
1535 <    public void testTimedInvokeAny4() {
1536 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1535 >    public void testTimedInvokeAny4() throws Exception {
1536 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1537 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1538 >        l.add(new NPETask());
1539          try {
1540 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1541 <            l.add(new NPETask());
1542 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1543 <        } catch(ExecutionException success) {
1493 <        } catch(Exception ex) {
1494 <            unexpectedException();
1540 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1541 >            shouldThrow();
1542 >        } catch (ExecutionException success) {
1543 >            assertTrue(success.getCause() instanceof NullPointerException);
1544          } finally {
1545              joinPool(e);
1546          }
# Line 1500 | Line 1549 | public class ThreadPoolExecutorSubclassT
1549      /**
1550       * timed invokeAny(c) returns result of some task
1551       */
1552 <    public void testTimedInvokeAny5() {
1553 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1552 >    public void testTimedInvokeAny5() throws Exception {
1553 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1554          try {
1555 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1555 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1556              l.add(new StringTask());
1557              l.add(new StringTask());
1558 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1558 >            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1559              assertSame(TEST_STRING, result);
1511        } catch (ExecutionException success) {
1512        } catch(Exception ex) {
1513            unexpectedException();
1560          } finally {
1561              joinPool(e);
1562          }
# Line 1519 | Line 1565 | public class ThreadPoolExecutorSubclassT
1565      /**
1566       * timed invokeAll(null) throws NPE
1567       */
1568 <    public void testTimedInvokeAll1() {
1569 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1568 >    public void testTimedInvokeAll1() throws Exception {
1569 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1570          try {
1571 <            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1571 >            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1572 >            shouldThrow();
1573          } catch (NullPointerException success) {
1527        } catch(Exception ex) {
1528            unexpectedException();
1574          } finally {
1575              joinPool(e);
1576          }
# Line 1534 | Line 1579 | public class ThreadPoolExecutorSubclassT
1579      /**
1580       * timed invokeAll(,,null) throws NPE
1581       */
1582 <    public void testTimedInvokeAllNullTimeUnit() {
1583 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1582 >    public void testTimedInvokeAllNullTimeUnit() throws Exception {
1583 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1584 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1585 >        l.add(new StringTask());
1586          try {
1540            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1541            l.add(new StringTask());
1587              e.invokeAll(l, MEDIUM_DELAY_MS, null);
1588 +            shouldThrow();
1589          } catch (NullPointerException success) {
1544        } catch(Exception ex) {
1545            unexpectedException();
1590          } finally {
1591              joinPool(e);
1592          }
# Line 1551 | Line 1595 | public class ThreadPoolExecutorSubclassT
1595      /**
1596       * timed invokeAll(empty collection) returns empty collection
1597       */
1598 <    public void testTimedInvokeAll2() {
1599 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1598 >    public void testTimedInvokeAll2() throws Exception {
1599 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1600          try {
1601 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1601 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1602              assertTrue(r.isEmpty());
1559        } catch(Exception ex) {
1560            unexpectedException();
1603          } finally {
1604              joinPool(e);
1605          }
# Line 1566 | Line 1608 | public class ThreadPoolExecutorSubclassT
1608      /**
1609       * timed invokeAll(c) throws NPE if c has null elements
1610       */
1611 <    public void testTimedInvokeAll3() {
1612 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1611 >    public void testTimedInvokeAll3() throws Exception {
1612 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1613 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1614 >        l.add(new StringTask());
1615 >        l.add(null);
1616          try {
1617 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1618 <            l.add(new StringTask());
1574 <            l.add(null);
1575 <            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1617 >            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1618 >            shouldThrow();
1619          } catch (NullPointerException success) {
1577        } catch(Exception ex) {
1578            unexpectedException();
1620          } finally {
1621              joinPool(e);
1622          }
# Line 1584 | Line 1625 | public class ThreadPoolExecutorSubclassT
1625      /**
1626       * get of element of invokeAll(c) throws exception on failed task
1627       */
1628 <    public void testTimedInvokeAll4() {
1629 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1628 >    public void testTimedInvokeAll4() throws Exception {
1629 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1630 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1631 >        l.add(new NPETask());
1632 >        List<Future<String>> futures =
1633 >            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1634 >        assertEquals(1, futures.size());
1635          try {
1636 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1637 <            l.add(new NPETask());
1638 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1639 <            assertEquals(1, result.size());
1594 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1595 <                it.next().get();
1596 <        } catch(ExecutionException success) {
1597 <        } catch(Exception ex) {
1598 <            unexpectedException();
1636 >            futures.get(0).get();
1637 >            shouldThrow();
1638 >        } catch (ExecutionException success) {
1639 >            assertTrue(success.getCause() instanceof NullPointerException);
1640          } finally {
1641              joinPool(e);
1642          }
# Line 1604 | Line 1645 | public class ThreadPoolExecutorSubclassT
1645      /**
1646       * timed invokeAll(c) returns results of all completed tasks
1647       */
1648 <    public void testTimedInvokeAll5() {
1649 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1648 >    public void testTimedInvokeAll5() throws Exception {
1649 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1650          try {
1651 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1651 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1652              l.add(new StringTask());
1653              l.add(new StringTask());
1654 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1655 <            assertEquals(2, result.size());
1656 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1657 <                assertSame(TEST_STRING, it.next().get());
1658 <        } catch (ExecutionException success) {
1618 <        } catch(Exception ex) {
1619 <            unexpectedException();
1654 >            List<Future<String>> futures =
1655 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1656 >            assertEquals(2, futures.size());
1657 >            for (Future<String> future : futures)
1658 >                assertSame(TEST_STRING, future.get());
1659          } finally {
1660              joinPool(e);
1661          }
# Line 1625 | Line 1664 | public class ThreadPoolExecutorSubclassT
1664      /**
1665       * timed invokeAll(c) cancels tasks not completed by timeout
1666       */
1667 <    public void testTimedInvokeAll6() {
1668 <        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1667 >    public void testTimedInvokeAll6() throws Exception {
1668 >        ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1669          try {
1670 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1670 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1671              l.add(new StringTask());
1672              l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1673              l.add(new StringTask());
1674 <            List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
1675 <            assertEquals(3, result.size());
1676 <            Iterator<Future<String>> it = result.iterator();
1677 <            Future<String> f1 = it.next();
1678 <            Future<String> f2 = it.next();
1679 <            Future<String> f3 = it.next();
1680 <            assertTrue(f1.isDone());
1642 <            assertTrue(f2.isDone());
1643 <            assertTrue(f3.isDone());
1644 <            assertFalse(f1.isCancelled());
1645 <            assertTrue(f2.isCancelled());
1646 <        } catch(Exception ex) {
1647 <            unexpectedException();
1674 >            List<Future<String>> futures =
1675 >                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1676 >            assertEquals(l.size(), futures.size());
1677 >            for (Future future : futures)
1678 >                assertTrue(future.isDone());
1679 >            assertFalse(futures.get(0).isCancelled());
1680 >            assertTrue(futures.get(1).isCancelled());
1681          } finally {
1682              joinPool(e);
1683          }
# Line 1654 | Line 1687 | public class ThreadPoolExecutorSubclassT
1687       * Execution continues if there is at least one thread even if
1688       * thread factory fails to create more
1689       */
1690 <    public void testFailingThreadFactory() {
1691 <        ExecutorService e = new CustomTPE(100, 100, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1692 <        try {
1693 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1694 <            for (int k = 0; k < 100; ++k) {
1695 <                e.execute(new NoOpRunnable());
1696 <            }
1697 <            Thread.sleep(LONG_DELAY_MS);
1698 <        } catch(Exception ex) {
1699 <            unexpectedException();
1690 >    public void testFailingThreadFactory() throws InterruptedException {
1691 >        final ExecutorService e =
1692 >            new CustomTPE(100, 100,
1693 >                          LONG_DELAY_MS, MILLISECONDS,
1694 >                          new LinkedBlockingQueue<Runnable>(),
1695 >                          new FailingThreadFactory());
1696 >        try {
1697 >            final int TASKS = 100;
1698 >            final CountDownLatch done = new CountDownLatch(TASKS);
1699 >            for (int k = 0; k < TASKS; ++k)
1700 >                e.execute(new CheckedRunnable() {
1701 >                    public void realRun() {
1702 >                        done.countDown();
1703 >                    }});
1704 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1705          } finally {
1706              joinPool(e);
1707          }
# Line 1673 | Line 1711 | public class ThreadPoolExecutorSubclassT
1711       * allowsCoreThreadTimeOut is by default false.
1712       */
1713      public void testAllowsCoreThreadTimeOut() {
1714 <        ThreadPoolExecutor tpe = new CustomTPE(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1715 <        assertFalse(tpe.allowsCoreThreadTimeOut());
1716 <        joinPool(tpe);
1714 >        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1715 >        assertFalse(p.allowsCoreThreadTimeOut());
1716 >        joinPool(p);
1717      }
1718  
1719      /**
1720       * allowCoreThreadTimeOut(true) causes idle threads to time out
1721       */
1722 <    public void testAllowCoreThreadTimeOut_true() {
1723 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1724 <        tpe.allowCoreThreadTimeOut(true);
1725 <        tpe.execute(new NoOpRunnable());
1726 <        try {
1727 <            Thread.sleep(MEDIUM_DELAY_MS);
1728 <            assertEquals(0, tpe.getPoolSize());
1729 <        } catch(InterruptedException e){
1730 <            unexpectedException();
1722 >    public void testAllowCoreThreadTimeOut_true() throws Exception {
1723 >        long coreThreadTimeOut = SHORT_DELAY_MS;
1724 >        final ThreadPoolExecutor p =
1725 >            new CustomTPE(2, 10,
1726 >                          coreThreadTimeOut, MILLISECONDS,
1727 >                          new ArrayBlockingQueue<Runnable>(10));
1728 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1729 >        try {
1730 >            p.allowCoreThreadTimeOut(true);
1731 >            p.execute(new CheckedRunnable() {
1732 >                public void realRun() throws InterruptedException {
1733 >                    threadStarted.countDown();
1734 >                    assertEquals(1, p.getPoolSize());
1735 >                }});
1736 >            await(threadStarted);
1737 >            delay(coreThreadTimeOut);
1738 >            long startTime = System.nanoTime();
1739 >            while (p.getPoolSize() > 0
1740 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1741 >                Thread.yield();
1742 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1743 >            assertEquals(0, p.getPoolSize());
1744          } finally {
1745 <            joinPool(tpe);
1745 >            joinPool(p);
1746          }
1747      }
1748  
1749      /**
1750       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1751       */
1752 <    public void testAllowCoreThreadTimeOut_false() {
1753 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1754 <        tpe.allowCoreThreadTimeOut(false);
1755 <        tpe.execute(new NoOpRunnable());
1756 <        try {
1757 <            Thread.sleep(MEDIUM_DELAY_MS);
1758 <            assertTrue(tpe.getPoolSize() >= 1);
1759 <        } catch(InterruptedException e){
1760 <            unexpectedException();
1752 >    public void testAllowCoreThreadTimeOut_false() throws Exception {
1753 >        long coreThreadTimeOut = SHORT_DELAY_MS;
1754 >        final ThreadPoolExecutor p =
1755 >            new CustomTPE(2, 10,
1756 >                          coreThreadTimeOut, MILLISECONDS,
1757 >                          new ArrayBlockingQueue<Runnable>(10));
1758 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1759 >        try {
1760 >            p.allowCoreThreadTimeOut(false);
1761 >            p.execute(new CheckedRunnable() {
1762 >                public void realRun() throws InterruptedException {
1763 >                    threadStarted.countDown();
1764 >                    assertTrue(p.getPoolSize() >= 1);
1765 >                }});
1766 >            delay(2 * coreThreadTimeOut);
1767 >            assertTrue(p.getPoolSize() >= 1);
1768          } finally {
1769 <            joinPool(tpe);
1769 >            joinPool(p);
1770          }
1771      }
1772  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines