7 |
|
*/ |
8 |
|
|
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
+ |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
11 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
12 |
|
|
13 |
|
import java.util.ArrayList; |
14 |
+ |
import java.util.Collection; |
15 |
+ |
import java.util.Collections; |
16 |
|
import java.util.HashSet; |
17 |
|
import java.util.List; |
18 |
|
import java.util.concurrent.BlockingQueue; |
20 |
|
import java.util.concurrent.CancellationException; |
21 |
|
import java.util.concurrent.CountDownLatch; |
22 |
|
import java.util.concurrent.ExecutionException; |
20 |
– |
import java.util.concurrent.Executors; |
23 |
|
import java.util.concurrent.ExecutorService; |
24 |
|
import java.util.concurrent.Future; |
25 |
|
import java.util.concurrent.RejectedExecutionException; |
26 |
|
import java.util.concurrent.ScheduledFuture; |
27 |
|
import java.util.concurrent.ScheduledThreadPoolExecutor; |
28 |
|
import java.util.concurrent.ThreadFactory; |
29 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
30 |
|
import java.util.concurrent.ThreadPoolExecutor; |
31 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
32 |
|
import java.util.concurrent.atomic.AtomicInteger; |
33 |
+ |
import java.util.concurrent.atomic.AtomicLong; |
34 |
+ |
import java.util.stream.Stream; |
35 |
|
|
36 |
|
import junit.framework.Test; |
37 |
|
import junit.framework.TestSuite; |
48 |
|
* execute successfully executes a runnable |
49 |
|
*/ |
50 |
|
public void testExecute() throws InterruptedException { |
51 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
51 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
52 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
53 |
|
final CountDownLatch done = new CountDownLatch(1); |
54 |
|
final Runnable task = new CheckedRunnable() { |
55 |
|
public void realRun() { done.countDown(); }}; |
56 |
|
p.execute(task); |
57 |
< |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
57 |
> |
await(done); |
58 |
|
} |
59 |
|
} |
60 |
|
|
62 |
|
* delayed schedule of callable successfully executes after delay |
63 |
|
*/ |
64 |
|
public void testSchedule1() throws Exception { |
65 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
65 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
66 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
67 |
|
final long startTime = System.nanoTime(); |
68 |
|
final CountDownLatch done = new CountDownLatch(1); |
75 |
|
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
76 |
|
assertSame(Boolean.TRUE, f.get()); |
77 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
78 |
< |
assertTrue(done.await(0L, MILLISECONDS)); |
78 |
> |
assertEquals(0L, done.getCount()); |
79 |
|
} |
80 |
|
} |
81 |
|
|
83 |
|
* delayed schedule of runnable successfully executes after delay |
84 |
|
*/ |
85 |
|
public void testSchedule3() throws Exception { |
86 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
86 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
87 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
88 |
|
final long startTime = System.nanoTime(); |
89 |
|
final CountDownLatch done = new CountDownLatch(1); |
103 |
|
* scheduleAtFixedRate executes runnable after given initial delay |
104 |
|
*/ |
105 |
|
public void testSchedule4() throws Exception { |
106 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
106 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
107 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
108 |
|
final long startTime = System.nanoTime(); |
109 |
|
final CountDownLatch done = new CountDownLatch(1); |
125 |
|
* scheduleWithFixedDelay executes runnable after given initial delay |
126 |
|
*/ |
127 |
|
public void testSchedule5() throws Exception { |
128 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
128 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
129 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
130 |
|
final long startTime = System.nanoTime(); |
131 |
|
final CountDownLatch done = new CountDownLatch(1); |
149 |
|
} |
150 |
|
|
151 |
|
/** |
152 |
< |
* scheduleAtFixedRate executes series of tasks at given rate |
152 |
> |
* scheduleAtFixedRate executes series of tasks at given rate. |
153 |
> |
* Eventually, it must hold that: |
154 |
> |
* cycles - 1 <= elapsedMillis/delay < cycles |
155 |
|
*/ |
156 |
|
public void testFixedRateSequence() throws InterruptedException { |
157 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
157 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
158 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
159 |
|
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
160 |
< |
long startTime = System.nanoTime(); |
161 |
< |
int cycles = 10; |
160 |
> |
final long startTime = System.nanoTime(); |
161 |
> |
final int cycles = 8; |
162 |
|
final CountDownLatch done = new CountDownLatch(cycles); |
163 |
< |
Runnable task = new CheckedRunnable() { |
163 |
> |
final Runnable task = new CheckedRunnable() { |
164 |
|
public void realRun() { done.countDown(); }}; |
165 |
< |
ScheduledFuture h = |
165 |
> |
final ScheduledFuture periodicTask = |
166 |
|
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
167 |
< |
done.await(); |
168 |
< |
h.cancel(true); |
169 |
< |
double normalizedTime = |
170 |
< |
(double) millisElapsedSince(startTime) / delay; |
171 |
< |
if (normalizedTime >= cycles - 1 && |
172 |
< |
normalizedTime <= cycles) |
167 |
> |
final int totalDelayMillis = (cycles - 1) * delay; |
168 |
> |
await(done, totalDelayMillis + LONG_DELAY_MS); |
169 |
> |
periodicTask.cancel(true); |
170 |
> |
final long elapsedMillis = millisElapsedSince(startTime); |
171 |
> |
assertTrue(elapsedMillis >= totalDelayMillis); |
172 |
> |
if (elapsedMillis <= cycles * delay) |
173 |
|
return; |
174 |
+ |
// else retry with longer delay |
175 |
|
} |
176 |
< |
throw new AssertionError("unexpected execution rate"); |
176 |
> |
fail("unexpected execution rate"); |
177 |
|
} |
178 |
|
} |
179 |
|
|
180 |
|
/** |
181 |
< |
* scheduleWithFixedDelay executes series of tasks with given period |
181 |
> |
* scheduleWithFixedDelay executes series of tasks with given period. |
182 |
> |
* Eventually, it must hold that each task starts at least delay and at |
183 |
> |
* most 2 * delay after the termination of the previous task. |
184 |
|
*/ |
185 |
|
public void testFixedDelaySequence() throws InterruptedException { |
186 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
186 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
187 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
188 |
|
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
189 |
< |
long startTime = System.nanoTime(); |
190 |
< |
int cycles = 10; |
189 |
> |
final long startTime = System.nanoTime(); |
190 |
> |
final AtomicLong previous = new AtomicLong(startTime); |
191 |
> |
final AtomicBoolean tryLongerDelay = new AtomicBoolean(false); |
192 |
> |
final int cycles = 8; |
193 |
|
final CountDownLatch done = new CountDownLatch(cycles); |
194 |
< |
Runnable task = new CheckedRunnable() { |
195 |
< |
public void realRun() { done.countDown(); }}; |
196 |
< |
ScheduledFuture h = |
194 |
> |
final int d = delay; |
195 |
> |
final Runnable task = new CheckedRunnable() { |
196 |
> |
public void realRun() { |
197 |
> |
long now = System.nanoTime(); |
198 |
> |
long elapsedMillis |
199 |
> |
= NANOSECONDS.toMillis(now - previous.get()); |
200 |
> |
if (done.getCount() == cycles) { // first execution |
201 |
> |
if (elapsedMillis >= d) |
202 |
> |
tryLongerDelay.set(true); |
203 |
> |
} else { |
204 |
> |
assertTrue(elapsedMillis >= d); |
205 |
> |
if (elapsedMillis >= 2 * d) |
206 |
> |
tryLongerDelay.set(true); |
207 |
> |
} |
208 |
> |
previous.set(now); |
209 |
> |
done.countDown(); |
210 |
> |
}}; |
211 |
> |
final ScheduledFuture periodicTask = |
212 |
|
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
213 |
< |
done.await(); |
214 |
< |
h.cancel(true); |
215 |
< |
double normalizedTime = |
216 |
< |
(double) millisElapsedSince(startTime) / delay; |
217 |
< |
if (normalizedTime >= cycles - 1 && |
218 |
< |
normalizedTime <= cycles) |
213 |
> |
final int totalDelayMillis = (cycles - 1) * delay; |
214 |
> |
await(done, totalDelayMillis + cycles * LONG_DELAY_MS); |
215 |
> |
periodicTask.cancel(true); |
216 |
> |
final long elapsedMillis = millisElapsedSince(startTime); |
217 |
> |
assertTrue(elapsedMillis >= totalDelayMillis); |
218 |
> |
if (!tryLongerDelay.get()) |
219 |
|
return; |
220 |
+ |
// else retry with longer delay |
221 |
|
} |
222 |
< |
throw new AssertionError("unexpected execution rate"); |
222 |
> |
fail("unexpected execution rate"); |
223 |
|
} |
224 |
|
} |
225 |
|
|
227 |
|
* execute(null) throws NPE |
228 |
|
*/ |
229 |
|
public void testExecuteNull() throws InterruptedException { |
230 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
230 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
231 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
232 |
|
try { |
233 |
|
p.execute(null); |
243 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
244 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
245 |
|
try { |
246 |
< |
TrackedCallable callable = null; |
247 |
< |
Future f = p.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
246 |
> |
Future f = p.schedule((Callable)null, |
247 |
> |
randomTimeout(), randomTimeUnit()); |
248 |
|
shouldThrow(); |
249 |
|
} catch (NullPointerException success) {} |
250 |
|
} |
254 |
|
* execute throws RejectedExecutionException if shutdown |
255 |
|
*/ |
256 |
|
public void testSchedule1_RejectedExecutionException() throws InterruptedException { |
257 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
257 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
258 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
259 |
|
try { |
260 |
|
p.shutdown(); |
261 |
|
p.schedule(new NoOpRunnable(), |
262 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
262 |
> |
randomTimeout(), randomTimeUnit()); |
263 |
|
shouldThrow(); |
264 |
|
} catch (RejectedExecutionException success) { |
265 |
|
} catch (SecurityException ok) {} |
270 |
|
* schedule throws RejectedExecutionException if shutdown |
271 |
|
*/ |
272 |
|
public void testSchedule2_RejectedExecutionException() throws InterruptedException { |
273 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
273 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
274 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
275 |
|
try { |
276 |
|
p.shutdown(); |
277 |
|
p.schedule(new NoOpCallable(), |
278 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
278 |
> |
randomTimeout(), randomTimeUnit()); |
279 |
|
shouldThrow(); |
280 |
|
} catch (RejectedExecutionException success) { |
281 |
|
} catch (SecurityException ok) {} |
286 |
|
* schedule callable throws RejectedExecutionException if shutdown |
287 |
|
*/ |
288 |
|
public void testSchedule3_RejectedExecutionException() throws InterruptedException { |
289 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
289 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
290 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
291 |
|
try { |
292 |
|
p.shutdown(); |
293 |
|
p.schedule(new NoOpCallable(), |
294 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
294 |
> |
randomTimeout(), randomTimeUnit()); |
295 |
|
shouldThrow(); |
296 |
|
} catch (RejectedExecutionException success) { |
297 |
|
} catch (SecurityException ok) {} |
302 |
|
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
303 |
|
*/ |
304 |
|
public void testScheduleAtFixedRate1_RejectedExecutionException() throws InterruptedException { |
305 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
305 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
306 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
307 |
|
try { |
308 |
|
p.shutdown(); |
318 |
|
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown |
319 |
|
*/ |
320 |
|
public void testScheduleWithFixedDelay1_RejectedExecutionException() throws InterruptedException { |
321 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
321 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
322 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
323 |
|
try { |
324 |
|
p.shutdown(); |
335 |
|
* thread becomes active |
336 |
|
*/ |
337 |
|
public void testGetActiveCount() throws InterruptedException { |
338 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
339 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2); |
340 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
340 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
341 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
312 |
– |
final CountDownLatch done = new CountDownLatch(1); |
342 |
|
assertEquals(0, p.getActiveCount()); |
343 |
|
p.execute(new CheckedRunnable() { |
344 |
|
public void realRun() throws InterruptedException { |
345 |
|
threadStarted.countDown(); |
346 |
|
assertEquals(1, p.getActiveCount()); |
347 |
< |
done.await(); |
347 |
> |
await(done); |
348 |
|
}}); |
349 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
349 |
> |
await(threadStarted); |
350 |
|
assertEquals(1, p.getActiveCount()); |
322 |
– |
done.countDown(); |
351 |
|
} |
352 |
|
} |
353 |
|
|
366 |
|
public void realRun() throws InterruptedException { |
367 |
|
threadStarted.countDown(); |
368 |
|
assertEquals(0, p.getCompletedTaskCount()); |
369 |
< |
threadProceed.await(); |
369 |
> |
await(threadProceed); |
370 |
|
threadDone.countDown(); |
371 |
|
}}); |
372 |
|
await(threadStarted); |
373 |
|
assertEquals(0, p.getCompletedTaskCount()); |
374 |
|
threadProceed.countDown(); |
375 |
< |
threadDone.await(); |
375 |
> |
await(threadDone); |
376 |
|
long startTime = System.nanoTime(); |
377 |
|
while (p.getCompletedTaskCount() != 1) { |
378 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
401 |
|
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(THREADS); |
402 |
|
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
403 |
|
final CountDownLatch done = new CountDownLatch(1); |
404 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
404 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
405 |
|
assertEquals(0, p.getLargestPoolSize()); |
406 |
|
for (int i = 0; i < THREADS; i++) |
407 |
|
p.execute(new CheckedRunnable() { |
408 |
|
public void realRun() throws InterruptedException { |
409 |
|
threadsStarted.countDown(); |
410 |
< |
done.await(); |
410 |
> |
await(done); |
411 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
412 |
|
}}); |
413 |
< |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
413 |
> |
await(threadsStarted); |
414 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
387 |
– |
done.countDown(); |
415 |
|
} |
416 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
417 |
|
} |
424 |
|
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
425 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
426 |
|
final CountDownLatch done = new CountDownLatch(1); |
427 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
427 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
428 |
|
assertEquals(0, p.getPoolSize()); |
429 |
|
p.execute(new CheckedRunnable() { |
430 |
|
public void realRun() throws InterruptedException { |
431 |
|
threadStarted.countDown(); |
432 |
|
assertEquals(1, p.getPoolSize()); |
433 |
< |
done.await(); |
433 |
> |
await(done); |
434 |
|
}}); |
435 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
435 |
> |
await(threadStarted); |
436 |
|
assertEquals(1, p.getPoolSize()); |
410 |
– |
done.countDown(); |
437 |
|
} |
438 |
|
} |
439 |
|
|
452 |
|
p.execute(new CheckedRunnable() { |
453 |
|
public void realRun() throws InterruptedException { |
454 |
|
threadStarted.countDown(); |
455 |
< |
done.await(); |
455 |
> |
await(done); |
456 |
|
}}); |
457 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
457 |
> |
await(threadStarted); |
458 |
|
assertEquals(1, p.getTaskCount()); |
459 |
|
assertEquals(0, p.getCompletedTaskCount()); |
460 |
|
for (int i = 0; i < TASKS; i++) { |
463 |
|
public void realRun() throws InterruptedException { |
464 |
|
threadStarted.countDown(); |
465 |
|
assertEquals(1 + TASKS, p.getTaskCount()); |
466 |
< |
done.await(); |
466 |
> |
await(done); |
467 |
|
}}); |
468 |
|
} |
469 |
|
assertEquals(1 + TASKS, p.getTaskCount()); |
490 |
|
*/ |
491 |
|
public void testSetThreadFactory() throws InterruptedException { |
492 |
|
ThreadFactory threadFactory = new SimpleThreadFactory(); |
493 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
493 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
494 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
495 |
|
p.setThreadFactory(threadFactory); |
496 |
|
assertSame(threadFactory, p.getThreadFactory()); |
501 |
|
* setThreadFactory(null) throws NPE |
502 |
|
*/ |
503 |
|
public void testSetThreadFactoryNull() throws InterruptedException { |
504 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
504 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
505 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
506 |
|
try { |
507 |
|
p.setThreadFactory(null); |
511 |
|
} |
512 |
|
|
513 |
|
/** |
514 |
+ |
* The default rejected execution handler is AbortPolicy. |
515 |
+ |
*/ |
516 |
+ |
public void testDefaultRejectedExecutionHandler() { |
517 |
+ |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
518 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
519 |
+ |
assertTrue(p.getRejectedExecutionHandler() |
520 |
+ |
instanceof ThreadPoolExecutor.AbortPolicy); |
521 |
+ |
} |
522 |
+ |
} |
523 |
+ |
|
524 |
+ |
/** |
525 |
|
* isShutdown is false before shutdown, true after |
526 |
|
*/ |
527 |
|
public void testIsShutdown() { |
528 |
< |
|
529 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
530 |
< |
try { |
531 |
< |
assertFalse(p.isShutdown()); |
532 |
< |
} |
533 |
< |
finally { |
534 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
528 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
529 |
> |
assertFalse(p.isShutdown()); |
530 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
531 |
> |
try { |
532 |
> |
p.shutdown(); |
533 |
> |
assertTrue(p.isShutdown()); |
534 |
> |
} catch (SecurityException ok) {} |
535 |
|
} |
499 |
– |
assertTrue(p.isShutdown()); |
536 |
|
} |
537 |
|
|
538 |
|
/** |
548 |
|
public void realRun() throws InterruptedException { |
549 |
|
assertFalse(p.isTerminated()); |
550 |
|
threadStarted.countDown(); |
551 |
< |
done.await(); |
551 |
> |
await(done); |
552 |
|
}}); |
553 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
553 |
> |
await(threadStarted); |
554 |
|
assertFalse(p.isTerminating()); |
555 |
|
done.countDown(); |
556 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
572 |
|
public void realRun() throws InterruptedException { |
573 |
|
assertFalse(p.isTerminating()); |
574 |
|
threadStarted.countDown(); |
575 |
< |
done.await(); |
575 |
> |
await(done); |
576 |
|
}}); |
577 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
577 |
> |
await(threadStarted); |
578 |
|
assertFalse(p.isTerminating()); |
579 |
|
done.countDown(); |
580 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
588 |
|
* getQueue returns the work queue, which contains queued tasks |
589 |
|
*/ |
590 |
|
public void testGetQueue() throws InterruptedException { |
591 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
592 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
591 |
> |
final CountDownLatch done = new CountDownLatch(1); |
592 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
593 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
594 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
558 |
– |
final CountDownLatch done = new CountDownLatch(1); |
595 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
596 |
|
for (int i = 0; i < tasks.length; i++) { |
597 |
|
Runnable r = new CheckedRunnable() { |
598 |
|
public void realRun() throws InterruptedException { |
599 |
|
threadStarted.countDown(); |
600 |
< |
done.await(); |
600 |
> |
await(done); |
601 |
|
}}; |
602 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
603 |
|
} |
604 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
604 |
> |
await(threadStarted); |
605 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
606 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
607 |
|
assertFalse(q.contains(tasks[0])); |
572 |
– |
done.countDown(); |
608 |
|
} |
609 |
|
} |
610 |
|
|
612 |
|
* remove(task) removes queued task, and fails to remove active task |
613 |
|
*/ |
614 |
|
public void testRemove() throws InterruptedException { |
615 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
616 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
617 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
617 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
618 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
619 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
584 |
– |
final CountDownLatch done = new CountDownLatch(1); |
620 |
|
for (int i = 0; i < tasks.length; i++) { |
621 |
|
Runnable r = new CheckedRunnable() { |
622 |
|
public void realRun() throws InterruptedException { |
623 |
|
threadStarted.countDown(); |
624 |
< |
done.await(); |
624 |
> |
await(done); |
625 |
|
}}; |
626 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
627 |
|
} |
628 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
628 |
> |
await(threadStarted); |
629 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
630 |
|
assertFalse(p.remove((Runnable)tasks[0])); |
631 |
|
assertTrue(q.contains((Runnable)tasks[4])); |
636 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
637 |
|
assertTrue(p.remove((Runnable)tasks[3])); |
638 |
|
assertFalse(q.contains((Runnable)tasks[3])); |
604 |
– |
done.countDown(); |
639 |
|
} |
640 |
|
} |
641 |
|
|
688 |
|
}}; |
689 |
|
for (int i = 0; i < count; i++) |
690 |
|
p.execute(waiter); |
691 |
< |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
691 |
> |
await(threadsStarted); |
692 |
|
assertEquals(poolSize, p.getActiveCount()); |
693 |
|
assertEquals(0, p.getCompletedTaskCount()); |
694 |
|
final List<Runnable> queuedTasks; |
711 |
|
* and those tasks are drained from the queue |
712 |
|
*/ |
713 |
|
public void testShutdownNow_delayedTasks() throws InterruptedException { |
714 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
714 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
715 |
|
List<ScheduledFuture> tasks = new ArrayList<>(); |
716 |
|
for (int i = 0; i < 3; i++) { |
717 |
|
Runnable r = new NoOpRunnable(); |
748 |
|
* - setContinueExistingPeriodicTasksAfterShutdownPolicy |
749 |
|
*/ |
750 |
|
public void testShutdown_cancellation() throws Exception { |
751 |
< |
Boolean[] allBooleans = { null, Boolean.FALSE, Boolean.TRUE }; |
718 |
< |
for (Boolean policy : allBooleans) |
719 |
< |
{ |
720 |
< |
final int poolSize = 2; |
751 |
> |
final int poolSize = 4; |
752 |
|
final ScheduledThreadPoolExecutor p |
753 |
|
= new ScheduledThreadPoolExecutor(poolSize); |
754 |
< |
final boolean effectiveDelayedPolicy = (policy != Boolean.FALSE); |
755 |
< |
final boolean effectivePeriodicPolicy = (policy == Boolean.TRUE); |
756 |
< |
final boolean effectiveRemovePolicy = (policy == Boolean.TRUE); |
757 |
< |
if (policy != null) { |
758 |
< |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(policy); |
759 |
< |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(policy); |
760 |
< |
p.setRemoveOnCancelPolicy(policy); |
761 |
< |
} |
754 |
> |
final BlockingQueue<Runnable> q = p.getQueue(); |
755 |
> |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
756 |
> |
final long delay = rnd.nextInt(2); |
757 |
> |
final int rounds = rnd.nextInt(1, 3); |
758 |
> |
final boolean effectiveDelayedPolicy; |
759 |
> |
final boolean effectivePeriodicPolicy; |
760 |
> |
final boolean effectiveRemovePolicy; |
761 |
> |
|
762 |
> |
if (rnd.nextBoolean()) |
763 |
> |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy( |
764 |
> |
effectiveDelayedPolicy = rnd.nextBoolean()); |
765 |
> |
else |
766 |
> |
effectiveDelayedPolicy = true; |
767 |
|
assertEquals(effectiveDelayedPolicy, |
768 |
|
p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
769 |
+ |
|
770 |
+ |
if (rnd.nextBoolean()) |
771 |
+ |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy( |
772 |
+ |
effectivePeriodicPolicy = rnd.nextBoolean()); |
773 |
+ |
else |
774 |
+ |
effectivePeriodicPolicy = false; |
775 |
|
assertEquals(effectivePeriodicPolicy, |
776 |
|
p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
777 |
+ |
|
778 |
+ |
if (rnd.nextBoolean()) |
779 |
+ |
p.setRemoveOnCancelPolicy( |
780 |
+ |
effectiveRemovePolicy = rnd.nextBoolean()); |
781 |
+ |
else |
782 |
+ |
effectiveRemovePolicy = false; |
783 |
|
assertEquals(effectiveRemovePolicy, |
784 |
|
p.getRemoveOnCancelPolicy()); |
785 |
< |
// Strategy: Wedge the pool with poolSize "blocker" threads |
785 |
> |
|
786 |
> |
final boolean periodicTasksContinue = effectivePeriodicPolicy && rnd.nextBoolean(); |
787 |
> |
|
788 |
> |
// Strategy: Wedge the pool with one wave of "blocker" tasks, |
789 |
> |
// then add a second wave that waits in the queue until unblocked. |
790 |
|
final AtomicInteger ran = new AtomicInteger(0); |
791 |
|
final CountDownLatch poolBlocked = new CountDownLatch(poolSize); |
792 |
|
final CountDownLatch unblock = new CountDownLatch(1); |
793 |
< |
final CountDownLatch periodicLatch1 = new CountDownLatch(2); |
742 |
< |
final CountDownLatch periodicLatch2 = new CountDownLatch(2); |
743 |
< |
Runnable task = new CheckedRunnable() { public void realRun() |
744 |
< |
throws InterruptedException { |
745 |
< |
poolBlocked.countDown(); |
746 |
< |
assertTrue(unblock.await(LONG_DELAY_MS, MILLISECONDS)); |
747 |
< |
ran.getAndIncrement(); |
748 |
< |
}}; |
749 |
< |
List<Future<?>> blockers = new ArrayList<>(); |
750 |
< |
List<Future<?>> periodics = new ArrayList<>(); |
751 |
< |
List<Future<?>> delayeds = new ArrayList<>(); |
752 |
< |
for (int i = 0; i < poolSize; i++) |
753 |
< |
blockers.add(p.submit(task)); |
754 |
< |
assertTrue(poolBlocked.await(LONG_DELAY_MS, MILLISECONDS)); |
755 |
< |
|
756 |
< |
periodics.add(p.scheduleAtFixedRate(countDowner(periodicLatch1), |
757 |
< |
1, 1, MILLISECONDS)); |
758 |
< |
periodics.add(p.scheduleWithFixedDelay(countDowner(periodicLatch2), |
759 |
< |
1, 1, MILLISECONDS)); |
760 |
< |
delayeds.add(p.schedule(task, 1, MILLISECONDS)); |
793 |
> |
final RuntimeException exception = new RuntimeException(); |
794 |
|
|
795 |
< |
assertTrue(p.getQueue().containsAll(periodics)); |
796 |
< |
assertTrue(p.getQueue().containsAll(delayeds)); |
797 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
798 |
< |
assertTrue(p.isShutdown()); |
799 |
< |
assertFalse(p.isTerminated()); |
800 |
< |
for (Future<?> periodic : periodics) { |
801 |
< |
assertTrue(effectivePeriodicPolicy ^ periodic.isCancelled()); |
769 |
< |
assertTrue(effectivePeriodicPolicy ^ periodic.isDone()); |
770 |
< |
} |
771 |
< |
for (Future<?> delayed : delayeds) { |
772 |
< |
assertTrue(effectiveDelayedPolicy ^ delayed.isCancelled()); |
773 |
< |
assertTrue(effectiveDelayedPolicy ^ delayed.isDone()); |
774 |
< |
} |
775 |
< |
if (testImplementationDetails) { |
776 |
< |
assertEquals(effectivePeriodicPolicy, |
777 |
< |
p.getQueue().containsAll(periodics)); |
778 |
< |
assertEquals(effectiveDelayedPolicy, |
779 |
< |
p.getQueue().containsAll(delayeds)); |
780 |
< |
} |
781 |
< |
// Release all pool threads |
782 |
< |
unblock.countDown(); |
783 |
< |
|
784 |
< |
for (Future<?> delayed : delayeds) { |
785 |
< |
if (effectiveDelayedPolicy) { |
786 |
< |
assertNull(delayed.get()); |
795 |
> |
class Task implements Runnable { |
796 |
> |
public void run() { |
797 |
> |
try { |
798 |
> |
ran.getAndIncrement(); |
799 |
> |
poolBlocked.countDown(); |
800 |
> |
await(unblock); |
801 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
802 |
|
} |
803 |
|
} |
804 |
< |
if (effectivePeriodicPolicy) { |
805 |
< |
assertTrue(periodicLatch1.await(LONG_DELAY_MS, MILLISECONDS)); |
806 |
< |
assertTrue(periodicLatch2.await(LONG_DELAY_MS, MILLISECONDS)); |
807 |
< |
for (Future<?> periodic : periodics) { |
808 |
< |
assertTrue(periodic.cancel(false)); |
809 |
< |
assertTrue(periodic.isCancelled()); |
810 |
< |
assertTrue(periodic.isDone()); |
804 |
> |
|
805 |
> |
class PeriodicTask extends Task { |
806 |
> |
PeriodicTask(int rounds) { this.rounds = rounds; } |
807 |
> |
int rounds; |
808 |
> |
public void run() { |
809 |
> |
if (--rounds == 0) super.run(); |
810 |
> |
// throw exception to surely terminate this periodic task, |
811 |
> |
// but in a separate execution and in a detectable way. |
812 |
> |
if (rounds == -1) throw exception; |
813 |
|
} |
814 |
|
} |
815 |
+ |
|
816 |
+ |
Runnable task = new Task(); |
817 |
+ |
|
818 |
+ |
List<Future<?>> immediates = new ArrayList<>(); |
819 |
+ |
List<Future<?>> delayeds = new ArrayList<>(); |
820 |
+ |
List<Future<?>> periodics = new ArrayList<>(); |
821 |
+ |
|
822 |
+ |
immediates.add(p.submit(task)); |
823 |
+ |
delayeds.add(p.schedule(task, delay, MILLISECONDS)); |
824 |
+ |
periodics.add(p.scheduleAtFixedRate( |
825 |
+ |
new PeriodicTask(rounds), delay, 1, MILLISECONDS)); |
826 |
+ |
periodics.add(p.scheduleWithFixedDelay( |
827 |
+ |
new PeriodicTask(rounds), delay, 1, MILLISECONDS)); |
828 |
+ |
|
829 |
+ |
await(poolBlocked); |
830 |
+ |
|
831 |
+ |
assertEquals(poolSize, ran.get()); |
832 |
+ |
assertEquals(poolSize, p.getActiveCount()); |
833 |
+ |
assertTrue(q.isEmpty()); |
834 |
+ |
|
835 |
+ |
// Add second wave of tasks. |
836 |
+ |
immediates.add(p.submit(task)); |
837 |
+ |
delayeds.add(p.schedule(task, effectiveDelayedPolicy ? delay : LONG_DELAY_MS, MILLISECONDS)); |
838 |
+ |
periodics.add(p.scheduleAtFixedRate( |
839 |
+ |
new PeriodicTask(rounds), delay, 1, MILLISECONDS)); |
840 |
+ |
periodics.add(p.scheduleWithFixedDelay( |
841 |
+ |
new PeriodicTask(rounds), delay, 1, MILLISECONDS)); |
842 |
+ |
|
843 |
+ |
assertEquals(poolSize, q.size()); |
844 |
+ |
assertEquals(poolSize, ran.get()); |
845 |
+ |
|
846 |
+ |
immediates.forEach( |
847 |
+ |
f -> assertTrue(((ScheduledFuture)f).getDelay(NANOSECONDS) <= 0L)); |
848 |
+ |
|
849 |
+ |
Stream.of(immediates, delayeds, periodics).flatMap(c -> c.stream()) |
850 |
+ |
.forEach(f -> assertFalse(f.isDone())); |
851 |
+ |
|
852 |
+ |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
853 |
+ |
assertTrue(p.isShutdown()); |
854 |
+ |
assertTrue(p.isTerminating()); |
855 |
+ |
assertFalse(p.isTerminated()); |
856 |
+ |
|
857 |
+ |
if (rnd.nextBoolean()) |
858 |
+ |
assertThrows( |
859 |
+ |
RejectedExecutionException.class, |
860 |
+ |
() -> p.submit(task), |
861 |
+ |
() -> p.schedule(task, 1, SECONDS), |
862 |
+ |
() -> p.scheduleAtFixedRate( |
863 |
+ |
new PeriodicTask(1), 1, 1, SECONDS), |
864 |
+ |
() -> p.scheduleWithFixedDelay( |
865 |
+ |
new PeriodicTask(2), 1, 1, SECONDS)); |
866 |
+ |
|
867 |
+ |
assertTrue(q.contains(immediates.get(1))); |
868 |
+ |
assertTrue(!effectiveDelayedPolicy |
869 |
+ |
^ q.contains(delayeds.get(1))); |
870 |
+ |
assertTrue(!effectivePeriodicPolicy |
871 |
+ |
^ q.containsAll(periodics.subList(2, 4))); |
872 |
+ |
|
873 |
+ |
immediates.forEach(f -> assertFalse(f.isDone())); |
874 |
+ |
|
875 |
+ |
assertFalse(delayeds.get(0).isDone()); |
876 |
+ |
if (effectiveDelayedPolicy) |
877 |
+ |
assertFalse(delayeds.get(1).isDone()); |
878 |
+ |
else |
879 |
+ |
assertTrue(delayeds.get(1).isCancelled()); |
880 |
+ |
|
881 |
+ |
if (effectivePeriodicPolicy) |
882 |
+ |
periodics.forEach( |
883 |
+ |
f -> { |
884 |
+ |
assertFalse(f.isDone()); |
885 |
+ |
if (!periodicTasksContinue) { |
886 |
+ |
assertTrue(f.cancel(false)); |
887 |
+ |
assertTrue(f.isCancelled()); |
888 |
+ |
} |
889 |
+ |
}); |
890 |
+ |
else { |
891 |
+ |
periodics.subList(0, 2).forEach(f -> assertFalse(f.isDone())); |
892 |
+ |
periodics.subList(2, 4).forEach(f -> assertTrue(f.isCancelled())); |
893 |
+ |
} |
894 |
+ |
|
895 |
+ |
unblock.countDown(); // Release all pool threads |
896 |
+ |
|
897 |
|
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
898 |
+ |
assertFalse(p.isTerminating()); |
899 |
|
assertTrue(p.isTerminated()); |
900 |
< |
assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get()); |
901 |
< |
}} |
900 |
> |
|
901 |
> |
assertTrue(q.isEmpty()); |
902 |
> |
|
903 |
> |
Stream.of(immediates, delayeds, periodics).flatMap(c -> c.stream()) |
904 |
> |
.forEach(f -> assertTrue(f.isDone())); |
905 |
> |
|
906 |
> |
for (Future<?> f : immediates) assertNull(f.get()); |
907 |
> |
|
908 |
> |
assertNull(delayeds.get(0).get()); |
909 |
> |
if (effectiveDelayedPolicy) |
910 |
> |
assertNull(delayeds.get(1).get()); |
911 |
> |
else |
912 |
> |
assertTrue(delayeds.get(1).isCancelled()); |
913 |
> |
|
914 |
> |
if (periodicTasksContinue) |
915 |
> |
periodics.forEach( |
916 |
> |
f -> { |
917 |
> |
try { f.get(); } |
918 |
> |
catch (ExecutionException success) { |
919 |
> |
assertSame(exception, success.getCause()); |
920 |
> |
} |
921 |
> |
catch (Throwable fail) { threadUnexpectedException(fail); } |
922 |
> |
}); |
923 |
> |
else |
924 |
> |
periodics.forEach(f -> assertTrue(f.isCancelled())); |
925 |
> |
|
926 |
> |
assertEquals(poolSize + 1 |
927 |
> |
+ (effectiveDelayedPolicy ? 1 : 0) |
928 |
> |
+ (periodicTasksContinue ? 2 : 0), |
929 |
> |
ran.get()); |
930 |
> |
} |
931 |
|
|
932 |
|
/** |
933 |
|
* completed submit of callable returns result |
966 |
|
} |
967 |
|
|
968 |
|
/** |
969 |
< |
* invokeAny(null) throws NPE |
969 |
> |
* invokeAny(null) throws NullPointerException |
970 |
|
*/ |
971 |
|
public void testInvokeAny1() throws Exception { |
972 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
979 |
|
} |
980 |
|
|
981 |
|
/** |
982 |
< |
* invokeAny(empty collection) throws IAE |
982 |
> |
* invokeAny(empty collection) throws IllegalArgumentException |
983 |
|
*/ |
984 |
|
public void testInvokeAny2() throws Exception { |
985 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
992 |
|
} |
993 |
|
|
994 |
|
/** |
995 |
< |
* invokeAny(c) throws NPE if c has null elements |
995 |
> |
* invokeAny(c) throws NullPointerException if c has null elements |
996 |
|
*/ |
997 |
|
public void testInvokeAny3() throws Exception { |
998 |
|
CountDownLatch latch = new CountDownLatch(1); |
999 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1000 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1001 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1001 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1002 |
|
l.add(latchAwaitingStringTask(latch)); |
1003 |
|
l.add(null); |
1004 |
|
try { |
1015 |
|
public void testInvokeAny4() throws Exception { |
1016 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1017 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1018 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1018 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1019 |
|
l.add(new NPETask()); |
1020 |
|
try { |
1021 |
|
e.invokeAny(l); |
1032 |
|
public void testInvokeAny5() throws Exception { |
1033 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1034 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1035 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1035 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1036 |
|
l.add(new StringTask()); |
1037 |
|
l.add(new StringTask()); |
1038 |
|
String result = e.invokeAny(l); |
1054 |
|
} |
1055 |
|
|
1056 |
|
/** |
1057 |
< |
* invokeAll(empty collection) returns empty collection |
1057 |
> |
* invokeAll(empty collection) returns empty list |
1058 |
|
*/ |
1059 |
|
public void testInvokeAll2() throws Exception { |
1060 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1061 |
+ |
final Collection<Callable<String>> emptyCollection |
1062 |
+ |
= Collections.emptyList(); |
1063 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1064 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1064 |
> |
List<Future<String>> r = e.invokeAll(emptyCollection); |
1065 |
|
assertTrue(r.isEmpty()); |
1066 |
|
} |
1067 |
|
} |
1072 |
|
public void testInvokeAll3() throws Exception { |
1073 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1074 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1075 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1075 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1076 |
|
l.add(new StringTask()); |
1077 |
|
l.add(null); |
1078 |
|
try { |
1088 |
|
public void testInvokeAll4() throws Exception { |
1089 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1090 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1091 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1091 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1092 |
|
l.add(new NPETask()); |
1093 |
|
List<Future<String>> futures = e.invokeAll(l); |
1094 |
|
assertEquals(1, futures.size()); |
1107 |
|
public void testInvokeAll5() throws Exception { |
1108 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1109 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1110 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1110 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1111 |
|
l.add(new StringTask()); |
1112 |
|
l.add(new StringTask()); |
1113 |
|
List<Future<String>> futures = e.invokeAll(l); |
1124 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1125 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1126 |
|
try { |
1127 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1127 |
> |
e.invokeAny(null, randomTimeout(), randomTimeUnit()); |
1128 |
|
shouldThrow(); |
1129 |
|
} catch (NullPointerException success) {} |
1130 |
|
} |
1131 |
|
} |
1132 |
|
|
1133 |
|
/** |
1134 |
< |
* timed invokeAny(,,null) throws NPE |
1134 |
> |
* timed invokeAny(,,null) throws NullPointerException |
1135 |
|
*/ |
1136 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1137 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1138 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1139 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1139 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1140 |
|
l.add(new StringTask()); |
1141 |
|
try { |
1142 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1142 |
> |
e.invokeAny(l, randomTimeout(), null); |
1143 |
|
shouldThrow(); |
1144 |
|
} catch (NullPointerException success) {} |
1145 |
|
} |
1146 |
|
} |
1147 |
|
|
1148 |
|
/** |
1149 |
< |
* timed invokeAny(empty collection) throws IAE |
1149 |
> |
* timed invokeAny(empty collection) throws IllegalArgumentException |
1150 |
|
*/ |
1151 |
|
public void testTimedInvokeAny2() throws Exception { |
1152 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1153 |
+ |
final Collection<Callable<String>> emptyCollection |
1154 |
+ |
= Collections.emptyList(); |
1155 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1156 |
|
try { |
1157 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1157 |
> |
e.invokeAny(emptyCollection, randomTimeout(), randomTimeUnit()); |
1158 |
|
shouldThrow(); |
1159 |
|
} catch (IllegalArgumentException success) {} |
1160 |
|
} |
1167 |
|
CountDownLatch latch = new CountDownLatch(1); |
1168 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1169 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1170 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1170 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1171 |
|
l.add(latchAwaitingStringTask(latch)); |
1172 |
|
l.add(null); |
1173 |
|
try { |
1174 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1174 |
> |
e.invokeAny(l, randomTimeout(), randomTimeUnit()); |
1175 |
|
shouldThrow(); |
1176 |
|
} catch (NullPointerException success) {} |
1177 |
|
latch.countDown(); |
1184 |
|
public void testTimedInvokeAny4() throws Exception { |
1185 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1186 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1187 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1187 |
> |
long startTime = System.nanoTime(); |
1188 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1189 |
|
l.add(new NPETask()); |
1190 |
|
try { |
1191 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1191 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1192 |
|
shouldThrow(); |
1193 |
|
} catch (ExecutionException success) { |
1194 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1195 |
|
} |
1196 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1197 |
|
} |
1198 |
|
} |
1199 |
|
|
1203 |
|
public void testTimedInvokeAny5() throws Exception { |
1204 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1205 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1206 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1206 |
> |
long startTime = System.nanoTime(); |
1207 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1208 |
|
l.add(new StringTask()); |
1209 |
|
l.add(new StringTask()); |
1210 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1210 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1211 |
|
assertSame(TEST_STRING, result); |
1212 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1213 |
|
} |
1214 |
|
} |
1215 |
|
|
1220 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1221 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1222 |
|
try { |
1223 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1223 |
> |
e.invokeAll(null, randomTimeout(), randomTimeUnit()); |
1224 |
|
shouldThrow(); |
1225 |
|
} catch (NullPointerException success) {} |
1226 |
|
} |
1232 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1233 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1234 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1235 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1235 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1236 |
|
l.add(new StringTask()); |
1237 |
|
try { |
1238 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1238 |
> |
e.invokeAll(l, randomTimeout(), null); |
1239 |
|
shouldThrow(); |
1240 |
|
} catch (NullPointerException success) {} |
1241 |
|
} |
1242 |
|
} |
1243 |
|
|
1244 |
|
/** |
1245 |
< |
* timed invokeAll(empty collection) returns empty collection |
1245 |
> |
* timed invokeAll(empty collection) returns empty list |
1246 |
|
*/ |
1247 |
|
public void testTimedInvokeAll2() throws Exception { |
1248 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1249 |
+ |
final Collection<Callable<String>> emptyCollection |
1250 |
+ |
= Collections.emptyList(); |
1251 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1252 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), |
1253 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
1252 |
> |
List<Future<String>> r = |
1253 |
> |
e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit()); |
1254 |
|
assertTrue(r.isEmpty()); |
1255 |
|
} |
1256 |
|
} |
1261 |
|
public void testTimedInvokeAll3() throws Exception { |
1262 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1263 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1264 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1264 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1265 |
|
l.add(new StringTask()); |
1266 |
|
l.add(null); |
1267 |
|
try { |
1268 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1268 |
> |
e.invokeAll(l, randomTimeout(), randomTimeUnit()); |
1269 |
|
shouldThrow(); |
1270 |
|
} catch (NullPointerException success) {} |
1271 |
|
} |
1277 |
|
public void testTimedInvokeAll4() throws Exception { |
1278 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1279 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1280 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1280 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1281 |
|
l.add(new NPETask()); |
1282 |
|
List<Future<String>> futures = |
1283 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1283 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1284 |
|
assertEquals(1, futures.size()); |
1285 |
|
try { |
1286 |
|
futures.get(0).get(); |
1297 |
|
public void testTimedInvokeAll5() throws Exception { |
1298 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1299 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1300 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1300 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1301 |
|
l.add(new StringTask()); |
1302 |
|
l.add(new StringTask()); |
1303 |
|
List<Future<String>> futures = |
1312 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1313 |
|
*/ |
1314 |
|
public void testTimedInvokeAll6() throws Exception { |
1315 |
< |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1316 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
1317 |
< |
for (long timeout = timeoutMillis();;) { |
1315 |
> |
for (long timeout = timeoutMillis();;) { |
1316 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1317 |
> |
final Callable<String> waiter = new CheckedCallable<String>() { |
1318 |
> |
public String realCall() { |
1319 |
> |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1320 |
> |
catch (InterruptedException ok) {} |
1321 |
> |
return "1"; }}; |
1322 |
> |
final ExecutorService p = new ScheduledThreadPoolExecutor(2); |
1323 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1324 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1325 |
|
tasks.add(new StringTask("0")); |
1326 |
< |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1326 |
> |
tasks.add(waiter); |
1327 |
|
tasks.add(new StringTask("2")); |
1328 |
|
long startTime = System.nanoTime(); |
1329 |
|
List<Future<String>> futures = |
1330 |
< |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1330 |
> |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1331 |
|
assertEquals(tasks.size(), futures.size()); |
1332 |
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1333 |
|
for (Future future : futures) |
1346 |
|
} |
1347 |
|
} |
1348 |
|
|
1349 |
+ |
/** |
1350 |
+ |
* A fixed delay task with overflowing period should not prevent a |
1351 |
+ |
* one-shot task from executing. |
1352 |
+ |
* https://bugs.openjdk.java.net/browse/JDK-8051859 |
1353 |
+ |
*/ |
1354 |
+ |
public void testScheduleWithFixedDelay_overflow() throws Exception { |
1355 |
+ |
final CountDownLatch delayedDone = new CountDownLatch(1); |
1356 |
+ |
final CountDownLatch immediateDone = new CountDownLatch(1); |
1357 |
+ |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
1358 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
1359 |
+ |
final Runnable immediate = new Runnable() { public void run() { |
1360 |
+ |
immediateDone.countDown(); |
1361 |
+ |
}}; |
1362 |
+ |
final Runnable delayed = new Runnable() { public void run() { |
1363 |
+ |
delayedDone.countDown(); |
1364 |
+ |
p.submit(immediate); |
1365 |
+ |
}}; |
1366 |
+ |
p.scheduleWithFixedDelay(delayed, 0L, Long.MAX_VALUE, SECONDS); |
1367 |
+ |
await(delayedDone); |
1368 |
+ |
await(immediateDone); |
1369 |
+ |
} |
1370 |
+ |
} |
1371 |
+ |
|
1372 |
|
} |