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

Comparing jsr166/src/test/tck/ThreadPoolExecutorTest.java (file contents):
Revision 1.41 by dl, Fri May 6 11:22:08 2011 UTC vs.
Revision 1.81 by jsr166, Sun Oct 4 02:24:49 2015 UTC

# Line 6 | Line 6
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.atomic.*;
11 < import junit.framework.*;
12 < import java.util.*;
10 > import static java.util.concurrent.TimeUnit.NANOSECONDS;
11 > import static java.util.concurrent.TimeUnit.SECONDS;
12 >
13 > import java.util.ArrayList;
14 > import java.util.List;
15 > import java.util.concurrent.ArrayBlockingQueue;
16 > import java.util.concurrent.BlockingQueue;
17 > import java.util.concurrent.Callable;
18 > import java.util.concurrent.CancellationException;
19 > import java.util.concurrent.CountDownLatch;
20 > import java.util.concurrent.ExecutionException;
21 > import java.util.concurrent.Executors;
22 > import java.util.concurrent.ExecutorService;
23 > import java.util.concurrent.Future;
24 > import java.util.concurrent.FutureTask;
25 > import java.util.concurrent.LinkedBlockingQueue;
26 > import java.util.concurrent.RejectedExecutionException;
27 > import java.util.concurrent.RejectedExecutionHandler;
28 > import java.util.concurrent.SynchronousQueue;
29 > import java.util.concurrent.ThreadFactory;
30 > import java.util.concurrent.ThreadPoolExecutor;
31 > import java.util.concurrent.TimeUnit;
32 > import java.util.concurrent.atomic.AtomicInteger;
33 >
34 > import junit.framework.Test;
35 > import junit.framework.TestSuite;
36  
37   public class ThreadPoolExecutorTest extends JSR166TestCase {
38      public static void main(String[] args) {
39 <        junit.textui.TestRunner.run(suite());
39 >        main(suite(), args);
40      }
41      public static Test suite() {
42          return new TestSuite(ThreadPoolExecutorTest.class);
43      }
44  
45      static class ExtendedTPE extends ThreadPoolExecutor {
46 <        volatile boolean beforeCalled = false;
47 <        volatile boolean afterCalled = false;
48 <        volatile boolean terminatedCalled = false;
46 >        final CountDownLatch beforeCalled = new CountDownLatch(1);
47 >        final CountDownLatch afterCalled = new CountDownLatch(1);
48 >        final CountDownLatch terminatedCalled = new CountDownLatch(1);
49 >
50          public ExtendedTPE() {
51              super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
52          }
53          protected void beforeExecute(Thread t, Runnable r) {
54 <            beforeCalled = true;
54 >            beforeCalled.countDown();
55          }
56          protected void afterExecute(Runnable r, Throwable t) {
57 <            afterCalled = true;
57 >            afterCalled.countDown();
58          }
59          protected void terminated() {
60 <            terminatedCalled = true;
60 >            terminatedCalled.countDown();
61 >        }
62 >
63 >        public boolean beforeCalled() {
64 >            return beforeCalled.getCount() == 0;
65 >        }
66 >        public boolean afterCalled() {
67 >            return afterCalled.getCount() == 0;
68 >        }
69 >        public boolean terminatedCalled() {
70 >            return terminatedCalled.getCount() == 0;
71          }
72      }
73  
# Line 46 | Line 79 | public class ThreadPoolExecutorTest exte
79          }
80      }
81  
49
82      /**
83       * execute successfully executes a runnable
84       */
# Line 55 | Line 87 | public class ThreadPoolExecutorTest exte
87              new ThreadPoolExecutor(1, 1,
88                                     LONG_DELAY_MS, MILLISECONDS,
89                                     new ArrayBlockingQueue<Runnable>(10));
90 <        final CountDownLatch done = new CountDownLatch(1);
91 <        final Runnable task = new CheckedRunnable() {
92 <            public void realRun() {
93 <                done.countDown();
62 <            }};
63 <        try {
90 >        try (PoolCleaner cleaner = cleaner(p)) {
91 >            final CountDownLatch done = new CountDownLatch(1);
92 >            final Runnable task = new CheckedRunnable() {
93 >                public void realRun() { done.countDown(); }};
94              p.execute(task);
95 <            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
66 <        } finally {
67 <            joinPool(p);
95 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
96          }
97      }
98  
# Line 77 | Line 105 | public class ThreadPoolExecutorTest exte
105              new ThreadPoolExecutor(2, 2,
106                                     LONG_DELAY_MS, MILLISECONDS,
107                                     new ArrayBlockingQueue<Runnable>(10));
108 <        final CountDownLatch threadStarted = new CountDownLatch(1);
109 <        final CountDownLatch done = new CountDownLatch(1);
110 <        try {
108 >        try (PoolCleaner cleaner = cleaner(p)) {
109 >            final CountDownLatch threadStarted = new CountDownLatch(1);
110 >            final CountDownLatch done = new CountDownLatch(1);
111              assertEquals(0, p.getActiveCount());
112              p.execute(new CheckedRunnable() {
113                  public void realRun() throws InterruptedException {
# Line 87 | Line 115 | public class ThreadPoolExecutorTest exte
115                      assertEquals(1, p.getActiveCount());
116                      done.await();
117                  }});
118 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
118 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
119              assertEquals(1, p.getActiveCount());
92        } finally {
120              done.countDown();
94            joinPool(p);
121          }
122      }
123  
# Line 100 | Line 126 | public class ThreadPoolExecutorTest exte
126       */
127      public void testPrestartCoreThread() {
128          final ThreadPoolExecutor p =
129 <            new ThreadPoolExecutor(2, 2,
129 >            new ThreadPoolExecutor(2, 6,
130                                     LONG_DELAY_MS, MILLISECONDS,
131                                     new ArrayBlockingQueue<Runnable>(10));
132 <        assertEquals(0, p.getPoolSize());
133 <        assertTrue(p.prestartCoreThread());
134 <        assertEquals(1, p.getPoolSize());
135 <        assertTrue(p.prestartCoreThread());
136 <        assertEquals(2, p.getPoolSize());
137 <        assertFalse(p.prestartCoreThread());
138 <        assertEquals(2, p.getPoolSize());
139 <        joinPool(p);
132 >        try (PoolCleaner cleaner = cleaner(p)) {
133 >            assertEquals(0, p.getPoolSize());
134 >            assertTrue(p.prestartCoreThread());
135 >            assertEquals(1, p.getPoolSize());
136 >            assertTrue(p.prestartCoreThread());
137 >            assertEquals(2, p.getPoolSize());
138 >            assertFalse(p.prestartCoreThread());
139 >            assertEquals(2, p.getPoolSize());
140 >            p.setCorePoolSize(4);
141 >            assertTrue(p.prestartCoreThread());
142 >            assertEquals(3, p.getPoolSize());
143 >            assertTrue(p.prestartCoreThread());
144 >            assertEquals(4, p.getPoolSize());
145 >            assertFalse(p.prestartCoreThread());
146 >            assertEquals(4, p.getPoolSize());
147 >        }
148      }
149  
150      /**
# Line 118 | Line 152 | public class ThreadPoolExecutorTest exte
152       */
153      public void testPrestartAllCoreThreads() {
154          final ThreadPoolExecutor p =
155 <            new ThreadPoolExecutor(2, 2,
155 >            new ThreadPoolExecutor(2, 6,
156                                     LONG_DELAY_MS, MILLISECONDS,
157                                     new ArrayBlockingQueue<Runnable>(10));
158 <        assertEquals(0, p.getPoolSize());
159 <        p.prestartAllCoreThreads();
160 <        assertEquals(2, p.getPoolSize());
161 <        p.prestartAllCoreThreads();
162 <        assertEquals(2, p.getPoolSize());
163 <        joinPool(p);
158 >        try (PoolCleaner cleaner = cleaner(p)) {
159 >            assertEquals(0, p.getPoolSize());
160 >            p.prestartAllCoreThreads();
161 >            assertEquals(2, p.getPoolSize());
162 >            p.prestartAllCoreThreads();
163 >            assertEquals(2, p.getPoolSize());
164 >            p.setCorePoolSize(4);
165 >            p.prestartAllCoreThreads();
166 >            assertEquals(4, p.getPoolSize());
167 >            p.prestartAllCoreThreads();
168 >            assertEquals(4, p.getPoolSize());
169 >        }
170      }
171  
172      /**
# Line 138 | Line 178 | public class ThreadPoolExecutorTest exte
178              new ThreadPoolExecutor(2, 2,
179                                     LONG_DELAY_MS, MILLISECONDS,
180                                     new ArrayBlockingQueue<Runnable>(10));
181 <        final CountDownLatch threadStarted = new CountDownLatch(1);
182 <        final CountDownLatch threadProceed = new CountDownLatch(1);
183 <        final CountDownLatch threadDone = new CountDownLatch(1);
184 <        try {
181 >        try (PoolCleaner cleaner = cleaner(p)) {
182 >            final CountDownLatch threadStarted = new CountDownLatch(1);
183 >            final CountDownLatch threadProceed = new CountDownLatch(1);
184 >            final CountDownLatch threadDone = new CountDownLatch(1);
185              assertEquals(0, p.getCompletedTaskCount());
186              p.execute(new CheckedRunnable() {
187                  public void realRun() throws InterruptedException {
# Line 150 | Line 190 | public class ThreadPoolExecutorTest exte
190                      threadProceed.await();
191                      threadDone.countDown();
192                  }});
193 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
193 >            await(threadStarted);
194              assertEquals(0, p.getCompletedTaskCount());
195              threadProceed.countDown();
196              threadDone.await();
197 <            delay(SHORT_DELAY_MS);
198 <            assertEquals(1, p.getCompletedTaskCount());
199 <        } finally {
200 <            joinPool(p);
197 >            long startTime = System.nanoTime();
198 >            while (p.getCompletedTaskCount() != 1) {
199 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
200 >                    fail("timed out");
201 >                Thread.yield();
202 >            }
203          }
204      }
205  
# Line 169 | Line 211 | public class ThreadPoolExecutorTest exte
211              new ThreadPoolExecutor(1, 1,
212                                     LONG_DELAY_MS, MILLISECONDS,
213                                     new ArrayBlockingQueue<Runnable>(10));
214 <        assertEquals(1, p.getCorePoolSize());
215 <        joinPool(p);
214 >        try (PoolCleaner cleaner = cleaner(p)) {
215 >            assertEquals(1, p.getCorePoolSize());
216 >        }
217      }
218  
219      /**
# Line 181 | Line 224 | public class ThreadPoolExecutorTest exte
224              new ThreadPoolExecutor(2, 2,
225                                     1000, MILLISECONDS,
226                                     new ArrayBlockingQueue<Runnable>(10));
227 <        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
228 <        joinPool(p);
227 >        try (PoolCleaner cleaner = cleaner(p)) {
228 >            assertEquals(1, p.getKeepAliveTime(SECONDS));
229 >        }
230      }
231  
188
232      /**
233       * getThreadFactory returns factory in constructor if not set
234       */
235      public void testGetThreadFactory() {
236 <        ThreadFactory tf = new SimpleThreadFactory();
236 >        ThreadFactory threadFactory = new SimpleThreadFactory();
237          final ThreadPoolExecutor p =
238              new ThreadPoolExecutor(1, 2,
239                                     LONG_DELAY_MS, MILLISECONDS,
240                                     new ArrayBlockingQueue<Runnable>(10),
241 <                                   tf,
241 >                                   threadFactory,
242                                     new NoOpREHandler());
243 <        assertSame(tf, p.getThreadFactory());
244 <        joinPool(p);
243 >        try (PoolCleaner cleaner = cleaner(p)) {
244 >            assertSame(threadFactory, p.getThreadFactory());
245 >        }
246      }
247  
248      /**
# Line 209 | Line 253 | public class ThreadPoolExecutorTest exte
253              new ThreadPoolExecutor(1, 2,
254                                     LONG_DELAY_MS, MILLISECONDS,
255                                     new ArrayBlockingQueue<Runnable>(10));
256 <        ThreadFactory tf = new SimpleThreadFactory();
257 <        p.setThreadFactory(tf);
258 <        assertSame(tf, p.getThreadFactory());
259 <        joinPool(p);
256 >        try (PoolCleaner cleaner = cleaner(p)) {
257 >            ThreadFactory threadFactory = new SimpleThreadFactory();
258 >            p.setThreadFactory(threadFactory);
259 >            assertSame(threadFactory, p.getThreadFactory());
260 >        }
261      }
262  
218
263      /**
264       * setThreadFactory(null) throws NPE
265       */
# Line 224 | Line 268 | public class ThreadPoolExecutorTest exte
268              new ThreadPoolExecutor(1, 2,
269                                     LONG_DELAY_MS, MILLISECONDS,
270                                     new ArrayBlockingQueue<Runnable>(10));
271 <        try {
272 <            p.setThreadFactory(null);
273 <            shouldThrow();
274 <        } catch (NullPointerException success) {
275 <        } finally {
232 <            joinPool(p);
271 >        try (PoolCleaner cleaner = cleaner(p)) {
272 >            try {
273 >                p.setThreadFactory(null);
274 >                shouldThrow();
275 >            } catch (NullPointerException success) {}
276          }
277      }
278  
# Line 237 | Line 280 | public class ThreadPoolExecutorTest exte
280       * getRejectedExecutionHandler returns handler in constructor if not set
281       */
282      public void testGetRejectedExecutionHandler() {
283 <        final RejectedExecutionHandler h = new NoOpREHandler();
283 >        final RejectedExecutionHandler handler = new NoOpREHandler();
284          final ThreadPoolExecutor p =
285              new ThreadPoolExecutor(1, 2,
286                                     LONG_DELAY_MS, MILLISECONDS,
287                                     new ArrayBlockingQueue<Runnable>(10),
288 <                                   h);
289 <        assertSame(h, p.getRejectedExecutionHandler());
290 <        joinPool(p);
288 >                                   handler);
289 >        try (PoolCleaner cleaner = cleaner(p)) {
290 >            assertSame(handler, p.getRejectedExecutionHandler());
291 >        }
292      }
293  
294      /**
# Line 256 | Line 300 | public class ThreadPoolExecutorTest exte
300              new ThreadPoolExecutor(1, 2,
301                                     LONG_DELAY_MS, MILLISECONDS,
302                                     new ArrayBlockingQueue<Runnable>(10));
303 <        RejectedExecutionHandler h = new NoOpREHandler();
304 <        p.setRejectedExecutionHandler(h);
305 <        assertSame(h, p.getRejectedExecutionHandler());
306 <        joinPool(p);
303 >        try (PoolCleaner cleaner = cleaner(p)) {
304 >            RejectedExecutionHandler handler = new NoOpREHandler();
305 >            p.setRejectedExecutionHandler(handler);
306 >            assertSame(handler, p.getRejectedExecutionHandler());
307 >        }
308      }
309  
265
310      /**
311       * setRejectedExecutionHandler(null) throws NPE
312       */
# Line 271 | Line 315 | public class ThreadPoolExecutorTest exte
315              new ThreadPoolExecutor(1, 2,
316                                     LONG_DELAY_MS, MILLISECONDS,
317                                     new ArrayBlockingQueue<Runnable>(10));
318 <        try {
319 <            p.setRejectedExecutionHandler(null);
320 <            shouldThrow();
321 <        } catch (NullPointerException success) {
322 <        } finally {
279 <            joinPool(p);
318 >        try (PoolCleaner cleaner = cleaner(p)) {
319 >            try {
320 >                p.setRejectedExecutionHandler(null);
321 >                shouldThrow();
322 >            } catch (NullPointerException success) {}
323          }
324      }
325  
283
326      /**
327       * getLargestPoolSize increases, but doesn't overestimate, when
328       * multiple threads active
# Line 291 | Line 333 | public class ThreadPoolExecutorTest exte
333              new ThreadPoolExecutor(THREADS, THREADS,
334                                     LONG_DELAY_MS, MILLISECONDS,
335                                     new ArrayBlockingQueue<Runnable>(10));
336 <        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
337 <        final CountDownLatch done = new CountDownLatch(1);
338 <        try {
336 >        try (PoolCleaner cleaner = cleaner(p)) {
337 >            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
338 >            final CountDownLatch done = new CountDownLatch(1);
339              assertEquals(0, p.getLargestPoolSize());
340              for (int i = 0; i < THREADS; i++)
341                  p.execute(new CheckedRunnable() {
# Line 302 | Line 344 | public class ThreadPoolExecutorTest exte
344                          done.await();
345                          assertEquals(THREADS, p.getLargestPoolSize());
346                      }});
347 <            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
306 <            assertEquals(THREADS, p.getLargestPoolSize());
307 <        } finally {
308 <            done.countDown();
309 <            joinPool(p);
347 >            assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
348              assertEquals(THREADS, p.getLargestPoolSize());
349 +            done.countDown();   // release pool
350          }
351 +        assertEquals(THREADS, p.getLargestPoolSize());
352      }
353  
354      /**
# Line 320 | Line 360 | public class ThreadPoolExecutorTest exte
360              new ThreadPoolExecutor(2, 3,
361                                     LONG_DELAY_MS, MILLISECONDS,
362                                     new ArrayBlockingQueue<Runnable>(10));
363 <        assertEquals(3, p.getMaximumPoolSize());
364 <        joinPool(p);
363 >        try (PoolCleaner cleaner = cleaner(p)) {
364 >            assertEquals(3, p.getMaximumPoolSize());
365 >            p.setMaximumPoolSize(5);
366 >            assertEquals(5, p.getMaximumPoolSize());
367 >            p.setMaximumPoolSize(4);
368 >            assertEquals(4, p.getMaximumPoolSize());
369 >        }
370      }
371  
372      /**
# Line 333 | Line 378 | public class ThreadPoolExecutorTest exte
378              new ThreadPoolExecutor(1, 1,
379                                     LONG_DELAY_MS, MILLISECONDS,
380                                     new ArrayBlockingQueue<Runnable>(10));
381 <        final CountDownLatch threadStarted = new CountDownLatch(1);
382 <        final CountDownLatch done = new CountDownLatch(1);
383 <        try {
381 >        try (PoolCleaner cleaner = cleaner(p)) {
382 >            final CountDownLatch threadStarted = new CountDownLatch(1);
383 >            final CountDownLatch done = new CountDownLatch(1);
384              assertEquals(0, p.getPoolSize());
385              p.execute(new CheckedRunnable() {
386                  public void realRun() throws InterruptedException {
# Line 343 | Line 388 | public class ThreadPoolExecutorTest exte
388                      assertEquals(1, p.getPoolSize());
389                      done.await();
390                  }});
391 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
391 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
392              assertEquals(1, p.getPoolSize());
393 <        } finally {
349 <            done.countDown();
350 <            joinPool(p);
393 >            done.countDown();   // release pool
394          }
395      }
396  
# Line 359 | Line 402 | public class ThreadPoolExecutorTest exte
402              new ThreadPoolExecutor(1, 1,
403                                     LONG_DELAY_MS, MILLISECONDS,
404                                     new ArrayBlockingQueue<Runnable>(10));
405 <        final CountDownLatch threadStarted = new CountDownLatch(1);
406 <        final CountDownLatch done = new CountDownLatch(1);
407 <        try {
405 >        try (PoolCleaner cleaner = cleaner(p)) {
406 >            final CountDownLatch threadStarted = new CountDownLatch(1);
407 >            final CountDownLatch done = new CountDownLatch(1);
408              assertEquals(0, p.getTaskCount());
409              p.execute(new CheckedRunnable() {
410                  public void realRun() throws InterruptedException {
# Line 369 | Line 412 | public class ThreadPoolExecutorTest exte
412                      assertEquals(1, p.getTaskCount());
413                      done.await();
414                  }});
415 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
415 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
416              assertEquals(1, p.getTaskCount());
374        } finally {
417              done.countDown();
376            joinPool(p);
418          }
419      }
420  
421      /**
422 <     * isShutDown is false before shutdown, true after
422 >     * isShutdown is false before shutdown, true after
423       */
424      public void testIsShutdown() {
425          final ThreadPoolExecutor p =
# Line 391 | Line 432 | public class ThreadPoolExecutorTest exte
432          joinPool(p);
433      }
434  
435 +    /**
436 +     * awaitTermination on a non-shutdown pool times out
437 +     */
438 +    public void testAwaitTermination_timesOut() throws InterruptedException {
439 +        final ThreadPoolExecutor p =
440 +            new ThreadPoolExecutor(1, 1,
441 +                                   LONG_DELAY_MS, MILLISECONDS,
442 +                                   new ArrayBlockingQueue<Runnable>(10));
443 +        assertFalse(p.isTerminated());
444 +        assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
445 +        assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
446 +        assertFalse(p.awaitTermination(-1L, NANOSECONDS));
447 +        assertFalse(p.awaitTermination(-1L, MILLISECONDS));
448 +        assertFalse(p.awaitTermination(0L, NANOSECONDS));
449 +        assertFalse(p.awaitTermination(0L, MILLISECONDS));
450 +        long timeoutNanos = 999999L;
451 +        long startTime = System.nanoTime();
452 +        assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
453 +        assertTrue(System.nanoTime() - startTime >= timeoutNanos);
454 +        assertFalse(p.isTerminated());
455 +        startTime = System.nanoTime();
456 +        long timeoutMillis = timeoutMillis();
457 +        assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
458 +        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
459 +        assertFalse(p.isTerminated());
460 +        p.shutdown();
461 +        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
462 +        assertTrue(p.isTerminated());
463 +    }
464  
465      /**
466       * isTerminated is false before termination, true after
# Line 410 | Line 480 | public class ThreadPoolExecutorTest exte
480                      threadStarted.countDown();
481                      done.await();
482                  }});
483 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
483 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
484              assertFalse(p.isTerminating());
485              done.countDown();
486          } finally {
# Line 438 | Line 508 | public class ThreadPoolExecutorTest exte
508                      threadStarted.countDown();
509                      done.await();
510                  }});
511 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
511 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
512              assertFalse(p.isTerminating());
513              done.countDown();
514          } finally {
# Line 473 | Line 543 | public class ThreadPoolExecutorTest exte
543                  tasks[i] = new FutureTask(task);
544                  p.execute(tasks[i]);
545              }
546 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
546 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
547              assertSame(q, p.getQueue());
548              assertFalse(q.contains(tasks[0]));
549              assertTrue(q.contains(tasks[tasks.length - 1]));
# Line 505 | Line 575 | public class ThreadPoolExecutorTest exte
575                      }};
576                  p.execute(tasks[i]);
577              }
578 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
578 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
579              assertFalse(p.remove(tasks[0]));
580              assertTrue(q.contains(tasks[4]));
581              assertTrue(q.contains(tasks[3]));
# Line 544 | Line 614 | public class ThreadPoolExecutorTest exte
614                  tasks[i] = new FutureTask(task);
615                  p.execute(tasks[i]);
616              }
617 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
617 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
618              assertEquals(tasks.length, p.getTaskCount());
619              assertEquals(tasks.length - 1, q.size());
620              assertEquals(1L, p.getActiveCount());
# Line 564 | Line 634 | public class ThreadPoolExecutorTest exte
634      }
635  
636      /**
637 <     * shutDownNow returns a list containing tasks that were not run
637 >     * shutdownNow returns a list containing tasks that were not run,
638 >     * and those tasks are drained from the queue
639       */
640 <    public void testShutDownNow() {
640 >    public void testShutdownNow() throws InterruptedException {
641 >        final int poolSize = 2;
642 >        final int count = 5;
643 >        final AtomicInteger ran = new AtomicInteger(0);
644          final ThreadPoolExecutor p =
645 <            new ThreadPoolExecutor(1, 1,
645 >            new ThreadPoolExecutor(poolSize, poolSize,
646                                     LONG_DELAY_MS, MILLISECONDS,
647                                     new ArrayBlockingQueue<Runnable>(10));
648 <        List l;
649 <        try {
650 <            for (int i = 0; i < 5; i++)
577 <                p.execute(new MediumPossiblyInterruptedRunnable());
578 <        }
579 <        finally {
648 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
649 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
650 >            threadsStarted.countDown();
651              try {
652 <                l = p.shutdownNow();
653 <            } catch (SecurityException ok) { return; }
652 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
653 >            } catch (InterruptedException success) {}
654 >            ran.getAndIncrement();
655 >        }};
656 >        for (int i = 0; i < count; i++)
657 >            p.execute(waiter);
658 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
659 >        assertEquals(poolSize, p.getActiveCount());
660 >        assertEquals(0, p.getCompletedTaskCount());
661 >        final List<Runnable> queuedTasks;
662 >        try {
663 >            queuedTasks = p.shutdownNow();
664 >        } catch (SecurityException ok) {
665 >            return; // Allowed in case test doesn't have privs
666          }
667          assertTrue(p.isShutdown());
668 <        assertTrue(l.size() <= 4);
668 >        assertTrue(p.getQueue().isEmpty());
669 >        assertEquals(count - poolSize, queuedTasks.size());
670 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
671 >        assertTrue(p.isTerminated());
672 >        assertEquals(poolSize, ran.get());
673 >        assertEquals(poolSize, p.getCompletedTaskCount());
674      }
675  
676      // Exception Tests
677  
590
678      /**
679       * Constructor throws if corePoolSize argument is less than zero
680       */
681      public void testConstructor1() {
682          try {
683 <            new ThreadPoolExecutor(-1, 1,
597 <                                   LONG_DELAY_MS, MILLISECONDS,
683 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
684                                     new ArrayBlockingQueue<Runnable>(10));
685              shouldThrow();
686          } catch (IllegalArgumentException success) {}
# Line 605 | Line 691 | public class ThreadPoolExecutorTest exte
691       */
692      public void testConstructor2() {
693          try {
694 <            new ThreadPoolExecutor(1, -1,
609 <                                   LONG_DELAY_MS, MILLISECONDS,
694 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
695                                     new ArrayBlockingQueue<Runnable>(10));
696              shouldThrow();
697          } catch (IllegalArgumentException success) {}
# Line 617 | Line 702 | public class ThreadPoolExecutorTest exte
702       */
703      public void testConstructor3() {
704          try {
705 <            new ThreadPoolExecutor(1, 0,
621 <                                   LONG_DELAY_MS, MILLISECONDS,
705 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
706                                     new ArrayBlockingQueue<Runnable>(10));
707              shouldThrow();
708          } catch (IllegalArgumentException success) {}
# Line 629 | Line 713 | public class ThreadPoolExecutorTest exte
713       */
714      public void testConstructor4() {
715          try {
716 <            new ThreadPoolExecutor(1, 2,
633 <                                   -1L, MILLISECONDS,
716 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
717                                     new ArrayBlockingQueue<Runnable>(10));
718              shouldThrow();
719          } catch (IllegalArgumentException success) {}
# Line 641 | Line 724 | public class ThreadPoolExecutorTest exte
724       */
725      public void testConstructor5() {
726          try {
727 <            new ThreadPoolExecutor(2, 1,
645 <                                   LONG_DELAY_MS, MILLISECONDS,
727 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
728                                     new ArrayBlockingQueue<Runnable>(10));
729              shouldThrow();
730          } catch (IllegalArgumentException success) {}
# Line 653 | Line 735 | public class ThreadPoolExecutorTest exte
735       */
736      public void testConstructorNullPointerException() {
737          try {
738 <            new ThreadPoolExecutor(1, 2,
657 <                                   LONG_DELAY_MS, MILLISECONDS,
738 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
739                                     (BlockingQueue) null);
740              shouldThrow();
741          } catch (NullPointerException success) {}
742      }
743  
663
664
744      /**
745       * Constructor throws if corePoolSize argument is less than zero
746       */
747      public void testConstructor6() {
748          try {
749 <            new ThreadPoolExecutor(-1, 1,
671 <                                   LONG_DELAY_MS, MILLISECONDS,
749 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
750                                     new ArrayBlockingQueue<Runnable>(10),
751                                     new SimpleThreadFactory());
752              shouldThrow();
# Line 680 | Line 758 | public class ThreadPoolExecutorTest exte
758       */
759      public void testConstructor7() {
760          try {
761 <            new ThreadPoolExecutor(1, -1,
684 <                                   LONG_DELAY_MS, MILLISECONDS,
761 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
762                                     new ArrayBlockingQueue<Runnable>(10),
763                                     new SimpleThreadFactory());
764              shouldThrow();
# Line 693 | Line 770 | public class ThreadPoolExecutorTest exte
770       */
771      public void testConstructor8() {
772          try {
773 <            new ThreadPoolExecutor(1, 0,
697 <                                   LONG_DELAY_MS, MILLISECONDS,
773 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
774                                     new ArrayBlockingQueue<Runnable>(10),
775                                     new SimpleThreadFactory());
776              shouldThrow();
# Line 706 | Line 782 | public class ThreadPoolExecutorTest exte
782       */
783      public void testConstructor9() {
784          try {
785 <            new ThreadPoolExecutor(1, 2,
710 <                                   -1L, MILLISECONDS,
785 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
786                                     new ArrayBlockingQueue<Runnable>(10),
787                                     new SimpleThreadFactory());
788              shouldThrow();
# Line 719 | Line 794 | public class ThreadPoolExecutorTest exte
794       */
795      public void testConstructor10() {
796          try {
797 <            new ThreadPoolExecutor(2, 1,
723 <                                   LONG_DELAY_MS, MILLISECONDS,
797 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
798                                     new ArrayBlockingQueue<Runnable>(10),
799                                     new SimpleThreadFactory());
800              shouldThrow();
# Line 732 | Line 806 | public class ThreadPoolExecutorTest exte
806       */
807      public void testConstructorNullPointerException2() {
808          try {
809 <            new ThreadPoolExecutor(1, 2,
736 <                                   LONG_DELAY_MS, MILLISECONDS,
809 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
810                                     (BlockingQueue) null,
811                                     new SimpleThreadFactory());
812              shouldThrow();
# Line 745 | Line 818 | public class ThreadPoolExecutorTest exte
818       */
819      public void testConstructorNullPointerException3() {
820          try {
821 <            new ThreadPoolExecutor(1, 2,
749 <                                   LONG_DELAY_MS, MILLISECONDS,
821 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
822                                     new ArrayBlockingQueue<Runnable>(10),
823                                     (ThreadFactory) null);
824              shouldThrow();
825          } catch (NullPointerException success) {}
826      }
827  
756
828      /**
829       * Constructor throws if corePoolSize argument is less than zero
830       */
831      public void testConstructor11() {
832          try {
833 <            new ThreadPoolExecutor(-1, 1,
763 <                                   LONG_DELAY_MS, MILLISECONDS,
833 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
834                                     new ArrayBlockingQueue<Runnable>(10),
835                                     new NoOpREHandler());
836              shouldThrow();
# Line 772 | Line 842 | public class ThreadPoolExecutorTest exte
842       */
843      public void testConstructor12() {
844          try {
845 <            new ThreadPoolExecutor(1, -1,
776 <                                   LONG_DELAY_MS, MILLISECONDS,
845 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
846                                     new ArrayBlockingQueue<Runnable>(10),
847                                     new NoOpREHandler());
848              shouldThrow();
# Line 785 | Line 854 | public class ThreadPoolExecutorTest exte
854       */
855      public void testConstructor13() {
856          try {
857 <            new ThreadPoolExecutor(1, 0,
789 <                                   LONG_DELAY_MS, MILLISECONDS,
857 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
858                                     new ArrayBlockingQueue<Runnable>(10),
859                                     new NoOpREHandler());
860              shouldThrow();
# Line 798 | Line 866 | public class ThreadPoolExecutorTest exte
866       */
867      public void testConstructor14() {
868          try {
869 <            new ThreadPoolExecutor(1, 2,
802 <                                   -1L, MILLISECONDS,
869 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
870                                     new ArrayBlockingQueue<Runnable>(10),
871                                     new NoOpREHandler());
872              shouldThrow();
# Line 811 | Line 878 | public class ThreadPoolExecutorTest exte
878       */
879      public void testConstructor15() {
880          try {
881 <            new ThreadPoolExecutor(2, 1,
815 <                                   LONG_DELAY_MS, MILLISECONDS,
881 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
882                                     new ArrayBlockingQueue<Runnable>(10),
883                                     new NoOpREHandler());
884              shouldThrow();
# Line 824 | Line 890 | public class ThreadPoolExecutorTest exte
890       */
891      public void testConstructorNullPointerException4() {
892          try {
893 <            new ThreadPoolExecutor(1, 2,
828 <                                   LONG_DELAY_MS, MILLISECONDS,
893 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
894                                     (BlockingQueue) null,
895                                     new NoOpREHandler());
896              shouldThrow();
# Line 837 | Line 902 | public class ThreadPoolExecutorTest exte
902       */
903      public void testConstructorNullPointerException5() {
904          try {
905 <            new ThreadPoolExecutor(1, 2,
841 <                                   LONG_DELAY_MS, MILLISECONDS,
905 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
906                                     new ArrayBlockingQueue<Runnable>(10),
907                                     (RejectedExecutionHandler) null);
908              shouldThrow();
909          } catch (NullPointerException success) {}
910      }
911  
848
912      /**
913       * Constructor throws if corePoolSize argument is less than zero
914       */
915      public void testConstructor16() {
916          try {
917 <            new ThreadPoolExecutor(-1, 1,
855 <                                   LONG_DELAY_MS, MILLISECONDS,
917 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
918                                     new ArrayBlockingQueue<Runnable>(10),
919                                     new SimpleThreadFactory(),
920                                     new NoOpREHandler());
# Line 865 | Line 927 | public class ThreadPoolExecutorTest exte
927       */
928      public void testConstructor17() {
929          try {
930 <            new ThreadPoolExecutor(1, -1,
869 <                                   LONG_DELAY_MS, MILLISECONDS,
930 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
931                                     new ArrayBlockingQueue<Runnable>(10),
932                                     new SimpleThreadFactory(),
933                                     new NoOpREHandler());
# Line 879 | Line 940 | public class ThreadPoolExecutorTest exte
940       */
941      public void testConstructor18() {
942          try {
943 <            new ThreadPoolExecutor(1, 0,
883 <                                   LONG_DELAY_MS, MILLISECONDS,
943 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
944                                     new ArrayBlockingQueue<Runnable>(10),
945                                     new SimpleThreadFactory(),
946                                     new NoOpREHandler());
# Line 893 | Line 953 | public class ThreadPoolExecutorTest exte
953       */
954      public void testConstructor19() {
955          try {
956 <            new ThreadPoolExecutor(1, 2,
897 <                                   -1L, MILLISECONDS,
956 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
957                                     new ArrayBlockingQueue<Runnable>(10),
958                                     new SimpleThreadFactory(),
959                                     new NoOpREHandler());
# Line 907 | Line 966 | public class ThreadPoolExecutorTest exte
966       */
967      public void testConstructor20() {
968          try {
969 <            new ThreadPoolExecutor(2, 1,
911 <                                   LONG_DELAY_MS, MILLISECONDS,
969 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
970                                     new ArrayBlockingQueue<Runnable>(10),
971                                     new SimpleThreadFactory(),
972                                     new NoOpREHandler());
# Line 921 | Line 979 | public class ThreadPoolExecutorTest exte
979       */
980      public void testConstructorNullPointerException6() {
981          try {
982 <            new ThreadPoolExecutor(1, 2,
925 <                                   LONG_DELAY_MS, MILLISECONDS,
982 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
983                                     (BlockingQueue) null,
984                                     new SimpleThreadFactory(),
985                                     new NoOpREHandler());
# Line 935 | Line 992 | public class ThreadPoolExecutorTest exte
992       */
993      public void testConstructorNullPointerException7() {
994          try {
995 <            new ThreadPoolExecutor(1, 2,
939 <                                   LONG_DELAY_MS, MILLISECONDS,
995 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
996                                     new ArrayBlockingQueue<Runnable>(10),
997                                     new SimpleThreadFactory(),
998                                     (RejectedExecutionHandler) null);
# Line 949 | Line 1005 | public class ThreadPoolExecutorTest exte
1005       */
1006      public void testConstructorNullPointerException8() {
1007          try {
1008 <            new ThreadPoolExecutor(1, 2,
953 <                                   LONG_DELAY_MS, MILLISECONDS,
1008 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1009                                     new ArrayBlockingQueue<Runnable>(10),
1010                                     (ThreadFactory) null,
1011                                     new NoOpREHandler());
# Line 964 | Line 1019 | public class ThreadPoolExecutorTest exte
1019      public void testInterruptedSubmit() throws InterruptedException {
1020          final ThreadPoolExecutor p =
1021              new ThreadPoolExecutor(1, 1,
1022 <                                   60, TimeUnit.SECONDS,
1022 >                                   60, SECONDS,
1023                                     new ArrayBlockingQueue<Runnable>(10));
1024  
1025          final CountDownLatch threadStarted = new CountDownLatch(1);
# Line 981 | Line 1036 | public class ThreadPoolExecutorTest exte
1036                      p.submit(task).get();
1037                  }});
1038  
1039 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1039 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
1040              t.interrupt();
1041              awaitTermination(t, MEDIUM_DELAY_MS);
1042          } finally {
# Line 1212 | Line 1267 | public class ThreadPoolExecutorTest exte
1267          }
1268      }
1269  
1215
1270      /**
1271       * execute using DiscardOldestPolicy drops task on shutdown
1272       */
# Line 1234 | Line 1288 | public class ThreadPoolExecutorTest exte
1288          }
1289      }
1290  
1237
1291      /**
1292       * execute(null) throws NPE
1293       */
1294      public void testExecuteNull() {
1295          ThreadPoolExecutor p =
1296 <            new ThreadPoolExecutor(1, 2,
1244 <                                   LONG_DELAY_MS, MILLISECONDS,
1296 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1297                                     new ArrayBlockingQueue<Runnable>(10));
1298          try {
1299              p.execute(null);
# Line 1307 | Line 1359 | public class ThreadPoolExecutorTest exte
1359          joinPool(p);
1360      }
1361  
1362 +    /**
1363 +     * Configuration changes that allow core pool size greater than
1364 +     * max pool size result in IllegalArgumentException.
1365 +     */
1366 +    public void testPoolSizeInvariants() {
1367 +        ThreadPoolExecutor p =
1368 +            new ThreadPoolExecutor(1, 1,
1369 +                                   LONG_DELAY_MS, MILLISECONDS,
1370 +                                   new ArrayBlockingQueue<Runnable>(10));
1371 +        for (int s = 1; s < 5; s++) {
1372 +            p.setMaximumPoolSize(s);
1373 +            p.setCorePoolSize(s);
1374 +            try {
1375 +                p.setMaximumPoolSize(s - 1);
1376 +                shouldThrow();
1377 +            } catch (IllegalArgumentException success) {}
1378 +            assertEquals(s, p.getCorePoolSize());
1379 +            assertEquals(s, p.getMaximumPoolSize());
1380 +            try {
1381 +                p.setCorePoolSize(s + 1);
1382 +                shouldThrow();
1383 +            } catch (IllegalArgumentException success) {}
1384 +            assertEquals(s, p.getCorePoolSize());
1385 +            assertEquals(s, p.getMaximumPoolSize());
1386 +        }
1387 +        joinPool(p);
1388 +    }
1389  
1390      /**
1391       * setKeepAliveTime throws IllegalArgumentException
# Line 1333 | Line 1412 | public class ThreadPoolExecutorTest exte
1412      public void testTerminated() {
1413          ExtendedTPE p = new ExtendedTPE();
1414          try { p.shutdown(); } catch (SecurityException ok) { return; }
1415 <        assertTrue(p.terminatedCalled);
1415 >        assertTrue(p.terminatedCalled());
1416          joinPool(p);
1417      }
1418  
# Line 1343 | Line 1422 | public class ThreadPoolExecutorTest exte
1422      public void testBeforeAfter() throws InterruptedException {
1423          ExtendedTPE p = new ExtendedTPE();
1424          try {
1425 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1426 <            p.execute(r);
1427 <            delay(SHORT_DELAY_MS);
1428 <            assertTrue(r.done);
1429 <            assertTrue(p.beforeCalled);
1430 <            assertTrue(p.afterCalled);
1425 >            final CountDownLatch done = new CountDownLatch(1);
1426 >            p.execute(new CheckedRunnable() {
1427 >                public void realRun() {
1428 >                    done.countDown();
1429 >                }});
1430 >            await(p.afterCalled);
1431 >            assertEquals(0, done.getCount());
1432 >            assertTrue(p.afterCalled());
1433 >            assertTrue(p.beforeCalled());
1434              try { p.shutdown(); } catch (SecurityException ok) { return; }
1435          } finally {
1436              joinPool(p);
# Line 1406 | Line 1488 | public class ThreadPoolExecutorTest exte
1488          }
1489      }
1490  
1409
1491      /**
1492       * invokeAny(null) throws NPE
1493       */
# Line 1600 | Line 1681 | public class ThreadPoolExecutorTest exte
1681          }
1682      }
1683  
1603
1604
1684      /**
1685       * timed invokeAny(null) throws NPE
1686       */
# Line 1824 | Line 1903 | public class ThreadPoolExecutorTest exte
1903              l.add(new StringTask());
1904              l.add(new StringTask());
1905              List<Future<String>> futures =
1906 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1906 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1907              assertEquals(2, futures.size());
1908              for (Future<String> future : futures)
1909                  assertSame(TEST_STRING, future.get());
# Line 1842 | Line 1921 | public class ThreadPoolExecutorTest exte
1921                                     LONG_DELAY_MS, MILLISECONDS,
1922                                     new ArrayBlockingQueue<Runnable>(10));
1923          try {
1924 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1925 <            l.add(new StringTask());
1926 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1927 <            l.add(new StringTask());
1928 <            List<Future<String>> futures =
1929 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1930 <            assertEquals(3, futures.size());
1931 <            Iterator<Future<String>> it = futures.iterator();
1932 <            Future<String> f1 = it.next();
1933 <            Future<String> f2 = it.next();
1934 <            Future<String> f3 = it.next();
1935 <            assertTrue(f1.isDone());
1936 <            assertTrue(f2.isDone());
1937 <            assertTrue(f3.isDone());
1938 <            assertFalse(f1.isCancelled());
1939 <            assertTrue(f2.isCancelled());
1924 >            for (long timeout = timeoutMillis();;) {
1925 >                List<Callable<String>> tasks = new ArrayList<>();
1926 >                tasks.add(new StringTask("0"));
1927 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1928 >                tasks.add(new StringTask("2"));
1929 >                long startTime = System.nanoTime();
1930 >                List<Future<String>> futures =
1931 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1932 >                assertEquals(tasks.size(), futures.size());
1933 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1934 >                for (Future future : futures)
1935 >                    assertTrue(future.isDone());
1936 >                assertTrue(futures.get(1).isCancelled());
1937 >                try {
1938 >                    assertEquals("0", futures.get(0).get());
1939 >                    assertEquals("2", futures.get(2).get());
1940 >                    break;
1941 >                } catch (CancellationException retryWithLongerTimeout) {
1942 >                    timeout *= 2;
1943 >                    if (timeout >= LONG_DELAY_MS / 2)
1944 >                        fail("expected exactly one task to be cancelled");
1945 >                }
1946 >            }
1947          } finally {
1948              joinPool(e);
1949          }
# Line 1903 | Line 1989 | public class ThreadPoolExecutorTest exte
1989       * allowCoreThreadTimeOut(true) causes idle threads to time out
1990       */
1991      public void testAllowCoreThreadTimeOut_true() throws Exception {
1992 +        long keepAliveTime = timeoutMillis();
1993          final ThreadPoolExecutor p =
1994              new ThreadPoolExecutor(2, 10,
1995 <                                   SHORT_DELAY_MS, MILLISECONDS,
1995 >                                   keepAliveTime, MILLISECONDS,
1996                                     new ArrayBlockingQueue<Runnable>(10));
1997          final CountDownLatch threadStarted = new CountDownLatch(1);
1998          try {
1999              p.allowCoreThreadTimeOut(true);
2000              p.execute(new CheckedRunnable() {
2001 <                public void realRun() throws InterruptedException {
2001 >                public void realRun() {
2002                      threadStarted.countDown();
2003                      assertEquals(1, p.getPoolSize());
2004                  }});
2005 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
2006 <            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
2007 <                if (p.getPoolSize() == 0)
2008 <                    break;
2009 <                delay(10);
2010 <            }
2005 >            await(threadStarted);
2006 >            delay(keepAliveTime);
2007 >            long startTime = System.nanoTime();
2008 >            while (p.getPoolSize() > 0
2009 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
2010 >                Thread.yield();
2011 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
2012              assertEquals(0, p.getPoolSize());
2013          } finally {
2014              joinPool(p);
# Line 1931 | Line 2019 | public class ThreadPoolExecutorTest exte
2019       * allowCoreThreadTimeOut(false) causes idle threads not to time out
2020       */
2021      public void testAllowCoreThreadTimeOut_false() throws Exception {
2022 +        long keepAliveTime = timeoutMillis();
2023          final ThreadPoolExecutor p =
2024              new ThreadPoolExecutor(2, 10,
2025 <                                   SHORT_DELAY_MS, MILLISECONDS,
2025 >                                   keepAliveTime, MILLISECONDS,
2026                                     new ArrayBlockingQueue<Runnable>(10));
2027          final CountDownLatch threadStarted = new CountDownLatch(1);
2028          try {
# Line 1943 | Line 2032 | public class ThreadPoolExecutorTest exte
2032                      threadStarted.countDown();
2033                      assertTrue(p.getPoolSize() >= 1);
2034                  }});
2035 <            delay(SMALL_DELAY_MS);
2035 >            delay(2 * keepAliveTime);
2036              assertTrue(p.getPoolSize() >= 1);
2037          } finally {
2038              joinPool(p);
# Line 1962 | Line 2051 | public class ThreadPoolExecutorTest exte
2051                  done.countDown();
2052              }};
2053          final ThreadPoolExecutor p =
2054 <            new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
2054 >            new ThreadPoolExecutor(1, 30,
2055 >                                   60, SECONDS,
2056                                     new ArrayBlockingQueue(30));
2057          try {
2058              for (int i = 0; i < nTasks; ++i) {
# Line 1977 | Line 2067 | public class ThreadPoolExecutorTest exte
2067              // enough time to run all tasks
2068              assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
2069          } finally {
2070 <            p.shutdown();
2070 >            joinPool(p);
2071 >        }
2072 >    }
2073 >
2074 >    /**
2075 >     * get(cancelled task) throws CancellationException
2076 >     */
2077 >    public void testGet_cancelled() throws Exception {
2078 >        final ExecutorService e =
2079 >            new ThreadPoolExecutor(1, 1,
2080 >                                   LONG_DELAY_MS, MILLISECONDS,
2081 >                                   new LinkedBlockingQueue<Runnable>());
2082 >        try {
2083 >            final CountDownLatch blockerStarted = new CountDownLatch(1);
2084 >            final CountDownLatch done = new CountDownLatch(1);
2085 >            final List<Future<?>> futures = new ArrayList<>();
2086 >            for (int i = 0; i < 2; i++) {
2087 >                Runnable r = new CheckedRunnable() { public void realRun()
2088 >                                                         throws Throwable {
2089 >                    blockerStarted.countDown();
2090 >                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
2091 >                }};
2092 >                futures.add(e.submit(r));
2093 >            }
2094 >            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
2095 >            for (Future<?> future : futures) future.cancel(false);
2096 >            for (Future<?> future : futures) {
2097 >                try {
2098 >                    future.get();
2099 >                    shouldThrow();
2100 >                } catch (CancellationException success) {}
2101 >                try {
2102 >                    future.get(LONG_DELAY_MS, MILLISECONDS);
2103 >                    shouldThrow();
2104 >                } catch (CancellationException success) {}
2105 >                assertTrue(future.isCancelled());
2106 >                assertTrue(future.isDone());
2107 >            }
2108 >            done.countDown();
2109 >        } finally {
2110 >            joinPool(e);
2111          }
2112      }
2113  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines