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.30 by jsr166, Sun May 29 07:01:17 2011 UTC vs.
Revision 1.57 by jsr166, Sun Oct 4 01:50:30 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 203 | 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();
212 <            }};
213 <        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));
216 <        } finally {
217 <            joinPool(p);
242 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
243          }
244      }
245  
# Line 229 | 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 239 | Line 264 | public class ThreadPoolExecutorSubclassT
264                  }});
265              assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
266              assertEquals(1, p.getActiveCount());
242        } finally {
267              done.countDown();
244            joinPool(p);
268          }
269      }
270  
# Line 249 | 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 282 | 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 304 | Line 347 | public class ThreadPoolExecutorSubclassT
347                      fail("timed out");
348                  Thread.yield();
349              }
307        } finally {
308            joinPool(p);
350          }
351      }
352  
# Line 313 | 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 >        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 >        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 >        ThreadFactory threadFactory = new SimpleThreadFactory();
384 >        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 >        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      /**
# Line 676 | Line 735 | public class ThreadPoolExecutorSubclassT
735      }
736  
737      /**
738 <     * shutdownNow returns a list containing tasks that were not run
738 >     * shutdownNow returns a list containing tasks that were not run,
739 >     * and those tasks are drained from the queue
740       */
741 <    public void testShutdownNow() {
742 <        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
743 <        List l;
744 <        try {
745 <            for (int i = 0; i < 5; i++)
746 <                p.execute(new MediumPossiblyInterruptedRunnable());
747 <        }
748 <        finally {
741 >    public void testShutdownNow() throws InterruptedException {
742 >        final int poolSize = 2;
743 >        final int count = 5;
744 >        final AtomicInteger ran = new AtomicInteger(0);
745 >        ThreadPoolExecutor p =
746 >            new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS,
747 >                          new ArrayBlockingQueue<Runnable>(10));
748 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
749 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
750 >            threadsStarted.countDown();
751              try {
752 <                l = p.shutdownNow();
753 <            } catch (SecurityException ok) { return; }
752 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
753 >            } catch (InterruptedException success) {}
754 >            ran.getAndIncrement();
755 >        }};
756 >        for (int i = 0; i < count; i++)
757 >            p.execute(waiter);
758 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
759 >        assertEquals(poolSize, p.getActiveCount());
760 >        assertEquals(0, p.getCompletedTaskCount());
761 >        final List<Runnable> queuedTasks;
762 >        try {
763 >            queuedTasks = p.shutdownNow();
764 >        } catch (SecurityException ok) {
765 >            return; // Allowed in case test doesn't have privs
766          }
767          assertTrue(p.isShutdown());
768 <        assertTrue(l.size() <= 4);
768 >        assertTrue(p.getQueue().isEmpty());
769 >        assertEquals(count - poolSize, queuedTasks.size());
770 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
771 >        assertTrue(p.isTerminated());
772 >        assertEquals(poolSize, ran.get());
773 >        assertEquals(poolSize, p.getCompletedTaskCount());
774      }
775  
776      // Exception Tests
# Line 701 | Line 780 | public class ThreadPoolExecutorSubclassT
780       */
781      public void testConstructor1() {
782          try {
783 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
783 >            new CustomTPE(-1, 1, 1L, SECONDS,
784 >                          new ArrayBlockingQueue<Runnable>(10));
785              shouldThrow();
786          } catch (IllegalArgumentException success) {}
787      }
# Line 711 | Line 791 | public class ThreadPoolExecutorSubclassT
791       */
792      public void testConstructor2() {
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 721 | Line 802 | public class ThreadPoolExecutorSubclassT
802       */
803      public void testConstructor3() {
804          try {
805 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
805 >            new CustomTPE(1, 0, 1L, SECONDS,
806 >                          new ArrayBlockingQueue<Runnable>(10));
807              shouldThrow();
808          } catch (IllegalArgumentException success) {}
809      }
# Line 731 | Line 813 | public class ThreadPoolExecutorSubclassT
813       */
814      public void testConstructor4() {
815          try {
816 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
816 >            new CustomTPE(1, 2, -1L, SECONDS,
817 >                          new ArrayBlockingQueue<Runnable>(10));
818              shouldThrow();
819          } catch (IllegalArgumentException success) {}
820      }
# Line 741 | Line 824 | public class ThreadPoolExecutorSubclassT
824       */
825      public void testConstructor5() {
826          try {
827 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
827 >            new CustomTPE(2, 1, 1L, SECONDS,
828 >                          new ArrayBlockingQueue<Runnable>(10));
829              shouldThrow();
830          } catch (IllegalArgumentException success) {}
831      }
# Line 751 | Line 835 | public class ThreadPoolExecutorSubclassT
835       */
836      public void testConstructorNullPointerException() {
837          try {
838 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
838 >            new CustomTPE(1, 2, 1L, SECONDS, null);
839              shouldThrow();
840          } catch (NullPointerException success) {}
841      }
# Line 761 | Line 845 | public class ThreadPoolExecutorSubclassT
845       */
846      public void testConstructor6() {
847          try {
848 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
848 >            new CustomTPE(-1, 1, 1L, SECONDS,
849 >                          new ArrayBlockingQueue<Runnable>(10),
850 >                          new SimpleThreadFactory());
851              shouldThrow();
852          } catch (IllegalArgumentException success) {}
853      }
# Line 771 | Line 857 | public class ThreadPoolExecutorSubclassT
857       */
858      public void testConstructor7() {
859          try {
860 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
860 >            new CustomTPE(1,-1, 1L, SECONDS,
861 >                          new ArrayBlockingQueue<Runnable>(10),
862 >                          new SimpleThreadFactory());
863              shouldThrow();
864          } catch (IllegalArgumentException success) {}
865      }
# Line 781 | Line 869 | public class ThreadPoolExecutorSubclassT
869       */
870      public void testConstructor8() {
871          try {
872 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
872 >            new CustomTPE(1, 0, 1L, SECONDS,
873 >                          new ArrayBlockingQueue<Runnable>(10),
874 >                          new SimpleThreadFactory());
875              shouldThrow();
876          } catch (IllegalArgumentException success) {}
877      }
# Line 791 | Line 881 | public class ThreadPoolExecutorSubclassT
881       */
882      public void testConstructor9() {
883          try {
884 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
884 >            new CustomTPE(1, 2, -1L, SECONDS,
885 >                          new ArrayBlockingQueue<Runnable>(10),
886 >                          new SimpleThreadFactory());
887              shouldThrow();
888          } catch (IllegalArgumentException success) {}
889      }
# Line 801 | Line 893 | public class ThreadPoolExecutorSubclassT
893       */
894      public void testConstructor10() {
895          try {
896 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
896 >            new CustomTPE(2, 1, 1L, SECONDS,
897 >                          new ArrayBlockingQueue<Runnable>(10),
898 >                          new SimpleThreadFactory());
899              shouldThrow();
900          } catch (IllegalArgumentException success) {}
901      }
# Line 811 | Line 905 | public class ThreadPoolExecutorSubclassT
905       */
906      public void testConstructorNullPointerException2() {
907          try {
908 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
908 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
909              shouldThrow();
910          } catch (NullPointerException success) {}
911      }
# Line 821 | Line 915 | public class ThreadPoolExecutorSubclassT
915       */
916      public void testConstructorNullPointerException3() {
917          try {
918 <            ThreadFactory f = null;
919 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
918 >            new CustomTPE(1, 2, 1L, SECONDS,
919 >                          new ArrayBlockingQueue<Runnable>(10),
920 >                          (ThreadFactory) null);
921              shouldThrow();
922          } catch (NullPointerException success) {}
923      }
# Line 832 | Line 927 | public class ThreadPoolExecutorSubclassT
927       */
928      public void testConstructor11() {
929          try {
930 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
930 >            new CustomTPE(-1, 1, 1L, SECONDS,
931 >                          new ArrayBlockingQueue<Runnable>(10),
932 >                          new NoOpREHandler());
933              shouldThrow();
934          } catch (IllegalArgumentException success) {}
935      }
# Line 842 | Line 939 | public class ThreadPoolExecutorSubclassT
939       */
940      public void testConstructor12() {
941          try {
942 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
942 >            new CustomTPE(1, -1, 1L, SECONDS,
943 >                          new ArrayBlockingQueue<Runnable>(10),
944 >                          new NoOpREHandler());
945              shouldThrow();
946          } catch (IllegalArgumentException success) {}
947      }
# Line 852 | Line 951 | public class ThreadPoolExecutorSubclassT
951       */
952      public void testConstructor13() {
953          try {
954 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
954 >            new CustomTPE(1, 0, 1L, SECONDS,
955 >                          new ArrayBlockingQueue<Runnable>(10),
956 >                          new NoOpREHandler());
957              shouldThrow();
958          } catch (IllegalArgumentException success) {}
959      }
# Line 862 | Line 963 | public class ThreadPoolExecutorSubclassT
963       */
964      public void testConstructor14() {
965          try {
966 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
966 >            new CustomTPE(1, 2, -1L, SECONDS,
967 >                          new ArrayBlockingQueue<Runnable>(10),
968 >                          new NoOpREHandler());
969              shouldThrow();
970          } catch (IllegalArgumentException success) {}
971      }
# Line 872 | Line 975 | public class ThreadPoolExecutorSubclassT
975       */
976      public void testConstructor15() {
977          try {
978 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
978 >            new CustomTPE(2, 1, 1L, SECONDS,
979 >                          new ArrayBlockingQueue<Runnable>(10),
980 >                          new NoOpREHandler());
981              shouldThrow();
982          } catch (IllegalArgumentException success) {}
983      }
# Line 882 | Line 987 | public class ThreadPoolExecutorSubclassT
987       */
988      public void testConstructorNullPointerException4() {
989          try {
990 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
990 >            new CustomTPE(1, 2, 1L, SECONDS,
991 >                          null,
992 >                          new NoOpREHandler());
993              shouldThrow();
994          } catch (NullPointerException success) {}
995      }
# Line 892 | Line 999 | public class ThreadPoolExecutorSubclassT
999       */
1000      public void testConstructorNullPointerException5() {
1001          try {
1002 <            RejectedExecutionHandler r = null;
1003 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
1002 >            new CustomTPE(1, 2, 1L, SECONDS,
1003 >                          new ArrayBlockingQueue<Runnable>(10),
1004 >                          (RejectedExecutionHandler) null);
1005              shouldThrow();
1006          } catch (NullPointerException success) {}
1007      }
# Line 903 | Line 1011 | public class ThreadPoolExecutorSubclassT
1011       */
1012      public void testConstructor16() {
1013          try {
1014 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1014 >            new CustomTPE(-1, 1, 1L, SECONDS,
1015 >                          new ArrayBlockingQueue<Runnable>(10),
1016 >                          new SimpleThreadFactory(),
1017 >                          new NoOpREHandler());
1018              shouldThrow();
1019          } catch (IllegalArgumentException success) {}
1020      }
# Line 913 | Line 1024 | public class ThreadPoolExecutorSubclassT
1024       */
1025      public void testConstructor17() {
1026          try {
1027 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1027 >            new CustomTPE(1, -1, 1L, SECONDS,
1028 >                          new ArrayBlockingQueue<Runnable>(10),
1029 >                          new SimpleThreadFactory(),
1030 >                          new NoOpREHandler());
1031              shouldThrow();
1032          } catch (IllegalArgumentException success) {}
1033      }
# Line 923 | Line 1037 | public class ThreadPoolExecutorSubclassT
1037       */
1038      public void testConstructor18() {
1039          try {
1040 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1040 >            new CustomTPE(1, 0, 1L, SECONDS,
1041 >                          new ArrayBlockingQueue<Runnable>(10),
1042 >                          new SimpleThreadFactory(),
1043 >                          new NoOpREHandler());
1044              shouldThrow();
1045          } catch (IllegalArgumentException success) {}
1046      }
# Line 933 | Line 1050 | public class ThreadPoolExecutorSubclassT
1050       */
1051      public void testConstructor19() {
1052          try {
1053 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1053 >            new CustomTPE(1, 2, -1L, SECONDS,
1054 >                          new ArrayBlockingQueue<Runnable>(10),
1055 >                          new SimpleThreadFactory(),
1056 >                          new NoOpREHandler());
1057              shouldThrow();
1058          } catch (IllegalArgumentException success) {}
1059      }
# Line 943 | Line 1063 | public class ThreadPoolExecutorSubclassT
1063       */
1064      public void testConstructor20() {
1065          try {
1066 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1066 >            new CustomTPE(2, 1, 1L, SECONDS,
1067 >                          new ArrayBlockingQueue<Runnable>(10),
1068 >                          new SimpleThreadFactory(),
1069 >                          new NoOpREHandler());
1070              shouldThrow();
1071          } catch (IllegalArgumentException success) {}
1072      }
# Line 953 | Line 1076 | public class ThreadPoolExecutorSubclassT
1076       */
1077      public void testConstructorNullPointerException6() {
1078          try {
1079 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1079 >            new CustomTPE(1, 2, 1L, SECONDS,
1080 >                          null,
1081 >                          new SimpleThreadFactory(),
1082 >                          new NoOpREHandler());
1083              shouldThrow();
1084          } catch (NullPointerException success) {}
1085      }
# Line 963 | Line 1089 | public class ThreadPoolExecutorSubclassT
1089       */
1090      public void testConstructorNullPointerException7() {
1091          try {
1092 <            RejectedExecutionHandler r = null;
1093 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
1092 >            new CustomTPE(1, 2, 1L, SECONDS,
1093 >                          new ArrayBlockingQueue<Runnable>(10),
1094 >                          new SimpleThreadFactory(),
1095 >                          (RejectedExecutionHandler) null);
1096              shouldThrow();
1097          } catch (NullPointerException success) {}
1098      }
# Line 974 | Line 1102 | public class ThreadPoolExecutorSubclassT
1102       */
1103      public void testConstructorNullPointerException8() {
1104          try {
1105 <            new CustomTPE(1, 2,
978 <                          LONG_DELAY_MS, MILLISECONDS,
1105 >            new CustomTPE(1, 2, 1L, SECONDS,
1106                            new ArrayBlockingQueue<Runnable>(10),
1107                            (ThreadFactory) null,
1108                            new NoOpREHandler());
# Line 1153 | Line 1280 | public class ThreadPoolExecutorSubclassT
1280       * execute(null) throws NPE
1281       */
1282      public void testExecuteNull() {
1283 <        ThreadPoolExecutor p = null;
1283 >        ThreadPoolExecutor p =
1284 >            new CustomTPE(1, 2, 1L, SECONDS,
1285 >                          new ArrayBlockingQueue<Runnable>(10));
1286          try {
1158            p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1287              p.execute(null);
1288              shouldThrow();
1289          } catch (NullPointerException success) {}
# Line 1248 | Line 1376 | public class ThreadPoolExecutorSubclassT
1376          CustomTPE p = new CustomTPE();
1377          try {
1378              final CountDownLatch done = new CountDownLatch(1);
1379 <            final CheckedRunnable task = new CheckedRunnable() {
1379 >            p.execute(new CheckedRunnable() {
1380                  public void realRun() {
1381                      done.countDown();
1382 <                }};
1255 <            p.execute(task);
1382 >                }});
1383              await(p.afterCalled);
1384              assertEquals(0, done.getCount());
1385              assertTrue(p.afterCalled());
# Line 1652 | Line 1779 | public class ThreadPoolExecutorSubclassT
1779              l.add(new StringTask());
1780              l.add(new StringTask());
1781              List<Future<String>> futures =
1782 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1782 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1783              assertEquals(2, futures.size());
1784              for (Future<String> future : futures)
1785                  assertSame(TEST_STRING, future.get());
# Line 1667 | Line 1794 | public class ThreadPoolExecutorSubclassT
1794      public void testTimedInvokeAll6() throws Exception {
1795          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1796          try {
1797 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1798 <            l.add(new StringTask());
1799 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1800 <            l.add(new StringTask());
1801 <            List<Future<String>> futures =
1802 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1803 <            assertEquals(l.size(), futures.size());
1804 <            for (Future future : futures)
1805 <                assertTrue(future.isDone());
1806 <            assertFalse(futures.get(0).isCancelled());
1807 <            assertTrue(futures.get(1).isCancelled());
1797 >            for (long timeout = timeoutMillis();;) {
1798 >                List<Callable<String>> tasks = new ArrayList<>();
1799 >                tasks.add(new StringTask("0"));
1800 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1801 >                tasks.add(new StringTask("2"));
1802 >                long startTime = System.nanoTime();
1803 >                List<Future<String>> futures =
1804 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1805 >                assertEquals(tasks.size(), futures.size());
1806 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1807 >                for (Future future : futures)
1808 >                    assertTrue(future.isDone());
1809 >                assertTrue(futures.get(1).isCancelled());
1810 >                try {
1811 >                    assertEquals("0", futures.get(0).get());
1812 >                    assertEquals("2", futures.get(2).get());
1813 >                    break;
1814 >                } catch (CancellationException retryWithLongerTimeout) {
1815 >                    timeout *= 2;
1816 >                    if (timeout >= LONG_DELAY_MS / 2)
1817 >                        fail("expected exactly one task to be cancelled");
1818 >                }
1819 >            }
1820          } finally {
1821              joinPool(e);
1822          }
# Line 1720 | Line 1859 | public class ThreadPoolExecutorSubclassT
1859       * allowCoreThreadTimeOut(true) causes idle threads to time out
1860       */
1861      public void testAllowCoreThreadTimeOut_true() throws Exception {
1862 <        long coreThreadTimeOut = SHORT_DELAY_MS;
1862 >        long keepAliveTime = timeoutMillis();
1863          final ThreadPoolExecutor p =
1864              new CustomTPE(2, 10,
1865 <                          coreThreadTimeOut, MILLISECONDS,
1865 >                          keepAliveTime, MILLISECONDS,
1866                            new ArrayBlockingQueue<Runnable>(10));
1867          final CountDownLatch threadStarted = new CountDownLatch(1);
1868          try {
1869              p.allowCoreThreadTimeOut(true);
1870              p.execute(new CheckedRunnable() {
1871 <                public void realRun() throws InterruptedException {
1871 >                public void realRun() {
1872                      threadStarted.countDown();
1873                      assertEquals(1, p.getPoolSize());
1874                  }});
1875              await(threadStarted);
1876 <            delay(coreThreadTimeOut);
1876 >            delay(keepAliveTime);
1877              long startTime = System.nanoTime();
1878              while (p.getPoolSize() > 0
1879                     && millisElapsedSince(startTime) < LONG_DELAY_MS)
# Line 1750 | Line 1889 | public class ThreadPoolExecutorSubclassT
1889       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1890       */
1891      public void testAllowCoreThreadTimeOut_false() throws Exception {
1892 <        long coreThreadTimeOut = SHORT_DELAY_MS;
1892 >        long keepAliveTime = timeoutMillis();
1893          final ThreadPoolExecutor p =
1894              new CustomTPE(2, 10,
1895 <                          coreThreadTimeOut, MILLISECONDS,
1895 >                          keepAliveTime, MILLISECONDS,
1896                            new ArrayBlockingQueue<Runnable>(10));
1897          final CountDownLatch threadStarted = new CountDownLatch(1);
1898          try {
# Line 1763 | Line 1902 | public class ThreadPoolExecutorSubclassT
1902                      threadStarted.countDown();
1903                      assertTrue(p.getPoolSize() >= 1);
1904                  }});
1905 <            delay(2 * coreThreadTimeOut);
1905 >            delay(2 * keepAliveTime);
1906              assertTrue(p.getPoolSize() >= 1);
1907          } finally {
1908              joinPool(p);
1909          }
1910      }
1911  
1912 +    /**
1913 +     * get(cancelled task) throws CancellationException
1914 +     * (in part, a test of CustomTPE itself)
1915 +     */
1916 +    public void testGet_cancelled() throws Exception {
1917 +        final ExecutorService e =
1918 +            new CustomTPE(1, 1,
1919 +                          LONG_DELAY_MS, MILLISECONDS,
1920 +                          new LinkedBlockingQueue<Runnable>());
1921 +        try {
1922 +            final CountDownLatch blockerStarted = new CountDownLatch(1);
1923 +            final CountDownLatch done = new CountDownLatch(1);
1924 +            final List<Future<?>> futures = new ArrayList<>();
1925 +            for (int i = 0; i < 2; i++) {
1926 +                Runnable r = new CheckedRunnable() { public void realRun()
1927 +                                                         throws Throwable {
1928 +                    blockerStarted.countDown();
1929 +                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
1930 +                }};
1931 +                futures.add(e.submit(r));
1932 +            }
1933 +            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
1934 +            for (Future<?> future : futures) future.cancel(false);
1935 +            for (Future<?> future : futures) {
1936 +                try {
1937 +                    future.get();
1938 +                    shouldThrow();
1939 +                } catch (CancellationException success) {}
1940 +                try {
1941 +                    future.get(LONG_DELAY_MS, MILLISECONDS);
1942 +                    shouldThrow();
1943 +                } catch (CancellationException success) {}
1944 +                assertTrue(future.isCancelled());
1945 +                assertTrue(future.isDone());
1946 +            }
1947 +            done.countDown();
1948 +        } finally {
1949 +            joinPool(e);
1950 +        }
1951 +    }
1952 +
1953   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines