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); |
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; |
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; |
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 { |
317 |
|
assertEquals(1, p.getActiveCount()); |
318 |
|
done.await(); |
319 |
|
}}); |
320 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
320 |
> |
assertTrue(threadStarted.await(MEDIUM_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)) { |
377 |
|
assertEquals(0, p.getLargestPoolSize()); |
378 |
|
for (int i = 0; i < THREADS; i++) |
379 |
|
p.execute(new CheckedRunnable() { |
382 |
|
done.await(); |
383 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
384 |
|
}}); |
385 |
< |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
385 |
> |
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
386 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
407 |
– |
} finally { |
387 |
|
done.countDown(); |
409 |
– |
joinPool(p); |
410 |
– |
assertEquals(THREADS, p.getLargestPoolSize()); |
388 |
|
} |
389 |
+ |
assertEquals(THREADS, p.getLargestPoolSize()); |
390 |
|
} |
391 |
|
|
392 |
|
/** |
397 |
|
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
398 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
399 |
|
final CountDownLatch done = new CountDownLatch(1); |
400 |
< |
try { |
400 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
401 |
|
assertEquals(0, p.getPoolSize()); |
402 |
|
p.execute(new CheckedRunnable() { |
403 |
|
public void realRun() throws InterruptedException { |
405 |
|
assertEquals(1, p.getPoolSize()); |
406 |
|
done.await(); |
407 |
|
}}); |
408 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
408 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
409 |
|
assertEquals(1, p.getPoolSize()); |
432 |
– |
} finally { |
410 |
|
done.countDown(); |
434 |
– |
joinPool(p); |
411 |
|
} |
412 |
|
} |
413 |
|
|
417 |
|
*/ |
418 |
|
public void testGetTaskCount() throws InterruptedException { |
419 |
|
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
420 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
421 |
< |
final CountDownLatch done = new CountDownLatch(1); |
422 |
< |
final int TASKS = 5; |
423 |
< |
try { |
420 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
421 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
422 |
> |
final CountDownLatch done = new CountDownLatch(1); |
423 |
> |
final int TASKS = 5; |
424 |
|
assertEquals(0, p.getTaskCount()); |
425 |
|
for (int i = 0; i < TASKS; i++) |
426 |
|
p.execute(new CheckedRunnable() { |
428 |
|
threadStarted.countDown(); |
429 |
|
done.await(); |
430 |
|
}}); |
431 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
431 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
432 |
|
assertEquals(TASKS, p.getTaskCount()); |
457 |
– |
} finally { |
433 |
|
done.countDown(); |
459 |
– |
joinPool(p); |
434 |
|
} |
435 |
|
} |
436 |
|
|
438 |
|
* getThreadFactory returns factory in constructor if not set |
439 |
|
*/ |
440 |
|
public void testGetThreadFactory() throws InterruptedException { |
441 |
< |
ThreadFactory tf = new SimpleThreadFactory(); |
442 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf); |
443 |
< |
assertSame(tf, p.getThreadFactory()); |
444 |
< |
joinPool(p); |
441 |
> |
final ThreadFactory threadFactory = new SimpleThreadFactory(); |
442 |
> |
final ScheduledThreadPoolExecutor p = |
443 |
> |
new ScheduledThreadPoolExecutor(1, threadFactory); |
444 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
445 |
> |
assertSame(threadFactory, p.getThreadFactory()); |
446 |
> |
} |
447 |
|
} |
448 |
|
|
449 |
|
/** |
450 |
|
* setThreadFactory sets the thread factory returned by getThreadFactory |
451 |
|
*/ |
452 |
|
public void testSetThreadFactory() throws InterruptedException { |
453 |
< |
ThreadFactory tf = new SimpleThreadFactory(); |
453 |
> |
ThreadFactory threadFactory = new SimpleThreadFactory(); |
454 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
455 |
< |
p.setThreadFactory(tf); |
456 |
< |
assertSame(tf, p.getThreadFactory()); |
457 |
< |
joinPool(p); |
455 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
456 |
> |
p.setThreadFactory(threadFactory); |
457 |
> |
assertSame(threadFactory, p.getThreadFactory()); |
458 |
> |
} |
459 |
|
} |
460 |
|
|
461 |
|
/** |
463 |
|
*/ |
464 |
|
public void testSetThreadFactoryNull() throws InterruptedException { |
465 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
466 |
< |
try { |
467 |
< |
p.setThreadFactory(null); |
468 |
< |
shouldThrow(); |
469 |
< |
} catch (NullPointerException success) { |
470 |
< |
} finally { |
494 |
< |
joinPool(p); |
466 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
467 |
> |
try { |
468 |
> |
p.setThreadFactory(null); |
469 |
> |
shouldThrow(); |
470 |
> |
} catch (NullPointerException success) {} |
471 |
|
} |
472 |
|
} |
473 |
|
|
491 |
|
*/ |
492 |
|
public void testIsTerminated() throws InterruptedException { |
493 |
|
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
494 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
495 |
< |
final CountDownLatch done = new CountDownLatch(1); |
496 |
< |
assertFalse(p.isTerminated()); |
497 |
< |
try { |
494 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
495 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
496 |
> |
final CountDownLatch done = new CountDownLatch(1); |
497 |
> |
assertFalse(p.isTerminated()); |
498 |
|
p.execute(new CheckedRunnable() { |
499 |
|
public void realRun() throws InterruptedException { |
500 |
|
assertFalse(p.isTerminated()); |
501 |
|
threadStarted.countDown(); |
502 |
|
done.await(); |
503 |
|
}}); |
504 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
504 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
505 |
|
assertFalse(p.isTerminating()); |
506 |
|
done.countDown(); |
531 |
– |
} finally { |
507 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
508 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
509 |
+ |
assertTrue(p.isTerminated()); |
510 |
|
} |
534 |
– |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
535 |
– |
assertTrue(p.isTerminated()); |
511 |
|
} |
512 |
|
|
513 |
|
/** |
517 |
|
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
518 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
519 |
|
final CountDownLatch done = new CountDownLatch(1); |
520 |
< |
try { |
520 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
521 |
|
assertFalse(p.isTerminating()); |
522 |
|
p.execute(new CheckedRunnable() { |
523 |
|
public void realRun() throws InterruptedException { |
525 |
|
threadStarted.countDown(); |
526 |
|
done.await(); |
527 |
|
}}); |
528 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
528 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
529 |
|
assertFalse(p.isTerminating()); |
530 |
|
done.countDown(); |
556 |
– |
} finally { |
531 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
532 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
533 |
+ |
assertTrue(p.isTerminated()); |
534 |
+ |
assertFalse(p.isTerminating()); |
535 |
|
} |
559 |
– |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
560 |
– |
assertTrue(p.isTerminated()); |
561 |
– |
assertFalse(p.isTerminating()); |
536 |
|
} |
537 |
|
|
538 |
|
/** |
540 |
|
*/ |
541 |
|
public void testGetQueue() throws InterruptedException { |
542 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
543 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
544 |
< |
final CountDownLatch done = new CountDownLatch(1); |
545 |
< |
try { |
543 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
544 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
545 |
> |
final CountDownLatch done = new CountDownLatch(1); |
546 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
547 |
|
for (int i = 0; i < tasks.length; i++) { |
548 |
|
Runnable r = new CheckedRunnable() { |
552 |
|
}}; |
553 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
554 |
|
} |
555 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
555 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
556 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
557 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
558 |
|
assertFalse(q.contains(tasks[0])); |
585 |
– |
} finally { |
559 |
|
done.countDown(); |
587 |
– |
joinPool(p); |
560 |
|
} |
561 |
|
} |
562 |
|
|
565 |
|
*/ |
566 |
|
public void testRemove() throws InterruptedException { |
567 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
568 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
569 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
570 |
< |
final CountDownLatch done = new CountDownLatch(1); |
571 |
< |
try { |
568 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
569 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
570 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
571 |
> |
final CountDownLatch done = new CountDownLatch(1); |
572 |
|
for (int i = 0; i < tasks.length; i++) { |
573 |
|
Runnable r = new CheckedRunnable() { |
574 |
|
public void realRun() throws InterruptedException { |
577 |
|
}}; |
578 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
579 |
|
} |
580 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
580 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
581 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
582 |
|
assertFalse(p.remove((Runnable)tasks[0])); |
583 |
|
assertTrue(q.contains((Runnable)tasks[4])); |
588 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
589 |
|
assertTrue(p.remove((Runnable)tasks[3])); |
590 |
|
assertFalse(q.contains((Runnable)tasks[3])); |
619 |
– |
} finally { |
591 |
|
done.countDown(); |
621 |
– |
joinPool(p); |
592 |
|
} |
593 |
|
} |
594 |
|
|
632 |
|
final AtomicInteger ran = new AtomicInteger(0); |
633 |
|
final ScheduledThreadPoolExecutor p = |
634 |
|
new ScheduledThreadPoolExecutor(poolSize); |
635 |
< |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
635 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
636 |
|
Runnable waiter = new CheckedRunnable() { public void realRun() { |
637 |
|
threadsStarted.countDown(); |
638 |
|
try { |
792 |
|
* completed submit of callable returns result |
793 |
|
*/ |
794 |
|
public void testSubmitCallable() throws Exception { |
795 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
796 |
< |
try { |
795 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
796 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
797 |
|
Future<String> future = e.submit(new StringTask()); |
798 |
|
String result = future.get(); |
799 |
|
assertSame(TEST_STRING, result); |
830 |
– |
} finally { |
831 |
– |
joinPool(e); |
800 |
|
} |
801 |
|
} |
802 |
|
|
804 |
|
* completed submit of runnable returns successfully |
805 |
|
*/ |
806 |
|
public void testSubmitRunnable() throws Exception { |
807 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
808 |
< |
try { |
807 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
808 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
809 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
810 |
|
future.get(); |
811 |
|
assertTrue(future.isDone()); |
844 |
– |
} finally { |
845 |
– |
joinPool(e); |
812 |
|
} |
813 |
|
} |
814 |
|
|
816 |
|
* completed submit of (runnable, result) returns result |
817 |
|
*/ |
818 |
|
public void testSubmitRunnable2() throws Exception { |
819 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
820 |
< |
try { |
819 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
820 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
821 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
822 |
|
String result = future.get(); |
823 |
|
assertSame(TEST_STRING, result); |
858 |
– |
} finally { |
859 |
– |
joinPool(e); |
824 |
|
} |
825 |
|
} |
826 |
|
|
828 |
|
* invokeAny(null) throws NPE |
829 |
|
*/ |
830 |
|
public void testInvokeAny1() throws Exception { |
831 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
832 |
< |
try { |
833 |
< |
e.invokeAny(null); |
834 |
< |
shouldThrow(); |
835 |
< |
} catch (NullPointerException success) { |
836 |
< |
} finally { |
873 |
< |
joinPool(e); |
831 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
832 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
833 |
> |
try { |
834 |
> |
e.invokeAny(null); |
835 |
> |
shouldThrow(); |
836 |
> |
} catch (NullPointerException success) {} |
837 |
|
} |
838 |
|
} |
839 |
|
|
841 |
|
* invokeAny(empty collection) throws IAE |
842 |
|
*/ |
843 |
|
public void testInvokeAny2() throws Exception { |
844 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
845 |
< |
try { |
846 |
< |
e.invokeAny(new ArrayList<Callable<String>>()); |
847 |
< |
shouldThrow(); |
848 |
< |
} catch (IllegalArgumentException success) { |
849 |
< |
} finally { |
887 |
< |
joinPool(e); |
844 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
845 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
846 |
> |
try { |
847 |
> |
e.invokeAny(new ArrayList<Callable<String>>()); |
848 |
> |
shouldThrow(); |
849 |
> |
} catch (IllegalArgumentException success) {} |
850 |
|
} |
851 |
|
} |
852 |
|
|
855 |
|
*/ |
856 |
|
public void testInvokeAny3() throws Exception { |
857 |
|
CountDownLatch latch = new CountDownLatch(1); |
858 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
859 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
860 |
< |
l.add(latchAwaitingStringTask(latch)); |
861 |
< |
l.add(null); |
862 |
< |
try { |
863 |
< |
e.invokeAny(l); |
864 |
< |
shouldThrow(); |
865 |
< |
} catch (NullPointerException success) { |
866 |
< |
} finally { |
858 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
859 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
860 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
861 |
> |
l.add(latchAwaitingStringTask(latch)); |
862 |
> |
l.add(null); |
863 |
> |
try { |
864 |
> |
e.invokeAny(l); |
865 |
> |
shouldThrow(); |
866 |
> |
} catch (NullPointerException success) {} |
867 |
|
latch.countDown(); |
906 |
– |
joinPool(e); |
868 |
|
} |
869 |
|
} |
870 |
|
|
872 |
|
* invokeAny(c) throws ExecutionException if no task completes |
873 |
|
*/ |
874 |
|
public void testInvokeAny4() throws Exception { |
875 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
876 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
877 |
< |
l.add(new NPETask()); |
878 |
< |
try { |
879 |
< |
e.invokeAny(l); |
880 |
< |
shouldThrow(); |
881 |
< |
} catch (ExecutionException success) { |
882 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
883 |
< |
} finally { |
884 |
< |
joinPool(e); |
875 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
876 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
877 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
878 |
> |
l.add(new NPETask()); |
879 |
> |
try { |
880 |
> |
e.invokeAny(l); |
881 |
> |
shouldThrow(); |
882 |
> |
} catch (ExecutionException success) { |
883 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
884 |
> |
} |
885 |
|
} |
886 |
|
} |
887 |
|
|
889 |
|
* invokeAny(c) returns result of some task |
890 |
|
*/ |
891 |
|
public void testInvokeAny5() throws Exception { |
892 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
893 |
< |
try { |
892 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
893 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
894 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
895 |
|
l.add(new StringTask()); |
896 |
|
l.add(new StringTask()); |
897 |
|
String result = e.invokeAny(l); |
898 |
|
assertSame(TEST_STRING, result); |
938 |
– |
} finally { |
939 |
– |
joinPool(e); |
899 |
|
} |
900 |
|
} |
901 |
|
|
903 |
|
* invokeAll(null) throws NPE |
904 |
|
*/ |
905 |
|
public void testInvokeAll1() throws Exception { |
906 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
907 |
< |
try { |
908 |
< |
e.invokeAll(null); |
909 |
< |
shouldThrow(); |
910 |
< |
} catch (NullPointerException success) { |
911 |
< |
} finally { |
953 |
< |
joinPool(e); |
906 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
907 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
908 |
> |
try { |
909 |
> |
e.invokeAll(null); |
910 |
> |
shouldThrow(); |
911 |
> |
} catch (NullPointerException success) {} |
912 |
|
} |
913 |
|
} |
914 |
|
|
916 |
|
* invokeAll(empty collection) returns empty collection |
917 |
|
*/ |
918 |
|
public void testInvokeAll2() throws Exception { |
919 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
920 |
< |
try { |
919 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
920 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
921 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
922 |
|
assertTrue(r.isEmpty()); |
965 |
– |
} finally { |
966 |
– |
joinPool(e); |
923 |
|
} |
924 |
|
} |
925 |
|
|
927 |
|
* invokeAll(c) throws NPE if c has null elements |
928 |
|
*/ |
929 |
|
public void testInvokeAll3() throws Exception { |
930 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
931 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
932 |
< |
l.add(new StringTask()); |
933 |
< |
l.add(null); |
934 |
< |
try { |
935 |
< |
e.invokeAll(l); |
936 |
< |
shouldThrow(); |
937 |
< |
} catch (NullPointerException success) { |
938 |
< |
} finally { |
983 |
< |
joinPool(e); |
930 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
931 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
932 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
933 |
> |
l.add(new StringTask()); |
934 |
> |
l.add(null); |
935 |
> |
try { |
936 |
> |
e.invokeAll(l); |
937 |
> |
shouldThrow(); |
938 |
> |
} catch (NullPointerException success) {} |
939 |
|
} |
940 |
|
} |
941 |
|
|
943 |
|
* get of invokeAll(c) throws exception on failed task |
944 |
|
*/ |
945 |
|
public void testInvokeAll4() throws Exception { |
946 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
947 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
948 |
< |
l.add(new NPETask()); |
949 |
< |
List<Future<String>> futures = e.invokeAll(l); |
950 |
< |
assertEquals(1, futures.size()); |
951 |
< |
try { |
952 |
< |
futures.get(0).get(); |
953 |
< |
shouldThrow(); |
954 |
< |
} catch (ExecutionException success) { |
955 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
956 |
< |
} finally { |
957 |
< |
joinPool(e); |
946 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
947 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
948 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
949 |
> |
l.add(new NPETask()); |
950 |
> |
List<Future<String>> futures = e.invokeAll(l); |
951 |
> |
assertEquals(1, futures.size()); |
952 |
> |
try { |
953 |
> |
futures.get(0).get(); |
954 |
> |
shouldThrow(); |
955 |
> |
} catch (ExecutionException success) { |
956 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
957 |
> |
} |
958 |
|
} |
959 |
|
} |
960 |
|
|
962 |
|
* invokeAll(c) returns results of all completed tasks |
963 |
|
*/ |
964 |
|
public void testInvokeAll5() throws Exception { |
965 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
966 |
< |
try { |
965 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
966 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
967 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
968 |
|
l.add(new StringTask()); |
969 |
|
l.add(new StringTask()); |
971 |
|
assertEquals(2, futures.size()); |
972 |
|
for (Future<String> future : futures) |
973 |
|
assertSame(TEST_STRING, future.get()); |
1019 |
– |
} finally { |
1020 |
– |
joinPool(e); |
974 |
|
} |
975 |
|
} |
976 |
|
|
978 |
|
* timed invokeAny(null) throws NPE |
979 |
|
*/ |
980 |
|
public void testTimedInvokeAny1() throws Exception { |
981 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
982 |
< |
try { |
983 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
984 |
< |
shouldThrow(); |
985 |
< |
} catch (NullPointerException success) { |
986 |
< |
} finally { |
1034 |
< |
joinPool(e); |
981 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
982 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
983 |
> |
try { |
984 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
985 |
> |
shouldThrow(); |
986 |
> |
} catch (NullPointerException success) {} |
987 |
|
} |
988 |
|
} |
989 |
|
|
991 |
|
* timed invokeAny(,,null) throws NPE |
992 |
|
*/ |
993 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
994 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
995 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
996 |
< |
l.add(new StringTask()); |
997 |
< |
try { |
998 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
999 |
< |
shouldThrow(); |
1000 |
< |
} catch (NullPointerException success) { |
1001 |
< |
} finally { |
1050 |
< |
joinPool(e); |
994 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
995 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
996 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
997 |
> |
l.add(new StringTask()); |
998 |
> |
try { |
999 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1000 |
> |
shouldThrow(); |
1001 |
> |
} catch (NullPointerException success) {} |
1002 |
|
} |
1003 |
|
} |
1004 |
|
|
1006 |
|
* timed invokeAny(empty collection) throws IAE |
1007 |
|
*/ |
1008 |
|
public void testTimedInvokeAny2() throws Exception { |
1009 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1010 |
< |
try { |
1011 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1012 |
< |
shouldThrow(); |
1013 |
< |
} catch (IllegalArgumentException success) { |
1014 |
< |
} finally { |
1064 |
< |
joinPool(e); |
1009 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1010 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1011 |
> |
try { |
1012 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1013 |
> |
shouldThrow(); |
1014 |
> |
} catch (IllegalArgumentException success) {} |
1015 |
|
} |
1016 |
|
} |
1017 |
|
|
1020 |
|
*/ |
1021 |
|
public void testTimedInvokeAny3() throws Exception { |
1022 |
|
CountDownLatch latch = new CountDownLatch(1); |
1023 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1024 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1025 |
< |
l.add(latchAwaitingStringTask(latch)); |
1026 |
< |
l.add(null); |
1027 |
< |
try { |
1028 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1029 |
< |
shouldThrow(); |
1030 |
< |
} catch (NullPointerException success) { |
1031 |
< |
} finally { |
1023 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1024 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1025 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1026 |
> |
l.add(latchAwaitingStringTask(latch)); |
1027 |
> |
l.add(null); |
1028 |
> |
try { |
1029 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1030 |
> |
shouldThrow(); |
1031 |
> |
} catch (NullPointerException success) {} |
1032 |
|
latch.countDown(); |
1083 |
– |
joinPool(e); |
1033 |
|
} |
1034 |
|
} |
1035 |
|
|
1037 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
1038 |
|
*/ |
1039 |
|
public void testTimedInvokeAny4() throws Exception { |
1040 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1041 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1042 |
< |
l.add(new NPETask()); |
1043 |
< |
try { |
1044 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1045 |
< |
shouldThrow(); |
1046 |
< |
} catch (ExecutionException success) { |
1047 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1048 |
< |
} finally { |
1049 |
< |
joinPool(e); |
1040 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1041 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1042 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1043 |
> |
l.add(new NPETask()); |
1044 |
> |
try { |
1045 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1046 |
> |
shouldThrow(); |
1047 |
> |
} catch (ExecutionException success) { |
1048 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1049 |
> |
} |
1050 |
|
} |
1051 |
|
} |
1052 |
|
|
1054 |
|
* timed invokeAny(c) returns result of some task |
1055 |
|
*/ |
1056 |
|
public void testTimedInvokeAny5() throws Exception { |
1057 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1058 |
< |
try { |
1057 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1058 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1059 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1060 |
|
l.add(new StringTask()); |
1061 |
|
l.add(new StringTask()); |
1062 |
|
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1063 |
|
assertSame(TEST_STRING, result); |
1115 |
– |
} finally { |
1116 |
– |
joinPool(e); |
1064 |
|
} |
1065 |
|
} |
1066 |
|
|
1068 |
|
* timed invokeAll(null) throws NPE |
1069 |
|
*/ |
1070 |
|
public void testTimedInvokeAll1() throws Exception { |
1071 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1072 |
< |
try { |
1073 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1074 |
< |
shouldThrow(); |
1075 |
< |
} catch (NullPointerException success) { |
1076 |
< |
} finally { |
1130 |
< |
joinPool(e); |
1071 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1072 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1073 |
> |
try { |
1074 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1075 |
> |
shouldThrow(); |
1076 |
> |
} catch (NullPointerException success) {} |
1077 |
|
} |
1078 |
|
} |
1079 |
|
|
1081 |
|
* timed invokeAll(,,null) throws NPE |
1082 |
|
*/ |
1083 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1084 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1085 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1086 |
< |
l.add(new StringTask()); |
1087 |
< |
try { |
1088 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1089 |
< |
shouldThrow(); |
1090 |
< |
} catch (NullPointerException success) { |
1091 |
< |
} finally { |
1146 |
< |
joinPool(e); |
1084 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1085 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1086 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1087 |
> |
l.add(new StringTask()); |
1088 |
> |
try { |
1089 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1090 |
> |
shouldThrow(); |
1091 |
> |
} catch (NullPointerException success) {} |
1092 |
|
} |
1093 |
|
} |
1094 |
|
|
1096 |
|
* timed invokeAll(empty collection) returns empty collection |
1097 |
|
*/ |
1098 |
|
public void testTimedInvokeAll2() throws Exception { |
1099 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1100 |
< |
try { |
1101 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1099 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1100 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1101 |
> |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), |
1102 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
1103 |
|
assertTrue(r.isEmpty()); |
1158 |
– |
} finally { |
1159 |
– |
joinPool(e); |
1104 |
|
} |
1105 |
|
} |
1106 |
|
|
1108 |
|
* timed invokeAll(c) throws NPE if c has null elements |
1109 |
|
*/ |
1110 |
|
public void testTimedInvokeAll3() throws Exception { |
1111 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1112 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1113 |
< |
l.add(new StringTask()); |
1114 |
< |
l.add(null); |
1115 |
< |
try { |
1116 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1117 |
< |
shouldThrow(); |
1118 |
< |
} catch (NullPointerException success) { |
1119 |
< |
} finally { |
1176 |
< |
joinPool(e); |
1111 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1112 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1113 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1114 |
> |
l.add(new StringTask()); |
1115 |
> |
l.add(null); |
1116 |
> |
try { |
1117 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1118 |
> |
shouldThrow(); |
1119 |
> |
} catch (NullPointerException success) {} |
1120 |
|
} |
1121 |
|
} |
1122 |
|
|
1124 |
|
* get of element of invokeAll(c) throws exception on failed task |
1125 |
|
*/ |
1126 |
|
public void testTimedInvokeAll4() throws Exception { |
1127 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1128 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1129 |
< |
l.add(new NPETask()); |
1130 |
< |
List<Future<String>> futures = |
1131 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1132 |
< |
assertEquals(1, futures.size()); |
1133 |
< |
try { |
1134 |
< |
futures.get(0).get(); |
1135 |
< |
shouldThrow(); |
1136 |
< |
} catch (ExecutionException success) { |
1137 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1138 |
< |
} finally { |
1139 |
< |
joinPool(e); |
1127 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1128 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1129 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1130 |
> |
l.add(new NPETask()); |
1131 |
> |
List<Future<String>> futures = |
1132 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1133 |
> |
assertEquals(1, futures.size()); |
1134 |
> |
try { |
1135 |
> |
futures.get(0).get(); |
1136 |
> |
shouldThrow(); |
1137 |
> |
} catch (ExecutionException success) { |
1138 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1139 |
> |
} |
1140 |
|
} |
1141 |
|
} |
1142 |
|
|
1144 |
|
* timed invokeAll(c) returns results of all completed tasks |
1145 |
|
*/ |
1146 |
|
public void testTimedInvokeAll5() throws Exception { |
1147 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1148 |
< |
try { |
1147 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1148 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1149 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1150 |
|
l.add(new StringTask()); |
1151 |
|
l.add(new StringTask()); |
1154 |
|
assertEquals(2, futures.size()); |
1155 |
|
for (Future<String> future : futures) |
1156 |
|
assertSame(TEST_STRING, future.get()); |
1214 |
– |
} finally { |
1215 |
– |
joinPool(e); |
1157 |
|
} |
1158 |
|
} |
1159 |
|
|
1161 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1162 |
|
*/ |
1163 |
|
public void testTimedInvokeAll6() throws Exception { |
1164 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1165 |
< |
try { |
1164 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1165 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1166 |
|
for (long timeout = timeoutMillis();;) { |
1167 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1168 |
|
tasks.add(new StringTask("0")); |
1186 |
|
fail("expected exactly one task to be cancelled"); |
1187 |
|
} |
1188 |
|
} |
1248 |
– |
} finally { |
1249 |
– |
joinPool(e); |
1189 |
|
} |
1190 |
|
} |
1191 |
|
|