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.123 by jsr166, Sat Jul 15 23:15:21 2017 UTC vs.
Revision 1.124 by jsr166, Mon Jul 17 22:27:31 2017 UTC

# Line 1059 | 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 <     * Submitted tasks are rejected when saturated.
1069 >     * Submitted tasks are rejected when saturated or shutdown
1070       */
1071 <    @SuppressWarnings("FutureReturnValueIgnored")
1072 <    public void testSubmittedTasksRejectedWhenSaturated() {
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 Runnable r = awaiter(done);
1079 <        final Callable<Boolean> c = new CheckedCallable() {
1080 <            public Boolean realCall() throws InterruptedException {
1081 <                await(done);
1082 <                return Boolean.TRUE;
1078 >        final Runnable r = () -> {
1079 >            threadsStarted.countDown();
1080 >            for (;;) {
1081 >                try {
1082 >                    done.await();
1083 >                    return;
1084 >                } catch (InterruptedException shutdownNowDeliberatelyIgnored) {}
1085              }};
1086 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(
1087 <            1, 1, 1, SECONDS, new ArrayBlockingQueue<Runnable>(1));
1086 >        final Callable<Boolean> c = () -> {
1087 >            threadsStarted.countDown();
1088 >            for (;;) {
1089 >                try {
1090 >                    done.await();
1091 >                    return Boolean.TRUE;
1092 >                } catch (InterruptedException shutdownNowDeliberatelyIgnored) {}
1093 >            }};
1094 >        final boolean shutdownNow = rnd.nextBoolean();
1095  
1096          try (PoolCleaner cleaner = cleaner(p, done)) {
1097              // saturate
1098 <            for (int i = saturatedSize(p); i--> 0; ) {
1099 <                switch (rnd.nextInt(3)) {
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(c).isDone()); break;
1103 <                }
1092 <            }
1093 <
1094 <            // check default handler
1095 <            assertTrue(p.getRejectedExecutionHandler() instanceof AbortPolicy);
1096 <            for (int i = 2; i--> 0; ) {
1097 <                try {
1098 <                    p.execute(r);
1099 <                    shouldThrow();
1100 <                } catch (RejectedExecutionException success) {}
1101 <                try {
1102 <                    p.submit(r);
1103 <                    shouldThrow();
1104 <                } catch (RejectedExecutionException success) {}
1105 <                try {
1106 <                    p.submit(c);
1107 <                    shouldThrow();
1108 <                } catch (RejectedExecutionException success) {}
1109 <            }
1110 <
1111 <            // check CallerRunsPolicy runs task in caller thread
1112 <            {
1113 <                RejectedExecutionHandler handler = new CallerRunsPolicy();
1114 <                p.setRejectedExecutionHandler(handler);
1115 <                assertSame(handler, p.getRejectedExecutionHandler());
1116 <                final AtomicReference<Thread> thread = new AtomicReference<>();
1117 <                p.execute(new Runnable() { public void run() {
1118 <                    thread.set(Thread.currentThread()); }});
1119 <                assertSame(Thread.currentThread(), thread.get());
1120 <            }
1121 <
1122 <            // check DiscardPolicy does nothing
1123 <            {
1124 <                RejectedExecutionHandler handler = new DiscardPolicy();
1125 <                p.setRejectedExecutionHandler(handler);
1126 <                assertSame(handler, p.getRejectedExecutionHandler());
1127 <                final AtomicReference<Thread> thread = new AtomicReference<>();
1128 <                p.execute(new Runnable() { public void run() {
1129 <                    thread.set(Thread.currentThread()); }});
1130 <                assertNull(thread.get());
1131 <            }
1132 <
1133 <            class Recorder implements RejectedExecutionHandler {
1134 <                public volatile Runnable r = null;
1135 <                public volatile ThreadPoolExecutor p = null;
1136 <                public void reset() { r = null; p = null; }
1137 <                public void rejectedExecution(Runnable r, ThreadPoolExecutor p) {
1138 <                    assertNull(this.r);
1139 <                    assertNull(this.p);
1140 <                    this.r = r;
1141 <                    this.p = p;
1102 >                case 2: assertFalse(p.submit(r, Boolean.TRUE).isDone()); break;
1103 >                case 3: assertFalse(p.submit(c).isDone()); break;
1104                  }
1105              }
1106  
1107 <            // check custom handler is invoked exactly once per task
1108 <            Recorder recorder = new Recorder();
1147 <            p.setRejectedExecutionHandler(recorder);
1148 <            assertSame(recorder, p.getRejectedExecutionHandler());
1149 <            for (int i = 2; i--> 0; ) {
1150 <                recorder.reset();
1151 <                p.execute(r);
1152 <                assertSame(r, recorder.r);
1153 <                assertSame(p, recorder.p);
1154 <
1155 <                recorder.reset();
1156 <                assertFalse(p.submit(r).isDone());
1157 <                assertTrue(recorder.r instanceof FutureTask);
1158 <                assertSame(p, recorder.p);
1159 <
1160 <                recorder.reset();
1161 <                assertFalse(p.submit(c).isDone());
1162 <                assertTrue(recorder.r instanceof FutureTask);
1163 <                assertSame(p, recorder.p);
1164 <            }
1165 <
1166 <            // check that pool was not perturbed by handlers
1167 <            assertEquals(2, p.getTaskCount());
1168 <            assertEquals(0, p.getCompletedTaskCount());
1169 <            assertEquals(0, p.getQueue().remainingCapacity());
1170 <        }
1171 <        assertEquals(saturatedSize(p), p.getCompletedTaskCount());
1172 <    }
1173 <
1174 <    /**
1175 <     * executor using CallerRunsPolicy runs task if saturated.
1176 <     */
1177 <    public void testSaturatedExecute2() {
1178 <        final RejectedExecutionHandler handler = new CallerRunsPolicy();
1179 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(
1180 <            1, 1, LONG_DELAY_MS, SECONDS, new ArrayBlockingQueue<Runnable>(1),
1181 <            handler);
1182 <        assertSame(handler, p.getRejectedExecutionHandler());
1183 <        final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1184 <        final CountDownLatch done = new CountDownLatch(1);
1185 <        try (PoolCleaner cleaner = cleaner(p, done)) {
1186 <            p.execute(awaiter(done));
1187 <
1188 <            for (int i = 0; i < tasks.length; i++)
1189 <                p.execute(tasks[i] = new TrackedNoOpRunnable());
1190 <
1191 <            for (int i = 1; i < tasks.length; i++)
1192 <                assertTrue(tasks[i].done);
1193 <            assertFalse(tasks[0].done); // waiting in queue
1194 <        }
1195 <        for (TrackedNoOpRunnable task : tasks)
1196 <            assertTrue(task.done);
1197 <    }
1198 <
1199 <    /**
1200 <     * executor using DiscardPolicy drops task if saturated.
1201 <     */
1202 <    public void testSaturatedExecute3() {
1203 <        final RejectedExecutionHandler handler = new DiscardPolicy();
1204 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(
1205 <            1, 1, LONG_DELAY_MS, SECONDS, new ArrayBlockingQueue<Runnable>(1),
1206 <            handler);
1207 <        assertSame(handler, p.getRejectedExecutionHandler());
1208 <        final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1209 <        final CountDownLatch done = new CountDownLatch(1);
1210 <        try (PoolCleaner cleaner = cleaner(p, done)) {
1211 <            p.execute(awaiter(done));
1212 <
1213 <            for (int i = 0; i < tasks.length; i++)
1214 <                p.execute(tasks[i] = new TrackedNoOpRunnable());
1107 >            await(threadsStarted);
1108 >            assertTaskSubmissionsAreRejected(p);
1109  
1110 <            for (int i = 1; i < tasks.length; i++)
1111 <                assertFalse(tasks[i].done);
1112 <        }
1113 <        for (int i = 1; i < tasks.length; i++)
1114 <            assertFalse(tasks[i].done);
1115 <        assertTrue(tasks[0].done); // was waiting in queue
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 >            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 1251 | Line 1155 | public class ThreadPoolExecutorTest exte
1155      }
1156  
1157      /**
1254     * execute throws RejectedExecutionException if shutdown
1255     */
1256    public void testRejectedExecutionExceptionOnShutdown() {
1257        final ThreadPoolExecutor p =
1258            new ThreadPoolExecutor(1, 1,
1259                                   LONG_DELAY_MS, MILLISECONDS,
1260                                   new ArrayBlockingQueue<Runnable>(1));
1261        try { p.shutdown(); } catch (SecurityException ok) { return; }
1262        try (PoolCleaner cleaner = cleaner(p)) {
1263            try {
1264                p.execute(new NoOpRunnable());
1265                shouldThrow();
1266            } catch (RejectedExecutionException success) {}
1267        }
1268    }
1269
1270    /**
1271     * execute using CallerRunsPolicy drops task on shutdown
1272     */
1273    public void testCallerRunsOnShutdown() {
1274        RejectedExecutionHandler h = new CallerRunsPolicy();
1275        final ThreadPoolExecutor p =
1276            new ThreadPoolExecutor(1, 1,
1277                                   LONG_DELAY_MS, MILLISECONDS,
1278                                   new ArrayBlockingQueue<Runnable>(1), h);
1279
1280        try { p.shutdown(); } catch (SecurityException ok) { return; }
1281        try (PoolCleaner cleaner = cleaner(p)) {
1282            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1283            p.execute(r);
1284            assertFalse(r.done);
1285        }
1286    }
1287
1288    /**
1289     * execute using DiscardPolicy drops task on shutdown
1290     */
1291    public void testDiscardOnShutdown() {
1292        final ThreadPoolExecutor p =
1293            new ThreadPoolExecutor(1, 1,
1294                                   LONG_DELAY_MS, MILLISECONDS,
1295                                   new ArrayBlockingQueue<Runnable>(1),
1296                                   new DiscardPolicy());
1297
1298        try { p.shutdown(); } catch (SecurityException ok) { return; }
1299        try (PoolCleaner cleaner = cleaner(p)) {
1300            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1301            p.execute(r);
1302            assertFalse(r.done);
1303        }
1304    }
1305
1306    /**
1158       * execute using DiscardOldestPolicy drops task on shutdown
1159       */
1160      public void testDiscardOldestOnShutdown() {
# Line 1322 | 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 {
1334 <                p.execute(null);
1335 <                shouldThrow();
1336 <            } catch (NullPointerException success) {}
1184 >            assertNullTaskSubmissionThrowsNullPointerException(p);
1185          }
1186      }
1187  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines