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.9 by jsr166, Sat Nov 21 02:33:20 2009 UTC vs.
Revision 1.43 by jsr166, Mon Sep 28 08:23:49 2015 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.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 < import java.util.concurrent.locks.*;
10 > import static java.util.concurrent.TimeUnit.SECONDS;
11  
12 < import junit.framework.*;
13 < import java.util.*;
12 > import java.util.ArrayList;
13 > import java.util.List;
14 > import java.util.concurrent.ArrayBlockingQueue;
15 > import java.util.concurrent.BlockingQueue;
16 > import java.util.concurrent.Callable;
17 > import java.util.concurrent.CancellationException;
18 > import java.util.concurrent.CountDownLatch;
19 > import java.util.concurrent.ExecutionException;
20 > import java.util.concurrent.Executors;
21 > import java.util.concurrent.ExecutorService;
22 > import java.util.concurrent.Future;
23 > import java.util.concurrent.FutureTask;
24 > import java.util.concurrent.LinkedBlockingQueue;
25 > import java.util.concurrent.RejectedExecutionException;
26 > import java.util.concurrent.RejectedExecutionHandler;
27 > import java.util.concurrent.RunnableFuture;
28 > import java.util.concurrent.SynchronousQueue;
29 > import java.util.concurrent.ThreadFactory;
30 > import java.util.concurrent.ThreadPoolExecutor;
31 > import java.util.concurrent.TimeoutException;
32 > import java.util.concurrent.TimeUnit;
33 > import java.util.concurrent.atomic.AtomicInteger;
34 > import java.util.concurrent.locks.Condition;
35 > import java.util.concurrent.locks.ReentrantLock;
36 >
37 > import junit.framework.Test;
38 > import junit.framework.TestSuite;
39  
40   public class ThreadPoolExecutorSubclassTest extends JSR166TestCase {
41      public static void main(String[] args) {
42 <        junit.textui.TestRunner.run(suite());
42 >        main(suite(), args);
43      }
44      public static Test suite() {
45          return new TestSuite(ThreadPoolExecutorSubclassTest.class);
# Line 37 | Line 61 | public class ThreadPoolExecutorSubclassT
61          CustomTask(final Runnable r, final V res) {
62              if (r == null) throw new NullPointerException();
63              callable = new Callable<V>() {
64 <            public V call() throws Exception { r.run(); return res; }};
64 >                public V call() throws Exception { r.run(); return res; }};
65          }
66          public boolean isDone() {
67              lock.lock(); try { return done; } finally { lock.unlock() ; }
# Line 60 | Line 84 | public class ThreadPoolExecutorSubclassT
84              finally { lock.unlock() ; }
85          }
86          public void run() {
63            boolean runme;
87              lock.lock();
88              try {
89 <                runme = !done;
90 <                if (!runme)
91 <                    thread = Thread.currentThread();
89 >                if (done)
90 >                    return;
91 >                thread = Thread.currentThread();
92              }
93              finally { lock.unlock() ; }
71            if (!runme) return;
94              V v = null;
95              Exception e = null;
96              try {
# Line 117 | Line 139 | public class ThreadPoolExecutorSubclassT
139          }
140      }
141  
120
142      static class CustomTPE extends ThreadPoolExecutor {
143          protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
144              return new CustomTask<V>(c);
# Line 164 | Line 185 | public class ThreadPoolExecutorSubclassT
185                workQueue, threadFactory, handler);
186          }
187  
188 <        volatile boolean beforeCalled = false;
189 <        volatile boolean afterCalled = false;
190 <        volatile boolean terminatedCalled = false;
188 >        final CountDownLatch beforeCalled = new CountDownLatch(1);
189 >        final CountDownLatch afterCalled = new CountDownLatch(1);
190 >        final CountDownLatch terminatedCalled = new CountDownLatch(1);
191 >
192          public CustomTPE() {
193              super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
194          }
195          protected void beforeExecute(Thread t, Runnable r) {
196 <            beforeCalled = true;
196 >            beforeCalled.countDown();
197          }
198          protected void afterExecute(Runnable r, Throwable t) {
199 <            afterCalled = true;
199 >            afterCalled.countDown();
200          }
201          protected void terminated() {
202 <            terminatedCalled = true;
202 >            terminatedCalled.countDown();
203          }
204  
205 +        public boolean beforeCalled() {
206 +            return beforeCalled.getCount() == 0;
207 +        }
208 +        public boolean afterCalled() {
209 +            return afterCalled.getCount() == 0;
210 +        }
211 +        public boolean terminatedCalled() {
212 +            return terminatedCalled.getCount() == 0;
213 +        }
214      }
215  
216      static class FailingThreadFactory implements ThreadFactory {
# Line 190 | Line 221 | public class ThreadPoolExecutorSubclassT
221          }
222      }
223  
193
224      /**
225 <     *  execute successfully executes a runnable
225 >     * execute successfully executes a runnable
226       */
227      public void testExecute() throws InterruptedException {
228 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
228 >        final ThreadPoolExecutor p =
229 >            new CustomTPE(1, 1,
230 >                          LONG_DELAY_MS, MILLISECONDS,
231 >                          new ArrayBlockingQueue<Runnable>(10));
232 >        final CountDownLatch done = new CountDownLatch(1);
233 >        final Runnable task = new CheckedRunnable() {
234 >            public void realRun() {
235 >                done.countDown();
236 >            }};
237          try {
238 <            p1.execute(new ShortRunnable());
239 <            Thread.sleep(SMALL_DELAY_MS);
238 >            p.execute(task);
239 >            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
240          } finally {
241 <            joinPool(p1);
241 >            joinPool(p);
242          }
243      }
244  
245      /**
246 <     *  getActiveCount increases but doesn't overestimate, when a
247 <     *  thread becomes active
246 >     * getActiveCount increases but doesn't overestimate, when a
247 >     * thread becomes active
248       */
249      public void testGetActiveCount() throws InterruptedException {
250 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
251 <        assertEquals(0, p2.getActiveCount());
252 <        p2.execute(new MediumRunnable());
253 <        Thread.sleep(SHORT_DELAY_MS);
254 <        assertEquals(1, p2.getActiveCount());
255 <        joinPool(p2);
250 >        final ThreadPoolExecutor p =
251 >            new CustomTPE(2, 2,
252 >                          LONG_DELAY_MS, MILLISECONDS,
253 >                          new ArrayBlockingQueue<Runnable>(10));
254 >        final CountDownLatch threadStarted = new CountDownLatch(1);
255 >        final CountDownLatch done = new CountDownLatch(1);
256 >        try {
257 >            assertEquals(0, p.getActiveCount());
258 >            p.execute(new CheckedRunnable() {
259 >                public void realRun() throws InterruptedException {
260 >                    threadStarted.countDown();
261 >                    assertEquals(1, p.getActiveCount());
262 >                    done.await();
263 >                }});
264 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
265 >            assertEquals(1, p.getActiveCount());
266 >        } finally {
267 >            done.countDown();
268 >            joinPool(p);
269 >        }
270      }
271  
272      /**
273 <     *  prestartCoreThread starts a thread if under corePoolSize, else doesn't
273 >     * prestartCoreThread starts a thread if under corePoolSize, else doesn't
274       */
275      public void testPrestartCoreThread() {
276 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
277 <        assertEquals(0, p2.getPoolSize());
278 <        assertTrue(p2.prestartCoreThread());
279 <        assertEquals(1, p2.getPoolSize());
280 <        assertTrue(p2.prestartCoreThread());
281 <        assertEquals(2, p2.getPoolSize());
282 <        assertFalse(p2.prestartCoreThread());
283 <        assertEquals(2, p2.getPoolSize());
284 <        joinPool(p2);
276 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
277 >        assertEquals(0, p.getPoolSize());
278 >        assertTrue(p.prestartCoreThread());
279 >        assertEquals(1, p.getPoolSize());
280 >        assertTrue(p.prestartCoreThread());
281 >        assertEquals(2, p.getPoolSize());
282 >        assertFalse(p.prestartCoreThread());
283 >        assertEquals(2, p.getPoolSize());
284 >        joinPool(p);
285      }
286  
287      /**
288 <     *  prestartAllCoreThreads starts all corePoolSize threads
288 >     * prestartAllCoreThreads starts all corePoolSize threads
289       */
290      public void testPrestartAllCoreThreads() {
291 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
292 <        assertEquals(0, p2.getPoolSize());
293 <        p2.prestartAllCoreThreads();
294 <        assertEquals(2, p2.getPoolSize());
295 <        p2.prestartAllCoreThreads();
296 <        assertEquals(2, p2.getPoolSize());
297 <        joinPool(p2);
291 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
292 >        assertEquals(0, p.getPoolSize());
293 >        p.prestartAllCoreThreads();
294 >        assertEquals(2, p.getPoolSize());
295 >        p.prestartAllCoreThreads();
296 >        assertEquals(2, p.getPoolSize());
297 >        joinPool(p);
298      }
299  
300      /**
301 <     *   getCompletedTaskCount increases, but doesn't overestimate,
302 <     *   when tasks complete
301 >     * getCompletedTaskCount increases, but doesn't overestimate,
302 >     * when tasks complete
303       */
304      public void testGetCompletedTaskCount() throws InterruptedException {
305 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
306 <        assertEquals(0, p2.getCompletedTaskCount());
307 <        p2.execute(new ShortRunnable());
308 <        Thread.sleep(SMALL_DELAY_MS);
309 <        assertEquals(1, p2.getCompletedTaskCount());
310 <        try { p2.shutdown(); } catch (SecurityException ok) { return; }
311 <        joinPool(p2);
305 >        final ThreadPoolExecutor p =
306 >            new CustomTPE(2, 2,
307 >                          LONG_DELAY_MS, MILLISECONDS,
308 >                          new ArrayBlockingQueue<Runnable>(10));
309 >        final CountDownLatch threadStarted = new CountDownLatch(1);
310 >        final CountDownLatch threadProceed = new CountDownLatch(1);
311 >        final CountDownLatch threadDone = new CountDownLatch(1);
312 >        try {
313 >            assertEquals(0, p.getCompletedTaskCount());
314 >            p.execute(new CheckedRunnable() {
315 >                public void realRun() throws InterruptedException {
316 >                    threadStarted.countDown();
317 >                    assertEquals(0, p.getCompletedTaskCount());
318 >                    threadProceed.await();
319 >                    threadDone.countDown();
320 >                }});
321 >            await(threadStarted);
322 >            assertEquals(0, p.getCompletedTaskCount());
323 >            threadProceed.countDown();
324 >            threadDone.await();
325 >            long startTime = System.nanoTime();
326 >            while (p.getCompletedTaskCount() != 1) {
327 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
328 >                    fail("timed out");
329 >                Thread.yield();
330 >            }
331 >        } finally {
332 >            joinPool(p);
333 >        }
334      }
335  
336      /**
337 <     *   getCorePoolSize returns size given in constructor if not otherwise set
337 >     * getCorePoolSize returns size given in constructor if not otherwise set
338       */
339      public void testGetCorePoolSize() {
340 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
341 <        assertEquals(1, p1.getCorePoolSize());
342 <        joinPool(p1);
340 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
341 >        assertEquals(1, p.getCorePoolSize());
342 >        joinPool(p);
343      }
344  
345      /**
346 <     *   getKeepAliveTime returns value given in constructor if not otherwise set
346 >     * getKeepAliveTime returns value given in constructor if not otherwise set
347       */
348      public void testGetKeepAliveTime() {
349 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
350 <        assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS));
351 <        joinPool(p2);
349 >        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
350 >        assertEquals(1, p.getKeepAliveTime(SECONDS));
351 >        joinPool(p);
352      }
353  
280
354      /**
355       * getThreadFactory returns factory in constructor if not set
356       */
# Line 299 | Line 372 | public class ThreadPoolExecutorSubclassT
372          joinPool(p);
373      }
374  
302
375      /**
376       * setThreadFactory(null) throws NPE
377       */
# Line 336 | Line 408 | public class ThreadPoolExecutorSubclassT
408          joinPool(p);
409      }
410  
339
411      /**
412       * setRejectedExecutionHandler(null) throws NPE
413       */
# Line 351 | Line 422 | public class ThreadPoolExecutorSubclassT
422          }
423      }
424  
354
425      /**
426 <     *   getLargestPoolSize increases, but doesn't overestimate, when
427 <     *   multiple threads active
426 >     * getLargestPoolSize increases, but doesn't overestimate, when
427 >     * multiple threads active
428       */
429      public void testGetLargestPoolSize() throws InterruptedException {
430 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
431 <        assertEquals(0, p2.getLargestPoolSize());
432 <        p2.execute(new MediumRunnable());
433 <        p2.execute(new MediumRunnable());
434 <        Thread.sleep(SHORT_DELAY_MS);
435 <        assertEquals(2, p2.getLargestPoolSize());
436 <        joinPool(p2);
430 >        final int THREADS = 3;
431 >        final ThreadPoolExecutor p =
432 >            new CustomTPE(THREADS, THREADS,
433 >                          LONG_DELAY_MS, MILLISECONDS,
434 >                          new ArrayBlockingQueue<Runnable>(10));
435 >        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
436 >        final CountDownLatch done = new CountDownLatch(1);
437 >        try {
438 >            assertEquals(0, p.getLargestPoolSize());
439 >            for (int i = 0; i < THREADS; i++)
440 >                p.execute(new CheckedRunnable() {
441 >                    public void realRun() throws InterruptedException {
442 >                        threadsStarted.countDown();
443 >                        done.await();
444 >                        assertEquals(THREADS, p.getLargestPoolSize());
445 >                    }});
446 >            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
447 >            assertEquals(THREADS, p.getLargestPoolSize());
448 >        } finally {
449 >            done.countDown();
450 >            joinPool(p);
451 >            assertEquals(THREADS, p.getLargestPoolSize());
452 >        }
453      }
454  
455      /**
456 <     *   getMaximumPoolSize returns value given in constructor if not
457 <     *   otherwise set
456 >     * getMaximumPoolSize returns value given in constructor if not
457 >     * otherwise set
458       */
459      public void testGetMaximumPoolSize() {
460 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
461 <        assertEquals(2, p2.getMaximumPoolSize());
462 <        joinPool(p2);
460 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
461 >        assertEquals(2, p.getMaximumPoolSize());
462 >        joinPool(p);
463      }
464  
465      /**
466 <     *   getPoolSize increases, but doesn't overestimate, when threads
467 <     *   become active
466 >     * getPoolSize increases, but doesn't overestimate, when threads
467 >     * become active
468       */
469 <    public void testGetPoolSize() {
470 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
471 <        assertEquals(0, p1.getPoolSize());
472 <        p1.execute(new MediumRunnable());
473 <        assertEquals(1, p1.getPoolSize());
474 <        joinPool(p1);
469 >    public void testGetPoolSize() throws InterruptedException {
470 >        final ThreadPoolExecutor p =
471 >            new CustomTPE(1, 1,
472 >                          LONG_DELAY_MS, MILLISECONDS,
473 >                          new ArrayBlockingQueue<Runnable>(10));
474 >        final CountDownLatch threadStarted = new CountDownLatch(1);
475 >        final CountDownLatch done = new CountDownLatch(1);
476 >        try {
477 >            assertEquals(0, p.getPoolSize());
478 >            p.execute(new CheckedRunnable() {
479 >                public void realRun() throws InterruptedException {
480 >                    threadStarted.countDown();
481 >                    assertEquals(1, p.getPoolSize());
482 >                    done.await();
483 >                }});
484 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
485 >            assertEquals(1, p.getPoolSize());
486 >        } finally {
487 >            done.countDown();
488 >            joinPool(p);
489 >        }
490      }
491  
492      /**
493 <     *  getTaskCount increases, but doesn't overestimate, when tasks submitted
493 >     * getTaskCount increases, but doesn't overestimate, when tasks submitted
494       */
495      public void testGetTaskCount() throws InterruptedException {
496 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
497 <        assertEquals(0, p1.getTaskCount());
498 <        p1.execute(new MediumRunnable());
499 <        Thread.sleep(SHORT_DELAY_MS);
500 <        assertEquals(1, p1.getTaskCount());
501 <        joinPool(p1);
496 >        final ThreadPoolExecutor p =
497 >            new CustomTPE(1, 1,
498 >                          LONG_DELAY_MS, MILLISECONDS,
499 >                          new ArrayBlockingQueue<Runnable>(10));
500 >        final CountDownLatch threadStarted = new CountDownLatch(1);
501 >        final CountDownLatch done = new CountDownLatch(1);
502 >        try {
503 >            assertEquals(0, p.getTaskCount());
504 >            p.execute(new CheckedRunnable() {
505 >                public void realRun() throws InterruptedException {
506 >                    threadStarted.countDown();
507 >                    assertEquals(1, p.getTaskCount());
508 >                    done.await();
509 >                }});
510 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
511 >            assertEquals(1, p.getTaskCount());
512 >        } finally {
513 >            done.countDown();
514 >            joinPool(p);
515 >        }
516      }
517  
518      /**
519 <     *   isShutDown is false before shutdown, true after
519 >     * isShutdown is false before shutdown, true after
520       */
521      public void testIsShutdown() {
522  
523 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
524 <        assertFalse(p1.isShutdown());
525 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
526 <        assertTrue(p1.isShutdown());
527 <        joinPool(p1);
523 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
524 >        assertFalse(p.isShutdown());
525 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
526 >        assertTrue(p.isShutdown());
527 >        joinPool(p);
528      }
529  
415
530      /**
531 <     *  isTerminated is false before termination, true after
531 >     * isTerminated is false before termination, true after
532       */
533      public void testIsTerminated() throws InterruptedException {
534 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
535 <        assertFalse(p1.isTerminated());
536 <        try {
537 <            p1.execute(new MediumRunnable());
538 <        } finally {
539 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
540 <        }
541 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
542 <        assertTrue(p1.isTerminated());
534 >        final ThreadPoolExecutor p =
535 >            new CustomTPE(1, 1,
536 >                          LONG_DELAY_MS, MILLISECONDS,
537 >                          new ArrayBlockingQueue<Runnable>(10));
538 >        final CountDownLatch threadStarted = new CountDownLatch(1);
539 >        final CountDownLatch done = new CountDownLatch(1);
540 >        try {
541 >            assertFalse(p.isTerminating());
542 >            p.execute(new CheckedRunnable() {
543 >                public void realRun() throws InterruptedException {
544 >                    assertFalse(p.isTerminating());
545 >                    threadStarted.countDown();
546 >                    done.await();
547 >                }});
548 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
549 >            assertFalse(p.isTerminating());
550 >            done.countDown();
551 >        } finally {
552 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
553 >        }
554 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
555 >        assertTrue(p.isTerminated());
556 >        assertFalse(p.isTerminating());
557      }
558  
559      /**
560 <     *  isTerminating is not true when running or when terminated
560 >     * isTerminating is not true when running or when terminated
561       */
562      public void testIsTerminating() throws InterruptedException {
563 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
564 <        assertFalse(p1.isTerminating());
565 <        try {
566 <            p1.execute(new SmallRunnable());
567 <            assertFalse(p1.isTerminating());
568 <        } finally {
569 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
570 <        }
571 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
572 <        assertTrue(p1.isTerminated());
573 <        assertFalse(p1.isTerminating());
563 >        final ThreadPoolExecutor p =
564 >            new CustomTPE(1, 1,
565 >                          LONG_DELAY_MS, MILLISECONDS,
566 >                          new ArrayBlockingQueue<Runnable>(10));
567 >        final CountDownLatch threadStarted = new CountDownLatch(1);
568 >        final CountDownLatch done = new CountDownLatch(1);
569 >        try {
570 >            assertFalse(p.isTerminating());
571 >            p.execute(new CheckedRunnable() {
572 >                public void realRun() throws InterruptedException {
573 >                    assertFalse(p.isTerminating());
574 >                    threadStarted.countDown();
575 >                    done.await();
576 >                }});
577 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
578 >            assertFalse(p.isTerminating());
579 >            done.countDown();
580 >        } finally {
581 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
582 >        }
583 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
584 >        assertTrue(p.isTerminated());
585 >        assertFalse(p.isTerminating());
586      }
587  
588      /**
589       * getQueue returns the work queue, which contains queued tasks
590       */
591      public void testGetQueue() throws InterruptedException {
592 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
593 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
594 <        FutureTask[] tasks = new FutureTask[5];
595 <        for (int i = 0; i < 5; i++) {
596 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
597 <            p1.execute(tasks[i]);
598 <        }
599 <        try {
600 <            Thread.sleep(SHORT_DELAY_MS);
601 <            BlockingQueue<Runnable> wq = p1.getQueue();
602 <            assertSame(q, wq);
603 <            assertFalse(wq.contains(tasks[0]));
604 <            assertTrue(wq.contains(tasks[4]));
605 <            for (int i = 1; i < 5; ++i)
606 <                tasks[i].cancel(true);
607 <            p1.shutdownNow();
592 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
593 >        final ThreadPoolExecutor p =
594 >            new CustomTPE(1, 1,
595 >                          LONG_DELAY_MS, MILLISECONDS,
596 >                          q);
597 >        final CountDownLatch threadStarted = new CountDownLatch(1);
598 >        final CountDownLatch done = new CountDownLatch(1);
599 >        try {
600 >            FutureTask[] tasks = new FutureTask[5];
601 >            for (int i = 0; i < tasks.length; i++) {
602 >                Callable task = new CheckedCallable<Boolean>() {
603 >                    public Boolean realCall() throws InterruptedException {
604 >                        threadStarted.countDown();
605 >                        assertSame(q, p.getQueue());
606 >                        done.await();
607 >                        return Boolean.TRUE;
608 >                    }};
609 >                tasks[i] = new FutureTask(task);
610 >                p.execute(tasks[i]);
611 >            }
612 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
613 >            assertSame(q, p.getQueue());
614 >            assertFalse(q.contains(tasks[0]));
615 >            assertTrue(q.contains(tasks[tasks.length - 1]));
616 >            assertEquals(tasks.length - 1, q.size());
617          } finally {
618 <            joinPool(p1);
618 >            done.countDown();
619 >            joinPool(p);
620          }
621      }
622  
# Line 475 | Line 625 | public class ThreadPoolExecutorSubclassT
625       */
626      public void testRemove() throws InterruptedException {
627          BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
628 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
629 <        FutureTask[] tasks = new FutureTask[5];
630 <        for (int i = 0; i < 5; i++) {
631 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
632 <            p1.execute(tasks[i]);
633 <        }
634 <        try {
635 <            Thread.sleep(SHORT_DELAY_MS);
636 <            assertFalse(p1.remove(tasks[0]));
628 >        final ThreadPoolExecutor p =
629 >            new CustomTPE(1, 1,
630 >                          LONG_DELAY_MS, MILLISECONDS,
631 >                          q);
632 >        Runnable[] tasks = new Runnable[6];
633 >        final CountDownLatch threadStarted = new CountDownLatch(1);
634 >        final CountDownLatch done = new CountDownLatch(1);
635 >        try {
636 >            for (int i = 0; i < tasks.length; i++) {
637 >                tasks[i] = new CheckedRunnable() {
638 >                        public void realRun() throws InterruptedException {
639 >                            threadStarted.countDown();
640 >                            done.await();
641 >                        }};
642 >                p.execute(tasks[i]);
643 >            }
644 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
645 >            assertFalse(p.remove(tasks[0]));
646              assertTrue(q.contains(tasks[4]));
647              assertTrue(q.contains(tasks[3]));
648 <            assertTrue(p1.remove(tasks[4]));
649 <            assertFalse(p1.remove(tasks[4]));
648 >            assertTrue(p.remove(tasks[4]));
649 >            assertFalse(p.remove(tasks[4]));
650              assertFalse(q.contains(tasks[4]));
651              assertTrue(q.contains(tasks[3]));
652 <            assertTrue(p1.remove(tasks[3]));
652 >            assertTrue(p.remove(tasks[3]));
653              assertFalse(q.contains(tasks[3]));
654          } finally {
655 <            joinPool(p1);
655 >            done.countDown();
656 >            joinPool(p);
657          }
658      }
659  
660      /**
661 <     *   purge removes cancelled tasks from the queue
661 >     * purge removes cancelled tasks from the queue
662       */
663 <    public void testPurge() {
664 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
663 >    public void testPurge() throws InterruptedException {
664 >        final CountDownLatch threadStarted = new CountDownLatch(1);
665 >        final CountDownLatch done = new CountDownLatch(1);
666 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
667 >        final ThreadPoolExecutor p =
668 >            new CustomTPE(1, 1,
669 >                          LONG_DELAY_MS, MILLISECONDS,
670 >                          q);
671          FutureTask[] tasks = new FutureTask[5];
672 <        for (int i = 0; i < 5; i++) {
673 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
674 <            p1.execute(tasks[i]);
672 >        try {
673 >            for (int i = 0; i < tasks.length; i++) {
674 >                Callable task = new CheckedCallable<Boolean>() {
675 >                    public Boolean realCall() throws InterruptedException {
676 >                        threadStarted.countDown();
677 >                        done.await();
678 >                        return Boolean.TRUE;
679 >                    }};
680 >                tasks[i] = new FutureTask(task);
681 >                p.execute(tasks[i]);
682 >            }
683 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
684 >            assertEquals(tasks.length, p.getTaskCount());
685 >            assertEquals(tasks.length - 1, q.size());
686 >            assertEquals(1L, p.getActiveCount());
687 >            assertEquals(0L, p.getCompletedTaskCount());
688 >            tasks[4].cancel(true);
689 >            tasks[3].cancel(false);
690 >            p.purge();
691 >            assertEquals(tasks.length - 3, q.size());
692 >            assertEquals(tasks.length - 2, p.getTaskCount());
693 >            p.purge();         // Nothing to do
694 >            assertEquals(tasks.length - 3, q.size());
695 >            assertEquals(tasks.length - 2, p.getTaskCount());
696 >        } finally {
697 >            done.countDown();
698 >            joinPool(p);
699          }
510        tasks[4].cancel(true);
511        tasks[3].cancel(true);
512        p1.purge();
513        long count = p1.getTaskCount();
514        assertTrue(count >= 2 && count < 5);
515        joinPool(p1);
700      }
701  
702      /**
703 <     *  shutDownNow returns a list containing tasks that were not run
703 >     * shutdownNow returns a list containing tasks that were not run,
704 >     * and those tasks are drained from the queue
705       */
706 <    public void testShutDownNow() {
707 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
708 <        List l;
709 <        try {
710 <            for (int i = 0; i < 5; i++)
711 <                p1.execute(new MediumPossiblyInterruptedRunnable());
712 <        }
713 <        finally {
706 >    public void testShutdownNow() throws InterruptedException {
707 >        final int poolSize = 2;
708 >        final int count = 5;
709 >        final AtomicInteger ran = new AtomicInteger(0);
710 >        ThreadPoolExecutor p =
711 >            new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS,
712 >                          new ArrayBlockingQueue<Runnable>(10));
713 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
714 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
715 >            threadsStarted.countDown();
716              try {
717 <                l = p1.shutdownNow();
718 <            } catch (SecurityException ok) { return; }
719 <
720 <        }
721 <        assertTrue(p1.isShutdown());
722 <        assertTrue(l.size() <= 4);
717 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
718 >            } catch (InterruptedException success) {}
719 >            ran.getAndIncrement();
720 >        }};
721 >        for (int i = 0; i < count; i++)
722 >            p.execute(waiter);
723 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
724 >        assertEquals(poolSize, p.getActiveCount());
725 >        assertEquals(0, p.getCompletedTaskCount());
726 >        final List<Runnable> queuedTasks;
727 >        try {
728 >            queuedTasks = p.shutdownNow();
729 >        } catch (SecurityException ok) {
730 >            return; // Allowed in case test doesn't have privs
731 >        }
732 >        assertTrue(p.isShutdown());
733 >        assertTrue(p.getQueue().isEmpty());
734 >        assertEquals(count - poolSize, queuedTasks.size());
735 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
736 >        assertTrue(p.isTerminated());
737 >        assertEquals(poolSize, ran.get());
738 >        assertEquals(poolSize, p.getCompletedTaskCount());
739      }
740  
741      // Exception Tests
742  
540
743      /**
744       * Constructor throws if corePoolSize argument is less than zero
745       */
746      public void testConstructor1() {
747          try {
748 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
748 >            new CustomTPE(-1, 1, 1L, SECONDS,
749 >                          new ArrayBlockingQueue<Runnable>(10));
750              shouldThrow();
751          } catch (IllegalArgumentException success) {}
752      }
# Line 553 | Line 756 | public class ThreadPoolExecutorSubclassT
756       */
757      public void testConstructor2() {
758          try {
759 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
759 >            new CustomTPE(1, -1, 1L, SECONDS,
760 >                          new ArrayBlockingQueue<Runnable>(10));
761              shouldThrow();
762          } catch (IllegalArgumentException success) {}
763      }
# Line 563 | Line 767 | public class ThreadPoolExecutorSubclassT
767       */
768      public void testConstructor3() {
769          try {
770 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
770 >            new CustomTPE(1, 0, 1L, SECONDS,
771 >                          new ArrayBlockingQueue<Runnable>(10));
772              shouldThrow();
773          } catch (IllegalArgumentException success) {}
774      }
# Line 573 | Line 778 | public class ThreadPoolExecutorSubclassT
778       */
779      public void testConstructor4() {
780          try {
781 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
781 >            new CustomTPE(1, 2, -1L, SECONDS,
782 >                          new ArrayBlockingQueue<Runnable>(10));
783              shouldThrow();
784          } catch (IllegalArgumentException success) {}
785      }
# Line 583 | Line 789 | public class ThreadPoolExecutorSubclassT
789       */
790      public void testConstructor5() {
791          try {
792 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
792 >            new CustomTPE(2, 1, 1L, SECONDS,
793 >                          new ArrayBlockingQueue<Runnable>(10));
794              shouldThrow();
795          } catch (IllegalArgumentException success) {}
796      }
# Line 593 | Line 800 | public class ThreadPoolExecutorSubclassT
800       */
801      public void testConstructorNullPointerException() {
802          try {
803 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
803 >            new CustomTPE(1, 2, 1L, SECONDS, null);
804              shouldThrow();
805          } catch (NullPointerException success) {}
806      }
807  
601
602
808      /**
809       * Constructor throws if corePoolSize argument is less than zero
810       */
811      public void testConstructor6() {
812          try {
813 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
813 >            new CustomTPE(-1, 1, 1L, SECONDS,
814 >                          new ArrayBlockingQueue<Runnable>(10),
815 >                          new SimpleThreadFactory());
816              shouldThrow();
817          } catch (IllegalArgumentException success) {}
818      }
# Line 615 | Line 822 | public class ThreadPoolExecutorSubclassT
822       */
823      public void testConstructor7() {
824          try {
825 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
825 >            new CustomTPE(1,-1, 1L, SECONDS,
826 >                          new ArrayBlockingQueue<Runnable>(10),
827 >                          new SimpleThreadFactory());
828              shouldThrow();
829          } catch (IllegalArgumentException success) {}
830      }
# Line 625 | Line 834 | public class ThreadPoolExecutorSubclassT
834       */
835      public void testConstructor8() {
836          try {
837 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
837 >            new CustomTPE(1, 0, 1L, SECONDS,
838 >                          new ArrayBlockingQueue<Runnable>(10),
839 >                          new SimpleThreadFactory());
840              shouldThrow();
841          } catch (IllegalArgumentException success) {}
842      }
# Line 635 | Line 846 | public class ThreadPoolExecutorSubclassT
846       */
847      public void testConstructor9() {
848          try {
849 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
849 >            new CustomTPE(1, 2, -1L, SECONDS,
850 >                          new ArrayBlockingQueue<Runnable>(10),
851 >                          new SimpleThreadFactory());
852              shouldThrow();
853          } catch (IllegalArgumentException success) {}
854      }
# Line 645 | Line 858 | public class ThreadPoolExecutorSubclassT
858       */
859      public void testConstructor10() {
860          try {
861 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
861 >            new CustomTPE(2, 1, 1L, SECONDS,
862 >                          new ArrayBlockingQueue<Runnable>(10),
863 >                          new SimpleThreadFactory());
864              shouldThrow();
865          } catch (IllegalArgumentException success) {}
866      }
# Line 655 | Line 870 | public class ThreadPoolExecutorSubclassT
870       */
871      public void testConstructorNullPointerException2() {
872          try {
873 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
873 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
874              shouldThrow();
875          } catch (NullPointerException success) {}
876      }
# Line 665 | Line 880 | public class ThreadPoolExecutorSubclassT
880       */
881      public void testConstructorNullPointerException3() {
882          try {
883 <            ThreadFactory f = null;
884 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
883 >            new CustomTPE(1, 2, 1L, SECONDS,
884 >                          new ArrayBlockingQueue<Runnable>(10),
885 >                          (ThreadFactory) null);
886              shouldThrow();
887          } catch (NullPointerException success) {}
888      }
889  
674
890      /**
891       * Constructor throws if corePoolSize argument is less than zero
892       */
893      public void testConstructor11() {
894          try {
895 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
895 >            new CustomTPE(-1, 1, 1L, SECONDS,
896 >                          new ArrayBlockingQueue<Runnable>(10),
897 >                          new NoOpREHandler());
898              shouldThrow();
899          } catch (IllegalArgumentException success) {}
900      }
# Line 687 | Line 904 | public class ThreadPoolExecutorSubclassT
904       */
905      public void testConstructor12() {
906          try {
907 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
907 >            new CustomTPE(1, -1, 1L, SECONDS,
908 >                          new ArrayBlockingQueue<Runnable>(10),
909 >                          new NoOpREHandler());
910              shouldThrow();
911          } catch (IllegalArgumentException success) {}
912      }
# Line 697 | Line 916 | public class ThreadPoolExecutorSubclassT
916       */
917      public void testConstructor13() {
918          try {
919 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
919 >            new CustomTPE(1, 0, 1L, SECONDS,
920 >                          new ArrayBlockingQueue<Runnable>(10),
921 >                          new NoOpREHandler());
922              shouldThrow();
923          } catch (IllegalArgumentException success) {}
924      }
# Line 707 | Line 928 | public class ThreadPoolExecutorSubclassT
928       */
929      public void testConstructor14() {
930          try {
931 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
931 >            new CustomTPE(1, 2, -1L, SECONDS,
932 >                          new ArrayBlockingQueue<Runnable>(10),
933 >                          new NoOpREHandler());
934              shouldThrow();
935          } catch (IllegalArgumentException success) {}
936      }
# Line 717 | Line 940 | public class ThreadPoolExecutorSubclassT
940       */
941      public void testConstructor15() {
942          try {
943 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
943 >            new CustomTPE(2, 1, 1L, SECONDS,
944 >                          new ArrayBlockingQueue<Runnable>(10),
945 >                          new NoOpREHandler());
946              shouldThrow();
947          } catch (IllegalArgumentException success) {}
948      }
# Line 727 | Line 952 | public class ThreadPoolExecutorSubclassT
952       */
953      public void testConstructorNullPointerException4() {
954          try {
955 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
955 >            new CustomTPE(1, 2, 1L, SECONDS,
956 >                          null,
957 >                          new NoOpREHandler());
958              shouldThrow();
959          } catch (NullPointerException success) {}
960      }
# Line 737 | Line 964 | public class ThreadPoolExecutorSubclassT
964       */
965      public void testConstructorNullPointerException5() {
966          try {
967 <            RejectedExecutionHandler r = null;
968 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
967 >            new CustomTPE(1, 2, 1L, SECONDS,
968 >                          new ArrayBlockingQueue<Runnable>(10),
969 >                          (RejectedExecutionHandler) null);
970              shouldThrow();
971          } catch (NullPointerException success) {}
972      }
973  
746
974      /**
975       * Constructor throws if corePoolSize argument is less than zero
976       */
977      public void testConstructor16() {
978          try {
979 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
979 >            new CustomTPE(-1, 1, 1L, SECONDS,
980 >                          new ArrayBlockingQueue<Runnable>(10),
981 >                          new SimpleThreadFactory(),
982 >                          new NoOpREHandler());
983              shouldThrow();
984          } catch (IllegalArgumentException success) {}
985      }
# Line 759 | Line 989 | public class ThreadPoolExecutorSubclassT
989       */
990      public void testConstructor17() {
991          try {
992 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
992 >            new CustomTPE(1, -1, 1L, SECONDS,
993 >                          new ArrayBlockingQueue<Runnable>(10),
994 >                          new SimpleThreadFactory(),
995 >                          new NoOpREHandler());
996              shouldThrow();
997          } catch (IllegalArgumentException success) {}
998      }
# Line 769 | Line 1002 | public class ThreadPoolExecutorSubclassT
1002       */
1003      public void testConstructor18() {
1004          try {
1005 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1005 >            new CustomTPE(1, 0, 1L, SECONDS,
1006 >                          new ArrayBlockingQueue<Runnable>(10),
1007 >                          new SimpleThreadFactory(),
1008 >                          new NoOpREHandler());
1009              shouldThrow();
1010          } catch (IllegalArgumentException success) {}
1011      }
# Line 779 | Line 1015 | public class ThreadPoolExecutorSubclassT
1015       */
1016      public void testConstructor19() {
1017          try {
1018 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1018 >            new CustomTPE(1, 2, -1L, SECONDS,
1019 >                          new ArrayBlockingQueue<Runnable>(10),
1020 >                          new SimpleThreadFactory(),
1021 >                          new NoOpREHandler());
1022              shouldThrow();
1023          } catch (IllegalArgumentException success) {}
1024      }
# Line 789 | Line 1028 | public class ThreadPoolExecutorSubclassT
1028       */
1029      public void testConstructor20() {
1030          try {
1031 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1031 >            new CustomTPE(2, 1, 1L, SECONDS,
1032 >                          new ArrayBlockingQueue<Runnable>(10),
1033 >                          new SimpleThreadFactory(),
1034 >                          new NoOpREHandler());
1035              shouldThrow();
1036          } catch (IllegalArgumentException success) {}
1037      }
1038  
1039      /**
1040 <     * Constructor throws if workQueue is set to null
1040 >     * Constructor throws if workQueue is null
1041       */
1042      public void testConstructorNullPointerException6() {
1043          try {
1044 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1044 >            new CustomTPE(1, 2, 1L, SECONDS,
1045 >                          null,
1046 >                          new SimpleThreadFactory(),
1047 >                          new NoOpREHandler());
1048              shouldThrow();
1049          } catch (NullPointerException success) {}
1050      }
1051  
1052      /**
1053 <     * Constructor throws if handler is set to null
1053 >     * Constructor throws if handler is null
1054       */
1055      public void testConstructorNullPointerException7() {
1056          try {
1057 <            RejectedExecutionHandler r = null;
1058 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
1057 >            new CustomTPE(1, 2, 1L, SECONDS,
1058 >                          new ArrayBlockingQueue<Runnable>(10),
1059 >                          new SimpleThreadFactory(),
1060 >                          (RejectedExecutionHandler) null);
1061              shouldThrow();
1062          } catch (NullPointerException success) {}
1063      }
1064  
1065      /**
1066 <     * Constructor throws if ThreadFactory is set top null
1066 >     * Constructor throws if ThreadFactory is null
1067       */
1068      public void testConstructorNullPointerException8() {
1069          try {
1070 <            ThreadFactory f = null;
1071 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
1070 >            new CustomTPE(1, 2, 1L, SECONDS,
1071 >                          new ArrayBlockingQueue<Runnable>(10),
1072 >                          (ThreadFactory) null,
1073 >                          new NoOpREHandler());
1074              shouldThrow();
1075          } catch (NullPointerException success) {}
1076      }
1077  
829
1078      /**
1079 <     *  execute throws RejectedExecutionException
832 <     *  if saturated.
1079 >     * execute throws RejectedExecutionException if saturated.
1080       */
1081      public void testSaturatedExecute() {
1082 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
1083 <        try {
1084 <
1085 <            for (int i = 0; i < 5; ++i) {
1086 <                p.execute(new MediumRunnable());
1082 >        ThreadPoolExecutor p =
1083 >            new CustomTPE(1, 1,
1084 >                          LONG_DELAY_MS, MILLISECONDS,
1085 >                          new ArrayBlockingQueue<Runnable>(1));
1086 >        final CountDownLatch done = new CountDownLatch(1);
1087 >        try {
1088 >            Runnable task = new CheckedRunnable() {
1089 >                public void realRun() throws InterruptedException {
1090 >                    done.await();
1091 >                }};
1092 >            for (int i = 0; i < 2; ++i)
1093 >                p.execute(task);
1094 >            for (int i = 0; i < 2; ++i) {
1095 >                try {
1096 >                    p.execute(task);
1097 >                    shouldThrow();
1098 >                } catch (RejectedExecutionException success) {}
1099 >                assertTrue(p.getTaskCount() <= 2);
1100              }
1101 <            shouldThrow();
1102 <        } catch (RejectedExecutionException success) {}
1103 <        joinPool(p);
1101 >        } finally {
1102 >            done.countDown();
1103 >            joinPool(p);
1104 >        }
1105      }
1106  
1107      /**
1108 <     *  executor using CallerRunsPolicy runs task if saturated.
1108 >     * executor using CallerRunsPolicy runs task if saturated.
1109       */
1110      public void testSaturatedExecute2() {
1111          RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1112 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1112 >        ThreadPoolExecutor p = new CustomTPE(1, 1,
1113 >                                             LONG_DELAY_MS, MILLISECONDS,
1114 >                                             new ArrayBlockingQueue<Runnable>(1),
1115 >                                             h);
1116          try {
853
1117              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1118 <            for (int i = 0; i < 5; ++i) {
1118 >            for (int i = 0; i < tasks.length; ++i)
1119                  tasks[i] = new TrackedNoOpRunnable();
857            }
1120              TrackedLongRunnable mr = new TrackedLongRunnable();
1121              p.execute(mr);
1122 <            for (int i = 0; i < 5; ++i) {
1122 >            for (int i = 0; i < tasks.length; ++i)
1123                  p.execute(tasks[i]);
1124 <            }
863 <            for (int i = 1; i < 5; ++i) {
1124 >            for (int i = 1; i < tasks.length; ++i)
1125                  assertTrue(tasks[i].done);
865            }
1126              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1127          } finally {
1128              joinPool(p);
# Line 870 | Line 1130 | public class ThreadPoolExecutorSubclassT
1130      }
1131  
1132      /**
1133 <     *  executor using DiscardPolicy drops task if saturated.
1133 >     * executor using DiscardPolicy drops task if saturated.
1134       */
1135      public void testSaturatedExecute3() {
1136          RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1137 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1137 >        ThreadPoolExecutor p =
1138 >            new CustomTPE(1, 1,
1139 >                          LONG_DELAY_MS, MILLISECONDS,
1140 >                          new ArrayBlockingQueue<Runnable>(1),
1141 >                          h);
1142          try {
879
1143              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1144 <            for (int i = 0; i < 5; ++i) {
1144 >            for (int i = 0; i < tasks.length; ++i)
1145                  tasks[i] = new TrackedNoOpRunnable();
883            }
1146              p.execute(new TrackedLongRunnable());
1147 <            for (int i = 0; i < 5; ++i) {
1148 <                p.execute(tasks[i]);
1149 <            }
1150 <            for (int i = 0; i < 5; ++i) {
889 <                assertFalse(tasks[i].done);
890 <            }
1147 >            for (TrackedNoOpRunnable task : tasks)
1148 >                p.execute(task);
1149 >            for (TrackedNoOpRunnable task : tasks)
1150 >                assertFalse(task.done);
1151              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1152          } finally {
1153              joinPool(p);
# Line 895 | Line 1155 | public class ThreadPoolExecutorSubclassT
1155      }
1156  
1157      /**
1158 <     *  executor using DiscardOldestPolicy drops oldest task if saturated.
1158 >     * executor using DiscardOldestPolicy drops oldest task if saturated.
1159       */
1160      public void testSaturatedExecute4() {
1161          RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
# Line 916 | Line 1176 | public class ThreadPoolExecutorSubclassT
1176      }
1177  
1178      /**
1179 <     *  execute throws RejectedExecutionException if shutdown
1179 >     * execute throws RejectedExecutionException if shutdown
1180       */
1181      public void testRejectedExecutionExceptionOnShutdown() {
1182 <        ThreadPoolExecutor tpe =
1182 >        ThreadPoolExecutor p =
1183              new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1184 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1184 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1185          try {
1186 <            tpe.execute(new NoOpRunnable());
1186 >            p.execute(new NoOpRunnable());
1187              shouldThrow();
1188          } catch (RejectedExecutionException success) {}
1189  
1190 <        joinPool(tpe);
1190 >        joinPool(p);
1191      }
1192  
1193      /**
1194 <     *  execute using CallerRunsPolicy drops task on shutdown
1194 >     * execute using CallerRunsPolicy drops task on shutdown
1195       */
1196      public void testCallerRunsOnShutdown() {
1197          RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
# Line 948 | Line 1208 | public class ThreadPoolExecutorSubclassT
1208      }
1209  
1210      /**
1211 <     *  execute using DiscardPolicy drops task on shutdown
1211 >     * execute using DiscardPolicy drops task on shutdown
1212       */
1213      public void testDiscardOnShutdown() {
1214          RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
# Line 964 | Line 1224 | public class ThreadPoolExecutorSubclassT
1224          }
1225      }
1226  
967
1227      /**
1228 <     *  execute using DiscardOldestPolicy drops task on shutdown
1228 >     * execute using DiscardOldestPolicy drops task on shutdown
1229       */
1230      public void testDiscardOldestOnShutdown() {
1231          RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
# Line 982 | Line 1241 | public class ThreadPoolExecutorSubclassT
1241          }
1242      }
1243  
985
1244      /**
1245 <     *  execute (null) throws NPE
1245 >     * execute(null) throws NPE
1246       */
1247      public void testExecuteNull() {
1248 <        ThreadPoolExecutor tpe = null;
1248 >        ThreadPoolExecutor p =
1249 >            new CustomTPE(1, 2, 1L, SECONDS,
1250 >                          new ArrayBlockingQueue<Runnable>(10));
1251          try {
1252 <            tpe = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
993 <            tpe.execute(null);
1252 >            p.execute(null);
1253              shouldThrow();
1254          } catch (NullPointerException success) {}
1255  
1256 <        joinPool(tpe);
1256 >        joinPool(p);
1257      }
1258  
1259      /**
1260 <     *  setCorePoolSize of negative value throws IllegalArgumentException
1260 >     * setCorePoolSize of negative value throws IllegalArgumentException
1261       */
1262      public void testCorePoolSizeIllegalArgumentException() {
1263 <        ThreadPoolExecutor tpe =
1263 >        ThreadPoolExecutor p =
1264              new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1265          try {
1266 <            tpe.setCorePoolSize(-1);
1266 >            p.setCorePoolSize(-1);
1267              shouldThrow();
1268          } catch (IllegalArgumentException success) {
1269          } finally {
1270 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1270 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1271          }
1272 <        joinPool(tpe);
1272 >        joinPool(p);
1273      }
1274  
1275      /**
1276 <     *  setMaximumPoolSize(int) throws IllegalArgumentException if
1277 <     *  given a value less the core pool size
1276 >     * setMaximumPoolSize(int) throws IllegalArgumentException
1277 >     * if given a value less the core pool size
1278       */
1279      public void testMaximumPoolSizeIllegalArgumentException() {
1280 <        ThreadPoolExecutor tpe = null;
1281 <        try {
1023 <            tpe = new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1024 <        } catch (Exception e) {}
1280 >        ThreadPoolExecutor p =
1281 >            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1282          try {
1283 <            tpe.setMaximumPoolSize(1);
1283 >            p.setMaximumPoolSize(1);
1284              shouldThrow();
1285          } catch (IllegalArgumentException success) {
1286          } finally {
1287 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1287 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1288          }
1289 <        joinPool(tpe);
1289 >        joinPool(p);
1290      }
1291  
1292      /**
1293 <     *  setMaximumPoolSize throws IllegalArgumentException
1294 <     *  if given a negative value
1293 >     * setMaximumPoolSize throws IllegalArgumentException
1294 >     * if given a negative value
1295       */
1296      public void testMaximumPoolSizeIllegalArgumentException2() {
1297 <        ThreadPoolExecutor tpe = null;
1298 <        try {
1042 <            tpe = new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1043 <        } catch (Exception e) {}
1297 >        ThreadPoolExecutor p =
1298 >            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1299          try {
1300 <            tpe.setMaximumPoolSize(-1);
1300 >            p.setMaximumPoolSize(-1);
1301              shouldThrow();
1302          } catch (IllegalArgumentException success) {
1303          } finally {
1304 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1304 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1305          }
1306 <        joinPool(tpe);
1306 >        joinPool(p);
1307      }
1308  
1054
1309      /**
1310 <     *  setKeepAliveTime  throws IllegalArgumentException
1311 <     *  when given a negative value
1310 >     * setKeepAliveTime throws IllegalArgumentException
1311 >     * when given a negative value
1312       */
1313      public void testKeepAliveTimeIllegalArgumentException() {
1314 <        ThreadPoolExecutor tpe = null;
1315 <        try {
1062 <            tpe = new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1063 <        } catch (Exception e) {}
1314 >        ThreadPoolExecutor p =
1315 >            new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1316  
1317          try {
1318 <            tpe.setKeepAliveTime(-1,MILLISECONDS);
1318 >            p.setKeepAliveTime(-1,MILLISECONDS);
1319              shouldThrow();
1320          } catch (IllegalArgumentException success) {
1321          } finally {
1322 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1322 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1323          }
1324 <        joinPool(tpe);
1324 >        joinPool(p);
1325      }
1326  
1327      /**
1328       * terminated() is called on termination
1329       */
1330      public void testTerminated() {
1331 <        CustomTPE tpe = new CustomTPE();
1332 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1333 <        assertTrue(tpe.terminatedCalled);
1334 <        joinPool(tpe);
1331 >        CustomTPE p = new CustomTPE();
1332 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1333 >        assertTrue(p.terminatedCalled());
1334 >        joinPool(p);
1335      }
1336  
1337      /**
1338       * beforeExecute and afterExecute are called when executing task
1339       */
1340      public void testBeforeAfter() throws InterruptedException {
1341 <        CustomTPE tpe = new CustomTPE();
1341 >        CustomTPE p = new CustomTPE();
1342          try {
1343 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1344 <            tpe.execute(r);
1345 <            Thread.sleep(SHORT_DELAY_MS);
1346 <            assertTrue(r.done);
1347 <            assertTrue(tpe.beforeCalled);
1348 <            assertTrue(tpe.afterCalled);
1349 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1343 >            final CountDownLatch done = new CountDownLatch(1);
1344 >            p.execute(new CheckedRunnable() {
1345 >                public void realRun() {
1346 >                    done.countDown();
1347 >                }});
1348 >            await(p.afterCalled);
1349 >            assertEquals(0, done.getCount());
1350 >            assertTrue(p.afterCalled());
1351 >            assertTrue(p.beforeCalled());
1352 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1353          } finally {
1354 <            joinPool(tpe);
1354 >            joinPool(p);
1355          }
1356      }
1357  
# Line 1142 | Line 1397 | public class ThreadPoolExecutorSubclassT
1397          }
1398      }
1399  
1145
1400      /**
1401       * invokeAny(null) throws NPE
1402       */
# Line 1175 | Line 1429 | public class ThreadPoolExecutorSubclassT
1429       * invokeAny(c) throws NPE if c has null elements
1430       */
1431      public void testInvokeAny3() throws Exception {
1432 <        final CountDownLatch latch = new CountDownLatch(1);
1432 >        CountDownLatch latch = new CountDownLatch(1);
1433          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1434 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1435 +        l.add(latchAwaitingStringTask(latch));
1436 +        l.add(null);
1437          try {
1181            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1182            l.add(new Callable<String>() {
1183                      public String call() {
1184                          try {
1185                              latch.await();
1186                          } catch (InterruptedException ok) {}
1187                          return TEST_STRING;
1188                      }});
1189            l.add(null);
1438              e.invokeAny(l);
1439              shouldThrow();
1440          } catch (NullPointerException success) {
# Line 1201 | Line 1449 | public class ThreadPoolExecutorSubclassT
1449       */
1450      public void testInvokeAny4() throws Exception {
1451          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1452 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1453 +        l.add(new NPETask());
1454          try {
1205            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1206            l.add(new NPETask());
1455              e.invokeAny(l);
1456              shouldThrow();
1457          } catch (ExecutionException success) {
1458 +            assertTrue(success.getCause() instanceof NullPointerException);
1459          } finally {
1460              joinPool(e);
1461          }
# Line 1218 | Line 1467 | public class ThreadPoolExecutorSubclassT
1467      public void testInvokeAny5() throws Exception {
1468          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1469          try {
1470 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1470 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1471              l.add(new StringTask());
1472              l.add(new StringTask());
1473              String result = e.invokeAny(l);
# Line 1260 | Line 1509 | public class ThreadPoolExecutorSubclassT
1509       */
1510      public void testInvokeAll3() throws Exception {
1511          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1512 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1513 +        l.add(new StringTask());
1514 +        l.add(null);
1515          try {
1264            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1265            l.add(new StringTask());
1266            l.add(null);
1516              e.invokeAll(l);
1517              shouldThrow();
1518          } catch (NullPointerException success) {
# Line 1277 | Line 1526 | public class ThreadPoolExecutorSubclassT
1526       */
1527      public void testInvokeAll4() throws Exception {
1528          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1529 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1530 +        l.add(new NPETask());
1531 +        List<Future<String>> futures = e.invokeAll(l);
1532 +        assertEquals(1, futures.size());
1533          try {
1534 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1282 <            l.add(new NPETask());
1283 <            List<Future<String>> result = e.invokeAll(l);
1284 <            assertEquals(1, result.size());
1285 <            for (Future<String> future : result)
1286 <                future.get();
1534 >            futures.get(0).get();
1535              shouldThrow();
1536          } catch (ExecutionException success) {
1537 +            assertTrue(success.getCause() instanceof NullPointerException);
1538          } finally {
1539              joinPool(e);
1540          }
# Line 1297 | Line 1546 | public class ThreadPoolExecutorSubclassT
1546      public void testInvokeAll5() throws Exception {
1547          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1548          try {
1549 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1549 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1550              l.add(new StringTask());
1551              l.add(new StringTask());
1552 <            List<Future<String>> result = e.invokeAll(l);
1553 <            assertEquals(2, result.size());
1554 <            for (Future<String> future : result)
1552 >            List<Future<String>> futures = e.invokeAll(l);
1553 >            assertEquals(2, futures.size());
1554 >            for (Future<String> future : futures)
1555                  assertSame(TEST_STRING, future.get());
1556          } finally {
1557              joinPool(e);
1558          }
1559      }
1560  
1312
1313
1561      /**
1562       * timed invokeAny(null) throws NPE
1563       */
# Line 1330 | Line 1577 | public class ThreadPoolExecutorSubclassT
1577       */
1578      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1579          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1580 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1581 +        l.add(new StringTask());
1582          try {
1334            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1335            l.add(new StringTask());
1583              e.invokeAny(l, MEDIUM_DELAY_MS, null);
1584              shouldThrow();
1585          } catch (NullPointerException success) {
# Line 1359 | Line 1606 | public class ThreadPoolExecutorSubclassT
1606       * timed invokeAny(c) throws NPE if c has null elements
1607       */
1608      public void testTimedInvokeAny3() throws Exception {
1609 +        CountDownLatch latch = new CountDownLatch(1);
1610          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1611 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1612 +        l.add(latchAwaitingStringTask(latch));
1613 +        l.add(null);
1614          try {
1364            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1365            l.add(new StringTask());
1366            l.add(null);
1615              e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1616              shouldThrow();
1617          } catch (NullPointerException success) {
1618          } finally {
1619 +            latch.countDown();
1620              joinPool(e);
1621          }
1622      }
# Line 1377 | Line 1626 | public class ThreadPoolExecutorSubclassT
1626       */
1627      public void testTimedInvokeAny4() throws Exception {
1628          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1629 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1630 +        l.add(new NPETask());
1631          try {
1381            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1382            l.add(new NPETask());
1632              e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1633              shouldThrow();
1634          } catch (ExecutionException success) {
1635 +            assertTrue(success.getCause() instanceof NullPointerException);
1636          } finally {
1637              joinPool(e);
1638          }
# Line 1394 | Line 1644 | public class ThreadPoolExecutorSubclassT
1644      public void testTimedInvokeAny5() throws Exception {
1645          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1646          try {
1647 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1647 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1648              l.add(new StringTask());
1649              l.add(new StringTask());
1650              String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
# Line 1423 | Line 1673 | public class ThreadPoolExecutorSubclassT
1673       */
1674      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1675          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1676 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1677 +        l.add(new StringTask());
1678          try {
1427            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1428            l.add(new StringTask());
1679              e.invokeAll(l, MEDIUM_DELAY_MS, null);
1680              shouldThrow();
1681          } catch (NullPointerException success) {
# Line 1452 | Line 1702 | public class ThreadPoolExecutorSubclassT
1702       */
1703      public void testTimedInvokeAll3() throws Exception {
1704          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1705 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1706 +        l.add(new StringTask());
1707 +        l.add(null);
1708          try {
1456            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1457            l.add(new StringTask());
1458            l.add(null);
1709              e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1710              shouldThrow();
1711          } catch (NullPointerException success) {
# Line 1469 | Line 1719 | public class ThreadPoolExecutorSubclassT
1719       */
1720      public void testTimedInvokeAll4() throws Exception {
1721          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1722 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1723 +        l.add(new NPETask());
1724 +        List<Future<String>> futures =
1725 +            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1726 +        assertEquals(1, futures.size());
1727          try {
1728 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1474 <            l.add(new NPETask());
1475 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1476 <            assertEquals(1, result.size());
1477 <            for (Future<String> future : result)
1478 <                future.get();
1728 >            futures.get(0).get();
1729              shouldThrow();
1730          } catch (ExecutionException success) {
1731              assertTrue(success.getCause() instanceof NullPointerException);
# Line 1490 | Line 1740 | public class ThreadPoolExecutorSubclassT
1740      public void testTimedInvokeAll5() throws Exception {
1741          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1742          try {
1743 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1743 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1744              l.add(new StringTask());
1745              l.add(new StringTask());
1746 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1747 <            assertEquals(2, result.size());
1748 <            for (Future<String> future : result)
1746 >            List<Future<String>> futures =
1747 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1748 >            assertEquals(2, futures.size());
1749 >            for (Future<String> future : futures)
1750                  assertSame(TEST_STRING, future.get());
1500        } catch (ExecutionException success) {
1751          } finally {
1752              joinPool(e);
1753          }
# Line 1509 | Line 1759 | public class ThreadPoolExecutorSubclassT
1759      public void testTimedInvokeAll6() throws Exception {
1760          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1761          try {
1762 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1763 <            l.add(new StringTask());
1764 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1765 <            l.add(new StringTask());
1766 <            List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1767 <            assertEquals(3, result.size());
1768 <            Iterator<Future<String>> it = result.iterator();
1769 <            Future<String> f1 = it.next();
1770 <            Future<String> f2 = it.next();
1771 <            Future<String> f3 = it.next();
1772 <            assertTrue(f1.isDone());
1773 <            assertTrue(f2.isDone());
1774 <            assertTrue(f3.isDone());
1775 <            assertFalse(f1.isCancelled());
1776 <            assertTrue(f2.isCancelled());
1762 >            for (long timeout = timeoutMillis();;) {
1763 >                List<Callable<String>> tasks = new ArrayList<>();
1764 >                tasks.add(new StringTask("0"));
1765 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1766 >                tasks.add(new StringTask("2"));
1767 >                long startTime = System.nanoTime();
1768 >                List<Future<String>> futures =
1769 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1770 >                assertEquals(tasks.size(), futures.size());
1771 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1772 >                for (Future future : futures)
1773 >                    assertTrue(future.isDone());
1774 >                assertTrue(futures.get(1).isCancelled());
1775 >                try {
1776 >                    assertEquals("0", futures.get(0).get());
1777 >                    assertEquals("2", futures.get(2).get());
1778 >                    break;
1779 >                } catch (CancellationException retryWithLongerTimeout) {
1780 >                    timeout *= 2;
1781 >                    if (timeout >= LONG_DELAY_MS / 2)
1782 >                        fail("expected exactly one task to be cancelled");
1783 >                }
1784 >            }
1785          } finally {
1786              joinPool(e);
1787          }
# Line 1534 | Line 1792 | public class ThreadPoolExecutorSubclassT
1792       * thread factory fails to create more
1793       */
1794      public void testFailingThreadFactory() throws InterruptedException {
1795 <        ExecutorService e = new CustomTPE(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1796 <        try {
1797 <            for (int k = 0; k < 100; ++k) {
1798 <                e.execute(new NoOpRunnable());
1799 <            }
1800 <            Thread.sleep(LONG_DELAY_MS);
1795 >        final ExecutorService e =
1796 >            new CustomTPE(100, 100,
1797 >                          LONG_DELAY_MS, MILLISECONDS,
1798 >                          new LinkedBlockingQueue<Runnable>(),
1799 >                          new FailingThreadFactory());
1800 >        try {
1801 >            final int TASKS = 100;
1802 >            final CountDownLatch done = new CountDownLatch(TASKS);
1803 >            for (int k = 0; k < TASKS; ++k)
1804 >                e.execute(new CheckedRunnable() {
1805 >                    public void realRun() {
1806 >                        done.countDown();
1807 >                    }});
1808 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1809          } finally {
1810              joinPool(e);
1811          }
# Line 1549 | Line 1815 | public class ThreadPoolExecutorSubclassT
1815       * allowsCoreThreadTimeOut is by default false.
1816       */
1817      public void testAllowsCoreThreadTimeOut() {
1818 <        ThreadPoolExecutor tpe = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1819 <        assertFalse(tpe.allowsCoreThreadTimeOut());
1820 <        joinPool(tpe);
1818 >        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1819 >        assertFalse(p.allowsCoreThreadTimeOut());
1820 >        joinPool(p);
1821      }
1822  
1823      /**
1824       * allowCoreThreadTimeOut(true) causes idle threads to time out
1825       */
1826 <    public void testAllowCoreThreadTimeOut_true() throws InterruptedException {
1827 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1828 <        tpe.allowCoreThreadTimeOut(true);
1829 <        tpe.execute(new NoOpRunnable());
1830 <        try {
1831 <            Thread.sleep(MEDIUM_DELAY_MS);
1832 <            assertEquals(0, tpe.getPoolSize());
1826 >    public void testAllowCoreThreadTimeOut_true() throws Exception {
1827 >        long keepAliveTime = timeoutMillis();
1828 >        final ThreadPoolExecutor p =
1829 >            new CustomTPE(2, 10,
1830 >                          keepAliveTime, MILLISECONDS,
1831 >                          new ArrayBlockingQueue<Runnable>(10));
1832 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1833 >        try {
1834 >            p.allowCoreThreadTimeOut(true);
1835 >            p.execute(new CheckedRunnable() {
1836 >                public void realRun() {
1837 >                    threadStarted.countDown();
1838 >                    assertEquals(1, p.getPoolSize());
1839 >                }});
1840 >            await(threadStarted);
1841 >            delay(keepAliveTime);
1842 >            long startTime = System.nanoTime();
1843 >            while (p.getPoolSize() > 0
1844 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1845 >                Thread.yield();
1846 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1847 >            assertEquals(0, p.getPoolSize());
1848          } finally {
1849 <            joinPool(tpe);
1849 >            joinPool(p);
1850          }
1851      }
1852  
1853      /**
1854       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1855       */
1856 <    public void testAllowCoreThreadTimeOut_false() throws InterruptedException {
1857 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1858 <        tpe.allowCoreThreadTimeOut(false);
1859 <        tpe.execute(new NoOpRunnable());
1860 <        try {
1861 <            Thread.sleep(MEDIUM_DELAY_MS);
1862 <            assertTrue(tpe.getPoolSize() >= 1);
1856 >    public void testAllowCoreThreadTimeOut_false() throws Exception {
1857 >        long keepAliveTime = timeoutMillis();
1858 >        final ThreadPoolExecutor p =
1859 >            new CustomTPE(2, 10,
1860 >                          keepAliveTime, MILLISECONDS,
1861 >                          new ArrayBlockingQueue<Runnable>(10));
1862 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1863 >        try {
1864 >            p.allowCoreThreadTimeOut(false);
1865 >            p.execute(new CheckedRunnable() {
1866 >                public void realRun() throws InterruptedException {
1867 >                    threadStarted.countDown();
1868 >                    assertTrue(p.getPoolSize() >= 1);
1869 >                }});
1870 >            delay(2 * keepAliveTime);
1871 >            assertTrue(p.getPoolSize() >= 1);
1872          } finally {
1873 <            joinPool(tpe);
1873 >            joinPool(p);
1874          }
1875      }
1876  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines