4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
– |
import junit.framework.*; |
8 |
– |
import java.util.*; |
9 |
– |
import java.util.concurrent.*; |
7 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
+ |
import static java.util.concurrent.TimeUnit.SECONDS; |
9 |
+ |
|
10 |
+ |
import java.util.ArrayList; |
11 |
+ |
import java.util.HashSet; |
12 |
+ |
import java.util.List; |
13 |
+ |
import java.util.concurrent.BlockingQueue; |
14 |
+ |
import java.util.concurrent.Callable; |
15 |
+ |
import java.util.concurrent.CancellationException; |
16 |
+ |
import java.util.concurrent.CountDownLatch; |
17 |
+ |
import java.util.concurrent.Delayed; |
18 |
+ |
import java.util.concurrent.ExecutionException; |
19 |
+ |
import java.util.concurrent.Executors; |
20 |
+ |
import java.util.concurrent.ExecutorService; |
21 |
+ |
import java.util.concurrent.Future; |
22 |
+ |
import java.util.concurrent.RejectedExecutionException; |
23 |
+ |
import java.util.concurrent.RejectedExecutionHandler; |
24 |
+ |
import java.util.concurrent.RunnableScheduledFuture; |
25 |
+ |
import java.util.concurrent.ScheduledFuture; |
26 |
+ |
import java.util.concurrent.ScheduledThreadPoolExecutor; |
27 |
+ |
import java.util.concurrent.ThreadFactory; |
28 |
+ |
import java.util.concurrent.ThreadPoolExecutor; |
29 |
+ |
import java.util.concurrent.TimeoutException; |
30 |
+ |
import java.util.concurrent.TimeUnit; |
31 |
|
import java.util.concurrent.atomic.AtomicInteger; |
32 |
|
|
33 |
+ |
import junit.framework.Test; |
34 |
+ |
import junit.framework.TestSuite; |
35 |
+ |
|
36 |
|
public class ScheduledExecutorSubclassTest extends JSR166TestCase { |
37 |
|
public static void main(String[] args) { |
38 |
< |
junit.textui.TestRunner.run(suite()); |
38 |
> |
main(suite(), args); |
39 |
|
} |
40 |
|
public static Test suite() { |
41 |
|
return new TestSuite(ScheduledExecutorSubclassTest.class); |
99 |
|
* execute successfully executes a runnable |
100 |
|
*/ |
101 |
|
public void testExecute() throws InterruptedException { |
102 |
< |
CustomExecutor p = new CustomExecutor(1); |
103 |
< |
final CountDownLatch done = new CountDownLatch(1); |
104 |
< |
final Runnable task = new CheckedRunnable() { |
105 |
< |
public void realRun() { |
106 |
< |
done.countDown(); |
84 |
< |
}}; |
85 |
< |
try { |
102 |
> |
final CustomExecutor p = new CustomExecutor(1); |
103 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
104 |
> |
final CountDownLatch done = new CountDownLatch(1); |
105 |
> |
final Runnable task = new CheckedRunnable() { |
106 |
> |
public void realRun() { done.countDown(); }}; |
107 |
|
p.execute(task); |
108 |
< |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
88 |
< |
} finally { |
89 |
< |
joinPool(p); |
108 |
> |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
109 |
|
} |
110 |
|
} |
111 |
|
|
113 |
|
* delayed schedule of callable successfully executes after delay |
114 |
|
*/ |
115 |
|
public void testSchedule1() throws Exception { |
116 |
< |
CustomExecutor p = new CustomExecutor(1); |
117 |
< |
final long startTime = System.nanoTime(); |
118 |
< |
final CountDownLatch done = new CountDownLatch(1); |
119 |
< |
try { |
116 |
> |
final CustomExecutor p = new CustomExecutor(1); |
117 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
118 |
> |
final long startTime = System.nanoTime(); |
119 |
> |
final CountDownLatch done = new CountDownLatch(1); |
120 |
|
Callable task = new CheckedCallable<Boolean>() { |
121 |
|
public Boolean realCall() { |
122 |
|
done.countDown(); |
127 |
|
assertSame(Boolean.TRUE, f.get()); |
128 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
129 |
|
assertTrue(done.await(0L, MILLISECONDS)); |
111 |
– |
} finally { |
112 |
– |
joinPool(p); |
130 |
|
} |
131 |
|
} |
132 |
|
|
134 |
|
* delayed schedule of runnable successfully executes after delay |
135 |
|
*/ |
136 |
|
public void testSchedule3() throws Exception { |
137 |
< |
CustomExecutor p = new CustomExecutor(1); |
138 |
< |
final long startTime = System.nanoTime(); |
139 |
< |
final CountDownLatch done = new CountDownLatch(1); |
140 |
< |
try { |
137 |
> |
final CustomExecutor p = new CustomExecutor(1); |
138 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
139 |
> |
final long startTime = System.nanoTime(); |
140 |
> |
final CountDownLatch done = new CountDownLatch(1); |
141 |
|
Runnable task = new CheckedRunnable() { |
142 |
|
public void realRun() { |
143 |
|
done.countDown(); |
147 |
|
await(done); |
148 |
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
149 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
133 |
– |
} finally { |
134 |
– |
joinPool(p); |
150 |
|
} |
151 |
|
} |
152 |
|
|
154 |
|
* scheduleAtFixedRate executes runnable after given initial delay |
155 |
|
*/ |
156 |
|
public void testSchedule4() throws InterruptedException { |
157 |
< |
CustomExecutor p = new CustomExecutor(1); |
158 |
< |
final long startTime = System.nanoTime(); |
159 |
< |
final CountDownLatch done = new CountDownLatch(1); |
160 |
< |
try { |
157 |
> |
final CustomExecutor p = new CustomExecutor(1); |
158 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
159 |
> |
final long startTime = System.nanoTime(); |
160 |
> |
final CountDownLatch done = new CountDownLatch(1); |
161 |
|
Runnable task = new CheckedRunnable() { |
162 |
|
public void realRun() { |
163 |
|
done.countDown(); |
169 |
|
await(done); |
170 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
171 |
|
f.cancel(true); |
157 |
– |
} finally { |
158 |
– |
joinPool(p); |
172 |
|
} |
173 |
|
} |
174 |
|
|
176 |
|
* scheduleWithFixedDelay executes runnable after given initial delay |
177 |
|
*/ |
178 |
|
public void testSchedule5() throws InterruptedException { |
179 |
< |
CustomExecutor p = new CustomExecutor(1); |
180 |
< |
final long startTime = System.nanoTime(); |
181 |
< |
final CountDownLatch done = new CountDownLatch(1); |
182 |
< |
try { |
179 |
> |
final CustomExecutor p = new CustomExecutor(1); |
180 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
181 |
> |
final long startTime = System.nanoTime(); |
182 |
> |
final CountDownLatch done = new CountDownLatch(1); |
183 |
|
Runnable task = new CheckedRunnable() { |
184 |
|
public void realRun() { |
185 |
|
done.countDown(); |
191 |
|
await(done); |
192 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
193 |
|
f.cancel(true); |
181 |
– |
} finally { |
182 |
– |
joinPool(p); |
194 |
|
} |
195 |
|
} |
196 |
|
|
203 |
|
* scheduleAtFixedRate executes series of tasks at given rate |
204 |
|
*/ |
205 |
|
public void testFixedRateSequence() throws InterruptedException { |
206 |
< |
CustomExecutor p = new CustomExecutor(1); |
207 |
< |
try { |
206 |
> |
final CustomExecutor p = new CustomExecutor(1); |
207 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
208 |
|
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
209 |
|
long startTime = System.nanoTime(); |
210 |
|
int cycles = 10; |
211 |
|
final CountDownLatch done = new CountDownLatch(cycles); |
212 |
< |
CheckedRunnable task = new CheckedRunnable() { |
212 |
> |
Runnable task = new CheckedRunnable() { |
213 |
|
public void realRun() { done.countDown(); }}; |
214 |
|
ScheduledFuture h = |
215 |
|
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
222 |
|
return; |
223 |
|
} |
224 |
|
throw new AssertionError("unexpected execution rate"); |
214 |
– |
} finally { |
215 |
– |
joinPool(p); |
225 |
|
} |
226 |
|
} |
227 |
|
|
229 |
|
* scheduleWithFixedDelay executes series of tasks with given period |
230 |
|
*/ |
231 |
|
public void testFixedDelaySequence() throws InterruptedException { |
232 |
< |
CustomExecutor p = new CustomExecutor(1); |
233 |
< |
try { |
232 |
> |
final CustomExecutor p = new CustomExecutor(1); |
233 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
234 |
|
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
235 |
|
long startTime = System.nanoTime(); |
236 |
|
int cycles = 10; |
237 |
|
final CountDownLatch done = new CountDownLatch(cycles); |
238 |
< |
CheckedRunnable task = new CheckedRunnable() { |
238 |
> |
Runnable task = new CheckedRunnable() { |
239 |
|
public void realRun() { done.countDown(); }}; |
240 |
|
ScheduledFuture h = |
241 |
|
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
248 |
|
return; |
249 |
|
} |
250 |
|
throw new AssertionError("unexpected execution rate"); |
242 |
– |
} finally { |
243 |
– |
joinPool(p); |
251 |
|
} |
252 |
|
} |
253 |
|
|
255 |
|
* execute(null) throws NPE |
256 |
|
*/ |
257 |
|
public void testExecuteNull() throws InterruptedException { |
258 |
< |
CustomExecutor se = new CustomExecutor(1); |
259 |
< |
try { |
260 |
< |
se.execute(null); |
261 |
< |
shouldThrow(); |
262 |
< |
} catch (NullPointerException success) {} |
263 |
< |
joinPool(se); |
258 |
> |
final CustomExecutor p = new CustomExecutor(1); |
259 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
260 |
> |
try { |
261 |
> |
p.execute(null); |
262 |
> |
shouldThrow(); |
263 |
> |
} catch (NullPointerException success) {} |
264 |
> |
} |
265 |
|
} |
266 |
|
|
267 |
|
/** |
268 |
|
* schedule(null) throws NPE |
269 |
|
*/ |
270 |
|
public void testScheduleNull() throws InterruptedException { |
271 |
< |
CustomExecutor se = new CustomExecutor(1); |
272 |
< |
try { |
273 |
< |
TrackedCallable callable = null; |
274 |
< |
Future f = se.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
275 |
< |
shouldThrow(); |
276 |
< |
} catch (NullPointerException success) {} |
277 |
< |
joinPool(se); |
271 |
> |
final CustomExecutor p = new CustomExecutor(1); |
272 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
273 |
> |
try { |
274 |
> |
TrackedCallable callable = null; |
275 |
> |
Future f = p.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
276 |
> |
shouldThrow(); |
277 |
> |
} catch (NullPointerException success) {} |
278 |
> |
} |
279 |
|
} |
280 |
|
|
281 |
|
/** |
282 |
|
* execute throws RejectedExecutionException if shutdown |
283 |
|
*/ |
284 |
|
public void testSchedule1_RejectedExecutionException() { |
285 |
< |
CustomExecutor se = new CustomExecutor(1); |
286 |
< |
try { |
287 |
< |
se.shutdown(); |
288 |
< |
se.schedule(new NoOpRunnable(), |
289 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
290 |
< |
shouldThrow(); |
291 |
< |
} catch (RejectedExecutionException success) { |
292 |
< |
} catch (SecurityException ok) { |
285 |
> |
final CustomExecutor p = new CustomExecutor(1); |
286 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
287 |
> |
try { |
288 |
> |
p.shutdown(); |
289 |
> |
p.schedule(new NoOpRunnable(), |
290 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
291 |
> |
shouldThrow(); |
292 |
> |
} catch (RejectedExecutionException success) { |
293 |
> |
} catch (SecurityException ok) {} |
294 |
|
} |
285 |
– |
|
286 |
– |
joinPool(se); |
295 |
|
} |
296 |
|
|
297 |
|
/** |
298 |
|
* schedule throws RejectedExecutionException if shutdown |
299 |
|
*/ |
300 |
|
public void testSchedule2_RejectedExecutionException() { |
301 |
< |
CustomExecutor se = new CustomExecutor(1); |
302 |
< |
try { |
303 |
< |
se.shutdown(); |
304 |
< |
se.schedule(new NoOpCallable(), |
305 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
306 |
< |
shouldThrow(); |
307 |
< |
} catch (RejectedExecutionException success) { |
308 |
< |
} catch (SecurityException ok) { |
301 |
> |
final CustomExecutor p = new CustomExecutor(1); |
302 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
303 |
> |
try { |
304 |
> |
p.shutdown(); |
305 |
> |
p.schedule(new NoOpCallable(), |
306 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
307 |
> |
shouldThrow(); |
308 |
> |
} catch (RejectedExecutionException success) { |
309 |
> |
} catch (SecurityException ok) {} |
310 |
|
} |
302 |
– |
joinPool(se); |
311 |
|
} |
312 |
|
|
313 |
|
/** |
314 |
|
* schedule callable throws RejectedExecutionException if shutdown |
315 |
|
*/ |
316 |
|
public void testSchedule3_RejectedExecutionException() { |
317 |
< |
CustomExecutor se = new CustomExecutor(1); |
318 |
< |
try { |
319 |
< |
se.shutdown(); |
320 |
< |
se.schedule(new NoOpCallable(), |
321 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
322 |
< |
shouldThrow(); |
323 |
< |
} catch (RejectedExecutionException success) { |
324 |
< |
} catch (SecurityException ok) { |
317 |
> |
final CustomExecutor p = new CustomExecutor(1); |
318 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
319 |
> |
try { |
320 |
> |
p.shutdown(); |
321 |
> |
p.schedule(new NoOpCallable(), |
322 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
323 |
> |
shouldThrow(); |
324 |
> |
} catch (RejectedExecutionException success) { |
325 |
> |
} catch (SecurityException ok) {} |
326 |
|
} |
318 |
– |
joinPool(se); |
327 |
|
} |
328 |
|
|
329 |
|
/** |
330 |
|
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
331 |
|
*/ |
332 |
|
public void testScheduleAtFixedRate1_RejectedExecutionException() { |
333 |
< |
CustomExecutor se = new CustomExecutor(1); |
334 |
< |
try { |
335 |
< |
se.shutdown(); |
336 |
< |
se.scheduleAtFixedRate(new NoOpRunnable(), |
337 |
< |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
338 |
< |
shouldThrow(); |
339 |
< |
} catch (RejectedExecutionException success) { |
340 |
< |
} catch (SecurityException ok) { |
333 |
> |
final CustomExecutor p = new CustomExecutor(1); |
334 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
335 |
> |
try { |
336 |
> |
p.shutdown(); |
337 |
> |
p.scheduleAtFixedRate(new NoOpRunnable(), |
338 |
> |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
339 |
> |
shouldThrow(); |
340 |
> |
} catch (RejectedExecutionException success) { |
341 |
> |
} catch (SecurityException ok) {} |
342 |
|
} |
334 |
– |
joinPool(se); |
343 |
|
} |
344 |
|
|
345 |
|
/** |
346 |
|
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown |
347 |
|
*/ |
348 |
|
public void testScheduleWithFixedDelay1_RejectedExecutionException() { |
349 |
< |
CustomExecutor se = new CustomExecutor(1); |
350 |
< |
try { |
351 |
< |
se.shutdown(); |
352 |
< |
se.scheduleWithFixedDelay(new NoOpRunnable(), |
353 |
< |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
354 |
< |
shouldThrow(); |
355 |
< |
} catch (RejectedExecutionException success) { |
356 |
< |
} catch (SecurityException ok) { |
349 |
> |
final CustomExecutor p = new CustomExecutor(1); |
350 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
351 |
> |
try { |
352 |
> |
p.shutdown(); |
353 |
> |
p.scheduleWithFixedDelay(new NoOpRunnable(), |
354 |
> |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
355 |
> |
shouldThrow(); |
356 |
> |
} catch (RejectedExecutionException success) { |
357 |
> |
} catch (SecurityException ok) {} |
358 |
|
} |
350 |
– |
joinPool(se); |
359 |
|
} |
360 |
|
|
361 |
|
/** |
364 |
|
*/ |
365 |
|
public void testGetActiveCount() throws InterruptedException { |
366 |
|
final ThreadPoolExecutor p = new CustomExecutor(2); |
367 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
368 |
< |
final CountDownLatch done = new CountDownLatch(1); |
369 |
< |
try { |
367 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
368 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
369 |
> |
final CountDownLatch done = new CountDownLatch(1); |
370 |
|
assertEquals(0, p.getActiveCount()); |
371 |
|
p.execute(new CheckedRunnable() { |
372 |
|
public void realRun() throws InterruptedException { |
374 |
|
assertEquals(1, p.getActiveCount()); |
375 |
|
done.await(); |
376 |
|
}}); |
377 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
377 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
378 |
|
assertEquals(1, p.getActiveCount()); |
371 |
– |
} finally { |
379 |
|
done.countDown(); |
373 |
– |
joinPool(p); |
380 |
|
} |
381 |
|
} |
382 |
|
|
386 |
|
*/ |
387 |
|
public void testGetCompletedTaskCount() throws InterruptedException { |
388 |
|
final ThreadPoolExecutor p = new CustomExecutor(2); |
389 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
390 |
< |
final CountDownLatch threadProceed = new CountDownLatch(1); |
391 |
< |
final CountDownLatch threadDone = new CountDownLatch(1); |
392 |
< |
try { |
389 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
390 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
391 |
> |
final CountDownLatch threadProceed = new CountDownLatch(1); |
392 |
> |
final CountDownLatch threadDone = new CountDownLatch(1); |
393 |
|
assertEquals(0, p.getCompletedTaskCount()); |
394 |
|
p.execute(new CheckedRunnable() { |
395 |
|
public void realRun() throws InterruptedException { |
408 |
|
fail("timed out"); |
409 |
|
Thread.yield(); |
410 |
|
} |
405 |
– |
} finally { |
406 |
– |
joinPool(p); |
411 |
|
} |
412 |
|
} |
413 |
|
|
415 |
|
* getCorePoolSize returns size given in constructor if not otherwise set |
416 |
|
*/ |
417 |
|
public void testGetCorePoolSize() { |
418 |
< |
CustomExecutor p = new CustomExecutor(1); |
419 |
< |
assertEquals(1, p.getCorePoolSize()); |
420 |
< |
joinPool(p); |
418 |
> |
final CustomExecutor p = new CustomExecutor(1); |
419 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
420 |
> |
assertEquals(1, p.getCorePoolSize()); |
421 |
> |
} |
422 |
|
} |
423 |
|
|
424 |
|
/** |
428 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
429 |
|
final int THREADS = 3; |
430 |
|
final ThreadPoolExecutor p = new CustomExecutor(THREADS); |
431 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
432 |
< |
final CountDownLatch done = new CountDownLatch(1); |
433 |
< |
try { |
431 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
432 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
433 |
> |
final CountDownLatch done = new CountDownLatch(1); |
434 |
|
assertEquals(0, p.getLargestPoolSize()); |
435 |
|
for (int i = 0; i < THREADS; i++) |
436 |
|
p.execute(new CheckedRunnable() { |
439 |
|
done.await(); |
440 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
441 |
|
}}); |
442 |
< |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
442 |
> |
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
443 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
439 |
– |
} finally { |
444 |
|
done.countDown(); |
441 |
– |
joinPool(p); |
442 |
– |
assertEquals(THREADS, p.getLargestPoolSize()); |
445 |
|
} |
446 |
+ |
assertEquals(THREADS, p.getLargestPoolSize()); |
447 |
|
} |
448 |
|
|
449 |
|
/** |
452 |
|
*/ |
453 |
|
public void testGetPoolSize() throws InterruptedException { |
454 |
|
final ThreadPoolExecutor p = new CustomExecutor(1); |
455 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
456 |
< |
final CountDownLatch done = new CountDownLatch(1); |
457 |
< |
try { |
455 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
456 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
457 |
> |
final CountDownLatch done = new CountDownLatch(1); |
458 |
|
assertEquals(0, p.getPoolSize()); |
459 |
|
p.execute(new CheckedRunnable() { |
460 |
|
public void realRun() throws InterruptedException { |
462 |
|
assertEquals(1, p.getPoolSize()); |
463 |
|
done.await(); |
464 |
|
}}); |
465 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
465 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
466 |
|
assertEquals(1, p.getPoolSize()); |
464 |
– |
} finally { |
467 |
|
done.countDown(); |
466 |
– |
joinPool(p); |
468 |
|
} |
469 |
|
} |
470 |
|
|
473 |
|
* submitted |
474 |
|
*/ |
475 |
|
public void testGetTaskCount() throws InterruptedException { |
476 |
< |
final ThreadPoolExecutor p = new CustomExecutor(1); |
476 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
476 |
> |
final int TASKS = 3; |
477 |
|
final CountDownLatch done = new CountDownLatch(1); |
478 |
< |
final int TASKS = 5; |
479 |
< |
try { |
478 |
> |
final ThreadPoolExecutor p = new CustomExecutor(1); |
479 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
480 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
481 |
|
assertEquals(0, p.getTaskCount()); |
482 |
< |
for (int i = 0; i < TASKS; i++) |
482 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
483 |
> |
p.execute(new CheckedRunnable() { |
484 |
> |
public void realRun() throws InterruptedException { |
485 |
> |
threadStarted.countDown(); |
486 |
> |
done.await(); |
487 |
> |
}}); |
488 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
489 |
> |
assertEquals(1, p.getTaskCount()); |
490 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
491 |
> |
for (int i = 0; i < TASKS; i++) { |
492 |
> |
assertEquals(1 + i, p.getTaskCount()); |
493 |
|
p.execute(new CheckedRunnable() { |
494 |
|
public void realRun() throws InterruptedException { |
495 |
|
threadStarted.countDown(); |
496 |
+ |
assertEquals(1 + TASKS, p.getTaskCount()); |
497 |
|
done.await(); |
498 |
|
}}); |
499 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
500 |
< |
assertEquals(TASKS, p.getTaskCount()); |
501 |
< |
} finally { |
490 |
< |
done.countDown(); |
491 |
< |
joinPool(p); |
499 |
> |
} |
500 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
501 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
502 |
|
} |
503 |
+ |
assertEquals(1 + TASKS, p.getTaskCount()); |
504 |
+ |
assertEquals(1 + TASKS, p.getCompletedTaskCount()); |
505 |
|
} |
506 |
|
|
507 |
|
/** |
508 |
|
* getThreadFactory returns factory in constructor if not set |
509 |
|
*/ |
510 |
|
public void testGetThreadFactory() { |
511 |
< |
ThreadFactory tf = new SimpleThreadFactory(); |
512 |
< |
CustomExecutor p = new CustomExecutor(1, tf); |
513 |
< |
assertSame(tf, p.getThreadFactory()); |
514 |
< |
joinPool(p); |
511 |
> |
final ThreadFactory threadFactory = new SimpleThreadFactory(); |
512 |
> |
final CustomExecutor p = new CustomExecutor(1, threadFactory); |
513 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
514 |
> |
assertSame(threadFactory, p.getThreadFactory()); |
515 |
> |
} |
516 |
|
} |
517 |
|
|
518 |
|
/** |
519 |
|
* setThreadFactory sets the thread factory returned by getThreadFactory |
520 |
|
*/ |
521 |
|
public void testSetThreadFactory() { |
522 |
< |
ThreadFactory tf = new SimpleThreadFactory(); |
523 |
< |
CustomExecutor p = new CustomExecutor(1); |
524 |
< |
p.setThreadFactory(tf); |
525 |
< |
assertSame(tf, p.getThreadFactory()); |
526 |
< |
joinPool(p); |
522 |
> |
final ThreadFactory threadFactory = new SimpleThreadFactory(); |
523 |
> |
final CustomExecutor p = new CustomExecutor(1); |
524 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
525 |
> |
p.setThreadFactory(threadFactory); |
526 |
> |
assertSame(threadFactory, p.getThreadFactory()); |
527 |
> |
} |
528 |
|
} |
529 |
|
|
530 |
|
/** |
531 |
|
* setThreadFactory(null) throws NPE |
532 |
|
*/ |
533 |
|
public void testSetThreadFactoryNull() { |
534 |
< |
CustomExecutor p = new CustomExecutor(1); |
535 |
< |
try { |
536 |
< |
p.setThreadFactory(null); |
537 |
< |
shouldThrow(); |
538 |
< |
} catch (NullPointerException success) { |
539 |
< |
} finally { |
526 |
< |
joinPool(p); |
534 |
> |
final CustomExecutor p = new CustomExecutor(1); |
535 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
536 |
> |
try { |
537 |
> |
p.setThreadFactory(null); |
538 |
> |
shouldThrow(); |
539 |
> |
} catch (NullPointerException success) {} |
540 |
|
} |
541 |
|
} |
542 |
|
|
544 |
|
* isShutdown is false before shutdown, true after |
545 |
|
*/ |
546 |
|
public void testIsShutdown() { |
547 |
< |
CustomExecutor p = new CustomExecutor(1); |
548 |
< |
try { |
547 |
> |
final CustomExecutor p = new CustomExecutor(1); |
548 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
549 |
|
assertFalse(p.isShutdown()); |
537 |
– |
} |
538 |
– |
finally { |
550 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
551 |
+ |
assertTrue(p.isShutdown()); |
552 |
|
} |
541 |
– |
assertTrue(p.isShutdown()); |
553 |
|
} |
554 |
|
|
555 |
|
/** |
557 |
|
*/ |
558 |
|
public void testIsTerminated() throws InterruptedException { |
559 |
|
final ThreadPoolExecutor p = new CustomExecutor(1); |
560 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
561 |
< |
final CountDownLatch done = new CountDownLatch(1); |
562 |
< |
assertFalse(p.isTerminated()); |
563 |
< |
try { |
560 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
561 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
562 |
> |
final CountDownLatch done = new CountDownLatch(1); |
563 |
> |
assertFalse(p.isTerminated()); |
564 |
|
p.execute(new CheckedRunnable() { |
565 |
|
public void realRun() throws InterruptedException { |
566 |
|
assertFalse(p.isTerminated()); |
567 |
|
threadStarted.countDown(); |
568 |
|
done.await(); |
569 |
|
}}); |
570 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
570 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
571 |
|
assertFalse(p.isTerminating()); |
572 |
|
done.countDown(); |
562 |
– |
} finally { |
573 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
574 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
575 |
+ |
assertTrue(p.isTerminated()); |
576 |
|
} |
565 |
– |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
566 |
– |
assertTrue(p.isTerminated()); |
577 |
|
} |
578 |
|
|
579 |
|
/** |
581 |
|
*/ |
582 |
|
public void testIsTerminating() throws InterruptedException { |
583 |
|
final ThreadPoolExecutor p = new CustomExecutor(1); |
584 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
585 |
< |
final CountDownLatch done = new CountDownLatch(1); |
586 |
< |
try { |
584 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
585 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
586 |
> |
final CountDownLatch done = new CountDownLatch(1); |
587 |
|
assertFalse(p.isTerminating()); |
588 |
|
p.execute(new CheckedRunnable() { |
589 |
|
public void realRun() throws InterruptedException { |
591 |
|
threadStarted.countDown(); |
592 |
|
done.await(); |
593 |
|
}}); |
594 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
594 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
595 |
|
assertFalse(p.isTerminating()); |
596 |
|
done.countDown(); |
587 |
– |
} finally { |
597 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
598 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
599 |
+ |
assertTrue(p.isTerminated()); |
600 |
+ |
assertFalse(p.isTerminating()); |
601 |
|
} |
590 |
– |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
591 |
– |
assertTrue(p.isTerminated()); |
592 |
– |
assertFalse(p.isTerminating()); |
602 |
|
} |
603 |
|
|
604 |
|
/** |
605 |
|
* getQueue returns the work queue, which contains queued tasks |
606 |
|
*/ |
607 |
|
public void testGetQueue() throws InterruptedException { |
608 |
< |
ScheduledThreadPoolExecutor p = new CustomExecutor(1); |
609 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
610 |
< |
final CountDownLatch done = new CountDownLatch(1); |
611 |
< |
try { |
608 |
> |
final ScheduledThreadPoolExecutor p = new CustomExecutor(1); |
609 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
610 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
611 |
> |
final CountDownLatch done = new CountDownLatch(1); |
612 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
613 |
|
for (int i = 0; i < tasks.length; i++) { |
614 |
|
Runnable r = new CheckedRunnable() { |
618 |
|
}}; |
619 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
620 |
|
} |
621 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
621 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
622 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
623 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
624 |
|
assertFalse(q.contains(tasks[0])); |
616 |
– |
} finally { |
625 |
|
done.countDown(); |
618 |
– |
joinPool(p); |
626 |
|
} |
627 |
|
} |
628 |
|
|
631 |
|
*/ |
632 |
|
public void testRemove() throws InterruptedException { |
633 |
|
final ScheduledThreadPoolExecutor p = new CustomExecutor(1); |
634 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
635 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
636 |
< |
final CountDownLatch done = new CountDownLatch(1); |
637 |
< |
try { |
634 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
635 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
636 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
637 |
> |
final CountDownLatch done = new CountDownLatch(1); |
638 |
|
for (int i = 0; i < tasks.length; i++) { |
639 |
|
Runnable r = new CheckedRunnable() { |
640 |
|
public void realRun() throws InterruptedException { |
643 |
|
}}; |
644 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
645 |
|
} |
646 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
646 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
647 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
648 |
|
assertFalse(p.remove((Runnable)tasks[0])); |
649 |
|
assertTrue(q.contains((Runnable)tasks[4])); |
654 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
655 |
|
assertTrue(p.remove((Runnable)tasks[3])); |
656 |
|
assertFalse(q.contains((Runnable)tasks[3])); |
650 |
– |
} finally { |
657 |
|
done.countDown(); |
652 |
– |
joinPool(p); |
658 |
|
} |
659 |
|
} |
660 |
|
|
662 |
|
* purge removes cancelled tasks from the queue |
663 |
|
*/ |
664 |
|
public void testPurge() throws InterruptedException { |
665 |
< |
CustomExecutor p = new CustomExecutor(1); |
666 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
667 |
< |
for (int i = 0; i < tasks.length; i++) |
668 |
< |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
669 |
< |
LONG_DELAY_MS, MILLISECONDS); |
670 |
< |
try { |
665 |
> |
final ScheduledFuture[] tasks = new ScheduledFuture[5]; |
666 |
> |
final Runnable releaser = new Runnable() { public void run() { |
667 |
> |
for (ScheduledFuture task : tasks) |
668 |
> |
if (task != null) task.cancel(true); }}; |
669 |
> |
final CustomExecutor p = new CustomExecutor(1); |
670 |
> |
try (PoolCleaner cleaner = cleaner(p, releaser)) { |
671 |
> |
for (int i = 0; i < tasks.length; i++) |
672 |
> |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
673 |
> |
LONG_DELAY_MS, MILLISECONDS); |
674 |
|
int max = tasks.length; |
675 |
|
if (tasks[4].cancel(true)) --max; |
676 |
|
if (tasks[3].cancel(true)) --max; |
682 |
|
long count = p.getTaskCount(); |
683 |
|
if (count == max) |
684 |
|
return; |
685 |
< |
} while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
685 |
> |
} while (millisElapsedSince(startTime) < LONG_DELAY_MS); |
686 |
|
fail("Purge failed to remove cancelled tasks"); |
679 |
– |
} finally { |
680 |
– |
for (ScheduledFuture task : tasks) |
681 |
– |
task.cancel(true); |
682 |
– |
joinPool(p); |
687 |
|
} |
688 |
|
} |
689 |
|
|
690 |
|
/** |
691 |
< |
* shutdownNow returns a list containing tasks that were not run |
691 |
> |
* shutdownNow returns a list containing tasks that were not run, |
692 |
> |
* and those tasks are drained from the queue |
693 |
|
*/ |
694 |
< |
public void testShutdownNow() { |
695 |
< |
CustomExecutor p = new CustomExecutor(1); |
696 |
< |
for (int i = 0; i < 5; i++) |
697 |
< |
p.schedule(new SmallPossiblyInterruptedRunnable(), |
698 |
< |
LONG_DELAY_MS, MILLISECONDS); |
694 |
> |
public void testShutdownNow() throws InterruptedException { |
695 |
> |
final int poolSize = 2; |
696 |
> |
final int count = 5; |
697 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
698 |
> |
final CustomExecutor p = new CustomExecutor(poolSize); |
699 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
700 |
> |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
701 |
> |
threadsStarted.countDown(); |
702 |
> |
try { |
703 |
> |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
704 |
> |
} catch (InterruptedException success) {} |
705 |
> |
ran.getAndIncrement(); |
706 |
> |
}}; |
707 |
> |
for (int i = 0; i < count; i++) |
708 |
> |
p.execute(waiter); |
709 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
710 |
> |
assertEquals(poolSize, p.getActiveCount()); |
711 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
712 |
> |
final List<Runnable> queuedTasks; |
713 |
|
try { |
714 |
< |
List<Runnable> l = p.shutdownNow(); |
696 |
< |
assertTrue(p.isShutdown()); |
697 |
< |
assertEquals(5, l.size()); |
714 |
> |
queuedTasks = p.shutdownNow(); |
715 |
|
} catch (SecurityException ok) { |
716 |
< |
// Allowed in case test doesn't have privs |
700 |
< |
} finally { |
701 |
< |
joinPool(p); |
716 |
> |
return; // Allowed in case test doesn't have privs |
717 |
|
} |
718 |
+ |
assertTrue(p.isShutdown()); |
719 |
+ |
assertTrue(p.getQueue().isEmpty()); |
720 |
+ |
assertEquals(count - poolSize, queuedTasks.size()); |
721 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
722 |
+ |
assertTrue(p.isTerminated()); |
723 |
+ |
assertEquals(poolSize, ran.get()); |
724 |
+ |
assertEquals(poolSize, p.getCompletedTaskCount()); |
725 |
|
} |
726 |
|
|
727 |
|
/** |
728 |
< |
* In default setting, shutdown cancels periodic but not delayed |
729 |
< |
* tasks at shutdown |
728 |
> |
* shutdownNow returns a list containing tasks that were not run, |
729 |
> |
* and those tasks are drained from the queue |
730 |
|
*/ |
731 |
< |
public void testShutdown1() throws InterruptedException { |
732 |
< |
CustomExecutor p = new CustomExecutor(1); |
733 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
734 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
735 |
< |
|
736 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
737 |
< |
for (int i = 0; i < tasks.length; i++) |
738 |
< |
tasks[i] = p.schedule(new NoOpRunnable(), |
739 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
740 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
741 |
< |
BlockingQueue<Runnable> q = p.getQueue(); |
742 |
< |
for (ScheduledFuture task : tasks) { |
743 |
< |
assertFalse(task.isDone()); |
744 |
< |
assertFalse(task.isCancelled()); |
745 |
< |
assertTrue(q.contains(task)); |
731 |
> |
public void testShutdownNow_delayedTasks() throws InterruptedException { |
732 |
> |
final CustomExecutor p = new CustomExecutor(1); |
733 |
> |
List<ScheduledFuture> tasks = new ArrayList<>(); |
734 |
> |
for (int i = 0; i < 3; i++) { |
735 |
> |
Runnable r = new NoOpRunnable(); |
736 |
> |
tasks.add(p.schedule(r, 9, SECONDS)); |
737 |
> |
tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS)); |
738 |
> |
tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS)); |
739 |
> |
} |
740 |
> |
if (testImplementationDetails) |
741 |
> |
assertEquals(new HashSet(tasks), new HashSet(p.getQueue())); |
742 |
> |
final List<Runnable> queuedTasks; |
743 |
> |
try { |
744 |
> |
queuedTasks = p.shutdownNow(); |
745 |
> |
} catch (SecurityException ok) { |
746 |
> |
return; // Allowed in case test doesn't have privs |
747 |
|
} |
748 |
|
assertTrue(p.isShutdown()); |
749 |
< |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
750 |
< |
assertTrue(p.isTerminated()); |
749 |
> |
assertTrue(p.getQueue().isEmpty()); |
750 |
> |
if (testImplementationDetails) |
751 |
> |
assertEquals(new HashSet(tasks), new HashSet(queuedTasks)); |
752 |
> |
assertEquals(tasks.size(), queuedTasks.size()); |
753 |
|
for (ScheduledFuture task : tasks) { |
754 |
< |
assertTrue(task.isDone()); |
754 |
> |
assertFalse(((CustomTask)task).ran); |
755 |
> |
assertFalse(task.isDone()); |
756 |
|
assertFalse(task.isCancelled()); |
757 |
|
} |
758 |
< |
} |
733 |
< |
|
734 |
< |
/** |
735 |
< |
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
736 |
< |
* delayed tasks are cancelled at shutdown |
737 |
< |
*/ |
738 |
< |
public void testShutdown2() throws InterruptedException { |
739 |
< |
CustomExecutor p = new CustomExecutor(1); |
740 |
< |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
741 |
< |
assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
742 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
743 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
744 |
< |
for (int i = 0; i < tasks.length; i++) |
745 |
< |
tasks[i] = p.schedule(new NoOpRunnable(), |
746 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
747 |
< |
BlockingQueue q = p.getQueue(); |
748 |
< |
assertEquals(tasks.length, q.size()); |
749 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
750 |
< |
assertTrue(p.isShutdown()); |
751 |
< |
assertTrue(q.isEmpty()); |
752 |
< |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
758 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
759 |
|
assertTrue(p.isTerminated()); |
754 |
– |
for (ScheduledFuture task : tasks) { |
755 |
– |
assertTrue(task.isDone()); |
756 |
– |
assertTrue(task.isCancelled()); |
757 |
– |
} |
760 |
|
} |
761 |
|
|
762 |
|
/** |
763 |
< |
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
764 |
< |
* periodic tasks are cancelled at shutdown |
765 |
< |
*/ |
766 |
< |
public void testShutdown3() throws InterruptedException { |
767 |
< |
CustomExecutor p = new CustomExecutor(1); |
768 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
769 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
770 |
< |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
771 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
772 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
773 |
< |
long initialDelay = LONG_DELAY_MS; |
774 |
< |
ScheduledFuture task = |
775 |
< |
p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay, |
776 |
< |
5, MILLISECONDS); |
763 |
> |
* By default, periodic tasks are cancelled at shutdown. |
764 |
> |
* By default, delayed tasks keep running after shutdown. |
765 |
> |
* Check that changing the default values work: |
766 |
> |
* - setExecuteExistingDelayedTasksAfterShutdownPolicy |
767 |
> |
* - setContinueExistingPeriodicTasksAfterShutdownPolicy |
768 |
> |
*/ |
769 |
> |
public void testShutdown_cancellation() throws Exception { |
770 |
> |
Boolean[] allBooleans = { null, Boolean.FALSE, Boolean.TRUE }; |
771 |
> |
for (Boolean policy : allBooleans) |
772 |
> |
{ |
773 |
> |
final int poolSize = 2; |
774 |
> |
final CustomExecutor p = new CustomExecutor(poolSize); |
775 |
> |
final boolean effectiveDelayedPolicy = (policy != Boolean.FALSE); |
776 |
> |
final boolean effectivePeriodicPolicy = (policy == Boolean.TRUE); |
777 |
> |
final boolean effectiveRemovePolicy = (policy == Boolean.TRUE); |
778 |
> |
if (policy != null) { |
779 |
> |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(policy); |
780 |
> |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(policy); |
781 |
> |
p.setRemoveOnCancelPolicy(policy); |
782 |
> |
} |
783 |
> |
assertEquals(effectiveDelayedPolicy, |
784 |
> |
p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
785 |
> |
assertEquals(effectivePeriodicPolicy, |
786 |
> |
p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
787 |
> |
assertEquals(effectiveRemovePolicy, |
788 |
> |
p.getRemoveOnCancelPolicy()); |
789 |
> |
// Strategy: Wedge the pool with poolSize "blocker" threads |
790 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
791 |
> |
final CountDownLatch poolBlocked = new CountDownLatch(poolSize); |
792 |
> |
final CountDownLatch unblock = new CountDownLatch(1); |
793 |
> |
final CountDownLatch periodicLatch1 = new CountDownLatch(2); |
794 |
> |
final CountDownLatch periodicLatch2 = new CountDownLatch(2); |
795 |
> |
Runnable task = new CheckedRunnable() { public void realRun() |
796 |
> |
throws InterruptedException { |
797 |
> |
poolBlocked.countDown(); |
798 |
> |
assertTrue(unblock.await(LONG_DELAY_MS, MILLISECONDS)); |
799 |
> |
ran.getAndIncrement(); |
800 |
> |
}}; |
801 |
> |
List<Future<?>> blockers = new ArrayList<>(); |
802 |
> |
List<Future<?>> periodics = new ArrayList<>(); |
803 |
> |
List<Future<?>> delayeds = new ArrayList<>(); |
804 |
> |
for (int i = 0; i < poolSize; i++) |
805 |
> |
blockers.add(p.submit(task)); |
806 |
> |
assertTrue(poolBlocked.await(LONG_DELAY_MS, MILLISECONDS)); |
807 |
> |
|
808 |
> |
periodics.add(p.scheduleAtFixedRate(countDowner(periodicLatch1), |
809 |
> |
1, 1, MILLISECONDS)); |
810 |
> |
periodics.add(p.scheduleWithFixedDelay(countDowner(periodicLatch2), |
811 |
> |
1, 1, MILLISECONDS)); |
812 |
> |
delayeds.add(p.schedule(task, 1, MILLISECONDS)); |
813 |
> |
|
814 |
> |
assertTrue(p.getQueue().containsAll(periodics)); |
815 |
> |
assertTrue(p.getQueue().containsAll(delayeds)); |
816 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
817 |
|
assertTrue(p.isShutdown()); |
818 |
< |
assertTrue(p.getQueue().isEmpty()); |
819 |
< |
assertTrue(task.isDone()); |
820 |
< |
assertTrue(task.isCancelled()); |
821 |
< |
joinPool(p); |
822 |
< |
} |
823 |
< |
|
824 |
< |
/** |
825 |
< |
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
826 |
< |
* periodic tasks are not cancelled at shutdown |
827 |
< |
*/ |
828 |
< |
public void testShutdown4() throws InterruptedException { |
829 |
< |
CustomExecutor p = new CustomExecutor(1); |
830 |
< |
final CountDownLatch counter = new CountDownLatch(2); |
831 |
< |
try { |
832 |
< |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
833 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
834 |
< |
assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
835 |
< |
final Runnable r = new CheckedRunnable() { |
836 |
< |
public void realRun() { |
837 |
< |
counter.countDown(); |
838 |
< |
}}; |
839 |
< |
ScheduledFuture task = |
799 |
< |
p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS); |
800 |
< |
assertFalse(task.isDone()); |
801 |
< |
assertFalse(task.isCancelled()); |
802 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
803 |
< |
assertFalse(task.isCancelled()); |
804 |
< |
assertFalse(p.isTerminated()); |
805 |
< |
assertTrue(p.isShutdown()); |
806 |
< |
assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS)); |
807 |
< |
assertFalse(task.isCancelled()); |
808 |
< |
assertTrue(task.cancel(false)); |
809 |
< |
assertTrue(task.isDone()); |
810 |
< |
assertTrue(task.isCancelled()); |
811 |
< |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
812 |
< |
assertTrue(p.isTerminated()); |
818 |
> |
assertFalse(p.isTerminated()); |
819 |
> |
for (Future<?> periodic : periodics) { |
820 |
> |
assertTrue(effectivePeriodicPolicy ^ periodic.isCancelled()); |
821 |
> |
assertTrue(effectivePeriodicPolicy ^ periodic.isDone()); |
822 |
> |
} |
823 |
> |
for (Future<?> delayed : delayeds) { |
824 |
> |
assertTrue(effectiveDelayedPolicy ^ delayed.isCancelled()); |
825 |
> |
assertTrue(effectiveDelayedPolicy ^ delayed.isDone()); |
826 |
> |
} |
827 |
> |
if (testImplementationDetails) { |
828 |
> |
assertEquals(effectivePeriodicPolicy, |
829 |
> |
p.getQueue().containsAll(periodics)); |
830 |
> |
assertEquals(effectiveDelayedPolicy, |
831 |
> |
p.getQueue().containsAll(delayeds)); |
832 |
> |
} |
833 |
> |
// Release all pool threads |
834 |
> |
unblock.countDown(); |
835 |
> |
|
836 |
> |
for (Future<?> delayed : delayeds) { |
837 |
> |
if (effectiveDelayedPolicy) { |
838 |
> |
assertNull(delayed.get()); |
839 |
> |
} |
840 |
|
} |
841 |
< |
finally { |
842 |
< |
joinPool(p); |
841 |
> |
if (effectivePeriodicPolicy) { |
842 |
> |
assertTrue(periodicLatch1.await(LONG_DELAY_MS, MILLISECONDS)); |
843 |
> |
assertTrue(periodicLatch2.await(LONG_DELAY_MS, MILLISECONDS)); |
844 |
> |
for (Future<?> periodic : periodics) { |
845 |
> |
assertTrue(periodic.cancel(false)); |
846 |
> |
assertTrue(periodic.isCancelled()); |
847 |
> |
assertTrue(periodic.isDone()); |
848 |
> |
} |
849 |
|
} |
850 |
< |
} |
850 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
851 |
> |
assertTrue(p.isTerminated()); |
852 |
> |
assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get()); |
853 |
> |
}} |
854 |
|
|
855 |
|
/** |
856 |
|
* completed submit of callable returns result |
857 |
|
*/ |
858 |
|
public void testSubmitCallable() throws Exception { |
859 |
< |
ExecutorService e = new CustomExecutor(2); |
860 |
< |
try { |
859 |
> |
final ExecutorService e = new CustomExecutor(2); |
860 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
861 |
|
Future<String> future = e.submit(new StringTask()); |
862 |
|
String result = future.get(); |
863 |
|
assertSame(TEST_STRING, result); |
828 |
– |
} finally { |
829 |
– |
joinPool(e); |
864 |
|
} |
865 |
|
} |
866 |
|
|
868 |
|
* completed submit of runnable returns successfully |
869 |
|
*/ |
870 |
|
public void testSubmitRunnable() throws Exception { |
871 |
< |
ExecutorService e = new CustomExecutor(2); |
872 |
< |
try { |
871 |
> |
final ExecutorService e = new CustomExecutor(2); |
872 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
873 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
874 |
|
future.get(); |
875 |
|
assertTrue(future.isDone()); |
842 |
– |
} finally { |
843 |
– |
joinPool(e); |
876 |
|
} |
877 |
|
} |
878 |
|
|
880 |
|
* completed submit of (runnable, result) returns result |
881 |
|
*/ |
882 |
|
public void testSubmitRunnable2() throws Exception { |
883 |
< |
ExecutorService e = new CustomExecutor(2); |
884 |
< |
try { |
883 |
> |
final ExecutorService e = new CustomExecutor(2); |
884 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
885 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
886 |
|
String result = future.get(); |
887 |
|
assertSame(TEST_STRING, result); |
856 |
– |
} finally { |
857 |
– |
joinPool(e); |
888 |
|
} |
889 |
|
} |
890 |
|
|
892 |
|
* invokeAny(null) throws NPE |
893 |
|
*/ |
894 |
|
public void testInvokeAny1() throws Exception { |
895 |
< |
ExecutorService e = new CustomExecutor(2); |
896 |
< |
try { |
897 |
< |
e.invokeAny(null); |
898 |
< |
shouldThrow(); |
899 |
< |
} catch (NullPointerException success) { |
900 |
< |
} finally { |
871 |
< |
joinPool(e); |
895 |
> |
final ExecutorService e = new CustomExecutor(2); |
896 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
897 |
> |
try { |
898 |
> |
e.invokeAny(null); |
899 |
> |
shouldThrow(); |
900 |
> |
} catch (NullPointerException success) {} |
901 |
|
} |
902 |
|
} |
903 |
|
|
905 |
|
* invokeAny(empty collection) throws IAE |
906 |
|
*/ |
907 |
|
public void testInvokeAny2() throws Exception { |
908 |
< |
ExecutorService e = new CustomExecutor(2); |
909 |
< |
try { |
910 |
< |
e.invokeAny(new ArrayList<Callable<String>>()); |
911 |
< |
shouldThrow(); |
912 |
< |
} catch (IllegalArgumentException success) { |
913 |
< |
} finally { |
885 |
< |
joinPool(e); |
908 |
> |
final ExecutorService e = new CustomExecutor(2); |
909 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
910 |
> |
try { |
911 |
> |
e.invokeAny(new ArrayList<Callable<String>>()); |
912 |
> |
shouldThrow(); |
913 |
> |
} catch (IllegalArgumentException success) {} |
914 |
|
} |
915 |
|
} |
916 |
|
|
918 |
|
* invokeAny(c) throws NPE if c has null elements |
919 |
|
*/ |
920 |
|
public void testInvokeAny3() throws Exception { |
921 |
< |
CountDownLatch latch = new CountDownLatch(1); |
922 |
< |
ExecutorService e = new CustomExecutor(2); |
923 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
924 |
< |
l.add(latchAwaitingStringTask(latch)); |
925 |
< |
l.add(null); |
926 |
< |
try { |
927 |
< |
e.invokeAny(l); |
928 |
< |
shouldThrow(); |
929 |
< |
} catch (NullPointerException success) { |
930 |
< |
} finally { |
921 |
> |
final CountDownLatch latch = new CountDownLatch(1); |
922 |
> |
final ExecutorService e = new CustomExecutor(2); |
923 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
924 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
925 |
> |
l.add(latchAwaitingStringTask(latch)); |
926 |
> |
l.add(null); |
927 |
> |
try { |
928 |
> |
e.invokeAny(l); |
929 |
> |
shouldThrow(); |
930 |
> |
} catch (NullPointerException success) {} |
931 |
|
latch.countDown(); |
904 |
– |
joinPool(e); |
932 |
|
} |
933 |
|
} |
934 |
|
|
936 |
|
* invokeAny(c) throws ExecutionException if no task completes |
937 |
|
*/ |
938 |
|
public void testInvokeAny4() throws Exception { |
939 |
< |
ExecutorService e = new CustomExecutor(2); |
940 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
941 |
< |
l.add(new NPETask()); |
942 |
< |
try { |
943 |
< |
e.invokeAny(l); |
944 |
< |
shouldThrow(); |
945 |
< |
} catch (ExecutionException success) { |
946 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
947 |
< |
} finally { |
948 |
< |
joinPool(e); |
939 |
> |
final ExecutorService e = new CustomExecutor(2); |
940 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
941 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
942 |
> |
l.add(new NPETask()); |
943 |
> |
try { |
944 |
> |
e.invokeAny(l); |
945 |
> |
shouldThrow(); |
946 |
> |
} catch (ExecutionException success) { |
947 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
948 |
> |
} |
949 |
|
} |
950 |
|
} |
951 |
|
|
953 |
|
* invokeAny(c) returns result of some task |
954 |
|
*/ |
955 |
|
public void testInvokeAny5() throws Exception { |
956 |
< |
ExecutorService e = new CustomExecutor(2); |
957 |
< |
try { |
956 |
> |
final ExecutorService e = new CustomExecutor(2); |
957 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
958 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
959 |
|
l.add(new StringTask()); |
960 |
|
l.add(new StringTask()); |
961 |
|
String result = e.invokeAny(l); |
962 |
|
assertSame(TEST_STRING, result); |
936 |
– |
} finally { |
937 |
– |
joinPool(e); |
963 |
|
} |
964 |
|
} |
965 |
|
|
967 |
|
* invokeAll(null) throws NPE |
968 |
|
*/ |
969 |
|
public void testInvokeAll1() throws Exception { |
970 |
< |
ExecutorService e = new CustomExecutor(2); |
971 |
< |
try { |
972 |
< |
e.invokeAll(null); |
973 |
< |
shouldThrow(); |
974 |
< |
} catch (NullPointerException success) { |
975 |
< |
} finally { |
951 |
< |
joinPool(e); |
970 |
> |
final ExecutorService e = new CustomExecutor(2); |
971 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
972 |
> |
try { |
973 |
> |
e.invokeAll(null); |
974 |
> |
shouldThrow(); |
975 |
> |
} catch (NullPointerException success) {} |
976 |
|
} |
977 |
|
} |
978 |
|
|
980 |
|
* invokeAll(empty collection) returns empty collection |
981 |
|
*/ |
982 |
|
public void testInvokeAll2() throws Exception { |
983 |
< |
ExecutorService e = new CustomExecutor(2); |
984 |
< |
try { |
983 |
> |
final ExecutorService e = new CustomExecutor(2); |
984 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
985 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
986 |
|
assertTrue(r.isEmpty()); |
963 |
– |
} finally { |
964 |
– |
joinPool(e); |
987 |
|
} |
988 |
|
} |
989 |
|
|
991 |
|
* invokeAll(c) throws NPE if c has null elements |
992 |
|
*/ |
993 |
|
public void testInvokeAll3() throws Exception { |
994 |
< |
ExecutorService e = new CustomExecutor(2); |
995 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
996 |
< |
l.add(new StringTask()); |
997 |
< |
l.add(null); |
998 |
< |
try { |
999 |
< |
e.invokeAll(l); |
1000 |
< |
shouldThrow(); |
1001 |
< |
} catch (NullPointerException success) { |
1002 |
< |
} finally { |
981 |
< |
joinPool(e); |
994 |
> |
final ExecutorService e = new CustomExecutor(2); |
995 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
996 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
997 |
> |
l.add(new StringTask()); |
998 |
> |
l.add(null); |
999 |
> |
try { |
1000 |
> |
e.invokeAll(l); |
1001 |
> |
shouldThrow(); |
1002 |
> |
} catch (NullPointerException success) {} |
1003 |
|
} |
1004 |
|
} |
1005 |
|
|
1007 |
|
* get of invokeAll(c) throws exception on failed task |
1008 |
|
*/ |
1009 |
|
public void testInvokeAll4() throws Exception { |
1010 |
< |
ExecutorService e = new CustomExecutor(2); |
1011 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1012 |
< |
l.add(new NPETask()); |
1013 |
< |
List<Future<String>> futures = e.invokeAll(l); |
1014 |
< |
assertEquals(1, futures.size()); |
1015 |
< |
try { |
1016 |
< |
futures.get(0).get(); |
1017 |
< |
shouldThrow(); |
1018 |
< |
} catch (ExecutionException success) { |
1019 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1020 |
< |
} finally { |
1021 |
< |
joinPool(e); |
1010 |
> |
final ExecutorService e = new CustomExecutor(2); |
1011 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1012 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1013 |
> |
l.add(new NPETask()); |
1014 |
> |
List<Future<String>> futures = e.invokeAll(l); |
1015 |
> |
assertEquals(1, futures.size()); |
1016 |
> |
try { |
1017 |
> |
futures.get(0).get(); |
1018 |
> |
shouldThrow(); |
1019 |
> |
} catch (ExecutionException success) { |
1020 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1021 |
> |
} |
1022 |
|
} |
1023 |
|
} |
1024 |
|
|
1026 |
|
* invokeAll(c) returns results of all completed tasks |
1027 |
|
*/ |
1028 |
|
public void testInvokeAll5() throws Exception { |
1029 |
< |
ExecutorService e = new CustomExecutor(2); |
1030 |
< |
try { |
1029 |
> |
final ExecutorService e = new CustomExecutor(2); |
1030 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1031 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1032 |
|
l.add(new StringTask()); |
1033 |
|
l.add(new StringTask()); |
1035 |
|
assertEquals(2, futures.size()); |
1036 |
|
for (Future<String> future : futures) |
1037 |
|
assertSame(TEST_STRING, future.get()); |
1017 |
– |
} finally { |
1018 |
– |
joinPool(e); |
1038 |
|
} |
1039 |
|
} |
1040 |
|
|
1042 |
|
* timed invokeAny(null) throws NPE |
1043 |
|
*/ |
1044 |
|
public void testTimedInvokeAny1() throws Exception { |
1045 |
< |
ExecutorService e = new CustomExecutor(2); |
1046 |
< |
try { |
1047 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1048 |
< |
shouldThrow(); |
1049 |
< |
} catch (NullPointerException success) { |
1050 |
< |
} finally { |
1032 |
< |
joinPool(e); |
1045 |
> |
final ExecutorService e = new CustomExecutor(2); |
1046 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1047 |
> |
try { |
1048 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1049 |
> |
shouldThrow(); |
1050 |
> |
} catch (NullPointerException success) {} |
1051 |
|
} |
1052 |
|
} |
1053 |
|
|
1055 |
|
* timed invokeAny(,,null) throws NPE |
1056 |
|
*/ |
1057 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1058 |
< |
ExecutorService e = new CustomExecutor(2); |
1059 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1060 |
< |
l.add(new StringTask()); |
1061 |
< |
try { |
1062 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1063 |
< |
shouldThrow(); |
1064 |
< |
} catch (NullPointerException success) { |
1065 |
< |
} finally { |
1048 |
< |
joinPool(e); |
1058 |
> |
final ExecutorService e = new CustomExecutor(2); |
1059 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1060 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1061 |
> |
l.add(new StringTask()); |
1062 |
> |
try { |
1063 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1064 |
> |
shouldThrow(); |
1065 |
> |
} catch (NullPointerException success) {} |
1066 |
|
} |
1067 |
|
} |
1068 |
|
|
1070 |
|
* timed invokeAny(empty collection) throws IAE |
1071 |
|
*/ |
1072 |
|
public void testTimedInvokeAny2() throws Exception { |
1073 |
< |
ExecutorService e = new CustomExecutor(2); |
1074 |
< |
try { |
1075 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1076 |
< |
shouldThrow(); |
1077 |
< |
} catch (IllegalArgumentException success) { |
1078 |
< |
} finally { |
1062 |
< |
joinPool(e); |
1073 |
> |
final ExecutorService e = new CustomExecutor(2); |
1074 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1075 |
> |
try { |
1076 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1077 |
> |
shouldThrow(); |
1078 |
> |
} catch (IllegalArgumentException success) {} |
1079 |
|
} |
1080 |
|
} |
1081 |
|
|
1084 |
|
*/ |
1085 |
|
public void testTimedInvokeAny3() throws Exception { |
1086 |
|
CountDownLatch latch = new CountDownLatch(1); |
1087 |
< |
ExecutorService e = new CustomExecutor(2); |
1088 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1089 |
< |
l.add(latchAwaitingStringTask(latch)); |
1090 |
< |
l.add(null); |
1091 |
< |
try { |
1092 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1093 |
< |
shouldThrow(); |
1094 |
< |
} catch (NullPointerException success) { |
1095 |
< |
} finally { |
1087 |
> |
final ExecutorService e = new CustomExecutor(2); |
1088 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1089 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1090 |
> |
l.add(latchAwaitingStringTask(latch)); |
1091 |
> |
l.add(null); |
1092 |
> |
try { |
1093 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1094 |
> |
shouldThrow(); |
1095 |
> |
} catch (NullPointerException success) {} |
1096 |
|
latch.countDown(); |
1081 |
– |
joinPool(e); |
1097 |
|
} |
1098 |
|
} |
1099 |
|
|
1101 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
1102 |
|
*/ |
1103 |
|
public void testTimedInvokeAny4() throws Exception { |
1104 |
< |
ExecutorService e = new CustomExecutor(2); |
1105 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1106 |
< |
l.add(new NPETask()); |
1107 |
< |
try { |
1108 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1109 |
< |
shouldThrow(); |
1110 |
< |
} catch (ExecutionException success) { |
1111 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1112 |
< |
} finally { |
1113 |
< |
joinPool(e); |
1104 |
> |
final ExecutorService e = new CustomExecutor(2); |
1105 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1106 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1107 |
> |
l.add(new NPETask()); |
1108 |
> |
try { |
1109 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1110 |
> |
shouldThrow(); |
1111 |
> |
} catch (ExecutionException success) { |
1112 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1113 |
> |
} |
1114 |
|
} |
1115 |
|
} |
1116 |
|
|
1118 |
|
* timed invokeAny(c) returns result of some task |
1119 |
|
*/ |
1120 |
|
public void testTimedInvokeAny5() throws Exception { |
1121 |
< |
ExecutorService e = new CustomExecutor(2); |
1122 |
< |
try { |
1121 |
> |
final ExecutorService e = new CustomExecutor(2); |
1122 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1123 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1124 |
|
l.add(new StringTask()); |
1125 |
|
l.add(new StringTask()); |
1126 |
|
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1127 |
|
assertSame(TEST_STRING, result); |
1113 |
– |
} finally { |
1114 |
– |
joinPool(e); |
1128 |
|
} |
1129 |
|
} |
1130 |
|
|
1132 |
|
* timed invokeAll(null) throws NPE |
1133 |
|
*/ |
1134 |
|
public void testTimedInvokeAll1() throws Exception { |
1135 |
< |
ExecutorService e = new CustomExecutor(2); |
1136 |
< |
try { |
1137 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1138 |
< |
shouldThrow(); |
1139 |
< |
} catch (NullPointerException success) { |
1140 |
< |
} finally { |
1128 |
< |
joinPool(e); |
1135 |
> |
final ExecutorService e = new CustomExecutor(2); |
1136 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1137 |
> |
try { |
1138 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1139 |
> |
shouldThrow(); |
1140 |
> |
} catch (NullPointerException success) {} |
1141 |
|
} |
1142 |
|
} |
1143 |
|
|
1145 |
|
* timed invokeAll(,,null) throws NPE |
1146 |
|
*/ |
1147 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1148 |
< |
ExecutorService e = new CustomExecutor(2); |
1149 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1150 |
< |
l.add(new StringTask()); |
1151 |
< |
try { |
1152 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1153 |
< |
shouldThrow(); |
1154 |
< |
} catch (NullPointerException success) { |
1155 |
< |
} finally { |
1144 |
< |
joinPool(e); |
1148 |
> |
final ExecutorService e = new CustomExecutor(2); |
1149 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1150 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1151 |
> |
l.add(new StringTask()); |
1152 |
> |
try { |
1153 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1154 |
> |
shouldThrow(); |
1155 |
> |
} catch (NullPointerException success) {} |
1156 |
|
} |
1157 |
|
} |
1158 |
|
|
1160 |
|
* timed invokeAll(empty collection) returns empty collection |
1161 |
|
*/ |
1162 |
|
public void testTimedInvokeAll2() throws Exception { |
1163 |
< |
ExecutorService e = new CustomExecutor(2); |
1164 |
< |
try { |
1163 |
> |
final ExecutorService e = new CustomExecutor(2); |
1164 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1165 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1166 |
|
assertTrue(r.isEmpty()); |
1156 |
– |
} finally { |
1157 |
– |
joinPool(e); |
1167 |
|
} |
1168 |
|
} |
1169 |
|
|
1171 |
|
* timed invokeAll(c) throws NPE if c has null elements |
1172 |
|
*/ |
1173 |
|
public void testTimedInvokeAll3() throws Exception { |
1174 |
< |
ExecutorService e = new CustomExecutor(2); |
1175 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1176 |
< |
l.add(new StringTask()); |
1177 |
< |
l.add(null); |
1178 |
< |
try { |
1179 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1180 |
< |
shouldThrow(); |
1181 |
< |
} catch (NullPointerException success) { |
1182 |
< |
} finally { |
1174 |
< |
joinPool(e); |
1174 |
> |
final ExecutorService e = new CustomExecutor(2); |
1175 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1176 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1177 |
> |
l.add(new StringTask()); |
1178 |
> |
l.add(null); |
1179 |
> |
try { |
1180 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1181 |
> |
shouldThrow(); |
1182 |
> |
} catch (NullPointerException success) {} |
1183 |
|
} |
1184 |
|
} |
1185 |
|
|
1187 |
|
* get of element of invokeAll(c) throws exception on failed task |
1188 |
|
*/ |
1189 |
|
public void testTimedInvokeAll4() throws Exception { |
1190 |
< |
ExecutorService e = new CustomExecutor(2); |
1191 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1192 |
< |
l.add(new NPETask()); |
1193 |
< |
List<Future<String>> futures = |
1194 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1195 |
< |
assertEquals(1, futures.size()); |
1196 |
< |
try { |
1197 |
< |
futures.get(0).get(); |
1198 |
< |
shouldThrow(); |
1199 |
< |
} catch (ExecutionException success) { |
1200 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1201 |
< |
} finally { |
1202 |
< |
joinPool(e); |
1190 |
> |
final ExecutorService e = new CustomExecutor(2); |
1191 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1192 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1193 |
> |
l.add(new NPETask()); |
1194 |
> |
List<Future<String>> futures = |
1195 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1196 |
> |
assertEquals(1, futures.size()); |
1197 |
> |
try { |
1198 |
> |
futures.get(0).get(); |
1199 |
> |
shouldThrow(); |
1200 |
> |
} catch (ExecutionException success) { |
1201 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1202 |
> |
} |
1203 |
|
} |
1204 |
|
} |
1205 |
|
|
1207 |
|
* timed invokeAll(c) returns results of all completed tasks |
1208 |
|
*/ |
1209 |
|
public void testTimedInvokeAll5() throws Exception { |
1210 |
< |
ExecutorService e = new CustomExecutor(2); |
1211 |
< |
try { |
1210 |
> |
final ExecutorService e = new CustomExecutor(2); |
1211 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1212 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1213 |
|
l.add(new StringTask()); |
1214 |
|
l.add(new StringTask()); |
1215 |
|
List<Future<String>> futures = |
1216 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1216 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1217 |
|
assertEquals(2, futures.size()); |
1218 |
|
for (Future<String> future : futures) |
1219 |
|
assertSame(TEST_STRING, future.get()); |
1212 |
– |
} finally { |
1213 |
– |
joinPool(e); |
1220 |
|
} |
1221 |
|
} |
1222 |
|
|
1224 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1225 |
|
*/ |
1226 |
|
public void testTimedInvokeAll6() throws Exception { |
1227 |
< |
ExecutorService e = new CustomExecutor(2); |
1228 |
< |
try { |
1229 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1230 |
< |
l.add(new StringTask()); |
1231 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1232 |
< |
l.add(new StringTask()); |
1233 |
< |
List<Future<String>> futures = |
1234 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1235 |
< |
assertEquals(l.size(), futures.size()); |
1236 |
< |
for (Future future : futures) |
1237 |
< |
assertTrue(future.isDone()); |
1238 |
< |
assertFalse(futures.get(0).isCancelled()); |
1239 |
< |
assertTrue(futures.get(1).isCancelled()); |
1240 |
< |
} finally { |
1241 |
< |
joinPool(e); |
1227 |
> |
final ExecutorService e = new CustomExecutor(2); |
1228 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1229 |
> |
for (long timeout = timeoutMillis();;) { |
1230 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1231 |
> |
tasks.add(new StringTask("0")); |
1232 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1233 |
> |
tasks.add(new StringTask("2")); |
1234 |
> |
long startTime = System.nanoTime(); |
1235 |
> |
List<Future<String>> futures = |
1236 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1237 |
> |
assertEquals(tasks.size(), futures.size()); |
1238 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1239 |
> |
for (Future future : futures) |
1240 |
> |
assertTrue(future.isDone()); |
1241 |
> |
assertTrue(futures.get(1).isCancelled()); |
1242 |
> |
try { |
1243 |
> |
assertEquals("0", futures.get(0).get()); |
1244 |
> |
assertEquals("2", futures.get(2).get()); |
1245 |
> |
break; |
1246 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1247 |
> |
timeout *= 2; |
1248 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1249 |
> |
fail("expected exactly one task to be cancelled"); |
1250 |
> |
} |
1251 |
> |
} |
1252 |
|
} |
1253 |
|
} |
1254 |
|
|