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.54 by jsr166, Sun Oct 4 01:27:32 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 323 | Line 364 | public class ThreadPoolExecutorSubclassT
364       */
365      public void testGetKeepAliveTime() {
366          ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
367 <        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
367 >        assertEquals(1, p.getKeepAliveTime(SECONDS));
368          joinPool(p);
369      }
370  
# Line 676 | Line 717 | public class ThreadPoolExecutorSubclassT
717      }
718  
719      /**
720 <     * shutdownNow returns a list containing tasks that were not run
720 >     * shutdownNow returns a list containing tasks that were not run,
721 >     * and those tasks are drained from the queue
722       */
723 <    public void testShutdownNow() {
724 <        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
725 <        List l;
726 <        try {
727 <            for (int i = 0; i < 5; i++)
728 <                p.execute(new MediumPossiblyInterruptedRunnable());
729 <        }
730 <        finally {
723 >    public void testShutdownNow() throws InterruptedException {
724 >        final int poolSize = 2;
725 >        final int count = 5;
726 >        final AtomicInteger ran = new AtomicInteger(0);
727 >        ThreadPoolExecutor p =
728 >            new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS,
729 >                          new ArrayBlockingQueue<Runnable>(10));
730 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
731 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
732 >            threadsStarted.countDown();
733              try {
734 <                l = p.shutdownNow();
735 <            } catch (SecurityException ok) { return; }
734 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
735 >            } catch (InterruptedException success) {}
736 >            ran.getAndIncrement();
737 >        }};
738 >        for (int i = 0; i < count; i++)
739 >            p.execute(waiter);
740 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
741 >        assertEquals(poolSize, p.getActiveCount());
742 >        assertEquals(0, p.getCompletedTaskCount());
743 >        final List<Runnable> queuedTasks;
744 >        try {
745 >            queuedTasks = p.shutdownNow();
746 >        } catch (SecurityException ok) {
747 >            return; // Allowed in case test doesn't have privs
748          }
749          assertTrue(p.isShutdown());
750 <        assertTrue(l.size() <= 4);
750 >        assertTrue(p.getQueue().isEmpty());
751 >        assertEquals(count - poolSize, queuedTasks.size());
752 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
753 >        assertTrue(p.isTerminated());
754 >        assertEquals(poolSize, ran.get());
755 >        assertEquals(poolSize, p.getCompletedTaskCount());
756      }
757  
758      // Exception Tests
# Line 701 | Line 762 | public class ThreadPoolExecutorSubclassT
762       */
763      public void testConstructor1() {
764          try {
765 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
765 >            new CustomTPE(-1, 1, 1L, SECONDS,
766 >                          new ArrayBlockingQueue<Runnable>(10));
767              shouldThrow();
768          } catch (IllegalArgumentException success) {}
769      }
# Line 711 | Line 773 | public class ThreadPoolExecutorSubclassT
773       */
774      public void testConstructor2() {
775          try {
776 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
776 >            new CustomTPE(1, -1, 1L, SECONDS,
777 >                          new ArrayBlockingQueue<Runnable>(10));
778              shouldThrow();
779          } catch (IllegalArgumentException success) {}
780      }
# Line 721 | Line 784 | public class ThreadPoolExecutorSubclassT
784       */
785      public void testConstructor3() {
786          try {
787 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
787 >            new CustomTPE(1, 0, 1L, SECONDS,
788 >                          new ArrayBlockingQueue<Runnable>(10));
789              shouldThrow();
790          } catch (IllegalArgumentException success) {}
791      }
# Line 731 | Line 795 | public class ThreadPoolExecutorSubclassT
795       */
796      public void testConstructor4() {
797          try {
798 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
798 >            new CustomTPE(1, 2, -1L, SECONDS,
799 >                          new ArrayBlockingQueue<Runnable>(10));
800              shouldThrow();
801          } catch (IllegalArgumentException success) {}
802      }
# Line 741 | Line 806 | public class ThreadPoolExecutorSubclassT
806       */
807      public void testConstructor5() {
808          try {
809 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
809 >            new CustomTPE(2, 1, 1L, SECONDS,
810 >                          new ArrayBlockingQueue<Runnable>(10));
811              shouldThrow();
812          } catch (IllegalArgumentException success) {}
813      }
# Line 751 | Line 817 | public class ThreadPoolExecutorSubclassT
817       */
818      public void testConstructorNullPointerException() {
819          try {
820 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
820 >            new CustomTPE(1, 2, 1L, SECONDS, null);
821              shouldThrow();
822          } catch (NullPointerException success) {}
823      }
# Line 761 | Line 827 | public class ThreadPoolExecutorSubclassT
827       */
828      public void testConstructor6() {
829          try {
830 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
830 >            new CustomTPE(-1, 1, 1L, SECONDS,
831 >                          new ArrayBlockingQueue<Runnable>(10),
832 >                          new SimpleThreadFactory());
833              shouldThrow();
834          } catch (IllegalArgumentException success) {}
835      }
# Line 771 | Line 839 | public class ThreadPoolExecutorSubclassT
839       */
840      public void testConstructor7() {
841          try {
842 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
842 >            new CustomTPE(1,-1, 1L, SECONDS,
843 >                          new ArrayBlockingQueue<Runnable>(10),
844 >                          new SimpleThreadFactory());
845              shouldThrow();
846          } catch (IllegalArgumentException success) {}
847      }
# Line 781 | Line 851 | public class ThreadPoolExecutorSubclassT
851       */
852      public void testConstructor8() {
853          try {
854 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
854 >            new CustomTPE(1, 0, 1L, SECONDS,
855 >                          new ArrayBlockingQueue<Runnable>(10),
856 >                          new SimpleThreadFactory());
857              shouldThrow();
858          } catch (IllegalArgumentException success) {}
859      }
# Line 791 | Line 863 | public class ThreadPoolExecutorSubclassT
863       */
864      public void testConstructor9() {
865          try {
866 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
866 >            new CustomTPE(1, 2, -1L, SECONDS,
867 >                          new ArrayBlockingQueue<Runnable>(10),
868 >                          new SimpleThreadFactory());
869              shouldThrow();
870          } catch (IllegalArgumentException success) {}
871      }
# Line 801 | Line 875 | public class ThreadPoolExecutorSubclassT
875       */
876      public void testConstructor10() {
877          try {
878 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
878 >            new CustomTPE(2, 1, 1L, SECONDS,
879 >                          new ArrayBlockingQueue<Runnable>(10),
880 >                          new SimpleThreadFactory());
881              shouldThrow();
882          } catch (IllegalArgumentException success) {}
883      }
# Line 811 | Line 887 | public class ThreadPoolExecutorSubclassT
887       */
888      public void testConstructorNullPointerException2() {
889          try {
890 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
890 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
891              shouldThrow();
892          } catch (NullPointerException success) {}
893      }
# Line 821 | Line 897 | public class ThreadPoolExecutorSubclassT
897       */
898      public void testConstructorNullPointerException3() {
899          try {
900 <            ThreadFactory f = null;
901 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
900 >            new CustomTPE(1, 2, 1L, SECONDS,
901 >                          new ArrayBlockingQueue<Runnable>(10),
902 >                          (ThreadFactory) null);
903              shouldThrow();
904          } catch (NullPointerException success) {}
905      }
# Line 832 | Line 909 | public class ThreadPoolExecutorSubclassT
909       */
910      public void testConstructor11() {
911          try {
912 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
912 >            new CustomTPE(-1, 1, 1L, SECONDS,
913 >                          new ArrayBlockingQueue<Runnable>(10),
914 >                          new NoOpREHandler());
915              shouldThrow();
916          } catch (IllegalArgumentException success) {}
917      }
# Line 842 | Line 921 | public class ThreadPoolExecutorSubclassT
921       */
922      public void testConstructor12() {
923          try {
924 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
924 >            new CustomTPE(1, -1, 1L, SECONDS,
925 >                          new ArrayBlockingQueue<Runnable>(10),
926 >                          new NoOpREHandler());
927              shouldThrow();
928          } catch (IllegalArgumentException success) {}
929      }
# Line 852 | Line 933 | public class ThreadPoolExecutorSubclassT
933       */
934      public void testConstructor13() {
935          try {
936 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
936 >            new CustomTPE(1, 0, 1L, SECONDS,
937 >                          new ArrayBlockingQueue<Runnable>(10),
938 >                          new NoOpREHandler());
939              shouldThrow();
940          } catch (IllegalArgumentException success) {}
941      }
# Line 862 | Line 945 | public class ThreadPoolExecutorSubclassT
945       */
946      public void testConstructor14() {
947          try {
948 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
948 >            new CustomTPE(1, 2, -1L, SECONDS,
949 >                          new ArrayBlockingQueue<Runnable>(10),
950 >                          new NoOpREHandler());
951              shouldThrow();
952          } catch (IllegalArgumentException success) {}
953      }
# Line 872 | Line 957 | public class ThreadPoolExecutorSubclassT
957       */
958      public void testConstructor15() {
959          try {
960 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
960 >            new CustomTPE(2, 1, 1L, SECONDS,
961 >                          new ArrayBlockingQueue<Runnable>(10),
962 >                          new NoOpREHandler());
963              shouldThrow();
964          } catch (IllegalArgumentException success) {}
965      }
# Line 882 | Line 969 | public class ThreadPoolExecutorSubclassT
969       */
970      public void testConstructorNullPointerException4() {
971          try {
972 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
972 >            new CustomTPE(1, 2, 1L, SECONDS,
973 >                          null,
974 >                          new NoOpREHandler());
975              shouldThrow();
976          } catch (NullPointerException success) {}
977      }
# Line 892 | Line 981 | public class ThreadPoolExecutorSubclassT
981       */
982      public void testConstructorNullPointerException5() {
983          try {
984 <            RejectedExecutionHandler r = null;
985 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
984 >            new CustomTPE(1, 2, 1L, SECONDS,
985 >                          new ArrayBlockingQueue<Runnable>(10),
986 >                          (RejectedExecutionHandler) null);
987              shouldThrow();
988          } catch (NullPointerException success) {}
989      }
# Line 903 | Line 993 | public class ThreadPoolExecutorSubclassT
993       */
994      public void testConstructor16() {
995          try {
996 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
996 >            new CustomTPE(-1, 1, 1L, SECONDS,
997 >                          new ArrayBlockingQueue<Runnable>(10),
998 >                          new SimpleThreadFactory(),
999 >                          new NoOpREHandler());
1000              shouldThrow();
1001          } catch (IllegalArgumentException success) {}
1002      }
# Line 913 | Line 1006 | public class ThreadPoolExecutorSubclassT
1006       */
1007      public void testConstructor17() {
1008          try {
1009 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1009 >            new CustomTPE(1, -1, 1L, SECONDS,
1010 >                          new ArrayBlockingQueue<Runnable>(10),
1011 >                          new SimpleThreadFactory(),
1012 >                          new NoOpREHandler());
1013              shouldThrow();
1014          } catch (IllegalArgumentException success) {}
1015      }
# Line 923 | Line 1019 | public class ThreadPoolExecutorSubclassT
1019       */
1020      public void testConstructor18() {
1021          try {
1022 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1022 >            new CustomTPE(1, 0, 1L, SECONDS,
1023 >                          new ArrayBlockingQueue<Runnable>(10),
1024 >                          new SimpleThreadFactory(),
1025 >                          new NoOpREHandler());
1026              shouldThrow();
1027          } catch (IllegalArgumentException success) {}
1028      }
# Line 933 | Line 1032 | public class ThreadPoolExecutorSubclassT
1032       */
1033      public void testConstructor19() {
1034          try {
1035 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1035 >            new CustomTPE(1, 2, -1L, SECONDS,
1036 >                          new ArrayBlockingQueue<Runnable>(10),
1037 >                          new SimpleThreadFactory(),
1038 >                          new NoOpREHandler());
1039              shouldThrow();
1040          } catch (IllegalArgumentException success) {}
1041      }
# Line 943 | Line 1045 | public class ThreadPoolExecutorSubclassT
1045       */
1046      public void testConstructor20() {
1047          try {
1048 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1048 >            new CustomTPE(2, 1, 1L, SECONDS,
1049 >                          new ArrayBlockingQueue<Runnable>(10),
1050 >                          new SimpleThreadFactory(),
1051 >                          new NoOpREHandler());
1052              shouldThrow();
1053          } catch (IllegalArgumentException success) {}
1054      }
# Line 953 | Line 1058 | public class ThreadPoolExecutorSubclassT
1058       */
1059      public void testConstructorNullPointerException6() {
1060          try {
1061 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1061 >            new CustomTPE(1, 2, 1L, SECONDS,
1062 >                          null,
1063 >                          new SimpleThreadFactory(),
1064 >                          new NoOpREHandler());
1065              shouldThrow();
1066          } catch (NullPointerException success) {}
1067      }
# Line 963 | Line 1071 | public class ThreadPoolExecutorSubclassT
1071       */
1072      public void testConstructorNullPointerException7() {
1073          try {
1074 <            RejectedExecutionHandler r = null;
1075 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
1074 >            new CustomTPE(1, 2, 1L, SECONDS,
1075 >                          new ArrayBlockingQueue<Runnable>(10),
1076 >                          new SimpleThreadFactory(),
1077 >                          (RejectedExecutionHandler) null);
1078              shouldThrow();
1079          } catch (NullPointerException success) {}
1080      }
# Line 974 | Line 1084 | public class ThreadPoolExecutorSubclassT
1084       */
1085      public void testConstructorNullPointerException8() {
1086          try {
1087 <            new CustomTPE(1, 2,
978 <                          LONG_DELAY_MS, MILLISECONDS,
1087 >            new CustomTPE(1, 2, 1L, SECONDS,
1088                            new ArrayBlockingQueue<Runnable>(10),
1089                            (ThreadFactory) null,
1090                            new NoOpREHandler());
# Line 1153 | Line 1262 | public class ThreadPoolExecutorSubclassT
1262       * execute(null) throws NPE
1263       */
1264      public void testExecuteNull() {
1265 <        ThreadPoolExecutor p = null;
1265 >        ThreadPoolExecutor p =
1266 >            new CustomTPE(1, 2, 1L, SECONDS,
1267 >                          new ArrayBlockingQueue<Runnable>(10));
1268          try {
1158            p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1269              p.execute(null);
1270              shouldThrow();
1271          } catch (NullPointerException success) {}
# Line 1248 | Line 1358 | public class ThreadPoolExecutorSubclassT
1358          CustomTPE p = new CustomTPE();
1359          try {
1360              final CountDownLatch done = new CountDownLatch(1);
1361 <            final CheckedRunnable task = new CheckedRunnable() {
1361 >            p.execute(new CheckedRunnable() {
1362                  public void realRun() {
1363                      done.countDown();
1364 <                }};
1255 <            p.execute(task);
1364 >                }});
1365              await(p.afterCalled);
1366              assertEquals(0, done.getCount());
1367              assertTrue(p.afterCalled());
# Line 1652 | Line 1761 | public class ThreadPoolExecutorSubclassT
1761              l.add(new StringTask());
1762              l.add(new StringTask());
1763              List<Future<String>> futures =
1764 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1764 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1765              assertEquals(2, futures.size());
1766              for (Future<String> future : futures)
1767                  assertSame(TEST_STRING, future.get());
# Line 1667 | Line 1776 | public class ThreadPoolExecutorSubclassT
1776      public void testTimedInvokeAll6() throws Exception {
1777          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1778          try {
1779 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1780 <            l.add(new StringTask());
1781 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1782 <            l.add(new StringTask());
1783 <            List<Future<String>> futures =
1784 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1785 <            assertEquals(l.size(), futures.size());
1786 <            for (Future future : futures)
1787 <                assertTrue(future.isDone());
1788 <            assertFalse(futures.get(0).isCancelled());
1789 <            assertTrue(futures.get(1).isCancelled());
1779 >            for (long timeout = timeoutMillis();;) {
1780 >                List<Callable<String>> tasks = new ArrayList<>();
1781 >                tasks.add(new StringTask("0"));
1782 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1783 >                tasks.add(new StringTask("2"));
1784 >                long startTime = System.nanoTime();
1785 >                List<Future<String>> futures =
1786 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1787 >                assertEquals(tasks.size(), futures.size());
1788 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1789 >                for (Future future : futures)
1790 >                    assertTrue(future.isDone());
1791 >                assertTrue(futures.get(1).isCancelled());
1792 >                try {
1793 >                    assertEquals("0", futures.get(0).get());
1794 >                    assertEquals("2", futures.get(2).get());
1795 >                    break;
1796 >                } catch (CancellationException retryWithLongerTimeout) {
1797 >                    timeout *= 2;
1798 >                    if (timeout >= LONG_DELAY_MS / 2)
1799 >                        fail("expected exactly one task to be cancelled");
1800 >                }
1801 >            }
1802          } finally {
1803              joinPool(e);
1804          }
# Line 1720 | Line 1841 | public class ThreadPoolExecutorSubclassT
1841       * allowCoreThreadTimeOut(true) causes idle threads to time out
1842       */
1843      public void testAllowCoreThreadTimeOut_true() throws Exception {
1844 <        long coreThreadTimeOut = SHORT_DELAY_MS;
1844 >        long keepAliveTime = timeoutMillis();
1845          final ThreadPoolExecutor p =
1846              new CustomTPE(2, 10,
1847 <                          coreThreadTimeOut, MILLISECONDS,
1847 >                          keepAliveTime, MILLISECONDS,
1848                            new ArrayBlockingQueue<Runnable>(10));
1849          final CountDownLatch threadStarted = new CountDownLatch(1);
1850          try {
1851              p.allowCoreThreadTimeOut(true);
1852              p.execute(new CheckedRunnable() {
1853 <                public void realRun() throws InterruptedException {
1853 >                public void realRun() {
1854                      threadStarted.countDown();
1855                      assertEquals(1, p.getPoolSize());
1856                  }});
1857              await(threadStarted);
1858 <            delay(coreThreadTimeOut);
1858 >            delay(keepAliveTime);
1859              long startTime = System.nanoTime();
1860              while (p.getPoolSize() > 0
1861                     && millisElapsedSince(startTime) < LONG_DELAY_MS)
# Line 1750 | Line 1871 | public class ThreadPoolExecutorSubclassT
1871       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1872       */
1873      public void testAllowCoreThreadTimeOut_false() throws Exception {
1874 <        long coreThreadTimeOut = SHORT_DELAY_MS;
1874 >        long keepAliveTime = timeoutMillis();
1875          final ThreadPoolExecutor p =
1876              new CustomTPE(2, 10,
1877 <                          coreThreadTimeOut, MILLISECONDS,
1877 >                          keepAliveTime, MILLISECONDS,
1878                            new ArrayBlockingQueue<Runnable>(10));
1879          final CountDownLatch threadStarted = new CountDownLatch(1);
1880          try {
# Line 1763 | Line 1884 | public class ThreadPoolExecutorSubclassT
1884                      threadStarted.countDown();
1885                      assertTrue(p.getPoolSize() >= 1);
1886                  }});
1887 <            delay(2 * coreThreadTimeOut);
1887 >            delay(2 * keepAliveTime);
1888              assertTrue(p.getPoolSize() >= 1);
1889          } finally {
1890              joinPool(p);
1891          }
1892      }
1893  
1894 +    /**
1895 +     * get(cancelled task) throws CancellationException
1896 +     * (in part, a test of CustomTPE itself)
1897 +     */
1898 +    public void testGet_cancelled() throws Exception {
1899 +        final ExecutorService e =
1900 +            new CustomTPE(1, 1,
1901 +                          LONG_DELAY_MS, MILLISECONDS,
1902 +                          new LinkedBlockingQueue<Runnable>());
1903 +        try {
1904 +            final CountDownLatch blockerStarted = new CountDownLatch(1);
1905 +            final CountDownLatch done = new CountDownLatch(1);
1906 +            final List<Future<?>> futures = new ArrayList<>();
1907 +            for (int i = 0; i < 2; i++) {
1908 +                Runnable r = new CheckedRunnable() { public void realRun()
1909 +                                                         throws Throwable {
1910 +                    blockerStarted.countDown();
1911 +                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
1912 +                }};
1913 +                futures.add(e.submit(r));
1914 +            }
1915 +            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
1916 +            for (Future<?> future : futures) future.cancel(false);
1917 +            for (Future<?> future : futures) {
1918 +                try {
1919 +                    future.get();
1920 +                    shouldThrow();
1921 +                } catch (CancellationException success) {}
1922 +                try {
1923 +                    future.get(LONG_DELAY_MS, MILLISECONDS);
1924 +                    shouldThrow();
1925 +                } catch (CancellationException success) {}
1926 +                assertTrue(future.isCancelled());
1927 +                assertTrue(future.isDone());
1928 +            }
1929 +            done.countDown();
1930 +        } finally {
1931 +            joinPool(e);
1932 +        }
1933 +    }
1934 +
1935   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines