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/licenses/publicdomain |
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 |
/** |
51 |
* execute successfully executes a runnable |
52 |
*/ |
53 |
public void testExecute() throws InterruptedException { |
54 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
55 |
try { |
56 |
p1.execute(new ShortRunnable()); |
57 |
Thread.sleep(SMALL_DELAY_MS); |
58 |
} finally { |
59 |
joinPool(p1); |
60 |
} |
61 |
} |
62 |
|
63 |
/** |
64 |
* getActiveCount increases but doesn't overestimate, when a |
65 |
* thread becomes active |
66 |
*/ |
67 |
public void testGetActiveCount() throws InterruptedException { |
68 |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
69 |
assertEquals(0, p2.getActiveCount()); |
70 |
p2.execute(new MediumRunnable()); |
71 |
Thread.sleep(SHORT_DELAY_MS); |
72 |
assertEquals(1, p2.getActiveCount()); |
73 |
joinPool(p2); |
74 |
} |
75 |
|
76 |
/** |
77 |
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
78 |
*/ |
79 |
public void testPrestartCoreThread() { |
80 |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
81 |
assertEquals(0, p2.getPoolSize()); |
82 |
assertTrue(p2.prestartCoreThread()); |
83 |
assertEquals(1, p2.getPoolSize()); |
84 |
assertTrue(p2.prestartCoreThread()); |
85 |
assertEquals(2, p2.getPoolSize()); |
86 |
assertFalse(p2.prestartCoreThread()); |
87 |
assertEquals(2, p2.getPoolSize()); |
88 |
joinPool(p2); |
89 |
} |
90 |
|
91 |
/** |
92 |
* prestartAllCoreThreads starts all corePoolSize threads |
93 |
*/ |
94 |
public void testPrestartAllCoreThreads() { |
95 |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
96 |
assertEquals(0, p2.getPoolSize()); |
97 |
p2.prestartAllCoreThreads(); |
98 |
assertEquals(2, p2.getPoolSize()); |
99 |
p2.prestartAllCoreThreads(); |
100 |
assertEquals(2, p2.getPoolSize()); |
101 |
joinPool(p2); |
102 |
} |
103 |
|
104 |
/** |
105 |
* getCompletedTaskCount increases, but doesn't overestimate, |
106 |
* when tasks complete |
107 |
*/ |
108 |
public void testGetCompletedTaskCount() throws InterruptedException { |
109 |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
110 |
assertEquals(0, p2.getCompletedTaskCount()); |
111 |
p2.execute(new ShortRunnable()); |
112 |
Thread.sleep(SMALL_DELAY_MS); |
113 |
assertEquals(1, p2.getCompletedTaskCount()); |
114 |
try { p2.shutdown(); } catch (SecurityException ok) { return; } |
115 |
joinPool(p2); |
116 |
} |
117 |
|
118 |
/** |
119 |
* getCorePoolSize returns size given in constructor if not otherwise set |
120 |
*/ |
121 |
public void testGetCorePoolSize() { |
122 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
123 |
assertEquals(1, p1.getCorePoolSize()); |
124 |
joinPool(p1); |
125 |
} |
126 |
|
127 |
/** |
128 |
* getKeepAliveTime returns value given in constructor if not otherwise set |
129 |
*/ |
130 |
public void testGetKeepAliveTime() { |
131 |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
132 |
assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS)); |
133 |
joinPool(p2); |
134 |
} |
135 |
|
136 |
|
137 |
/** |
138 |
* getThreadFactory returns factory in constructor if not set |
139 |
*/ |
140 |
public void testGetThreadFactory() { |
141 |
ThreadFactory tf = new SimpleThreadFactory(); |
142 |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler()); |
143 |
assertSame(tf, p.getThreadFactory()); |
144 |
joinPool(p); |
145 |
} |
146 |
|
147 |
/** |
148 |
* setThreadFactory sets the thread factory returned by getThreadFactory |
149 |
*/ |
150 |
public void testSetThreadFactory() { |
151 |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
152 |
ThreadFactory tf = new SimpleThreadFactory(); |
153 |
p.setThreadFactory(tf); |
154 |
assertSame(tf, p.getThreadFactory()); |
155 |
joinPool(p); |
156 |
} |
157 |
|
158 |
|
159 |
/** |
160 |
* setThreadFactory(null) throws NPE |
161 |
*/ |
162 |
public void testSetThreadFactoryNull() { |
163 |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
164 |
try { |
165 |
p.setThreadFactory(null); |
166 |
shouldThrow(); |
167 |
} catch (NullPointerException success) { |
168 |
} finally { |
169 |
joinPool(p); |
170 |
} |
171 |
} |
172 |
|
173 |
/** |
174 |
* getRejectedExecutionHandler returns handler in constructor if not set |
175 |
*/ |
176 |
public void testGetRejectedExecutionHandler() { |
177 |
RejectedExecutionHandler h = new NoOpREHandler(); |
178 |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h); |
179 |
assertSame(h, p.getRejectedExecutionHandler()); |
180 |
joinPool(p); |
181 |
} |
182 |
|
183 |
/** |
184 |
* setRejectedExecutionHandler sets the handler returned by |
185 |
* getRejectedExecutionHandler |
186 |
*/ |
187 |
public void testSetRejectedExecutionHandler() { |
188 |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
189 |
RejectedExecutionHandler h = new NoOpREHandler(); |
190 |
p.setRejectedExecutionHandler(h); |
191 |
assertSame(h, p.getRejectedExecutionHandler()); |
192 |
joinPool(p); |
193 |
} |
194 |
|
195 |
|
196 |
/** |
197 |
* setRejectedExecutionHandler(null) throws NPE |
198 |
*/ |
199 |
public void testSetRejectedExecutionHandlerNull() { |
200 |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
201 |
try { |
202 |
p.setRejectedExecutionHandler(null); |
203 |
shouldThrow(); |
204 |
} catch (NullPointerException success) { |
205 |
} finally { |
206 |
joinPool(p); |
207 |
} |
208 |
} |
209 |
|
210 |
|
211 |
/** |
212 |
* getLargestPoolSize increases, but doesn't overestimate, when |
213 |
* multiple threads active |
214 |
*/ |
215 |
public void testGetLargestPoolSize() throws InterruptedException { |
216 |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
217 |
assertEquals(0, p2.getLargestPoolSize()); |
218 |
p2.execute(new MediumRunnable()); |
219 |
p2.execute(new MediumRunnable()); |
220 |
Thread.sleep(SHORT_DELAY_MS); |
221 |
assertEquals(2, p2.getLargestPoolSize()); |
222 |
joinPool(p2); |
223 |
} |
224 |
|
225 |
/** |
226 |
* getMaximumPoolSize returns value given in constructor if not |
227 |
* otherwise set |
228 |
*/ |
229 |
public void testGetMaximumPoolSize() { |
230 |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
231 |
assertEquals(2, p2.getMaximumPoolSize()); |
232 |
joinPool(p2); |
233 |
} |
234 |
|
235 |
/** |
236 |
* getPoolSize increases, but doesn't overestimate, when threads |
237 |
* become active |
238 |
*/ |
239 |
public void testGetPoolSize() { |
240 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
241 |
assertEquals(0, p1.getPoolSize()); |
242 |
p1.execute(new MediumRunnable()); |
243 |
assertEquals(1, p1.getPoolSize()); |
244 |
joinPool(p1); |
245 |
} |
246 |
|
247 |
/** |
248 |
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
249 |
*/ |
250 |
public void testGetTaskCount() throws InterruptedException { |
251 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
252 |
assertEquals(0, p1.getTaskCount()); |
253 |
p1.execute(new MediumRunnable()); |
254 |
Thread.sleep(SHORT_DELAY_MS); |
255 |
assertEquals(1, p1.getTaskCount()); |
256 |
joinPool(p1); |
257 |
} |
258 |
|
259 |
/** |
260 |
* isShutDown is false before shutdown, true after |
261 |
*/ |
262 |
public void testIsShutdown() { |
263 |
|
264 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
265 |
assertFalse(p1.isShutdown()); |
266 |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
267 |
assertTrue(p1.isShutdown()); |
268 |
joinPool(p1); |
269 |
} |
270 |
|
271 |
|
272 |
/** |
273 |
* isTerminated is false before termination, true after |
274 |
*/ |
275 |
public void testIsTerminated() throws InterruptedException { |
276 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
277 |
assertFalse(p1.isTerminated()); |
278 |
try { |
279 |
p1.execute(new MediumRunnable()); |
280 |
} finally { |
281 |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
282 |
} |
283 |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
284 |
assertTrue(p1.isTerminated()); |
285 |
} |
286 |
|
287 |
/** |
288 |
* isTerminating is not true when running or when terminated |
289 |
*/ |
290 |
public void testIsTerminating() throws InterruptedException { |
291 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
292 |
assertFalse(p1.isTerminating()); |
293 |
try { |
294 |
p1.execute(new SmallRunnable()); |
295 |
assertFalse(p1.isTerminating()); |
296 |
} finally { |
297 |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
298 |
} |
299 |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
300 |
assertTrue(p1.isTerminated()); |
301 |
assertFalse(p1.isTerminating()); |
302 |
} |
303 |
|
304 |
/** |
305 |
* getQueue returns the work queue, which contains queued tasks |
306 |
*/ |
307 |
public void testGetQueue() throws InterruptedException { |
308 |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
309 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, q); |
310 |
FutureTask[] tasks = new FutureTask[5]; |
311 |
for (int i = 0; i < 5; i++) { |
312 |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
313 |
p1.execute(tasks[i]); |
314 |
} |
315 |
try { |
316 |
Thread.sleep(SHORT_DELAY_MS); |
317 |
BlockingQueue<Runnable> wq = p1.getQueue(); |
318 |
assertSame(q, wq); |
319 |
assertFalse(wq.contains(tasks[0])); |
320 |
assertTrue(wq.contains(tasks[4])); |
321 |
for (int i = 1; i < 5; ++i) |
322 |
tasks[i].cancel(true); |
323 |
p1.shutdownNow(); |
324 |
} finally { |
325 |
joinPool(p1); |
326 |
} |
327 |
} |
328 |
|
329 |
/** |
330 |
* remove(task) removes queued task, and fails to remove active task |
331 |
*/ |
332 |
public void testRemove() throws InterruptedException { |
333 |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
334 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, q); |
335 |
FutureTask[] tasks = new FutureTask[5]; |
336 |
for (int i = 0; i < 5; i++) { |
337 |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
338 |
p1.execute(tasks[i]); |
339 |
} |
340 |
try { |
341 |
Thread.sleep(SHORT_DELAY_MS); |
342 |
assertFalse(p1.remove(tasks[0])); |
343 |
assertTrue(q.contains(tasks[4])); |
344 |
assertTrue(q.contains(tasks[3])); |
345 |
assertTrue(p1.remove(tasks[4])); |
346 |
assertFalse(p1.remove(tasks[4])); |
347 |
assertFalse(q.contains(tasks[4])); |
348 |
assertTrue(q.contains(tasks[3])); |
349 |
assertTrue(p1.remove(tasks[3])); |
350 |
assertFalse(q.contains(tasks[3])); |
351 |
} finally { |
352 |
joinPool(p1); |
353 |
} |
354 |
} |
355 |
|
356 |
/** |
357 |
* purge removes cancelled tasks from the queue |
358 |
*/ |
359 |
public void testPurge() { |
360 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
361 |
FutureTask[] tasks = new FutureTask[5]; |
362 |
for (int i = 0; i < 5; i++) { |
363 |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
364 |
p1.execute(tasks[i]); |
365 |
} |
366 |
tasks[4].cancel(true); |
367 |
tasks[3].cancel(true); |
368 |
p1.purge(); |
369 |
long count = p1.getTaskCount(); |
370 |
assertTrue(count >= 2 && count < 5); |
371 |
joinPool(p1); |
372 |
} |
373 |
|
374 |
/** |
375 |
* shutDownNow returns a list containing tasks that were not run |
376 |
*/ |
377 |
public void testShutDownNow() { |
378 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
379 |
List l; |
380 |
try { |
381 |
for (int i = 0; i < 5; i++) |
382 |
p1.execute(new MediumPossiblyInterruptedRunnable()); |
383 |
} |
384 |
finally { |
385 |
try { |
386 |
l = p1.shutdownNow(); |
387 |
} catch (SecurityException ok) { return; } |
388 |
} |
389 |
assertTrue(p1.isShutdown()); |
390 |
assertTrue(l.size() <= 4); |
391 |
} |
392 |
|
393 |
// Exception Tests |
394 |
|
395 |
|
396 |
/** |
397 |
* Constructor throws if corePoolSize argument is less than zero |
398 |
*/ |
399 |
public void testConstructor1() { |
400 |
try { |
401 |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
402 |
shouldThrow(); |
403 |
} catch (IllegalArgumentException success) {} |
404 |
} |
405 |
|
406 |
/** |
407 |
* Constructor throws if maximumPoolSize is less than zero |
408 |
*/ |
409 |
public void testConstructor2() { |
410 |
try { |
411 |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
412 |
shouldThrow(); |
413 |
} catch (IllegalArgumentException success) {} |
414 |
} |
415 |
|
416 |
/** |
417 |
* Constructor throws if maximumPoolSize is equal to zero |
418 |
*/ |
419 |
public void testConstructor3() { |
420 |
try { |
421 |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
422 |
shouldThrow(); |
423 |
} catch (IllegalArgumentException success) {} |
424 |
} |
425 |
|
426 |
/** |
427 |
* Constructor throws if keepAliveTime is less than zero |
428 |
*/ |
429 |
public void testConstructor4() { |
430 |
try { |
431 |
new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
432 |
shouldThrow(); |
433 |
} catch (IllegalArgumentException success) {} |
434 |
} |
435 |
|
436 |
/** |
437 |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
438 |
*/ |
439 |
public void testConstructor5() { |
440 |
try { |
441 |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
442 |
shouldThrow(); |
443 |
} catch (IllegalArgumentException success) {} |
444 |
} |
445 |
|
446 |
/** |
447 |
* Constructor throws if workQueue is set to null |
448 |
*/ |
449 |
public void testConstructorNullPointerException() { |
450 |
try { |
451 |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null); |
452 |
shouldThrow(); |
453 |
} catch (NullPointerException success) {} |
454 |
} |
455 |
|
456 |
|
457 |
|
458 |
/** |
459 |
* Constructor throws if corePoolSize argument is less than zero |
460 |
*/ |
461 |
public void testConstructor6() { |
462 |
try { |
463 |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
464 |
shouldThrow(); |
465 |
} catch (IllegalArgumentException success) {} |
466 |
} |
467 |
|
468 |
/** |
469 |
* Constructor throws if maximumPoolSize is less than zero |
470 |
*/ |
471 |
public void testConstructor7() { |
472 |
try { |
473 |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
474 |
shouldThrow(); |
475 |
} catch (IllegalArgumentException success) {} |
476 |
} |
477 |
|
478 |
/** |
479 |
* Constructor throws if maximumPoolSize is equal to zero |
480 |
*/ |
481 |
public void testConstructor8() { |
482 |
try { |
483 |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
484 |
shouldThrow(); |
485 |
} catch (IllegalArgumentException success) {} |
486 |
} |
487 |
|
488 |
/** |
489 |
* Constructor throws if keepAliveTime is less than zero |
490 |
*/ |
491 |
public void testConstructor9() { |
492 |
try { |
493 |
new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
494 |
shouldThrow(); |
495 |
} catch (IllegalArgumentException success) {} |
496 |
} |
497 |
|
498 |
/** |
499 |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
500 |
*/ |
501 |
public void testConstructor10() { |
502 |
try { |
503 |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
504 |
shouldThrow(); |
505 |
} catch (IllegalArgumentException success) {} |
506 |
} |
507 |
|
508 |
/** |
509 |
* Constructor throws if workQueue is set to null |
510 |
*/ |
511 |
public void testConstructorNullPointerException2() { |
512 |
try { |
513 |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory()); |
514 |
shouldThrow(); |
515 |
} catch (NullPointerException success) {} |
516 |
} |
517 |
|
518 |
/** |
519 |
* Constructor throws if threadFactory is set to null |
520 |
*/ |
521 |
public void testConstructorNullPointerException3() { |
522 |
try { |
523 |
ThreadFactory f = null; |
524 |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
525 |
shouldThrow(); |
526 |
} catch (NullPointerException success) {} |
527 |
} |
528 |
|
529 |
|
530 |
/** |
531 |
* Constructor throws if corePoolSize argument is less than zero |
532 |
*/ |
533 |
public void testConstructor11() { |
534 |
try { |
535 |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
536 |
shouldThrow(); |
537 |
} catch (IllegalArgumentException success) {} |
538 |
} |
539 |
|
540 |
/** |
541 |
* Constructor throws if maximumPoolSize is less than zero |
542 |
*/ |
543 |
public void testConstructor12() { |
544 |
try { |
545 |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
546 |
shouldThrow(); |
547 |
} catch (IllegalArgumentException success) {} |
548 |
} |
549 |
|
550 |
/** |
551 |
* Constructor throws if maximumPoolSize is equal to zero |
552 |
*/ |
553 |
public void testConstructor13() { |
554 |
try { |
555 |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
556 |
shouldThrow(); |
557 |
} catch (IllegalArgumentException success) {} |
558 |
} |
559 |
|
560 |
/** |
561 |
* Constructor throws if keepAliveTime is less than zero |
562 |
*/ |
563 |
public void testConstructor14() { |
564 |
try { |
565 |
new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
566 |
shouldThrow(); |
567 |
} catch (IllegalArgumentException success) {} |
568 |
} |
569 |
|
570 |
/** |
571 |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
572 |
*/ |
573 |
public void testConstructor15() { |
574 |
try { |
575 |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
576 |
shouldThrow(); |
577 |
} catch (IllegalArgumentException success) {} |
578 |
} |
579 |
|
580 |
/** |
581 |
* Constructor throws if workQueue is set to null |
582 |
*/ |
583 |
public void testConstructorNullPointerException4() { |
584 |
try { |
585 |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler()); |
586 |
shouldThrow(); |
587 |
} catch (NullPointerException success) {} |
588 |
} |
589 |
|
590 |
/** |
591 |
* Constructor throws if handler is set to null |
592 |
*/ |
593 |
public void testConstructorNullPointerException5() { |
594 |
try { |
595 |
RejectedExecutionHandler r = null; |
596 |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
597 |
shouldThrow(); |
598 |
} catch (NullPointerException success) {} |
599 |
} |
600 |
|
601 |
|
602 |
/** |
603 |
* Constructor throws if corePoolSize argument is less than zero |
604 |
*/ |
605 |
public void testConstructor16() { |
606 |
try { |
607 |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
608 |
shouldThrow(); |
609 |
} catch (IllegalArgumentException success) {} |
610 |
} |
611 |
|
612 |
/** |
613 |
* Constructor throws if maximumPoolSize is less than zero |
614 |
*/ |
615 |
public void testConstructor17() { |
616 |
try { |
617 |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
618 |
shouldThrow(); |
619 |
} catch (IllegalArgumentException success) {} |
620 |
} |
621 |
|
622 |
/** |
623 |
* Constructor throws if maximumPoolSize is equal to zero |
624 |
*/ |
625 |
public void testConstructor18() { |
626 |
try { |
627 |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
628 |
shouldThrow(); |
629 |
} catch (IllegalArgumentException success) {} |
630 |
} |
631 |
|
632 |
/** |
633 |
* Constructor throws if keepAliveTime is less than zero |
634 |
*/ |
635 |
public void testConstructor19() { |
636 |
try { |
637 |
new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
638 |
shouldThrow(); |
639 |
} catch (IllegalArgumentException success) {} |
640 |
} |
641 |
|
642 |
/** |
643 |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
644 |
*/ |
645 |
public void testConstructor20() { |
646 |
try { |
647 |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
648 |
shouldThrow(); |
649 |
} catch (IllegalArgumentException success) {} |
650 |
} |
651 |
|
652 |
/** |
653 |
* Constructor throws if workQueue is set to null |
654 |
*/ |
655 |
public void testConstructorNullPointerException6() { |
656 |
try { |
657 |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); |
658 |
shouldThrow(); |
659 |
} catch (NullPointerException success) {} |
660 |
} |
661 |
|
662 |
/** |
663 |
* Constructor throws if handler is set to null |
664 |
*/ |
665 |
public void testConstructorNullPointerException7() { |
666 |
try { |
667 |
RejectedExecutionHandler r = null; |
668 |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r); |
669 |
shouldThrow(); |
670 |
} catch (NullPointerException success) {} |
671 |
} |
672 |
|
673 |
/** |
674 |
* Constructor throws if ThreadFactory is set top null |
675 |
*/ |
676 |
public void testConstructorNullPointerException8() { |
677 |
try { |
678 |
ThreadFactory f = null; |
679 |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler()); |
680 |
shouldThrow(); |
681 |
} catch (NullPointerException success) {} |
682 |
} |
683 |
|
684 |
|
685 |
/** |
686 |
* execute throws RejectedExecutionException |
687 |
* if saturated. |
688 |
*/ |
689 |
public void testSaturatedExecute() { |
690 |
ThreadPoolExecutor p = |
691 |
new ThreadPoolExecutor(1, 1, |
692 |
LONG_DELAY_MS, MILLISECONDS, |
693 |
new ArrayBlockingQueue<Runnable>(1)); |
694 |
try { |
695 |
for (int i = 0; i < 2; ++i) |
696 |
p.execute(new MediumRunnable()); |
697 |
for (int i = 0; i < 2; ++i) { |
698 |
try { |
699 |
p.execute(new MediumRunnable()); |
700 |
shouldThrow(); |
701 |
} catch (RejectedExecutionException success) {} |
702 |
} |
703 |
} finally { |
704 |
joinPool(p); |
705 |
} |
706 |
} |
707 |
|
708 |
/** |
709 |
* executor using CallerRunsPolicy runs task if saturated. |
710 |
*/ |
711 |
public void testSaturatedExecute2() { |
712 |
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
713 |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
714 |
try { |
715 |
|
716 |
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
717 |
for (int i = 0; i < 5; ++i) { |
718 |
tasks[i] = new TrackedNoOpRunnable(); |
719 |
} |
720 |
TrackedLongRunnable mr = new TrackedLongRunnable(); |
721 |
p.execute(mr); |
722 |
for (int i = 0; i < 5; ++i) { |
723 |
p.execute(tasks[i]); |
724 |
} |
725 |
for (int i = 1; i < 5; ++i) { |
726 |
assertTrue(tasks[i].done); |
727 |
} |
728 |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
729 |
} finally { |
730 |
joinPool(p); |
731 |
} |
732 |
} |
733 |
|
734 |
/** |
735 |
* executor using DiscardPolicy drops task if saturated. |
736 |
*/ |
737 |
public void testSaturatedExecute3() { |
738 |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
739 |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
740 |
try { |
741 |
|
742 |
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
743 |
for (int i = 0; i < 5; ++i) { |
744 |
tasks[i] = new TrackedNoOpRunnable(); |
745 |
} |
746 |
p.execute(new TrackedLongRunnable()); |
747 |
for (int i = 0; i < 5; ++i) { |
748 |
p.execute(tasks[i]); |
749 |
} |
750 |
for (int i = 0; i < 5; ++i) { |
751 |
assertFalse(tasks[i].done); |
752 |
} |
753 |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
754 |
} finally { |
755 |
joinPool(p); |
756 |
} |
757 |
} |
758 |
|
759 |
/** |
760 |
* executor using DiscardOldestPolicy drops oldest task if saturated. |
761 |
*/ |
762 |
public void testSaturatedExecute4() { |
763 |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
764 |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
765 |
try { |
766 |
p.execute(new TrackedLongRunnable()); |
767 |
TrackedLongRunnable r2 = new TrackedLongRunnable(); |
768 |
p.execute(r2); |
769 |
assertTrue(p.getQueue().contains(r2)); |
770 |
TrackedNoOpRunnable r3 = new TrackedNoOpRunnable(); |
771 |
p.execute(r3); |
772 |
assertFalse(p.getQueue().contains(r2)); |
773 |
assertTrue(p.getQueue().contains(r3)); |
774 |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
775 |
} finally { |
776 |
joinPool(p); |
777 |
} |
778 |
} |
779 |
|
780 |
/** |
781 |
* execute throws RejectedExecutionException if shutdown |
782 |
*/ |
783 |
public void testRejectedExecutionExceptionOnShutdown() { |
784 |
ThreadPoolExecutor tpe = |
785 |
new ThreadPoolExecutor(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
786 |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
787 |
try { |
788 |
tpe.execute(new NoOpRunnable()); |
789 |
shouldThrow(); |
790 |
} catch (RejectedExecutionException success) {} |
791 |
|
792 |
joinPool(tpe); |
793 |
} |
794 |
|
795 |
/** |
796 |
* execute using CallerRunsPolicy drops task on shutdown |
797 |
*/ |
798 |
public void testCallerRunsOnShutdown() { |
799 |
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
800 |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
801 |
|
802 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
803 |
try { |
804 |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
805 |
p.execute(r); |
806 |
assertFalse(r.done); |
807 |
} finally { |
808 |
joinPool(p); |
809 |
} |
810 |
} |
811 |
|
812 |
/** |
813 |
* execute using DiscardPolicy drops task on shutdown |
814 |
*/ |
815 |
public void testDiscardOnShutdown() { |
816 |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
817 |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
818 |
|
819 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
820 |
try { |
821 |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
822 |
p.execute(r); |
823 |
assertFalse(r.done); |
824 |
} finally { |
825 |
joinPool(p); |
826 |
} |
827 |
} |
828 |
|
829 |
|
830 |
/** |
831 |
* execute using DiscardOldestPolicy drops task on shutdown |
832 |
*/ |
833 |
public void testDiscardOldestOnShutdown() { |
834 |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
835 |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
836 |
|
837 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
838 |
try { |
839 |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
840 |
p.execute(r); |
841 |
assertFalse(r.done); |
842 |
} finally { |
843 |
joinPool(p); |
844 |
} |
845 |
} |
846 |
|
847 |
|
848 |
/** |
849 |
* execute (null) throws NPE |
850 |
*/ |
851 |
public void testExecuteNull() { |
852 |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
853 |
try { |
854 |
tpe.execute(null); |
855 |
shouldThrow(); |
856 |
} catch (NullPointerException success) {} |
857 |
|
858 |
joinPool(tpe); |
859 |
} |
860 |
|
861 |
/** |
862 |
* setCorePoolSize of negative value throws IllegalArgumentException |
863 |
*/ |
864 |
public void testCorePoolSizeIllegalArgumentException() { |
865 |
ThreadPoolExecutor tpe = |
866 |
new ThreadPoolExecutor(1, 2, |
867 |
LONG_DELAY_MS, MILLISECONDS, |
868 |
new ArrayBlockingQueue<Runnable>(10)); |
869 |
try { |
870 |
tpe.setCorePoolSize(-1); |
871 |
shouldThrow(); |
872 |
} catch (IllegalArgumentException success) { |
873 |
} finally { |
874 |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
875 |
} |
876 |
joinPool(tpe); |
877 |
} |
878 |
|
879 |
/** |
880 |
* setMaximumPoolSize(int) throws IllegalArgumentException if |
881 |
* given a value less the core pool size |
882 |
*/ |
883 |
public void testMaximumPoolSizeIllegalArgumentException() { |
884 |
ThreadPoolExecutor tpe = |
885 |
new ThreadPoolExecutor(2, 3, |
886 |
LONG_DELAY_MS, MILLISECONDS, |
887 |
new ArrayBlockingQueue<Runnable>(10)); |
888 |
try { |
889 |
tpe.setMaximumPoolSize(1); |
890 |
shouldThrow(); |
891 |
} catch (IllegalArgumentException success) { |
892 |
} finally { |
893 |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
894 |
} |
895 |
joinPool(tpe); |
896 |
} |
897 |
|
898 |
/** |
899 |
* setMaximumPoolSize throws IllegalArgumentException |
900 |
* if given a negative value |
901 |
*/ |
902 |
public void testMaximumPoolSizeIllegalArgumentException2() { |
903 |
ThreadPoolExecutor tpe = |
904 |
new ThreadPoolExecutor(2, 3, |
905 |
LONG_DELAY_MS, MILLISECONDS, |
906 |
new ArrayBlockingQueue<Runnable>(10)); |
907 |
try { |
908 |
tpe.setMaximumPoolSize(-1); |
909 |
shouldThrow(); |
910 |
} catch (IllegalArgumentException success) { |
911 |
} finally { |
912 |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
913 |
} |
914 |
joinPool(tpe); |
915 |
} |
916 |
|
917 |
|
918 |
/** |
919 |
* setKeepAliveTime throws IllegalArgumentException |
920 |
* when given a negative value |
921 |
*/ |
922 |
public void testKeepAliveTimeIllegalArgumentException() { |
923 |
ThreadPoolExecutor tpe = |
924 |
new ThreadPoolExecutor(2, 3, |
925 |
LONG_DELAY_MS, MILLISECONDS, |
926 |
new ArrayBlockingQueue<Runnable>(10)); |
927 |
try { |
928 |
tpe.setKeepAliveTime(-1,MILLISECONDS); |
929 |
shouldThrow(); |
930 |
} catch (IllegalArgumentException success) { |
931 |
} finally { |
932 |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
933 |
} |
934 |
joinPool(tpe); |
935 |
} |
936 |
|
937 |
/** |
938 |
* terminated() is called on termination |
939 |
*/ |
940 |
public void testTerminated() { |
941 |
ExtendedTPE tpe = new ExtendedTPE(); |
942 |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
943 |
assertTrue(tpe.terminatedCalled); |
944 |
joinPool(tpe); |
945 |
} |
946 |
|
947 |
/** |
948 |
* beforeExecute and afterExecute are called when executing task |
949 |
*/ |
950 |
public void testBeforeAfter() throws InterruptedException { |
951 |
ExtendedTPE tpe = new ExtendedTPE(); |
952 |
try { |
953 |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
954 |
tpe.execute(r); |
955 |
Thread.sleep(SHORT_DELAY_MS); |
956 |
assertTrue(r.done); |
957 |
assertTrue(tpe.beforeCalled); |
958 |
assertTrue(tpe.afterCalled); |
959 |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
960 |
} finally { |
961 |
joinPool(tpe); |
962 |
} |
963 |
} |
964 |
|
965 |
/** |
966 |
* completed submit of callable returns result |
967 |
*/ |
968 |
public void testSubmitCallable() throws Exception { |
969 |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
970 |
try { |
971 |
Future<String> future = e.submit(new StringTask()); |
972 |
String result = future.get(); |
973 |
assertSame(TEST_STRING, result); |
974 |
} finally { |
975 |
joinPool(e); |
976 |
} |
977 |
} |
978 |
|
979 |
/** |
980 |
* completed submit of runnable returns successfully |
981 |
*/ |
982 |
public void testSubmitRunnable() throws Exception { |
983 |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
984 |
try { |
985 |
Future<?> future = e.submit(new NoOpRunnable()); |
986 |
future.get(); |
987 |
assertTrue(future.isDone()); |
988 |
} finally { |
989 |
joinPool(e); |
990 |
} |
991 |
} |
992 |
|
993 |
/** |
994 |
* completed submit of (runnable, result) returns result |
995 |
*/ |
996 |
public void testSubmitRunnable2() throws Exception { |
997 |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
998 |
try { |
999 |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1000 |
String result = future.get(); |
1001 |
assertSame(TEST_STRING, result); |
1002 |
} finally { |
1003 |
joinPool(e); |
1004 |
} |
1005 |
} |
1006 |
|
1007 |
|
1008 |
/** |
1009 |
* invokeAny(null) throws NPE |
1010 |
*/ |
1011 |
public void testInvokeAny1() throws Exception { |
1012 |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1013 |
try { |
1014 |
e.invokeAny(null); |
1015 |
shouldThrow(); |
1016 |
} catch (NullPointerException success) { |
1017 |
} finally { |
1018 |
joinPool(e); |
1019 |
} |
1020 |
} |
1021 |
|
1022 |
/** |
1023 |
* invokeAny(empty collection) throws IAE |
1024 |
*/ |
1025 |
public void testInvokeAny2() throws Exception { |
1026 |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1027 |
try { |
1028 |
e.invokeAny(new ArrayList<Callable<String>>()); |
1029 |
shouldThrow(); |
1030 |
} catch (IllegalArgumentException success) { |
1031 |
} finally { |
1032 |
joinPool(e); |
1033 |
} |
1034 |
} |
1035 |
|
1036 |
/** |
1037 |
* invokeAny(c) throws NPE if c has null elements |
1038 |
*/ |
1039 |
public void testInvokeAny3() throws Exception { |
1040 |
CountDownLatch latch = new CountDownLatch(1); |
1041 |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1042 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1043 |
l.add(latchAwaitingStringTask(latch)); |
1044 |
l.add(null); |
1045 |
try { |
1046 |
e.invokeAny(l); |
1047 |
shouldThrow(); |
1048 |
} catch (NullPointerException success) { |
1049 |
} finally { |
1050 |
latch.countDown(); |
1051 |
joinPool(e); |
1052 |
} |
1053 |
} |
1054 |
|
1055 |
/** |
1056 |
* invokeAny(c) throws ExecutionException if no task completes |
1057 |
*/ |
1058 |
public void testInvokeAny4() throws Exception { |
1059 |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1060 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1061 |
l.add(new NPETask()); |
1062 |
try { |
1063 |
e.invokeAny(l); |
1064 |
shouldThrow(); |
1065 |
} catch (ExecutionException success) { |
1066 |
assertTrue(success.getCause() instanceof NullPointerException); |
1067 |
} finally { |
1068 |
joinPool(e); |
1069 |
} |
1070 |
} |
1071 |
|
1072 |
/** |
1073 |
* invokeAny(c) returns result of some task |
1074 |
*/ |
1075 |
public void testInvokeAny5() throws Exception { |
1076 |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1077 |
try { |
1078 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1079 |
l.add(new StringTask()); |
1080 |
l.add(new StringTask()); |
1081 |
String result = e.invokeAny(l); |
1082 |
assertSame(TEST_STRING, result); |
1083 |
} finally { |
1084 |
joinPool(e); |
1085 |
} |
1086 |
} |
1087 |
|
1088 |
/** |
1089 |
* invokeAll(null) throws NPE |
1090 |
*/ |
1091 |
public void testInvokeAll1() throws Exception { |
1092 |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1093 |
try { |
1094 |
e.invokeAll(null); |
1095 |
shouldThrow(); |
1096 |
} catch (NullPointerException success) { |
1097 |
} finally { |
1098 |
joinPool(e); |
1099 |
} |
1100 |
} |
1101 |
|
1102 |
/** |
1103 |
* invokeAll(empty collection) returns empty collection |
1104 |
*/ |
1105 |
public void testInvokeAll2() throws InterruptedException { |
1106 |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1107 |
try { |
1108 |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1109 |
assertTrue(r.isEmpty()); |
1110 |
} finally { |
1111 |
joinPool(e); |
1112 |
} |
1113 |
} |
1114 |
|
1115 |
/** |
1116 |
* invokeAll(c) throws NPE if c has null elements |
1117 |
*/ |
1118 |
public void testInvokeAll3() throws Exception { |
1119 |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1120 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1121 |
l.add(new StringTask()); |
1122 |
l.add(null); |
1123 |
try { |
1124 |
e.invokeAll(l); |
1125 |
shouldThrow(); |
1126 |
} catch (NullPointerException success) { |
1127 |
} finally { |
1128 |
joinPool(e); |
1129 |
} |
1130 |
} |
1131 |
|
1132 |
/** |
1133 |
* get of element of invokeAll(c) throws exception on failed task |
1134 |
*/ |
1135 |
public void testInvokeAll4() throws Exception { |
1136 |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1137 |
try { |
1138 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1139 |
l.add(new NPETask()); |
1140 |
List<Future<String>> futures = e.invokeAll(l); |
1141 |
assertEquals(1, futures.size()); |
1142 |
try { |
1143 |
futures.get(0).get(); |
1144 |
shouldThrow(); |
1145 |
} catch (ExecutionException success) { |
1146 |
assertTrue(success.getCause() instanceof NullPointerException); |
1147 |
} |
1148 |
} finally { |
1149 |
joinPool(e); |
1150 |
} |
1151 |
} |
1152 |
|
1153 |
/** |
1154 |
* invokeAll(c) returns results of all completed tasks |
1155 |
*/ |
1156 |
public void testInvokeAll5() throws Exception { |
1157 |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1158 |
try { |
1159 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1160 |
l.add(new StringTask()); |
1161 |
l.add(new StringTask()); |
1162 |
List<Future<String>> futures = e.invokeAll(l); |
1163 |
assertEquals(2, futures.size()); |
1164 |
for (Future<String> future : futures) |
1165 |
assertSame(TEST_STRING, future.get()); |
1166 |
} finally { |
1167 |
joinPool(e); |
1168 |
} |
1169 |
} |
1170 |
|
1171 |
|
1172 |
|
1173 |
/** |
1174 |
* timed invokeAny(null) throws NPE |
1175 |
*/ |
1176 |
public void testTimedInvokeAny1() throws Exception { |
1177 |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1178 |
try { |
1179 |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1180 |
shouldThrow(); |
1181 |
} catch (NullPointerException success) { |
1182 |
} finally { |
1183 |
joinPool(e); |
1184 |
} |
1185 |
} |
1186 |
|
1187 |
/** |
1188 |
* timed invokeAny(,,null) throws NPE |
1189 |
*/ |
1190 |
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1191 |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1192 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1193 |
l.add(new StringTask()); |
1194 |
try { |
1195 |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1196 |
shouldThrow(); |
1197 |
} catch (NullPointerException success) { |
1198 |
} finally { |
1199 |
joinPool(e); |
1200 |
} |
1201 |
} |
1202 |
|
1203 |
/** |
1204 |
* timed invokeAny(empty collection) throws IAE |
1205 |
*/ |
1206 |
public void testTimedInvokeAny2() throws Exception { |
1207 |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1208 |
try { |
1209 |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1210 |
shouldThrow(); |
1211 |
} catch (IllegalArgumentException success) { |
1212 |
} finally { |
1213 |
joinPool(e); |
1214 |
} |
1215 |
} |
1216 |
|
1217 |
/** |
1218 |
* timed invokeAny(c) throws NPE if c has null elements |
1219 |
*/ |
1220 |
public void testTimedInvokeAny3() throws Exception { |
1221 |
CountDownLatch latch = new CountDownLatch(1); |
1222 |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1223 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1224 |
l.add(latchAwaitingStringTask(latch)); |
1225 |
l.add(null); |
1226 |
try { |
1227 |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1228 |
shouldThrow(); |
1229 |
} catch (NullPointerException success) { |
1230 |
} finally { |
1231 |
latch.countDown(); |
1232 |
joinPool(e); |
1233 |
} |
1234 |
} |
1235 |
|
1236 |
/** |
1237 |
* timed invokeAny(c) throws ExecutionException if no task completes |
1238 |
*/ |
1239 |
public void testTimedInvokeAny4() throws Exception { |
1240 |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1241 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1242 |
l.add(new NPETask()); |
1243 |
try { |
1244 |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1245 |
shouldThrow(); |
1246 |
} catch (ExecutionException success) { |
1247 |
assertTrue(success.getCause() instanceof NullPointerException); |
1248 |
} finally { |
1249 |
joinPool(e); |
1250 |
} |
1251 |
} |
1252 |
|
1253 |
/** |
1254 |
* timed invokeAny(c) returns result of some task |
1255 |
*/ |
1256 |
public void testTimedInvokeAny5() throws Exception { |
1257 |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1258 |
try { |
1259 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1260 |
l.add(new StringTask()); |
1261 |
l.add(new StringTask()); |
1262 |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1263 |
assertSame(TEST_STRING, result); |
1264 |
} finally { |
1265 |
joinPool(e); |
1266 |
} |
1267 |
} |
1268 |
|
1269 |
/** |
1270 |
* timed invokeAll(null) throws NPE |
1271 |
*/ |
1272 |
public void testTimedInvokeAll1() throws Exception { |
1273 |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1274 |
try { |
1275 |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1276 |
shouldThrow(); |
1277 |
} catch (NullPointerException success) { |
1278 |
} finally { |
1279 |
joinPool(e); |
1280 |
} |
1281 |
} |
1282 |
|
1283 |
/** |
1284 |
* timed invokeAll(,,null) throws NPE |
1285 |
*/ |
1286 |
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1287 |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1288 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1289 |
l.add(new StringTask()); |
1290 |
try { |
1291 |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1292 |
shouldThrow(); |
1293 |
} catch (NullPointerException success) { |
1294 |
} finally { |
1295 |
joinPool(e); |
1296 |
} |
1297 |
} |
1298 |
|
1299 |
/** |
1300 |
* timed invokeAll(empty collection) returns empty collection |
1301 |
*/ |
1302 |
public void testTimedInvokeAll2() throws InterruptedException { |
1303 |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1304 |
try { |
1305 |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1306 |
assertTrue(r.isEmpty()); |
1307 |
} finally { |
1308 |
joinPool(e); |
1309 |
} |
1310 |
} |
1311 |
|
1312 |
/** |
1313 |
* timed invokeAll(c) throws NPE if c has null elements |
1314 |
*/ |
1315 |
public void testTimedInvokeAll3() throws Exception { |
1316 |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1317 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1318 |
l.add(new StringTask()); |
1319 |
l.add(null); |
1320 |
try { |
1321 |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1322 |
shouldThrow(); |
1323 |
} catch (NullPointerException success) { |
1324 |
} finally { |
1325 |
joinPool(e); |
1326 |
} |
1327 |
} |
1328 |
|
1329 |
/** |
1330 |
* get of element of invokeAll(c) throws exception on failed task |
1331 |
*/ |
1332 |
public void testTimedInvokeAll4() throws Exception { |
1333 |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1334 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1335 |
l.add(new NPETask()); |
1336 |
List<Future<String>> futures = |
1337 |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1338 |
assertEquals(1, futures.size()); |
1339 |
try { |
1340 |
futures.get(0).get(); |
1341 |
shouldThrow(); |
1342 |
} catch (ExecutionException success) { |
1343 |
assertTrue(success.getCause() instanceof NullPointerException); |
1344 |
} finally { |
1345 |
joinPool(e); |
1346 |
} |
1347 |
} |
1348 |
|
1349 |
/** |
1350 |
* timed invokeAll(c) returns results of all completed tasks |
1351 |
*/ |
1352 |
public void testTimedInvokeAll5() throws Exception { |
1353 |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1354 |
try { |
1355 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1356 |
l.add(new StringTask()); |
1357 |
l.add(new StringTask()); |
1358 |
List<Future<String>> futures = |
1359 |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1360 |
assertEquals(2, futures.size()); |
1361 |
for (Future<String> future : futures) |
1362 |
assertSame(TEST_STRING, future.get()); |
1363 |
} finally { |
1364 |
joinPool(e); |
1365 |
} |
1366 |
} |
1367 |
|
1368 |
/** |
1369 |
* timed invokeAll(c) cancels tasks not completed by timeout |
1370 |
*/ |
1371 |
public void testTimedInvokeAll6() throws Exception { |
1372 |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1373 |
try { |
1374 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1375 |
l.add(new StringTask()); |
1376 |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1377 |
l.add(new StringTask()); |
1378 |
List<Future<String>> futures = |
1379 |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1380 |
assertEquals(3, futures.size()); |
1381 |
Iterator<Future<String>> it = futures.iterator(); |
1382 |
Future<String> f1 = it.next(); |
1383 |
Future<String> f2 = it.next(); |
1384 |
Future<String> f3 = it.next(); |
1385 |
assertTrue(f1.isDone()); |
1386 |
assertTrue(f2.isDone()); |
1387 |
assertTrue(f3.isDone()); |
1388 |
assertFalse(f1.isCancelled()); |
1389 |
assertTrue(f2.isCancelled()); |
1390 |
} finally { |
1391 |
joinPool(e); |
1392 |
} |
1393 |
} |
1394 |
|
1395 |
/** |
1396 |
* Execution continues if there is at least one thread even if |
1397 |
* thread factory fails to create more |
1398 |
*/ |
1399 |
public void testFailingThreadFactory() throws InterruptedException { |
1400 |
ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory()); |
1401 |
try { |
1402 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1403 |
for (int k = 0; k < 100; ++k) { |
1404 |
e.execute(new NoOpRunnable()); |
1405 |
} |
1406 |
Thread.sleep(LONG_DELAY_MS); |
1407 |
} finally { |
1408 |
joinPool(e); |
1409 |
} |
1410 |
} |
1411 |
|
1412 |
/** |
1413 |
* allowsCoreThreadTimeOut is by default false. |
1414 |
*/ |
1415 |
public void testAllowsCoreThreadTimeOut() { |
1416 |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1417 |
assertFalse(tpe.allowsCoreThreadTimeOut()); |
1418 |
joinPool(tpe); |
1419 |
} |
1420 |
|
1421 |
/** |
1422 |
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1423 |
*/ |
1424 |
public void testAllowCoreThreadTimeOut_true() throws InterruptedException { |
1425 |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1426 |
tpe.allowCoreThreadTimeOut(true); |
1427 |
tpe.execute(new NoOpRunnable()); |
1428 |
try { |
1429 |
Thread.sleep(MEDIUM_DELAY_MS); |
1430 |
assertEquals(0, tpe.getPoolSize()); |
1431 |
} finally { |
1432 |
joinPool(tpe); |
1433 |
} |
1434 |
} |
1435 |
|
1436 |
/** |
1437 |
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1438 |
*/ |
1439 |
public void testAllowCoreThreadTimeOut_false() throws InterruptedException { |
1440 |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1441 |
tpe.allowCoreThreadTimeOut(false); |
1442 |
tpe.execute(new NoOpRunnable()); |
1443 |
try { |
1444 |
Thread.sleep(MEDIUM_DELAY_MS); |
1445 |
assertTrue(tpe.getPoolSize() >= 1); |
1446 |
} finally { |
1447 |
joinPool(tpe); |
1448 |
} |
1449 |
} |
1450 |
|
1451 |
/** |
1452 |
* execute allows the same task to be submitted multiple times, even |
1453 |
* if rejected |
1454 |
*/ |
1455 |
public void testRejectedRecycledTask() throws InterruptedException { |
1456 |
final int nTasks = 1000; |
1457 |
final AtomicInteger nRun = new AtomicInteger(0); |
1458 |
final Runnable recycledTask = new Runnable() { |
1459 |
public void run() { |
1460 |
nRun.getAndIncrement(); |
1461 |
} }; |
1462 |
final ThreadPoolExecutor p = |
1463 |
new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS, |
1464 |
new ArrayBlockingQueue(30)); |
1465 |
try { |
1466 |
for (int i = 0; i < nTasks; ++i) { |
1467 |
for (;;) { |
1468 |
try { |
1469 |
p.execute(recycledTask); |
1470 |
break; |
1471 |
} |
1472 |
catch (RejectedExecutionException ignore) { |
1473 |
} |
1474 |
} |
1475 |
} |
1476 |
Thread.sleep(5000); // enough time to run all tasks |
1477 |
assertEquals(nRun.get(), nTasks); |
1478 |
} finally { |
1479 |
p.shutdown(); |
1480 |
} |
1481 |
} |
1482 |
|
1483 |
} |