ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ThreadPoolExecutorTest.java (file contents):
Revision 1.122 by jsr166, Sat Jul 15 18:42:01 2017 UTC vs.
Revision 1.125 by jsr166, Wed Apr 3 20:55:45 2019 UTC

# Line 28 | Line 28 | import java.util.concurrent.RejectedExec
28   import java.util.concurrent.RejectedExecutionHandler;
29   import java.util.concurrent.SynchronousQueue;
30   import java.util.concurrent.ThreadFactory;
31 + import java.util.concurrent.ThreadLocalRandom;
32   import java.util.concurrent.ThreadPoolExecutor;
33   import java.util.concurrent.ThreadPoolExecutor.AbortPolicy;
34   import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;
# Line 1058 | Line 1059 | public class ThreadPoolExecutorTest exte
1059                      p.submit(task).get();
1060                  }});
1061  
1062 <            await(threadStarted);
1062 >            await(threadStarted); // ensure quiescence
1063              t.interrupt();
1064              awaitTermination(t);
1065          }
1066      }
1067  
1068      /**
1069 <     * execute throws RejectedExecutionException if saturated.
1069 >     * Submitted tasks are rejected when saturated or shutdown
1070       */
1071 <    public void testSaturatedExecute() {
1071 >    public void testSubmittedTasksRejectedWhenSaturatedOrShutdown() throws InterruptedException {
1072 >        final ThreadPoolExecutor p = new ThreadPoolExecutor(
1073 >            1, 1, 1, SECONDS, new ArrayBlockingQueue<Runnable>(1));
1074 >        final int saturatedSize = saturatedSize(p);
1075 >        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
1076 >        final CountDownLatch threadsStarted = new CountDownLatch(p.getMaximumPoolSize());
1077          final CountDownLatch done = new CountDownLatch(1);
1078 <        final ThreadPoolExecutor p =
1079 <            new ThreadPoolExecutor(1, 1,
1080 <                                   LONG_DELAY_MS, MILLISECONDS,
1075 <                                   new ArrayBlockingQueue<Runnable>(1));
1076 <        try (PoolCleaner cleaner = cleaner(p, done)) {
1077 <            Runnable task = new CheckedRunnable() {
1078 <                public void realRun() throws InterruptedException {
1079 <                    await(done);
1080 <                }};
1081 <            for (int i = 0; i < 2; ++i)
1082 <                p.execute(task);
1083 <            for (int i = 0; i < 2; ++i) {
1078 >        final Runnable r = () -> {
1079 >            threadsStarted.countDown();
1080 >            for (;;) {
1081                  try {
1082 <                    p.execute(task);
1083 <                    shouldThrow();
1084 <                } catch (RejectedExecutionException success) {}
1085 <                assertTrue(p.getTaskCount() <= 2);
1086 <            }
1087 <        }
1088 <    }
1092 <
1093 <    /**
1094 <     * submit(runnable) throws RejectedExecutionException if saturated.
1095 <     */
1096 <    public void testSaturatedSubmitRunnable() {
1097 <        final CountDownLatch done = new CountDownLatch(1);
1098 <        final ThreadPoolExecutor p =
1099 <            new ThreadPoolExecutor(1, 1,
1100 <                                   LONG_DELAY_MS, MILLISECONDS,
1101 <                                   new ArrayBlockingQueue<Runnable>(1));
1102 <        try (PoolCleaner cleaner = cleaner(p, done)) {
1103 <            Runnable task = new CheckedRunnable() {
1104 <                public void realRun() throws InterruptedException {
1105 <                    await(done);
1106 <                }};
1107 <            for (int i = 0; i < 2; ++i)
1108 <                p.submit(task);
1109 <            for (int i = 0; i < 2; ++i) {
1082 >                    done.await();
1083 >                    return;
1084 >                } catch (InterruptedException shutdownNowDeliberatelyIgnored) {}
1085 >            }};
1086 >        final Callable<Boolean> c = () -> {
1087 >            threadsStarted.countDown();
1088 >            for (;;) {
1089                  try {
1090 <                    p.execute(task);
1091 <                    shouldThrow();
1092 <                } catch (RejectedExecutionException success) {}
1093 <                assertTrue(p.getTaskCount() <= 2);
1094 <            }
1116 <        }
1117 <    }
1090 >                    done.await();
1091 >                    return Boolean.TRUE;
1092 >                } catch (InterruptedException shutdownNowDeliberatelyIgnored) {}
1093 >            }};
1094 >        final boolean shutdownNow = rnd.nextBoolean();
1095  
1119    /**
1120     * submit(callable) throws RejectedExecutionException if saturated.
1121     */
1122    public void testSaturatedSubmitCallable() {
1123        final CountDownLatch done = new CountDownLatch(1);
1124        final ThreadPoolExecutor p =
1125            new ThreadPoolExecutor(1, 1,
1126                                   LONG_DELAY_MS, MILLISECONDS,
1127                                   new ArrayBlockingQueue<Runnable>(1));
1096          try (PoolCleaner cleaner = cleaner(p, done)) {
1097 <            Runnable task = new CheckedRunnable() {
1098 <                public void realRun() throws InterruptedException {
1099 <                    await(done);
1100 <                }};
1101 <            for (int i = 0; i < 2; ++i)
1102 <                p.execute(task);
1103 <            for (int i = 0; i < 2; ++i) {
1104 <                try {
1137 <                    p.execute(task);
1138 <                    shouldThrow();
1139 <                } catch (RejectedExecutionException success) {}
1140 <                assertTrue(p.getTaskCount() <= 2);
1097 >            // saturate
1098 >            for (int i = saturatedSize; i--> 0; ) {
1099 >                switch (rnd.nextInt(4)) {
1100 >                case 0: p.execute(r); break;
1101 >                case 1: assertFalse(p.submit(r).isDone()); break;
1102 >                case 2: assertFalse(p.submit(r, Boolean.TRUE).isDone()); break;
1103 >                case 3: assertFalse(p.submit(c).isDone()); break;
1104 >                }
1105              }
1142        }
1143    }
1106  
1107 <    /**
1108 <     * executor using CallerRunsPolicy runs task if saturated.
1147 <     */
1148 <    public void testSaturatedExecute2() {
1149 <        final ThreadPoolExecutor p =
1150 <            new ThreadPoolExecutor(1, 1,
1151 <                                   LONG_DELAY_MS,
1152 <                                   MILLISECONDS,
1153 <                                   new ArrayBlockingQueue<Runnable>(1),
1154 <                                   new CallerRunsPolicy());
1155 <        try (PoolCleaner cleaner = cleaner(p)) {
1156 <            final CountDownLatch done = new CountDownLatch(1);
1157 <            Runnable blocker = new CheckedRunnable() {
1158 <                public void realRun() throws InterruptedException {
1159 <                    await(done);
1160 <                }};
1161 <            p.execute(blocker);
1162 <            TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1163 <            for (int i = 0; i < tasks.length; i++)
1164 <                tasks[i] = new TrackedNoOpRunnable();
1165 <            for (int i = 0; i < tasks.length; i++)
1166 <                p.execute(tasks[i]);
1167 <            for (int i = 1; i < tasks.length; i++)
1168 <                assertTrue(tasks[i].done);
1169 <            assertFalse(tasks[0].done); // waiting in queue
1170 <            done.countDown();
1171 <        }
1172 <    }
1107 >            await(threadsStarted);
1108 >            assertTaskSubmissionsAreRejected(p);
1109  
1110 <    /**
1111 <     * executor using DiscardPolicy drops task if saturated.
1112 <     */
1113 <    public void testSaturatedExecute3() {
1114 <        final CountDownLatch done = new CountDownLatch(1);
1115 <        final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1116 <        for (int i = 0; i < tasks.length; ++i)
1181 <            tasks[i] = new TrackedNoOpRunnable();
1182 <        final ThreadPoolExecutor p =
1183 <            new ThreadPoolExecutor(1, 1,
1184 <                          LONG_DELAY_MS, MILLISECONDS,
1185 <                          new ArrayBlockingQueue<Runnable>(1),
1186 <                          new DiscardPolicy());
1187 <        try (PoolCleaner cleaner = cleaner(p, done)) {
1188 <            p.execute(awaiter(done));
1110 >            if (shutdownNow)
1111 >                p.shutdownNow();
1112 >            else
1113 >                p.shutdown();
1114 >            // Pool is shutdown, but not yet terminated
1115 >            assertTaskSubmissionsAreRejected(p);
1116 >            assertFalse(p.isTerminated());
1117  
1118 <            for (TrackedNoOpRunnable task : tasks)
1119 <                p.execute(task);
1120 <            for (int i = 1; i < tasks.length; i++)
1121 <                assertFalse(tasks[i].done);
1122 <        }
1123 <        for (int i = 1; i < tasks.length; i++)
1124 <            assertFalse(tasks[i].done);
1125 <        assertTrue(tasks[0].done); // was waiting in queue
1118 >            done.countDown();   // release blocking tasks
1119 >            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
1120 >
1121 >            assertTaskSubmissionsAreRejected(p);
1122 >        }
1123 >        assertEquals(saturatedSize(p)
1124 >                     - (shutdownNow ? p.getQueue().remainingCapacity() : 0),
1125 >                     p.getCompletedTaskCount());
1126      }
1127  
1128      /**
1129       * executor using DiscardOldestPolicy drops oldest task if saturated.
1130       */
1131 <    public void testSaturatedExecute4() {
1131 >    public void testSaturatedExecute_DiscardOldestPolicy() {
1132          final CountDownLatch done = new CountDownLatch(1);
1133          LatchAwaiter r1 = awaiter(done);
1134          LatchAwaiter r2 = awaiter(done);
# Line 1227 | Line 1155 | public class ThreadPoolExecutorTest exte
1155      }
1156  
1157      /**
1230     * execute throws RejectedExecutionException if shutdown
1231     */
1232    public void testRejectedExecutionExceptionOnShutdown() {
1233        final ThreadPoolExecutor p =
1234            new ThreadPoolExecutor(1, 1,
1235                                   LONG_DELAY_MS, MILLISECONDS,
1236                                   new ArrayBlockingQueue<Runnable>(1));
1237        try { p.shutdown(); } catch (SecurityException ok) { return; }
1238        try (PoolCleaner cleaner = cleaner(p)) {
1239            try {
1240                p.execute(new NoOpRunnable());
1241                shouldThrow();
1242            } catch (RejectedExecutionException success) {}
1243        }
1244    }
1245
1246    /**
1247     * execute using CallerRunsPolicy drops task on shutdown
1248     */
1249    public void testCallerRunsOnShutdown() {
1250        RejectedExecutionHandler h = new CallerRunsPolicy();
1251        final ThreadPoolExecutor p =
1252            new ThreadPoolExecutor(1, 1,
1253                                   LONG_DELAY_MS, MILLISECONDS,
1254                                   new ArrayBlockingQueue<Runnable>(1), h);
1255
1256        try { p.shutdown(); } catch (SecurityException ok) { return; }
1257        try (PoolCleaner cleaner = cleaner(p)) {
1258            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1259            p.execute(r);
1260            assertFalse(r.done);
1261        }
1262    }
1263
1264    /**
1265     * execute using DiscardPolicy drops task on shutdown
1266     */
1267    public void testDiscardOnShutdown() {
1268        final ThreadPoolExecutor p =
1269            new ThreadPoolExecutor(1, 1,
1270                                   LONG_DELAY_MS, MILLISECONDS,
1271                                   new ArrayBlockingQueue<Runnable>(1),
1272                                   new DiscardPolicy());
1273
1274        try { p.shutdown(); } catch (SecurityException ok) { return; }
1275        try (PoolCleaner cleaner = cleaner(p)) {
1276            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1277            p.execute(r);
1278            assertFalse(r.done);
1279        }
1280    }
1281
1282    /**
1158       * execute using DiscardOldestPolicy drops task on shutdown
1159       */
1160      public void testDiscardOldestOnShutdown() {
# Line 1298 | Line 1173 | public class ThreadPoolExecutorTest exte
1173      }
1174  
1175      /**
1176 <     * execute(null) throws NPE
1176 >     * Submitting null tasks throws NullPointerException
1177       */
1178 <    public void testExecuteNull() {
1178 >    public void testNullTaskSubmission() {
1179          final ThreadPoolExecutor p =
1180              new ThreadPoolExecutor(1, 2,
1181                                     1L, SECONDS,
1182                                     new ArrayBlockingQueue<Runnable>(10));
1183          try (PoolCleaner cleaner = cleaner(p)) {
1184 <            try {
1310 <                p.execute(null);
1311 <                shouldThrow();
1312 <            } catch (NullPointerException success) {}
1184 >            assertNullTaskSubmissionThrowsNullPointerException(p);
1185          }
1186      }
1187  
# Line 2112 | Line 1984 | public class ThreadPoolExecutorTest exte
1984          assertTrue(p.getQueue().isEmpty());
1985      }
1986  
1987 +    public void testThreadFactoryReturnsTerminatedThread_shouldThrow() {
1988 +        if (!testImplementationDetails)
1989 +            return;
1990 +
1991 +        ThreadFactory returnsTerminatedThread = runnableIgnored -> {
1992 +            Thread thread = new Thread(() -> {});
1993 +            thread.start();
1994 +            try { thread.join(); }
1995 +            catch (InterruptedException ex) { throw new Error(ex); }
1996 +            return thread;
1997 +        };
1998 +        ThreadPoolExecutor p =
1999 +            new ThreadPoolExecutor(1, 1, 1, SECONDS,
2000 +                                   new ArrayBlockingQueue<Runnable>(1),
2001 +                                   returnsTerminatedThread);
2002 +        try (PoolCleaner cleaner = cleaner(p)) {
2003 +            assertThrows(IllegalThreadStateException.class,
2004 +                         () -> p.execute(() -> {}));
2005 +        }
2006 +    }
2007 +
2008 +    public void testThreadFactoryReturnsStartedThread_shouldThrow() {
2009 +        if (!testImplementationDetails)
2010 +            return;
2011 +
2012 +        CountDownLatch latch = new CountDownLatch(1);
2013 +        Runnable awaitLatch = () -> {
2014 +            try { latch.await(); }
2015 +            catch (InterruptedException ex) { throw new Error(ex); }};
2016 +        ThreadFactory returnsStartedThread = runnable -> {
2017 +            Thread thread = new Thread(awaitLatch);
2018 +            thread.start();
2019 +            return thread;
2020 +        };
2021 +        ThreadPoolExecutor p =
2022 +            new ThreadPoolExecutor(1, 1, 1, SECONDS,
2023 +                                   new ArrayBlockingQueue<Runnable>(1),
2024 +                                   returnsStartedThread);
2025 +        try (PoolCleaner cleaner = cleaner(p)) {
2026 +            assertThrows(IllegalThreadStateException.class,
2027 +                         () -> p.execute(() -> {}));
2028 +            latch.countDown();
2029 +        }
2030 +    }
2031 +
2032   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines