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.99 by jsr166, Mon May 29 22:44:27 2017 UTC vs.
Revision 1.100 by jsr166, Mon Jul 17 22:27:31 2017 UTC

# Line 23 | Line 23 | import java.util.concurrent.ExecutorServ
23   import java.util.concurrent.Future;
24   import java.util.concurrent.FutureTask;
25   import java.util.concurrent.LinkedBlockingQueue;
26 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.ThreadLocalRandom;
31   import java.util.concurrent.ThreadPoolExecutor;
32   import java.util.concurrent.TimeoutException;
33   import java.util.concurrent.TimeUnit;
# Line 1131 | Line 1131 | public class ThreadPoolExecutorSubclassT
1131      }
1132  
1133      /**
1134 <     * execute throws RejectedExecutionException if saturated.
1134 >     * Submitted tasks are rejected when saturated or shutdown
1135       */
1136 <    public void testSaturatedExecute() {
1137 <        final CountDownLatch done = new CountDownLatch(1);
1136 >    public void testSubmittedTasksRejectedWhenSaturatedOrShutdown() throws InterruptedException {
1137          final ThreadPoolExecutor p =
1138              new CustomTPE(1, 1,
1139                            LONG_DELAY_MS, MILLISECONDS,
1140                            new ArrayBlockingQueue<Runnable>(1));
1141 <        try (PoolCleaner cleaner = cleaner(p, done)) {
1142 <            Runnable task = new CheckedRunnable() {
1143 <                public void realRun() throws InterruptedException {
1144 <                    await(done);
1145 <                }};
1146 <            for (int i = 0; i < 2; ++i)
1147 <                p.execute(task);
1149 <            for (int i = 0; i < 2; ++i) {
1141 >        final int saturatedSize = saturatedSize(p);
1142 >        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
1143 >        final CountDownLatch threadsStarted = new CountDownLatch(p.getMaximumPoolSize());
1144 >        final CountDownLatch done = new CountDownLatch(1);
1145 >        final Runnable r = () -> {
1146 >            threadsStarted.countDown();
1147 >            for (;;) {
1148                  try {
1149 <                    p.execute(task);
1150 <                    shouldThrow();
1151 <                } catch (RejectedExecutionException success) {}
1152 <                assertTrue(p.getTaskCount() <= 2);
1153 <            }
1154 <        }
1155 <    }
1149 >                    done.await();
1150 >                    return;
1151 >                } catch (InterruptedException shutdownNowDeliberatelyIgnored) {}
1152 >            }};
1153 >        final Callable<Boolean> c = () -> {
1154 >            threadsStarted.countDown();
1155 >            for (;;) {
1156 >                try {
1157 >                    done.await();
1158 >                    return Boolean.TRUE;
1159 >                } catch (InterruptedException shutdownNowDeliberatelyIgnored) {}
1160 >            }};
1161 >        final boolean shutdownNow = rnd.nextBoolean();
1162  
1159    /**
1160     * executor using CallerRunsPolicy runs task if saturated.
1161     */
1162    public void testSaturatedExecute2() {
1163        final CountDownLatch done = new CountDownLatch(1);
1164        final ThreadPoolExecutor p =
1165            new CustomTPE(1, 1,
1166                          LONG_DELAY_MS, MILLISECONDS,
1167                          new ArrayBlockingQueue<Runnable>(1),
1168                          new CustomTPE.CallerRunsPolicy());
1163          try (PoolCleaner cleaner = cleaner(p, done)) {
1164 <            Runnable blocker = new CheckedRunnable() {
1165 <                public void realRun() throws InterruptedException {
1166 <                    await(done);
1167 <                }};
1168 <            p.execute(blocker);
1169 <            TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1170 <            for (int i = 0; i < tasks.length; i++)
1171 <                tasks[i] = new TrackedNoOpRunnable();
1172 <            for (int i = 0; i < tasks.length; i++)
1179 <                p.execute(tasks[i]);
1180 <            for (int i = 1; i < tasks.length; i++)
1181 <                assertTrue(tasks[i].done);
1182 <            assertFalse(tasks[0].done); // waiting in queue
1183 <        }
1184 <    }
1164 >            // saturate
1165 >            for (int i = saturatedSize; i--> 0; ) {
1166 >                switch (rnd.nextInt(4)) {
1167 >                case 0: p.execute(r); break;
1168 >                case 1: assertFalse(p.submit(r).isDone()); break;
1169 >                case 2: assertFalse(p.submit(r, Boolean.TRUE).isDone()); break;
1170 >                case 3: assertFalse(p.submit(c).isDone()); break;
1171 >                }
1172 >            }
1173  
1174 <    /**
1175 <     * executor using DiscardPolicy drops task if saturated.
1176 <     */
1177 <    public void testSaturatedExecute3() {
1178 <        final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1179 <        for (int i = 0; i < tasks.length; ++i)
1180 <            tasks[i] = new TrackedNoOpRunnable();
1181 <        final CountDownLatch done = new CountDownLatch(1);
1182 <        final ThreadPoolExecutor p =
1183 <            new CustomTPE(1, 1,
1184 <                          LONG_DELAY_MS, MILLISECONDS,
1185 <                          new ArrayBlockingQueue<Runnable>(1),
1186 <                          new CustomTPE.DiscardPolicy());
1199 <        try (PoolCleaner cleaner = cleaner(p, done)) {
1200 <            p.execute(awaiter(done));
1174 >            await(threadsStarted);
1175 >            assertTaskSubmissionsAreRejected(p);
1176 >
1177 >            if (shutdownNow)
1178 >                p.shutdownNow();
1179 >            else
1180 >                p.shutdown();
1181 >            // Pool is shutdown, but not yet terminated
1182 >            assertTaskSubmissionsAreRejected(p);
1183 >            assertFalse(p.isTerminated());
1184 >
1185 >            done.countDown();   // release blocking tasks
1186 >            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
1187  
1188 <            for (TrackedNoOpRunnable task : tasks)
1189 <                p.execute(task);
1190 <            for (int i = 1; i < tasks.length; i++)
1191 <                assertFalse(tasks[i].done);
1192 <        }
1207 <        for (int i = 1; i < tasks.length; i++)
1208 <            assertFalse(tasks[i].done);
1209 <        assertTrue(tasks[0].done); // was waiting in queue
1188 >            assertTaskSubmissionsAreRejected(p);
1189 >        }
1190 >        assertEquals(saturatedSize(p)
1191 >                     - (shutdownNow ? p.getQueue().remainingCapacity() : 0),
1192 >                     p.getCompletedTaskCount());
1193      }
1194  
1195      /**
1196       * executor using DiscardOldestPolicy drops oldest task if saturated.
1197       */
1198 <    public void testSaturatedExecute4() {
1198 >    public void testSaturatedExecute_DiscardOldestPolicy() {
1199          final CountDownLatch done = new CountDownLatch(1);
1200          LatchAwaiter r1 = awaiter(done);
1201          LatchAwaiter r2 = awaiter(done);
# Line 1221 | Line 1204 | public class ThreadPoolExecutorSubclassT
1204              new CustomTPE(1, 1,
1205                            LONG_DELAY_MS, MILLISECONDS,
1206                            new ArrayBlockingQueue<Runnable>(1),
1207 <                          new CustomTPE.DiscardOldestPolicy());
1207 >                          new ThreadPoolExecutor.DiscardOldestPolicy());
1208          try (PoolCleaner cleaner = cleaner(p, done)) {
1209              assertEquals(LatchAwaiter.NEW, r1.state);
1210              assertEquals(LatchAwaiter.NEW, r2.state);
# Line 1239 | Line 1222 | public class ThreadPoolExecutorSubclassT
1222      }
1223  
1224      /**
1242     * execute throws RejectedExecutionException if shutdown
1243     */
1244    public void testRejectedExecutionExceptionOnShutdown() {
1245        final ThreadPoolExecutor p =
1246            new CustomTPE(1, 1,
1247                          LONG_DELAY_MS, MILLISECONDS,
1248                          new ArrayBlockingQueue<Runnable>(1));
1249        try { p.shutdown(); } catch (SecurityException ok) { return; }
1250        try (PoolCleaner cleaner = cleaner(p)) {
1251            try {
1252                p.execute(new NoOpRunnable());
1253                shouldThrow();
1254            } catch (RejectedExecutionException success) {}
1255        }
1256    }
1257
1258    /**
1259     * execute using CallerRunsPolicy drops task on shutdown
1260     */
1261    public void testCallerRunsOnShutdown() {
1262        final ThreadPoolExecutor p =
1263            new CustomTPE(1, 1,
1264                          LONG_DELAY_MS, MILLISECONDS,
1265                          new ArrayBlockingQueue<Runnable>(1),
1266                          new CustomTPE.CallerRunsPolicy());
1267        try { p.shutdown(); } catch (SecurityException ok) { return; }
1268        try (PoolCleaner cleaner = cleaner(p)) {
1269            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1270            p.execute(r);
1271            assertFalse(r.done);
1272        }
1273    }
1274
1275    /**
1276     * execute using DiscardPolicy drops task on shutdown
1277     */
1278    public void testDiscardOnShutdown() {
1279        final ThreadPoolExecutor p =
1280            new CustomTPE(1, 1,
1281                          LONG_DELAY_MS, MILLISECONDS,
1282                          new ArrayBlockingQueue<Runnable>(1),
1283                          new CustomTPE.DiscardPolicy());
1284        try { p.shutdown(); } catch (SecurityException ok) { return; }
1285        try (PoolCleaner cleaner = cleaner(p)) {
1286            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1287            p.execute(r);
1288            assertFalse(r.done);
1289        }
1290    }
1291
1292    /**
1225       * execute using DiscardOldestPolicy drops task on shutdown
1226       */
1227      public void testDiscardOldestOnShutdown() {
# Line 1297 | Line 1229 | public class ThreadPoolExecutorSubclassT
1229              new CustomTPE(1, 1,
1230                            LONG_DELAY_MS, MILLISECONDS,
1231                            new ArrayBlockingQueue<Runnable>(1),
1232 <                          new CustomTPE.DiscardOldestPolicy());
1232 >                          new ThreadPoolExecutor.DiscardOldestPolicy());
1233  
1234          try { p.shutdown(); } catch (SecurityException ok) { return; }
1235          try (PoolCleaner cleaner = cleaner(p)) {
# Line 1308 | Line 1240 | public class ThreadPoolExecutorSubclassT
1240      }
1241  
1242      /**
1243 <     * execute(null) throws NPE
1243 >     * Submitting null tasks throws NullPointerException
1244       */
1245 <    public void testExecuteNull() {
1245 >    public void testNullTaskSubmission() {
1246          final ThreadPoolExecutor p =
1247              new CustomTPE(1, 2,
1248                            1L, SECONDS,
1249                            new ArrayBlockingQueue<Runnable>(10));
1250          try (PoolCleaner cleaner = cleaner(p)) {
1251 <            try {
1320 <                p.execute(null);
1321 <                shouldThrow();
1322 <            } catch (NullPointerException success) {}
1251 >            assertNullTaskSubmissionThrowsNullPointerException(p);
1252          }
1253      }
1254  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines