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.17 by jsr166, Tue Dec 1 22:51:44 2009 UTC vs.
Revision 1.40 by jsr166, Sun Sep 27 18:50:50 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.locks.Condition;
34 > import java.util.concurrent.locks.ReentrantLock;
35 >
36 > import junit.framework.Test;
37 > import junit.framework.TestSuite;
38  
39   public class ThreadPoolExecutorSubclassTest extends JSR166TestCase {
40      public static void main(String[] args) {
41 <        junit.textui.TestRunner.run(suite());
41 >        main(suite(), args);
42      }
43      public static Test suite() {
44          return new TestSuite(ThreadPoolExecutorSubclassTest.class);
# Line 37 | Line 60 | public class ThreadPoolExecutorSubclassT
60          CustomTask(final Runnable r, final V res) {
61              if (r == null) throw new NullPointerException();
62              callable = new Callable<V>() {
63 <            public V call() throws Exception { r.run(); return res; }};
63 >                public V call() throws Exception { r.run(); return res; }};
64          }
65          public boolean isDone() {
66              lock.lock(); try { return done; } finally { lock.unlock() ; }
# Line 60 | Line 83 | public class ThreadPoolExecutorSubclassT
83              finally { lock.unlock() ; }
84          }
85          public void run() {
63            boolean runme;
86              lock.lock();
87              try {
88 <                runme = !done;
89 <                if (!runme)
90 <                    thread = Thread.currentThread();
88 >                if (done)
89 >                    return;
90 >                thread = Thread.currentThread();
91              }
92              finally { lock.unlock() ; }
71            if (!runme) return;
93              V v = null;
94              Exception e = null;
95              try {
# Line 117 | Line 138 | public class ThreadPoolExecutorSubclassT
138          }
139      }
140  
120
141      static class CustomTPE extends ThreadPoolExecutor {
142          protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
143              return new CustomTask<V>(c);
# Line 164 | Line 184 | public class ThreadPoolExecutorSubclassT
184                workQueue, threadFactory, handler);
185          }
186  
187 <        volatile boolean beforeCalled = false;
188 <        volatile boolean afterCalled = false;
189 <        volatile boolean terminatedCalled = false;
187 >        final CountDownLatch beforeCalled = new CountDownLatch(1);
188 >        final CountDownLatch afterCalled = new CountDownLatch(1);
189 >        final CountDownLatch terminatedCalled = new CountDownLatch(1);
190 >
191          public CustomTPE() {
192              super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
193          }
194          protected void beforeExecute(Thread t, Runnable r) {
195 <            beforeCalled = true;
195 >            beforeCalled.countDown();
196          }
197          protected void afterExecute(Runnable r, Throwable t) {
198 <            afterCalled = true;
198 >            afterCalled.countDown();
199          }
200          protected void terminated() {
201 <            terminatedCalled = true;
201 >            terminatedCalled.countDown();
202          }
203  
204 +        public boolean beforeCalled() {
205 +            return beforeCalled.getCount() == 0;
206 +        }
207 +        public boolean afterCalled() {
208 +            return afterCalled.getCount() == 0;
209 +        }
210 +        public boolean terminatedCalled() {
211 +            return terminatedCalled.getCount() == 0;
212 +        }
213      }
214  
215      static class FailingThreadFactory implements ThreadFactory {
# Line 190 | Line 220 | public class ThreadPoolExecutorSubclassT
220          }
221      }
222  
193
223      /**
224 <     *  execute successfully executes a runnable
224 >     * execute successfully executes a runnable
225       */
226      public void testExecute() throws InterruptedException {
227 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
227 >        final ThreadPoolExecutor p =
228 >            new CustomTPE(1, 1,
229 >                          LONG_DELAY_MS, MILLISECONDS,
230 >                          new ArrayBlockingQueue<Runnable>(10));
231 >        final CountDownLatch done = new CountDownLatch(1);
232 >        final Runnable task = new CheckedRunnable() {
233 >            public void realRun() {
234 >                done.countDown();
235 >            }};
236          try {
237 <            p1.execute(new ShortRunnable());
238 <            Thread.sleep(SMALL_DELAY_MS);
237 >            p.execute(task);
238 >            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
239          } finally {
240 <            joinPool(p1);
240 >            joinPool(p);
241          }
242      }
243  
244      /**
245 <     *  getActiveCount increases but doesn't overestimate, when a
246 <     *  thread becomes active
245 >     * getActiveCount increases but doesn't overestimate, when a
246 >     * thread becomes active
247       */
248      public void testGetActiveCount() throws InterruptedException {
249 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
250 <        assertEquals(0, p2.getActiveCount());
251 <        p2.execute(new MediumRunnable());
252 <        Thread.sleep(SHORT_DELAY_MS);
253 <        assertEquals(1, p2.getActiveCount());
254 <        joinPool(p2);
249 >        final ThreadPoolExecutor p =
250 >            new CustomTPE(2, 2,
251 >                          LONG_DELAY_MS, MILLISECONDS,
252 >                          new ArrayBlockingQueue<Runnable>(10));
253 >        final CountDownLatch threadStarted = new CountDownLatch(1);
254 >        final CountDownLatch done = new CountDownLatch(1);
255 >        try {
256 >            assertEquals(0, p.getActiveCount());
257 >            p.execute(new CheckedRunnable() {
258 >                public void realRun() throws InterruptedException {
259 >                    threadStarted.countDown();
260 >                    assertEquals(1, p.getActiveCount());
261 >                    done.await();
262 >                }});
263 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
264 >            assertEquals(1, p.getActiveCount());
265 >        } finally {
266 >            done.countDown();
267 >            joinPool(p);
268 >        }
269      }
270  
271      /**
272 <     *  prestartCoreThread starts a thread if under corePoolSize, else doesn't
272 >     * prestartCoreThread starts a thread if under corePoolSize, else doesn't
273       */
274      public void testPrestartCoreThread() {
275 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
276 <        assertEquals(0, p2.getPoolSize());
277 <        assertTrue(p2.prestartCoreThread());
278 <        assertEquals(1, p2.getPoolSize());
279 <        assertTrue(p2.prestartCoreThread());
280 <        assertEquals(2, p2.getPoolSize());
281 <        assertFalse(p2.prestartCoreThread());
282 <        assertEquals(2, p2.getPoolSize());
283 <        joinPool(p2);
275 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
276 >        assertEquals(0, p.getPoolSize());
277 >        assertTrue(p.prestartCoreThread());
278 >        assertEquals(1, p.getPoolSize());
279 >        assertTrue(p.prestartCoreThread());
280 >        assertEquals(2, p.getPoolSize());
281 >        assertFalse(p.prestartCoreThread());
282 >        assertEquals(2, p.getPoolSize());
283 >        joinPool(p);
284      }
285  
286      /**
287 <     *  prestartAllCoreThreads starts all corePoolSize threads
287 >     * prestartAllCoreThreads starts all corePoolSize threads
288       */
289      public void testPrestartAllCoreThreads() {
290 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
291 <        assertEquals(0, p2.getPoolSize());
292 <        p2.prestartAllCoreThreads();
293 <        assertEquals(2, p2.getPoolSize());
294 <        p2.prestartAllCoreThreads();
295 <        assertEquals(2, p2.getPoolSize());
296 <        joinPool(p2);
290 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
291 >        assertEquals(0, p.getPoolSize());
292 >        p.prestartAllCoreThreads();
293 >        assertEquals(2, p.getPoolSize());
294 >        p.prestartAllCoreThreads();
295 >        assertEquals(2, p.getPoolSize());
296 >        joinPool(p);
297      }
298  
299      /**
300 <     *   getCompletedTaskCount increases, but doesn't overestimate,
301 <     *   when tasks complete
300 >     * getCompletedTaskCount increases, but doesn't overestimate,
301 >     * when tasks complete
302       */
303      public void testGetCompletedTaskCount() throws InterruptedException {
304 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
305 <        assertEquals(0, p2.getCompletedTaskCount());
306 <        p2.execute(new ShortRunnable());
307 <        Thread.sleep(SMALL_DELAY_MS);
308 <        assertEquals(1, p2.getCompletedTaskCount());
309 <        try { p2.shutdown(); } catch (SecurityException ok) { return; }
310 <        joinPool(p2);
304 >        final ThreadPoolExecutor p =
305 >            new CustomTPE(2, 2,
306 >                          LONG_DELAY_MS, MILLISECONDS,
307 >                          new ArrayBlockingQueue<Runnable>(10));
308 >        final CountDownLatch threadStarted = new CountDownLatch(1);
309 >        final CountDownLatch threadProceed = new CountDownLatch(1);
310 >        final CountDownLatch threadDone = new CountDownLatch(1);
311 >        try {
312 >            assertEquals(0, p.getCompletedTaskCount());
313 >            p.execute(new CheckedRunnable() {
314 >                public void realRun() throws InterruptedException {
315 >                    threadStarted.countDown();
316 >                    assertEquals(0, p.getCompletedTaskCount());
317 >                    threadProceed.await();
318 >                    threadDone.countDown();
319 >                }});
320 >            await(threadStarted);
321 >            assertEquals(0, p.getCompletedTaskCount());
322 >            threadProceed.countDown();
323 >            threadDone.await();
324 >            long startTime = System.nanoTime();
325 >            while (p.getCompletedTaskCount() != 1) {
326 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
327 >                    fail("timed out");
328 >                Thread.yield();
329 >            }
330 >        } finally {
331 >            joinPool(p);
332 >        }
333      }
334  
335      /**
336 <     *   getCorePoolSize returns size given in constructor if not otherwise set
336 >     * getCorePoolSize returns size given in constructor if not otherwise set
337       */
338      public void testGetCorePoolSize() {
339 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
340 <        assertEquals(1, p1.getCorePoolSize());
341 <        joinPool(p1);
339 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
340 >        assertEquals(1, p.getCorePoolSize());
341 >        joinPool(p);
342      }
343  
344      /**
345 <     *   getKeepAliveTime returns value given in constructor if not otherwise set
345 >     * getKeepAliveTime returns value given in constructor if not otherwise set
346       */
347      public void testGetKeepAliveTime() {
348 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
349 <        assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS));
350 <        joinPool(p2);
348 >        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
349 >        assertEquals(1, p.getKeepAliveTime(SECONDS));
350 >        joinPool(p);
351      }
352  
280
353      /**
354       * getThreadFactory returns factory in constructor if not set
355       */
# Line 299 | Line 371 | public class ThreadPoolExecutorSubclassT
371          joinPool(p);
372      }
373  
302
374      /**
375       * setThreadFactory(null) throws NPE
376       */
# Line 336 | Line 407 | public class ThreadPoolExecutorSubclassT
407          joinPool(p);
408      }
409  
339
410      /**
411       * setRejectedExecutionHandler(null) throws NPE
412       */
# Line 351 | Line 421 | public class ThreadPoolExecutorSubclassT
421          }
422      }
423  
354
424      /**
425 <     *   getLargestPoolSize increases, but doesn't overestimate, when
426 <     *   multiple threads active
425 >     * getLargestPoolSize increases, but doesn't overestimate, when
426 >     * multiple threads active
427       */
428      public void testGetLargestPoolSize() throws InterruptedException {
429 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
430 <        assertEquals(0, p2.getLargestPoolSize());
431 <        p2.execute(new MediumRunnable());
432 <        p2.execute(new MediumRunnable());
433 <        Thread.sleep(SHORT_DELAY_MS);
434 <        assertEquals(2, p2.getLargestPoolSize());
435 <        joinPool(p2);
429 >        final int THREADS = 3;
430 >        final ThreadPoolExecutor p =
431 >            new CustomTPE(THREADS, THREADS,
432 >                          LONG_DELAY_MS, MILLISECONDS,
433 >                          new ArrayBlockingQueue<Runnable>(10));
434 >        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
435 >        final CountDownLatch done = new CountDownLatch(1);
436 >        try {
437 >            assertEquals(0, p.getLargestPoolSize());
438 >            for (int i = 0; i < THREADS; i++)
439 >                p.execute(new CheckedRunnable() {
440 >                    public void realRun() throws InterruptedException {
441 >                        threadsStarted.countDown();
442 >                        done.await();
443 >                        assertEquals(THREADS, p.getLargestPoolSize());
444 >                    }});
445 >            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
446 >            assertEquals(THREADS, p.getLargestPoolSize());
447 >        } finally {
448 >            done.countDown();
449 >            joinPool(p);
450 >            assertEquals(THREADS, p.getLargestPoolSize());
451 >        }
452      }
453  
454      /**
455 <     *   getMaximumPoolSize returns value given in constructor if not
456 <     *   otherwise set
455 >     * getMaximumPoolSize returns value given in constructor if not
456 >     * otherwise set
457       */
458      public void testGetMaximumPoolSize() {
459 <        ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
460 <        assertEquals(2, p2.getMaximumPoolSize());
461 <        joinPool(p2);
459 >        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
460 >        assertEquals(2, p.getMaximumPoolSize());
461 >        joinPool(p);
462      }
463  
464      /**
465 <     *   getPoolSize increases, but doesn't overestimate, when threads
466 <     *   become active
465 >     * getPoolSize increases, but doesn't overestimate, when threads
466 >     * become active
467       */
468 <    public void testGetPoolSize() {
469 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
470 <        assertEquals(0, p1.getPoolSize());
471 <        p1.execute(new MediumRunnable());
472 <        assertEquals(1, p1.getPoolSize());
473 <        joinPool(p1);
468 >    public void testGetPoolSize() throws InterruptedException {
469 >        final ThreadPoolExecutor p =
470 >            new CustomTPE(1, 1,
471 >                          LONG_DELAY_MS, MILLISECONDS,
472 >                          new ArrayBlockingQueue<Runnable>(10));
473 >        final CountDownLatch threadStarted = new CountDownLatch(1);
474 >        final CountDownLatch done = new CountDownLatch(1);
475 >        try {
476 >            assertEquals(0, p.getPoolSize());
477 >            p.execute(new CheckedRunnable() {
478 >                public void realRun() throws InterruptedException {
479 >                    threadStarted.countDown();
480 >                    assertEquals(1, p.getPoolSize());
481 >                    done.await();
482 >                }});
483 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
484 >            assertEquals(1, p.getPoolSize());
485 >        } finally {
486 >            done.countDown();
487 >            joinPool(p);
488 >        }
489      }
490  
491      /**
492 <     *  getTaskCount increases, but doesn't overestimate, when tasks submitted
492 >     * getTaskCount increases, but doesn't overestimate, when tasks submitted
493       */
494      public void testGetTaskCount() throws InterruptedException {
495 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
496 <        assertEquals(0, p1.getTaskCount());
497 <        p1.execute(new MediumRunnable());
498 <        Thread.sleep(SHORT_DELAY_MS);
499 <        assertEquals(1, p1.getTaskCount());
500 <        joinPool(p1);
495 >        final ThreadPoolExecutor p =
496 >            new CustomTPE(1, 1,
497 >                          LONG_DELAY_MS, MILLISECONDS,
498 >                          new ArrayBlockingQueue<Runnable>(10));
499 >        final CountDownLatch threadStarted = new CountDownLatch(1);
500 >        final CountDownLatch done = new CountDownLatch(1);
501 >        try {
502 >            assertEquals(0, p.getTaskCount());
503 >            p.execute(new CheckedRunnable() {
504 >                public void realRun() throws InterruptedException {
505 >                    threadStarted.countDown();
506 >                    assertEquals(1, p.getTaskCount());
507 >                    done.await();
508 >                }});
509 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
510 >            assertEquals(1, p.getTaskCount());
511 >        } finally {
512 >            done.countDown();
513 >            joinPool(p);
514 >        }
515      }
516  
517      /**
518 <     *   isShutDown is false before shutdown, true after
518 >     * isShutdown is false before shutdown, true after
519       */
520      public void testIsShutdown() {
521  
522 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
523 <        assertFalse(p1.isShutdown());
524 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
525 <        assertTrue(p1.isShutdown());
526 <        joinPool(p1);
522 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
523 >        assertFalse(p.isShutdown());
524 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
525 >        assertTrue(p.isShutdown());
526 >        joinPool(p);
527      }
528  
415
529      /**
530 <     *  isTerminated is false before termination, true after
530 >     * isTerminated is false before termination, true after
531       */
532      public void testIsTerminated() throws InterruptedException {
533 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
534 <        assertFalse(p1.isTerminated());
535 <        try {
536 <            p1.execute(new MediumRunnable());
537 <        } finally {
538 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
539 <        }
540 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
541 <        assertTrue(p1.isTerminated());
533 >        final ThreadPoolExecutor p =
534 >            new CustomTPE(1, 1,
535 >                          LONG_DELAY_MS, MILLISECONDS,
536 >                          new ArrayBlockingQueue<Runnable>(10));
537 >        final CountDownLatch threadStarted = new CountDownLatch(1);
538 >        final CountDownLatch done = new CountDownLatch(1);
539 >        try {
540 >            assertFalse(p.isTerminating());
541 >            p.execute(new CheckedRunnable() {
542 >                public void realRun() throws InterruptedException {
543 >                    assertFalse(p.isTerminating());
544 >                    threadStarted.countDown();
545 >                    done.await();
546 >                }});
547 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
548 >            assertFalse(p.isTerminating());
549 >            done.countDown();
550 >        } finally {
551 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
552 >        }
553 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
554 >        assertTrue(p.isTerminated());
555 >        assertFalse(p.isTerminating());
556      }
557  
558      /**
559 <     *  isTerminating is not true when running or when terminated
559 >     * isTerminating is not true when running or when terminated
560       */
561      public void testIsTerminating() throws InterruptedException {
562 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
563 <        assertFalse(p1.isTerminating());
564 <        try {
565 <            p1.execute(new SmallRunnable());
566 <            assertFalse(p1.isTerminating());
567 <        } finally {
568 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
569 <        }
570 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
571 <        assertTrue(p1.isTerminated());
572 <        assertFalse(p1.isTerminating());
562 >        final ThreadPoolExecutor p =
563 >            new CustomTPE(1, 1,
564 >                          LONG_DELAY_MS, MILLISECONDS,
565 >                          new ArrayBlockingQueue<Runnable>(10));
566 >        final CountDownLatch threadStarted = new CountDownLatch(1);
567 >        final CountDownLatch done = new CountDownLatch(1);
568 >        try {
569 >            assertFalse(p.isTerminating());
570 >            p.execute(new CheckedRunnable() {
571 >                public void realRun() throws InterruptedException {
572 >                    assertFalse(p.isTerminating());
573 >                    threadStarted.countDown();
574 >                    done.await();
575 >                }});
576 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
577 >            assertFalse(p.isTerminating());
578 >            done.countDown();
579 >        } finally {
580 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
581 >        }
582 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
583 >        assertTrue(p.isTerminated());
584 >        assertFalse(p.isTerminating());
585      }
586  
587      /**
588       * getQueue returns the work queue, which contains queued tasks
589       */
590      public void testGetQueue() throws InterruptedException {
591 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
592 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
593 <        FutureTask[] tasks = new FutureTask[5];
594 <        for (int i = 0; i < 5; i++) {
595 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
596 <            p1.execute(tasks[i]);
597 <        }
598 <        try {
599 <            Thread.sleep(SHORT_DELAY_MS);
600 <            BlockingQueue<Runnable> wq = p1.getQueue();
601 <            assertSame(q, wq);
602 <            assertFalse(wq.contains(tasks[0]));
603 <            assertTrue(wq.contains(tasks[4]));
604 <            for (int i = 1; i < 5; ++i)
605 <                tasks[i].cancel(true);
606 <            p1.shutdownNow();
591 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
592 >        final ThreadPoolExecutor p =
593 >            new CustomTPE(1, 1,
594 >                          LONG_DELAY_MS, MILLISECONDS,
595 >                          q);
596 >        final CountDownLatch threadStarted = new CountDownLatch(1);
597 >        final CountDownLatch done = new CountDownLatch(1);
598 >        try {
599 >            FutureTask[] tasks = new FutureTask[5];
600 >            for (int i = 0; i < tasks.length; i++) {
601 >                Callable task = new CheckedCallable<Boolean>() {
602 >                    public Boolean realCall() throws InterruptedException {
603 >                        threadStarted.countDown();
604 >                        assertSame(q, p.getQueue());
605 >                        done.await();
606 >                        return Boolean.TRUE;
607 >                    }};
608 >                tasks[i] = new FutureTask(task);
609 >                p.execute(tasks[i]);
610 >            }
611 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
612 >            assertSame(q, p.getQueue());
613 >            assertFalse(q.contains(tasks[0]));
614 >            assertTrue(q.contains(tasks[tasks.length - 1]));
615 >            assertEquals(tasks.length - 1, q.size());
616          } finally {
617 <            joinPool(p1);
617 >            done.countDown();
618 >            joinPool(p);
619          }
620      }
621  
# Line 475 | Line 624 | public class ThreadPoolExecutorSubclassT
624       */
625      public void testRemove() throws InterruptedException {
626          BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
627 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
628 <        FutureTask[] tasks = new FutureTask[5];
629 <        for (int i = 0; i < 5; i++) {
630 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
631 <            p1.execute(tasks[i]);
632 <        }
633 <        try {
634 <            Thread.sleep(SHORT_DELAY_MS);
635 <            assertFalse(p1.remove(tasks[0]));
627 >        final ThreadPoolExecutor p =
628 >            new CustomTPE(1, 1,
629 >                          LONG_DELAY_MS, MILLISECONDS,
630 >                          q);
631 >        Runnable[] tasks = new Runnable[6];
632 >        final CountDownLatch threadStarted = new CountDownLatch(1);
633 >        final CountDownLatch done = new CountDownLatch(1);
634 >        try {
635 >            for (int i = 0; i < tasks.length; i++) {
636 >                tasks[i] = new CheckedRunnable() {
637 >                        public void realRun() throws InterruptedException {
638 >                            threadStarted.countDown();
639 >                            done.await();
640 >                        }};
641 >                p.execute(tasks[i]);
642 >            }
643 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
644 >            assertFalse(p.remove(tasks[0]));
645              assertTrue(q.contains(tasks[4]));
646              assertTrue(q.contains(tasks[3]));
647 <            assertTrue(p1.remove(tasks[4]));
648 <            assertFalse(p1.remove(tasks[4]));
647 >            assertTrue(p.remove(tasks[4]));
648 >            assertFalse(p.remove(tasks[4]));
649              assertFalse(q.contains(tasks[4]));
650              assertTrue(q.contains(tasks[3]));
651 <            assertTrue(p1.remove(tasks[3]));
651 >            assertTrue(p.remove(tasks[3]));
652              assertFalse(q.contains(tasks[3]));
653          } finally {
654 <            joinPool(p1);
654 >            done.countDown();
655 >            joinPool(p);
656          }
657      }
658  
659      /**
660 <     *   purge removes cancelled tasks from the queue
660 >     * purge removes cancelled tasks from the queue
661       */
662 <    public void testPurge() {
663 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
662 >    public void testPurge() throws InterruptedException {
663 >        final CountDownLatch threadStarted = new CountDownLatch(1);
664 >        final CountDownLatch done = new CountDownLatch(1);
665 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
666 >        final ThreadPoolExecutor p =
667 >            new CustomTPE(1, 1,
668 >                          LONG_DELAY_MS, MILLISECONDS,
669 >                          q);
670          FutureTask[] tasks = new FutureTask[5];
671 <        for (int i = 0; i < 5; i++) {
672 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
673 <            p1.execute(tasks[i]);
671 >        try {
672 >            for (int i = 0; i < tasks.length; i++) {
673 >                Callable task = new CheckedCallable<Boolean>() {
674 >                    public Boolean realCall() throws InterruptedException {
675 >                        threadStarted.countDown();
676 >                        done.await();
677 >                        return Boolean.TRUE;
678 >                    }};
679 >                tasks[i] = new FutureTask(task);
680 >                p.execute(tasks[i]);
681 >            }
682 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
683 >            assertEquals(tasks.length, p.getTaskCount());
684 >            assertEquals(tasks.length - 1, q.size());
685 >            assertEquals(1L, p.getActiveCount());
686 >            assertEquals(0L, p.getCompletedTaskCount());
687 >            tasks[4].cancel(true);
688 >            tasks[3].cancel(false);
689 >            p.purge();
690 >            assertEquals(tasks.length - 3, q.size());
691 >            assertEquals(tasks.length - 2, p.getTaskCount());
692 >            p.purge();         // Nothing to do
693 >            assertEquals(tasks.length - 3, q.size());
694 >            assertEquals(tasks.length - 2, p.getTaskCount());
695 >        } finally {
696 >            done.countDown();
697 >            joinPool(p);
698          }
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);
699      }
700  
701      /**
702 <     *  shutDownNow returns a list containing tasks that were not run
702 >     * shutdownNow returns a list containing tasks that were not run,
703 >     * and those tasks are drained from the queue
704       */
705 <    public void testShutDownNow() {
706 <        ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
705 >    public void testShutdownNow() {
706 >        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
707          List l;
708          try {
709              for (int i = 0; i < 5; i++)
710 <                p1.execute(new MediumPossiblyInterruptedRunnable());
710 >                p.execute(new MediumPossiblyInterruptedRunnable());
711          }
712          finally {
713              try {
714 <                l = p1.shutdownNow();
714 >                l = p.shutdownNow();
715              } catch (SecurityException ok) { return; }
716          }
717 <        assertTrue(p1.isShutdown());
717 >        assertTrue(p.isShutdown());
718 >        assertTrue(p.getQueue().isEmpty());
719          assertTrue(l.size() <= 4);
720      }
721  
722      // Exception Tests
723  
539
724      /**
725       * Constructor throws if corePoolSize argument is less than zero
726       */
727      public void testConstructor1() {
728          try {
729 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
729 >            new CustomTPE(-1, 1, 1L, SECONDS,
730 >                          new ArrayBlockingQueue<Runnable>(10));
731              shouldThrow();
732          } catch (IllegalArgumentException success) {}
733      }
# Line 552 | Line 737 | public class ThreadPoolExecutorSubclassT
737       */
738      public void testConstructor2() {
739          try {
740 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
740 >            new CustomTPE(1, -1, 1L, SECONDS,
741 >                          new ArrayBlockingQueue<Runnable>(10));
742              shouldThrow();
743          } catch (IllegalArgumentException success) {}
744      }
# Line 562 | Line 748 | public class ThreadPoolExecutorSubclassT
748       */
749      public void testConstructor3() {
750          try {
751 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
751 >            new CustomTPE(1, 0, 1L, SECONDS,
752 >                          new ArrayBlockingQueue<Runnable>(10));
753              shouldThrow();
754          } catch (IllegalArgumentException success) {}
755      }
# Line 572 | Line 759 | public class ThreadPoolExecutorSubclassT
759       */
760      public void testConstructor4() {
761          try {
762 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
762 >            new CustomTPE(1, 2, -1L, SECONDS,
763 >                          new ArrayBlockingQueue<Runnable>(10));
764              shouldThrow();
765          } catch (IllegalArgumentException success) {}
766      }
# Line 582 | Line 770 | public class ThreadPoolExecutorSubclassT
770       */
771      public void testConstructor5() {
772          try {
773 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
773 >            new CustomTPE(2, 1, 1L, SECONDS,
774 >                          new ArrayBlockingQueue<Runnable>(10));
775              shouldThrow();
776          } catch (IllegalArgumentException success) {}
777      }
# Line 592 | Line 781 | public class ThreadPoolExecutorSubclassT
781       */
782      public void testConstructorNullPointerException() {
783          try {
784 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
784 >            new CustomTPE(1, 2, 1L, SECONDS, null);
785              shouldThrow();
786          } catch (NullPointerException success) {}
787      }
788  
600
601
789      /**
790       * Constructor throws if corePoolSize argument is less than zero
791       */
792      public void testConstructor6() {
793          try {
794 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
794 >            new CustomTPE(-1, 1, 1L, SECONDS,
795 >                          new ArrayBlockingQueue<Runnable>(10),
796 >                          new SimpleThreadFactory());
797              shouldThrow();
798          } catch (IllegalArgumentException success) {}
799      }
# Line 614 | Line 803 | public class ThreadPoolExecutorSubclassT
803       */
804      public void testConstructor7() {
805          try {
806 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
806 >            new CustomTPE(1,-1, 1L, SECONDS,
807 >                          new ArrayBlockingQueue<Runnable>(10),
808 >                          new SimpleThreadFactory());
809              shouldThrow();
810          } catch (IllegalArgumentException success) {}
811      }
# Line 624 | Line 815 | public class ThreadPoolExecutorSubclassT
815       */
816      public void testConstructor8() {
817          try {
818 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
818 >            new CustomTPE(1, 0, 1L, SECONDS,
819 >                          new ArrayBlockingQueue<Runnable>(10),
820 >                          new SimpleThreadFactory());
821              shouldThrow();
822          } catch (IllegalArgumentException success) {}
823      }
# Line 634 | Line 827 | public class ThreadPoolExecutorSubclassT
827       */
828      public void testConstructor9() {
829          try {
830 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
830 >            new CustomTPE(1, 2, -1L, SECONDS,
831 >                          new ArrayBlockingQueue<Runnable>(10),
832 >                          new SimpleThreadFactory());
833              shouldThrow();
834          } catch (IllegalArgumentException success) {}
835      }
# Line 644 | Line 839 | public class ThreadPoolExecutorSubclassT
839       */
840      public void testConstructor10() {
841          try {
842 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
842 >            new CustomTPE(2, 1, 1L, SECONDS,
843 >                          new ArrayBlockingQueue<Runnable>(10),
844 >                          new SimpleThreadFactory());
845              shouldThrow();
846          } catch (IllegalArgumentException success) {}
847      }
# Line 654 | Line 851 | public class ThreadPoolExecutorSubclassT
851       */
852      public void testConstructorNullPointerException2() {
853          try {
854 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
854 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
855              shouldThrow();
856          } catch (NullPointerException success) {}
857      }
# Line 664 | Line 861 | public class ThreadPoolExecutorSubclassT
861       */
862      public void testConstructorNullPointerException3() {
863          try {
864 <            ThreadFactory f = null;
865 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
864 >            new CustomTPE(1, 2, 1L, SECONDS,
865 >                          new ArrayBlockingQueue<Runnable>(10),
866 >                          (ThreadFactory) null);
867              shouldThrow();
868          } catch (NullPointerException success) {}
869      }
870  
673
871      /**
872       * Constructor throws if corePoolSize argument is less than zero
873       */
874      public void testConstructor11() {
875          try {
876 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
876 >            new CustomTPE(-1, 1, 1L, SECONDS,
877 >                          new ArrayBlockingQueue<Runnable>(10),
878 >                          new NoOpREHandler());
879              shouldThrow();
880          } catch (IllegalArgumentException success) {}
881      }
# Line 686 | Line 885 | public class ThreadPoolExecutorSubclassT
885       */
886      public void testConstructor12() {
887          try {
888 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
888 >            new CustomTPE(1, -1, 1L, SECONDS,
889 >                          new ArrayBlockingQueue<Runnable>(10),
890 >                          new NoOpREHandler());
891              shouldThrow();
892          } catch (IllegalArgumentException success) {}
893      }
# Line 696 | Line 897 | public class ThreadPoolExecutorSubclassT
897       */
898      public void testConstructor13() {
899          try {
900 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
900 >            new CustomTPE(1, 0, 1L, SECONDS,
901 >                          new ArrayBlockingQueue<Runnable>(10),
902 >                          new NoOpREHandler());
903              shouldThrow();
904          } catch (IllegalArgumentException success) {}
905      }
# Line 706 | Line 909 | public class ThreadPoolExecutorSubclassT
909       */
910      public void testConstructor14() {
911          try {
912 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
912 >            new CustomTPE(1, 2, -1L, SECONDS,
913 >                          new ArrayBlockingQueue<Runnable>(10),
914 >                          new NoOpREHandler());
915              shouldThrow();
916          } catch (IllegalArgumentException success) {}
917      }
# Line 716 | Line 921 | public class ThreadPoolExecutorSubclassT
921       */
922      public void testConstructor15() {
923          try {
924 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
924 >            new CustomTPE(2, 1, 1L, SECONDS,
925 >                          new ArrayBlockingQueue<Runnable>(10),
926 >                          new NoOpREHandler());
927              shouldThrow();
928          } catch (IllegalArgumentException success) {}
929      }
# Line 726 | Line 933 | public class ThreadPoolExecutorSubclassT
933       */
934      public void testConstructorNullPointerException4() {
935          try {
936 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
936 >            new CustomTPE(1, 2, 1L, SECONDS,
937 >                          null,
938 >                          new NoOpREHandler());
939              shouldThrow();
940          } catch (NullPointerException success) {}
941      }
# Line 736 | Line 945 | public class ThreadPoolExecutorSubclassT
945       */
946      public void testConstructorNullPointerException5() {
947          try {
948 <            RejectedExecutionHandler r = null;
949 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
948 >            new CustomTPE(1, 2, 1L, SECONDS,
949 >                          new ArrayBlockingQueue<Runnable>(10),
950 >                          (RejectedExecutionHandler) null);
951              shouldThrow();
952          } catch (NullPointerException success) {}
953      }
954  
745
955      /**
956       * Constructor throws if corePoolSize argument is less than zero
957       */
958      public void testConstructor16() {
959          try {
960 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
960 >            new CustomTPE(-1, 1, 1L, SECONDS,
961 >                          new ArrayBlockingQueue<Runnable>(10),
962 >                          new SimpleThreadFactory(),
963 >                          new NoOpREHandler());
964              shouldThrow();
965          } catch (IllegalArgumentException success) {}
966      }
# Line 758 | Line 970 | public class ThreadPoolExecutorSubclassT
970       */
971      public void testConstructor17() {
972          try {
973 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
973 >            new CustomTPE(1, -1, 1L, SECONDS,
974 >                          new ArrayBlockingQueue<Runnable>(10),
975 >                          new SimpleThreadFactory(),
976 >                          new NoOpREHandler());
977              shouldThrow();
978          } catch (IllegalArgumentException success) {}
979      }
# Line 768 | Line 983 | public class ThreadPoolExecutorSubclassT
983       */
984      public void testConstructor18() {
985          try {
986 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
986 >            new CustomTPE(1, 0, 1L, SECONDS,
987 >                          new ArrayBlockingQueue<Runnable>(10),
988 >                          new SimpleThreadFactory(),
989 >                          new NoOpREHandler());
990              shouldThrow();
991          } catch (IllegalArgumentException success) {}
992      }
# Line 778 | Line 996 | public class ThreadPoolExecutorSubclassT
996       */
997      public void testConstructor19() {
998          try {
999 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
999 >            new CustomTPE(1, 2, -1L, SECONDS,
1000 >                          new ArrayBlockingQueue<Runnable>(10),
1001 >                          new SimpleThreadFactory(),
1002 >                          new NoOpREHandler());
1003              shouldThrow();
1004          } catch (IllegalArgumentException success) {}
1005      }
# Line 788 | Line 1009 | public class ThreadPoolExecutorSubclassT
1009       */
1010      public void testConstructor20() {
1011          try {
1012 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1012 >            new CustomTPE(2, 1, 1L, SECONDS,
1013 >                          new ArrayBlockingQueue<Runnable>(10),
1014 >                          new SimpleThreadFactory(),
1015 >                          new NoOpREHandler());
1016              shouldThrow();
1017          } catch (IllegalArgumentException success) {}
1018      }
1019  
1020      /**
1021 <     * Constructor throws if workQueue is set to null
1021 >     * Constructor throws if workQueue is null
1022       */
1023      public void testConstructorNullPointerException6() {
1024          try {
1025 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1025 >            new CustomTPE(1, 2, 1L, SECONDS,
1026 >                          null,
1027 >                          new SimpleThreadFactory(),
1028 >                          new NoOpREHandler());
1029              shouldThrow();
1030          } catch (NullPointerException success) {}
1031      }
1032  
1033      /**
1034 <     * Constructor throws if handler is set to null
1034 >     * Constructor throws if handler is null
1035       */
1036      public void testConstructorNullPointerException7() {
1037          try {
1038 <            RejectedExecutionHandler r = null;
1039 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
1038 >            new CustomTPE(1, 2, 1L, SECONDS,
1039 >                          new ArrayBlockingQueue<Runnable>(10),
1040 >                          new SimpleThreadFactory(),
1041 >                          (RejectedExecutionHandler) null);
1042              shouldThrow();
1043          } catch (NullPointerException success) {}
1044      }
1045  
1046      /**
1047 <     * Constructor throws if ThreadFactory is set top null
1047 >     * Constructor throws if ThreadFactory is null
1048       */
1049      public void testConstructorNullPointerException8() {
1050          try {
1051 <            ThreadFactory f = null;
1052 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
1051 >            new CustomTPE(1, 2, 1L, SECONDS,
1052 >                          new ArrayBlockingQueue<Runnable>(10),
1053 >                          (ThreadFactory) null,
1054 >                          new NoOpREHandler());
1055              shouldThrow();
1056          } catch (NullPointerException success) {}
1057      }
1058  
828
1059      /**
1060 <     *  execute throws RejectedExecutionException
831 <     *  if saturated.
1060 >     * execute throws RejectedExecutionException if saturated.
1061       */
1062      public void testSaturatedExecute() {
1063 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
1064 <        try {
1065 <
1066 <            for (int i = 0; i < 5; ++i) {
1067 <                p.execute(new MediumRunnable());
1063 >        ThreadPoolExecutor p =
1064 >            new CustomTPE(1, 1,
1065 >                          LONG_DELAY_MS, MILLISECONDS,
1066 >                          new ArrayBlockingQueue<Runnable>(1));
1067 >        final CountDownLatch done = new CountDownLatch(1);
1068 >        try {
1069 >            Runnable task = new CheckedRunnable() {
1070 >                public void realRun() throws InterruptedException {
1071 >                    done.await();
1072 >                }};
1073 >            for (int i = 0; i < 2; ++i)
1074 >                p.execute(task);
1075 >            for (int i = 0; i < 2; ++i) {
1076 >                try {
1077 >                    p.execute(task);
1078 >                    shouldThrow();
1079 >                } catch (RejectedExecutionException success) {}
1080 >                assertTrue(p.getTaskCount() <= 2);
1081              }
1082 <            shouldThrow();
1083 <        } catch (RejectedExecutionException success) {}
1084 <        joinPool(p);
1082 >        } finally {
1083 >            done.countDown();
1084 >            joinPool(p);
1085 >        }
1086      }
1087  
1088      /**
1089 <     *  executor using CallerRunsPolicy runs task if saturated.
1089 >     * executor using CallerRunsPolicy runs task if saturated.
1090       */
1091      public void testSaturatedExecute2() {
1092          RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1093 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1093 >        ThreadPoolExecutor p = new CustomTPE(1, 1,
1094 >                                             LONG_DELAY_MS, MILLISECONDS,
1095 >                                             new ArrayBlockingQueue<Runnable>(1),
1096 >                                             h);
1097          try {
852
1098              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1099 <            for (int i = 0; i < 5; ++i) {
1099 >            for (int i = 0; i < tasks.length; ++i)
1100                  tasks[i] = new TrackedNoOpRunnable();
856            }
1101              TrackedLongRunnable mr = new TrackedLongRunnable();
1102              p.execute(mr);
1103 <            for (int i = 0; i < 5; ++i) {
1103 >            for (int i = 0; i < tasks.length; ++i)
1104                  p.execute(tasks[i]);
1105 <            }
862 <            for (int i = 1; i < 5; ++i) {
1105 >            for (int i = 1; i < tasks.length; ++i)
1106                  assertTrue(tasks[i].done);
864            }
1107              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1108          } finally {
1109              joinPool(p);
# Line 869 | Line 1111 | public class ThreadPoolExecutorSubclassT
1111      }
1112  
1113      /**
1114 <     *  executor using DiscardPolicy drops task if saturated.
1114 >     * executor using DiscardPolicy drops task if saturated.
1115       */
1116      public void testSaturatedExecute3() {
1117          RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1118 <        ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1118 >        ThreadPoolExecutor p =
1119 >            new CustomTPE(1, 1,
1120 >                          LONG_DELAY_MS, MILLISECONDS,
1121 >                          new ArrayBlockingQueue<Runnable>(1),
1122 >                          h);
1123          try {
878
1124              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1125 <            for (int i = 0; i < 5; ++i) {
1125 >            for (int i = 0; i < tasks.length; ++i)
1126                  tasks[i] = new TrackedNoOpRunnable();
882            }
1127              p.execute(new TrackedLongRunnable());
1128 <            for (int i = 0; i < 5; ++i) {
1129 <                p.execute(tasks[i]);
1130 <            }
1131 <            for (int i = 0; i < 5; ++i) {
888 <                assertFalse(tasks[i].done);
889 <            }
1128 >            for (TrackedNoOpRunnable task : tasks)
1129 >                p.execute(task);
1130 >            for (TrackedNoOpRunnable task : tasks)
1131 >                assertFalse(task.done);
1132              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1133          } finally {
1134              joinPool(p);
# Line 894 | Line 1136 | public class ThreadPoolExecutorSubclassT
1136      }
1137  
1138      /**
1139 <     *  executor using DiscardOldestPolicy drops oldest task if saturated.
1139 >     * executor using DiscardOldestPolicy drops oldest task if saturated.
1140       */
1141      public void testSaturatedExecute4() {
1142          RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
# Line 915 | Line 1157 | public class ThreadPoolExecutorSubclassT
1157      }
1158  
1159      /**
1160 <     *  execute throws RejectedExecutionException if shutdown
1160 >     * execute throws RejectedExecutionException if shutdown
1161       */
1162      public void testRejectedExecutionExceptionOnShutdown() {
1163 <        ThreadPoolExecutor tpe =
1163 >        ThreadPoolExecutor p =
1164              new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1165 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1165 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1166          try {
1167 <            tpe.execute(new NoOpRunnable());
1167 >            p.execute(new NoOpRunnable());
1168              shouldThrow();
1169          } catch (RejectedExecutionException success) {}
1170  
1171 <        joinPool(tpe);
1171 >        joinPool(p);
1172      }
1173  
1174      /**
1175 <     *  execute using CallerRunsPolicy drops task on shutdown
1175 >     * execute using CallerRunsPolicy drops task on shutdown
1176       */
1177      public void testCallerRunsOnShutdown() {
1178          RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
# Line 947 | Line 1189 | public class ThreadPoolExecutorSubclassT
1189      }
1190  
1191      /**
1192 <     *  execute using DiscardPolicy drops task on shutdown
1192 >     * execute using DiscardPolicy drops task on shutdown
1193       */
1194      public void testDiscardOnShutdown() {
1195          RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
# Line 963 | Line 1205 | public class ThreadPoolExecutorSubclassT
1205          }
1206      }
1207  
966
1208      /**
1209 <     *  execute using DiscardOldestPolicy drops task on shutdown
1209 >     * execute using DiscardOldestPolicy drops task on shutdown
1210       */
1211      public void testDiscardOldestOnShutdown() {
1212          RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
# Line 981 | Line 1222 | public class ThreadPoolExecutorSubclassT
1222          }
1223      }
1224  
984
1225      /**
1226 <     *  execute (null) throws NPE
1226 >     * execute(null) throws NPE
1227       */
1228      public void testExecuteNull() {
1229 <        ThreadPoolExecutor tpe = null;
1229 >        ThreadPoolExecutor p =
1230 >            new CustomTPE(1, 2, 1L, SECONDS,
1231 >                          new ArrayBlockingQueue<Runnable>(10));
1232          try {
1233 <            tpe = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
992 <            tpe.execute(null);
1233 >            p.execute(null);
1234              shouldThrow();
1235          } catch (NullPointerException success) {}
1236  
1237 <        joinPool(tpe);
1237 >        joinPool(p);
1238      }
1239  
1240      /**
1241 <     *  setCorePoolSize of negative value throws IllegalArgumentException
1241 >     * setCorePoolSize of negative value throws IllegalArgumentException
1242       */
1243      public void testCorePoolSizeIllegalArgumentException() {
1244 <        ThreadPoolExecutor tpe =
1244 >        ThreadPoolExecutor p =
1245              new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1246          try {
1247 <            tpe.setCorePoolSize(-1);
1247 >            p.setCorePoolSize(-1);
1248              shouldThrow();
1249          } catch (IllegalArgumentException success) {
1250          } finally {
1251 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1251 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1252          }
1253 <        joinPool(tpe);
1253 >        joinPool(p);
1254      }
1255  
1256      /**
1257 <     *  setMaximumPoolSize(int) throws IllegalArgumentException if
1258 <     *  given a value less the core pool size
1257 >     * setMaximumPoolSize(int) throws IllegalArgumentException
1258 >     * if given a value less the core pool size
1259       */
1260      public void testMaximumPoolSizeIllegalArgumentException() {
1261 <        ThreadPoolExecutor tpe =
1261 >        ThreadPoolExecutor p =
1262              new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1263          try {
1264 <            tpe.setMaximumPoolSize(1);
1264 >            p.setMaximumPoolSize(1);
1265              shouldThrow();
1266          } catch (IllegalArgumentException success) {
1267          } finally {
1268 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1268 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1269          }
1270 <        joinPool(tpe);
1270 >        joinPool(p);
1271      }
1272  
1273      /**
1274 <     *  setMaximumPoolSize throws IllegalArgumentException
1275 <     *  if given a negative value
1274 >     * setMaximumPoolSize throws IllegalArgumentException
1275 >     * if given a negative value
1276       */
1277      public void testMaximumPoolSizeIllegalArgumentException2() {
1278 <        ThreadPoolExecutor tpe =
1278 >        ThreadPoolExecutor p =
1279              new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1280          try {
1281 <            tpe.setMaximumPoolSize(-1);
1281 >            p.setMaximumPoolSize(-1);
1282              shouldThrow();
1283          } catch (IllegalArgumentException success) {
1284          } finally {
1285 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1285 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1286          }
1287 <        joinPool(tpe);
1287 >        joinPool(p);
1288      }
1289  
1049
1290      /**
1291 <     *  setKeepAliveTime  throws IllegalArgumentException
1292 <     *  when given a negative value
1291 >     * setKeepAliveTime throws IllegalArgumentException
1292 >     * when given a negative value
1293       */
1294      public void testKeepAliveTimeIllegalArgumentException() {
1295 <        ThreadPoolExecutor tpe =
1295 >        ThreadPoolExecutor p =
1296              new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1297  
1298          try {
1299 <            tpe.setKeepAliveTime(-1,MILLISECONDS);
1299 >            p.setKeepAliveTime(-1,MILLISECONDS);
1300              shouldThrow();
1301          } catch (IllegalArgumentException success) {
1302          } finally {
1303 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1303 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1304          }
1305 <        joinPool(tpe);
1305 >        joinPool(p);
1306      }
1307  
1308      /**
1309       * terminated() is called on termination
1310       */
1311      public void testTerminated() {
1312 <        CustomTPE tpe = new CustomTPE();
1313 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1314 <        assertTrue(tpe.terminatedCalled);
1315 <        joinPool(tpe);
1312 >        CustomTPE p = new CustomTPE();
1313 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1314 >        assertTrue(p.terminatedCalled());
1315 >        joinPool(p);
1316      }
1317  
1318      /**
1319       * beforeExecute and afterExecute are called when executing task
1320       */
1321      public void testBeforeAfter() throws InterruptedException {
1322 <        CustomTPE tpe = new CustomTPE();
1322 >        CustomTPE p = new CustomTPE();
1323          try {
1324 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1325 <            tpe.execute(r);
1326 <            Thread.sleep(SHORT_DELAY_MS);
1327 <            assertTrue(r.done);
1328 <            assertTrue(tpe.beforeCalled);
1329 <            assertTrue(tpe.afterCalled);
1330 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1324 >            final CountDownLatch done = new CountDownLatch(1);
1325 >            p.execute(new CheckedRunnable() {
1326 >                public void realRun() {
1327 >                    done.countDown();
1328 >                }});
1329 >            await(p.afterCalled);
1330 >            assertEquals(0, done.getCount());
1331 >            assertTrue(p.afterCalled());
1332 >            assertTrue(p.beforeCalled());
1333 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1334          } finally {
1335 <            joinPool(tpe);
1335 >            joinPool(p);
1336          }
1337      }
1338  
# Line 1135 | Line 1378 | public class ThreadPoolExecutorSubclassT
1378          }
1379      }
1380  
1138
1381      /**
1382       * invokeAny(null) throws NPE
1383       */
# Line 1297 | Line 1539 | public class ThreadPoolExecutorSubclassT
1539          }
1540      }
1541  
1300
1301
1542      /**
1543       * timed invokeAny(null) throws NPE
1544       */
# Line 1500 | Line 1740 | public class ThreadPoolExecutorSubclassT
1740      public void testTimedInvokeAll6() throws Exception {
1741          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1742          try {
1743 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1744 <            l.add(new StringTask());
1745 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1746 <            l.add(new StringTask());
1747 <            List<Future<String>> futures =
1748 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1749 <            assertEquals(3, futures.size());
1750 <            Iterator<Future<String>> it = futures.iterator();
1751 <            Future<String> f1 = it.next();
1752 <            Future<String> f2 = it.next();
1753 <            Future<String> f3 = it.next();
1754 <            assertTrue(f1.isDone());
1755 <            assertTrue(f2.isDone());
1756 <            assertTrue(f3.isDone());
1757 <            assertFalse(f1.isCancelled());
1758 <            assertTrue(f2.isCancelled());
1743 >            for (long timeout = timeoutMillis();;) {
1744 >                List<Callable<String>> tasks = new ArrayList<>();
1745 >                tasks.add(new StringTask("0"));
1746 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1747 >                tasks.add(new StringTask("2"));
1748 >                long startTime = System.nanoTime();
1749 >                List<Future<String>> futures =
1750 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1751 >                assertEquals(tasks.size(), futures.size());
1752 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1753 >                for (Future future : futures)
1754 >                    assertTrue(future.isDone());
1755 >                assertTrue(futures.get(1).isCancelled());
1756 >                try {
1757 >                    assertEquals("0", futures.get(0).get());
1758 >                    assertEquals("2", futures.get(2).get());
1759 >                    break;
1760 >                } catch (CancellationException retryWithLongerTimeout) {
1761 >                    timeout *= 2;
1762 >                    if (timeout >= LONG_DELAY_MS / 2)
1763 >                        fail("expected exactly one task to be cancelled");
1764 >                }
1765 >            }
1766          } finally {
1767              joinPool(e);
1768          }
# Line 1526 | Line 1773 | public class ThreadPoolExecutorSubclassT
1773       * thread factory fails to create more
1774       */
1775      public void testFailingThreadFactory() throws InterruptedException {
1776 <        ExecutorService e = new CustomTPE(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1777 <        try {
1778 <            for (int k = 0; k < 100; ++k) {
1779 <                e.execute(new NoOpRunnable());
1780 <            }
1781 <            Thread.sleep(LONG_DELAY_MS);
1776 >        final ExecutorService e =
1777 >            new CustomTPE(100, 100,
1778 >                          LONG_DELAY_MS, MILLISECONDS,
1779 >                          new LinkedBlockingQueue<Runnable>(),
1780 >                          new FailingThreadFactory());
1781 >        try {
1782 >            final int TASKS = 100;
1783 >            final CountDownLatch done = new CountDownLatch(TASKS);
1784 >            for (int k = 0; k < TASKS; ++k)
1785 >                e.execute(new CheckedRunnable() {
1786 >                    public void realRun() {
1787 >                        done.countDown();
1788 >                    }});
1789 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1790          } finally {
1791              joinPool(e);
1792          }
# Line 1541 | Line 1796 | public class ThreadPoolExecutorSubclassT
1796       * allowsCoreThreadTimeOut is by default false.
1797       */
1798      public void testAllowsCoreThreadTimeOut() {
1799 <        ThreadPoolExecutor tpe = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1800 <        assertFalse(tpe.allowsCoreThreadTimeOut());
1801 <        joinPool(tpe);
1799 >        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1800 >        assertFalse(p.allowsCoreThreadTimeOut());
1801 >        joinPool(p);
1802      }
1803  
1804      /**
1805       * allowCoreThreadTimeOut(true) causes idle threads to time out
1806       */
1807 <    public void testAllowCoreThreadTimeOut_true() throws InterruptedException {
1808 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1809 <        tpe.allowCoreThreadTimeOut(true);
1810 <        tpe.execute(new NoOpRunnable());
1811 <        try {
1812 <            Thread.sleep(MEDIUM_DELAY_MS);
1813 <            assertEquals(0, tpe.getPoolSize());
1807 >    public void testAllowCoreThreadTimeOut_true() throws Exception {
1808 >        long keepAliveTime = timeoutMillis();
1809 >        final ThreadPoolExecutor p =
1810 >            new CustomTPE(2, 10,
1811 >                          keepAliveTime, MILLISECONDS,
1812 >                          new ArrayBlockingQueue<Runnable>(10));
1813 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1814 >        try {
1815 >            p.allowCoreThreadTimeOut(true);
1816 >            p.execute(new CheckedRunnable() {
1817 >                public void realRun() {
1818 >                    threadStarted.countDown();
1819 >                    assertEquals(1, p.getPoolSize());
1820 >                }});
1821 >            await(threadStarted);
1822 >            delay(keepAliveTime);
1823 >            long startTime = System.nanoTime();
1824 >            while (p.getPoolSize() > 0
1825 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1826 >                Thread.yield();
1827 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1828 >            assertEquals(0, p.getPoolSize());
1829          } finally {
1830 <            joinPool(tpe);
1830 >            joinPool(p);
1831          }
1832      }
1833  
1834      /**
1835       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1836       */
1837 <    public void testAllowCoreThreadTimeOut_false() throws InterruptedException {
1838 <        ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1839 <        tpe.allowCoreThreadTimeOut(false);
1840 <        tpe.execute(new NoOpRunnable());
1841 <        try {
1842 <            Thread.sleep(MEDIUM_DELAY_MS);
1843 <            assertTrue(tpe.getPoolSize() >= 1);
1837 >    public void testAllowCoreThreadTimeOut_false() throws Exception {
1838 >        long keepAliveTime = timeoutMillis();
1839 >        final ThreadPoolExecutor p =
1840 >            new CustomTPE(2, 10,
1841 >                          keepAliveTime, MILLISECONDS,
1842 >                          new ArrayBlockingQueue<Runnable>(10));
1843 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1844 >        try {
1845 >            p.allowCoreThreadTimeOut(false);
1846 >            p.execute(new CheckedRunnable() {
1847 >                public void realRun() throws InterruptedException {
1848 >                    threadStarted.countDown();
1849 >                    assertTrue(p.getPoolSize() >= 1);
1850 >                }});
1851 >            delay(2 * keepAliveTime);
1852 >            assertTrue(p.getPoolSize() >= 1);
1853          } finally {
1854 <            joinPool(tpe);
1854 >            joinPool(p);
1855          }
1856      }
1857  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines