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; |
26 |
|
import java.util.concurrent.ScheduledThreadPoolExecutor; |
27 |
|
import java.util.concurrent.ThreadFactory; |
28 |
|
import java.util.concurrent.ThreadPoolExecutor; |
29 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
30 |
|
import java.util.concurrent.atomic.AtomicInteger; |
31 |
+ |
import java.util.concurrent.atomic.AtomicLong; |
32 |
|
|
33 |
|
import junit.framework.Test; |
34 |
|
import junit.framework.TestSuite; |
45 |
|
* execute successfully executes a runnable |
46 |
|
*/ |
47 |
|
public void testExecute() throws InterruptedException { |
48 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
48 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
49 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
50 |
|
final CountDownLatch done = new CountDownLatch(1); |
51 |
|
final Runnable task = new CheckedRunnable() { |
52 |
|
public void realRun() { done.countDown(); }}; |
53 |
|
p.execute(task); |
54 |
< |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
54 |
> |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
55 |
|
} |
56 |
|
} |
57 |
|
|
59 |
|
* delayed schedule of callable successfully executes after delay |
60 |
|
*/ |
61 |
|
public void testSchedule1() throws Exception { |
62 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
62 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
63 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
64 |
|
final long startTime = System.nanoTime(); |
65 |
|
final CountDownLatch done = new CountDownLatch(1); |
80 |
|
* delayed schedule of runnable successfully executes after delay |
81 |
|
*/ |
82 |
|
public void testSchedule3() throws Exception { |
83 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
83 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
84 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
85 |
|
final long startTime = System.nanoTime(); |
86 |
|
final CountDownLatch done = new CountDownLatch(1); |
100 |
|
* scheduleAtFixedRate executes runnable after given initial delay |
101 |
|
*/ |
102 |
|
public void testSchedule4() throws Exception { |
103 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
103 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
104 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
105 |
|
final long startTime = System.nanoTime(); |
106 |
|
final CountDownLatch done = new CountDownLatch(1); |
122 |
|
* scheduleWithFixedDelay executes runnable after given initial delay |
123 |
|
*/ |
124 |
|
public void testSchedule5() throws Exception { |
125 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
125 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
126 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
127 |
|
final long startTime = System.nanoTime(); |
128 |
|
final CountDownLatch done = new CountDownLatch(1); |
146 |
|
} |
147 |
|
|
148 |
|
/** |
149 |
< |
* scheduleAtFixedRate executes series of tasks at given rate |
149 |
> |
* scheduleAtFixedRate executes series of tasks at given rate. |
150 |
> |
* Eventually, it must hold that: |
151 |
> |
* cycles - 1 <= elapsedMillis/delay < cycles |
152 |
|
*/ |
153 |
|
public void testFixedRateSequence() throws InterruptedException { |
154 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
154 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
155 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
156 |
|
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
157 |
< |
long startTime = System.nanoTime(); |
158 |
< |
int cycles = 10; |
157 |
> |
final long startTime = System.nanoTime(); |
158 |
> |
final int cycles = 8; |
159 |
|
final CountDownLatch done = new CountDownLatch(cycles); |
160 |
< |
Runnable task = new CheckedRunnable() { |
160 |
> |
final Runnable task = new CheckedRunnable() { |
161 |
|
public void realRun() { done.countDown(); }}; |
162 |
< |
ScheduledFuture h = |
162 |
> |
final ScheduledFuture periodicTask = |
163 |
|
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
164 |
< |
done.await(); |
165 |
< |
h.cancel(true); |
166 |
< |
double normalizedTime = |
167 |
< |
(double) millisElapsedSince(startTime) / delay; |
168 |
< |
if (normalizedTime >= cycles - 1 && |
169 |
< |
normalizedTime <= cycles) |
164 |
> |
final int totalDelayMillis = (cycles - 1) * delay; |
165 |
> |
await(done, totalDelayMillis + LONG_DELAY_MS); |
166 |
> |
periodicTask.cancel(true); |
167 |
> |
final long elapsedMillis = millisElapsedSince(startTime); |
168 |
> |
assertTrue(elapsedMillis >= totalDelayMillis); |
169 |
> |
if (elapsedMillis <= cycles * delay) |
170 |
|
return; |
171 |
+ |
// else retry with longer delay |
172 |
|
} |
173 |
< |
throw new AssertionError("unexpected execution rate"); |
173 |
> |
fail("unexpected execution rate"); |
174 |
|
} |
175 |
|
} |
176 |
|
|
177 |
|
/** |
178 |
< |
* scheduleWithFixedDelay executes series of tasks with given period |
178 |
> |
* scheduleWithFixedDelay executes series of tasks with given period. |
179 |
> |
* Eventually, it must hold that each task starts at least delay and at |
180 |
> |
* most 2 * delay after the termination of the previous task. |
181 |
|
*/ |
182 |
|
public void testFixedDelaySequence() throws InterruptedException { |
183 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
183 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
184 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
185 |
|
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
186 |
< |
long startTime = System.nanoTime(); |
187 |
< |
int cycles = 10; |
186 |
> |
final long startTime = System.nanoTime(); |
187 |
> |
final AtomicLong previous = new AtomicLong(startTime); |
188 |
> |
final AtomicBoolean tryLongerDelay = new AtomicBoolean(false); |
189 |
> |
final int cycles = 8; |
190 |
|
final CountDownLatch done = new CountDownLatch(cycles); |
191 |
< |
Runnable task = new CheckedRunnable() { |
192 |
< |
public void realRun() { done.countDown(); }}; |
193 |
< |
ScheduledFuture h = |
191 |
> |
final int d = delay; |
192 |
> |
final Runnable task = new CheckedRunnable() { |
193 |
> |
public void realRun() { |
194 |
> |
long now = System.nanoTime(); |
195 |
> |
long elapsedMillis |
196 |
> |
= NANOSECONDS.toMillis(now - previous.get()); |
197 |
> |
if (done.getCount() == cycles) { // first execution |
198 |
> |
if (elapsedMillis >= d) |
199 |
> |
tryLongerDelay.set(true); |
200 |
> |
} else { |
201 |
> |
assertTrue(elapsedMillis >= d); |
202 |
> |
if (elapsedMillis >= 2 * d) |
203 |
> |
tryLongerDelay.set(true); |
204 |
> |
} |
205 |
> |
previous.set(now); |
206 |
> |
done.countDown(); |
207 |
> |
}}; |
208 |
> |
final ScheduledFuture periodicTask = |
209 |
|
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
210 |
< |
done.await(); |
211 |
< |
h.cancel(true); |
212 |
< |
double normalizedTime = |
213 |
< |
(double) millisElapsedSince(startTime) / delay; |
214 |
< |
if (normalizedTime >= cycles - 1 && |
215 |
< |
normalizedTime <= cycles) |
210 |
> |
final int totalDelayMillis = (cycles - 1) * delay; |
211 |
> |
await(done, totalDelayMillis + cycles * LONG_DELAY_MS); |
212 |
> |
periodicTask.cancel(true); |
213 |
> |
final long elapsedMillis = millisElapsedSince(startTime); |
214 |
> |
assertTrue(elapsedMillis >= totalDelayMillis); |
215 |
> |
if (!tryLongerDelay.get()) |
216 |
|
return; |
217 |
+ |
// else retry with longer delay |
218 |
|
} |
219 |
< |
throw new AssertionError("unexpected execution rate"); |
219 |
> |
fail("unexpected execution rate"); |
220 |
|
} |
221 |
|
} |
222 |
|
|
224 |
|
* execute(null) throws NPE |
225 |
|
*/ |
226 |
|
public void testExecuteNull() throws InterruptedException { |
227 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
227 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
228 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
229 |
|
try { |
230 |
|
p.execute(null); |
251 |
|
* execute throws RejectedExecutionException if shutdown |
252 |
|
*/ |
253 |
|
public void testSchedule1_RejectedExecutionException() throws InterruptedException { |
254 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
254 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
255 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
256 |
|
try { |
257 |
|
p.shutdown(); |
267 |
|
* schedule throws RejectedExecutionException if shutdown |
268 |
|
*/ |
269 |
|
public void testSchedule2_RejectedExecutionException() throws InterruptedException { |
270 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
270 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
271 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
272 |
|
try { |
273 |
|
p.shutdown(); |
283 |
|
* schedule callable throws RejectedExecutionException if shutdown |
284 |
|
*/ |
285 |
|
public void testSchedule3_RejectedExecutionException() throws InterruptedException { |
286 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
286 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
287 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
288 |
|
try { |
289 |
|
p.shutdown(); |
299 |
|
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
300 |
|
*/ |
301 |
|
public void testScheduleAtFixedRate1_RejectedExecutionException() throws InterruptedException { |
302 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
302 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
303 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
304 |
|
try { |
305 |
|
p.shutdown(); |
315 |
|
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown |
316 |
|
*/ |
317 |
|
public void testScheduleWithFixedDelay1_RejectedExecutionException() throws InterruptedException { |
318 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
318 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
319 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
320 |
|
try { |
321 |
|
p.shutdown(); |
332 |
|
* thread becomes active |
333 |
|
*/ |
334 |
|
public void testGetActiveCount() throws InterruptedException { |
335 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
336 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2); |
337 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
337 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
338 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
312 |
– |
final CountDownLatch done = new CountDownLatch(1); |
339 |
|
assertEquals(0, p.getActiveCount()); |
340 |
|
p.execute(new CheckedRunnable() { |
341 |
|
public void realRun() throws InterruptedException { |
342 |
|
threadStarted.countDown(); |
343 |
|
assertEquals(1, p.getActiveCount()); |
344 |
< |
done.await(); |
344 |
> |
await(done); |
345 |
|
}}); |
346 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
346 |
> |
await(threadStarted); |
347 |
|
assertEquals(1, p.getActiveCount()); |
322 |
– |
done.countDown(); |
348 |
|
} |
349 |
|
} |
350 |
|
|
398 |
|
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(THREADS); |
399 |
|
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
400 |
|
final CountDownLatch done = new CountDownLatch(1); |
401 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
401 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
402 |
|
assertEquals(0, p.getLargestPoolSize()); |
403 |
|
for (int i = 0; i < THREADS; i++) |
404 |
|
p.execute(new CheckedRunnable() { |
405 |
|
public void realRun() throws InterruptedException { |
406 |
|
threadsStarted.countDown(); |
407 |
< |
done.await(); |
407 |
> |
await(done); |
408 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
409 |
|
}}); |
410 |
< |
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
410 |
> |
await(threadsStarted); |
411 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
387 |
– |
done.countDown(); |
412 |
|
} |
413 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
414 |
|
} |
421 |
|
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
422 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
423 |
|
final CountDownLatch done = new CountDownLatch(1); |
424 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
424 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
425 |
|
assertEquals(0, p.getPoolSize()); |
426 |
|
p.execute(new CheckedRunnable() { |
427 |
|
public void realRun() throws InterruptedException { |
428 |
|
threadStarted.countDown(); |
429 |
|
assertEquals(1, p.getPoolSize()); |
430 |
< |
done.await(); |
430 |
> |
await(done); |
431 |
|
}}); |
432 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
432 |
> |
await(threadStarted); |
433 |
|
assertEquals(1, p.getPoolSize()); |
410 |
– |
done.countDown(); |
434 |
|
} |
435 |
|
} |
436 |
|
|
439 |
|
* submitted |
440 |
|
*/ |
441 |
|
public void testGetTaskCount() throws InterruptedException { |
442 |
+ |
final int TASKS = 3; |
443 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
444 |
|
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
445 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
445 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
446 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
422 |
– |
final CountDownLatch done = new CountDownLatch(1); |
423 |
– |
final int TASKS = 5; |
447 |
|
assertEquals(0, p.getTaskCount()); |
448 |
< |
for (int i = 0; i < TASKS; i++) |
448 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
449 |
> |
p.execute(new CheckedRunnable() { |
450 |
> |
public void realRun() throws InterruptedException { |
451 |
> |
threadStarted.countDown(); |
452 |
> |
await(done); |
453 |
> |
}}); |
454 |
> |
await(threadStarted); |
455 |
> |
assertEquals(1, p.getTaskCount()); |
456 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
457 |
> |
for (int i = 0; i < TASKS; i++) { |
458 |
> |
assertEquals(1 + i, p.getTaskCount()); |
459 |
|
p.execute(new CheckedRunnable() { |
460 |
|
public void realRun() throws InterruptedException { |
461 |
|
threadStarted.countDown(); |
462 |
< |
done.await(); |
462 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
463 |
> |
await(done); |
464 |
|
}}); |
465 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
466 |
< |
assertEquals(TASKS, p.getTaskCount()); |
467 |
< |
done.countDown(); |
465 |
> |
} |
466 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
467 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
468 |
|
} |
469 |
+ |
assertEquals(1 + TASKS, p.getTaskCount()); |
470 |
+ |
assertEquals(1 + TASKS, p.getCompletedTaskCount()); |
471 |
|
} |
472 |
|
|
473 |
|
/** |
474 |
|
* getThreadFactory returns factory in constructor if not set |
475 |
|
*/ |
476 |
|
public void testGetThreadFactory() throws InterruptedException { |
477 |
< |
ThreadFactory threadFactory = new SimpleThreadFactory(); |
478 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, threadFactory); |
479 |
< |
assertSame(threadFactory, p.getThreadFactory()); |
480 |
< |
joinPool(p); |
477 |
> |
final ThreadFactory threadFactory = new SimpleThreadFactory(); |
478 |
> |
final ScheduledThreadPoolExecutor p = |
479 |
> |
new ScheduledThreadPoolExecutor(1, threadFactory); |
480 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
481 |
> |
assertSame(threadFactory, p.getThreadFactory()); |
482 |
> |
} |
483 |
|
} |
484 |
|
|
485 |
|
/** |
487 |
|
*/ |
488 |
|
public void testSetThreadFactory() throws InterruptedException { |
489 |
|
ThreadFactory threadFactory = new SimpleThreadFactory(); |
490 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
490 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
491 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
492 |
|
p.setThreadFactory(threadFactory); |
493 |
|
assertSame(threadFactory, p.getThreadFactory()); |
498 |
|
* setThreadFactory(null) throws NPE |
499 |
|
*/ |
500 |
|
public void testSetThreadFactoryNull() throws InterruptedException { |
501 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
501 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
502 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
503 |
|
try { |
504 |
|
p.setThreadFactory(null); |
512 |
|
*/ |
513 |
|
public void testIsShutdown() { |
514 |
|
|
515 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
515 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
516 |
|
try { |
517 |
|
assertFalse(p.isShutdown()); |
518 |
|
} |
535 |
|
public void realRun() throws InterruptedException { |
536 |
|
assertFalse(p.isTerminated()); |
537 |
|
threadStarted.countDown(); |
538 |
< |
done.await(); |
538 |
> |
await(done); |
539 |
|
}}); |
540 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
540 |
> |
await(threadStarted); |
541 |
|
assertFalse(p.isTerminating()); |
542 |
|
done.countDown(); |
543 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
559 |
|
public void realRun() throws InterruptedException { |
560 |
|
assertFalse(p.isTerminating()); |
561 |
|
threadStarted.countDown(); |
562 |
< |
done.await(); |
562 |
> |
await(done); |
563 |
|
}}); |
564 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
564 |
> |
await(threadStarted); |
565 |
|
assertFalse(p.isTerminating()); |
566 |
|
done.countDown(); |
567 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
575 |
|
* getQueue returns the work queue, which contains queued tasks |
576 |
|
*/ |
577 |
|
public void testGetQueue() throws InterruptedException { |
578 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
579 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
578 |
> |
final CountDownLatch done = new CountDownLatch(1); |
579 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
580 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
581 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
543 |
– |
final CountDownLatch done = new CountDownLatch(1); |
582 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
583 |
|
for (int i = 0; i < tasks.length; i++) { |
584 |
|
Runnable r = new CheckedRunnable() { |
585 |
|
public void realRun() throws InterruptedException { |
586 |
|
threadStarted.countDown(); |
587 |
< |
done.await(); |
587 |
> |
await(done); |
588 |
|
}}; |
589 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
590 |
|
} |
591 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
591 |
> |
await(threadStarted); |
592 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
593 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
594 |
|
assertFalse(q.contains(tasks[0])); |
557 |
– |
done.countDown(); |
595 |
|
} |
596 |
|
} |
597 |
|
|
599 |
|
* remove(task) removes queued task, and fails to remove active task |
600 |
|
*/ |
601 |
|
public void testRemove() throws InterruptedException { |
602 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
603 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
604 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
604 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
605 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
606 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
569 |
– |
final CountDownLatch done = new CountDownLatch(1); |
607 |
|
for (int i = 0; i < tasks.length; i++) { |
608 |
|
Runnable r = new CheckedRunnable() { |
609 |
|
public void realRun() throws InterruptedException { |
610 |
|
threadStarted.countDown(); |
611 |
< |
done.await(); |
611 |
> |
await(done); |
612 |
|
}}; |
613 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
614 |
|
} |
615 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
615 |
> |
await(threadStarted); |
616 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
617 |
|
assertFalse(p.remove((Runnable)tasks[0])); |
618 |
|
assertTrue(q.contains((Runnable)tasks[4])); |
623 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
624 |
|
assertTrue(p.remove((Runnable)tasks[3])); |
625 |
|
assertFalse(q.contains((Runnable)tasks[3])); |
589 |
– |
done.countDown(); |
626 |
|
} |
627 |
|
} |
628 |
|
|
630 |
|
* purge eventually removes cancelled tasks from the queue |
631 |
|
*/ |
632 |
|
public void testPurge() throws InterruptedException { |
633 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
634 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
635 |
< |
for (int i = 0; i < tasks.length; i++) |
636 |
< |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
637 |
< |
LONG_DELAY_MS, MILLISECONDS); |
638 |
< |
try { |
633 |
> |
final ScheduledFuture[] tasks = new ScheduledFuture[5]; |
634 |
> |
final Runnable releaser = new Runnable() { public void run() { |
635 |
> |
for (ScheduledFuture task : tasks) |
636 |
> |
if (task != null) task.cancel(true); }}; |
637 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
638 |
> |
try (PoolCleaner cleaner = cleaner(p, releaser)) { |
639 |
> |
for (int i = 0; i < tasks.length; i++) |
640 |
> |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
641 |
> |
LONG_DELAY_MS, MILLISECONDS); |
642 |
|
int max = tasks.length; |
643 |
|
if (tasks[4].cancel(true)) --max; |
644 |
|
if (tasks[3].cancel(true)) --max; |
650 |
|
long count = p.getTaskCount(); |
651 |
|
if (count == max) |
652 |
|
return; |
653 |
< |
} while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
653 |
> |
} while (millisElapsedSince(startTime) < LONG_DELAY_MS); |
654 |
|
fail("Purge failed to remove cancelled tasks"); |
616 |
– |
} finally { |
617 |
– |
for (ScheduledFuture task : tasks) |
618 |
– |
task.cancel(true); |
619 |
– |
joinPool(p); |
655 |
|
} |
656 |
|
} |
657 |
|
|
665 |
|
final AtomicInteger ran = new AtomicInteger(0); |
666 |
|
final ScheduledThreadPoolExecutor p = |
667 |
|
new ScheduledThreadPoolExecutor(poolSize); |
668 |
< |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
668 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
669 |
|
Runnable waiter = new CheckedRunnable() { public void realRun() { |
670 |
|
threadsStarted.countDown(); |
671 |
|
try { |
675 |
|
}}; |
676 |
|
for (int i = 0; i < count; i++) |
677 |
|
p.execute(waiter); |
678 |
< |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
678 |
> |
await(threadsStarted); |
679 |
|
assertEquals(poolSize, p.getActiveCount()); |
680 |
|
assertEquals(0, p.getCompletedTaskCount()); |
681 |
|
final List<Runnable> queuedTasks; |
698 |
|
* and those tasks are drained from the queue |
699 |
|
*/ |
700 |
|
public void testShutdownNow_delayedTasks() throws InterruptedException { |
701 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
701 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
702 |
|
List<ScheduledFuture> tasks = new ArrayList<>(); |
703 |
|
for (int i = 0; i < 3; i++) { |
704 |
|
Runnable r = new NoOpRunnable(); |
1072 |
|
public void testTimedInvokeAny4() throws Exception { |
1073 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1074 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1075 |
+ |
long startTime = System.nanoTime(); |
1076 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1077 |
|
l.add(new NPETask()); |
1078 |
|
try { |
1079 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1079 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1080 |
|
shouldThrow(); |
1081 |
|
} catch (ExecutionException success) { |
1082 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1083 |
|
} |
1084 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1085 |
|
} |
1086 |
|
} |
1087 |
|
|
1091 |
|
public void testTimedInvokeAny5() throws Exception { |
1092 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1093 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1094 |
+ |
long startTime = System.nanoTime(); |
1095 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1096 |
|
l.add(new StringTask()); |
1097 |
|
l.add(new StringTask()); |
1098 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1098 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1099 |
|
assertSame(TEST_STRING, result); |
1100 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1101 |
|
} |
1102 |
|
} |
1103 |
|
|
1166 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1167 |
|
l.add(new NPETask()); |
1168 |
|
List<Future<String>> futures = |
1169 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1169 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1170 |
|
assertEquals(1, futures.size()); |
1171 |
|
try { |
1172 |
|
futures.get(0).get(); |
1198 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1199 |
|
*/ |
1200 |
|
public void testTimedInvokeAll6() throws Exception { |
1201 |
< |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1202 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
1203 |
< |
for (long timeout = timeoutMillis();;) { |
1201 |
> |
for (long timeout = timeoutMillis();;) { |
1202 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1203 |
> |
final Callable<String> waiter = new CheckedCallable<String>() { |
1204 |
> |
public String realCall() { |
1205 |
> |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1206 |
> |
catch (InterruptedException ok) {} |
1207 |
> |
return "1"; }}; |
1208 |
> |
final ExecutorService p = new ScheduledThreadPoolExecutor(2); |
1209 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1210 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1211 |
|
tasks.add(new StringTask("0")); |
1212 |
< |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1212 |
> |
tasks.add(waiter); |
1213 |
|
tasks.add(new StringTask("2")); |
1214 |
|
long startTime = System.nanoTime(); |
1215 |
|
List<Future<String>> futures = |
1216 |
< |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1216 |
> |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1217 |
|
assertEquals(tasks.size(), futures.size()); |
1218 |
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1219 |
|
for (Future future : futures) |
1232 |
|
} |
1233 |
|
} |
1234 |
|
|
1235 |
+ |
/** |
1236 |
+ |
* A fixed delay task with overflowing period should not prevent a |
1237 |
+ |
* one-shot task from executing. |
1238 |
+ |
* https://bugs.openjdk.java.net/browse/JDK-8051859 |
1239 |
+ |
*/ |
1240 |
+ |
public void testScheduleWithFixedDelay_overflow() throws Exception { |
1241 |
+ |
final CountDownLatch delayedDone = new CountDownLatch(1); |
1242 |
+ |
final CountDownLatch immediateDone = new CountDownLatch(1); |
1243 |
+ |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
1244 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
1245 |
+ |
final Runnable immediate = new Runnable() { public void run() { |
1246 |
+ |
immediateDone.countDown(); |
1247 |
+ |
}}; |
1248 |
+ |
final Runnable delayed = new Runnable() { public void run() { |
1249 |
+ |
delayedDone.countDown(); |
1250 |
+ |
p.submit(immediate); |
1251 |
+ |
}}; |
1252 |
+ |
p.scheduleWithFixedDelay(delayed, 0L, Long.MAX_VALUE, SECONDS); |
1253 |
+ |
await(delayedDone); |
1254 |
+ |
await(immediateDone); |
1255 |
+ |
} |
1256 |
+ |
} |
1257 |
+ |
|
1258 |
|
} |