1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
* Pat Fisher, Mike Judd. |
7 |
*/ |
8 |
|
9 |
import java.util.concurrent.*; |
10 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
11 |
import java.util.concurrent.atomic.*; |
12 |
import junit.framework.*; |
13 |
import java.util.*; |
14 |
|
15 |
public class ThreadPoolExecutorTest extends JSR166TestCase { |
16 |
public static void main(String[] args) { |
17 |
junit.textui.TestRunner.run(suite()); |
18 |
} |
19 |
public static Test suite() { |
20 |
return new TestSuite(ThreadPoolExecutorTest.class); |
21 |
} |
22 |
|
23 |
static class ExtendedTPE extends ThreadPoolExecutor { |
24 |
volatile boolean beforeCalled = false; |
25 |
volatile boolean afterCalled = false; |
26 |
volatile boolean terminatedCalled = false; |
27 |
public ExtendedTPE() { |
28 |
super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>()); |
29 |
} |
30 |
protected void beforeExecute(Thread t, Runnable r) { |
31 |
beforeCalled = true; |
32 |
} |
33 |
protected void afterExecute(Runnable r, Throwable t) { |
34 |
afterCalled = true; |
35 |
} |
36 |
protected void terminated() { |
37 |
terminatedCalled = true; |
38 |
} |
39 |
} |
40 |
|
41 |
static class FailingThreadFactory implements ThreadFactory { |
42 |
int calls = 0; |
43 |
public Thread newThread(Runnable r) { |
44 |
if (++calls > 1) return null; |
45 |
return new Thread(r); |
46 |
} |
47 |
} |
48 |
|
49 |
/** |
50 |
* execute successfully executes a runnable |
51 |
*/ |
52 |
public void testExecute() throws InterruptedException { |
53 |
final ThreadPoolExecutor p = |
54 |
new ThreadPoolExecutor(1, 1, |
55 |
LONG_DELAY_MS, MILLISECONDS, |
56 |
new ArrayBlockingQueue<Runnable>(10)); |
57 |
final CountDownLatch done = new CountDownLatch(1); |
58 |
final Runnable task = new CheckedRunnable() { |
59 |
public void realRun() { |
60 |
done.countDown(); |
61 |
}}; |
62 |
try { |
63 |
p.execute(task); |
64 |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
65 |
} finally { |
66 |
joinPool(p); |
67 |
} |
68 |
} |
69 |
|
70 |
/** |
71 |
* getActiveCount increases but doesn't overestimate, when a |
72 |
* thread becomes active |
73 |
*/ |
74 |
public void testGetActiveCount() throws InterruptedException { |
75 |
final ThreadPoolExecutor p = |
76 |
new ThreadPoolExecutor(2, 2, |
77 |
LONG_DELAY_MS, MILLISECONDS, |
78 |
new ArrayBlockingQueue<Runnable>(10)); |
79 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
80 |
final CountDownLatch done = new CountDownLatch(1); |
81 |
try { |
82 |
assertEquals(0, p.getActiveCount()); |
83 |
p.execute(new CheckedRunnable() { |
84 |
public void realRun() throws InterruptedException { |
85 |
threadStarted.countDown(); |
86 |
assertEquals(1, p.getActiveCount()); |
87 |
done.await(); |
88 |
}}); |
89 |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
90 |
assertEquals(1, p.getActiveCount()); |
91 |
} finally { |
92 |
done.countDown(); |
93 |
joinPool(p); |
94 |
} |
95 |
} |
96 |
|
97 |
/** |
98 |
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
99 |
*/ |
100 |
public void testPrestartCoreThread() { |
101 |
final ThreadPoolExecutor p = |
102 |
new ThreadPoolExecutor(2, 2, |
103 |
LONG_DELAY_MS, MILLISECONDS, |
104 |
new ArrayBlockingQueue<Runnable>(10)); |
105 |
assertEquals(0, p.getPoolSize()); |
106 |
assertTrue(p.prestartCoreThread()); |
107 |
assertEquals(1, p.getPoolSize()); |
108 |
assertTrue(p.prestartCoreThread()); |
109 |
assertEquals(2, p.getPoolSize()); |
110 |
assertFalse(p.prestartCoreThread()); |
111 |
assertEquals(2, p.getPoolSize()); |
112 |
joinPool(p); |
113 |
} |
114 |
|
115 |
/** |
116 |
* prestartAllCoreThreads starts all corePoolSize threads |
117 |
*/ |
118 |
public void testPrestartAllCoreThreads() { |
119 |
final ThreadPoolExecutor p = |
120 |
new ThreadPoolExecutor(2, 2, |
121 |
LONG_DELAY_MS, MILLISECONDS, |
122 |
new ArrayBlockingQueue<Runnable>(10)); |
123 |
assertEquals(0, p.getPoolSize()); |
124 |
p.prestartAllCoreThreads(); |
125 |
assertEquals(2, p.getPoolSize()); |
126 |
p.prestartAllCoreThreads(); |
127 |
assertEquals(2, p.getPoolSize()); |
128 |
joinPool(p); |
129 |
} |
130 |
|
131 |
/** |
132 |
* getCompletedTaskCount increases, but doesn't overestimate, |
133 |
* when tasks complete |
134 |
*/ |
135 |
public void testGetCompletedTaskCount() throws InterruptedException { |
136 |
final ThreadPoolExecutor p = |
137 |
new ThreadPoolExecutor(2, 2, |
138 |
LONG_DELAY_MS, MILLISECONDS, |
139 |
new ArrayBlockingQueue<Runnable>(10)); |
140 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
141 |
final CountDownLatch threadProceed = new CountDownLatch(1); |
142 |
final CountDownLatch threadDone = new CountDownLatch(1); |
143 |
try { |
144 |
assertEquals(0, p.getCompletedTaskCount()); |
145 |
p.execute(new CheckedRunnable() { |
146 |
public void realRun() throws InterruptedException { |
147 |
threadStarted.countDown(); |
148 |
assertEquals(0, p.getCompletedTaskCount()); |
149 |
threadProceed.await(); |
150 |
threadDone.countDown(); |
151 |
}}); |
152 |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
153 |
assertEquals(0, p.getCompletedTaskCount()); |
154 |
threadProceed.countDown(); |
155 |
threadDone.await(); |
156 |
delay(SHORT_DELAY_MS); |
157 |
assertEquals(1, p.getCompletedTaskCount()); |
158 |
} finally { |
159 |
joinPool(p); |
160 |
} |
161 |
} |
162 |
|
163 |
/** |
164 |
* getCorePoolSize returns size given in constructor if not otherwise set |
165 |
*/ |
166 |
public void testGetCorePoolSize() { |
167 |
final ThreadPoolExecutor p = |
168 |
new ThreadPoolExecutor(1, 1, |
169 |
LONG_DELAY_MS, MILLISECONDS, |
170 |
new ArrayBlockingQueue<Runnable>(10)); |
171 |
assertEquals(1, p.getCorePoolSize()); |
172 |
joinPool(p); |
173 |
} |
174 |
|
175 |
/** |
176 |
* getKeepAliveTime returns value given in constructor if not otherwise set |
177 |
*/ |
178 |
public void testGetKeepAliveTime() { |
179 |
final ThreadPoolExecutor p = |
180 |
new ThreadPoolExecutor(2, 2, |
181 |
1000, MILLISECONDS, |
182 |
new ArrayBlockingQueue<Runnable>(10)); |
183 |
assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS)); |
184 |
joinPool(p); |
185 |
} |
186 |
|
187 |
/** |
188 |
* getThreadFactory returns factory in constructor if not set |
189 |
*/ |
190 |
public void testGetThreadFactory() { |
191 |
ThreadFactory tf = new SimpleThreadFactory(); |
192 |
final ThreadPoolExecutor p = |
193 |
new ThreadPoolExecutor(1, 2, |
194 |
LONG_DELAY_MS, MILLISECONDS, |
195 |
new ArrayBlockingQueue<Runnable>(10), |
196 |
tf, |
197 |
new NoOpREHandler()); |
198 |
assertSame(tf, p.getThreadFactory()); |
199 |
joinPool(p); |
200 |
} |
201 |
|
202 |
/** |
203 |
* setThreadFactory sets the thread factory returned by getThreadFactory |
204 |
*/ |
205 |
public void testSetThreadFactory() { |
206 |
final ThreadPoolExecutor p = |
207 |
new ThreadPoolExecutor(1, 2, |
208 |
LONG_DELAY_MS, MILLISECONDS, |
209 |
new ArrayBlockingQueue<Runnable>(10)); |
210 |
ThreadFactory tf = new SimpleThreadFactory(); |
211 |
p.setThreadFactory(tf); |
212 |
assertSame(tf, p.getThreadFactory()); |
213 |
joinPool(p); |
214 |
} |
215 |
|
216 |
/** |
217 |
* setThreadFactory(null) throws NPE |
218 |
*/ |
219 |
public void testSetThreadFactoryNull() { |
220 |
final ThreadPoolExecutor p = |
221 |
new ThreadPoolExecutor(1, 2, |
222 |
LONG_DELAY_MS, MILLISECONDS, |
223 |
new ArrayBlockingQueue<Runnable>(10)); |
224 |
try { |
225 |
p.setThreadFactory(null); |
226 |
shouldThrow(); |
227 |
} catch (NullPointerException success) { |
228 |
} finally { |
229 |
joinPool(p); |
230 |
} |
231 |
} |
232 |
|
233 |
/** |
234 |
* getRejectedExecutionHandler returns handler in constructor if not set |
235 |
*/ |
236 |
public void testGetRejectedExecutionHandler() { |
237 |
final RejectedExecutionHandler h = new NoOpREHandler(); |
238 |
final ThreadPoolExecutor p = |
239 |
new ThreadPoolExecutor(1, 2, |
240 |
LONG_DELAY_MS, MILLISECONDS, |
241 |
new ArrayBlockingQueue<Runnable>(10), |
242 |
h); |
243 |
assertSame(h, p.getRejectedExecutionHandler()); |
244 |
joinPool(p); |
245 |
} |
246 |
|
247 |
/** |
248 |
* setRejectedExecutionHandler sets the handler returned by |
249 |
* getRejectedExecutionHandler |
250 |
*/ |
251 |
public void testSetRejectedExecutionHandler() { |
252 |
final ThreadPoolExecutor p = |
253 |
new ThreadPoolExecutor(1, 2, |
254 |
LONG_DELAY_MS, MILLISECONDS, |
255 |
new ArrayBlockingQueue<Runnable>(10)); |
256 |
RejectedExecutionHandler h = new NoOpREHandler(); |
257 |
p.setRejectedExecutionHandler(h); |
258 |
assertSame(h, p.getRejectedExecutionHandler()); |
259 |
joinPool(p); |
260 |
} |
261 |
|
262 |
/** |
263 |
* setRejectedExecutionHandler(null) throws NPE |
264 |
*/ |
265 |
public void testSetRejectedExecutionHandlerNull() { |
266 |
final ThreadPoolExecutor p = |
267 |
new ThreadPoolExecutor(1, 2, |
268 |
LONG_DELAY_MS, MILLISECONDS, |
269 |
new ArrayBlockingQueue<Runnable>(10)); |
270 |
try { |
271 |
p.setRejectedExecutionHandler(null); |
272 |
shouldThrow(); |
273 |
} catch (NullPointerException success) { |
274 |
} finally { |
275 |
joinPool(p); |
276 |
} |
277 |
} |
278 |
|
279 |
/** |
280 |
* getLargestPoolSize increases, but doesn't overestimate, when |
281 |
* multiple threads active |
282 |
*/ |
283 |
public void testGetLargestPoolSize() throws InterruptedException { |
284 |
final int THREADS = 3; |
285 |
final ThreadPoolExecutor p = |
286 |
new ThreadPoolExecutor(THREADS, THREADS, |
287 |
LONG_DELAY_MS, MILLISECONDS, |
288 |
new ArrayBlockingQueue<Runnable>(10)); |
289 |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
290 |
final CountDownLatch done = new CountDownLatch(1); |
291 |
try { |
292 |
assertEquals(0, p.getLargestPoolSize()); |
293 |
for (int i = 0; i < THREADS; i++) |
294 |
p.execute(new CheckedRunnable() { |
295 |
public void realRun() throws InterruptedException { |
296 |
threadsStarted.countDown(); |
297 |
done.await(); |
298 |
assertEquals(THREADS, p.getLargestPoolSize()); |
299 |
}}); |
300 |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
301 |
assertEquals(THREADS, p.getLargestPoolSize()); |
302 |
} finally { |
303 |
done.countDown(); |
304 |
joinPool(p); |
305 |
assertEquals(THREADS, p.getLargestPoolSize()); |
306 |
} |
307 |
} |
308 |
|
309 |
/** |
310 |
* getMaximumPoolSize returns value given in constructor if not |
311 |
* otherwise set |
312 |
*/ |
313 |
public void testGetMaximumPoolSize() { |
314 |
final ThreadPoolExecutor p = |
315 |
new ThreadPoolExecutor(2, 3, |
316 |
LONG_DELAY_MS, MILLISECONDS, |
317 |
new ArrayBlockingQueue<Runnable>(10)); |
318 |
assertEquals(3, p.getMaximumPoolSize()); |
319 |
joinPool(p); |
320 |
} |
321 |
|
322 |
/** |
323 |
* getPoolSize increases, but doesn't overestimate, when threads |
324 |
* become active |
325 |
*/ |
326 |
public void testGetPoolSize() throws InterruptedException { |
327 |
final ThreadPoolExecutor p = |
328 |
new ThreadPoolExecutor(1, 1, |
329 |
LONG_DELAY_MS, MILLISECONDS, |
330 |
new ArrayBlockingQueue<Runnable>(10)); |
331 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
332 |
final CountDownLatch done = new CountDownLatch(1); |
333 |
try { |
334 |
assertEquals(0, p.getPoolSize()); |
335 |
p.execute(new CheckedRunnable() { |
336 |
public void realRun() throws InterruptedException { |
337 |
threadStarted.countDown(); |
338 |
assertEquals(1, p.getPoolSize()); |
339 |
done.await(); |
340 |
}}); |
341 |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
342 |
assertEquals(1, p.getPoolSize()); |
343 |
} finally { |
344 |
done.countDown(); |
345 |
joinPool(p); |
346 |
} |
347 |
} |
348 |
|
349 |
/** |
350 |
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
351 |
*/ |
352 |
public void testGetTaskCount() throws InterruptedException { |
353 |
final ThreadPoolExecutor p = |
354 |
new ThreadPoolExecutor(1, 1, |
355 |
LONG_DELAY_MS, MILLISECONDS, |
356 |
new ArrayBlockingQueue<Runnable>(10)); |
357 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
358 |
final CountDownLatch done = new CountDownLatch(1); |
359 |
try { |
360 |
assertEquals(0, p.getTaskCount()); |
361 |
p.execute(new CheckedRunnable() { |
362 |
public void realRun() throws InterruptedException { |
363 |
threadStarted.countDown(); |
364 |
assertEquals(1, p.getTaskCount()); |
365 |
done.await(); |
366 |
}}); |
367 |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
368 |
assertEquals(1, p.getTaskCount()); |
369 |
} finally { |
370 |
done.countDown(); |
371 |
joinPool(p); |
372 |
} |
373 |
} |
374 |
|
375 |
/** |
376 |
* isShutdown is false before shutdown, true after |
377 |
*/ |
378 |
public void testIsShutdown() { |
379 |
final ThreadPoolExecutor p = |
380 |
new ThreadPoolExecutor(1, 1, |
381 |
LONG_DELAY_MS, MILLISECONDS, |
382 |
new ArrayBlockingQueue<Runnable>(10)); |
383 |
assertFalse(p.isShutdown()); |
384 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
385 |
assertTrue(p.isShutdown()); |
386 |
joinPool(p); |
387 |
} |
388 |
|
389 |
/** |
390 |
* isTerminated is false before termination, true after |
391 |
*/ |
392 |
public void testIsTerminated() throws InterruptedException { |
393 |
final ThreadPoolExecutor p = |
394 |
new ThreadPoolExecutor(1, 1, |
395 |
LONG_DELAY_MS, MILLISECONDS, |
396 |
new ArrayBlockingQueue<Runnable>(10)); |
397 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
398 |
final CountDownLatch done = new CountDownLatch(1); |
399 |
assertFalse(p.isTerminated()); |
400 |
try { |
401 |
p.execute(new CheckedRunnable() { |
402 |
public void realRun() throws InterruptedException { |
403 |
assertFalse(p.isTerminated()); |
404 |
threadStarted.countDown(); |
405 |
done.await(); |
406 |
}}); |
407 |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
408 |
assertFalse(p.isTerminating()); |
409 |
done.countDown(); |
410 |
} finally { |
411 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
412 |
} |
413 |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
414 |
assertTrue(p.isTerminated()); |
415 |
} |
416 |
|
417 |
/** |
418 |
* isTerminating is not true when running or when terminated |
419 |
*/ |
420 |
public void testIsTerminating() throws InterruptedException { |
421 |
final ThreadPoolExecutor p = |
422 |
new ThreadPoolExecutor(1, 1, |
423 |
LONG_DELAY_MS, MILLISECONDS, |
424 |
new ArrayBlockingQueue<Runnable>(10)); |
425 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
426 |
final CountDownLatch done = new CountDownLatch(1); |
427 |
try { |
428 |
assertFalse(p.isTerminating()); |
429 |
p.execute(new CheckedRunnable() { |
430 |
public void realRun() throws InterruptedException { |
431 |
assertFalse(p.isTerminating()); |
432 |
threadStarted.countDown(); |
433 |
done.await(); |
434 |
}}); |
435 |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
436 |
assertFalse(p.isTerminating()); |
437 |
done.countDown(); |
438 |
} finally { |
439 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
440 |
} |
441 |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
442 |
assertTrue(p.isTerminated()); |
443 |
assertFalse(p.isTerminating()); |
444 |
} |
445 |
|
446 |
/** |
447 |
* getQueue returns the work queue, which contains queued tasks |
448 |
*/ |
449 |
public void testGetQueue() throws InterruptedException { |
450 |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
451 |
final ThreadPoolExecutor p = |
452 |
new ThreadPoolExecutor(1, 1, |
453 |
LONG_DELAY_MS, MILLISECONDS, |
454 |
q); |
455 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
456 |
final CountDownLatch done = new CountDownLatch(1); |
457 |
try { |
458 |
FutureTask[] tasks = new FutureTask[5]; |
459 |
for (int i = 0; i < tasks.length; i++) { |
460 |
Callable task = new CheckedCallable<Boolean>() { |
461 |
public Boolean realCall() throws InterruptedException { |
462 |
threadStarted.countDown(); |
463 |
assertSame(q, p.getQueue()); |
464 |
done.await(); |
465 |
return Boolean.TRUE; |
466 |
}}; |
467 |
tasks[i] = new FutureTask(task); |
468 |
p.execute(tasks[i]); |
469 |
} |
470 |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
471 |
assertSame(q, p.getQueue()); |
472 |
assertFalse(q.contains(tasks[0])); |
473 |
assertTrue(q.contains(tasks[tasks.length - 1])); |
474 |
assertEquals(tasks.length - 1, q.size()); |
475 |
} finally { |
476 |
done.countDown(); |
477 |
joinPool(p); |
478 |
} |
479 |
} |
480 |
|
481 |
/** |
482 |
* remove(task) removes queued task, and fails to remove active task |
483 |
*/ |
484 |
public void testRemove() throws InterruptedException { |
485 |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
486 |
final ThreadPoolExecutor p = |
487 |
new ThreadPoolExecutor(1, 1, |
488 |
LONG_DELAY_MS, MILLISECONDS, |
489 |
q); |
490 |
Runnable[] tasks = new Runnable[5]; |
491 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
492 |
final CountDownLatch done = new CountDownLatch(1); |
493 |
try { |
494 |
for (int i = 0; i < tasks.length; i++) { |
495 |
tasks[i] = new CheckedRunnable() { |
496 |
public void realRun() throws InterruptedException { |
497 |
threadStarted.countDown(); |
498 |
done.await(); |
499 |
}}; |
500 |
p.execute(tasks[i]); |
501 |
} |
502 |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
503 |
assertFalse(p.remove(tasks[0])); |
504 |
assertTrue(q.contains(tasks[4])); |
505 |
assertTrue(q.contains(tasks[3])); |
506 |
assertTrue(p.remove(tasks[4])); |
507 |
assertFalse(p.remove(tasks[4])); |
508 |
assertFalse(q.contains(tasks[4])); |
509 |
assertTrue(q.contains(tasks[3])); |
510 |
assertTrue(p.remove(tasks[3])); |
511 |
assertFalse(q.contains(tasks[3])); |
512 |
} finally { |
513 |
done.countDown(); |
514 |
joinPool(p); |
515 |
} |
516 |
} |
517 |
|
518 |
/** |
519 |
* purge removes cancelled tasks from the queue |
520 |
*/ |
521 |
public void testPurge() throws InterruptedException { |
522 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
523 |
final CountDownLatch done = new CountDownLatch(1); |
524 |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
525 |
final ThreadPoolExecutor p = |
526 |
new ThreadPoolExecutor(1, 1, |
527 |
LONG_DELAY_MS, MILLISECONDS, |
528 |
q); |
529 |
FutureTask[] tasks = new FutureTask[5]; |
530 |
try { |
531 |
for (int i = 0; i < tasks.length; i++) { |
532 |
Callable task = new CheckedCallable<Boolean>() { |
533 |
public Boolean realCall() throws InterruptedException { |
534 |
threadStarted.countDown(); |
535 |
done.await(); |
536 |
return Boolean.TRUE; |
537 |
}}; |
538 |
tasks[i] = new FutureTask(task); |
539 |
p.execute(tasks[i]); |
540 |
} |
541 |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
542 |
assertEquals(tasks.length, p.getTaskCount()); |
543 |
assertEquals(tasks.length - 1, q.size()); |
544 |
assertEquals(1L, p.getActiveCount()); |
545 |
assertEquals(0L, p.getCompletedTaskCount()); |
546 |
tasks[4].cancel(true); |
547 |
tasks[3].cancel(false); |
548 |
p.purge(); |
549 |
assertEquals(tasks.length - 3, q.size()); |
550 |
assertEquals(tasks.length - 2, p.getTaskCount()); |
551 |
p.purge(); // Nothing to do |
552 |
assertEquals(tasks.length - 3, q.size()); |
553 |
assertEquals(tasks.length - 2, p.getTaskCount()); |
554 |
} finally { |
555 |
done.countDown(); |
556 |
joinPool(p); |
557 |
} |
558 |
} |
559 |
|
560 |
/** |
561 |
* shutdownNow returns a list containing tasks that were not run |
562 |
*/ |
563 |
public void testShutdownNow() { |
564 |
final ThreadPoolExecutor p = |
565 |
new ThreadPoolExecutor(1, 1, |
566 |
LONG_DELAY_MS, MILLISECONDS, |
567 |
new ArrayBlockingQueue<Runnable>(10)); |
568 |
List l; |
569 |
try { |
570 |
for (int i = 0; i < 5; i++) |
571 |
p.execute(new MediumPossiblyInterruptedRunnable()); |
572 |
} |
573 |
finally { |
574 |
try { |
575 |
l = p.shutdownNow(); |
576 |
} catch (SecurityException ok) { return; } |
577 |
} |
578 |
assertTrue(p.isShutdown()); |
579 |
assertTrue(l.size() <= 4); |
580 |
} |
581 |
|
582 |
// Exception Tests |
583 |
|
584 |
/** |
585 |
* Constructor throws if corePoolSize argument is less than zero |
586 |
*/ |
587 |
public void testConstructor1() { |
588 |
try { |
589 |
new ThreadPoolExecutor(-1, 1, |
590 |
LONG_DELAY_MS, MILLISECONDS, |
591 |
new ArrayBlockingQueue<Runnable>(10)); |
592 |
shouldThrow(); |
593 |
} catch (IllegalArgumentException success) {} |
594 |
} |
595 |
|
596 |
/** |
597 |
* Constructor throws if maximumPoolSize is less than zero |
598 |
*/ |
599 |
public void testConstructor2() { |
600 |
try { |
601 |
new ThreadPoolExecutor(1, -1, |
602 |
LONG_DELAY_MS, MILLISECONDS, |
603 |
new ArrayBlockingQueue<Runnable>(10)); |
604 |
shouldThrow(); |
605 |
} catch (IllegalArgumentException success) {} |
606 |
} |
607 |
|
608 |
/** |
609 |
* Constructor throws if maximumPoolSize is equal to zero |
610 |
*/ |
611 |
public void testConstructor3() { |
612 |
try { |
613 |
new ThreadPoolExecutor(1, 0, |
614 |
LONG_DELAY_MS, MILLISECONDS, |
615 |
new ArrayBlockingQueue<Runnable>(10)); |
616 |
shouldThrow(); |
617 |
} catch (IllegalArgumentException success) {} |
618 |
} |
619 |
|
620 |
/** |
621 |
* Constructor throws if keepAliveTime is less than zero |
622 |
*/ |
623 |
public void testConstructor4() { |
624 |
try { |
625 |
new ThreadPoolExecutor(1, 2, |
626 |
-1L, MILLISECONDS, |
627 |
new ArrayBlockingQueue<Runnable>(10)); |
628 |
shouldThrow(); |
629 |
} catch (IllegalArgumentException success) {} |
630 |
} |
631 |
|
632 |
/** |
633 |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
634 |
*/ |
635 |
public void testConstructor5() { |
636 |
try { |
637 |
new ThreadPoolExecutor(2, 1, |
638 |
LONG_DELAY_MS, MILLISECONDS, |
639 |
new ArrayBlockingQueue<Runnable>(10)); |
640 |
shouldThrow(); |
641 |
} catch (IllegalArgumentException success) {} |
642 |
} |
643 |
|
644 |
/** |
645 |
* Constructor throws if workQueue is set to null |
646 |
*/ |
647 |
public void testConstructorNullPointerException() { |
648 |
try { |
649 |
new ThreadPoolExecutor(1, 2, |
650 |
LONG_DELAY_MS, MILLISECONDS, |
651 |
(BlockingQueue) null); |
652 |
shouldThrow(); |
653 |
} catch (NullPointerException success) {} |
654 |
} |
655 |
|
656 |
/** |
657 |
* Constructor throws if corePoolSize argument is less than zero |
658 |
*/ |
659 |
public void testConstructor6() { |
660 |
try { |
661 |
new ThreadPoolExecutor(-1, 1, |
662 |
LONG_DELAY_MS, MILLISECONDS, |
663 |
new ArrayBlockingQueue<Runnable>(10), |
664 |
new SimpleThreadFactory()); |
665 |
shouldThrow(); |
666 |
} catch (IllegalArgumentException success) {} |
667 |
} |
668 |
|
669 |
/** |
670 |
* Constructor throws if maximumPoolSize is less than zero |
671 |
*/ |
672 |
public void testConstructor7() { |
673 |
try { |
674 |
new ThreadPoolExecutor(1, -1, |
675 |
LONG_DELAY_MS, MILLISECONDS, |
676 |
new ArrayBlockingQueue<Runnable>(10), |
677 |
new SimpleThreadFactory()); |
678 |
shouldThrow(); |
679 |
} catch (IllegalArgumentException success) {} |
680 |
} |
681 |
|
682 |
/** |
683 |
* Constructor throws if maximumPoolSize is equal to zero |
684 |
*/ |
685 |
public void testConstructor8() { |
686 |
try { |
687 |
new ThreadPoolExecutor(1, 0, |
688 |
LONG_DELAY_MS, MILLISECONDS, |
689 |
new ArrayBlockingQueue<Runnable>(10), |
690 |
new SimpleThreadFactory()); |
691 |
shouldThrow(); |
692 |
} catch (IllegalArgumentException success) {} |
693 |
} |
694 |
|
695 |
/** |
696 |
* Constructor throws if keepAliveTime is less than zero |
697 |
*/ |
698 |
public void testConstructor9() { |
699 |
try { |
700 |
new ThreadPoolExecutor(1, 2, |
701 |
-1L, MILLISECONDS, |
702 |
new ArrayBlockingQueue<Runnable>(10), |
703 |
new SimpleThreadFactory()); |
704 |
shouldThrow(); |
705 |
} catch (IllegalArgumentException success) {} |
706 |
} |
707 |
|
708 |
/** |
709 |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
710 |
*/ |
711 |
public void testConstructor10() { |
712 |
try { |
713 |
new ThreadPoolExecutor(2, 1, |
714 |
LONG_DELAY_MS, MILLISECONDS, |
715 |
new ArrayBlockingQueue<Runnable>(10), |
716 |
new SimpleThreadFactory()); |
717 |
shouldThrow(); |
718 |
} catch (IllegalArgumentException success) {} |
719 |
} |
720 |
|
721 |
/** |
722 |
* Constructor throws if workQueue is set to null |
723 |
*/ |
724 |
public void testConstructorNullPointerException2() { |
725 |
try { |
726 |
new ThreadPoolExecutor(1, 2, |
727 |
LONG_DELAY_MS, MILLISECONDS, |
728 |
(BlockingQueue) null, |
729 |
new SimpleThreadFactory()); |
730 |
shouldThrow(); |
731 |
} catch (NullPointerException success) {} |
732 |
} |
733 |
|
734 |
/** |
735 |
* Constructor throws if threadFactory is set to null |
736 |
*/ |
737 |
public void testConstructorNullPointerException3() { |
738 |
try { |
739 |
new ThreadPoolExecutor(1, 2, |
740 |
LONG_DELAY_MS, MILLISECONDS, |
741 |
new ArrayBlockingQueue<Runnable>(10), |
742 |
(ThreadFactory) null); |
743 |
shouldThrow(); |
744 |
} catch (NullPointerException success) {} |
745 |
} |
746 |
|
747 |
/** |
748 |
* Constructor throws if corePoolSize argument is less than zero |
749 |
*/ |
750 |
public void testConstructor11() { |
751 |
try { |
752 |
new ThreadPoolExecutor(-1, 1, |
753 |
LONG_DELAY_MS, MILLISECONDS, |
754 |
new ArrayBlockingQueue<Runnable>(10), |
755 |
new NoOpREHandler()); |
756 |
shouldThrow(); |
757 |
} catch (IllegalArgumentException success) {} |
758 |
} |
759 |
|
760 |
/** |
761 |
* Constructor throws if maximumPoolSize is less than zero |
762 |
*/ |
763 |
public void testConstructor12() { |
764 |
try { |
765 |
new ThreadPoolExecutor(1, -1, |
766 |
LONG_DELAY_MS, MILLISECONDS, |
767 |
new ArrayBlockingQueue<Runnable>(10), |
768 |
new NoOpREHandler()); |
769 |
shouldThrow(); |
770 |
} catch (IllegalArgumentException success) {} |
771 |
} |
772 |
|
773 |
/** |
774 |
* Constructor throws if maximumPoolSize is equal to zero |
775 |
*/ |
776 |
public void testConstructor13() { |
777 |
try { |
778 |
new ThreadPoolExecutor(1, 0, |
779 |
LONG_DELAY_MS, MILLISECONDS, |
780 |
new ArrayBlockingQueue<Runnable>(10), |
781 |
new NoOpREHandler()); |
782 |
shouldThrow(); |
783 |
} catch (IllegalArgumentException success) {} |
784 |
} |
785 |
|
786 |
/** |
787 |
* Constructor throws if keepAliveTime is less than zero |
788 |
*/ |
789 |
public void testConstructor14() { |
790 |
try { |
791 |
new ThreadPoolExecutor(1, 2, |
792 |
-1L, MILLISECONDS, |
793 |
new ArrayBlockingQueue<Runnable>(10), |
794 |
new NoOpREHandler()); |
795 |
shouldThrow(); |
796 |
} catch (IllegalArgumentException success) {} |
797 |
} |
798 |
|
799 |
/** |
800 |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
801 |
*/ |
802 |
public void testConstructor15() { |
803 |
try { |
804 |
new ThreadPoolExecutor(2, 1, |
805 |
LONG_DELAY_MS, MILLISECONDS, |
806 |
new ArrayBlockingQueue<Runnable>(10), |
807 |
new NoOpREHandler()); |
808 |
shouldThrow(); |
809 |
} catch (IllegalArgumentException success) {} |
810 |
} |
811 |
|
812 |
/** |
813 |
* Constructor throws if workQueue is set to null |
814 |
*/ |
815 |
public void testConstructorNullPointerException4() { |
816 |
try { |
817 |
new ThreadPoolExecutor(1, 2, |
818 |
LONG_DELAY_MS, MILLISECONDS, |
819 |
(BlockingQueue) null, |
820 |
new NoOpREHandler()); |
821 |
shouldThrow(); |
822 |
} catch (NullPointerException success) {} |
823 |
} |
824 |
|
825 |
/** |
826 |
* Constructor throws if handler is set to null |
827 |
*/ |
828 |
public void testConstructorNullPointerException5() { |
829 |
try { |
830 |
new ThreadPoolExecutor(1, 2, |
831 |
LONG_DELAY_MS, MILLISECONDS, |
832 |
new ArrayBlockingQueue<Runnable>(10), |
833 |
(RejectedExecutionHandler) null); |
834 |
shouldThrow(); |
835 |
} catch (NullPointerException success) {} |
836 |
} |
837 |
|
838 |
/** |
839 |
* Constructor throws if corePoolSize argument is less than zero |
840 |
*/ |
841 |
public void testConstructor16() { |
842 |
try { |
843 |
new ThreadPoolExecutor(-1, 1, |
844 |
LONG_DELAY_MS, MILLISECONDS, |
845 |
new ArrayBlockingQueue<Runnable>(10), |
846 |
new SimpleThreadFactory(), |
847 |
new NoOpREHandler()); |
848 |
shouldThrow(); |
849 |
} catch (IllegalArgumentException success) {} |
850 |
} |
851 |
|
852 |
/** |
853 |
* Constructor throws if maximumPoolSize is less than zero |
854 |
*/ |
855 |
public void testConstructor17() { |
856 |
try { |
857 |
new ThreadPoolExecutor(1, -1, |
858 |
LONG_DELAY_MS, MILLISECONDS, |
859 |
new ArrayBlockingQueue<Runnable>(10), |
860 |
new SimpleThreadFactory(), |
861 |
new NoOpREHandler()); |
862 |
shouldThrow(); |
863 |
} catch (IllegalArgumentException success) {} |
864 |
} |
865 |
|
866 |
/** |
867 |
* Constructor throws if maximumPoolSize is equal to zero |
868 |
*/ |
869 |
public void testConstructor18() { |
870 |
try { |
871 |
new ThreadPoolExecutor(1, 0, |
872 |
LONG_DELAY_MS, MILLISECONDS, |
873 |
new ArrayBlockingQueue<Runnable>(10), |
874 |
new SimpleThreadFactory(), |
875 |
new NoOpREHandler()); |
876 |
shouldThrow(); |
877 |
} catch (IllegalArgumentException success) {} |
878 |
} |
879 |
|
880 |
/** |
881 |
* Constructor throws if keepAliveTime is less than zero |
882 |
*/ |
883 |
public void testConstructor19() { |
884 |
try { |
885 |
new ThreadPoolExecutor(1, 2, |
886 |
-1L, MILLISECONDS, |
887 |
new ArrayBlockingQueue<Runnable>(10), |
888 |
new SimpleThreadFactory(), |
889 |
new NoOpREHandler()); |
890 |
shouldThrow(); |
891 |
} catch (IllegalArgumentException success) {} |
892 |
} |
893 |
|
894 |
/** |
895 |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
896 |
*/ |
897 |
public void testConstructor20() { |
898 |
try { |
899 |
new ThreadPoolExecutor(2, 1, |
900 |
LONG_DELAY_MS, MILLISECONDS, |
901 |
new ArrayBlockingQueue<Runnable>(10), |
902 |
new SimpleThreadFactory(), |
903 |
new NoOpREHandler()); |
904 |
shouldThrow(); |
905 |
} catch (IllegalArgumentException success) {} |
906 |
} |
907 |
|
908 |
/** |
909 |
* Constructor throws if workQueue is null |
910 |
*/ |
911 |
public void testConstructorNullPointerException6() { |
912 |
try { |
913 |
new ThreadPoolExecutor(1, 2, |
914 |
LONG_DELAY_MS, MILLISECONDS, |
915 |
(BlockingQueue) null, |
916 |
new SimpleThreadFactory(), |
917 |
new NoOpREHandler()); |
918 |
shouldThrow(); |
919 |
} catch (NullPointerException success) {} |
920 |
} |
921 |
|
922 |
/** |
923 |
* Constructor throws if handler is null |
924 |
*/ |
925 |
public void testConstructorNullPointerException7() { |
926 |
try { |
927 |
new ThreadPoolExecutor(1, 2, |
928 |
LONG_DELAY_MS, MILLISECONDS, |
929 |
new ArrayBlockingQueue<Runnable>(10), |
930 |
new SimpleThreadFactory(), |
931 |
(RejectedExecutionHandler) null); |
932 |
shouldThrow(); |
933 |
} catch (NullPointerException success) {} |
934 |
} |
935 |
|
936 |
/** |
937 |
* Constructor throws if ThreadFactory is null |
938 |
*/ |
939 |
public void testConstructorNullPointerException8() { |
940 |
try { |
941 |
new ThreadPoolExecutor(1, 2, |
942 |
LONG_DELAY_MS, MILLISECONDS, |
943 |
new ArrayBlockingQueue<Runnable>(10), |
944 |
(ThreadFactory) null, |
945 |
new NoOpREHandler()); |
946 |
shouldThrow(); |
947 |
} catch (NullPointerException success) {} |
948 |
} |
949 |
|
950 |
/** |
951 |
* get of submitted callable throws InterruptedException if interrupted |
952 |
*/ |
953 |
public void testInterruptedSubmit() throws InterruptedException { |
954 |
final ThreadPoolExecutor p = |
955 |
new ThreadPoolExecutor(1, 1, |
956 |
60, TimeUnit.SECONDS, |
957 |
new ArrayBlockingQueue<Runnable>(10)); |
958 |
|
959 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
960 |
final CountDownLatch done = new CountDownLatch(1); |
961 |
try { |
962 |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
963 |
public void realRun() throws Exception { |
964 |
Callable task = new CheckedCallable<Boolean>() { |
965 |
public Boolean realCall() throws InterruptedException { |
966 |
threadStarted.countDown(); |
967 |
done.await(); |
968 |
return Boolean.TRUE; |
969 |
}}; |
970 |
p.submit(task).get(); |
971 |
}}); |
972 |
|
973 |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
974 |
t.interrupt(); |
975 |
awaitTermination(t, MEDIUM_DELAY_MS); |
976 |
} finally { |
977 |
done.countDown(); |
978 |
joinPool(p); |
979 |
} |
980 |
} |
981 |
|
982 |
/** |
983 |
* execute throws RejectedExecutionException if saturated. |
984 |
*/ |
985 |
public void testSaturatedExecute() { |
986 |
ThreadPoolExecutor p = |
987 |
new ThreadPoolExecutor(1, 1, |
988 |
LONG_DELAY_MS, MILLISECONDS, |
989 |
new ArrayBlockingQueue<Runnable>(1)); |
990 |
final CountDownLatch done = new CountDownLatch(1); |
991 |
try { |
992 |
Runnable task = new CheckedRunnable() { |
993 |
public void realRun() throws InterruptedException { |
994 |
done.await(); |
995 |
}}; |
996 |
for (int i = 0; i < 2; ++i) |
997 |
p.execute(task); |
998 |
for (int i = 0; i < 2; ++i) { |
999 |
try { |
1000 |
p.execute(task); |
1001 |
shouldThrow(); |
1002 |
} catch (RejectedExecutionException success) {} |
1003 |
assertTrue(p.getTaskCount() <= 2); |
1004 |
} |
1005 |
} finally { |
1006 |
done.countDown(); |
1007 |
joinPool(p); |
1008 |
} |
1009 |
} |
1010 |
|
1011 |
/** |
1012 |
* submit(runnable) throws RejectedExecutionException if saturated. |
1013 |
*/ |
1014 |
public void testSaturatedSubmitRunnable() { |
1015 |
ThreadPoolExecutor p = |
1016 |
new ThreadPoolExecutor(1, 1, |
1017 |
LONG_DELAY_MS, MILLISECONDS, |
1018 |
new ArrayBlockingQueue<Runnable>(1)); |
1019 |
final CountDownLatch done = new CountDownLatch(1); |
1020 |
try { |
1021 |
Runnable task = new CheckedRunnable() { |
1022 |
public void realRun() throws InterruptedException { |
1023 |
done.await(); |
1024 |
}}; |
1025 |
for (int i = 0; i < 2; ++i) |
1026 |
p.submit(task); |
1027 |
for (int i = 0; i < 2; ++i) { |
1028 |
try { |
1029 |
p.execute(task); |
1030 |
shouldThrow(); |
1031 |
} catch (RejectedExecutionException success) {} |
1032 |
assertTrue(p.getTaskCount() <= 2); |
1033 |
} |
1034 |
} finally { |
1035 |
done.countDown(); |
1036 |
joinPool(p); |
1037 |
} |
1038 |
} |
1039 |
|
1040 |
/** |
1041 |
* submit(callable) throws RejectedExecutionException if saturated. |
1042 |
*/ |
1043 |
public void testSaturatedSubmitCallable() { |
1044 |
ThreadPoolExecutor p = |
1045 |
new ThreadPoolExecutor(1, 1, |
1046 |
LONG_DELAY_MS, MILLISECONDS, |
1047 |
new ArrayBlockingQueue<Runnable>(1)); |
1048 |
final CountDownLatch done = new CountDownLatch(1); |
1049 |
try { |
1050 |
Runnable task = new CheckedRunnable() { |
1051 |
public void realRun() throws InterruptedException { |
1052 |
done.await(); |
1053 |
}}; |
1054 |
for (int i = 0; i < 2; ++i) |
1055 |
p.submit(Executors.callable(task)); |
1056 |
for (int i = 0; i < 2; ++i) { |
1057 |
try { |
1058 |
p.execute(task); |
1059 |
shouldThrow(); |
1060 |
} catch (RejectedExecutionException success) {} |
1061 |
assertTrue(p.getTaskCount() <= 2); |
1062 |
} |
1063 |
} finally { |
1064 |
done.countDown(); |
1065 |
joinPool(p); |
1066 |
} |
1067 |
} |
1068 |
|
1069 |
/** |
1070 |
* executor using CallerRunsPolicy runs task if saturated. |
1071 |
*/ |
1072 |
public void testSaturatedExecute2() { |
1073 |
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
1074 |
final ThreadPoolExecutor p = |
1075 |
new ThreadPoolExecutor(1, 1, |
1076 |
LONG_DELAY_MS, |
1077 |
MILLISECONDS, |
1078 |
new ArrayBlockingQueue<Runnable>(1), |
1079 |
h); |
1080 |
try { |
1081 |
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1082 |
for (int i = 0; i < tasks.length; ++i) |
1083 |
tasks[i] = new TrackedNoOpRunnable(); |
1084 |
TrackedLongRunnable mr = new TrackedLongRunnable(); |
1085 |
p.execute(mr); |
1086 |
for (int i = 0; i < tasks.length; ++i) |
1087 |
p.execute(tasks[i]); |
1088 |
for (int i = 1; i < tasks.length; ++i) |
1089 |
assertTrue(tasks[i].done); |
1090 |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1091 |
} finally { |
1092 |
joinPool(p); |
1093 |
} |
1094 |
} |
1095 |
|
1096 |
/** |
1097 |
* executor using DiscardPolicy drops task if saturated. |
1098 |
*/ |
1099 |
public void testSaturatedExecute3() { |
1100 |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
1101 |
final ThreadPoolExecutor p = |
1102 |
new ThreadPoolExecutor(1, 1, |
1103 |
LONG_DELAY_MS, MILLISECONDS, |
1104 |
new ArrayBlockingQueue<Runnable>(1), |
1105 |
h); |
1106 |
try { |
1107 |
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1108 |
for (int i = 0; i < tasks.length; ++i) |
1109 |
tasks[i] = new TrackedNoOpRunnable(); |
1110 |
p.execute(new TrackedLongRunnable()); |
1111 |
for (TrackedNoOpRunnable task : tasks) |
1112 |
p.execute(task); |
1113 |
for (TrackedNoOpRunnable task : tasks) |
1114 |
assertFalse(task.done); |
1115 |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1116 |
} finally { |
1117 |
joinPool(p); |
1118 |
} |
1119 |
} |
1120 |
|
1121 |
/** |
1122 |
* executor using DiscardOldestPolicy drops oldest task if saturated. |
1123 |
*/ |
1124 |
public void testSaturatedExecute4() { |
1125 |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
1126 |
final ThreadPoolExecutor p = |
1127 |
new ThreadPoolExecutor(1, 1, |
1128 |
LONG_DELAY_MS, MILLISECONDS, |
1129 |
new ArrayBlockingQueue<Runnable>(1), |
1130 |
h); |
1131 |
try { |
1132 |
p.execute(new TrackedLongRunnable()); |
1133 |
TrackedLongRunnable r2 = new TrackedLongRunnable(); |
1134 |
p.execute(r2); |
1135 |
assertTrue(p.getQueue().contains(r2)); |
1136 |
TrackedNoOpRunnable r3 = new TrackedNoOpRunnable(); |
1137 |
p.execute(r3); |
1138 |
assertFalse(p.getQueue().contains(r2)); |
1139 |
assertTrue(p.getQueue().contains(r3)); |
1140 |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1141 |
} finally { |
1142 |
joinPool(p); |
1143 |
} |
1144 |
} |
1145 |
|
1146 |
/** |
1147 |
* execute throws RejectedExecutionException if shutdown |
1148 |
*/ |
1149 |
public void testRejectedExecutionExceptionOnShutdown() { |
1150 |
ThreadPoolExecutor p = |
1151 |
new ThreadPoolExecutor(1, 1, |
1152 |
LONG_DELAY_MS, MILLISECONDS, |
1153 |
new ArrayBlockingQueue<Runnable>(1)); |
1154 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1155 |
try { |
1156 |
p.execute(new NoOpRunnable()); |
1157 |
shouldThrow(); |
1158 |
} catch (RejectedExecutionException success) {} |
1159 |
|
1160 |
joinPool(p); |
1161 |
} |
1162 |
|
1163 |
/** |
1164 |
* execute using CallerRunsPolicy drops task on shutdown |
1165 |
*/ |
1166 |
public void testCallerRunsOnShutdown() { |
1167 |
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
1168 |
final ThreadPoolExecutor p = |
1169 |
new ThreadPoolExecutor(1, 1, |
1170 |
LONG_DELAY_MS, MILLISECONDS, |
1171 |
new ArrayBlockingQueue<Runnable>(1), h); |
1172 |
|
1173 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1174 |
try { |
1175 |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1176 |
p.execute(r); |
1177 |
assertFalse(r.done); |
1178 |
} finally { |
1179 |
joinPool(p); |
1180 |
} |
1181 |
} |
1182 |
|
1183 |
/** |
1184 |
* execute using DiscardPolicy drops task on shutdown |
1185 |
*/ |
1186 |
public void testDiscardOnShutdown() { |
1187 |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
1188 |
ThreadPoolExecutor p = |
1189 |
new ThreadPoolExecutor(1, 1, |
1190 |
LONG_DELAY_MS, MILLISECONDS, |
1191 |
new ArrayBlockingQueue<Runnable>(1), |
1192 |
h); |
1193 |
|
1194 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1195 |
try { |
1196 |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1197 |
p.execute(r); |
1198 |
assertFalse(r.done); |
1199 |
} finally { |
1200 |
joinPool(p); |
1201 |
} |
1202 |
} |
1203 |
|
1204 |
/** |
1205 |
* execute using DiscardOldestPolicy drops task on shutdown |
1206 |
*/ |
1207 |
public void testDiscardOldestOnShutdown() { |
1208 |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
1209 |
ThreadPoolExecutor p = |
1210 |
new ThreadPoolExecutor(1, 1, |
1211 |
LONG_DELAY_MS, MILLISECONDS, |
1212 |
new ArrayBlockingQueue<Runnable>(1), |
1213 |
h); |
1214 |
|
1215 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1216 |
try { |
1217 |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1218 |
p.execute(r); |
1219 |
assertFalse(r.done); |
1220 |
} finally { |
1221 |
joinPool(p); |
1222 |
} |
1223 |
} |
1224 |
|
1225 |
/** |
1226 |
* execute(null) throws NPE |
1227 |
*/ |
1228 |
public void testExecuteNull() { |
1229 |
ThreadPoolExecutor p = |
1230 |
new ThreadPoolExecutor(1, 2, |
1231 |
LONG_DELAY_MS, MILLISECONDS, |
1232 |
new ArrayBlockingQueue<Runnable>(10)); |
1233 |
try { |
1234 |
p.execute(null); |
1235 |
shouldThrow(); |
1236 |
} catch (NullPointerException success) {} |
1237 |
|
1238 |
joinPool(p); |
1239 |
} |
1240 |
|
1241 |
/** |
1242 |
* setCorePoolSize of negative value throws IllegalArgumentException |
1243 |
*/ |
1244 |
public void testCorePoolSizeIllegalArgumentException() { |
1245 |
ThreadPoolExecutor p = |
1246 |
new ThreadPoolExecutor(1, 2, |
1247 |
LONG_DELAY_MS, MILLISECONDS, |
1248 |
new ArrayBlockingQueue<Runnable>(10)); |
1249 |
try { |
1250 |
p.setCorePoolSize(-1); |
1251 |
shouldThrow(); |
1252 |
} catch (IllegalArgumentException success) { |
1253 |
} finally { |
1254 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1255 |
} |
1256 |
joinPool(p); |
1257 |
} |
1258 |
|
1259 |
/** |
1260 |
* setMaximumPoolSize(int) throws IllegalArgumentException if |
1261 |
* given a value less the core pool size |
1262 |
*/ |
1263 |
public void testMaximumPoolSizeIllegalArgumentException() { |
1264 |
ThreadPoolExecutor p = |
1265 |
new ThreadPoolExecutor(2, 3, |
1266 |
LONG_DELAY_MS, MILLISECONDS, |
1267 |
new ArrayBlockingQueue<Runnable>(10)); |
1268 |
try { |
1269 |
p.setMaximumPoolSize(1); |
1270 |
shouldThrow(); |
1271 |
} catch (IllegalArgumentException success) { |
1272 |
} finally { |
1273 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1274 |
} |
1275 |
joinPool(p); |
1276 |
} |
1277 |
|
1278 |
/** |
1279 |
* setMaximumPoolSize throws IllegalArgumentException |
1280 |
* if given a negative value |
1281 |
*/ |
1282 |
public void testMaximumPoolSizeIllegalArgumentException2() { |
1283 |
ThreadPoolExecutor p = |
1284 |
new ThreadPoolExecutor(2, 3, |
1285 |
LONG_DELAY_MS, MILLISECONDS, |
1286 |
new ArrayBlockingQueue<Runnable>(10)); |
1287 |
try { |
1288 |
p.setMaximumPoolSize(-1); |
1289 |
shouldThrow(); |
1290 |
} catch (IllegalArgumentException success) { |
1291 |
} finally { |
1292 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1293 |
} |
1294 |
joinPool(p); |
1295 |
} |
1296 |
|
1297 |
/** |
1298 |
* setKeepAliveTime throws IllegalArgumentException |
1299 |
* when given a negative value |
1300 |
*/ |
1301 |
public void testKeepAliveTimeIllegalArgumentException() { |
1302 |
ThreadPoolExecutor p = |
1303 |
new ThreadPoolExecutor(2, 3, |
1304 |
LONG_DELAY_MS, MILLISECONDS, |
1305 |
new ArrayBlockingQueue<Runnable>(10)); |
1306 |
try { |
1307 |
p.setKeepAliveTime(-1,MILLISECONDS); |
1308 |
shouldThrow(); |
1309 |
} catch (IllegalArgumentException success) { |
1310 |
} finally { |
1311 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1312 |
} |
1313 |
joinPool(p); |
1314 |
} |
1315 |
|
1316 |
/** |
1317 |
* terminated() is called on termination |
1318 |
*/ |
1319 |
public void testTerminated() { |
1320 |
ExtendedTPE p = new ExtendedTPE(); |
1321 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1322 |
assertTrue(p.terminatedCalled); |
1323 |
joinPool(p); |
1324 |
} |
1325 |
|
1326 |
/** |
1327 |
* beforeExecute and afterExecute are called when executing task |
1328 |
*/ |
1329 |
public void testBeforeAfter() throws InterruptedException { |
1330 |
ExtendedTPE p = new ExtendedTPE(); |
1331 |
try { |
1332 |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1333 |
p.execute(r); |
1334 |
delay(SHORT_DELAY_MS); |
1335 |
assertTrue(r.done); |
1336 |
assertTrue(p.beforeCalled); |
1337 |
assertTrue(p.afterCalled); |
1338 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1339 |
} finally { |
1340 |
joinPool(p); |
1341 |
} |
1342 |
} |
1343 |
|
1344 |
/** |
1345 |
* completed submit of callable returns result |
1346 |
*/ |
1347 |
public void testSubmitCallable() throws Exception { |
1348 |
ExecutorService e = |
1349 |
new ThreadPoolExecutor(2, 2, |
1350 |
LONG_DELAY_MS, MILLISECONDS, |
1351 |
new ArrayBlockingQueue<Runnable>(10)); |
1352 |
try { |
1353 |
Future<String> future = e.submit(new StringTask()); |
1354 |
String result = future.get(); |
1355 |
assertSame(TEST_STRING, result); |
1356 |
} finally { |
1357 |
joinPool(e); |
1358 |
} |
1359 |
} |
1360 |
|
1361 |
/** |
1362 |
* completed submit of runnable returns successfully |
1363 |
*/ |
1364 |
public void testSubmitRunnable() throws Exception { |
1365 |
ExecutorService e = |
1366 |
new ThreadPoolExecutor(2, 2, |
1367 |
LONG_DELAY_MS, MILLISECONDS, |
1368 |
new ArrayBlockingQueue<Runnable>(10)); |
1369 |
try { |
1370 |
Future<?> future = e.submit(new NoOpRunnable()); |
1371 |
future.get(); |
1372 |
assertTrue(future.isDone()); |
1373 |
} finally { |
1374 |
joinPool(e); |
1375 |
} |
1376 |
} |
1377 |
|
1378 |
/** |
1379 |
* completed submit of (runnable, result) returns result |
1380 |
*/ |
1381 |
public void testSubmitRunnable2() throws Exception { |
1382 |
ExecutorService e = |
1383 |
new ThreadPoolExecutor(2, 2, |
1384 |
LONG_DELAY_MS, MILLISECONDS, |
1385 |
new ArrayBlockingQueue<Runnable>(10)); |
1386 |
try { |
1387 |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1388 |
String result = future.get(); |
1389 |
assertSame(TEST_STRING, result); |
1390 |
} finally { |
1391 |
joinPool(e); |
1392 |
} |
1393 |
} |
1394 |
|
1395 |
/** |
1396 |
* invokeAny(null) throws NPE |
1397 |
*/ |
1398 |
public void testInvokeAny1() throws Exception { |
1399 |
ExecutorService e = |
1400 |
new ThreadPoolExecutor(2, 2, |
1401 |
LONG_DELAY_MS, MILLISECONDS, |
1402 |
new ArrayBlockingQueue<Runnable>(10)); |
1403 |
try { |
1404 |
e.invokeAny(null); |
1405 |
shouldThrow(); |
1406 |
} catch (NullPointerException success) { |
1407 |
} finally { |
1408 |
joinPool(e); |
1409 |
} |
1410 |
} |
1411 |
|
1412 |
/** |
1413 |
* invokeAny(empty collection) throws IAE |
1414 |
*/ |
1415 |
public void testInvokeAny2() throws Exception { |
1416 |
ExecutorService e = |
1417 |
new ThreadPoolExecutor(2, 2, |
1418 |
LONG_DELAY_MS, MILLISECONDS, |
1419 |
new ArrayBlockingQueue<Runnable>(10)); |
1420 |
try { |
1421 |
e.invokeAny(new ArrayList<Callable<String>>()); |
1422 |
shouldThrow(); |
1423 |
} catch (IllegalArgumentException success) { |
1424 |
} finally { |
1425 |
joinPool(e); |
1426 |
} |
1427 |
} |
1428 |
|
1429 |
/** |
1430 |
* invokeAny(c) throws NPE if c has null elements |
1431 |
*/ |
1432 |
public void testInvokeAny3() throws Exception { |
1433 |
final CountDownLatch latch = new CountDownLatch(1); |
1434 |
final ExecutorService e = |
1435 |
new ThreadPoolExecutor(2, 2, |
1436 |
LONG_DELAY_MS, MILLISECONDS, |
1437 |
new ArrayBlockingQueue<Runnable>(10)); |
1438 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1439 |
l.add(latchAwaitingStringTask(latch)); |
1440 |
l.add(null); |
1441 |
try { |
1442 |
e.invokeAny(l); |
1443 |
shouldThrow(); |
1444 |
} catch (NullPointerException success) { |
1445 |
} finally { |
1446 |
latch.countDown(); |
1447 |
joinPool(e); |
1448 |
} |
1449 |
} |
1450 |
|
1451 |
/** |
1452 |
* invokeAny(c) throws ExecutionException if no task completes |
1453 |
*/ |
1454 |
public void testInvokeAny4() throws Exception { |
1455 |
ExecutorService e = |
1456 |
new ThreadPoolExecutor(2, 2, |
1457 |
LONG_DELAY_MS, MILLISECONDS, |
1458 |
new ArrayBlockingQueue<Runnable>(10)); |
1459 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1460 |
l.add(new NPETask()); |
1461 |
try { |
1462 |
e.invokeAny(l); |
1463 |
shouldThrow(); |
1464 |
} catch (ExecutionException success) { |
1465 |
assertTrue(success.getCause() instanceof NullPointerException); |
1466 |
} finally { |
1467 |
joinPool(e); |
1468 |
} |
1469 |
} |
1470 |
|
1471 |
/** |
1472 |
* invokeAny(c) returns result of some task |
1473 |
*/ |
1474 |
public void testInvokeAny5() throws Exception { |
1475 |
ExecutorService e = |
1476 |
new ThreadPoolExecutor(2, 2, |
1477 |
LONG_DELAY_MS, MILLISECONDS, |
1478 |
new ArrayBlockingQueue<Runnable>(10)); |
1479 |
try { |
1480 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1481 |
l.add(new StringTask()); |
1482 |
l.add(new StringTask()); |
1483 |
String result = e.invokeAny(l); |
1484 |
assertSame(TEST_STRING, result); |
1485 |
} finally { |
1486 |
joinPool(e); |
1487 |
} |
1488 |
} |
1489 |
|
1490 |
/** |
1491 |
* invokeAll(null) throws NPE |
1492 |
*/ |
1493 |
public void testInvokeAll1() throws Exception { |
1494 |
ExecutorService e = |
1495 |
new ThreadPoolExecutor(2, 2, |
1496 |
LONG_DELAY_MS, MILLISECONDS, |
1497 |
new ArrayBlockingQueue<Runnable>(10)); |
1498 |
try { |
1499 |
e.invokeAll(null); |
1500 |
shouldThrow(); |
1501 |
} catch (NullPointerException success) { |
1502 |
} finally { |
1503 |
joinPool(e); |
1504 |
} |
1505 |
} |
1506 |
|
1507 |
/** |
1508 |
* invokeAll(empty collection) returns empty collection |
1509 |
*/ |
1510 |
public void testInvokeAll2() throws InterruptedException { |
1511 |
ExecutorService e = |
1512 |
new ThreadPoolExecutor(2, 2, |
1513 |
LONG_DELAY_MS, MILLISECONDS, |
1514 |
new ArrayBlockingQueue<Runnable>(10)); |
1515 |
try { |
1516 |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1517 |
assertTrue(r.isEmpty()); |
1518 |
} finally { |
1519 |
joinPool(e); |
1520 |
} |
1521 |
} |
1522 |
|
1523 |
/** |
1524 |
* invokeAll(c) throws NPE if c has null elements |
1525 |
*/ |
1526 |
public void testInvokeAll3() throws Exception { |
1527 |
ExecutorService e = |
1528 |
new ThreadPoolExecutor(2, 2, |
1529 |
LONG_DELAY_MS, MILLISECONDS, |
1530 |
new ArrayBlockingQueue<Runnable>(10)); |
1531 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1532 |
l.add(new StringTask()); |
1533 |
l.add(null); |
1534 |
try { |
1535 |
e.invokeAll(l); |
1536 |
shouldThrow(); |
1537 |
} catch (NullPointerException success) { |
1538 |
} finally { |
1539 |
joinPool(e); |
1540 |
} |
1541 |
} |
1542 |
|
1543 |
/** |
1544 |
* get of element of invokeAll(c) throws exception on failed task |
1545 |
*/ |
1546 |
public void testInvokeAll4() throws Exception { |
1547 |
ExecutorService e = |
1548 |
new ThreadPoolExecutor(2, 2, |
1549 |
LONG_DELAY_MS, MILLISECONDS, |
1550 |
new ArrayBlockingQueue<Runnable>(10)); |
1551 |
try { |
1552 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1553 |
l.add(new NPETask()); |
1554 |
List<Future<String>> futures = e.invokeAll(l); |
1555 |
assertEquals(1, futures.size()); |
1556 |
try { |
1557 |
futures.get(0).get(); |
1558 |
shouldThrow(); |
1559 |
} catch (ExecutionException success) { |
1560 |
assertTrue(success.getCause() instanceof NullPointerException); |
1561 |
} |
1562 |
} finally { |
1563 |
joinPool(e); |
1564 |
} |
1565 |
} |
1566 |
|
1567 |
/** |
1568 |
* invokeAll(c) returns results of all completed tasks |
1569 |
*/ |
1570 |
public void testInvokeAll5() throws Exception { |
1571 |
ExecutorService e = |
1572 |
new ThreadPoolExecutor(2, 2, |
1573 |
LONG_DELAY_MS, MILLISECONDS, |
1574 |
new ArrayBlockingQueue<Runnable>(10)); |
1575 |
try { |
1576 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1577 |
l.add(new StringTask()); |
1578 |
l.add(new StringTask()); |
1579 |
List<Future<String>> futures = e.invokeAll(l); |
1580 |
assertEquals(2, futures.size()); |
1581 |
for (Future<String> future : futures) |
1582 |
assertSame(TEST_STRING, future.get()); |
1583 |
} finally { |
1584 |
joinPool(e); |
1585 |
} |
1586 |
} |
1587 |
|
1588 |
/** |
1589 |
* timed invokeAny(null) throws NPE |
1590 |
*/ |
1591 |
public void testTimedInvokeAny1() throws Exception { |
1592 |
ExecutorService e = |
1593 |
new ThreadPoolExecutor(2, 2, |
1594 |
LONG_DELAY_MS, MILLISECONDS, |
1595 |
new ArrayBlockingQueue<Runnable>(10)); |
1596 |
try { |
1597 |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1598 |
shouldThrow(); |
1599 |
} catch (NullPointerException success) { |
1600 |
} finally { |
1601 |
joinPool(e); |
1602 |
} |
1603 |
} |
1604 |
|
1605 |
/** |
1606 |
* timed invokeAny(,,null) throws NPE |
1607 |
*/ |
1608 |
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1609 |
ExecutorService e = |
1610 |
new ThreadPoolExecutor(2, 2, |
1611 |
LONG_DELAY_MS, MILLISECONDS, |
1612 |
new ArrayBlockingQueue<Runnable>(10)); |
1613 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1614 |
l.add(new StringTask()); |
1615 |
try { |
1616 |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1617 |
shouldThrow(); |
1618 |
} catch (NullPointerException success) { |
1619 |
} finally { |
1620 |
joinPool(e); |
1621 |
} |
1622 |
} |
1623 |
|
1624 |
/** |
1625 |
* timed invokeAny(empty collection) throws IAE |
1626 |
*/ |
1627 |
public void testTimedInvokeAny2() throws Exception { |
1628 |
ExecutorService e = |
1629 |
new ThreadPoolExecutor(2, 2, |
1630 |
LONG_DELAY_MS, MILLISECONDS, |
1631 |
new ArrayBlockingQueue<Runnable>(10)); |
1632 |
try { |
1633 |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1634 |
shouldThrow(); |
1635 |
} catch (IllegalArgumentException success) { |
1636 |
} finally { |
1637 |
joinPool(e); |
1638 |
} |
1639 |
} |
1640 |
|
1641 |
/** |
1642 |
* timed invokeAny(c) throws NPE if c has null elements |
1643 |
*/ |
1644 |
public void testTimedInvokeAny3() throws Exception { |
1645 |
final CountDownLatch latch = new CountDownLatch(1); |
1646 |
final ExecutorService e = |
1647 |
new ThreadPoolExecutor(2, 2, |
1648 |
LONG_DELAY_MS, MILLISECONDS, |
1649 |
new ArrayBlockingQueue<Runnable>(10)); |
1650 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1651 |
l.add(latchAwaitingStringTask(latch)); |
1652 |
l.add(null); |
1653 |
try { |
1654 |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1655 |
shouldThrow(); |
1656 |
} catch (NullPointerException success) { |
1657 |
} finally { |
1658 |
latch.countDown(); |
1659 |
joinPool(e); |
1660 |
} |
1661 |
} |
1662 |
|
1663 |
/** |
1664 |
* timed invokeAny(c) throws ExecutionException if no task completes |
1665 |
*/ |
1666 |
public void testTimedInvokeAny4() throws Exception { |
1667 |
ExecutorService e = |
1668 |
new ThreadPoolExecutor(2, 2, |
1669 |
LONG_DELAY_MS, MILLISECONDS, |
1670 |
new ArrayBlockingQueue<Runnable>(10)); |
1671 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1672 |
l.add(new NPETask()); |
1673 |
try { |
1674 |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1675 |
shouldThrow(); |
1676 |
} catch (ExecutionException success) { |
1677 |
assertTrue(success.getCause() instanceof NullPointerException); |
1678 |
} finally { |
1679 |
joinPool(e); |
1680 |
} |
1681 |
} |
1682 |
|
1683 |
/** |
1684 |
* timed invokeAny(c) returns result of some task |
1685 |
*/ |
1686 |
public void testTimedInvokeAny5() throws Exception { |
1687 |
ExecutorService e = |
1688 |
new ThreadPoolExecutor(2, 2, |
1689 |
LONG_DELAY_MS, MILLISECONDS, |
1690 |
new ArrayBlockingQueue<Runnable>(10)); |
1691 |
try { |
1692 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1693 |
l.add(new StringTask()); |
1694 |
l.add(new StringTask()); |
1695 |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1696 |
assertSame(TEST_STRING, result); |
1697 |
} finally { |
1698 |
joinPool(e); |
1699 |
} |
1700 |
} |
1701 |
|
1702 |
/** |
1703 |
* timed invokeAll(null) throws NPE |
1704 |
*/ |
1705 |
public void testTimedInvokeAll1() throws Exception { |
1706 |
ExecutorService e = |
1707 |
new ThreadPoolExecutor(2, 2, |
1708 |
LONG_DELAY_MS, MILLISECONDS, |
1709 |
new ArrayBlockingQueue<Runnable>(10)); |
1710 |
try { |
1711 |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1712 |
shouldThrow(); |
1713 |
} catch (NullPointerException success) { |
1714 |
} finally { |
1715 |
joinPool(e); |
1716 |
} |
1717 |
} |
1718 |
|
1719 |
/** |
1720 |
* timed invokeAll(,,null) throws NPE |
1721 |
*/ |
1722 |
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1723 |
ExecutorService e = |
1724 |
new ThreadPoolExecutor(2, 2, |
1725 |
LONG_DELAY_MS, MILLISECONDS, |
1726 |
new ArrayBlockingQueue<Runnable>(10)); |
1727 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1728 |
l.add(new StringTask()); |
1729 |
try { |
1730 |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1731 |
shouldThrow(); |
1732 |
} catch (NullPointerException success) { |
1733 |
} finally { |
1734 |
joinPool(e); |
1735 |
} |
1736 |
} |
1737 |
|
1738 |
/** |
1739 |
* timed invokeAll(empty collection) returns empty collection |
1740 |
*/ |
1741 |
public void testTimedInvokeAll2() throws InterruptedException { |
1742 |
ExecutorService e = |
1743 |
new ThreadPoolExecutor(2, 2, |
1744 |
LONG_DELAY_MS, MILLISECONDS, |
1745 |
new ArrayBlockingQueue<Runnable>(10)); |
1746 |
try { |
1747 |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1748 |
assertTrue(r.isEmpty()); |
1749 |
} finally { |
1750 |
joinPool(e); |
1751 |
} |
1752 |
} |
1753 |
|
1754 |
/** |
1755 |
* timed invokeAll(c) throws NPE if c has null elements |
1756 |
*/ |
1757 |
public void testTimedInvokeAll3() throws Exception { |
1758 |
ExecutorService e = |
1759 |
new ThreadPoolExecutor(2, 2, |
1760 |
LONG_DELAY_MS, MILLISECONDS, |
1761 |
new ArrayBlockingQueue<Runnable>(10)); |
1762 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1763 |
l.add(new StringTask()); |
1764 |
l.add(null); |
1765 |
try { |
1766 |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1767 |
shouldThrow(); |
1768 |
} catch (NullPointerException success) { |
1769 |
} finally { |
1770 |
joinPool(e); |
1771 |
} |
1772 |
} |
1773 |
|
1774 |
/** |
1775 |
* get of element of invokeAll(c) throws exception on failed task |
1776 |
*/ |
1777 |
public void testTimedInvokeAll4() throws Exception { |
1778 |
ExecutorService e = |
1779 |
new ThreadPoolExecutor(2, 2, |
1780 |
LONG_DELAY_MS, MILLISECONDS, |
1781 |
new ArrayBlockingQueue<Runnable>(10)); |
1782 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1783 |
l.add(new NPETask()); |
1784 |
List<Future<String>> futures = |
1785 |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1786 |
assertEquals(1, futures.size()); |
1787 |
try { |
1788 |
futures.get(0).get(); |
1789 |
shouldThrow(); |
1790 |
} catch (ExecutionException success) { |
1791 |
assertTrue(success.getCause() instanceof NullPointerException); |
1792 |
} finally { |
1793 |
joinPool(e); |
1794 |
} |
1795 |
} |
1796 |
|
1797 |
/** |
1798 |
* timed invokeAll(c) returns results of all completed tasks |
1799 |
*/ |
1800 |
public void testTimedInvokeAll5() throws Exception { |
1801 |
ExecutorService e = |
1802 |
new ThreadPoolExecutor(2, 2, |
1803 |
LONG_DELAY_MS, MILLISECONDS, |
1804 |
new ArrayBlockingQueue<Runnable>(10)); |
1805 |
try { |
1806 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1807 |
l.add(new StringTask()); |
1808 |
l.add(new StringTask()); |
1809 |
List<Future<String>> futures = |
1810 |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1811 |
assertEquals(2, futures.size()); |
1812 |
for (Future<String> future : futures) |
1813 |
assertSame(TEST_STRING, future.get()); |
1814 |
} finally { |
1815 |
joinPool(e); |
1816 |
} |
1817 |
} |
1818 |
|
1819 |
/** |
1820 |
* timed invokeAll(c) cancels tasks not completed by timeout |
1821 |
*/ |
1822 |
public void testTimedInvokeAll6() throws Exception { |
1823 |
ExecutorService e = |
1824 |
new ThreadPoolExecutor(2, 2, |
1825 |
LONG_DELAY_MS, MILLISECONDS, |
1826 |
new ArrayBlockingQueue<Runnable>(10)); |
1827 |
try { |
1828 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1829 |
l.add(new StringTask()); |
1830 |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1831 |
l.add(new StringTask()); |
1832 |
List<Future<String>> futures = |
1833 |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1834 |
assertEquals(3, futures.size()); |
1835 |
Iterator<Future<String>> it = futures.iterator(); |
1836 |
Future<String> f1 = it.next(); |
1837 |
Future<String> f2 = it.next(); |
1838 |
Future<String> f3 = it.next(); |
1839 |
assertTrue(f1.isDone()); |
1840 |
assertTrue(f2.isDone()); |
1841 |
assertTrue(f3.isDone()); |
1842 |
assertFalse(f1.isCancelled()); |
1843 |
assertTrue(f2.isCancelled()); |
1844 |
} finally { |
1845 |
joinPool(e); |
1846 |
} |
1847 |
} |
1848 |
|
1849 |
/** |
1850 |
* Execution continues if there is at least one thread even if |
1851 |
* thread factory fails to create more |
1852 |
*/ |
1853 |
public void testFailingThreadFactory() throws InterruptedException { |
1854 |
final ExecutorService e = |
1855 |
new ThreadPoolExecutor(100, 100, |
1856 |
LONG_DELAY_MS, MILLISECONDS, |
1857 |
new LinkedBlockingQueue<Runnable>(), |
1858 |
new FailingThreadFactory()); |
1859 |
try { |
1860 |
final int TASKS = 100; |
1861 |
final CountDownLatch done = new CountDownLatch(TASKS); |
1862 |
for (int k = 0; k < TASKS; ++k) |
1863 |
e.execute(new CheckedRunnable() { |
1864 |
public void realRun() { |
1865 |
done.countDown(); |
1866 |
}}); |
1867 |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1868 |
} finally { |
1869 |
joinPool(e); |
1870 |
} |
1871 |
} |
1872 |
|
1873 |
/** |
1874 |
* allowsCoreThreadTimeOut is by default false. |
1875 |
*/ |
1876 |
public void testAllowsCoreThreadTimeOut() { |
1877 |
final ThreadPoolExecutor p = |
1878 |
new ThreadPoolExecutor(2, 2, |
1879 |
1000, MILLISECONDS, |
1880 |
new ArrayBlockingQueue<Runnable>(10)); |
1881 |
assertFalse(p.allowsCoreThreadTimeOut()); |
1882 |
joinPool(p); |
1883 |
} |
1884 |
|
1885 |
/** |
1886 |
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1887 |
*/ |
1888 |
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1889 |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1890 |
final ThreadPoolExecutor p = |
1891 |
new ThreadPoolExecutor(2, 10, |
1892 |
coreThreadTimeOut, MILLISECONDS, |
1893 |
new ArrayBlockingQueue<Runnable>(10)); |
1894 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1895 |
try { |
1896 |
p.allowCoreThreadTimeOut(true); |
1897 |
p.execute(new CheckedRunnable() { |
1898 |
public void realRun() { |
1899 |
threadStarted.countDown(); |
1900 |
assertEquals(1, p.getPoolSize()); |
1901 |
}}); |
1902 |
await(threadStarted); |
1903 |
delay(coreThreadTimeOut); |
1904 |
long startTime = System.nanoTime(); |
1905 |
while (p.getPoolSize() > 0 |
1906 |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1907 |
Thread.yield(); |
1908 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1909 |
assertEquals(0, p.getPoolSize()); |
1910 |
} finally { |
1911 |
joinPool(p); |
1912 |
} |
1913 |
} |
1914 |
|
1915 |
/** |
1916 |
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1917 |
*/ |
1918 |
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1919 |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1920 |
final ThreadPoolExecutor p = |
1921 |
new ThreadPoolExecutor(2, 10, |
1922 |
coreThreadTimeOut, MILLISECONDS, |
1923 |
new ArrayBlockingQueue<Runnable>(10)); |
1924 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1925 |
try { |
1926 |
p.allowCoreThreadTimeOut(false); |
1927 |
p.execute(new CheckedRunnable() { |
1928 |
public void realRun() throws InterruptedException { |
1929 |
threadStarted.countDown(); |
1930 |
assertTrue(p.getPoolSize() >= 1); |
1931 |
}}); |
1932 |
delay(2 * coreThreadTimeOut); |
1933 |
assertTrue(p.getPoolSize() >= 1); |
1934 |
} finally { |
1935 |
joinPool(p); |
1936 |
} |
1937 |
} |
1938 |
|
1939 |
/** |
1940 |
* execute allows the same task to be submitted multiple times, even |
1941 |
* if rejected |
1942 |
*/ |
1943 |
public void testRejectedRecycledTask() throws InterruptedException { |
1944 |
final int nTasks = 1000; |
1945 |
final CountDownLatch done = new CountDownLatch(nTasks); |
1946 |
final Runnable recycledTask = new Runnable() { |
1947 |
public void run() { |
1948 |
done.countDown(); |
1949 |
}}; |
1950 |
final ThreadPoolExecutor p = |
1951 |
new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS, |
1952 |
new ArrayBlockingQueue(30)); |
1953 |
try { |
1954 |
for (int i = 0; i < nTasks; ++i) { |
1955 |
for (;;) { |
1956 |
try { |
1957 |
p.execute(recycledTask); |
1958 |
break; |
1959 |
} |
1960 |
catch (RejectedExecutionException ignore) {} |
1961 |
} |
1962 |
} |
1963 |
// enough time to run all tasks |
1964 |
assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); |
1965 |
} finally { |
1966 |
p.shutdown(); |
1967 |
} |
1968 |
} |
1969 |
|
1970 |
} |