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.32 by jsr166, Wed Sep 25 07:39:17 2013 UTC vs.
Revision 1.47 by jsr166, Sat Oct 3 18:17:51 2015 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import junit.framework.*;
10 import java.util.concurrent.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 + import static java.util.concurrent.TimeUnit.SECONDS;
11 +
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 < import java.util.*;
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 323 | Line 352 | public class ThreadPoolExecutorSubclassT
352       */
353      public void testGetKeepAliveTime() {
354          ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
355 <        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
355 >        assertEquals(1, p.getKeepAliveTime(SECONDS));
356          joinPool(p);
357      }
358  
# Line 676 | Line 705 | public class ThreadPoolExecutorSubclassT
705      }
706  
707      /**
708 <     * shutdownNow returns a list containing tasks that were not run
708 >     * shutdownNow returns a list containing tasks that were not run,
709 >     * and those tasks are drained from the queue
710       */
711 <    public void testShutdownNow() {
712 <        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
713 <        List l;
714 <        try {
715 <            for (int i = 0; i < 5; i++)
716 <                p.execute(new MediumPossiblyInterruptedRunnable());
717 <        }
718 <        finally {
711 >    public void testShutdownNow() throws InterruptedException {
712 >        final int poolSize = 2;
713 >        final int count = 5;
714 >        final AtomicInteger ran = new AtomicInteger(0);
715 >        ThreadPoolExecutor p =
716 >            new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS,
717 >                          new ArrayBlockingQueue<Runnable>(10));
718 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
719 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
720 >            threadsStarted.countDown();
721              try {
722 <                l = p.shutdownNow();
723 <            } catch (SecurityException ok) { return; }
722 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
723 >            } catch (InterruptedException success) {}
724 >            ran.getAndIncrement();
725 >        }};
726 >        for (int i = 0; i < count; i++)
727 >            p.execute(waiter);
728 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
729 >        assertEquals(poolSize, p.getActiveCount());
730 >        assertEquals(0, p.getCompletedTaskCount());
731 >        final List<Runnable> queuedTasks;
732 >        try {
733 >            queuedTasks = p.shutdownNow();
734 >        } catch (SecurityException ok) {
735 >            return; // Allowed in case test doesn't have privs
736          }
737          assertTrue(p.isShutdown());
738 <        assertTrue(l.size() <= 4);
738 >        assertTrue(p.getQueue().isEmpty());
739 >        assertEquals(count - poolSize, queuedTasks.size());
740 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
741 >        assertTrue(p.isTerminated());
742 >        assertEquals(poolSize, ran.get());
743 >        assertEquals(poolSize, p.getCompletedTaskCount());
744      }
745  
746      // Exception Tests
# Line 701 | Line 750 | public class ThreadPoolExecutorSubclassT
750       */
751      public void testConstructor1() {
752          try {
753 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
753 >            new CustomTPE(-1, 1, 1L, SECONDS,
754 >                          new ArrayBlockingQueue<Runnable>(10));
755              shouldThrow();
756          } catch (IllegalArgumentException success) {}
757      }
# Line 711 | Line 761 | public class ThreadPoolExecutorSubclassT
761       */
762      public void testConstructor2() {
763          try {
764 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
764 >            new CustomTPE(1, -1, 1L, SECONDS,
765 >                          new ArrayBlockingQueue<Runnable>(10));
766              shouldThrow();
767          } catch (IllegalArgumentException success) {}
768      }
# Line 721 | Line 772 | public class ThreadPoolExecutorSubclassT
772       */
773      public void testConstructor3() {
774          try {
775 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
775 >            new CustomTPE(1, 0, 1L, SECONDS,
776 >                          new ArrayBlockingQueue<Runnable>(10));
777              shouldThrow();
778          } catch (IllegalArgumentException success) {}
779      }
# Line 731 | Line 783 | public class ThreadPoolExecutorSubclassT
783       */
784      public void testConstructor4() {
785          try {
786 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
786 >            new CustomTPE(1, 2, -1L, SECONDS,
787 >                          new ArrayBlockingQueue<Runnable>(10));
788              shouldThrow();
789          } catch (IllegalArgumentException success) {}
790      }
# Line 741 | Line 794 | public class ThreadPoolExecutorSubclassT
794       */
795      public void testConstructor5() {
796          try {
797 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
797 >            new CustomTPE(2, 1, 1L, SECONDS,
798 >                          new ArrayBlockingQueue<Runnable>(10));
799              shouldThrow();
800          } catch (IllegalArgumentException success) {}
801      }
# Line 751 | Line 805 | public class ThreadPoolExecutorSubclassT
805       */
806      public void testConstructorNullPointerException() {
807          try {
808 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null);
808 >            new CustomTPE(1, 2, 1L, SECONDS, null);
809              shouldThrow();
810          } catch (NullPointerException success) {}
811      }
# Line 761 | Line 815 | public class ThreadPoolExecutorSubclassT
815       */
816      public void testConstructor6() {
817          try {
818 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
818 >            new CustomTPE(-1, 1, 1L, SECONDS,
819 >                          new ArrayBlockingQueue<Runnable>(10),
820 >                          new SimpleThreadFactory());
821              shouldThrow();
822          } catch (IllegalArgumentException success) {}
823      }
# Line 771 | Line 827 | public class ThreadPoolExecutorSubclassT
827       */
828      public void testConstructor7() {
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 781 | Line 839 | public class ThreadPoolExecutorSubclassT
839       */
840      public void testConstructor8() {
841          try {
842 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
842 >            new CustomTPE(1, 0, 1L, SECONDS,
843 >                          new ArrayBlockingQueue<Runnable>(10),
844 >                          new SimpleThreadFactory());
845              shouldThrow();
846          } catch (IllegalArgumentException success) {}
847      }
# Line 791 | Line 851 | public class ThreadPoolExecutorSubclassT
851       */
852      public void testConstructor9() {
853          try {
854 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
854 >            new CustomTPE(1, 2, -1L, SECONDS,
855 >                          new ArrayBlockingQueue<Runnable>(10),
856 >                          new SimpleThreadFactory());
857              shouldThrow();
858          } catch (IllegalArgumentException success) {}
859      }
# Line 801 | Line 863 | public class ThreadPoolExecutorSubclassT
863       */
864      public void testConstructor10() {
865          try {
866 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
866 >            new CustomTPE(2, 1, 1L, SECONDS,
867 >                          new ArrayBlockingQueue<Runnable>(10),
868 >                          new SimpleThreadFactory());
869              shouldThrow();
870          } catch (IllegalArgumentException success) {}
871      }
# Line 811 | Line 875 | public class ThreadPoolExecutorSubclassT
875       */
876      public void testConstructorNullPointerException2() {
877          try {
878 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
878 >            new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
879              shouldThrow();
880          } catch (NullPointerException success) {}
881      }
# Line 821 | Line 885 | public class ThreadPoolExecutorSubclassT
885       */
886      public void testConstructorNullPointerException3() {
887          try {
888 <            ThreadFactory f = null;
889 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
888 >            new CustomTPE(1, 2, 1L, SECONDS,
889 >                          new ArrayBlockingQueue<Runnable>(10),
890 >                          (ThreadFactory) null);
891              shouldThrow();
892          } catch (NullPointerException success) {}
893      }
# Line 832 | Line 897 | public class ThreadPoolExecutorSubclassT
897       */
898      public void testConstructor11() {
899          try {
900 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
900 >            new CustomTPE(-1, 1, 1L, SECONDS,
901 >                          new ArrayBlockingQueue<Runnable>(10),
902 >                          new NoOpREHandler());
903              shouldThrow();
904          } catch (IllegalArgumentException success) {}
905      }
# Line 842 | Line 909 | public class ThreadPoolExecutorSubclassT
909       */
910      public void testConstructor12() {
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 852 | Line 921 | public class ThreadPoolExecutorSubclassT
921       */
922      public void testConstructor13() {
923          try {
924 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
924 >            new CustomTPE(1, 0, 1L, SECONDS,
925 >                          new ArrayBlockingQueue<Runnable>(10),
926 >                          new NoOpREHandler());
927              shouldThrow();
928          } catch (IllegalArgumentException success) {}
929      }
# Line 862 | Line 933 | public class ThreadPoolExecutorSubclassT
933       */
934      public void testConstructor14() {
935          try {
936 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
936 >            new CustomTPE(1, 2, -1L, SECONDS,
937 >                          new ArrayBlockingQueue<Runnable>(10),
938 >                          new NoOpREHandler());
939              shouldThrow();
940          } catch (IllegalArgumentException success) {}
941      }
# Line 872 | Line 945 | public class ThreadPoolExecutorSubclassT
945       */
946      public void testConstructor15() {
947          try {
948 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
948 >            new CustomTPE(2, 1, 1L, SECONDS,
949 >                          new ArrayBlockingQueue<Runnable>(10),
950 >                          new NoOpREHandler());
951              shouldThrow();
952          } catch (IllegalArgumentException success) {}
953      }
# Line 882 | Line 957 | public class ThreadPoolExecutorSubclassT
957       */
958      public void testConstructorNullPointerException4() {
959          try {
960 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
960 >            new CustomTPE(1, 2, 1L, SECONDS,
961 >                          null,
962 >                          new NoOpREHandler());
963              shouldThrow();
964          } catch (NullPointerException success) {}
965      }
# Line 892 | Line 969 | public class ThreadPoolExecutorSubclassT
969       */
970      public void testConstructorNullPointerException5() {
971          try {
972 <            RejectedExecutionHandler r = null;
973 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
972 >            new CustomTPE(1, 2, 1L, SECONDS,
973 >                          new ArrayBlockingQueue<Runnable>(10),
974 >                          (RejectedExecutionHandler) null);
975              shouldThrow();
976          } catch (NullPointerException success) {}
977      }
# Line 903 | Line 981 | public class ThreadPoolExecutorSubclassT
981       */
982      public void testConstructor16() {
983          try {
984 <            new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
984 >            new CustomTPE(-1, 1, 1L, SECONDS,
985 >                          new ArrayBlockingQueue<Runnable>(10),
986 >                          new SimpleThreadFactory(),
987 >                          new NoOpREHandler());
988              shouldThrow();
989          } catch (IllegalArgumentException success) {}
990      }
# Line 913 | Line 994 | public class ThreadPoolExecutorSubclassT
994       */
995      public void testConstructor17() {
996          try {
997 <            new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
997 >            new CustomTPE(1, -1, 1L, SECONDS,
998 >                          new ArrayBlockingQueue<Runnable>(10),
999 >                          new SimpleThreadFactory(),
1000 >                          new NoOpREHandler());
1001              shouldThrow();
1002          } catch (IllegalArgumentException success) {}
1003      }
# Line 923 | Line 1007 | public class ThreadPoolExecutorSubclassT
1007       */
1008      public void testConstructor18() {
1009          try {
1010 <            new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1010 >            new CustomTPE(1, 0, 1L, SECONDS,
1011 >                          new ArrayBlockingQueue<Runnable>(10),
1012 >                          new SimpleThreadFactory(),
1013 >                          new NoOpREHandler());
1014              shouldThrow();
1015          } catch (IllegalArgumentException success) {}
1016      }
# Line 933 | Line 1020 | public class ThreadPoolExecutorSubclassT
1020       */
1021      public void testConstructor19() {
1022          try {
1023 <            new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1023 >            new CustomTPE(1, 2, -1L, SECONDS,
1024 >                          new ArrayBlockingQueue<Runnable>(10),
1025 >                          new SimpleThreadFactory(),
1026 >                          new NoOpREHandler());
1027              shouldThrow();
1028          } catch (IllegalArgumentException success) {}
1029      }
# Line 943 | Line 1033 | public class ThreadPoolExecutorSubclassT
1033       */
1034      public void testConstructor20() {
1035          try {
1036 <            new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
1036 >            new CustomTPE(2, 1, 1L, SECONDS,
1037 >                          new ArrayBlockingQueue<Runnable>(10),
1038 >                          new SimpleThreadFactory(),
1039 >                          new NoOpREHandler());
1040              shouldThrow();
1041          } catch (IllegalArgumentException success) {}
1042      }
# Line 953 | Line 1046 | public class ThreadPoolExecutorSubclassT
1046       */
1047      public void testConstructorNullPointerException6() {
1048          try {
1049 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
1049 >            new CustomTPE(1, 2, 1L, SECONDS,
1050 >                          null,
1051 >                          new SimpleThreadFactory(),
1052 >                          new NoOpREHandler());
1053              shouldThrow();
1054          } catch (NullPointerException success) {}
1055      }
# Line 963 | Line 1059 | public class ThreadPoolExecutorSubclassT
1059       */
1060      public void testConstructorNullPointerException7() {
1061          try {
1062 <            RejectedExecutionHandler r = null;
1063 <            new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
1062 >            new CustomTPE(1, 2, 1L, SECONDS,
1063 >                          new ArrayBlockingQueue<Runnable>(10),
1064 >                          new SimpleThreadFactory(),
1065 >                          (RejectedExecutionHandler) null);
1066              shouldThrow();
1067          } catch (NullPointerException success) {}
1068      }
# Line 974 | Line 1072 | public class ThreadPoolExecutorSubclassT
1072       */
1073      public void testConstructorNullPointerException8() {
1074          try {
1075 <            new CustomTPE(1, 2,
978 <                          LONG_DELAY_MS, MILLISECONDS,
1075 >            new CustomTPE(1, 2, 1L, SECONDS,
1076                            new ArrayBlockingQueue<Runnable>(10),
1077                            (ThreadFactory) null,
1078                            new NoOpREHandler());
# Line 1153 | Line 1250 | public class ThreadPoolExecutorSubclassT
1250       * execute(null) throws NPE
1251       */
1252      public void testExecuteNull() {
1253 <        ThreadPoolExecutor p = null;
1253 >        ThreadPoolExecutor p =
1254 >            new CustomTPE(1, 2, 1L, SECONDS,
1255 >                          new ArrayBlockingQueue<Runnable>(10));
1256          try {
1158            p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1257              p.execute(null);
1258              shouldThrow();
1259          } catch (NullPointerException success) {}
# Line 1651 | Line 1749 | public class ThreadPoolExecutorSubclassT
1749              l.add(new StringTask());
1750              l.add(new StringTask());
1751              List<Future<String>> futures =
1752 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1752 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1753              assertEquals(2, futures.size());
1754              for (Future<String> future : futures)
1755                  assertSame(TEST_STRING, future.get());
# Line 1666 | Line 1764 | public class ThreadPoolExecutorSubclassT
1764      public void testTimedInvokeAll6() throws Exception {
1765          ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1766          try {
1767 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1768 <            l.add(new StringTask());
1769 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1770 <            l.add(new StringTask());
1771 <            List<Future<String>> futures =
1772 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1773 <            assertEquals(l.size(), futures.size());
1774 <            for (Future future : futures)
1775 <                assertTrue(future.isDone());
1776 <            assertFalse(futures.get(0).isCancelled());
1777 <            assertTrue(futures.get(1).isCancelled());
1767 >            for (long timeout = timeoutMillis();;) {
1768 >                List<Callable<String>> tasks = new ArrayList<>();
1769 >                tasks.add(new StringTask("0"));
1770 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1771 >                tasks.add(new StringTask("2"));
1772 >                long startTime = System.nanoTime();
1773 >                List<Future<String>> futures =
1774 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1775 >                assertEquals(tasks.size(), futures.size());
1776 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1777 >                for (Future future : futures)
1778 >                    assertTrue(future.isDone());
1779 >                assertTrue(futures.get(1).isCancelled());
1780 >                try {
1781 >                    assertEquals("0", futures.get(0).get());
1782 >                    assertEquals("2", futures.get(2).get());
1783 >                    break;
1784 >                } catch (CancellationException retryWithLongerTimeout) {
1785 >                    timeout *= 2;
1786 >                    if (timeout >= LONG_DELAY_MS / 2)
1787 >                        fail("expected exactly one task to be cancelled");
1788 >                }
1789 >            }
1790          } finally {
1791              joinPool(e);
1792          }
# Line 1719 | Line 1829 | public class ThreadPoolExecutorSubclassT
1829       * allowCoreThreadTimeOut(true) causes idle threads to time out
1830       */
1831      public void testAllowCoreThreadTimeOut_true() throws Exception {
1832 <        long coreThreadTimeOut = SHORT_DELAY_MS;
1832 >        long keepAliveTime = timeoutMillis();
1833          final ThreadPoolExecutor p =
1834              new CustomTPE(2, 10,
1835 <                          coreThreadTimeOut, MILLISECONDS,
1835 >                          keepAliveTime, MILLISECONDS,
1836                            new ArrayBlockingQueue<Runnable>(10));
1837          final CountDownLatch threadStarted = new CountDownLatch(1);
1838          try {
1839              p.allowCoreThreadTimeOut(true);
1840              p.execute(new CheckedRunnable() {
1841 <                public void realRun() throws InterruptedException {
1841 >                public void realRun() {
1842                      threadStarted.countDown();
1843                      assertEquals(1, p.getPoolSize());
1844                  }});
1845              await(threadStarted);
1846 <            delay(coreThreadTimeOut);
1846 >            delay(keepAliveTime);
1847              long startTime = System.nanoTime();
1848              while (p.getPoolSize() > 0
1849                     && millisElapsedSince(startTime) < LONG_DELAY_MS)
# Line 1749 | Line 1859 | public class ThreadPoolExecutorSubclassT
1859       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1860       */
1861      public void testAllowCoreThreadTimeOut_false() throws Exception {
1862 <        long coreThreadTimeOut = SHORT_DELAY_MS;
1862 >        long keepAliveTime = timeoutMillis();
1863          final ThreadPoolExecutor p =
1864              new CustomTPE(2, 10,
1865 <                          coreThreadTimeOut, MILLISECONDS,
1865 >                          keepAliveTime, MILLISECONDS,
1866                            new ArrayBlockingQueue<Runnable>(10));
1867          final CountDownLatch threadStarted = new CountDownLatch(1);
1868          try {
# Line 1762 | Line 1872 | public class ThreadPoolExecutorSubclassT
1872                      threadStarted.countDown();
1873                      assertTrue(p.getPoolSize() >= 1);
1874                  }});
1875 <            delay(2 * coreThreadTimeOut);
1875 >            delay(2 * keepAliveTime);
1876              assertTrue(p.getPoolSize() >= 1);
1877          } finally {
1878              joinPool(p);
1879          }
1880      }
1881  
1882 +    /**
1883 +     * get(cancelled task) throws CancellationException
1884 +     * (in part, a test of CustomTPE itself)
1885 +     */
1886 +    public void testGet_cancelled() throws Exception {
1887 +        final ExecutorService e =
1888 +            new CustomTPE(1, 1,
1889 +                          LONG_DELAY_MS, MILLISECONDS,
1890 +                          new LinkedBlockingQueue<Runnable>());
1891 +        try {
1892 +            final CountDownLatch blockerStarted = new CountDownLatch(1);
1893 +            final CountDownLatch done = new CountDownLatch(1);
1894 +            final List<Future<?>> futures = new ArrayList<>();
1895 +            for (int i = 0; i < 2; i++) {
1896 +                Runnable r = new CheckedRunnable() { public void realRun()
1897 +                                                         throws Throwable {
1898 +                    blockerStarted.countDown();
1899 +                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
1900 +                }};
1901 +                futures.add(e.submit(r));
1902 +            }
1903 +            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
1904 +            for (Future<?> future : futures) future.cancel(false);
1905 +            for (Future<?> future : futures) {
1906 +                try {
1907 +                    future.get();
1908 +                    shouldThrow();
1909 +                } catch (CancellationException success) {}
1910 +                try {
1911 +                    future.get(LONG_DELAY_MS, MILLISECONDS);
1912 +                    shouldThrow();
1913 +                } catch (CancellationException success) {}
1914 +                assertTrue(future.isCancelled());
1915 +                assertTrue(future.isDone());
1916 +            }
1917 +            done.countDown();
1918 +        } finally {
1919 +            joinPool(e);
1920 +        }
1921 +    }
1922 +
1923   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines