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.50 by jsr166, Sun Oct 4 00:40:33 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<ThreadPoolExecutor> 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 286 | Line 319 | public class ThreadPoolExecutorSubclassT
319                      threadProceed.await();
320                      threadDone.countDown();
321                  }});
322 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
322 >            await(threadStarted);
323              assertEquals(0, p.getCompletedTaskCount());
324              threadProceed.countDown();
325              threadDone.await();
326 <            delay(SHORT_DELAY_MS);
327 <            assertEquals(1, p.getCompletedTaskCount());
326 >            long startTime = System.nanoTime();
327 >            while (p.getCompletedTaskCount() != 1) {
328 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
329 >                    fail("timed out");
330 >                Thread.yield();
331 >            }
332          } finally {
333              joinPool(p);
334          }
# Line 311 | Line 348 | public class ThreadPoolExecutorSubclassT
348       */
349      public void testGetKeepAliveTime() {
350          ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
351 <        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
351 >        assertEquals(1, p.getKeepAliveTime(SECONDS));
352          joinPool(p);
353      }
354  
318
355      /**
356       * getThreadFactory returns factory in constructor if not set
357       */
# Line 337 | Line 373 | public class ThreadPoolExecutorSubclassT
373          joinPool(p);
374      }
375  
340
376      /**
377       * setThreadFactory(null) throws NPE
378       */
# Line 374 | Line 409 | public class ThreadPoolExecutorSubclassT
409          joinPool(p);
410      }
411  
377
412      /**
413       * setRejectedExecutionHandler(null) throws NPE
414       */
# Line 389 | Line 423 | public class ThreadPoolExecutorSubclassT
423          }
424      }
425  
392
426      /**
427       * getLargestPoolSize increases, but doesn't overestimate, when
428       * multiple threads active
# Line 484 | Line 517 | public class ThreadPoolExecutorSubclassT
517      }
518  
519      /**
520 <     * isShutDown is false before shutdown, true after
520 >     * isShutdown is false before shutdown, true after
521       */
522      public void testIsShutdown() {
523  
# Line 495 | Line 528 | public class ThreadPoolExecutorSubclassT
528          joinPool(p);
529      }
530  
498
531      /**
532       * isTerminated is false before termination, true after
533       */
# Line 669 | Line 701 | public class ThreadPoolExecutorSubclassT
701      }
702  
703      /**
704 <     * shutDownNow returns a list containing tasks that were not run
704 >     * shutdownNow returns a list containing tasks that were not run,
705 >     * and those tasks are drained from the queue
706       */
707 <    public void testShutDownNow() {
708 <        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
709 <        List l;
710 <        try {
711 <            for (int i = 0; i < 5; i++)
712 <                p.execute(new MediumPossiblyInterruptedRunnable());
713 <        }
714 <        finally {
707 >    public void testShutdownNow() throws InterruptedException {
708 >        final int poolSize = 2;
709 >        final int count = 5;
710 >        final AtomicInteger ran = new AtomicInteger(0);
711 >        ThreadPoolExecutor p =
712 >            new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS,
713 >                          new ArrayBlockingQueue<Runnable>(10));
714 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
715 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
716 >            threadsStarted.countDown();
717              try {
718 <                l = p.shutdownNow();
719 <            } catch (SecurityException ok) { return; }
718 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
719 >            } catch (InterruptedException success) {}
720 >            ran.getAndIncrement();
721 >        }};
722 >        for (int i = 0; i < count; i++)
723 >            p.execute(waiter);
724 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
725 >        assertEquals(poolSize, p.getActiveCount());
726 >        assertEquals(0, p.getCompletedTaskCount());
727 >        final List<Runnable> queuedTasks;
728 >        try {
729 >            queuedTasks = p.shutdownNow();
730 >        } catch (SecurityException ok) {
731 >            return; // Allowed in case test doesn't have privs
732          }
733          assertTrue(p.isShutdown());
734 <        assertTrue(l.size() <= 4);
734 >        assertTrue(p.getQueue().isEmpty());
735 >        assertEquals(count - poolSize, queuedTasks.size());
736 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
737 >        assertTrue(p.isTerminated());
738 >        assertEquals(poolSize, ran.get());
739 >        assertEquals(poolSize, p.getCompletedTaskCount());
740      }
741  
742      // Exception Tests
743  
692
744      /**
745       * Constructor throws if corePoolSize argument is less than zero
746       */
747      public void testConstructor1() {
748          try {
749 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
749 >            new CustomTPE(-1, 1, 1L, SECONDS,
750 >                          new ArrayBlockingQueue<Runnable>(10));
751              shouldThrow();
752          } catch (IllegalArgumentException success) {}
753      }
# Line 705 | Line 757 | public class ThreadPoolExecutorSubclassT
757       */
758      public void testConstructor2() {
759          try {
760 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
760 >            new CustomTPE(1, -1, 1L, SECONDS,
761 >                          new ArrayBlockingQueue<Runnable>(10));
762              shouldThrow();
763          } catch (IllegalArgumentException success) {}
764      }
# Line 715 | Line 768 | public class ThreadPoolExecutorSubclassT
768       */
769      public void testConstructor3() {
770          try {
771 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
771 >            new CustomTPE(1, 0, 1L, SECONDS,
772 >                          new ArrayBlockingQueue<Runnable>(10));
773              shouldThrow();
774          } catch (IllegalArgumentException success) {}
775      }
# Line 725 | Line 779 | public class ThreadPoolExecutorSubclassT
779       */
780      public void testConstructor4() {
781          try {
782 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
782 >            new CustomTPE(1, 2, -1L, SECONDS,
783 >                          new ArrayBlockingQueue<Runnable>(10));
784              shouldThrow();
785          } catch (IllegalArgumentException success) {}
786      }
# Line 735 | Line 790 | public class ThreadPoolExecutorSubclassT
790       */
791      public void testConstructor5() {
792          try {
793 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
793 >            new CustomTPE(2, 1, 1L, SECONDS,
794 >                          new ArrayBlockingQueue<Runnable>(10));
795              shouldThrow();
796          } catch (IllegalArgumentException success) {}
797      }
# Line 745 | Line 801 | public class ThreadPoolExecutorSubclassT
801       */
802      public void testConstructorNullPointerException() {
803          try {
804 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
804 >            new CustomTPE(1, 2, 1L, SECONDS, null);
805              shouldThrow();
806          } catch (NullPointerException success) {}
807      }
808  
753
754
809      /**
810       * Constructor throws if corePoolSize argument is less than zero
811       */
812      public void testConstructor6() {
813          try {
814 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
814 >            new CustomTPE(-1, 1, 1L, SECONDS,
815 >                          new ArrayBlockingQueue<Runnable>(10),
816 >                          new SimpleThreadFactory());
817              shouldThrow();
818          } catch (IllegalArgumentException success) {}
819      }
# Line 767 | Line 823 | public class ThreadPoolExecutorSubclassT
823       */
824      public void testConstructor7() {
825          try {
826 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
826 >            new CustomTPE(1,-1, 1L, SECONDS,
827 >                          new ArrayBlockingQueue<Runnable>(10),
828 >                          new SimpleThreadFactory());
829              shouldThrow();
830          } catch (IllegalArgumentException success) {}
831      }
# Line 777 | Line 835 | public class ThreadPoolExecutorSubclassT
835       */
836      public void testConstructor8() {
837          try {
838 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
838 >            new CustomTPE(1, 0, 1L, SECONDS,
839 >                          new ArrayBlockingQueue<Runnable>(10),
840 >                          new SimpleThreadFactory());
841              shouldThrow();
842          } catch (IllegalArgumentException success) {}
843      }
# Line 787 | Line 847 | public class ThreadPoolExecutorSubclassT
847       */
848      public void testConstructor9() {
849          try {
850 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
850 >            new CustomTPE(1, 2, -1L, SECONDS,
851 >                          new ArrayBlockingQueue<Runnable>(10),
852 >                          new SimpleThreadFactory());
853              shouldThrow();
854          } catch (IllegalArgumentException success) {}
855      }
# Line 797 | Line 859 | public class ThreadPoolExecutorSubclassT
859       */
860      public void testConstructor10() {
861          try {
862 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
862 >            new CustomTPE(2, 1, 1L, SECONDS,
863 >                          new ArrayBlockingQueue<Runnable>(10),
864 >                          new SimpleThreadFactory());
865              shouldThrow();
866          } catch (IllegalArgumentException success) {}
867      }
# Line 807 | Line 871 | public class ThreadPoolExecutorSubclassT
871       */
872      public void testConstructorNullPointerException2() {
873          try {
874 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
874 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
875              shouldThrow();
876          } catch (NullPointerException success) {}
877      }
# Line 817 | Line 881 | public class ThreadPoolExecutorSubclassT
881       */
882      public void testConstructorNullPointerException3() {
883          try {
884 <            ThreadFactory f = null;
885 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
884 >            new CustomTPE(1, 2, 1L, SECONDS,
885 >                          new ArrayBlockingQueue<Runnable>(10),
886 >                          (ThreadFactory) null);
887              shouldThrow();
888          } catch (NullPointerException success) {}
889      }
890  
826
891      /**
892       * Constructor throws if corePoolSize argument is less than zero
893       */
894      public void testConstructor11() {
895          try {
896 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
896 >            new CustomTPE(-1, 1, 1L, SECONDS,
897 >                          new ArrayBlockingQueue<Runnable>(10),
898 >                          new NoOpREHandler());
899              shouldThrow();
900          } catch (IllegalArgumentException success) {}
901      }
# Line 839 | Line 905 | public class ThreadPoolExecutorSubclassT
905       */
906      public void testConstructor12() {
907          try {
908 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
908 >            new CustomTPE(1, -1, 1L, SECONDS,
909 >                          new ArrayBlockingQueue<Runnable>(10),
910 >                          new NoOpREHandler());
911              shouldThrow();
912          } catch (IllegalArgumentException success) {}
913      }
# Line 849 | Line 917 | public class ThreadPoolExecutorSubclassT
917       */
918      public void testConstructor13() {
919          try {
920 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
920 >            new CustomTPE(1, 0, 1L, SECONDS,
921 >                          new ArrayBlockingQueue<Runnable>(10),
922 >                          new NoOpREHandler());
923              shouldThrow();
924          } catch (IllegalArgumentException success) {}
925      }
# Line 859 | Line 929 | public class ThreadPoolExecutorSubclassT
929       */
930      public void testConstructor14() {
931          try {
932 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
932 >            new CustomTPE(1, 2, -1L, SECONDS,
933 >                          new ArrayBlockingQueue<Runnable>(10),
934 >                          new NoOpREHandler());
935              shouldThrow();
936          } catch (IllegalArgumentException success) {}
937      }
# Line 869 | Line 941 | public class ThreadPoolExecutorSubclassT
941       */
942      public void testConstructor15() {
943          try {
944 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
944 >            new CustomTPE(2, 1, 1L, SECONDS,
945 >                          new ArrayBlockingQueue<Runnable>(10),
946 >                          new NoOpREHandler());
947              shouldThrow();
948          } catch (IllegalArgumentException success) {}
949      }
# Line 879 | Line 953 | public class ThreadPoolExecutorSubclassT
953       */
954      public void testConstructorNullPointerException4() {
955          try {
956 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
956 >            new CustomTPE(1, 2, 1L, SECONDS,
957 >                          null,
958 >                          new NoOpREHandler());
959              shouldThrow();
960          } catch (NullPointerException success) {}
961      }
# Line 889 | Line 965 | public class ThreadPoolExecutorSubclassT
965       */
966      public void testConstructorNullPointerException5() {
967          try {
968 <            RejectedExecutionHandler r = null;
969 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
968 >            new CustomTPE(1, 2, 1L, SECONDS,
969 >                          new ArrayBlockingQueue<Runnable>(10),
970 >                          (RejectedExecutionHandler) null);
971              shouldThrow();
972          } catch (NullPointerException success) {}
973      }
974  
898
975      /**
976       * Constructor throws if corePoolSize argument is less than zero
977       */
978      public void testConstructor16() {
979          try {
980 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
980 >            new CustomTPE(-1, 1, 1L, SECONDS,
981 >                          new ArrayBlockingQueue<Runnable>(10),
982 >                          new SimpleThreadFactory(),
983 >                          new NoOpREHandler());
984              shouldThrow();
985          } catch (IllegalArgumentException success) {}
986      }
# Line 911 | Line 990 | public class ThreadPoolExecutorSubclassT
990       */
991      public void testConstructor17() {
992          try {
993 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
993 >            new CustomTPE(1, -1, 1L, SECONDS,
994 >                          new ArrayBlockingQueue<Runnable>(10),
995 >                          new SimpleThreadFactory(),
996 >                          new NoOpREHandler());
997              shouldThrow();
998          } catch (IllegalArgumentException success) {}
999      }
# Line 921 | Line 1003 | public class ThreadPoolExecutorSubclassT
1003       */
1004      public void testConstructor18() {
1005          try {
1006 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1006 >            new CustomTPE(1, 0, 1L, SECONDS,
1007 >                          new ArrayBlockingQueue<Runnable>(10),
1008 >                          new SimpleThreadFactory(),
1009 >                          new NoOpREHandler());
1010              shouldThrow();
1011          } catch (IllegalArgumentException success) {}
1012      }
# Line 931 | Line 1016 | public class ThreadPoolExecutorSubclassT
1016       */
1017      public void testConstructor19() {
1018          try {
1019 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1019 >            new CustomTPE(1, 2, -1L, SECONDS,
1020 >                          new ArrayBlockingQueue<Runnable>(10),
1021 >                          new SimpleThreadFactory(),
1022 >                          new NoOpREHandler());
1023              shouldThrow();
1024          } catch (IllegalArgumentException success) {}
1025      }
# Line 941 | Line 1029 | public class ThreadPoolExecutorSubclassT
1029       */
1030      public void testConstructor20() {
1031          try {
1032 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1032 >            new CustomTPE(2, 1, 1L, SECONDS,
1033 >                          new ArrayBlockingQueue<Runnable>(10),
1034 >                          new SimpleThreadFactory(),
1035 >                          new NoOpREHandler());
1036              shouldThrow();
1037          } catch (IllegalArgumentException success) {}
1038      }
# Line 951 | Line 1042 | public class ThreadPoolExecutorSubclassT
1042       */
1043      public void testConstructorNullPointerException6() {
1044          try {
1045 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1045 >            new CustomTPE(1, 2, 1L, SECONDS,
1046 >                          null,
1047 >                          new SimpleThreadFactory(),
1048 >                          new NoOpREHandler());
1049              shouldThrow();
1050          } catch (NullPointerException success) {}
1051      }
# Line 961 | Line 1055 | public class ThreadPoolExecutorSubclassT
1055       */
1056      public void testConstructorNullPointerException7() {
1057          try {
1058 <            RejectedExecutionHandler r = null;
1059 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
1058 >            new CustomTPE(1, 2, 1L, SECONDS,
1059 >                          new ArrayBlockingQueue<Runnable>(10),
1060 >                          new SimpleThreadFactory(),
1061 >                          (RejectedExecutionHandler) null);
1062              shouldThrow();
1063          } catch (NullPointerException success) {}
1064      }
# Line 972 | Line 1068 | public class ThreadPoolExecutorSubclassT
1068       */
1069      public void testConstructorNullPointerException8() {
1070          try {
1071 <            new CustomTPE(1, 2,
976 <                          LONG_DELAY_MS, MILLISECONDS,
1071 >            new CustomTPE(1, 2, 1L, SECONDS,
1072                            new ArrayBlockingQueue<Runnable>(10),
1073                            (ThreadFactory) null,
1074                            new NoOpREHandler());
# Line 981 | Line 1076 | public class ThreadPoolExecutorSubclassT
1076          } catch (NullPointerException success) {}
1077      }
1078  
984
1079      /**
1080       * execute throws RejectedExecutionException if saturated.
1081       */
# Line 1131 | Line 1225 | public class ThreadPoolExecutorSubclassT
1225          }
1226      }
1227  
1134
1228      /**
1229       * execute using DiscardOldestPolicy drops task on shutdown
1230       */
# Line 1149 | Line 1242 | public class ThreadPoolExecutorSubclassT
1242          }
1243      }
1244  
1152
1245      /**
1246       * execute(null) throws NPE
1247       */
1248      public void testExecuteNull() {
1249 <        ThreadPoolExecutor p = null;
1249 >        ThreadPoolExecutor p =
1250 >            new CustomTPE(1, 2, 1L, SECONDS,
1251 >                          new ArrayBlockingQueue<Runnable>(10));
1252          try {
1159            p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1253              p.execute(null);
1254              shouldThrow();
1255          } catch (NullPointerException success) {}
# Line 1214 | Line 1307 | public class ThreadPoolExecutorSubclassT
1307          joinPool(p);
1308      }
1309  
1217
1310      /**
1311       * setKeepAliveTime throws IllegalArgumentException
1312       * when given a negative value
# Line 1239 | Line 1331 | public class ThreadPoolExecutorSubclassT
1331      public void testTerminated() {
1332          CustomTPE p = new CustomTPE();
1333          try { p.shutdown(); } catch (SecurityException ok) { return; }
1334 <        assertTrue(p.terminatedCalled);
1334 >        assertTrue(p.terminatedCalled());
1335          joinPool(p);
1336      }
1337  
# Line 1249 | Line 1341 | public class ThreadPoolExecutorSubclassT
1341      public void testBeforeAfter() throws InterruptedException {
1342          CustomTPE p = new CustomTPE();
1343          try {
1344 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1345 <            p.execute(r);
1346 <            delay(SHORT_DELAY_MS);
1347 <            assertTrue(r.done);
1348 <            assertTrue(p.beforeCalled);
1349 <            assertTrue(p.afterCalled);
1344 >            final CountDownLatch done = new CountDownLatch(1);
1345 >            p.execute(new CheckedRunnable() {
1346 >                public void realRun() {
1347 >                    done.countDown();
1348 >                }});
1349 >            await(p.afterCalled);
1350 >            assertEquals(0, done.getCount());
1351 >            assertTrue(p.afterCalled());
1352 >            assertTrue(p.beforeCalled());
1353              try { p.shutdown(); } catch (SecurityException ok) { return; }
1354          } finally {
1355              joinPool(p);
# Line 1303 | Line 1398 | public class ThreadPoolExecutorSubclassT
1398          }
1399      }
1400  
1306
1401      /**
1402       * invokeAny(null) throws NPE
1403       */
# Line 1465 | Line 1559 | public class ThreadPoolExecutorSubclassT
1559          }
1560      }
1561  
1468
1469
1562      /**
1563       * timed invokeAny(null) throws NPE
1564       */
# Line 1653 | Line 1745 | public class ThreadPoolExecutorSubclassT
1745              l.add(new StringTask());
1746              l.add(new StringTask());
1747              List<Future<String>> futures =
1748 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1748 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1749              assertEquals(2, futures.size());
1750              for (Future<String> future : futures)
1751                  assertSame(TEST_STRING, future.get());
# Line 1668 | Line 1760 | public class ThreadPoolExecutorSubclassT
1760      public void testTimedInvokeAll6() throws Exception {
1761          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1762          try {
1763 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1764 <            l.add(new StringTask());
1765 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1766 <            l.add(new StringTask());
1767 <            List<Future<String>> futures =
1768 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1769 <            assertEquals(3, futures.size());
1770 <            Iterator<Future<String>> it = futures.iterator();
1771 <            Future<String> f1 = it.next();
1772 <            Future<String> f2 = it.next();
1773 <            Future<String> f3 = it.next();
1774 <            assertTrue(f1.isDone());
1775 <            assertTrue(f2.isDone());
1776 <            assertTrue(f3.isDone());
1777 <            assertFalse(f1.isCancelled());
1778 <            assertTrue(f2.isCancelled());
1763 >            for (long timeout = timeoutMillis();;) {
1764 >                List<Callable<String>> tasks = new ArrayList<>();
1765 >                tasks.add(new StringTask("0"));
1766 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1767 >                tasks.add(new StringTask("2"));
1768 >                long startTime = System.nanoTime();
1769 >                List<Future<String>> futures =
1770 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1771 >                assertEquals(tasks.size(), futures.size());
1772 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1773 >                for (Future future : futures)
1774 >                    assertTrue(future.isDone());
1775 >                assertTrue(futures.get(1).isCancelled());
1776 >                try {
1777 >                    assertEquals("0", futures.get(0).get());
1778 >                    assertEquals("2", futures.get(2).get());
1779 >                    break;
1780 >                } catch (CancellationException retryWithLongerTimeout) {
1781 >                    timeout *= 2;
1782 >                    if (timeout >= LONG_DELAY_MS / 2)
1783 >                        fail("expected exactly one task to be cancelled");
1784 >                }
1785 >            }
1786          } finally {
1787              joinPool(e);
1788          }
# Line 1726 | Line 1825 | public class ThreadPoolExecutorSubclassT
1825       * allowCoreThreadTimeOut(true) causes idle threads to time out
1826       */
1827      public void testAllowCoreThreadTimeOut_true() throws Exception {
1828 +        long keepAliveTime = timeoutMillis();
1829          final ThreadPoolExecutor p =
1830              new CustomTPE(2, 10,
1831 <                          SHORT_DELAY_MS, MILLISECONDS,
1831 >                          keepAliveTime, MILLISECONDS,
1832                            new ArrayBlockingQueue<Runnable>(10));
1833          final CountDownLatch threadStarted = new CountDownLatch(1);
1834          try {
1835              p.allowCoreThreadTimeOut(true);
1836              p.execute(new CheckedRunnable() {
1837 <                public void realRun() throws InterruptedException {
1837 >                public void realRun() {
1838                      threadStarted.countDown();
1839                      assertEquals(1, p.getPoolSize());
1840                  }});
1841 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1842 <            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
1843 <                if (p.getPoolSize() == 0)
1844 <                    break;
1845 <                delay(10);
1846 <            }
1841 >            await(threadStarted);
1842 >            delay(keepAliveTime);
1843 >            long startTime = System.nanoTime();
1844 >            while (p.getPoolSize() > 0
1845 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1846 >                Thread.yield();
1847 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1848              assertEquals(0, p.getPoolSize());
1849          } finally {
1850              joinPool(p);
# Line 1754 | Line 1855 | public class ThreadPoolExecutorSubclassT
1855       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1856       */
1857      public void testAllowCoreThreadTimeOut_false() throws Exception {
1858 +        long keepAliveTime = timeoutMillis();
1859          final ThreadPoolExecutor p =
1860              new CustomTPE(2, 10,
1861 <                          SHORT_DELAY_MS, MILLISECONDS,
1861 >                          keepAliveTime, MILLISECONDS,
1862                            new ArrayBlockingQueue<Runnable>(10));
1863          final CountDownLatch threadStarted = new CountDownLatch(1);
1864          try {
# Line 1766 | Line 1868 | public class ThreadPoolExecutorSubclassT
1868                      threadStarted.countDown();
1869                      assertTrue(p.getPoolSize() >= 1);
1870                  }});
1871 <            delay(SMALL_DELAY_MS);
1871 >            delay(2 * keepAliveTime);
1872              assertTrue(p.getPoolSize() >= 1);
1873          } finally {
1874              joinPool(p);
1875          }
1876      }
1877  
1878 +    /**
1879 +     * get(cancelled task) throws CancellationException
1880 +     * (in part, a test of CustomTPE itself)
1881 +     */
1882 +    public void testGet_cancelled() throws Exception {
1883 +        final ExecutorService e =
1884 +            new CustomTPE(1, 1,
1885 +                          LONG_DELAY_MS, MILLISECONDS,
1886 +                          new LinkedBlockingQueue<Runnable>());
1887 +        try {
1888 +            final CountDownLatch blockerStarted = new CountDownLatch(1);
1889 +            final CountDownLatch done = new CountDownLatch(1);
1890 +            final List<Future<?>> futures = new ArrayList<>();
1891 +            for (int i = 0; i < 2; i++) {
1892 +                Runnable r = new CheckedRunnable() { public void realRun()
1893 +                                                         throws Throwable {
1894 +                    blockerStarted.countDown();
1895 +                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
1896 +                }};
1897 +                futures.add(e.submit(r));
1898 +            }
1899 +            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
1900 +            for (Future<?> future : futures) future.cancel(false);
1901 +            for (Future<?> future : futures) {
1902 +                try {
1903 +                    future.get();
1904 +                    shouldThrow();
1905 +                } catch (CancellationException success) {}
1906 +                try {
1907 +                    future.get(LONG_DELAY_MS, MILLISECONDS);
1908 +                    shouldThrow();
1909 +                } catch (CancellationException success) {}
1910 +                assertTrue(future.isCancelled());
1911 +                assertTrue(future.isDone());
1912 +            }
1913 +            done.countDown();
1914 +        } finally {
1915 +            joinPool(e);
1916 +        }
1917 +    }
1918 +
1919   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines