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.26 by dl, Fri May 6 11:22:08 2011 UTC vs.
Revision 1.51 by jsr166, Sun Oct 4 00:59:09 2015 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import java.util.concurrent.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 < import java.util.concurrent.locks.*;
10 > import static java.util.concurrent.TimeUnit.SECONDS;
11  
12 < import junit.framework.*;
13 < import java.util.*;
12 > import java.util.ArrayList;
13 > import java.util.List;
14 > import java.util.concurrent.ArrayBlockingQueue;
15 > import java.util.concurrent.BlockingQueue;
16 > import java.util.concurrent.Callable;
17 > import java.util.concurrent.CancellationException;
18 > import java.util.concurrent.CountDownLatch;
19 > import java.util.concurrent.ExecutionException;
20 > import java.util.concurrent.Executors;
21 > import java.util.concurrent.ExecutorService;
22 > import java.util.concurrent.Future;
23 > import java.util.concurrent.FutureTask;
24 > import java.util.concurrent.LinkedBlockingQueue;
25 > import java.util.concurrent.RejectedExecutionException;
26 > import java.util.concurrent.RejectedExecutionHandler;
27 > import java.util.concurrent.RunnableFuture;
28 > import java.util.concurrent.SynchronousQueue;
29 > import java.util.concurrent.ThreadFactory;
30 > import java.util.concurrent.ThreadPoolExecutor;
31 > import java.util.concurrent.TimeoutException;
32 > import java.util.concurrent.TimeUnit;
33 > import java.util.concurrent.atomic.AtomicInteger;
34 > import java.util.concurrent.locks.Condition;
35 > import java.util.concurrent.locks.ReentrantLock;
36 >
37 > import junit.framework.Test;
38 > import junit.framework.TestSuite;
39  
40   public class ThreadPoolExecutorSubclassTest extends JSR166TestCase {
41      public static void main(String[] args) {
42 <        junit.textui.TestRunner.run(suite());
42 >        main(suite(), args);
43      }
44      public static Test suite() {
45          return new TestSuite(ThreadPoolExecutorSubclassTest.class);
# Line 37 | Line 61 | public class ThreadPoolExecutorSubclassT
61          CustomTask(final Runnable r, final V res) {
62              if (r == null) throw new NullPointerException();
63              callable = new Callable<V>() {
64 <            public V call() throws Exception { r.run(); return res; }};
64 >                public V call() throws Exception { r.run(); return res; }};
65          }
66          public boolean isDone() {
67              lock.lock(); try { return done; } finally { lock.unlock() ; }
# Line 77 | Line 101 | public class ThreadPoolExecutorSubclassT
101              }
102              lock.lock();
103              try {
104 <                result = v;
105 <                exception = e;
106 <                done = true;
107 <                thread = null;
108 <                cond.signalAll();
104 >                if (!done) {
105 >                    result = v;
106 >                    exception = e;
107 >                    done = true;
108 >                    thread = null;
109 >                    cond.signalAll();
110 >                }
111              }
112              finally { lock.unlock(); }
113          }
# Line 90 | Line 116 | public class ThreadPoolExecutorSubclassT
116              try {
117                  while (!done)
118                      cond.await();
119 +                if (cancelled)
120 +                    throw new CancellationException();
121                  if (exception != null)
122                      throw new ExecutionException(exception);
123                  return result;
# Line 101 | Line 129 | public class ThreadPoolExecutorSubclassT
129              long nanos = unit.toNanos(timeout);
130              lock.lock();
131              try {
132 <                for (;;) {
133 <                    if (done) break;
106 <                    if (nanos < 0)
132 >                while (!done) {
133 >                    if (nanos <= 0L)
134                          throw new TimeoutException();
135                      nanos = cond.awaitNanos(nanos);
136                  }
137 +                if (cancelled)
138 +                    throw new CancellationException();
139                  if (exception != null)
140                      throw new ExecutionException(exception);
141                  return result;
# Line 115 | Line 144 | public class ThreadPoolExecutorSubclassT
144          }
145      }
146  
118
147      static class CustomTPE extends ThreadPoolExecutor {
148          protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
149              return new CustomTask<V>(c);
# Line 162 | Line 190 | public class ThreadPoolExecutorSubclassT
190                workQueue, threadFactory, handler);
191          }
192  
193 <        volatile boolean beforeCalled = false;
194 <        volatile boolean afterCalled = false;
195 <        volatile boolean terminatedCalled = false;
193 >        final CountDownLatch beforeCalled = new CountDownLatch(1);
194 >        final CountDownLatch afterCalled = new CountDownLatch(1);
195 >        final CountDownLatch terminatedCalled = new CountDownLatch(1);
196 >
197          public CustomTPE() {
198              super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
199          }
200          protected void beforeExecute(Thread t, Runnable r) {
201 <            beforeCalled = true;
201 >            beforeCalled.countDown();
202          }
203          protected void afterExecute(Runnable r, Throwable t) {
204 <            afterCalled = true;
204 >            afterCalled.countDown();
205          }
206          protected void terminated() {
207 <            terminatedCalled = true;
207 >            terminatedCalled.countDown();
208          }
209  
210 +        public boolean beforeCalled() {
211 +            return beforeCalled.getCount() == 0;
212 +        }
213 +        public boolean afterCalled() {
214 +            return afterCalled.getCount() == 0;
215 +        }
216 +        public boolean terminatedCalled() {
217 +            return terminatedCalled.getCount() == 0;
218 +        }
219      }
220  
221      static class FailingThreadFactory implements ThreadFactory {
# Line 188 | Line 226 | public class ThreadPoolExecutorSubclassT
226          }
227      }
228  
191
229      /**
230       * execute successfully executes a runnable
231       */
232      public void testExecute() throws InterruptedException {
233          final ThreadPoolExecutor p =
234              new CustomTPE(1, 1,
235 <                          LONG_DELAY_MS, MILLISECONDS,
235 >                          2 * LONG_DELAY_MS, MILLISECONDS,
236                            new ArrayBlockingQueue<Runnable>(10));
237 <        final CountDownLatch done = new CountDownLatch(1);
238 <        final Runnable task = new CheckedRunnable() {
239 <            public void realRun() {
240 <                done.countDown();
204 <            }};
205 <        try {
237 >        try (PoolCleaner cleaner = cleaner(p)) {
238 >            final CountDownLatch done = new CountDownLatch(1);
239 >            final Runnable task = new CheckedRunnable() {
240 >                public void realRun() { done.countDown(); }};
241              p.execute(task);
242 <            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
208 <        } finally {
209 <            joinPool(p);
242 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
243          }
244      }
245  
# Line 221 | 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 231 | Line 264 | public class ThreadPoolExecutorSubclassT
264                  }});
265              assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
266              assertEquals(1, p.getActiveCount());
234        } finally {
267              done.countDown();
236            joinPool(p);
268          }
269      }
270  
# Line 286 | Line 317 | public class ThreadPoolExecutorSubclassT
317                      threadProceed.await();
318                      threadDone.countDown();
319                  }});
320 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
320 >            await(threadStarted);
321              assertEquals(0, p.getCompletedTaskCount());
322              threadProceed.countDown();
323              threadDone.await();
324 <            delay(SHORT_DELAY_MS);
325 <            assertEquals(1, p.getCompletedTaskCount());
324 >            long startTime = System.nanoTime();
325 >            while (p.getCompletedTaskCount() != 1) {
326 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
327 >                    fail("timed out");
328 >                Thread.yield();
329 >            }
330          } finally {
331              joinPool(p);
332          }
# Line 311 | Line 346 | public class ThreadPoolExecutorSubclassT
346       */
347      public void testGetKeepAliveTime() {
348          ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
349 <        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
349 >        assertEquals(1, p.getKeepAliveTime(SECONDS));
350          joinPool(p);
351      }
352  
318
353      /**
354       * getThreadFactory returns factory in constructor if not set
355       */
# Line 337 | Line 371 | public class ThreadPoolExecutorSubclassT
371          joinPool(p);
372      }
373  
340
374      /**
375       * setThreadFactory(null) throws NPE
376       */
# Line 374 | Line 407 | public class ThreadPoolExecutorSubclassT
407          joinPool(p);
408      }
409  
377
410      /**
411       * setRejectedExecutionHandler(null) throws NPE
412       */
# Line 389 | Line 421 | public class ThreadPoolExecutorSubclassT
421          }
422      }
423  
392
424      /**
425       * getLargestPoolSize increases, but doesn't overestimate, when
426       * multiple threads active
# Line 484 | Line 515 | public class ThreadPoolExecutorSubclassT
515      }
516  
517      /**
518 <     * isShutDown is false before shutdown, true after
518 >     * isShutdown is false before shutdown, true after
519       */
520      public void testIsShutdown() {
521  
# Line 495 | Line 526 | public class ThreadPoolExecutorSubclassT
526          joinPool(p);
527      }
528  
498
529      /**
530       * isTerminated is false before termination, true after
531       */
# Line 669 | Line 699 | public class ThreadPoolExecutorSubclassT
699      }
700  
701      /**
702 <     * shutDownNow returns a list containing tasks that were not run
702 >     * shutdownNow returns a list containing tasks that were not run,
703 >     * and those tasks are drained from the queue
704       */
705 <    public void testShutDownNow() {
706 <        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
707 <        List l;
708 <        try {
709 <            for (int i = 0; i < 5; i++)
710 <                p.execute(new MediumPossiblyInterruptedRunnable());
711 <        }
712 <        finally {
705 >    public void testShutdownNow() throws InterruptedException {
706 >        final int poolSize = 2;
707 >        final int count = 5;
708 >        final AtomicInteger ran = new AtomicInteger(0);
709 >        ThreadPoolExecutor p =
710 >            new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS,
711 >                          new ArrayBlockingQueue<Runnable>(10));
712 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
713 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
714 >            threadsStarted.countDown();
715              try {
716 <                l = p.shutdownNow();
717 <            } catch (SecurityException ok) { return; }
716 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
717 >            } catch (InterruptedException success) {}
718 >            ran.getAndIncrement();
719 >        }};
720 >        for (int i = 0; i < count; i++)
721 >            p.execute(waiter);
722 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
723 >        assertEquals(poolSize, p.getActiveCount());
724 >        assertEquals(0, p.getCompletedTaskCount());
725 >        final List<Runnable> queuedTasks;
726 >        try {
727 >            queuedTasks = p.shutdownNow();
728 >        } catch (SecurityException ok) {
729 >            return; // Allowed in case test doesn't have privs
730          }
731          assertTrue(p.isShutdown());
732 <        assertTrue(l.size() <= 4);
732 >        assertTrue(p.getQueue().isEmpty());
733 >        assertEquals(count - poolSize, queuedTasks.size());
734 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
735 >        assertTrue(p.isTerminated());
736 >        assertEquals(poolSize, ran.get());
737 >        assertEquals(poolSize, p.getCompletedTaskCount());
738      }
739  
740      // Exception Tests
741  
692
742      /**
743       * Constructor throws if corePoolSize argument is less than zero
744       */
745      public void testConstructor1() {
746          try {
747 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
747 >            new CustomTPE(-1, 1, 1L, SECONDS,
748 >                          new ArrayBlockingQueue<Runnable>(10));
749              shouldThrow();
750          } catch (IllegalArgumentException success) {}
751      }
# Line 705 | Line 755 | public class ThreadPoolExecutorSubclassT
755       */
756      public void testConstructor2() {
757          try {
758 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
758 >            new CustomTPE(1, -1, 1L, SECONDS,
759 >                          new ArrayBlockingQueue<Runnable>(10));
760              shouldThrow();
761          } catch (IllegalArgumentException success) {}
762      }
# Line 715 | Line 766 | public class ThreadPoolExecutorSubclassT
766       */
767      public void testConstructor3() {
768          try {
769 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
769 >            new CustomTPE(1, 0, 1L, SECONDS,
770 >                          new ArrayBlockingQueue<Runnable>(10));
771              shouldThrow();
772          } catch (IllegalArgumentException success) {}
773      }
# Line 725 | Line 777 | public class ThreadPoolExecutorSubclassT
777       */
778      public void testConstructor4() {
779          try {
780 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
780 >            new CustomTPE(1, 2, -1L, SECONDS,
781 >                          new ArrayBlockingQueue<Runnable>(10));
782              shouldThrow();
783          } catch (IllegalArgumentException success) {}
784      }
# Line 735 | Line 788 | public class ThreadPoolExecutorSubclassT
788       */
789      public void testConstructor5() {
790          try {
791 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
791 >            new CustomTPE(2, 1, 1L, SECONDS,
792 >                          new ArrayBlockingQueue<Runnable>(10));
793              shouldThrow();
794          } catch (IllegalArgumentException success) {}
795      }
# Line 745 | Line 799 | public class ThreadPoolExecutorSubclassT
799       */
800      public void testConstructorNullPointerException() {
801          try {
802 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
802 >            new CustomTPE(1, 2, 1L, SECONDS, null);
803              shouldThrow();
804          } catch (NullPointerException success) {}
805      }
806  
753
754
807      /**
808       * Constructor throws if corePoolSize argument is less than zero
809       */
810      public void testConstructor6() {
811          try {
812 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
812 >            new CustomTPE(-1, 1, 1L, SECONDS,
813 >                          new ArrayBlockingQueue<Runnable>(10),
814 >                          new SimpleThreadFactory());
815              shouldThrow();
816          } catch (IllegalArgumentException success) {}
817      }
# Line 767 | Line 821 | public class ThreadPoolExecutorSubclassT
821       */
822      public void testConstructor7() {
823          try {
824 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
824 >            new CustomTPE(1,-1, 1L, SECONDS,
825 >                          new ArrayBlockingQueue<Runnable>(10),
826 >                          new SimpleThreadFactory());
827              shouldThrow();
828          } catch (IllegalArgumentException success) {}
829      }
# Line 777 | Line 833 | public class ThreadPoolExecutorSubclassT
833       */
834      public void testConstructor8() {
835          try {
836 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
836 >            new CustomTPE(1, 0, 1L, SECONDS,
837 >                          new ArrayBlockingQueue<Runnable>(10),
838 >                          new SimpleThreadFactory());
839              shouldThrow();
840          } catch (IllegalArgumentException success) {}
841      }
# Line 787 | Line 845 | public class ThreadPoolExecutorSubclassT
845       */
846      public void testConstructor9() {
847          try {
848 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
848 >            new CustomTPE(1, 2, -1L, SECONDS,
849 >                          new ArrayBlockingQueue<Runnable>(10),
850 >                          new SimpleThreadFactory());
851              shouldThrow();
852          } catch (IllegalArgumentException success) {}
853      }
# Line 797 | Line 857 | public class ThreadPoolExecutorSubclassT
857       */
858      public void testConstructor10() {
859          try {
860 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
860 >            new CustomTPE(2, 1, 1L, SECONDS,
861 >                          new ArrayBlockingQueue<Runnable>(10),
862 >                          new SimpleThreadFactory());
863              shouldThrow();
864          } catch (IllegalArgumentException success) {}
865      }
# Line 807 | Line 869 | public class ThreadPoolExecutorSubclassT
869       */
870      public void testConstructorNullPointerException2() {
871          try {
872 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
872 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
873              shouldThrow();
874          } catch (NullPointerException success) {}
875      }
# Line 817 | Line 879 | public class ThreadPoolExecutorSubclassT
879       */
880      public void testConstructorNullPointerException3() {
881          try {
882 <            ThreadFactory f = null;
883 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
882 >            new CustomTPE(1, 2, 1L, SECONDS,
883 >                          new ArrayBlockingQueue<Runnable>(10),
884 >                          (ThreadFactory) null);
885              shouldThrow();
886          } catch (NullPointerException success) {}
887      }
888  
826
889      /**
890       * Constructor throws if corePoolSize argument is less than zero
891       */
892      public void testConstructor11() {
893          try {
894 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
894 >            new CustomTPE(-1, 1, 1L, SECONDS,
895 >                          new ArrayBlockingQueue<Runnable>(10),
896 >                          new NoOpREHandler());
897              shouldThrow();
898          } catch (IllegalArgumentException success) {}
899      }
# Line 839 | Line 903 | public class ThreadPoolExecutorSubclassT
903       */
904      public void testConstructor12() {
905          try {
906 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
906 >            new CustomTPE(1, -1, 1L, SECONDS,
907 >                          new ArrayBlockingQueue<Runnable>(10),
908 >                          new NoOpREHandler());
909              shouldThrow();
910          } catch (IllegalArgumentException success) {}
911      }
# Line 849 | Line 915 | public class ThreadPoolExecutorSubclassT
915       */
916      public void testConstructor13() {
917          try {
918 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
918 >            new CustomTPE(1, 0, 1L, SECONDS,
919 >                          new ArrayBlockingQueue<Runnable>(10),
920 >                          new NoOpREHandler());
921              shouldThrow();
922          } catch (IllegalArgumentException success) {}
923      }
# Line 859 | Line 927 | public class ThreadPoolExecutorSubclassT
927       */
928      public void testConstructor14() {
929          try {
930 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
930 >            new CustomTPE(1, 2, -1L, SECONDS,
931 >                          new ArrayBlockingQueue<Runnable>(10),
932 >                          new NoOpREHandler());
933              shouldThrow();
934          } catch (IllegalArgumentException success) {}
935      }
# Line 869 | Line 939 | public class ThreadPoolExecutorSubclassT
939       */
940      public void testConstructor15() {
941          try {
942 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
942 >            new CustomTPE(2, 1, 1L, SECONDS,
943 >                          new ArrayBlockingQueue<Runnable>(10),
944 >                          new NoOpREHandler());
945              shouldThrow();
946          } catch (IllegalArgumentException success) {}
947      }
# Line 879 | Line 951 | public class ThreadPoolExecutorSubclassT
951       */
952      public void testConstructorNullPointerException4() {
953          try {
954 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
954 >            new CustomTPE(1, 2, 1L, SECONDS,
955 >                          null,
956 >                          new NoOpREHandler());
957              shouldThrow();
958          } catch (NullPointerException success) {}
959      }
# Line 889 | Line 963 | public class ThreadPoolExecutorSubclassT
963       */
964      public void testConstructorNullPointerException5() {
965          try {
966 <            RejectedExecutionHandler r = null;
967 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
966 >            new CustomTPE(1, 2, 1L, SECONDS,
967 >                          new ArrayBlockingQueue<Runnable>(10),
968 >                          (RejectedExecutionHandler) null);
969              shouldThrow();
970          } catch (NullPointerException success) {}
971      }
972  
898
973      /**
974       * Constructor throws if corePoolSize argument is less than zero
975       */
976      public void testConstructor16() {
977          try {
978 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
978 >            new CustomTPE(-1, 1, 1L, SECONDS,
979 >                          new ArrayBlockingQueue<Runnable>(10),
980 >                          new SimpleThreadFactory(),
981 >                          new NoOpREHandler());
982              shouldThrow();
983          } catch (IllegalArgumentException success) {}
984      }
# Line 911 | Line 988 | public class ThreadPoolExecutorSubclassT
988       */
989      public void testConstructor17() {
990          try {
991 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
991 >            new CustomTPE(1, -1, 1L, SECONDS,
992 >                          new ArrayBlockingQueue<Runnable>(10),
993 >                          new SimpleThreadFactory(),
994 >                          new NoOpREHandler());
995              shouldThrow();
996          } catch (IllegalArgumentException success) {}
997      }
# Line 921 | Line 1001 | public class ThreadPoolExecutorSubclassT
1001       */
1002      public void testConstructor18() {
1003          try {
1004 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1004 >            new CustomTPE(1, 0, 1L, SECONDS,
1005 >                          new ArrayBlockingQueue<Runnable>(10),
1006 >                          new SimpleThreadFactory(),
1007 >                          new NoOpREHandler());
1008              shouldThrow();
1009          } catch (IllegalArgumentException success) {}
1010      }
# Line 931 | Line 1014 | public class ThreadPoolExecutorSubclassT
1014       */
1015      public void testConstructor19() {
1016          try {
1017 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1017 >            new CustomTPE(1, 2, -1L, SECONDS,
1018 >                          new ArrayBlockingQueue<Runnable>(10),
1019 >                          new SimpleThreadFactory(),
1020 >                          new NoOpREHandler());
1021              shouldThrow();
1022          } catch (IllegalArgumentException success) {}
1023      }
# Line 941 | Line 1027 | public class ThreadPoolExecutorSubclassT
1027       */
1028      public void testConstructor20() {
1029          try {
1030 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1030 >            new CustomTPE(2, 1, 1L, SECONDS,
1031 >                          new ArrayBlockingQueue<Runnable>(10),
1032 >                          new SimpleThreadFactory(),
1033 >                          new NoOpREHandler());
1034              shouldThrow();
1035          } catch (IllegalArgumentException success) {}
1036      }
# Line 951 | Line 1040 | public class ThreadPoolExecutorSubclassT
1040       */
1041      public void testConstructorNullPointerException6() {
1042          try {
1043 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1043 >            new CustomTPE(1, 2, 1L, SECONDS,
1044 >                          null,
1045 >                          new SimpleThreadFactory(),
1046 >                          new NoOpREHandler());
1047              shouldThrow();
1048          } catch (NullPointerException success) {}
1049      }
# Line 961 | Line 1053 | public class ThreadPoolExecutorSubclassT
1053       */
1054      public void testConstructorNullPointerException7() {
1055          try {
1056 <            RejectedExecutionHandler r = null;
1057 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
1056 >            new CustomTPE(1, 2, 1L, SECONDS,
1057 >                          new ArrayBlockingQueue<Runnable>(10),
1058 >                          new SimpleThreadFactory(),
1059 >                          (RejectedExecutionHandler) null);
1060              shouldThrow();
1061          } catch (NullPointerException success) {}
1062      }
# Line 972 | Line 1066 | public class ThreadPoolExecutorSubclassT
1066       */
1067      public void testConstructorNullPointerException8() {
1068          try {
1069 <            new CustomTPE(1, 2,
976 <                          LONG_DELAY_MS, MILLISECONDS,
1069 >            new CustomTPE(1, 2, 1L, SECONDS,
1070                            new ArrayBlockingQueue<Runnable>(10),
1071                            (ThreadFactory) null,
1072                            new NoOpREHandler());
# Line 981 | Line 1074 | public class ThreadPoolExecutorSubclassT
1074          } catch (NullPointerException success) {}
1075      }
1076  
984
1077      /**
1078       * execute throws RejectedExecutionException if saturated.
1079       */
# Line 1131 | Line 1223 | public class ThreadPoolExecutorSubclassT
1223          }
1224      }
1225  
1134
1226      /**
1227       * execute using DiscardOldestPolicy drops task on shutdown
1228       */
# Line 1149 | Line 1240 | public class ThreadPoolExecutorSubclassT
1240          }
1241      }
1242  
1152
1243      /**
1244       * execute(null) throws NPE
1245       */
1246      public void testExecuteNull() {
1247 <        ThreadPoolExecutor p = null;
1247 >        ThreadPoolExecutor p =
1248 >            new CustomTPE(1, 2, 1L, SECONDS,
1249 >                          new ArrayBlockingQueue<Runnable>(10));
1250          try {
1159            p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1251              p.execute(null);
1252              shouldThrow();
1253          } catch (NullPointerException success) {}
# Line 1214 | Line 1305 | public class ThreadPoolExecutorSubclassT
1305          joinPool(p);
1306      }
1307  
1217
1308      /**
1309       * setKeepAliveTime throws IllegalArgumentException
1310       * when given a negative value
# Line 1239 | Line 1329 | public class ThreadPoolExecutorSubclassT
1329      public void testTerminated() {
1330          CustomTPE p = new CustomTPE();
1331          try { p.shutdown(); } catch (SecurityException ok) { return; }
1332 <        assertTrue(p.terminatedCalled);
1332 >        assertTrue(p.terminatedCalled());
1333          joinPool(p);
1334      }
1335  
# Line 1249 | Line 1339 | public class ThreadPoolExecutorSubclassT
1339      public void testBeforeAfter() throws InterruptedException {
1340          CustomTPE p = new CustomTPE();
1341          try {
1342 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1343 <            p.execute(r);
1344 <            delay(SHORT_DELAY_MS);
1345 <            assertTrue(r.done);
1346 <            assertTrue(p.beforeCalled);
1347 <            assertTrue(p.afterCalled);
1342 >            final CountDownLatch done = new CountDownLatch(1);
1343 >            p.execute(new CheckedRunnable() {
1344 >                public void realRun() {
1345 >                    done.countDown();
1346 >                }});
1347 >            await(p.afterCalled);
1348 >            assertEquals(0, done.getCount());
1349 >            assertTrue(p.afterCalled());
1350 >            assertTrue(p.beforeCalled());
1351              try { p.shutdown(); } catch (SecurityException ok) { return; }
1352          } finally {
1353              joinPool(p);
# Line 1303 | Line 1396 | public class ThreadPoolExecutorSubclassT
1396          }
1397      }
1398  
1306
1399      /**
1400       * invokeAny(null) throws NPE
1401       */
# Line 1465 | Line 1557 | public class ThreadPoolExecutorSubclassT
1557          }
1558      }
1559  
1468
1469
1560      /**
1561       * timed invokeAny(null) throws NPE
1562       */
# Line 1653 | Line 1743 | public class ThreadPoolExecutorSubclassT
1743              l.add(new StringTask());
1744              l.add(new StringTask());
1745              List<Future<String>> futures =
1746 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1746 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1747              assertEquals(2, futures.size());
1748              for (Future<String> future : futures)
1749                  assertSame(TEST_STRING, future.get());
# Line 1668 | Line 1758 | public class ThreadPoolExecutorSubclassT
1758      public void testTimedInvokeAll6() throws Exception {
1759          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1760          try {
1761 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1762 <            l.add(new StringTask());
1763 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1764 <            l.add(new StringTask());
1765 <            List<Future<String>> futures =
1766 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1767 <            assertEquals(3, futures.size());
1768 <            Iterator<Future<String>> it = futures.iterator();
1769 <            Future<String> f1 = it.next();
1770 <            Future<String> f2 = it.next();
1771 <            Future<String> f3 = it.next();
1772 <            assertTrue(f1.isDone());
1773 <            assertTrue(f2.isDone());
1774 <            assertTrue(f3.isDone());
1775 <            assertFalse(f1.isCancelled());
1776 <            assertTrue(f2.isCancelled());
1761 >            for (long timeout = timeoutMillis();;) {
1762 >                List<Callable<String>> tasks = new ArrayList<>();
1763 >                tasks.add(new StringTask("0"));
1764 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1765 >                tasks.add(new StringTask("2"));
1766 >                long startTime = System.nanoTime();
1767 >                List<Future<String>> futures =
1768 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1769 >                assertEquals(tasks.size(), futures.size());
1770 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1771 >                for (Future future : futures)
1772 >                    assertTrue(future.isDone());
1773 >                assertTrue(futures.get(1).isCancelled());
1774 >                try {
1775 >                    assertEquals("0", futures.get(0).get());
1776 >                    assertEquals("2", futures.get(2).get());
1777 >                    break;
1778 >                } catch (CancellationException retryWithLongerTimeout) {
1779 >                    timeout *= 2;
1780 >                    if (timeout >= LONG_DELAY_MS / 2)
1781 >                        fail("expected exactly one task to be cancelled");
1782 >                }
1783 >            }
1784          } finally {
1785              joinPool(e);
1786          }
# Line 1726 | Line 1823 | public class ThreadPoolExecutorSubclassT
1823       * allowCoreThreadTimeOut(true) causes idle threads to time out
1824       */
1825      public void testAllowCoreThreadTimeOut_true() throws Exception {
1826 +        long keepAliveTime = timeoutMillis();
1827          final ThreadPoolExecutor p =
1828              new CustomTPE(2, 10,
1829 <                          SHORT_DELAY_MS, MILLISECONDS,
1829 >                          keepAliveTime, MILLISECONDS,
1830                            new ArrayBlockingQueue<Runnable>(10));
1831          final CountDownLatch threadStarted = new CountDownLatch(1);
1832          try {
1833              p.allowCoreThreadTimeOut(true);
1834              p.execute(new CheckedRunnable() {
1835 <                public void realRun() throws InterruptedException {
1835 >                public void realRun() {
1836                      threadStarted.countDown();
1837                      assertEquals(1, p.getPoolSize());
1838                  }});
1839 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1840 <            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
1841 <                if (p.getPoolSize() == 0)
1842 <                    break;
1843 <                delay(10);
1844 <            }
1839 >            await(threadStarted);
1840 >            delay(keepAliveTime);
1841 >            long startTime = System.nanoTime();
1842 >            while (p.getPoolSize() > 0
1843 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1844 >                Thread.yield();
1845 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1846              assertEquals(0, p.getPoolSize());
1847          } finally {
1848              joinPool(p);
# Line 1754 | Line 1853 | public class ThreadPoolExecutorSubclassT
1853       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1854       */
1855      public void testAllowCoreThreadTimeOut_false() throws Exception {
1856 +        long keepAliveTime = timeoutMillis();
1857          final ThreadPoolExecutor p =
1858              new CustomTPE(2, 10,
1859 <                          SHORT_DELAY_MS, MILLISECONDS,
1859 >                          keepAliveTime, MILLISECONDS,
1860                            new ArrayBlockingQueue<Runnable>(10));
1861          final CountDownLatch threadStarted = new CountDownLatch(1);
1862          try {
# Line 1766 | Line 1866 | public class ThreadPoolExecutorSubclassT
1866                      threadStarted.countDown();
1867                      assertTrue(p.getPoolSize() >= 1);
1868                  }});
1869 <            delay(SMALL_DELAY_MS);
1869 >            delay(2 * keepAliveTime);
1870              assertTrue(p.getPoolSize() >= 1);
1871          } finally {
1872              joinPool(p);
1873          }
1874      }
1875  
1876 +    /**
1877 +     * get(cancelled task) throws CancellationException
1878 +     * (in part, a test of CustomTPE itself)
1879 +     */
1880 +    public void testGet_cancelled() throws Exception {
1881 +        final ExecutorService e =
1882 +            new CustomTPE(1, 1,
1883 +                          LONG_DELAY_MS, MILLISECONDS,
1884 +                          new LinkedBlockingQueue<Runnable>());
1885 +        try {
1886 +            final CountDownLatch blockerStarted = new CountDownLatch(1);
1887 +            final CountDownLatch done = new CountDownLatch(1);
1888 +            final List<Future<?>> futures = new ArrayList<>();
1889 +            for (int i = 0; i < 2; i++) {
1890 +                Runnable r = new CheckedRunnable() { public void realRun()
1891 +                                                         throws Throwable {
1892 +                    blockerStarted.countDown();
1893 +                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
1894 +                }};
1895 +                futures.add(e.submit(r));
1896 +            }
1897 +            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
1898 +            for (Future<?> future : futures) future.cancel(false);
1899 +            for (Future<?> future : futures) {
1900 +                try {
1901 +                    future.get();
1902 +                    shouldThrow();
1903 +                } catch (CancellationException success) {}
1904 +                try {
1905 +                    future.get(LONG_DELAY_MS, MILLISECONDS);
1906 +                    shouldThrow();
1907 +                } catch (CancellationException success) {}
1908 +                assertTrue(future.isCancelled());
1909 +                assertTrue(future.isDone());
1910 +            }
1911 +            done.countDown();
1912 +        } finally {
1913 +            joinPool(e);
1914 +        }
1915 +    }
1916 +
1917   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines