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.28 by jsr166, Sat May 7 19:49:37 2011 UTC vs.
Revision 1.71 by jsr166, Sun Oct 4 02:34:48 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.locks.*;
10 > import static java.util.concurrent.TimeUnit.SECONDS;
11  
12 < import junit.framework.*;
13 < import java.util.*;
12 > import java.util.ArrayList;
13 > import java.util.List;
14 > import java.util.concurrent.ArrayBlockingQueue;
15 > import java.util.concurrent.BlockingQueue;
16 > import java.util.concurrent.Callable;
17 > import java.util.concurrent.CancellationException;
18 > import java.util.concurrent.CountDownLatch;
19 > import java.util.concurrent.ExecutionException;
20 > import java.util.concurrent.Executors;
21 > import java.util.concurrent.ExecutorService;
22 > import java.util.concurrent.Future;
23 > import java.util.concurrent.FutureTask;
24 > import java.util.concurrent.LinkedBlockingQueue;
25 > import java.util.concurrent.RejectedExecutionException;
26 > import java.util.concurrent.RejectedExecutionHandler;
27 > import java.util.concurrent.RunnableFuture;
28 > import java.util.concurrent.SynchronousQueue;
29 > import java.util.concurrent.ThreadFactory;
30 > import java.util.concurrent.ThreadPoolExecutor;
31 > import java.util.concurrent.TimeoutException;
32 > import java.util.concurrent.TimeUnit;
33 > import java.util.concurrent.atomic.AtomicInteger;
34 > import java.util.concurrent.locks.Condition;
35 > import java.util.concurrent.locks.ReentrantLock;
36 >
37 > import junit.framework.Test;
38 > import junit.framework.TestSuite;
39  
40   public class ThreadPoolExecutorSubclassTest extends JSR166TestCase {
41      public static void main(String[] args) {
42 <        junit.textui.TestRunner.run(suite());
42 >        main(suite(), args);
43      }
44      public static Test suite() {
45          return new TestSuite(ThreadPoolExecutorSubclassTest.class);
# Line 37 | Line 61 | public class ThreadPoolExecutorSubclassT
61          CustomTask(final Runnable r, final V res) {
62              if (r == null) throw new NullPointerException();
63              callable = new Callable<V>() {
64 <            public V call() throws Exception { r.run(); return res; }};
64 >                public V call() throws Exception { r.run(); return res; }};
65          }
66          public boolean isDone() {
67              lock.lock(); try { return done; } finally { lock.unlock() ; }
# Line 77 | Line 101 | public class ThreadPoolExecutorSubclassT
101              }
102              lock.lock();
103              try {
104 <                result = v;
105 <                exception = e;
106 <                done = true;
107 <                thread = null;
108 <                cond.signalAll();
104 >                if (!done) {
105 >                    result = v;
106 >                    exception = e;
107 >                    done = true;
108 >                    thread = null;
109 >                    cond.signalAll();
110 >                }
111              }
112              finally { lock.unlock(); }
113          }
# Line 90 | Line 116 | public class ThreadPoolExecutorSubclassT
116              try {
117                  while (!done)
118                      cond.await();
119 +                if (cancelled)
120 +                    throw new CancellationException();
121                  if (exception != null)
122                      throw new ExecutionException(exception);
123                  return result;
# Line 101 | Line 129 | public class ThreadPoolExecutorSubclassT
129              long nanos = unit.toNanos(timeout);
130              lock.lock();
131              try {
132 <                for (;;) {
133 <                    if (done) break;
106 <                    if (nanos < 0)
132 >                while (!done) {
133 >                    if (nanos <= 0L)
134                          throw new TimeoutException();
135                      nanos = cond.awaitNanos(nanos);
136                  }
137 +                if (cancelled)
138 +                    throw new CancellationException();
139                  if (exception != null)
140                      throw new ExecutionException(exception);
141                  return result;
# Line 115 | Line 144 | public class ThreadPoolExecutorSubclassT
144          }
145      }
146  
118
147      static class CustomTPE extends ThreadPoolExecutor {
148          protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
149              return new CustomTask<V>(c);
# Line 162 | Line 190 | public class ThreadPoolExecutorSubclassT
190                workQueue, threadFactory, handler);
191          }
192  
193 <        volatile boolean beforeCalled = false;
194 <        volatile boolean afterCalled = false;
195 <        volatile boolean terminatedCalled = false;
193 >        final CountDownLatch beforeCalled = new CountDownLatch(1);
194 >        final CountDownLatch afterCalled = new CountDownLatch(1);
195 >        final CountDownLatch terminatedCalled = new CountDownLatch(1);
196 >
197          public CustomTPE() {
198              super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
199          }
200          protected void beforeExecute(Thread t, Runnable r) {
201 <            beforeCalled = true;
201 >            beforeCalled.countDown();
202          }
203          protected void afterExecute(Runnable r, Throwable t) {
204 <            afterCalled = true;
204 >            afterCalled.countDown();
205          }
206          protected void terminated() {
207 <            terminatedCalled = true;
207 >            terminatedCalled.countDown();
208          }
209  
210 +        public boolean beforeCalled() {
211 +            return beforeCalled.getCount() == 0;
212 +        }
213 +        public boolean afterCalled() {
214 +            return afterCalled.getCount() == 0;
215 +        }
216 +        public boolean terminatedCalled() {
217 +            return terminatedCalled.getCount() == 0;
218 +        }
219      }
220  
221      static class FailingThreadFactory implements ThreadFactory {
# Line 188 | Line 226 | public class ThreadPoolExecutorSubclassT
226          }
227      }
228  
191
229      /**
230       * execute successfully executes a runnable
231       */
232      public void testExecute() throws InterruptedException {
233          final ThreadPoolExecutor p =
234              new CustomTPE(1, 1,
235 <                          LONG_DELAY_MS, MILLISECONDS,
235 >                          2 * LONG_DELAY_MS, MILLISECONDS,
236                            new ArrayBlockingQueue<Runnable>(10));
237 <        final CountDownLatch done = new CountDownLatch(1);
238 <        final Runnable task = new CheckedRunnable() {
239 <            public void realRun() {
240 <                done.countDown();
204 <            }};
205 <        try {
237 >        try (PoolCleaner cleaner = cleaner(p)) {
238 >            final CountDownLatch done = new CountDownLatch(1);
239 >            final Runnable task = new CheckedRunnable() {
240 >                public void realRun() { done.countDown(); }};
241              p.execute(task);
242 <            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
208 <        } finally {
209 <            joinPool(p);
242 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
243          }
244      }
245  
# Line 219 | Line 252 | public class ThreadPoolExecutorSubclassT
252              new CustomTPE(2, 2,
253                            LONG_DELAY_MS, MILLISECONDS,
254                            new ArrayBlockingQueue<Runnable>(10));
255 <        final CountDownLatch threadStarted = new CountDownLatch(1);
256 <        final CountDownLatch done = new CountDownLatch(1);
257 <        try {
255 >        try (PoolCleaner cleaner = cleaner(p)) {
256 >            final CountDownLatch threadStarted = new CountDownLatch(1);
257 >            final CountDownLatch done = new CountDownLatch(1);
258              assertEquals(0, p.getActiveCount());
259              p.execute(new CheckedRunnable() {
260                  public void realRun() throws InterruptedException {
# Line 229 | Line 262 | public class ThreadPoolExecutorSubclassT
262                      assertEquals(1, p.getActiveCount());
263                      done.await();
264                  }});
265 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
265 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
266              assertEquals(1, p.getActiveCount());
234        } finally {
267              done.countDown();
236            joinPool(p);
268          }
269      }
270  
# Line 241 | Line 272 | public class ThreadPoolExecutorSubclassT
272       * prestartCoreThread starts a thread if under corePoolSize, else doesn't
273       */
274      public void testPrestartCoreThread() {
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);
275 >        ThreadPoolExecutor p =
276 >            new CustomTPE(2, 6,
277 >                          LONG_DELAY_MS, MILLISECONDS,
278 >                          new ArrayBlockingQueue<Runnable>(10));
279 >        try (PoolCleaner cleaner = cleaner(p)) {
280 >            assertEquals(0, p.getPoolSize());
281 >            assertTrue(p.prestartCoreThread());
282 >            assertEquals(1, p.getPoolSize());
283 >            assertTrue(p.prestartCoreThread());
284 >            assertEquals(2, p.getPoolSize());
285 >            assertFalse(p.prestartCoreThread());
286 >            assertEquals(2, p.getPoolSize());
287 >            p.setCorePoolSize(4);
288 >            assertTrue(p.prestartCoreThread());
289 >            assertEquals(3, p.getPoolSize());
290 >            assertTrue(p.prestartCoreThread());
291 >            assertEquals(4, p.getPoolSize());
292 >            assertFalse(p.prestartCoreThread());
293 >            assertEquals(4, p.getPoolSize());
294 >        }
295      }
296  
297      /**
298       * prestartAllCoreThreads starts all corePoolSize threads
299       */
300      public void testPrestartAllCoreThreads() {
301 <        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
302 <        assertEquals(0, p.getPoolSize());
303 <        p.prestartAllCoreThreads();
304 <        assertEquals(2, p.getPoolSize());
305 <        p.prestartAllCoreThreads();
306 <        assertEquals(2, p.getPoolSize());
307 <        joinPool(p);
301 >        ThreadPoolExecutor p =
302 >            new CustomTPE(2, 6,
303 >                          LONG_DELAY_MS, MILLISECONDS,
304 >                          new ArrayBlockingQueue<Runnable>(10));
305 >        try (PoolCleaner cleaner = cleaner(p)) {
306 >            assertEquals(0, p.getPoolSize());
307 >            p.prestartAllCoreThreads();
308 >            assertEquals(2, p.getPoolSize());
309 >            p.prestartAllCoreThreads();
310 >            assertEquals(2, p.getPoolSize());
311 >            p.setCorePoolSize(4);
312 >            p.prestartAllCoreThreads();
313 >            assertEquals(4, p.getPoolSize());
314 >            p.prestartAllCoreThreads();
315 >            assertEquals(4, p.getPoolSize());
316 >        }
317      }
318  
319      /**
# Line 274 | Line 325 | public class ThreadPoolExecutorSubclassT
325              new CustomTPE(2, 2,
326                            LONG_DELAY_MS, MILLISECONDS,
327                            new ArrayBlockingQueue<Runnable>(10));
328 <        final CountDownLatch threadStarted = new CountDownLatch(1);
329 <        final CountDownLatch threadProceed = new CountDownLatch(1);
330 <        final CountDownLatch threadDone = new CountDownLatch(1);
331 <        try {
328 >        try (PoolCleaner cleaner = cleaner(p)) {
329 >            final CountDownLatch threadStarted = new CountDownLatch(1);
330 >            final CountDownLatch threadProceed = new CountDownLatch(1);
331 >            final CountDownLatch threadDone = new CountDownLatch(1);
332              assertEquals(0, p.getCompletedTaskCount());
333              p.execute(new CheckedRunnable() {
334                  public void realRun() throws InterruptedException {
# Line 286 | Line 337 | public class ThreadPoolExecutorSubclassT
337                      threadProceed.await();
338                      threadDone.countDown();
339                  }});
340 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
340 >            await(threadStarted);
341              assertEquals(0, p.getCompletedTaskCount());
342              threadProceed.countDown();
343              threadDone.await();
344 <            delay(SHORT_DELAY_MS);
345 <            assertEquals(1, p.getCompletedTaskCount());
346 <        } finally {
347 <            joinPool(p);
344 >            long startTime = System.nanoTime();
345 >            while (p.getCompletedTaskCount() != 1) {
346 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
347 >                    fail("timed out");
348 >                Thread.yield();
349 >            }
350          }
351      }
352  
# Line 301 | Line 354 | public class ThreadPoolExecutorSubclassT
354       * getCorePoolSize returns size given in constructor if not otherwise set
355       */
356      public void testGetCorePoolSize() {
357 <        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
358 <        assertEquals(1, p.getCorePoolSize());
359 <        joinPool(p);
357 >        final ThreadPoolExecutor p =
358 >            new CustomTPE(1, 1,
359 >                          LONG_DELAY_MS, MILLISECONDS,
360 >                          new ArrayBlockingQueue<Runnable>(10));
361 >        try (PoolCleaner cleaner = cleaner(p)) {
362 >            assertEquals(1, p.getCorePoolSize());
363 >        }
364      }
365  
366      /**
367       * getKeepAliveTime returns value given in constructor if not otherwise set
368       */
369      public void testGetKeepAliveTime() {
370 <        ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
371 <        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
372 <        joinPool(p);
370 >        final ThreadPoolExecutor p =
371 >            new CustomTPE(2, 2,
372 >                          1000, MILLISECONDS,
373 >                          new ArrayBlockingQueue<Runnable>(10));
374 >        try (PoolCleaner cleaner = cleaner(p)) {
375 >            assertEquals(1, p.getKeepAliveTime(SECONDS));
376 >        }
377      }
378  
318
379      /**
380       * getThreadFactory returns factory in constructor if not set
381       */
382      public void testGetThreadFactory() {
383 <        ThreadFactory tf = new SimpleThreadFactory();
384 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
385 <        assertSame(tf, p.getThreadFactory());
386 <        joinPool(p);
383 >        final ThreadFactory threadFactory = new SimpleThreadFactory();
384 >        final ThreadPoolExecutor p =
385 >            new CustomTPE(1, 2,
386 >                          LONG_DELAY_MS, MILLISECONDS,
387 >                          new ArrayBlockingQueue<Runnable>(10),
388 >                          threadFactory,
389 >                          new NoOpREHandler());
390 >        try (PoolCleaner cleaner = cleaner(p)) {
391 >            assertSame(threadFactory, p.getThreadFactory());
392 >        }
393      }
394  
395      /**
396       * setThreadFactory sets the thread factory returned by getThreadFactory
397       */
398      public void testSetThreadFactory() {
399 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
400 <        ThreadFactory tf = new SimpleThreadFactory();
401 <        p.setThreadFactory(tf);
402 <        assertSame(tf, p.getThreadFactory());
403 <        joinPool(p);
399 >        final ThreadPoolExecutor p =
400 >            new CustomTPE(1, 2,
401 >                          LONG_DELAY_MS, MILLISECONDS,
402 >                          new ArrayBlockingQueue<Runnable>(10));
403 >        try (PoolCleaner cleaner = cleaner(p)) {
404 >            ThreadFactory threadFactory = new SimpleThreadFactory();
405 >            p.setThreadFactory(threadFactory);
406 >            assertSame(threadFactory, p.getThreadFactory());
407 >        }
408      }
409  
340
410      /**
411       * setThreadFactory(null) throws NPE
412       */
413      public void testSetThreadFactoryNull() {
414 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
415 <        try {
416 <            p.setThreadFactory(null);
417 <            shouldThrow();
418 <        } catch (NullPointerException success) {
419 <        } finally {
420 <            joinPool(p);
414 >        final ThreadPoolExecutor p =
415 >            new CustomTPE(1, 2,
416 >                          LONG_DELAY_MS, MILLISECONDS,
417 >                          new ArrayBlockingQueue<Runnable>(10));
418 >        try (PoolCleaner cleaner = cleaner(p)) {
419 >            try {
420 >                p.setThreadFactory(null);
421 >                shouldThrow();
422 >            } catch (NullPointerException success) {}
423          }
424      }
425  
# Line 356 | Line 427 | public class ThreadPoolExecutorSubclassT
427       * getRejectedExecutionHandler returns handler in constructor if not set
428       */
429      public void testGetRejectedExecutionHandler() {
430 <        RejectedExecutionHandler h = new NoOpREHandler();
431 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
432 <        assertSame(h, p.getRejectedExecutionHandler());
433 <        joinPool(p);
430 >        final RejectedExecutionHandler handler = new NoOpREHandler();
431 >        final ThreadPoolExecutor p =
432 >            new CustomTPE(1, 2,
433 >                          LONG_DELAY_MS, MILLISECONDS,
434 >                          new ArrayBlockingQueue<Runnable>(10),
435 >                          handler);
436 >        try (PoolCleaner cleaner = cleaner(p)) {
437 >            assertSame(handler, p.getRejectedExecutionHandler());
438 >        }
439      }
440  
441      /**
# Line 367 | Line 443 | public class ThreadPoolExecutorSubclassT
443       * getRejectedExecutionHandler
444       */
445      public void testSetRejectedExecutionHandler() {
446 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
447 <        RejectedExecutionHandler h = new NoOpREHandler();
448 <        p.setRejectedExecutionHandler(h);
449 <        assertSame(h, p.getRejectedExecutionHandler());
450 <        joinPool(p);
446 >        final ThreadPoolExecutor p =
447 >            new CustomTPE(1, 2,
448 >                          LONG_DELAY_MS, MILLISECONDS,
449 >                          new ArrayBlockingQueue<Runnable>(10));
450 >        try (PoolCleaner cleaner = cleaner(p)) {
451 >            RejectedExecutionHandler handler = new NoOpREHandler();
452 >            p.setRejectedExecutionHandler(handler);
453 >            assertSame(handler, p.getRejectedExecutionHandler());
454 >        }
455      }
456  
377
457      /**
458       * setRejectedExecutionHandler(null) throws NPE
459       */
460      public void testSetRejectedExecutionHandlerNull() {
461 <        ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
462 <        try {
463 <            p.setRejectedExecutionHandler(null);
464 <            shouldThrow();
465 <        } catch (NullPointerException success) {
466 <        } finally {
467 <            joinPool(p);
461 >        final ThreadPoolExecutor p =
462 >            new CustomTPE(1, 2,
463 >                          LONG_DELAY_MS, MILLISECONDS,
464 >                          new ArrayBlockingQueue<Runnable>(10));
465 >        try (PoolCleaner cleaner = cleaner(p)) {
466 >            try {
467 >                p.setRejectedExecutionHandler(null);
468 >                shouldThrow();
469 >            } catch (NullPointerException success) {}
470          }
471      }
472  
392
473      /**
474       * getLargestPoolSize increases, but doesn't overestimate, when
475       * multiple threads active
# Line 400 | Line 480 | public class ThreadPoolExecutorSubclassT
480              new CustomTPE(THREADS, THREADS,
481                            LONG_DELAY_MS, MILLISECONDS,
482                            new ArrayBlockingQueue<Runnable>(10));
483 <        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
484 <        final CountDownLatch done = new CountDownLatch(1);
485 <        try {
483 >        try (PoolCleaner cleaner = cleaner(p)) {
484 >            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
485 >            final CountDownLatch done = new CountDownLatch(1);
486              assertEquals(0, p.getLargestPoolSize());
487              for (int i = 0; i < THREADS; i++)
488                  p.execute(new CheckedRunnable() {
# Line 411 | Line 491 | public class ThreadPoolExecutorSubclassT
491                          done.await();
492                          assertEquals(THREADS, p.getLargestPoolSize());
493                      }});
494 <            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
415 <            assertEquals(THREADS, p.getLargestPoolSize());
416 <        } finally {
417 <            done.countDown();
418 <            joinPool(p);
494 >            assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
495              assertEquals(THREADS, p.getLargestPoolSize());
496 +            done.countDown();   // release pool
497          }
498 +        assertEquals(THREADS, p.getLargestPoolSize());
499      }
500  
501      /**
# Line 425 | Line 503 | public class ThreadPoolExecutorSubclassT
503       * otherwise set
504       */
505      public void testGetMaximumPoolSize() {
506 <        ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
507 <        assertEquals(2, p.getMaximumPoolSize());
508 <        joinPool(p);
506 >        final ThreadPoolExecutor p =
507 >            new CustomTPE(2, 3,
508 >                          LONG_DELAY_MS, MILLISECONDS,
509 >                          new ArrayBlockingQueue<Runnable>(10));
510 >        try (PoolCleaner cleaner = cleaner(p)) {
511 >            assertEquals(3, p.getMaximumPoolSize());
512 >            p.setMaximumPoolSize(5);
513 >            assertEquals(5, p.getMaximumPoolSize());
514 >            p.setMaximumPoolSize(4);
515 >            assertEquals(4, p.getMaximumPoolSize());
516 >        }
517      }
518  
519      /**
# Line 439 | Line 525 | public class ThreadPoolExecutorSubclassT
525              new CustomTPE(1, 1,
526                            LONG_DELAY_MS, MILLISECONDS,
527                            new ArrayBlockingQueue<Runnable>(10));
528 <        final CountDownLatch threadStarted = new CountDownLatch(1);
529 <        final CountDownLatch done = new CountDownLatch(1);
530 <        try {
528 >        try (PoolCleaner cleaner = cleaner(p)) {
529 >            final CountDownLatch threadStarted = new CountDownLatch(1);
530 >            final CountDownLatch done = new CountDownLatch(1);
531              assertEquals(0, p.getPoolSize());
532              p.execute(new CheckedRunnable() {
533                  public void realRun() throws InterruptedException {
# Line 449 | Line 535 | public class ThreadPoolExecutorSubclassT
535                      assertEquals(1, p.getPoolSize());
536                      done.await();
537                  }});
538 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
538 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
539              assertEquals(1, p.getPoolSize());
540 <        } finally {
455 <            done.countDown();
456 <            joinPool(p);
540 >            done.countDown();   // release pool
541          }
542      }
543  
# Line 465 | Line 549 | public class ThreadPoolExecutorSubclassT
549              new CustomTPE(1, 1,
550                            LONG_DELAY_MS, MILLISECONDS,
551                            new ArrayBlockingQueue<Runnable>(10));
552 <        final CountDownLatch threadStarted = new CountDownLatch(1);
553 <        final CountDownLatch done = new CountDownLatch(1);
554 <        try {
552 >        try (PoolCleaner cleaner = cleaner(p)) {
553 >            final CountDownLatch threadStarted = new CountDownLatch(1);
554 >            final CountDownLatch done = new CountDownLatch(1);
555              assertEquals(0, p.getTaskCount());
556              p.execute(new CheckedRunnable() {
557                  public void realRun() throws InterruptedException {
# Line 475 | Line 559 | public class ThreadPoolExecutorSubclassT
559                      assertEquals(1, p.getTaskCount());
560                      done.await();
561                  }});
562 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
562 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
563              assertEquals(1, p.getTaskCount());
480        } finally {
564              done.countDown();
482            joinPool(p);
565          }
566      }
567  
# Line 487 | Line 569 | public class ThreadPoolExecutorSubclassT
569       * isShutdown is false before shutdown, true after
570       */
571      public void testIsShutdown() {
572 <
573 <        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
574 <        assertFalse(p.isShutdown());
575 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
576 <        assertTrue(p.isShutdown());
577 <        joinPool(p);
572 >        final ThreadPoolExecutor p =
573 >            new CustomTPE(1, 1,
574 >                          LONG_DELAY_MS, MILLISECONDS,
575 >                          new ArrayBlockingQueue<Runnable>(10));
576 >        try (PoolCleaner cleaner = cleaner(p)) {
577 >            assertFalse(p.isShutdown());
578 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
579 >            assertTrue(p.isShutdown());
580 >        }
581      }
582  
498
583      /**
584       * isTerminated is false before termination, true after
585       */
# Line 504 | Line 588 | public class ThreadPoolExecutorSubclassT
588              new CustomTPE(1, 1,
589                            LONG_DELAY_MS, MILLISECONDS,
590                            new ArrayBlockingQueue<Runnable>(10));
591 <        final CountDownLatch threadStarted = new CountDownLatch(1);
592 <        final CountDownLatch done = new CountDownLatch(1);
593 <        try {
591 >        try (PoolCleaner cleaner = cleaner(p)) {
592 >            final CountDownLatch threadStarted = new CountDownLatch(1);
593 >            final CountDownLatch done = new CountDownLatch(1);
594              assertFalse(p.isTerminating());
595              p.execute(new CheckedRunnable() {
596                  public void realRun() throws InterruptedException {
# Line 514 | Line 598 | public class ThreadPoolExecutorSubclassT
598                      threadStarted.countDown();
599                      done.await();
600                  }});
601 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
601 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
602              assertFalse(p.isTerminating());
603              done.countDown();
520        } finally {
604              try { p.shutdown(); } catch (SecurityException ok) { return; }
605 +            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
606 +            assertTrue(p.isTerminated());
607 +            assertFalse(p.isTerminating());
608          }
523        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
524        assertTrue(p.isTerminated());
525        assertFalse(p.isTerminating());
609      }
610  
611      /**
# Line 533 | Line 616 | public class ThreadPoolExecutorSubclassT
616              new CustomTPE(1, 1,
617                            LONG_DELAY_MS, MILLISECONDS,
618                            new ArrayBlockingQueue<Runnable>(10));
619 <        final CountDownLatch threadStarted = new CountDownLatch(1);
620 <        final CountDownLatch done = new CountDownLatch(1);
621 <        try {
619 >        try (PoolCleaner cleaner = cleaner(p)) {
620 >            final CountDownLatch threadStarted = new CountDownLatch(1);
621 >            final CountDownLatch done = new CountDownLatch(1);
622              assertFalse(p.isTerminating());
623              p.execute(new CheckedRunnable() {
624                  public void realRun() throws InterruptedException {
# Line 543 | Line 626 | public class ThreadPoolExecutorSubclassT
626                      threadStarted.countDown();
627                      done.await();
628                  }});
629 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
629 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
630              assertFalse(p.isTerminating());
631              done.countDown();
549        } finally {
632              try { p.shutdown(); } catch (SecurityException ok) { return; }
633 +            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
634 +            assertTrue(p.isTerminated());
635 +            assertFalse(p.isTerminating());
636          }
552        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
553        assertTrue(p.isTerminated());
554        assertFalse(p.isTerminating());
637      }
638  
639      /**
# Line 563 | Line 645 | public class ThreadPoolExecutorSubclassT
645              new CustomTPE(1, 1,
646                            LONG_DELAY_MS, MILLISECONDS,
647                            q);
648 <        final CountDownLatch threadStarted = new CountDownLatch(1);
649 <        final CountDownLatch done = new CountDownLatch(1);
650 <        try {
648 >        try (PoolCleaner cleaner = cleaner(p)) {
649 >            final CountDownLatch threadStarted = new CountDownLatch(1);
650 >            final CountDownLatch done = new CountDownLatch(1);
651              FutureTask[] tasks = new FutureTask[5];
652              for (int i = 0; i < tasks.length; i++) {
653                  Callable task = new CheckedCallable<Boolean>() {
# Line 578 | Line 660 | public class ThreadPoolExecutorSubclassT
660                  tasks[i] = new FutureTask(task);
661                  p.execute(tasks[i]);
662              }
663 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
663 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
664              assertSame(q, p.getQueue());
665              assertFalse(q.contains(tasks[0]));
666              assertTrue(q.contains(tasks[tasks.length - 1]));
667              assertEquals(tasks.length - 1, q.size());
586        } finally {
668              done.countDown();
588            joinPool(p);
669          }
670      }
671  
# Line 610 | Line 690 | public class ThreadPoolExecutorSubclassT
690                          }};
691                  p.execute(tasks[i]);
692              }
693 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
693 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
694              assertFalse(p.remove(tasks[0]));
695              assertTrue(q.contains(tasks[4]));
696              assertTrue(q.contains(tasks[3]));
# Line 649 | Line 729 | public class ThreadPoolExecutorSubclassT
729                  tasks[i] = new FutureTask(task);
730                  p.execute(tasks[i]);
731              }
732 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
732 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
733              assertEquals(tasks.length, p.getTaskCount());
734              assertEquals(tasks.length - 1, q.size());
735              assertEquals(1L, p.getActiveCount());
# Line 669 | Line 749 | public class ThreadPoolExecutorSubclassT
749      }
750  
751      /**
752 <     * shutdownNow returns a list containing tasks that were not run
752 >     * shutdownNow returns a list containing tasks that were not run,
753 >     * and those tasks are drained from the queue
754       */
755 <    public void testShutdownNow() {
756 <        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
757 <        List l;
758 <        try {
759 <            for (int i = 0; i < 5; i++)
760 <                p.execute(new MediumPossiblyInterruptedRunnable());
761 <        }
762 <        finally {
755 >    public void testShutdownNow() throws InterruptedException {
756 >        final int poolSize = 2;
757 >        final int count = 5;
758 >        final AtomicInteger ran = new AtomicInteger(0);
759 >        ThreadPoolExecutor p =
760 >            new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS,
761 >                          new ArrayBlockingQueue<Runnable>(10));
762 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
763 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
764 >            threadsStarted.countDown();
765              try {
766 <                l = p.shutdownNow();
767 <            } catch (SecurityException ok) { return; }
766 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
767 >            } catch (InterruptedException success) {}
768 >            ran.getAndIncrement();
769 >        }};
770 >        for (int i = 0; i < count; i++)
771 >            p.execute(waiter);
772 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
773 >        assertEquals(poolSize, p.getActiveCount());
774 >        assertEquals(0, p.getCompletedTaskCount());
775 >        final List<Runnable> queuedTasks;
776 >        try {
777 >            queuedTasks = p.shutdownNow();
778 >        } catch (SecurityException ok) {
779 >            return; // Allowed in case test doesn't have privs
780          }
781          assertTrue(p.isShutdown());
782 <        assertTrue(l.size() <= 4);
782 >        assertTrue(p.getQueue().isEmpty());
783 >        assertEquals(count - poolSize, queuedTasks.size());
784 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
785 >        assertTrue(p.isTerminated());
786 >        assertEquals(poolSize, ran.get());
787 >        assertEquals(poolSize, p.getCompletedTaskCount());
788      }
789  
790      // Exception Tests
791  
692
792      /**
793       * Constructor throws if corePoolSize argument is less than zero
794       */
795      public void testConstructor1() {
796          try {
797 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
797 >            new CustomTPE(-1, 1, 1L, SECONDS,
798 >                          new ArrayBlockingQueue<Runnable>(10));
799              shouldThrow();
800          } catch (IllegalArgumentException success) {}
801      }
# Line 705 | Line 805 | public class ThreadPoolExecutorSubclassT
805       */
806      public void testConstructor2() {
807          try {
808 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
808 >            new CustomTPE(1, -1, 1L, SECONDS,
809 >                          new ArrayBlockingQueue<Runnable>(10));
810              shouldThrow();
811          } catch (IllegalArgumentException success) {}
812      }
# Line 715 | Line 816 | public class ThreadPoolExecutorSubclassT
816       */
817      public void testConstructor3() {
818          try {
819 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
819 >            new CustomTPE(1, 0, 1L, SECONDS,
820 >                          new ArrayBlockingQueue<Runnable>(10));
821              shouldThrow();
822          } catch (IllegalArgumentException success) {}
823      }
# Line 725 | Line 827 | public class ThreadPoolExecutorSubclassT
827       */
828      public void testConstructor4() {
829          try {
830 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
830 >            new CustomTPE(1, 2, -1L, SECONDS,
831 >                          new ArrayBlockingQueue<Runnable>(10));
832              shouldThrow();
833          } catch (IllegalArgumentException success) {}
834      }
# Line 735 | Line 838 | public class ThreadPoolExecutorSubclassT
838       */
839      public void testConstructor5() {
840          try {
841 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
841 >            new CustomTPE(2, 1, 1L, SECONDS,
842 >                          new ArrayBlockingQueue<Runnable>(10));
843              shouldThrow();
844          } catch (IllegalArgumentException success) {}
845      }
# Line 745 | Line 849 | public class ThreadPoolExecutorSubclassT
849       */
850      public void testConstructorNullPointerException() {
851          try {
852 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
852 >            new CustomTPE(1, 2, 1L, SECONDS, null);
853              shouldThrow();
854          } catch (NullPointerException success) {}
855      }
856  
753
754
857      /**
858       * Constructor throws if corePoolSize argument is less than zero
859       */
860      public void testConstructor6() {
861          try {
862 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
862 >            new CustomTPE(-1, 1, 1L, SECONDS,
863 >                          new ArrayBlockingQueue<Runnable>(10),
864 >                          new SimpleThreadFactory());
865              shouldThrow();
866          } catch (IllegalArgumentException success) {}
867      }
# Line 767 | Line 871 | public class ThreadPoolExecutorSubclassT
871       */
872      public void testConstructor7() {
873          try {
874 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
874 >            new CustomTPE(1,-1, 1L, SECONDS,
875 >                          new ArrayBlockingQueue<Runnable>(10),
876 >                          new SimpleThreadFactory());
877              shouldThrow();
878          } catch (IllegalArgumentException success) {}
879      }
# Line 777 | Line 883 | public class ThreadPoolExecutorSubclassT
883       */
884      public void testConstructor8() {
885          try {
886 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
886 >            new CustomTPE(1, 0, 1L, SECONDS,
887 >                          new ArrayBlockingQueue<Runnable>(10),
888 >                          new SimpleThreadFactory());
889              shouldThrow();
890          } catch (IllegalArgumentException success) {}
891      }
# Line 787 | Line 895 | public class ThreadPoolExecutorSubclassT
895       */
896      public void testConstructor9() {
897          try {
898 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
898 >            new CustomTPE(1, 2, -1L, SECONDS,
899 >                          new ArrayBlockingQueue<Runnable>(10),
900 >                          new SimpleThreadFactory());
901              shouldThrow();
902          } catch (IllegalArgumentException success) {}
903      }
# Line 797 | Line 907 | public class ThreadPoolExecutorSubclassT
907       */
908      public void testConstructor10() {
909          try {
910 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
910 >            new CustomTPE(2, 1, 1L, SECONDS,
911 >                          new ArrayBlockingQueue<Runnable>(10),
912 >                          new SimpleThreadFactory());
913              shouldThrow();
914          } catch (IllegalArgumentException success) {}
915      }
# Line 807 | Line 919 | public class ThreadPoolExecutorSubclassT
919       */
920      public void testConstructorNullPointerException2() {
921          try {
922 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
922 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
923              shouldThrow();
924          } catch (NullPointerException success) {}
925      }
# Line 817 | Line 929 | public class ThreadPoolExecutorSubclassT
929       */
930      public void testConstructorNullPointerException3() {
931          try {
932 <            ThreadFactory f = null;
933 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
932 >            new CustomTPE(1, 2, 1L, SECONDS,
933 >                          new ArrayBlockingQueue<Runnable>(10),
934 >                          (ThreadFactory) null);
935              shouldThrow();
936          } catch (NullPointerException success) {}
937      }
938  
826
939      /**
940       * Constructor throws if corePoolSize argument is less than zero
941       */
942      public void testConstructor11() {
943          try {
944 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
944 >            new CustomTPE(-1, 1, 1L, SECONDS,
945 >                          new ArrayBlockingQueue<Runnable>(10),
946 >                          new NoOpREHandler());
947              shouldThrow();
948          } catch (IllegalArgumentException success) {}
949      }
# Line 839 | Line 953 | public class ThreadPoolExecutorSubclassT
953       */
954      public void testConstructor12() {
955          try {
956 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
956 >            new CustomTPE(1, -1, 1L, SECONDS,
957 >                          new ArrayBlockingQueue<Runnable>(10),
958 >                          new NoOpREHandler());
959              shouldThrow();
960          } catch (IllegalArgumentException success) {}
961      }
# Line 849 | Line 965 | public class ThreadPoolExecutorSubclassT
965       */
966      public void testConstructor13() {
967          try {
968 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
968 >            new CustomTPE(1, 0, 1L, SECONDS,
969 >                          new ArrayBlockingQueue<Runnable>(10),
970 >                          new NoOpREHandler());
971              shouldThrow();
972          } catch (IllegalArgumentException success) {}
973      }
# Line 859 | Line 977 | public class ThreadPoolExecutorSubclassT
977       */
978      public void testConstructor14() {
979          try {
980 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
980 >            new CustomTPE(1, 2, -1L, SECONDS,
981 >                          new ArrayBlockingQueue<Runnable>(10),
982 >                          new NoOpREHandler());
983              shouldThrow();
984          } catch (IllegalArgumentException success) {}
985      }
# Line 869 | Line 989 | public class ThreadPoolExecutorSubclassT
989       */
990      public void testConstructor15() {
991          try {
992 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
992 >            new CustomTPE(2, 1, 1L, SECONDS,
993 >                          new ArrayBlockingQueue<Runnable>(10),
994 >                          new NoOpREHandler());
995              shouldThrow();
996          } catch (IllegalArgumentException success) {}
997      }
# Line 879 | Line 1001 | public class ThreadPoolExecutorSubclassT
1001       */
1002      public void testConstructorNullPointerException4() {
1003          try {
1004 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
1004 >            new CustomTPE(1, 2, 1L, SECONDS,
1005 >                          null,
1006 >                          new NoOpREHandler());
1007              shouldThrow();
1008          } catch (NullPointerException success) {}
1009      }
# Line 889 | Line 1013 | public class ThreadPoolExecutorSubclassT
1013       */
1014      public void testConstructorNullPointerException5() {
1015          try {
1016 <            RejectedExecutionHandler r = null;
1017 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
1016 >            new CustomTPE(1, 2, 1L, SECONDS,
1017 >                          new ArrayBlockingQueue<Runnable>(10),
1018 >                          (RejectedExecutionHandler) null);
1019              shouldThrow();
1020          } catch (NullPointerException success) {}
1021      }
1022  
898
1023      /**
1024       * Constructor throws if corePoolSize argument is less than zero
1025       */
1026      public void testConstructor16() {
1027          try {
1028 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1028 >            new CustomTPE(-1, 1, 1L, SECONDS,
1029 >                          new ArrayBlockingQueue<Runnable>(10),
1030 >                          new SimpleThreadFactory(),
1031 >                          new NoOpREHandler());
1032              shouldThrow();
1033          } catch (IllegalArgumentException success) {}
1034      }
# Line 911 | Line 1038 | public class ThreadPoolExecutorSubclassT
1038       */
1039      public void testConstructor17() {
1040          try {
1041 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1041 >            new CustomTPE(1, -1, 1L, SECONDS,
1042 >                          new ArrayBlockingQueue<Runnable>(10),
1043 >                          new SimpleThreadFactory(),
1044 >                          new NoOpREHandler());
1045              shouldThrow();
1046          } catch (IllegalArgumentException success) {}
1047      }
# Line 921 | Line 1051 | public class ThreadPoolExecutorSubclassT
1051       */
1052      public void testConstructor18() {
1053          try {
1054 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1054 >            new CustomTPE(1, 0, 1L, SECONDS,
1055 >                          new ArrayBlockingQueue<Runnable>(10),
1056 >                          new SimpleThreadFactory(),
1057 >                          new NoOpREHandler());
1058              shouldThrow();
1059          } catch (IllegalArgumentException success) {}
1060      }
# Line 931 | Line 1064 | public class ThreadPoolExecutorSubclassT
1064       */
1065      public void testConstructor19() {
1066          try {
1067 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1067 >            new CustomTPE(1, 2, -1L, SECONDS,
1068 >                          new ArrayBlockingQueue<Runnable>(10),
1069 >                          new SimpleThreadFactory(),
1070 >                          new NoOpREHandler());
1071              shouldThrow();
1072          } catch (IllegalArgumentException success) {}
1073      }
# Line 941 | Line 1077 | public class ThreadPoolExecutorSubclassT
1077       */
1078      public void testConstructor20() {
1079          try {
1080 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1080 >            new CustomTPE(2, 1, 1L, SECONDS,
1081 >                          new ArrayBlockingQueue<Runnable>(10),
1082 >                          new SimpleThreadFactory(),
1083 >                          new NoOpREHandler());
1084              shouldThrow();
1085          } catch (IllegalArgumentException success) {}
1086      }
# Line 951 | Line 1090 | public class ThreadPoolExecutorSubclassT
1090       */
1091      public void testConstructorNullPointerException6() {
1092          try {
1093 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1093 >            new CustomTPE(1, 2, 1L, SECONDS,
1094 >                          null,
1095 >                          new SimpleThreadFactory(),
1096 >                          new NoOpREHandler());
1097              shouldThrow();
1098          } catch (NullPointerException success) {}
1099      }
# Line 961 | Line 1103 | public class ThreadPoolExecutorSubclassT
1103       */
1104      public void testConstructorNullPointerException7() {
1105          try {
1106 <            RejectedExecutionHandler r = null;
1107 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
1106 >            new CustomTPE(1, 2, 1L, SECONDS,
1107 >                          new ArrayBlockingQueue<Runnable>(10),
1108 >                          new SimpleThreadFactory(),
1109 >                          (RejectedExecutionHandler) null);
1110              shouldThrow();
1111          } catch (NullPointerException success) {}
1112      }
# Line 972 | Line 1116 | public class ThreadPoolExecutorSubclassT
1116       */
1117      public void testConstructorNullPointerException8() {
1118          try {
1119 <            new CustomTPE(1, 2,
976 <                          LONG_DELAY_MS, MILLISECONDS,
1119 >            new CustomTPE(1, 2, 1L, SECONDS,
1120                            new ArrayBlockingQueue<Runnable>(10),
1121                            (ThreadFactory) null,
1122                            new NoOpREHandler());
# Line 981 | Line 1124 | public class ThreadPoolExecutorSubclassT
1124          } catch (NullPointerException success) {}
1125      }
1126  
984
1127      /**
1128       * execute throws RejectedExecutionException if saturated.
1129       */
# Line 1131 | Line 1273 | public class ThreadPoolExecutorSubclassT
1273          }
1274      }
1275  
1134
1276      /**
1277       * execute using DiscardOldestPolicy drops task on shutdown
1278       */
# Line 1149 | Line 1290 | public class ThreadPoolExecutorSubclassT
1290          }
1291      }
1292  
1152
1293      /**
1294       * execute(null) throws NPE
1295       */
1296      public void testExecuteNull() {
1297 <        ThreadPoolExecutor p = null;
1297 >        ThreadPoolExecutor p =
1298 >            new CustomTPE(1, 2, 1L, SECONDS,
1299 >                          new ArrayBlockingQueue<Runnable>(10));
1300          try {
1159            p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1301              p.execute(null);
1302              shouldThrow();
1303          } catch (NullPointerException success) {}
# Line 1214 | Line 1355 | public class ThreadPoolExecutorSubclassT
1355          joinPool(p);
1356      }
1357  
1217
1358      /**
1359       * setKeepAliveTime throws IllegalArgumentException
1360       * when given a negative value
# Line 1239 | Line 1379 | public class ThreadPoolExecutorSubclassT
1379      public void testTerminated() {
1380          CustomTPE p = new CustomTPE();
1381          try { p.shutdown(); } catch (SecurityException ok) { return; }
1382 <        assertTrue(p.terminatedCalled);
1382 >        assertTrue(p.terminatedCalled());
1383          joinPool(p);
1384      }
1385  
# Line 1249 | Line 1389 | public class ThreadPoolExecutorSubclassT
1389      public void testBeforeAfter() throws InterruptedException {
1390          CustomTPE p = new CustomTPE();
1391          try {
1392 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1393 <            p.execute(r);
1394 <            delay(SHORT_DELAY_MS);
1395 <            assertTrue(r.done);
1396 <            assertTrue(p.beforeCalled);
1397 <            assertTrue(p.afterCalled);
1392 >            final CountDownLatch done = new CountDownLatch(1);
1393 >            p.execute(new CheckedRunnable() {
1394 >                public void realRun() {
1395 >                    done.countDown();
1396 >                }});
1397 >            await(p.afterCalled);
1398 >            assertEquals(0, done.getCount());
1399 >            assertTrue(p.afterCalled());
1400 >            assertTrue(p.beforeCalled());
1401              try { p.shutdown(); } catch (SecurityException ok) { return; }
1402          } finally {
1403              joinPool(p);
# Line 1303 | Line 1446 | public class ThreadPoolExecutorSubclassT
1446          }
1447      }
1448  
1306
1449      /**
1450       * invokeAny(null) throws NPE
1451       */
# Line 1465 | Line 1607 | public class ThreadPoolExecutorSubclassT
1607          }
1608      }
1609  
1468
1469
1610      /**
1611       * timed invokeAny(null) throws NPE
1612       */
# Line 1653 | Line 1793 | public class ThreadPoolExecutorSubclassT
1793              l.add(new StringTask());
1794              l.add(new StringTask());
1795              List<Future<String>> futures =
1796 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1796 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1797              assertEquals(2, futures.size());
1798              for (Future<String> future : futures)
1799                  assertSame(TEST_STRING, future.get());
# Line 1668 | Line 1808 | public class ThreadPoolExecutorSubclassT
1808      public void testTimedInvokeAll6() throws Exception {
1809          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1810          try {
1811 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1812 <            l.add(new StringTask());
1813 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1814 <            l.add(new StringTask());
1815 <            List<Future<String>> futures =
1816 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1817 <            assertEquals(3, futures.size());
1818 <            Iterator<Future<String>> it = futures.iterator();
1819 <            Future<String> f1 = it.next();
1820 <            Future<String> f2 = it.next();
1821 <            Future<String> f3 = it.next();
1822 <            assertTrue(f1.isDone());
1823 <            assertTrue(f2.isDone());
1824 <            assertTrue(f3.isDone());
1825 <            assertFalse(f1.isCancelled());
1826 <            assertTrue(f2.isCancelled());
1811 >            for (long timeout = timeoutMillis();;) {
1812 >                List<Callable<String>> tasks = new ArrayList<>();
1813 >                tasks.add(new StringTask("0"));
1814 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1815 >                tasks.add(new StringTask("2"));
1816 >                long startTime = System.nanoTime();
1817 >                List<Future<String>> futures =
1818 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1819 >                assertEquals(tasks.size(), futures.size());
1820 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1821 >                for (Future future : futures)
1822 >                    assertTrue(future.isDone());
1823 >                assertTrue(futures.get(1).isCancelled());
1824 >                try {
1825 >                    assertEquals("0", futures.get(0).get());
1826 >                    assertEquals("2", futures.get(2).get());
1827 >                    break;
1828 >                } catch (CancellationException retryWithLongerTimeout) {
1829 >                    timeout *= 2;
1830 >                    if (timeout >= LONG_DELAY_MS / 2)
1831 >                        fail("expected exactly one task to be cancelled");
1832 >                }
1833 >            }
1834          } finally {
1835              joinPool(e);
1836          }
# Line 1726 | Line 1873 | public class ThreadPoolExecutorSubclassT
1873       * allowCoreThreadTimeOut(true) causes idle threads to time out
1874       */
1875      public void testAllowCoreThreadTimeOut_true() throws Exception {
1876 +        long keepAliveTime = timeoutMillis();
1877          final ThreadPoolExecutor p =
1878              new CustomTPE(2, 10,
1879 <                          SHORT_DELAY_MS, MILLISECONDS,
1879 >                          keepAliveTime, MILLISECONDS,
1880                            new ArrayBlockingQueue<Runnable>(10));
1881          final CountDownLatch threadStarted = new CountDownLatch(1);
1882          try {
1883              p.allowCoreThreadTimeOut(true);
1884              p.execute(new CheckedRunnable() {
1885 <                public void realRun() throws InterruptedException {
1885 >                public void realRun() {
1886                      threadStarted.countDown();
1887                      assertEquals(1, p.getPoolSize());
1888                  }});
1889 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1890 <            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
1891 <                if (p.getPoolSize() == 0)
1892 <                    break;
1893 <                delay(10);
1894 <            }
1889 >            await(threadStarted);
1890 >            delay(keepAliveTime);
1891 >            long startTime = System.nanoTime();
1892 >            while (p.getPoolSize() > 0
1893 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1894 >                Thread.yield();
1895 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1896              assertEquals(0, p.getPoolSize());
1897          } finally {
1898              joinPool(p);
# Line 1754 | Line 1903 | public class ThreadPoolExecutorSubclassT
1903       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1904       */
1905      public void testAllowCoreThreadTimeOut_false() throws Exception {
1906 +        long keepAliveTime = timeoutMillis();
1907          final ThreadPoolExecutor p =
1908              new CustomTPE(2, 10,
1909 <                          SHORT_DELAY_MS, MILLISECONDS,
1909 >                          keepAliveTime, MILLISECONDS,
1910                            new ArrayBlockingQueue<Runnable>(10));
1911          final CountDownLatch threadStarted = new CountDownLatch(1);
1912          try {
# Line 1766 | Line 1916 | public class ThreadPoolExecutorSubclassT
1916                      threadStarted.countDown();
1917                      assertTrue(p.getPoolSize() >= 1);
1918                  }});
1919 <            delay(SMALL_DELAY_MS);
1919 >            delay(2 * keepAliveTime);
1920              assertTrue(p.getPoolSize() >= 1);
1921          } finally {
1922              joinPool(p);
1923          }
1924      }
1925  
1926 +    /**
1927 +     * get(cancelled task) throws CancellationException
1928 +     * (in part, a test of CustomTPE itself)
1929 +     */
1930 +    public void testGet_cancelled() throws Exception {
1931 +        final ExecutorService e =
1932 +            new CustomTPE(1, 1,
1933 +                          LONG_DELAY_MS, MILLISECONDS,
1934 +                          new LinkedBlockingQueue<Runnable>());
1935 +        try {
1936 +            final CountDownLatch blockerStarted = new CountDownLatch(1);
1937 +            final CountDownLatch done = new CountDownLatch(1);
1938 +            final List<Future<?>> futures = new ArrayList<>();
1939 +            for (int i = 0; i < 2; i++) {
1940 +                Runnable r = new CheckedRunnable() { public void realRun()
1941 +                                                         throws Throwable {
1942 +                    blockerStarted.countDown();
1943 +                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
1944 +                }};
1945 +                futures.add(e.submit(r));
1946 +            }
1947 +            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
1948 +            for (Future<?> future : futures) future.cancel(false);
1949 +            for (Future<?> future : futures) {
1950 +                try {
1951 +                    future.get();
1952 +                    shouldThrow();
1953 +                } catch (CancellationException success) {}
1954 +                try {
1955 +                    future.get(LONG_DELAY_MS, MILLISECONDS);
1956 +                    shouldThrow();
1957 +                } catch (CancellationException success) {}
1958 +                assertTrue(future.isCancelled());
1959 +                assertTrue(future.isDone());
1960 +            }
1961 +            done.countDown();
1962 +        } finally {
1963 +            joinPool(e);
1964 +        }
1965 +    }
1966 +
1967   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines