1 |
/* |
2 |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
* public domain. Use, modify, and redistribute this code in any way |
4 |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
6 |
*/ |
7 |
|
8 |
import java.util.concurrent.*; |
9 |
import junit.framework.*; |
10 |
import java.util.List; |
11 |
|
12 |
public class ThreadPoolExecutorTest extends JSR166TestCase { |
13 |
public static void main(String[] args) { |
14 |
junit.textui.TestRunner.run (suite()); |
15 |
} |
16 |
public static Test suite() { |
17 |
return new TestSuite(ThreadPoolExecutorTest.class); |
18 |
} |
19 |
|
20 |
static class ExtendedTPE extends ThreadPoolExecutor { |
21 |
volatile boolean beforeCalled = false; |
22 |
volatile boolean afterCalled = false; |
23 |
volatile boolean terminatedCalled = false; |
24 |
public ExtendedTPE() { |
25 |
super(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>()); |
26 |
} |
27 |
protected void beforeExecute(Thread t, Runnable r) { |
28 |
beforeCalled = true; |
29 |
} |
30 |
protected void afterExecute(Runnable r, Throwable t) { |
31 |
afterCalled = true; |
32 |
} |
33 |
protected void terminated() { |
34 |
terminatedCalled = true; |
35 |
} |
36 |
} |
37 |
|
38 |
/** |
39 |
* execute successfully executes a runnable |
40 |
*/ |
41 |
public void testExecute() { |
42 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
43 |
try { |
44 |
p1.execute(new Runnable() { |
45 |
public void run() { |
46 |
try { |
47 |
Thread.sleep(SHORT_DELAY_MS); |
48 |
} catch(InterruptedException e){ |
49 |
threadUnexpectedException(); |
50 |
} |
51 |
} |
52 |
}); |
53 |
Thread.sleep(SMALL_DELAY_MS); |
54 |
} catch(InterruptedException e){ |
55 |
unexpectedException(); |
56 |
} |
57 |
joinPool(p1); |
58 |
} |
59 |
|
60 |
/** |
61 |
* getActiveCount increases but doesn't overestimate, when a |
62 |
* thread becomes active |
63 |
*/ |
64 |
public void testGetActiveCount() { |
65 |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
66 |
assertEquals(0, p2.getActiveCount()); |
67 |
p2.execute(new MediumRunnable()); |
68 |
try { |
69 |
Thread.sleep(SHORT_DELAY_MS); |
70 |
} catch(Exception e){ |
71 |
unexpectedException(); |
72 |
} |
73 |
assertEquals(1, p2.getActiveCount()); |
74 |
joinPool(p2); |
75 |
} |
76 |
|
77 |
/** |
78 |
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
79 |
*/ |
80 |
public void testPrestartCoreThread() { |
81 |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
82 |
assertEquals(0, p2.getPoolSize()); |
83 |
assertTrue(p2.prestartCoreThread()); |
84 |
assertEquals(1, p2.getPoolSize()); |
85 |
assertTrue(p2.prestartCoreThread()); |
86 |
assertEquals(2, p2.getPoolSize()); |
87 |
assertFalse(p2.prestartCoreThread()); |
88 |
assertEquals(2, p2.getPoolSize()); |
89 |
joinPool(p2); |
90 |
} |
91 |
|
92 |
/** |
93 |
* prestartAllCoreThreads starts all corePoolSize threads |
94 |
*/ |
95 |
public void testPrestartAllCoreThreads() { |
96 |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
97 |
assertEquals(0, p2.getPoolSize()); |
98 |
p2.prestartAllCoreThreads(); |
99 |
assertEquals(2, p2.getPoolSize()); |
100 |
p2.prestartAllCoreThreads(); |
101 |
assertEquals(2, p2.getPoolSize()); |
102 |
joinPool(p2); |
103 |
} |
104 |
|
105 |
/** |
106 |
* getCompletedTaskCount increases, but doesn't overestimate, |
107 |
* when tasks complete |
108 |
*/ |
109 |
public void testGetCompletedTaskCount() { |
110 |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
111 |
assertEquals(0, p2.getCompletedTaskCount()); |
112 |
p2.execute(new ShortRunnable()); |
113 |
try { |
114 |
Thread.sleep(SMALL_DELAY_MS); |
115 |
} catch(Exception e){ |
116 |
unexpectedException(); |
117 |
} |
118 |
assertEquals(1, p2.getCompletedTaskCount()); |
119 |
p2.shutdown(); |
120 |
joinPool(p2); |
121 |
} |
122 |
|
123 |
/** |
124 |
* getCorePoolSize returns size given in constructor if not otherwise set |
125 |
*/ |
126 |
public void testGetCorePoolSize() { |
127 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
128 |
assertEquals(1, p1.getCorePoolSize()); |
129 |
joinPool(p1); |
130 |
} |
131 |
|
132 |
/** |
133 |
* getKeepAliveTime returns value given in constructor if not otherwise set |
134 |
*/ |
135 |
public void testGetKeepAliveTime() { |
136 |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
137 |
assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS)); |
138 |
joinPool(p2); |
139 |
} |
140 |
|
141 |
|
142 |
/** |
143 |
* getThreadFactory returns factory in constructor if not set |
144 |
*/ |
145 |
public void testGetThreadFactory() { |
146 |
ThreadFactory tf = new SimpleThreadFactory(); |
147 |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler()); |
148 |
assertSame(tf, p.getThreadFactory()); |
149 |
p.shutdown(); |
150 |
joinPool(p); |
151 |
} |
152 |
|
153 |
/** |
154 |
* setThreadFactory sets the thread factory returned by getThreadFactory |
155 |
*/ |
156 |
public void testSetThreadFactory() { |
157 |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
158 |
ThreadFactory tf = new SimpleThreadFactory(); |
159 |
p.setThreadFactory(tf); |
160 |
assertSame(tf, p.getThreadFactory()); |
161 |
p.shutdown(); |
162 |
joinPool(p); |
163 |
} |
164 |
|
165 |
|
166 |
/** |
167 |
* setThreadFactory(null) throws NPE |
168 |
*/ |
169 |
public void testSetThreadFactoryNull() { |
170 |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
171 |
try { |
172 |
p.setThreadFactory(null); |
173 |
shouldThrow(); |
174 |
} catch (NullPointerException success) { |
175 |
} finally { |
176 |
joinPool(p); |
177 |
} |
178 |
} |
179 |
|
180 |
|
181 |
/** |
182 |
* getLargestPoolSize increases, but doesn't overestimate, when |
183 |
* multiple threads active |
184 |
*/ |
185 |
public void testGetLargestPoolSize() { |
186 |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
187 |
try { |
188 |
assertEquals(0, p2.getLargestPoolSize()); |
189 |
p2.execute(new MediumRunnable()); |
190 |
p2.execute(new MediumRunnable()); |
191 |
Thread.sleep(SHORT_DELAY_MS); |
192 |
assertEquals(2, p2.getLargestPoolSize()); |
193 |
} catch(Exception e){ |
194 |
unexpectedException(); |
195 |
} |
196 |
joinPool(p2); |
197 |
} |
198 |
|
199 |
/** |
200 |
* getMaximumPoolSize returns value given in constructor if not |
201 |
* otherwise set |
202 |
*/ |
203 |
public void testGetMaximumPoolSize() { |
204 |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
205 |
assertEquals(2, p2.getMaximumPoolSize()); |
206 |
joinPool(p2); |
207 |
} |
208 |
|
209 |
/** |
210 |
* getPoolSize increases, but doesn't overestimate, when threads |
211 |
* become active |
212 |
*/ |
213 |
public void testGetPoolSize() { |
214 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
215 |
assertEquals(0, p1.getPoolSize()); |
216 |
p1.execute(new MediumRunnable()); |
217 |
assertEquals(1, p1.getPoolSize()); |
218 |
joinPool(p1); |
219 |
} |
220 |
|
221 |
/** |
222 |
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
223 |
*/ |
224 |
public void testGetTaskCount() { |
225 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
226 |
try { |
227 |
assertEquals(0, p1.getTaskCount()); |
228 |
p1.execute(new MediumRunnable()); |
229 |
Thread.sleep(SHORT_DELAY_MS); |
230 |
assertEquals(1, p1.getTaskCount()); |
231 |
} catch(Exception e){ |
232 |
unexpectedException(); |
233 |
} |
234 |
joinPool(p1); |
235 |
} |
236 |
|
237 |
/** |
238 |
* isShutDown is false before shutdown, true after |
239 |
*/ |
240 |
public void testIsShutdown() { |
241 |
|
242 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
243 |
assertFalse(p1.isShutdown()); |
244 |
p1.shutdown(); |
245 |
assertTrue(p1.isShutdown()); |
246 |
joinPool(p1); |
247 |
} |
248 |
|
249 |
|
250 |
/** |
251 |
* isTerminated is false before termination, true after |
252 |
*/ |
253 |
public void testIsTerminated() { |
254 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
255 |
assertFalse(p1.isTerminated()); |
256 |
try { |
257 |
p1.execute(new MediumRunnable()); |
258 |
} finally { |
259 |
p1.shutdown(); |
260 |
} |
261 |
try { |
262 |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
263 |
assertTrue(p1.isTerminated()); |
264 |
} catch(Exception e){ |
265 |
unexpectedException(); |
266 |
} |
267 |
} |
268 |
|
269 |
/** |
270 |
* isTerminating is not true when running or when terminated |
271 |
*/ |
272 |
public void testIsTerminating() { |
273 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
274 |
assertFalse(p1.isTerminating()); |
275 |
try { |
276 |
p1.execute(new SmallRunnable()); |
277 |
assertFalse(p1.isTerminating()); |
278 |
} finally { |
279 |
p1.shutdown(); |
280 |
} |
281 |
try { |
282 |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
283 |
assertTrue(p1.isTerminated()); |
284 |
assertFalse(p1.isTerminating()); |
285 |
} catch(Exception e){ |
286 |
unexpectedException(); |
287 |
} |
288 |
} |
289 |
|
290 |
/** |
291 |
* getQueue returns the work queue, which contains queued tasks |
292 |
*/ |
293 |
public void testGetQueue() { |
294 |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
295 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q); |
296 |
CancellableTask[] tasks = new CancellableTask[5]; |
297 |
for(int i = 0; i < 5; i++){ |
298 |
tasks[i] = new CancellableTask(new MediumPossiblyInterruptedRunnable()); |
299 |
p1.execute(tasks[i]); |
300 |
} |
301 |
try { |
302 |
Thread.sleep(SHORT_DELAY_MS); |
303 |
BlockingQueue<Runnable> wq = p1.getQueue(); |
304 |
assertSame(q, wq); |
305 |
assertFalse(wq.contains(tasks[0])); |
306 |
assertTrue(wq.contains(tasks[4])); |
307 |
p1.shutdownNow(); |
308 |
} catch(Exception e) { |
309 |
unexpectedException(); |
310 |
} finally { |
311 |
joinPool(p1); |
312 |
} |
313 |
} |
314 |
|
315 |
/** |
316 |
* remove(task) removes queued task, and fails to remove active task |
317 |
*/ |
318 |
public void testRemove() { |
319 |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
320 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q); |
321 |
CancellableTask[] tasks = new CancellableTask[5]; |
322 |
for(int i = 0; i < 5; i++){ |
323 |
tasks[i] = new CancellableTask(new MediumPossiblyInterruptedRunnable()); |
324 |
p1.execute(tasks[i]); |
325 |
} |
326 |
try { |
327 |
Thread.sleep(SHORT_DELAY_MS); |
328 |
assertFalse(p1.remove(tasks[0])); |
329 |
assertTrue(q.contains(tasks[4])); |
330 |
assertTrue(q.contains(tasks[3])); |
331 |
assertTrue(p1.remove(tasks[4])); |
332 |
assertFalse(p1.remove(tasks[4])); |
333 |
assertFalse(q.contains(tasks[4])); |
334 |
assertTrue(q.contains(tasks[3])); |
335 |
assertTrue(p1.remove(tasks[3])); |
336 |
assertFalse(q.contains(tasks[3])); |
337 |
p1.shutdownNow(); |
338 |
} catch(Exception e) { |
339 |
unexpectedException(); |
340 |
} finally { |
341 |
joinPool(p1); |
342 |
} |
343 |
} |
344 |
|
345 |
/** |
346 |
* purge removes cancelled tasks from the queue |
347 |
*/ |
348 |
public void testPurge() { |
349 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
350 |
CancellableTask[] tasks = new CancellableTask[5]; |
351 |
for(int i = 0; i < 5; i++){ |
352 |
tasks[i] = new CancellableTask(new MediumPossiblyInterruptedRunnable()); |
353 |
p1.execute(tasks[i]); |
354 |
} |
355 |
tasks[4].cancel(true); |
356 |
tasks[3].cancel(true); |
357 |
p1.purge(); |
358 |
long count = p1.getTaskCount(); |
359 |
assertTrue(count >= 2 && count < 5); |
360 |
p1.shutdownNow(); |
361 |
joinPool(p1); |
362 |
} |
363 |
|
364 |
/** |
365 |
* shutDownNow returns a list containing tasks that were not run |
366 |
*/ |
367 |
public void testShutDownNow() { |
368 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
369 |
List l; |
370 |
try { |
371 |
for(int i = 0; i < 5; i++) |
372 |
p1.execute(new MediumPossiblyInterruptedRunnable()); |
373 |
} |
374 |
finally { |
375 |
l = p1.shutdownNow(); |
376 |
} |
377 |
assertTrue(p1.isShutdown()); |
378 |
assertTrue(l.size() <= 4); |
379 |
} |
380 |
|
381 |
// Exception Tests |
382 |
|
383 |
|
384 |
/** |
385 |
* Constructor throws if corePoolSize argument is less than zero |
386 |
*/ |
387 |
public void testConstructor1() { |
388 |
try { |
389 |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
390 |
shouldThrow(); |
391 |
} |
392 |
catch (IllegalArgumentException success){} |
393 |
} |
394 |
|
395 |
/** |
396 |
* Constructor throws if maximumPoolSize is less than zero |
397 |
*/ |
398 |
public void testConstructor2() { |
399 |
try { |
400 |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
401 |
shouldThrow(); |
402 |
} |
403 |
catch (IllegalArgumentException success){} |
404 |
} |
405 |
|
406 |
/** |
407 |
* Constructor throws if maximumPoolSize is equal to zero |
408 |
*/ |
409 |
public void testConstructor3() { |
410 |
try { |
411 |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
412 |
shouldThrow(); |
413 |
} |
414 |
catch (IllegalArgumentException success){} |
415 |
} |
416 |
|
417 |
/** |
418 |
* Constructor throws if keepAliveTime is less than zero |
419 |
*/ |
420 |
public void testConstructor4() { |
421 |
try { |
422 |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
423 |
shouldThrow(); |
424 |
} |
425 |
catch (IllegalArgumentException success){} |
426 |
} |
427 |
|
428 |
/** |
429 |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
430 |
*/ |
431 |
public void testConstructor5() { |
432 |
try { |
433 |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
434 |
shouldThrow(); |
435 |
} |
436 |
catch (IllegalArgumentException success){} |
437 |
} |
438 |
|
439 |
/** |
440 |
* Constructor throws if workQueue is set to null |
441 |
*/ |
442 |
public void testConstructorNullPointerException() { |
443 |
try { |
444 |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null); |
445 |
shouldThrow(); |
446 |
} |
447 |
catch (NullPointerException success){} |
448 |
} |
449 |
|
450 |
|
451 |
|
452 |
/** |
453 |
* Constructor throws if corePoolSize argument is less than zero |
454 |
*/ |
455 |
public void testConstructor6() { |
456 |
try { |
457 |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
458 |
shouldThrow(); |
459 |
} catch (IllegalArgumentException success){} |
460 |
} |
461 |
|
462 |
/** |
463 |
* Constructor throws if maximumPoolSize is less than zero |
464 |
*/ |
465 |
public void testConstructor7() { |
466 |
try { |
467 |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
468 |
shouldThrow(); |
469 |
} |
470 |
catch (IllegalArgumentException success){} |
471 |
} |
472 |
|
473 |
/** |
474 |
* Constructor throws if maximumPoolSize is equal to zero |
475 |
*/ |
476 |
public void testConstructor8() { |
477 |
try { |
478 |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
479 |
shouldThrow(); |
480 |
} |
481 |
catch (IllegalArgumentException success){} |
482 |
} |
483 |
|
484 |
/** |
485 |
* Constructor throws if keepAliveTime is less than zero |
486 |
*/ |
487 |
public void testConstructor9() { |
488 |
try { |
489 |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
490 |
shouldThrow(); |
491 |
} |
492 |
catch (IllegalArgumentException success){} |
493 |
} |
494 |
|
495 |
/** |
496 |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
497 |
*/ |
498 |
public void testConstructor10() { |
499 |
try { |
500 |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
501 |
shouldThrow(); |
502 |
} |
503 |
catch (IllegalArgumentException success){} |
504 |
} |
505 |
|
506 |
/** |
507 |
* Constructor throws if workQueue is set to null |
508 |
*/ |
509 |
public void testConstructorNullPointerException2() { |
510 |
try { |
511 |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory()); |
512 |
shouldThrow(); |
513 |
} |
514 |
catch (NullPointerException success){} |
515 |
} |
516 |
|
517 |
/** |
518 |
* Constructor throws if threadFactory is set to null |
519 |
*/ |
520 |
public void testConstructorNullPointerException3() { |
521 |
try { |
522 |
ThreadFactory f = null; |
523 |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
524 |
shouldThrow(); |
525 |
} |
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, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
536 |
shouldThrow(); |
537 |
} |
538 |
catch (IllegalArgumentException success){} |
539 |
} |
540 |
|
541 |
/** |
542 |
* Constructor throws if maximumPoolSize is less than zero |
543 |
*/ |
544 |
public void testConstructor12() { |
545 |
try { |
546 |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
547 |
shouldThrow(); |
548 |
} |
549 |
catch (IllegalArgumentException success){} |
550 |
} |
551 |
|
552 |
/** |
553 |
* Constructor throws if maximumPoolSize is equal to zero |
554 |
*/ |
555 |
public void testConstructor13() { |
556 |
try { |
557 |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
558 |
shouldThrow(); |
559 |
} |
560 |
catch (IllegalArgumentException success){} |
561 |
} |
562 |
|
563 |
/** |
564 |
* Constructor throws if keepAliveTime is less than zero |
565 |
*/ |
566 |
public void testConstructor14() { |
567 |
try { |
568 |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
569 |
shouldThrow(); |
570 |
} |
571 |
catch (IllegalArgumentException success){} |
572 |
} |
573 |
|
574 |
/** |
575 |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
576 |
*/ |
577 |
public void testConstructor15() { |
578 |
try { |
579 |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
580 |
shouldThrow(); |
581 |
} |
582 |
catch (IllegalArgumentException success){} |
583 |
} |
584 |
|
585 |
/** |
586 |
* Constructor throws if workQueue is set to null |
587 |
*/ |
588 |
public void testConstructorNullPointerException4() { |
589 |
try { |
590 |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler()); |
591 |
shouldThrow(); |
592 |
} |
593 |
catch (NullPointerException success){} |
594 |
} |
595 |
|
596 |
/** |
597 |
* Constructor throws if handler is set to null |
598 |
*/ |
599 |
public void testConstructorNullPointerException5() { |
600 |
try { |
601 |
RejectedExecutionHandler r = null; |
602 |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
603 |
shouldThrow(); |
604 |
} |
605 |
catch (NullPointerException success){} |
606 |
} |
607 |
|
608 |
|
609 |
/** |
610 |
* Constructor throws if corePoolSize argument is less than zero |
611 |
*/ |
612 |
public void testConstructor16() { |
613 |
try { |
614 |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
615 |
shouldThrow(); |
616 |
} |
617 |
catch (IllegalArgumentException success){} |
618 |
} |
619 |
|
620 |
/** |
621 |
* Constructor throws if maximumPoolSize is less than zero |
622 |
*/ |
623 |
public void testConstructor17() { |
624 |
try { |
625 |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
626 |
shouldThrow(); |
627 |
} |
628 |
catch (IllegalArgumentException success){} |
629 |
} |
630 |
|
631 |
/** |
632 |
* Constructor throws if maximumPoolSize is equal to zero |
633 |
*/ |
634 |
public void testConstructor18() { |
635 |
try { |
636 |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
637 |
shouldThrow(); |
638 |
} |
639 |
catch (IllegalArgumentException success){} |
640 |
} |
641 |
|
642 |
/** |
643 |
* Constructor throws if keepAliveTime is less than zero |
644 |
*/ |
645 |
public void testConstructor19() { |
646 |
try { |
647 |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
648 |
shouldThrow(); |
649 |
} |
650 |
catch (IllegalArgumentException success){} |
651 |
} |
652 |
|
653 |
/** |
654 |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
655 |
*/ |
656 |
public void testConstructor20() { |
657 |
try { |
658 |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
659 |
shouldThrow(); |
660 |
} |
661 |
catch (IllegalArgumentException success){} |
662 |
} |
663 |
|
664 |
/** |
665 |
* Constructor throws if workQueue is set to null |
666 |
*/ |
667 |
public void testConstructorNullPointerException6() { |
668 |
try { |
669 |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); |
670 |
shouldThrow(); |
671 |
} |
672 |
catch (NullPointerException success){} |
673 |
} |
674 |
|
675 |
/** |
676 |
* Constructor throws if handler is set to null |
677 |
*/ |
678 |
public void testConstructorNullPointerException7() { |
679 |
try { |
680 |
RejectedExecutionHandler r = null; |
681 |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r); |
682 |
shouldThrow(); |
683 |
} |
684 |
catch (NullPointerException success){} |
685 |
} |
686 |
|
687 |
/** |
688 |
* Constructor throws if ThreadFactory is set top null |
689 |
*/ |
690 |
public void testConstructorNullPointerException8() { |
691 |
try { |
692 |
ThreadFactory f = null; |
693 |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler()); |
694 |
shouldThrow(); |
695 |
} |
696 |
catch (NullPointerException successdn8){} |
697 |
} |
698 |
|
699 |
|
700 |
/** |
701 |
* execute throws RejectedExecutionException |
702 |
* if saturated. |
703 |
*/ |
704 |
public void testSaturatedExecute() { |
705 |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
706 |
try { |
707 |
|
708 |
for(int i = 0; i < 5; ++i){ |
709 |
p.execute(new MediumRunnable()); |
710 |
} |
711 |
shouldThrow(); |
712 |
} catch(RejectedExecutionException success){} |
713 |
joinPool(p); |
714 |
} |
715 |
|
716 |
/** |
717 |
* executor using CallerRunsPolicy runs task if saturated. |
718 |
*/ |
719 |
public void testSaturatedExecute2() { |
720 |
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
721 |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
722 |
try { |
723 |
|
724 |
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
725 |
for(int i = 0; i < 5; ++i){ |
726 |
tasks[i] = new TrackedNoOpRunnable(); |
727 |
} |
728 |
TrackedLongRunnable mr = new TrackedLongRunnable(); |
729 |
p.execute(mr); |
730 |
for(int i = 0; i < 5; ++i){ |
731 |
p.execute(tasks[i]); |
732 |
} |
733 |
for(int i = 1; i < 5; ++i) { |
734 |
assertTrue(tasks[i].done); |
735 |
} |
736 |
p.shutdownNow(); |
737 |
} catch(RejectedExecutionException ex){ |
738 |
unexpectedException(); |
739 |
} finally { |
740 |
joinPool(p); |
741 |
} |
742 |
} |
743 |
|
744 |
/** |
745 |
* executor using DiscardPolicy drops task if saturated. |
746 |
*/ |
747 |
public void testSaturatedExecute3() { |
748 |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
749 |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
750 |
try { |
751 |
|
752 |
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
753 |
for(int i = 0; i < 5; ++i){ |
754 |
tasks[i] = new TrackedNoOpRunnable(); |
755 |
} |
756 |
p.execute(new TrackedLongRunnable()); |
757 |
for(int i = 0; i < 5; ++i){ |
758 |
p.execute(tasks[i]); |
759 |
} |
760 |
for(int i = 0; i < 5; ++i){ |
761 |
assertFalse(tasks[i].done); |
762 |
} |
763 |
p.shutdownNow(); |
764 |
} catch(RejectedExecutionException ex){ |
765 |
unexpectedException(); |
766 |
} finally { |
767 |
joinPool(p); |
768 |
} |
769 |
} |
770 |
|
771 |
/** |
772 |
* executor using DiscardOldestPolicy drops oldest task if saturated. |
773 |
*/ |
774 |
public void testSaturatedExecute4() { |
775 |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
776 |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
777 |
try { |
778 |
p.execute(new TrackedLongRunnable()); |
779 |
TrackedLongRunnable r2 = new TrackedLongRunnable(); |
780 |
p.execute(r2); |
781 |
assertTrue(p.getQueue().contains(r2)); |
782 |
TrackedNoOpRunnable r3 = new TrackedNoOpRunnable(); |
783 |
p.execute(r3); |
784 |
assertFalse(p.getQueue().contains(r2)); |
785 |
assertTrue(p.getQueue().contains(r3)); |
786 |
p.shutdownNow(); |
787 |
} catch(RejectedExecutionException ex){ |
788 |
unexpectedException(); |
789 |
} finally { |
790 |
joinPool(p); |
791 |
} |
792 |
} |
793 |
|
794 |
/** |
795 |
* execute throws RejectedExecutionException if shutdown |
796 |
*/ |
797 |
public void testRejectedExecutionExceptionOnShutdown() { |
798 |
ThreadPoolExecutor tpe = |
799 |
new ThreadPoolExecutor(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
800 |
tpe.shutdown(); |
801 |
try { |
802 |
tpe.execute(new NoOpRunnable()); |
803 |
shouldThrow(); |
804 |
} catch(RejectedExecutionException success){} |
805 |
|
806 |
joinPool(tpe); |
807 |
} |
808 |
|
809 |
/** |
810 |
* execute using CallerRunsPolicy drops task on shutdown |
811 |
*/ |
812 |
public void testCallerRunsOnShutdown() { |
813 |
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
814 |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
815 |
|
816 |
p.shutdown(); |
817 |
try { |
818 |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
819 |
p.execute(r); |
820 |
assertFalse(r.done); |
821 |
} catch(RejectedExecutionException success){ |
822 |
unexpectedException(); |
823 |
} finally { |
824 |
joinPool(p); |
825 |
} |
826 |
} |
827 |
|
828 |
/** |
829 |
* execute using DiscardPolicy drops task on shutdown |
830 |
*/ |
831 |
public void testDiscardOnShutdown() { |
832 |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
833 |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
834 |
|
835 |
p.shutdown(); |
836 |
try { |
837 |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
838 |
p.execute(r); |
839 |
assertFalse(r.done); |
840 |
} catch(RejectedExecutionException success){ |
841 |
unexpectedException(); |
842 |
} finally { |
843 |
joinPool(p); |
844 |
} |
845 |
} |
846 |
|
847 |
|
848 |
/** |
849 |
* execute using DiscardOldestPolicy drops task on shutdown |
850 |
*/ |
851 |
public void testDiscardOldestOnShutdown() { |
852 |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
853 |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
854 |
|
855 |
p.shutdown(); |
856 |
try { |
857 |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
858 |
p.execute(r); |
859 |
assertFalse(r.done); |
860 |
} catch(RejectedExecutionException success){ |
861 |
unexpectedException(); |
862 |
} finally { |
863 |
joinPool(p); |
864 |
} |
865 |
} |
866 |
|
867 |
|
868 |
/** |
869 |
* execute (null) throws NPE |
870 |
*/ |
871 |
public void testExecuteNull() { |
872 |
ThreadPoolExecutor tpe = null; |
873 |
try { |
874 |
tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
875 |
tpe.execute(null); |
876 |
shouldThrow(); |
877 |
} catch(NullPointerException success){} |
878 |
|
879 |
joinPool(tpe); |
880 |
} |
881 |
|
882 |
/** |
883 |
* setCorePoolSize of negative value throws IllegalArgumentException |
884 |
*/ |
885 |
public void testCorePoolSizeIllegalArgumentException() { |
886 |
ThreadPoolExecutor tpe = null; |
887 |
try { |
888 |
tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
889 |
} catch(Exception e){} |
890 |
try { |
891 |
tpe.setCorePoolSize(-1); |
892 |
shouldThrow(); |
893 |
} catch(IllegalArgumentException success){ |
894 |
} finally { |
895 |
tpe.shutdown(); |
896 |
} |
897 |
joinPool(tpe); |
898 |
} |
899 |
|
900 |
/** |
901 |
* setMaximumPoolSize(int) throws IllegalArgumentException if |
902 |
* given a value less the core pool size |
903 |
*/ |
904 |
public void testMaximumPoolSizeIllegalArgumentException() { |
905 |
ThreadPoolExecutor tpe = null; |
906 |
try { |
907 |
tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
908 |
} catch(Exception e){} |
909 |
try { |
910 |
tpe.setMaximumPoolSize(1); |
911 |
shouldThrow(); |
912 |
} catch(IllegalArgumentException success){ |
913 |
} finally { |
914 |
tpe.shutdown(); |
915 |
} |
916 |
joinPool(tpe); |
917 |
} |
918 |
|
919 |
/** |
920 |
* setMaximumPoolSize throws IllegalArgumentException |
921 |
* if given a negative value |
922 |
*/ |
923 |
public void testMaximumPoolSizeIllegalArgumentException2() { |
924 |
ThreadPoolExecutor tpe = null; |
925 |
try { |
926 |
tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
927 |
} catch(Exception e){} |
928 |
try { |
929 |
tpe.setMaximumPoolSize(-1); |
930 |
shouldThrow(); |
931 |
} catch(IllegalArgumentException success){ |
932 |
} finally { |
933 |
tpe.shutdown(); |
934 |
} |
935 |
joinPool(tpe); |
936 |
} |
937 |
|
938 |
|
939 |
/** |
940 |
* setKeepAliveTime throws IllegalArgumentException |
941 |
* when given a negative value |
942 |
*/ |
943 |
public void testKeepAliveTimeIllegalArgumentException() { |
944 |
ThreadPoolExecutor tpe = null; |
945 |
try { |
946 |
tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
947 |
} catch(Exception e){} |
948 |
|
949 |
try { |
950 |
tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS); |
951 |
shouldThrow(); |
952 |
} catch(IllegalArgumentException success){ |
953 |
} finally { |
954 |
tpe.shutdown(); |
955 |
} |
956 |
joinPool(tpe); |
957 |
} |
958 |
|
959 |
/** |
960 |
* terminated() is called on termination |
961 |
*/ |
962 |
public void testTerminated() { |
963 |
ExtendedTPE tpe = new ExtendedTPE(); |
964 |
tpe.shutdown(); |
965 |
assertTrue(tpe.terminatedCalled); |
966 |
joinPool(tpe); |
967 |
} |
968 |
|
969 |
/** |
970 |
* beforeExecute and afterExecute are called when executing task |
971 |
*/ |
972 |
public void testBeforeAfter() { |
973 |
ExtendedTPE tpe = new ExtendedTPE(); |
974 |
try { |
975 |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
976 |
tpe.execute(r); |
977 |
Thread.sleep(SHORT_DELAY_MS); |
978 |
assertTrue(r.done); |
979 |
assertTrue(tpe.beforeCalled); |
980 |
assertTrue(tpe.afterCalled); |
981 |
tpe.shutdown(); |
982 |
} |
983 |
catch(Exception ex) { |
984 |
unexpectedException(); |
985 |
} finally { |
986 |
joinPool(tpe); |
987 |
} |
988 |
} |
989 |
} |