42 |
|
* execute successfully executes a runnable |
43 |
|
*/ |
44 |
|
public void testExecute() throws InterruptedException { |
45 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
45 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
46 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
47 |
|
final CountDownLatch done = new CountDownLatch(1); |
48 |
|
final Runnable task = new CheckedRunnable() { |
56 |
|
* delayed schedule of callable successfully executes after delay |
57 |
|
*/ |
58 |
|
public void testSchedule1() throws Exception { |
59 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
59 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
60 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
61 |
|
final long startTime = System.nanoTime(); |
62 |
|
final CountDownLatch done = new CountDownLatch(1); |
77 |
|
* delayed schedule of runnable successfully executes after delay |
78 |
|
*/ |
79 |
|
public void testSchedule3() throws Exception { |
80 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
80 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
81 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
82 |
|
final long startTime = System.nanoTime(); |
83 |
|
final CountDownLatch done = new CountDownLatch(1); |
97 |
|
* scheduleAtFixedRate executes runnable after given initial delay |
98 |
|
*/ |
99 |
|
public void testSchedule4() throws Exception { |
100 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
100 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
101 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
102 |
|
final long startTime = System.nanoTime(); |
103 |
|
final CountDownLatch done = new CountDownLatch(1); |
119 |
|
* scheduleWithFixedDelay executes runnable after given initial delay |
120 |
|
*/ |
121 |
|
public void testSchedule5() throws Exception { |
122 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
122 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
123 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
124 |
|
final long startTime = System.nanoTime(); |
125 |
|
final CountDownLatch done = new CountDownLatch(1); |
146 |
|
* scheduleAtFixedRate executes series of tasks at given rate |
147 |
|
*/ |
148 |
|
public void testFixedRateSequence() throws InterruptedException { |
149 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
149 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
150 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
151 |
|
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
152 |
|
long startTime = System.nanoTime(); |
172 |
|
* scheduleWithFixedDelay executes series of tasks with given period |
173 |
|
*/ |
174 |
|
public void testFixedDelaySequence() throws InterruptedException { |
175 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
175 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
176 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
177 |
|
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
178 |
|
long startTime = System.nanoTime(); |
198 |
|
* execute(null) throws NPE |
199 |
|
*/ |
200 |
|
public void testExecuteNull() throws InterruptedException { |
201 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
201 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
202 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
203 |
|
try { |
204 |
|
p.execute(null); |
225 |
|
* execute throws RejectedExecutionException if shutdown |
226 |
|
*/ |
227 |
|
public void testSchedule1_RejectedExecutionException() throws InterruptedException { |
228 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
228 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
229 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
230 |
|
try { |
231 |
|
p.shutdown(); |
241 |
|
* schedule throws RejectedExecutionException if shutdown |
242 |
|
*/ |
243 |
|
public void testSchedule2_RejectedExecutionException() throws InterruptedException { |
244 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
244 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
245 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
246 |
|
try { |
247 |
|
p.shutdown(); |
257 |
|
* schedule callable throws RejectedExecutionException if shutdown |
258 |
|
*/ |
259 |
|
public void testSchedule3_RejectedExecutionException() throws InterruptedException { |
260 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
260 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
261 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
262 |
|
try { |
263 |
|
p.shutdown(); |
273 |
|
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
274 |
|
*/ |
275 |
|
public void testScheduleAtFixedRate1_RejectedExecutionException() throws InterruptedException { |
276 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
276 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
277 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
278 |
|
try { |
279 |
|
p.shutdown(); |
289 |
|
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown |
290 |
|
*/ |
291 |
|
public void testScheduleWithFixedDelay1_RejectedExecutionException() throws InterruptedException { |
292 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
292 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
293 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
294 |
|
try { |
295 |
|
p.shutdown(); |
306 |
|
* thread becomes active |
307 |
|
*/ |
308 |
|
public void testGetActiveCount() throws InterruptedException { |
309 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
310 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2); |
311 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
311 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
312 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
312 |
– |
final CountDownLatch done = new CountDownLatch(1); |
313 |
|
assertEquals(0, p.getActiveCount()); |
314 |
|
p.execute(new CheckedRunnable() { |
315 |
|
public void realRun() throws InterruptedException { |
317 |
|
assertEquals(1, p.getActiveCount()); |
318 |
|
await(done); |
319 |
|
}}); |
320 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
320 |
> |
await(threadStarted); |
321 |
|
assertEquals(1, p.getActiveCount()); |
322 |
– |
done.countDown(); |
322 |
|
} |
323 |
|
} |
324 |
|
|
425 |
|
threadStarted.countDown(); |
426 |
|
await(done); |
427 |
|
}}); |
428 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
428 |
> |
await(threadStarted); |
429 |
|
assertEquals(1, p.getTaskCount()); |
430 |
|
assertEquals(0, p.getCompletedTaskCount()); |
431 |
|
for (int i = 0; i < TASKS; i++) { |
461 |
|
*/ |
462 |
|
public void testSetThreadFactory() throws InterruptedException { |
463 |
|
ThreadFactory threadFactory = new SimpleThreadFactory(); |
464 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
464 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
465 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
466 |
|
p.setThreadFactory(threadFactory); |
467 |
|
assertSame(threadFactory, p.getThreadFactory()); |
472 |
|
* setThreadFactory(null) throws NPE |
473 |
|
*/ |
474 |
|
public void testSetThreadFactoryNull() throws InterruptedException { |
475 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
475 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
476 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
477 |
|
try { |
478 |
|
p.setThreadFactory(null); |
486 |
|
*/ |
487 |
|
public void testIsShutdown() { |
488 |
|
|
489 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
489 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
490 |
|
try { |
491 |
|
assertFalse(p.isShutdown()); |
492 |
|
} |
511 |
|
threadStarted.countDown(); |
512 |
|
await(done); |
513 |
|
}}); |
514 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
514 |
> |
await(threadStarted); |
515 |
|
assertFalse(p.isTerminating()); |
516 |
|
done.countDown(); |
517 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
535 |
|
threadStarted.countDown(); |
536 |
|
await(done); |
537 |
|
}}); |
538 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
538 |
> |
await(threadStarted); |
539 |
|
assertFalse(p.isTerminating()); |
540 |
|
done.countDown(); |
541 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
549 |
|
* getQueue returns the work queue, which contains queued tasks |
550 |
|
*/ |
551 |
|
public void testGetQueue() throws InterruptedException { |
552 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
553 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
552 |
> |
final CountDownLatch done = new CountDownLatch(1); |
553 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
554 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
555 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
556 |
– |
final CountDownLatch done = new CountDownLatch(1); |
556 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
557 |
|
for (int i = 0; i < tasks.length; i++) { |
558 |
|
Runnable r = new CheckedRunnable() { |
562 |
|
}}; |
563 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
564 |
|
} |
565 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
565 |
> |
await(threadStarted); |
566 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
567 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
568 |
|
assertFalse(q.contains(tasks[0])); |
570 |
– |
done.countDown(); |
569 |
|
} |
570 |
|
} |
571 |
|
|
573 |
|
* remove(task) removes queued task, and fails to remove active task |
574 |
|
*/ |
575 |
|
public void testRemove() throws InterruptedException { |
576 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
577 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
578 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
578 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
579 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
580 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
582 |
– |
final CountDownLatch done = new CountDownLatch(1); |
581 |
|
for (int i = 0; i < tasks.length; i++) { |
582 |
|
Runnable r = new CheckedRunnable() { |
583 |
|
public void realRun() throws InterruptedException { |
586 |
|
}}; |
587 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
588 |
|
} |
589 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
589 |
> |
await(threadStarted); |
590 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
591 |
|
assertFalse(p.remove((Runnable)tasks[0])); |
592 |
|
assertTrue(q.contains((Runnable)tasks[4])); |
597 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
598 |
|
assertTrue(p.remove((Runnable)tasks[3])); |
599 |
|
assertFalse(q.contains((Runnable)tasks[3])); |
602 |
– |
done.countDown(); |
600 |
|
} |
601 |
|
} |
602 |
|
|
672 |
|
* and those tasks are drained from the queue |
673 |
|
*/ |
674 |
|
public void testShutdownNow_delayedTasks() throws InterruptedException { |
675 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
675 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
676 |
|
List<ScheduledFuture> tasks = new ArrayList<>(); |
677 |
|
for (int i = 0; i < 3; i++) { |
678 |
|
Runnable r = new NoOpRunnable(); |
1046 |
|
public void testTimedInvokeAny4() throws Exception { |
1047 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1048 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1049 |
+ |
long startTime = System.nanoTime(); |
1050 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1051 |
|
l.add(new NPETask()); |
1052 |
|
try { |
1053 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1053 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1054 |
|
shouldThrow(); |
1055 |
|
} catch (ExecutionException success) { |
1056 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1057 |
|
} |
1058 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1059 |
|
} |
1060 |
|
} |
1061 |
|
|
1065 |
|
public void testTimedInvokeAny5() throws Exception { |
1066 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1067 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1068 |
+ |
long startTime = System.nanoTime(); |
1069 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1070 |
|
l.add(new StringTask()); |
1071 |
|
l.add(new StringTask()); |
1072 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1072 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1073 |
|
assertSame(TEST_STRING, result); |
1074 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1075 |
|
} |
1076 |
|
} |
1077 |
|
|
1172 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1173 |
|
*/ |
1174 |
|
public void testTimedInvokeAll6() throws Exception { |
1175 |
< |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1176 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
1177 |
< |
for (long timeout = timeoutMillis();;) { |
1175 |
> |
for (long timeout = timeoutMillis();;) { |
1176 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1177 |
> |
final Callable<String> waiter = new CheckedCallable<String>() { |
1178 |
> |
public String realCall() { |
1179 |
> |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1180 |
> |
catch (InterruptedException ok) {} |
1181 |
> |
return "1"; }}; |
1182 |
> |
final ExecutorService p = new ScheduledThreadPoolExecutor(2); |
1183 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1184 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1185 |
|
tasks.add(new StringTask("0")); |
1186 |
< |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1186 |
> |
tasks.add(waiter); |
1187 |
|
tasks.add(new StringTask("2")); |
1188 |
|
long startTime = System.nanoTime(); |
1189 |
|
List<Future<String>> futures = |
1190 |
< |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1190 |
> |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1191 |
|
assertEquals(tasks.size(), futures.size()); |
1192 |
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1193 |
|
for (Future future : futures) |
1206 |
|
} |
1207 |
|
} |
1208 |
|
|
1209 |
+ |
/** |
1210 |
+ |
* A fixed delay task with overflowing period should not prevent a |
1211 |
+ |
* one-shot task from executing. |
1212 |
+ |
* https://bugs.openjdk.java.net/browse/JDK-8051859 |
1213 |
+ |
*/ |
1214 |
+ |
public void testScheduleWithFixedDelay_overflow() throws Exception { |
1215 |
+ |
final CountDownLatch delayedDone = new CountDownLatch(1); |
1216 |
+ |
final CountDownLatch immediateDone = new CountDownLatch(1); |
1217 |
+ |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
1218 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
1219 |
+ |
final Runnable immediate = new Runnable() { public void run() { |
1220 |
+ |
immediateDone.countDown(); |
1221 |
+ |
}}; |
1222 |
+ |
final Runnable delayed = new Runnable() { public void run() { |
1223 |
+ |
delayedDone.countDown(); |
1224 |
+ |
p.submit(immediate); |
1225 |
+ |
}}; |
1226 |
+ |
p.scheduleWithFixedDelay(delayed, 0L, Long.MAX_VALUE, SECONDS); |
1227 |
+ |
await(delayedDone); |
1228 |
+ |
await(immediateDone); |
1229 |
+ |
} |
1230 |
+ |
} |
1231 |
+ |
|
1232 |
|
} |