43 |
|
*/ |
44 |
|
public void testExecute() throws InterruptedException { |
45 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
46 |
< |
final CountDownLatch done = new CountDownLatch(1); |
47 |
< |
final Runnable task = new CheckedRunnable() { |
48 |
< |
public void realRun() { |
49 |
< |
done.countDown(); |
50 |
< |
}}; |
51 |
< |
try { |
46 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
47 |
> |
final CountDownLatch done = new CountDownLatch(1); |
48 |
> |
final Runnable task = new CheckedRunnable() { |
49 |
> |
public void realRun() { done.countDown(); }}; |
50 |
|
p.execute(task); |
51 |
< |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
54 |
< |
} finally { |
55 |
< |
joinPool(p); |
51 |
> |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
52 |
|
} |
53 |
|
} |
54 |
|
|
57 |
|
*/ |
58 |
|
public void testSchedule1() throws Exception { |
59 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
60 |
< |
final long startTime = System.nanoTime(); |
61 |
< |
final CountDownLatch done = new CountDownLatch(1); |
62 |
< |
try { |
60 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
61 |
> |
final long startTime = System.nanoTime(); |
62 |
> |
final CountDownLatch done = new CountDownLatch(1); |
63 |
|
Callable task = new CheckedCallable<Boolean>() { |
64 |
|
public Boolean realCall() { |
65 |
|
done.countDown(); |
70 |
|
assertSame(Boolean.TRUE, f.get()); |
71 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
72 |
|
assertTrue(done.await(0L, MILLISECONDS)); |
77 |
– |
} finally { |
78 |
– |
joinPool(p); |
73 |
|
} |
74 |
|
} |
75 |
|
|
78 |
|
*/ |
79 |
|
public void testSchedule3() throws Exception { |
80 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
81 |
< |
final long startTime = System.nanoTime(); |
82 |
< |
final CountDownLatch done = new CountDownLatch(1); |
83 |
< |
try { |
81 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
82 |
> |
final long startTime = System.nanoTime(); |
83 |
> |
final CountDownLatch done = new CountDownLatch(1); |
84 |
|
Runnable task = new CheckedRunnable() { |
85 |
|
public void realRun() { |
86 |
|
done.countDown(); |
90 |
|
await(done); |
91 |
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
92 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
99 |
– |
} finally { |
100 |
– |
joinPool(p); |
93 |
|
} |
94 |
|
} |
95 |
|
|
98 |
|
*/ |
99 |
|
public void testSchedule4() throws Exception { |
100 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
101 |
< |
final long startTime = System.nanoTime(); |
102 |
< |
final CountDownLatch done = new CountDownLatch(1); |
103 |
< |
try { |
101 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
102 |
> |
final long startTime = System.nanoTime(); |
103 |
> |
final CountDownLatch done = new CountDownLatch(1); |
104 |
|
Runnable task = new CheckedRunnable() { |
105 |
|
public void realRun() { |
106 |
|
done.countDown(); |
112 |
|
await(done); |
113 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
114 |
|
f.cancel(true); |
123 |
– |
} finally { |
124 |
– |
joinPool(p); |
115 |
|
} |
116 |
|
} |
117 |
|
|
120 |
|
*/ |
121 |
|
public void testSchedule5() throws Exception { |
122 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
123 |
< |
final long startTime = System.nanoTime(); |
124 |
< |
final CountDownLatch done = new CountDownLatch(1); |
125 |
< |
try { |
123 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
124 |
> |
final long startTime = System.nanoTime(); |
125 |
> |
final CountDownLatch done = new CountDownLatch(1); |
126 |
|
Runnable task = new CheckedRunnable() { |
127 |
|
public void realRun() { |
128 |
|
done.countDown(); |
134 |
|
await(done); |
135 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
136 |
|
f.cancel(true); |
147 |
– |
} finally { |
148 |
– |
joinPool(p); |
137 |
|
} |
138 |
|
} |
139 |
|
|
147 |
|
*/ |
148 |
|
public void testFixedRateSequence() throws InterruptedException { |
149 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
150 |
< |
try { |
150 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
151 |
|
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
152 |
|
long startTime = System.nanoTime(); |
153 |
|
int cycles = 10; |
156 |
|
public void realRun() { done.countDown(); }}; |
157 |
|
ScheduledFuture h = |
158 |
|
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
159 |
< |
done.await(); |
159 |
> |
await(done); |
160 |
|
h.cancel(true); |
161 |
|
double normalizedTime = |
162 |
|
(double) millisElapsedSince(startTime) / delay; |
165 |
|
return; |
166 |
|
} |
167 |
|
throw new AssertionError("unexpected execution rate"); |
180 |
– |
} finally { |
181 |
– |
joinPool(p); |
168 |
|
} |
169 |
|
} |
170 |
|
|
173 |
|
*/ |
174 |
|
public void testFixedDelaySequence() throws InterruptedException { |
175 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
176 |
< |
try { |
176 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
177 |
|
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
178 |
|
long startTime = System.nanoTime(); |
179 |
|
int cycles = 10; |
182 |
|
public void realRun() { done.countDown(); }}; |
183 |
|
ScheduledFuture h = |
184 |
|
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
185 |
< |
done.await(); |
185 |
> |
await(done); |
186 |
|
h.cancel(true); |
187 |
|
double normalizedTime = |
188 |
|
(double) millisElapsedSince(startTime) / delay; |
191 |
|
return; |
192 |
|
} |
193 |
|
throw new AssertionError("unexpected execution rate"); |
208 |
– |
} finally { |
209 |
– |
joinPool(p); |
194 |
|
} |
195 |
|
} |
196 |
|
|
198 |
|
* execute(null) throws NPE |
199 |
|
*/ |
200 |
|
public void testExecuteNull() throws InterruptedException { |
201 |
< |
ScheduledThreadPoolExecutor se = null; |
202 |
< |
try { |
203 |
< |
se = new ScheduledThreadPoolExecutor(1); |
204 |
< |
se.execute(null); |
205 |
< |
shouldThrow(); |
206 |
< |
} catch (NullPointerException success) {} |
207 |
< |
|
224 |
< |
joinPool(se); |
201 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
202 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
203 |
> |
try { |
204 |
> |
p.execute(null); |
205 |
> |
shouldThrow(); |
206 |
> |
} catch (NullPointerException success) {} |
207 |
> |
} |
208 |
|
} |
209 |
|
|
210 |
|
/** |
211 |
|
* schedule(null) throws NPE |
212 |
|
*/ |
213 |
|
public void testScheduleNull() throws InterruptedException { |
214 |
< |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
215 |
< |
try { |
216 |
< |
TrackedCallable callable = null; |
217 |
< |
Future f = se.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
218 |
< |
shouldThrow(); |
219 |
< |
} catch (NullPointerException success) {} |
220 |
< |
joinPool(se); |
214 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
215 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
216 |
> |
try { |
217 |
> |
TrackedCallable callable = null; |
218 |
> |
Future f = p.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
219 |
> |
shouldThrow(); |
220 |
> |
} catch (NullPointerException success) {} |
221 |
> |
} |
222 |
|
} |
223 |
|
|
224 |
|
/** |
225 |
|
* execute throws RejectedExecutionException if shutdown |
226 |
|
*/ |
227 |
|
public void testSchedule1_RejectedExecutionException() throws InterruptedException { |
228 |
< |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
229 |
< |
try { |
230 |
< |
se.shutdown(); |
231 |
< |
se.schedule(new NoOpRunnable(), |
232 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
233 |
< |
shouldThrow(); |
234 |
< |
} catch (RejectedExecutionException success) { |
235 |
< |
} catch (SecurityException ok) { |
228 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
229 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
230 |
> |
try { |
231 |
> |
p.shutdown(); |
232 |
> |
p.schedule(new NoOpRunnable(), |
233 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
234 |
> |
shouldThrow(); |
235 |
> |
} catch (RejectedExecutionException success) { |
236 |
> |
} catch (SecurityException ok) {} |
237 |
|
} |
253 |
– |
|
254 |
– |
joinPool(se); |
238 |
|
} |
239 |
|
|
240 |
|
/** |
241 |
|
* schedule throws RejectedExecutionException if shutdown |
242 |
|
*/ |
243 |
|
public void testSchedule2_RejectedExecutionException() throws InterruptedException { |
244 |
< |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
245 |
< |
try { |
246 |
< |
se.shutdown(); |
247 |
< |
se.schedule(new NoOpCallable(), |
248 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
249 |
< |
shouldThrow(); |
250 |
< |
} catch (RejectedExecutionException success) { |
251 |
< |
} catch (SecurityException ok) { |
244 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
245 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
246 |
> |
try { |
247 |
> |
p.shutdown(); |
248 |
> |
p.schedule(new NoOpCallable(), |
249 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
250 |
> |
shouldThrow(); |
251 |
> |
} catch (RejectedExecutionException success) { |
252 |
> |
} catch (SecurityException ok) {} |
253 |
|
} |
270 |
– |
joinPool(se); |
254 |
|
} |
255 |
|
|
256 |
|
/** |
257 |
|
* schedule callable throws RejectedExecutionException if shutdown |
258 |
|
*/ |
259 |
|
public void testSchedule3_RejectedExecutionException() throws InterruptedException { |
260 |
< |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
261 |
< |
try { |
262 |
< |
se.shutdown(); |
263 |
< |
se.schedule(new NoOpCallable(), |
264 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
265 |
< |
shouldThrow(); |
266 |
< |
} catch (RejectedExecutionException success) { |
267 |
< |
} catch (SecurityException ok) { |
260 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
261 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
262 |
> |
try { |
263 |
> |
p.shutdown(); |
264 |
> |
p.schedule(new NoOpCallable(), |
265 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
266 |
> |
shouldThrow(); |
267 |
> |
} catch (RejectedExecutionException success) { |
268 |
> |
} catch (SecurityException ok) {} |
269 |
|
} |
286 |
– |
joinPool(se); |
270 |
|
} |
271 |
|
|
272 |
|
/** |
273 |
|
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
274 |
|
*/ |
275 |
|
public void testScheduleAtFixedRate1_RejectedExecutionException() throws InterruptedException { |
276 |
< |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
277 |
< |
try { |
278 |
< |
se.shutdown(); |
279 |
< |
se.scheduleAtFixedRate(new NoOpRunnable(), |
280 |
< |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
281 |
< |
shouldThrow(); |
282 |
< |
} catch (RejectedExecutionException success) { |
283 |
< |
} catch (SecurityException ok) { |
276 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
277 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
278 |
> |
try { |
279 |
> |
p.shutdown(); |
280 |
> |
p.scheduleAtFixedRate(new NoOpRunnable(), |
281 |
> |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
282 |
> |
shouldThrow(); |
283 |
> |
} catch (RejectedExecutionException success) { |
284 |
> |
} catch (SecurityException ok) {} |
285 |
|
} |
302 |
– |
joinPool(se); |
286 |
|
} |
287 |
|
|
288 |
|
/** |
289 |
|
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown |
290 |
|
*/ |
291 |
|
public void testScheduleWithFixedDelay1_RejectedExecutionException() throws InterruptedException { |
292 |
< |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
293 |
< |
try { |
294 |
< |
se.shutdown(); |
295 |
< |
se.scheduleWithFixedDelay(new NoOpRunnable(), |
296 |
< |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
297 |
< |
shouldThrow(); |
298 |
< |
} catch (RejectedExecutionException success) { |
299 |
< |
} catch (SecurityException ok) { |
292 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
293 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
294 |
> |
try { |
295 |
> |
p.shutdown(); |
296 |
> |
p.scheduleWithFixedDelay(new NoOpRunnable(), |
297 |
> |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
298 |
> |
shouldThrow(); |
299 |
> |
} catch (RejectedExecutionException success) { |
300 |
> |
} catch (SecurityException ok) {} |
301 |
|
} |
318 |
– |
joinPool(se); |
302 |
|
} |
303 |
|
|
304 |
|
/** |
307 |
|
*/ |
308 |
|
public void testGetActiveCount() throws InterruptedException { |
309 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2); |
310 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
311 |
< |
final CountDownLatch done = new CountDownLatch(1); |
312 |
< |
try { |
310 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
311 |
> |
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 { |
316 |
|
threadStarted.countDown(); |
317 |
|
assertEquals(1, p.getActiveCount()); |
318 |
< |
done.await(); |
318 |
> |
await(done); |
319 |
|
}}); |
320 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
320 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
321 |
|
assertEquals(1, p.getActiveCount()); |
339 |
– |
} finally { |
322 |
|
done.countDown(); |
341 |
– |
joinPool(p); |
323 |
|
} |
324 |
|
} |
325 |
|
|
329 |
|
*/ |
330 |
|
public void testGetCompletedTaskCount() throws InterruptedException { |
331 |
|
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2); |
332 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
333 |
< |
final CountDownLatch threadProceed = new CountDownLatch(1); |
334 |
< |
final CountDownLatch threadDone = new CountDownLatch(1); |
335 |
< |
try { |
332 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
333 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
334 |
> |
final CountDownLatch threadProceed = new CountDownLatch(1); |
335 |
> |
final CountDownLatch threadDone = new CountDownLatch(1); |
336 |
|
assertEquals(0, p.getCompletedTaskCount()); |
337 |
|
p.execute(new CheckedRunnable() { |
338 |
|
public void realRun() throws InterruptedException { |
351 |
|
fail("timed out"); |
352 |
|
Thread.yield(); |
353 |
|
} |
373 |
– |
} finally { |
374 |
– |
joinPool(p); |
354 |
|
} |
355 |
|
} |
356 |
|
|
359 |
|
*/ |
360 |
|
public void testGetCorePoolSize() throws InterruptedException { |
361 |
|
ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
362 |
< |
assertEquals(1, p.getCorePoolSize()); |
363 |
< |
joinPool(p); |
362 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
363 |
> |
assertEquals(1, p.getCorePoolSize()); |
364 |
> |
} |
365 |
|
} |
366 |
|
|
367 |
|
/** |
373 |
|
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(THREADS); |
374 |
|
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
375 |
|
final CountDownLatch done = new CountDownLatch(1); |
376 |
< |
try { |
376 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
377 |
|
assertEquals(0, p.getLargestPoolSize()); |
378 |
|
for (int i = 0; i < THREADS; i++) |
379 |
|
p.execute(new CheckedRunnable() { |
380 |
|
public void realRun() throws InterruptedException { |
381 |
|
threadsStarted.countDown(); |
382 |
< |
done.await(); |
382 |
> |
await(done); |
383 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
384 |
|
}}); |
385 |
< |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
406 |
< |
assertEquals(THREADS, p.getLargestPoolSize()); |
407 |
< |
} finally { |
408 |
< |
done.countDown(); |
409 |
< |
joinPool(p); |
385 |
> |
await(threadsStarted); |
386 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
387 |
|
} |
388 |
+ |
assertEquals(THREADS, p.getLargestPoolSize()); |
389 |
|
} |
390 |
|
|
391 |
|
/** |
396 |
|
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
397 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
398 |
|
final CountDownLatch done = new CountDownLatch(1); |
399 |
< |
try { |
399 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
400 |
|
assertEquals(0, p.getPoolSize()); |
401 |
|
p.execute(new CheckedRunnable() { |
402 |
|
public void realRun() throws InterruptedException { |
403 |
|
threadStarted.countDown(); |
404 |
|
assertEquals(1, p.getPoolSize()); |
405 |
< |
done.await(); |
405 |
> |
await(done); |
406 |
|
}}); |
407 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
407 |
> |
await(threadStarted); |
408 |
|
assertEquals(1, p.getPoolSize()); |
432 |
– |
} finally { |
433 |
– |
done.countDown(); |
434 |
– |
joinPool(p); |
409 |
|
} |
410 |
|
} |
411 |
|
|
414 |
|
* submitted |
415 |
|
*/ |
416 |
|
public void testGetTaskCount() throws InterruptedException { |
417 |
< |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
444 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
417 |
> |
final int TASKS = 3; |
418 |
|
final CountDownLatch done = new CountDownLatch(1); |
419 |
< |
final int TASKS = 5; |
420 |
< |
try { |
419 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
420 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
421 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
422 |
|
assertEquals(0, p.getTaskCount()); |
423 |
< |
for (int i = 0; i < TASKS; i++) |
423 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
424 |
> |
p.execute(new CheckedRunnable() { |
425 |
> |
public void realRun() throws InterruptedException { |
426 |
> |
threadStarted.countDown(); |
427 |
> |
await(done); |
428 |
> |
}}); |
429 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
430 |
> |
assertEquals(1, p.getTaskCount()); |
431 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
432 |
> |
for (int i = 0; i < TASKS; i++) { |
433 |
> |
assertEquals(1 + i, p.getTaskCount()); |
434 |
|
p.execute(new CheckedRunnable() { |
435 |
|
public void realRun() throws InterruptedException { |
436 |
|
threadStarted.countDown(); |
437 |
< |
done.await(); |
437 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
438 |
> |
await(done); |
439 |
|
}}); |
440 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
441 |
< |
assertEquals(TASKS, p.getTaskCount()); |
442 |
< |
} finally { |
458 |
< |
done.countDown(); |
459 |
< |
joinPool(p); |
440 |
> |
} |
441 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
442 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
443 |
|
} |
444 |
+ |
assertEquals(1 + TASKS, p.getTaskCount()); |
445 |
+ |
assertEquals(1 + TASKS, p.getCompletedTaskCount()); |
446 |
|
} |
447 |
|
|
448 |
|
/** |
449 |
|
* getThreadFactory returns factory in constructor if not set |
450 |
|
*/ |
451 |
|
public void testGetThreadFactory() throws InterruptedException { |
452 |
< |
ThreadFactory tf = new SimpleThreadFactory(); |
453 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf); |
454 |
< |
assertSame(tf, p.getThreadFactory()); |
455 |
< |
joinPool(p); |
452 |
> |
final ThreadFactory threadFactory = new SimpleThreadFactory(); |
453 |
> |
final ScheduledThreadPoolExecutor p = |
454 |
> |
new ScheduledThreadPoolExecutor(1, threadFactory); |
455 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
456 |
> |
assertSame(threadFactory, p.getThreadFactory()); |
457 |
> |
} |
458 |
|
} |
459 |
|
|
460 |
|
/** |
461 |
|
* setThreadFactory sets the thread factory returned by getThreadFactory |
462 |
|
*/ |
463 |
|
public void testSetThreadFactory() throws InterruptedException { |
464 |
< |
ThreadFactory tf = new SimpleThreadFactory(); |
464 |
> |
ThreadFactory threadFactory = new SimpleThreadFactory(); |
465 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
466 |
< |
p.setThreadFactory(tf); |
467 |
< |
assertSame(tf, p.getThreadFactory()); |
468 |
< |
joinPool(p); |
466 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
467 |
> |
p.setThreadFactory(threadFactory); |
468 |
> |
assertSame(threadFactory, p.getThreadFactory()); |
469 |
> |
} |
470 |
|
} |
471 |
|
|
472 |
|
/** |
474 |
|
*/ |
475 |
|
public void testSetThreadFactoryNull() throws InterruptedException { |
476 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
477 |
< |
try { |
478 |
< |
p.setThreadFactory(null); |
479 |
< |
shouldThrow(); |
480 |
< |
} catch (NullPointerException success) { |
481 |
< |
} finally { |
494 |
< |
joinPool(p); |
477 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
478 |
> |
try { |
479 |
> |
p.setThreadFactory(null); |
480 |
> |
shouldThrow(); |
481 |
> |
} catch (NullPointerException success) {} |
482 |
|
} |
483 |
|
} |
484 |
|
|
502 |
|
*/ |
503 |
|
public void testIsTerminated() throws InterruptedException { |
504 |
|
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
505 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
506 |
< |
final CountDownLatch done = new CountDownLatch(1); |
507 |
< |
assertFalse(p.isTerminated()); |
508 |
< |
try { |
505 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
506 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
507 |
> |
final CountDownLatch done = new CountDownLatch(1); |
508 |
> |
assertFalse(p.isTerminated()); |
509 |
|
p.execute(new CheckedRunnable() { |
510 |
|
public void realRun() throws InterruptedException { |
511 |
|
assertFalse(p.isTerminated()); |
512 |
|
threadStarted.countDown(); |
513 |
< |
done.await(); |
513 |
> |
await(done); |
514 |
|
}}); |
515 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
515 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
516 |
|
assertFalse(p.isTerminating()); |
517 |
|
done.countDown(); |
531 |
– |
} finally { |
518 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
519 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
520 |
+ |
assertTrue(p.isTerminated()); |
521 |
|
} |
534 |
– |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
535 |
– |
assertTrue(p.isTerminated()); |
522 |
|
} |
523 |
|
|
524 |
|
/** |
528 |
|
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
529 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
530 |
|
final CountDownLatch done = new CountDownLatch(1); |
531 |
< |
try { |
531 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
532 |
|
assertFalse(p.isTerminating()); |
533 |
|
p.execute(new CheckedRunnable() { |
534 |
|
public void realRun() throws InterruptedException { |
535 |
|
assertFalse(p.isTerminating()); |
536 |
|
threadStarted.countDown(); |
537 |
< |
done.await(); |
537 |
> |
await(done); |
538 |
|
}}); |
539 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
539 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
540 |
|
assertFalse(p.isTerminating()); |
541 |
|
done.countDown(); |
556 |
– |
} finally { |
542 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
543 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
544 |
+ |
assertTrue(p.isTerminated()); |
545 |
+ |
assertFalse(p.isTerminating()); |
546 |
|
} |
559 |
– |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
560 |
– |
assertTrue(p.isTerminated()); |
561 |
– |
assertFalse(p.isTerminating()); |
547 |
|
} |
548 |
|
|
549 |
|
/** |
551 |
|
*/ |
552 |
|
public void testGetQueue() throws InterruptedException { |
553 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
554 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
555 |
< |
final CountDownLatch done = new CountDownLatch(1); |
556 |
< |
try { |
554 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
555 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
556 |
> |
final CountDownLatch done = new CountDownLatch(1); |
557 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
558 |
|
for (int i = 0; i < tasks.length; i++) { |
559 |
|
Runnable r = new CheckedRunnable() { |
560 |
|
public void realRun() throws InterruptedException { |
561 |
|
threadStarted.countDown(); |
562 |
< |
done.await(); |
562 |
> |
await(done); |
563 |
|
}}; |
564 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
565 |
|
} |
566 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
566 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
567 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
568 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
569 |
|
assertFalse(q.contains(tasks[0])); |
585 |
– |
} finally { |
570 |
|
done.countDown(); |
587 |
– |
joinPool(p); |
571 |
|
} |
572 |
|
} |
573 |
|
|
576 |
|
*/ |
577 |
|
public void testRemove() throws InterruptedException { |
578 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
579 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
580 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
581 |
< |
final CountDownLatch done = new CountDownLatch(1); |
582 |
< |
try { |
579 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
580 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
581 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
582 |
> |
final CountDownLatch done = new CountDownLatch(1); |
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(SMALL_DELAY_MS, MILLISECONDS)); |
591 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
592 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
593 |
|
assertFalse(p.remove((Runnable)tasks[0])); |
594 |
|
assertTrue(q.contains((Runnable)tasks[4])); |
599 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
600 |
|
assertTrue(p.remove((Runnable)tasks[3])); |
601 |
|
assertFalse(q.contains((Runnable)tasks[3])); |
619 |
– |
} finally { |
602 |
|
done.countDown(); |
621 |
– |
joinPool(p); |
603 |
|
} |
604 |
|
} |
605 |
|
|
607 |
|
* purge eventually removes cancelled tasks from the queue |
608 |
|
*/ |
609 |
|
public void testPurge() throws InterruptedException { |
610 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
611 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
612 |
< |
for (int i = 0; i < tasks.length; i++) |
613 |
< |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
614 |
< |
LONG_DELAY_MS, MILLISECONDS); |
615 |
< |
try { |
610 |
> |
final ScheduledFuture[] tasks = new ScheduledFuture[5]; |
611 |
> |
final Runnable releaser = new Runnable() { public void run() { |
612 |
> |
for (ScheduledFuture task : tasks) |
613 |
> |
if (task != null) task.cancel(true); }}; |
614 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
615 |
> |
try (PoolCleaner cleaner = cleaner(p, releaser)) { |
616 |
> |
for (int i = 0; i < tasks.length; i++) |
617 |
> |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
618 |
> |
LONG_DELAY_MS, MILLISECONDS); |
619 |
|
int max = tasks.length; |
620 |
|
if (tasks[4].cancel(true)) --max; |
621 |
|
if (tasks[3].cancel(true)) --max; |
627 |
|
long count = p.getTaskCount(); |
628 |
|
if (count == max) |
629 |
|
return; |
630 |
< |
} while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
630 |
> |
} while (millisElapsedSince(startTime) < LONG_DELAY_MS); |
631 |
|
fail("Purge failed to remove cancelled tasks"); |
648 |
– |
} finally { |
649 |
– |
for (ScheduledFuture task : tasks) |
650 |
– |
task.cancel(true); |
651 |
– |
joinPool(p); |
632 |
|
} |
633 |
|
} |
634 |
|
|
636 |
|
* shutdownNow returns a list containing tasks that were not run, |
637 |
|
* and those tasks are drained from the queue |
638 |
|
*/ |
639 |
+ |
public void testShutdownNow() throws InterruptedException { |
640 |
+ |
final int poolSize = 2; |
641 |
+ |
final int count = 5; |
642 |
+ |
final AtomicInteger ran = new AtomicInteger(0); |
643 |
+ |
final ScheduledThreadPoolExecutor p = |
644 |
+ |
new ScheduledThreadPoolExecutor(poolSize); |
645 |
+ |
final CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
646 |
+ |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
647 |
+ |
threadsStarted.countDown(); |
648 |
+ |
try { |
649 |
+ |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
650 |
+ |
} catch (InterruptedException success) {} |
651 |
+ |
ran.getAndIncrement(); |
652 |
+ |
}}; |
653 |
+ |
for (int i = 0; i < count; i++) |
654 |
+ |
p.execute(waiter); |
655 |
+ |
await(threadsStarted); |
656 |
+ |
assertEquals(poolSize, p.getActiveCount()); |
657 |
+ |
assertEquals(0, p.getCompletedTaskCount()); |
658 |
+ |
final List<Runnable> queuedTasks; |
659 |
+ |
try { |
660 |
+ |
queuedTasks = p.shutdownNow(); |
661 |
+ |
} catch (SecurityException ok) { |
662 |
+ |
return; // Allowed in case test doesn't have privs |
663 |
+ |
} |
664 |
+ |
assertTrue(p.isShutdown()); |
665 |
+ |
assertTrue(p.getQueue().isEmpty()); |
666 |
+ |
assertEquals(count - poolSize, queuedTasks.size()); |
667 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
668 |
+ |
assertTrue(p.isTerminated()); |
669 |
+ |
assertEquals(poolSize, ran.get()); |
670 |
+ |
assertEquals(poolSize, p.getCompletedTaskCount()); |
671 |
+ |
} |
672 |
+ |
|
673 |
+ |
/** |
674 |
+ |
* shutdownNow returns a list containing tasks that were not run, |
675 |
+ |
* and those tasks are drained from the queue |
676 |
+ |
*/ |
677 |
|
public void testShutdownNow_delayedTasks() throws InterruptedException { |
678 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
679 |
|
List<ScheduledFuture> tasks = new ArrayList<>(); |
705 |
|
} |
706 |
|
|
707 |
|
/** |
708 |
< |
* In default setting, shutdown cancels periodic but not delayed |
709 |
< |
* tasks at shutdown |
710 |
< |
*/ |
711 |
< |
public void testShutdown1() throws InterruptedException { |
712 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
713 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
714 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
715 |
< |
|
716 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
717 |
< |
for (int i = 0; i < tasks.length; i++) |
718 |
< |
tasks[i] = p.schedule(new NoOpRunnable(), |
719 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
720 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
721 |
< |
BlockingQueue<Runnable> q = p.getQueue(); |
722 |
< |
for (ScheduledFuture task : tasks) { |
723 |
< |
assertFalse(task.isDone()); |
724 |
< |
assertFalse(task.isCancelled()); |
725 |
< |
assertTrue(q.contains(task)); |
726 |
< |
} |
727 |
< |
assertTrue(p.isShutdown()); |
728 |
< |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
729 |
< |
assertTrue(p.isTerminated()); |
730 |
< |
for (ScheduledFuture task : tasks) { |
731 |
< |
assertTrue(task.isDone()); |
732 |
< |
assertFalse(task.isCancelled()); |
733 |
< |
} |
734 |
< |
} |
708 |
> |
* By default, periodic tasks are cancelled at shutdown. |
709 |
> |
* By default, delayed tasks keep running after shutdown. |
710 |
> |
* Check that changing the default values work: |
711 |
> |
* - setExecuteExistingDelayedTasksAfterShutdownPolicy |
712 |
> |
* - setContinueExistingPeriodicTasksAfterShutdownPolicy |
713 |
> |
*/ |
714 |
> |
public void testShutdown_cancellation() throws Exception { |
715 |
> |
Boolean[] allBooleans = { null, Boolean.FALSE, Boolean.TRUE }; |
716 |
> |
for (Boolean policy : allBooleans) |
717 |
> |
{ |
718 |
> |
final int poolSize = 2; |
719 |
> |
final ScheduledThreadPoolExecutor p |
720 |
> |
= new ScheduledThreadPoolExecutor(poolSize); |
721 |
> |
final boolean effectiveDelayedPolicy = (policy != Boolean.FALSE); |
722 |
> |
final boolean effectivePeriodicPolicy = (policy == Boolean.TRUE); |
723 |
> |
final boolean effectiveRemovePolicy = (policy == Boolean.TRUE); |
724 |
> |
if (policy != null) { |
725 |
> |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(policy); |
726 |
> |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(policy); |
727 |
> |
p.setRemoveOnCancelPolicy(policy); |
728 |
> |
} |
729 |
> |
assertEquals(effectiveDelayedPolicy, |
730 |
> |
p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
731 |
> |
assertEquals(effectivePeriodicPolicy, |
732 |
> |
p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
733 |
> |
assertEquals(effectiveRemovePolicy, |
734 |
> |
p.getRemoveOnCancelPolicy()); |
735 |
> |
// Strategy: Wedge the pool with poolSize "blocker" threads |
736 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
737 |
> |
final CountDownLatch poolBlocked = new CountDownLatch(poolSize); |
738 |
> |
final CountDownLatch unblock = new CountDownLatch(1); |
739 |
> |
final CountDownLatch periodicLatch1 = new CountDownLatch(2); |
740 |
> |
final CountDownLatch periodicLatch2 = new CountDownLatch(2); |
741 |
> |
Runnable task = new CheckedRunnable() { public void realRun() |
742 |
> |
throws InterruptedException { |
743 |
> |
poolBlocked.countDown(); |
744 |
> |
assertTrue(unblock.await(LONG_DELAY_MS, MILLISECONDS)); |
745 |
> |
ran.getAndIncrement(); |
746 |
> |
}}; |
747 |
> |
List<Future<?>> blockers = new ArrayList<>(); |
748 |
> |
List<Future<?>> periodics = new ArrayList<>(); |
749 |
> |
List<Future<?>> delayeds = new ArrayList<>(); |
750 |
> |
for (int i = 0; i < poolSize; i++) |
751 |
> |
blockers.add(p.submit(task)); |
752 |
> |
assertTrue(poolBlocked.await(LONG_DELAY_MS, MILLISECONDS)); |
753 |
> |
|
754 |
> |
periodics.add(p.scheduleAtFixedRate(countDowner(periodicLatch1), |
755 |
> |
1, 1, MILLISECONDS)); |
756 |
> |
periodics.add(p.scheduleWithFixedDelay(countDowner(periodicLatch2), |
757 |
> |
1, 1, MILLISECONDS)); |
758 |
> |
delayeds.add(p.schedule(task, 1, MILLISECONDS)); |
759 |
|
|
760 |
< |
/** |
761 |
< |
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
720 |
< |
* delayed tasks are cancelled at shutdown |
721 |
< |
*/ |
722 |
< |
public void testShutdown2() throws InterruptedException { |
723 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
724 |
< |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
725 |
< |
assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
726 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
727 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
728 |
< |
for (int i = 0; i < tasks.length; i++) |
729 |
< |
tasks[i] = p.schedule(new NoOpRunnable(), |
730 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
731 |
< |
BlockingQueue q = p.getQueue(); |
732 |
< |
assertEquals(tasks.length, q.size()); |
760 |
> |
assertTrue(p.getQueue().containsAll(periodics)); |
761 |
> |
assertTrue(p.getQueue().containsAll(delayeds)); |
762 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
763 |
|
assertTrue(p.isShutdown()); |
764 |
< |
assertTrue(q.isEmpty()); |
765 |
< |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
766 |
< |
assertTrue(p.isTerminated()); |
767 |
< |
for (ScheduledFuture task : tasks) { |
768 |
< |
assertTrue(task.isDone()); |
769 |
< |
assertTrue(task.isCancelled()); |
770 |
< |
} |
771 |
< |
} |
772 |
< |
|
773 |
< |
/** |
774 |
< |
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
775 |
< |
* periodic tasks are cancelled at shutdown |
776 |
< |
*/ |
777 |
< |
public void testShutdown3() throws InterruptedException { |
778 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
779 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
780 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
781 |
< |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
782 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
783 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
784 |
< |
long initialDelay = LONG_DELAY_MS; |
785 |
< |
ScheduledFuture task = |
757 |
< |
p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay, |
758 |
< |
5, MILLISECONDS); |
759 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
760 |
< |
assertTrue(p.isShutdown()); |
761 |
< |
assertTrue(p.getQueue().isEmpty()); |
762 |
< |
assertTrue(task.isDone()); |
763 |
< |
assertTrue(task.isCancelled()); |
764 |
< |
joinPool(p); |
765 |
< |
} |
766 |
< |
|
767 |
< |
/** |
768 |
< |
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
769 |
< |
* periodic tasks are not cancelled at shutdown |
770 |
< |
*/ |
771 |
< |
public void testShutdown4() throws InterruptedException { |
772 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
773 |
< |
final CountDownLatch counter = new CountDownLatch(2); |
774 |
< |
try { |
775 |
< |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
776 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
777 |
< |
assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
778 |
< |
final Runnable r = new CheckedRunnable() { |
779 |
< |
public void realRun() { |
780 |
< |
counter.countDown(); |
781 |
< |
}}; |
782 |
< |
ScheduledFuture task = |
783 |
< |
p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS); |
784 |
< |
assertFalse(task.isDone()); |
785 |
< |
assertFalse(task.isCancelled()); |
786 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
787 |
< |
assertFalse(task.isCancelled()); |
788 |
< |
assertFalse(p.isTerminated()); |
789 |
< |
assertTrue(p.isShutdown()); |
790 |
< |
assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS)); |
791 |
< |
assertFalse(task.isCancelled()); |
792 |
< |
assertTrue(task.cancel(false)); |
793 |
< |
assertTrue(task.isDone()); |
794 |
< |
assertTrue(task.isCancelled()); |
795 |
< |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
796 |
< |
assertTrue(p.isTerminated()); |
764 |
> |
assertFalse(p.isTerminated()); |
765 |
> |
for (Future<?> periodic : periodics) { |
766 |
> |
assertTrue(effectivePeriodicPolicy ^ periodic.isCancelled()); |
767 |
> |
assertTrue(effectivePeriodicPolicy ^ periodic.isDone()); |
768 |
> |
} |
769 |
> |
for (Future<?> delayed : delayeds) { |
770 |
> |
assertTrue(effectiveDelayedPolicy ^ delayed.isCancelled()); |
771 |
> |
assertTrue(effectiveDelayedPolicy ^ delayed.isDone()); |
772 |
> |
} |
773 |
> |
if (testImplementationDetails) { |
774 |
> |
assertEquals(effectivePeriodicPolicy, |
775 |
> |
p.getQueue().containsAll(periodics)); |
776 |
> |
assertEquals(effectiveDelayedPolicy, |
777 |
> |
p.getQueue().containsAll(delayeds)); |
778 |
> |
} |
779 |
> |
// Release all pool threads |
780 |
> |
unblock.countDown(); |
781 |
> |
|
782 |
> |
for (Future<?> delayed : delayeds) { |
783 |
> |
if (effectiveDelayedPolicy) { |
784 |
> |
assertNull(delayed.get()); |
785 |
> |
} |
786 |
|
} |
787 |
< |
finally { |
788 |
< |
joinPool(p); |
787 |
> |
if (effectivePeriodicPolicy) { |
788 |
> |
assertTrue(periodicLatch1.await(LONG_DELAY_MS, MILLISECONDS)); |
789 |
> |
assertTrue(periodicLatch2.await(LONG_DELAY_MS, MILLISECONDS)); |
790 |
> |
for (Future<?> periodic : periodics) { |
791 |
> |
assertTrue(periodic.cancel(false)); |
792 |
> |
assertTrue(periodic.isCancelled()); |
793 |
> |
assertTrue(periodic.isDone()); |
794 |
> |
} |
795 |
|
} |
796 |
< |
} |
796 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
797 |
> |
assertTrue(p.isTerminated()); |
798 |
> |
assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get()); |
799 |
> |
}} |
800 |
|
|
801 |
|
/** |
802 |
|
* completed submit of callable returns result |
803 |
|
*/ |
804 |
|
public void testSubmitCallable() throws Exception { |
805 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
806 |
< |
try { |
805 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
806 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
807 |
|
Future<String> future = e.submit(new StringTask()); |
808 |
|
String result = future.get(); |
809 |
|
assertSame(TEST_STRING, result); |
812 |
– |
} finally { |
813 |
– |
joinPool(e); |
810 |
|
} |
811 |
|
} |
812 |
|
|
814 |
|
* completed submit of runnable returns successfully |
815 |
|
*/ |
816 |
|
public void testSubmitRunnable() throws Exception { |
817 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
818 |
< |
try { |
817 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
818 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
819 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
820 |
|
future.get(); |
821 |
|
assertTrue(future.isDone()); |
826 |
– |
} finally { |
827 |
– |
joinPool(e); |
822 |
|
} |
823 |
|
} |
824 |
|
|
826 |
|
* completed submit of (runnable, result) returns result |
827 |
|
*/ |
828 |
|
public void testSubmitRunnable2() throws Exception { |
829 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
830 |
< |
try { |
829 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
830 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
831 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
832 |
|
String result = future.get(); |
833 |
|
assertSame(TEST_STRING, result); |
840 |
– |
} finally { |
841 |
– |
joinPool(e); |
834 |
|
} |
835 |
|
} |
836 |
|
|
838 |
|
* invokeAny(null) throws NPE |
839 |
|
*/ |
840 |
|
public void testInvokeAny1() throws Exception { |
841 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
842 |
< |
try { |
843 |
< |
e.invokeAny(null); |
844 |
< |
shouldThrow(); |
845 |
< |
} catch (NullPointerException success) { |
846 |
< |
} finally { |
855 |
< |
joinPool(e); |
841 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
842 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
843 |
> |
try { |
844 |
> |
e.invokeAny(null); |
845 |
> |
shouldThrow(); |
846 |
> |
} catch (NullPointerException success) {} |
847 |
|
} |
848 |
|
} |
849 |
|
|
851 |
|
* invokeAny(empty collection) throws IAE |
852 |
|
*/ |
853 |
|
public void testInvokeAny2() throws Exception { |
854 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
855 |
< |
try { |
856 |
< |
e.invokeAny(new ArrayList<Callable<String>>()); |
857 |
< |
shouldThrow(); |
858 |
< |
} catch (IllegalArgumentException success) { |
859 |
< |
} finally { |
869 |
< |
joinPool(e); |
854 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
855 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
856 |
> |
try { |
857 |
> |
e.invokeAny(new ArrayList<Callable<String>>()); |
858 |
> |
shouldThrow(); |
859 |
> |
} catch (IllegalArgumentException success) {} |
860 |
|
} |
861 |
|
} |
862 |
|
|
865 |
|
*/ |
866 |
|
public void testInvokeAny3() throws Exception { |
867 |
|
CountDownLatch latch = new CountDownLatch(1); |
868 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
869 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
870 |
< |
l.add(latchAwaitingStringTask(latch)); |
871 |
< |
l.add(null); |
872 |
< |
try { |
873 |
< |
e.invokeAny(l); |
874 |
< |
shouldThrow(); |
875 |
< |
} catch (NullPointerException success) { |
876 |
< |
} finally { |
868 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
869 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
870 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
871 |
> |
l.add(latchAwaitingStringTask(latch)); |
872 |
> |
l.add(null); |
873 |
> |
try { |
874 |
> |
e.invokeAny(l); |
875 |
> |
shouldThrow(); |
876 |
> |
} catch (NullPointerException success) {} |
877 |
|
latch.countDown(); |
888 |
– |
joinPool(e); |
878 |
|
} |
879 |
|
} |
880 |
|
|
882 |
|
* invokeAny(c) throws ExecutionException if no task completes |
883 |
|
*/ |
884 |
|
public void testInvokeAny4() throws Exception { |
885 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
886 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
887 |
< |
l.add(new NPETask()); |
888 |
< |
try { |
889 |
< |
e.invokeAny(l); |
890 |
< |
shouldThrow(); |
891 |
< |
} catch (ExecutionException success) { |
892 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
893 |
< |
} finally { |
894 |
< |
joinPool(e); |
885 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
886 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
887 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
888 |
> |
l.add(new NPETask()); |
889 |
> |
try { |
890 |
> |
e.invokeAny(l); |
891 |
> |
shouldThrow(); |
892 |
> |
} catch (ExecutionException success) { |
893 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
894 |
> |
} |
895 |
|
} |
896 |
|
} |
897 |
|
|
899 |
|
* invokeAny(c) returns result of some task |
900 |
|
*/ |
901 |
|
public void testInvokeAny5() throws Exception { |
902 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
903 |
< |
try { |
902 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
903 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
904 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
905 |
|
l.add(new StringTask()); |
906 |
|
l.add(new StringTask()); |
907 |
|
String result = e.invokeAny(l); |
908 |
|
assertSame(TEST_STRING, result); |
920 |
– |
} finally { |
921 |
– |
joinPool(e); |
909 |
|
} |
910 |
|
} |
911 |
|
|
913 |
|
* invokeAll(null) throws NPE |
914 |
|
*/ |
915 |
|
public void testInvokeAll1() throws Exception { |
916 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
917 |
< |
try { |
918 |
< |
e.invokeAll(null); |
919 |
< |
shouldThrow(); |
920 |
< |
} catch (NullPointerException success) { |
921 |
< |
} finally { |
935 |
< |
joinPool(e); |
916 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
917 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
918 |
> |
try { |
919 |
> |
e.invokeAll(null); |
920 |
> |
shouldThrow(); |
921 |
> |
} catch (NullPointerException success) {} |
922 |
|
} |
923 |
|
} |
924 |
|
|
926 |
|
* invokeAll(empty collection) returns empty collection |
927 |
|
*/ |
928 |
|
public void testInvokeAll2() throws Exception { |
929 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
930 |
< |
try { |
929 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
930 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
931 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
932 |
|
assertTrue(r.isEmpty()); |
947 |
– |
} finally { |
948 |
– |
joinPool(e); |
933 |
|
} |
934 |
|
} |
935 |
|
|
937 |
|
* invokeAll(c) throws NPE if c has null elements |
938 |
|
*/ |
939 |
|
public void testInvokeAll3() throws Exception { |
940 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
941 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
942 |
< |
l.add(new StringTask()); |
943 |
< |
l.add(null); |
944 |
< |
try { |
945 |
< |
e.invokeAll(l); |
946 |
< |
shouldThrow(); |
947 |
< |
} catch (NullPointerException success) { |
948 |
< |
} finally { |
965 |
< |
joinPool(e); |
940 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
941 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
942 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
943 |
> |
l.add(new StringTask()); |
944 |
> |
l.add(null); |
945 |
> |
try { |
946 |
> |
e.invokeAll(l); |
947 |
> |
shouldThrow(); |
948 |
> |
} catch (NullPointerException success) {} |
949 |
|
} |
950 |
|
} |
951 |
|
|
953 |
|
* get of invokeAll(c) throws exception on failed task |
954 |
|
*/ |
955 |
|
public void testInvokeAll4() throws Exception { |
956 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
957 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
958 |
< |
l.add(new NPETask()); |
959 |
< |
List<Future<String>> futures = e.invokeAll(l); |
960 |
< |
assertEquals(1, futures.size()); |
961 |
< |
try { |
962 |
< |
futures.get(0).get(); |
963 |
< |
shouldThrow(); |
964 |
< |
} catch (ExecutionException success) { |
965 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
966 |
< |
} finally { |
967 |
< |
joinPool(e); |
956 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
957 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
958 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
959 |
> |
l.add(new NPETask()); |
960 |
> |
List<Future<String>> futures = e.invokeAll(l); |
961 |
> |
assertEquals(1, futures.size()); |
962 |
> |
try { |
963 |
> |
futures.get(0).get(); |
964 |
> |
shouldThrow(); |
965 |
> |
} catch (ExecutionException success) { |
966 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
967 |
> |
} |
968 |
|
} |
969 |
|
} |
970 |
|
|
972 |
|
* invokeAll(c) returns results of all completed tasks |
973 |
|
*/ |
974 |
|
public void testInvokeAll5() throws Exception { |
975 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
976 |
< |
try { |
975 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
976 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
977 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
978 |
|
l.add(new StringTask()); |
979 |
|
l.add(new StringTask()); |
981 |
|
assertEquals(2, futures.size()); |
982 |
|
for (Future<String> future : futures) |
983 |
|
assertSame(TEST_STRING, future.get()); |
1001 |
– |
} finally { |
1002 |
– |
joinPool(e); |
984 |
|
} |
985 |
|
} |
986 |
|
|
988 |
|
* timed invokeAny(null) throws NPE |
989 |
|
*/ |
990 |
|
public void testTimedInvokeAny1() throws Exception { |
991 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
992 |
< |
try { |
993 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
994 |
< |
shouldThrow(); |
995 |
< |
} catch (NullPointerException success) { |
996 |
< |
} finally { |
1016 |
< |
joinPool(e); |
991 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
992 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
993 |
> |
try { |
994 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
995 |
> |
shouldThrow(); |
996 |
> |
} catch (NullPointerException success) {} |
997 |
|
} |
998 |
|
} |
999 |
|
|
1001 |
|
* timed invokeAny(,,null) throws NPE |
1002 |
|
*/ |
1003 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1004 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1005 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1006 |
< |
l.add(new StringTask()); |
1007 |
< |
try { |
1008 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1009 |
< |
shouldThrow(); |
1010 |
< |
} catch (NullPointerException success) { |
1011 |
< |
} finally { |
1032 |
< |
joinPool(e); |
1004 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1005 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1006 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1007 |
> |
l.add(new StringTask()); |
1008 |
> |
try { |
1009 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1010 |
> |
shouldThrow(); |
1011 |
> |
} catch (NullPointerException success) {} |
1012 |
|
} |
1013 |
|
} |
1014 |
|
|
1016 |
|
* timed invokeAny(empty collection) throws IAE |
1017 |
|
*/ |
1018 |
|
public void testTimedInvokeAny2() throws Exception { |
1019 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1020 |
< |
try { |
1021 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1022 |
< |
shouldThrow(); |
1023 |
< |
} catch (IllegalArgumentException success) { |
1024 |
< |
} finally { |
1046 |
< |
joinPool(e); |
1019 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1020 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1021 |
> |
try { |
1022 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1023 |
> |
shouldThrow(); |
1024 |
> |
} catch (IllegalArgumentException success) {} |
1025 |
|
} |
1026 |
|
} |
1027 |
|
|
1030 |
|
*/ |
1031 |
|
public void testTimedInvokeAny3() throws Exception { |
1032 |
|
CountDownLatch latch = new CountDownLatch(1); |
1033 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1034 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1035 |
< |
l.add(latchAwaitingStringTask(latch)); |
1036 |
< |
l.add(null); |
1037 |
< |
try { |
1038 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1039 |
< |
shouldThrow(); |
1040 |
< |
} catch (NullPointerException success) { |
1041 |
< |
} finally { |
1033 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1034 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1035 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1036 |
> |
l.add(latchAwaitingStringTask(latch)); |
1037 |
> |
l.add(null); |
1038 |
> |
try { |
1039 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1040 |
> |
shouldThrow(); |
1041 |
> |
} catch (NullPointerException success) {} |
1042 |
|
latch.countDown(); |
1065 |
– |
joinPool(e); |
1043 |
|
} |
1044 |
|
} |
1045 |
|
|
1047 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
1048 |
|
*/ |
1049 |
|
public void testTimedInvokeAny4() throws Exception { |
1050 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1051 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1052 |
< |
l.add(new NPETask()); |
1053 |
< |
try { |
1054 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1055 |
< |
shouldThrow(); |
1056 |
< |
} catch (ExecutionException success) { |
1057 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1058 |
< |
} finally { |
1059 |
< |
joinPool(e); |
1050 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1051 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1052 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1053 |
> |
l.add(new NPETask()); |
1054 |
> |
try { |
1055 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1056 |
> |
shouldThrow(); |
1057 |
> |
} catch (ExecutionException success) { |
1058 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1059 |
> |
} |
1060 |
|
} |
1061 |
|
} |
1062 |
|
|
1064 |
|
* timed invokeAny(c) returns result of some task |
1065 |
|
*/ |
1066 |
|
public void testTimedInvokeAny5() throws Exception { |
1067 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1068 |
< |
try { |
1067 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1068 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
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); |
1073 |
|
assertSame(TEST_STRING, result); |
1097 |
– |
} finally { |
1098 |
– |
joinPool(e); |
1074 |
|
} |
1075 |
|
} |
1076 |
|
|
1078 |
|
* timed invokeAll(null) throws NPE |
1079 |
|
*/ |
1080 |
|
public void testTimedInvokeAll1() throws Exception { |
1081 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1082 |
< |
try { |
1083 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1084 |
< |
shouldThrow(); |
1085 |
< |
} catch (NullPointerException success) { |
1086 |
< |
} finally { |
1112 |
< |
joinPool(e); |
1081 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1082 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1083 |
> |
try { |
1084 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1085 |
> |
shouldThrow(); |
1086 |
> |
} catch (NullPointerException success) {} |
1087 |
|
} |
1088 |
|
} |
1089 |
|
|
1091 |
|
* timed invokeAll(,,null) throws NPE |
1092 |
|
*/ |
1093 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1094 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1095 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1096 |
< |
l.add(new StringTask()); |
1097 |
< |
try { |
1098 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1099 |
< |
shouldThrow(); |
1100 |
< |
} catch (NullPointerException success) { |
1101 |
< |
} finally { |
1128 |
< |
joinPool(e); |
1094 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1095 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1096 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1097 |
> |
l.add(new StringTask()); |
1098 |
> |
try { |
1099 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1100 |
> |
shouldThrow(); |
1101 |
> |
} catch (NullPointerException success) {} |
1102 |
|
} |
1103 |
|
} |
1104 |
|
|
1106 |
|
* timed invokeAll(empty collection) returns empty collection |
1107 |
|
*/ |
1108 |
|
public void testTimedInvokeAll2() throws Exception { |
1109 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1110 |
< |
try { |
1111 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1109 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1110 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1111 |
> |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), |
1112 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
1113 |
|
assertTrue(r.isEmpty()); |
1140 |
– |
} finally { |
1141 |
– |
joinPool(e); |
1114 |
|
} |
1115 |
|
} |
1116 |
|
|
1118 |
|
* timed invokeAll(c) throws NPE if c has null elements |
1119 |
|
*/ |
1120 |
|
public void testTimedInvokeAll3() throws Exception { |
1121 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1122 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1123 |
< |
l.add(new StringTask()); |
1124 |
< |
l.add(null); |
1125 |
< |
try { |
1126 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1127 |
< |
shouldThrow(); |
1128 |
< |
} catch (NullPointerException success) { |
1129 |
< |
} finally { |
1158 |
< |
joinPool(e); |
1121 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1122 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1123 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1124 |
> |
l.add(new StringTask()); |
1125 |
> |
l.add(null); |
1126 |
> |
try { |
1127 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1128 |
> |
shouldThrow(); |
1129 |
> |
} catch (NullPointerException success) {} |
1130 |
|
} |
1131 |
|
} |
1132 |
|
|
1134 |
|
* get of element of invokeAll(c) throws exception on failed task |
1135 |
|
*/ |
1136 |
|
public void testTimedInvokeAll4() throws Exception { |
1137 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1138 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1139 |
< |
l.add(new NPETask()); |
1140 |
< |
List<Future<String>> futures = |
1141 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1142 |
< |
assertEquals(1, futures.size()); |
1143 |
< |
try { |
1144 |
< |
futures.get(0).get(); |
1145 |
< |
shouldThrow(); |
1146 |
< |
} catch (ExecutionException success) { |
1147 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1148 |
< |
} finally { |
1149 |
< |
joinPool(e); |
1137 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1138 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1139 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1140 |
> |
l.add(new NPETask()); |
1141 |
> |
List<Future<String>> futures = |
1142 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1143 |
> |
assertEquals(1, futures.size()); |
1144 |
> |
try { |
1145 |
> |
futures.get(0).get(); |
1146 |
> |
shouldThrow(); |
1147 |
> |
} catch (ExecutionException success) { |
1148 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1149 |
> |
} |
1150 |
|
} |
1151 |
|
} |
1152 |
|
|
1154 |
|
* timed invokeAll(c) returns results of all completed tasks |
1155 |
|
*/ |
1156 |
|
public void testTimedInvokeAll5() throws Exception { |
1157 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1158 |
< |
try { |
1157 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1158 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1159 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1160 |
|
l.add(new StringTask()); |
1161 |
|
l.add(new StringTask()); |
1162 |
|
List<Future<String>> futures = |
1163 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1163 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1164 |
|
assertEquals(2, futures.size()); |
1165 |
|
for (Future<String> future : futures) |
1166 |
|
assertSame(TEST_STRING, future.get()); |
1196 |
– |
} finally { |
1197 |
– |
joinPool(e); |
1167 |
|
} |
1168 |
|
} |
1169 |
|
|
1171 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1172 |
|
*/ |
1173 |
|
public void testTimedInvokeAll6() throws Exception { |
1174 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1175 |
< |
try { |
1174 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1175 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1176 |
|
for (long timeout = timeoutMillis();;) { |
1177 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1178 |
|
tasks.add(new StringTask("0")); |
1196 |
|
fail("expected exactly one task to be cancelled"); |
1197 |
|
} |
1198 |
|
} |
1230 |
– |
} finally { |
1231 |
– |
joinPool(e); |
1199 |
|
} |
1200 |
|
} |
1201 |
|
|