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.29 by jsr166, Fri May 27 19:35:24 2011 UTC vs.
Revision 1.62 by jsr166, Sun Oct 4 02:04:56 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 161 | 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 193 | Line 232 | public class ThreadPoolExecutorSubclassT
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();
202 <            }};
203 <        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));
206 <        } finally {
207 <            joinPool(p);
242 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
243          }
244      }
245  
# Line 219 | Line 254 | public class ThreadPoolExecutorSubclassT
254                            new ArrayBlockingQueue<Runnable>(10));
255          final CountDownLatch threadStarted = new CountDownLatch(1);
256          final CountDownLatch done = new CountDownLatch(1);
257 <        try {
257 >        try (PoolCleaner cleaner = cleaner(p)) {
258              assertEquals(0, p.getActiveCount());
259              p.execute(new CheckedRunnable() {
260                  public void realRun() throws InterruptedException {
# Line 229 | Line 264 | public class ThreadPoolExecutorSubclassT
264                  }});
265              assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
266              assertEquals(1, p.getActiveCount());
232        } finally {
267              done.countDown();
234            joinPool(p);
268          }
269      }
270  
# Line 239 | 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 272 | 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 284 | 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 299 | 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  
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  
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 352 | 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 363 | 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  
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  
# Line 394 | 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 405 | Line 491 | public class ThreadPoolExecutorSubclassT
491                          done.await();
492                          assertEquals(THREADS, p.getLargestPoolSize());
493                      }});
494 <            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
409 <            assertEquals(THREADS, p.getLargestPoolSize());
410 <        } finally {
411 <            done.countDown();
412 <            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 662 | Line 746 | public class ThreadPoolExecutorSubclassT
746      }
747  
748      /**
749 <     * shutdownNow returns a list containing tasks that were not run
749 >     * shutdownNow returns a list containing tasks that were not run,
750 >     * and those tasks are drained from the queue
751       */
752 <    public void testShutdownNow() {
753 <        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
754 <        List l;
755 <        try {
756 <            for (int i = 0; i < 5; i++)
757 <                p.execute(new MediumPossiblyInterruptedRunnable());
758 <        }
759 <        finally {
752 >    public void testShutdownNow() throws InterruptedException {
753 >        final int poolSize = 2;
754 >        final int count = 5;
755 >        final AtomicInteger ran = new AtomicInteger(0);
756 >        ThreadPoolExecutor p =
757 >            new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS,
758 >                          new ArrayBlockingQueue<Runnable>(10));
759 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
760 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
761 >            threadsStarted.countDown();
762              try {
763 <                l = p.shutdownNow();
764 <            } catch (SecurityException ok) { return; }
763 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
764 >            } catch (InterruptedException success) {}
765 >            ran.getAndIncrement();
766 >        }};
767 >        for (int i = 0; i < count; i++)
768 >            p.execute(waiter);
769 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
770 >        assertEquals(poolSize, p.getActiveCount());
771 >        assertEquals(0, p.getCompletedTaskCount());
772 >        final List<Runnable> queuedTasks;
773 >        try {
774 >            queuedTasks = p.shutdownNow();
775 >        } catch (SecurityException ok) {
776 >            return; // Allowed in case test doesn't have privs
777          }
778          assertTrue(p.isShutdown());
779 <        assertTrue(l.size() <= 4);
779 >        assertTrue(p.getQueue().isEmpty());
780 >        assertEquals(count - poolSize, queuedTasks.size());
781 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
782 >        assertTrue(p.isTerminated());
783 >        assertEquals(poolSize, ran.get());
784 >        assertEquals(poolSize, p.getCompletedTaskCount());
785      }
786  
787      // Exception Tests
# Line 687 | Line 791 | public class ThreadPoolExecutorSubclassT
791       */
792      public void testConstructor1() {
793          try {
794 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
794 >            new CustomTPE(-1, 1, 1L, SECONDS,
795 >                          new ArrayBlockingQueue<Runnable>(10));
796              shouldThrow();
797          } catch (IllegalArgumentException success) {}
798      }
# Line 697 | Line 802 | public class ThreadPoolExecutorSubclassT
802       */
803      public void testConstructor2() {
804          try {
805 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
805 >            new CustomTPE(1, -1, 1L, SECONDS,
806 >                          new ArrayBlockingQueue<Runnable>(10));
807              shouldThrow();
808          } catch (IllegalArgumentException success) {}
809      }
# Line 707 | Line 813 | public class ThreadPoolExecutorSubclassT
813       */
814      public void testConstructor3() {
815          try {
816 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
816 >            new CustomTPE(1, 0, 1L, SECONDS,
817 >                          new ArrayBlockingQueue<Runnable>(10));
818              shouldThrow();
819          } catch (IllegalArgumentException success) {}
820      }
# Line 717 | Line 824 | public class ThreadPoolExecutorSubclassT
824       */
825      public void testConstructor4() {
826          try {
827 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
827 >            new CustomTPE(1, 2, -1L, SECONDS,
828 >                          new ArrayBlockingQueue<Runnable>(10));
829              shouldThrow();
830          } catch (IllegalArgumentException success) {}
831      }
# Line 727 | Line 835 | public class ThreadPoolExecutorSubclassT
835       */
836      public void testConstructor5() {
837          try {
838 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
838 >            new CustomTPE(2, 1, 1L, SECONDS,
839 >                          new ArrayBlockingQueue<Runnable>(10));
840              shouldThrow();
841          } catch (IllegalArgumentException success) {}
842      }
# Line 737 | Line 846 | public class ThreadPoolExecutorSubclassT
846       */
847      public void testConstructorNullPointerException() {
848          try {
849 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
849 >            new CustomTPE(1, 2, 1L, SECONDS, null);
850              shouldThrow();
851          } catch (NullPointerException success) {}
852      }
# Line 747 | Line 856 | public class ThreadPoolExecutorSubclassT
856       */
857      public void testConstructor6() {
858          try {
859 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
859 >            new CustomTPE(-1, 1, 1L, SECONDS,
860 >                          new ArrayBlockingQueue<Runnable>(10),
861 >                          new SimpleThreadFactory());
862              shouldThrow();
863          } catch (IllegalArgumentException success) {}
864      }
# Line 757 | Line 868 | public class ThreadPoolExecutorSubclassT
868       */
869      public void testConstructor7() {
870          try {
871 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
871 >            new CustomTPE(1,-1, 1L, SECONDS,
872 >                          new ArrayBlockingQueue<Runnable>(10),
873 >                          new SimpleThreadFactory());
874              shouldThrow();
875          } catch (IllegalArgumentException success) {}
876      }
# Line 767 | Line 880 | public class ThreadPoolExecutorSubclassT
880       */
881      public void testConstructor8() {
882          try {
883 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
883 >            new CustomTPE(1, 0, 1L, SECONDS,
884 >                          new ArrayBlockingQueue<Runnable>(10),
885 >                          new SimpleThreadFactory());
886              shouldThrow();
887          } catch (IllegalArgumentException success) {}
888      }
# Line 777 | Line 892 | public class ThreadPoolExecutorSubclassT
892       */
893      public void testConstructor9() {
894          try {
895 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
895 >            new CustomTPE(1, 2, -1L, SECONDS,
896 >                          new ArrayBlockingQueue<Runnable>(10),
897 >                          new SimpleThreadFactory());
898              shouldThrow();
899          } catch (IllegalArgumentException success) {}
900      }
# Line 787 | Line 904 | public class ThreadPoolExecutorSubclassT
904       */
905      public void testConstructor10() {
906          try {
907 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
907 >            new CustomTPE(2, 1, 1L, SECONDS,
908 >                          new ArrayBlockingQueue<Runnable>(10),
909 >                          new SimpleThreadFactory());
910              shouldThrow();
911          } catch (IllegalArgumentException success) {}
912      }
# Line 797 | Line 916 | public class ThreadPoolExecutorSubclassT
916       */
917      public void testConstructorNullPointerException2() {
918          try {
919 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
919 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
920              shouldThrow();
921          } catch (NullPointerException success) {}
922      }
# Line 807 | Line 926 | public class ThreadPoolExecutorSubclassT
926       */
927      public void testConstructorNullPointerException3() {
928          try {
929 <            ThreadFactory f = null;
930 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
929 >            new CustomTPE(1, 2, 1L, SECONDS,
930 >                          new ArrayBlockingQueue<Runnable>(10),
931 >                          (ThreadFactory) null);
932              shouldThrow();
933          } catch (NullPointerException success) {}
934      }
# Line 818 | Line 938 | public class ThreadPoolExecutorSubclassT
938       */
939      public void testConstructor11() {
940          try {
941 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
941 >            new CustomTPE(-1, 1, 1L, SECONDS,
942 >                          new ArrayBlockingQueue<Runnable>(10),
943 >                          new NoOpREHandler());
944              shouldThrow();
945          } catch (IllegalArgumentException success) {}
946      }
# Line 828 | Line 950 | public class ThreadPoolExecutorSubclassT
950       */
951      public void testConstructor12() {
952          try {
953 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
953 >            new CustomTPE(1, -1, 1L, SECONDS,
954 >                          new ArrayBlockingQueue<Runnable>(10),
955 >                          new NoOpREHandler());
956              shouldThrow();
957          } catch (IllegalArgumentException success) {}
958      }
# Line 838 | Line 962 | public class ThreadPoolExecutorSubclassT
962       */
963      public void testConstructor13() {
964          try {
965 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
965 >            new CustomTPE(1, 0, 1L, SECONDS,
966 >                          new ArrayBlockingQueue<Runnable>(10),
967 >                          new NoOpREHandler());
968              shouldThrow();
969          } catch (IllegalArgumentException success) {}
970      }
# Line 848 | Line 974 | public class ThreadPoolExecutorSubclassT
974       */
975      public void testConstructor14() {
976          try {
977 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
977 >            new CustomTPE(1, 2, -1L, SECONDS,
978 >                          new ArrayBlockingQueue<Runnable>(10),
979 >                          new NoOpREHandler());
980              shouldThrow();
981          } catch (IllegalArgumentException success) {}
982      }
# Line 858 | Line 986 | public class ThreadPoolExecutorSubclassT
986       */
987      public void testConstructor15() {
988          try {
989 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
989 >            new CustomTPE(2, 1, 1L, SECONDS,
990 >                          new ArrayBlockingQueue<Runnable>(10),
991 >                          new NoOpREHandler());
992              shouldThrow();
993          } catch (IllegalArgumentException success) {}
994      }
# Line 868 | Line 998 | public class ThreadPoolExecutorSubclassT
998       */
999      public void testConstructorNullPointerException4() {
1000          try {
1001 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
1001 >            new CustomTPE(1, 2, 1L, SECONDS,
1002 >                          null,
1003 >                          new NoOpREHandler());
1004              shouldThrow();
1005          } catch (NullPointerException success) {}
1006      }
# Line 878 | Line 1010 | public class ThreadPoolExecutorSubclassT
1010       */
1011      public void testConstructorNullPointerException5() {
1012          try {
1013 <            RejectedExecutionHandler r = null;
1014 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
1013 >            new CustomTPE(1, 2, 1L, SECONDS,
1014 >                          new ArrayBlockingQueue<Runnable>(10),
1015 >                          (RejectedExecutionHandler) null);
1016              shouldThrow();
1017          } catch (NullPointerException success) {}
1018      }
# Line 889 | Line 1022 | public class ThreadPoolExecutorSubclassT
1022       */
1023      public void testConstructor16() {
1024          try {
1025 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1025 >            new CustomTPE(-1, 1, 1L, SECONDS,
1026 >                          new ArrayBlockingQueue<Runnable>(10),
1027 >                          new SimpleThreadFactory(),
1028 >                          new NoOpREHandler());
1029              shouldThrow();
1030          } catch (IllegalArgumentException success) {}
1031      }
# Line 899 | Line 1035 | public class ThreadPoolExecutorSubclassT
1035       */
1036      public void testConstructor17() {
1037          try {
1038 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1038 >            new CustomTPE(1, -1, 1L, SECONDS,
1039 >                          new ArrayBlockingQueue<Runnable>(10),
1040 >                          new SimpleThreadFactory(),
1041 >                          new NoOpREHandler());
1042              shouldThrow();
1043          } catch (IllegalArgumentException success) {}
1044      }
# Line 909 | Line 1048 | public class ThreadPoolExecutorSubclassT
1048       */
1049      public void testConstructor18() {
1050          try {
1051 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1051 >            new CustomTPE(1, 0, 1L, SECONDS,
1052 >                          new ArrayBlockingQueue<Runnable>(10),
1053 >                          new SimpleThreadFactory(),
1054 >                          new NoOpREHandler());
1055              shouldThrow();
1056          } catch (IllegalArgumentException success) {}
1057      }
# Line 919 | Line 1061 | public class ThreadPoolExecutorSubclassT
1061       */
1062      public void testConstructor19() {
1063          try {
1064 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1064 >            new CustomTPE(1, 2, -1L, SECONDS,
1065 >                          new ArrayBlockingQueue<Runnable>(10),
1066 >                          new SimpleThreadFactory(),
1067 >                          new NoOpREHandler());
1068              shouldThrow();
1069          } catch (IllegalArgumentException success) {}
1070      }
# Line 929 | Line 1074 | public class ThreadPoolExecutorSubclassT
1074       */
1075      public void testConstructor20() {
1076          try {
1077 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1077 >            new CustomTPE(2, 1, 1L, SECONDS,
1078 >                          new ArrayBlockingQueue<Runnable>(10),
1079 >                          new SimpleThreadFactory(),
1080 >                          new NoOpREHandler());
1081              shouldThrow();
1082          } catch (IllegalArgumentException success) {}
1083      }
# Line 939 | Line 1087 | public class ThreadPoolExecutorSubclassT
1087       */
1088      public void testConstructorNullPointerException6() {
1089          try {
1090 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1090 >            new CustomTPE(1, 2, 1L, SECONDS,
1091 >                          null,
1092 >                          new SimpleThreadFactory(),
1093 >                          new NoOpREHandler());
1094              shouldThrow();
1095          } catch (NullPointerException success) {}
1096      }
# Line 949 | Line 1100 | public class ThreadPoolExecutorSubclassT
1100       */
1101      public void testConstructorNullPointerException7() {
1102          try {
1103 <            RejectedExecutionHandler r = null;
1104 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
1103 >            new CustomTPE(1, 2, 1L, SECONDS,
1104 >                          new ArrayBlockingQueue<Runnable>(10),
1105 >                          new SimpleThreadFactory(),
1106 >                          (RejectedExecutionHandler) null);
1107              shouldThrow();
1108          } catch (NullPointerException success) {}
1109      }
# Line 960 | Line 1113 | public class ThreadPoolExecutorSubclassT
1113       */
1114      public void testConstructorNullPointerException8() {
1115          try {
1116 <            new CustomTPE(1, 2,
964 <                          LONG_DELAY_MS, MILLISECONDS,
1116 >            new CustomTPE(1, 2, 1L, SECONDS,
1117                            new ArrayBlockingQueue<Runnable>(10),
1118                            (ThreadFactory) null,
1119                            new NoOpREHandler());
# Line 1139 | Line 1291 | public class ThreadPoolExecutorSubclassT
1291       * execute(null) throws NPE
1292       */
1293      public void testExecuteNull() {
1294 <        ThreadPoolExecutor p = null;
1294 >        ThreadPoolExecutor p =
1295 >            new CustomTPE(1, 2, 1L, SECONDS,
1296 >                          new ArrayBlockingQueue<Runnable>(10));
1297          try {
1144            p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1298              p.execute(null);
1299              shouldThrow();
1300          } catch (NullPointerException success) {}
# Line 1223 | Line 1376 | public class ThreadPoolExecutorSubclassT
1376      public void testTerminated() {
1377          CustomTPE p = new CustomTPE();
1378          try { p.shutdown(); } catch (SecurityException ok) { return; }
1379 <        assertTrue(p.terminatedCalled);
1379 >        assertTrue(p.terminatedCalled());
1380          joinPool(p);
1381      }
1382  
# Line 1233 | Line 1386 | public class ThreadPoolExecutorSubclassT
1386      public void testBeforeAfter() throws InterruptedException {
1387          CustomTPE p = new CustomTPE();
1388          try {
1389 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1390 <            p.execute(r);
1391 <            delay(SHORT_DELAY_MS);
1392 <            assertTrue(r.done);
1393 <            assertTrue(p.beforeCalled);
1394 <            assertTrue(p.afterCalled);
1389 >            final CountDownLatch done = new CountDownLatch(1);
1390 >            p.execute(new CheckedRunnable() {
1391 >                public void realRun() {
1392 >                    done.countDown();
1393 >                }});
1394 >            await(p.afterCalled);
1395 >            assertEquals(0, done.getCount());
1396 >            assertTrue(p.afterCalled());
1397 >            assertTrue(p.beforeCalled());
1398              try { p.shutdown(); } catch (SecurityException ok) { return; }
1399          } finally {
1400              joinPool(p);
# Line 1634 | Line 1790 | public class ThreadPoolExecutorSubclassT
1790              l.add(new StringTask());
1791              l.add(new StringTask());
1792              List<Future<String>> futures =
1793 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1793 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1794              assertEquals(2, futures.size());
1795              for (Future<String> future : futures)
1796                  assertSame(TEST_STRING, future.get());
# Line 1649 | Line 1805 | public class ThreadPoolExecutorSubclassT
1805      public void testTimedInvokeAll6() throws Exception {
1806          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1807          try {
1808 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1809 <            l.add(new StringTask());
1810 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1811 <            l.add(new StringTask());
1812 <            List<Future<String>> futures =
1813 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1814 <            assertEquals(3, futures.size());
1815 <            Iterator<Future<String>> it = futures.iterator();
1816 <            Future<String> f1 = it.next();
1817 <            Future<String> f2 = it.next();
1818 <            Future<String> f3 = it.next();
1819 <            assertTrue(f1.isDone());
1820 <            assertTrue(f2.isDone());
1821 <            assertTrue(f3.isDone());
1822 <            assertFalse(f1.isCancelled());
1823 <            assertTrue(f2.isCancelled());
1808 >            for (long timeout = timeoutMillis();;) {
1809 >                List<Callable<String>> tasks = new ArrayList<>();
1810 >                tasks.add(new StringTask("0"));
1811 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1812 >                tasks.add(new StringTask("2"));
1813 >                long startTime = System.nanoTime();
1814 >                List<Future<String>> futures =
1815 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1816 >                assertEquals(tasks.size(), futures.size());
1817 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1818 >                for (Future future : futures)
1819 >                    assertTrue(future.isDone());
1820 >                assertTrue(futures.get(1).isCancelled());
1821 >                try {
1822 >                    assertEquals("0", futures.get(0).get());
1823 >                    assertEquals("2", futures.get(2).get());
1824 >                    break;
1825 >                } catch (CancellationException retryWithLongerTimeout) {
1826 >                    timeout *= 2;
1827 >                    if (timeout >= LONG_DELAY_MS / 2)
1828 >                        fail("expected exactly one task to be cancelled");
1829 >                }
1830 >            }
1831          } finally {
1832              joinPool(e);
1833          }
# Line 1707 | Line 1870 | public class ThreadPoolExecutorSubclassT
1870       * allowCoreThreadTimeOut(true) causes idle threads to time out
1871       */
1872      public void testAllowCoreThreadTimeOut_true() throws Exception {
1873 <        long coreThreadTimeOut = SHORT_DELAY_MS;
1873 >        long keepAliveTime = timeoutMillis();
1874          final ThreadPoolExecutor p =
1875              new CustomTPE(2, 10,
1876 <                          coreThreadTimeOut, MILLISECONDS,
1876 >                          keepAliveTime, MILLISECONDS,
1877                            new ArrayBlockingQueue<Runnable>(10));
1878          final CountDownLatch threadStarted = new CountDownLatch(1);
1879          try {
1880              p.allowCoreThreadTimeOut(true);
1881              p.execute(new CheckedRunnable() {
1882 <                public void realRun() throws InterruptedException {
1882 >                public void realRun() {
1883                      threadStarted.countDown();
1884                      assertEquals(1, p.getPoolSize());
1885                  }});
1886              await(threadStarted);
1887 <            delay(coreThreadTimeOut);
1887 >            delay(keepAliveTime);
1888              long startTime = System.nanoTime();
1889              while (p.getPoolSize() > 0
1890                     && millisElapsedSince(startTime) < LONG_DELAY_MS)
# Line 1737 | Line 1900 | public class ThreadPoolExecutorSubclassT
1900       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1901       */
1902      public void testAllowCoreThreadTimeOut_false() throws Exception {
1903 <        long coreThreadTimeOut = SHORT_DELAY_MS;
1903 >        long keepAliveTime = timeoutMillis();
1904          final ThreadPoolExecutor p =
1905              new CustomTPE(2, 10,
1906 <                          coreThreadTimeOut, MILLISECONDS,
1906 >                          keepAliveTime, MILLISECONDS,
1907                            new ArrayBlockingQueue<Runnable>(10));
1908          final CountDownLatch threadStarted = new CountDownLatch(1);
1909          try {
# Line 1750 | Line 1913 | public class ThreadPoolExecutorSubclassT
1913                      threadStarted.countDown();
1914                      assertTrue(p.getPoolSize() >= 1);
1915                  }});
1916 <            delay(2 * coreThreadTimeOut);
1916 >            delay(2 * keepAliveTime);
1917              assertTrue(p.getPoolSize() >= 1);
1918          } finally {
1919              joinPool(p);
1920          }
1921      }
1922  
1923 +    /**
1924 +     * get(cancelled task) throws CancellationException
1925 +     * (in part, a test of CustomTPE itself)
1926 +     */
1927 +    public void testGet_cancelled() throws Exception {
1928 +        final ExecutorService e =
1929 +            new CustomTPE(1, 1,
1930 +                          LONG_DELAY_MS, MILLISECONDS,
1931 +                          new LinkedBlockingQueue<Runnable>());
1932 +        try {
1933 +            final CountDownLatch blockerStarted = new CountDownLatch(1);
1934 +            final CountDownLatch done = new CountDownLatch(1);
1935 +            final List<Future<?>> futures = new ArrayList<>();
1936 +            for (int i = 0; i < 2; i++) {
1937 +                Runnable r = new CheckedRunnable() { public void realRun()
1938 +                                                         throws Throwable {
1939 +                    blockerStarted.countDown();
1940 +                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
1941 +                }};
1942 +                futures.add(e.submit(r));
1943 +            }
1944 +            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
1945 +            for (Future<?> future : futures) future.cancel(false);
1946 +            for (Future<?> future : futures) {
1947 +                try {
1948 +                    future.get();
1949 +                    shouldThrow();
1950 +                } catch (CancellationException success) {}
1951 +                try {
1952 +                    future.get(LONG_DELAY_MS, MILLISECONDS);
1953 +                    shouldThrow();
1954 +                } catch (CancellationException success) {}
1955 +                assertTrue(future.isCancelled());
1956 +                assertTrue(future.isDone());
1957 +            }
1958 +            done.countDown();
1959 +        } finally {
1960 +            joinPool(e);
1961 +        }
1962 +    }
1963 +
1964   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines