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 |
/** |
21 |
* For use as ThreadFactory in constructors |
22 |
*/ |
23 |
static class MyThreadFactory implements ThreadFactory{ |
24 |
public Thread newThread(Runnable r){ |
25 |
return new Thread(r); |
26 |
} |
27 |
} |
28 |
|
29 |
/** |
30 |
* For use as RejectedExecutionHandler in constructors |
31 |
*/ |
32 |
static class MyREHandler implements RejectedExecutionHandler{ |
33 |
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){} |
34 |
} |
35 |
|
36 |
/** |
37 |
* execute successfully executes a runnable |
38 |
*/ |
39 |
public void testExecute(){ |
40 |
ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
41 |
try { |
42 |
one.execute(new Runnable(){ |
43 |
public void run(){ |
44 |
try{ |
45 |
Thread.sleep(SHORT_DELAY_MS); |
46 |
} catch(InterruptedException e){ |
47 |
fail("unexpected exception"); |
48 |
} |
49 |
} |
50 |
}); |
51 |
Thread.sleep(SMALL_DELAY_MS); |
52 |
} catch(InterruptedException e){ |
53 |
fail("unexpected exception"); |
54 |
} |
55 |
joinPool(one); |
56 |
} |
57 |
|
58 |
/** |
59 |
* getActiveCount gives correct values |
60 |
*/ |
61 |
public void testGetActiveCount(){ |
62 |
ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
63 |
assertEquals(0, two.getActiveCount()); |
64 |
two.execute(new MediumRunnable()); |
65 |
try{ |
66 |
Thread.sleep(SHORT_DELAY_MS); |
67 |
} catch(Exception e){ |
68 |
fail("unexpected exception"); |
69 |
} |
70 |
assertEquals(1, two.getActiveCount()); |
71 |
joinPool(two); |
72 |
} |
73 |
|
74 |
/** |
75 |
* getCompleteTaskCount gives correct values |
76 |
*/ |
77 |
public void testGetCompletedTaskCount(){ |
78 |
ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
79 |
assertEquals(0, two.getCompletedTaskCount()); |
80 |
two.execute(new ShortRunnable()); |
81 |
try{ |
82 |
Thread.sleep(MEDIUM_DELAY_MS); |
83 |
} catch(Exception e){ |
84 |
fail("unexpected exception"); |
85 |
} |
86 |
assertEquals(1, two.getCompletedTaskCount()); |
87 |
two.shutdown(); |
88 |
joinPool(two); |
89 |
} |
90 |
|
91 |
/** |
92 |
* getCorePoolSize gives correct values |
93 |
*/ |
94 |
public void testGetCorePoolSize(){ |
95 |
ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
96 |
assertEquals(1, one.getCorePoolSize()); |
97 |
joinPool(one); |
98 |
} |
99 |
|
100 |
/** |
101 |
* getKeepAliveTime gives correct values |
102 |
*/ |
103 |
public void testGetKeepAliveTime(){ |
104 |
ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
105 |
assertEquals(1, two.getKeepAliveTime(TimeUnit.SECONDS)); |
106 |
joinPool(two); |
107 |
} |
108 |
|
109 |
/** |
110 |
* getLargestPoolSize gives correct values |
111 |
*/ |
112 |
public void testGetLargestPoolSize(){ |
113 |
ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
114 |
try { |
115 |
assertEquals(0, two.getLargestPoolSize()); |
116 |
two.execute(new MediumRunnable()); |
117 |
two.execute(new MediumRunnable()); |
118 |
Thread.sleep(SHORT_DELAY_MS); |
119 |
assertEquals(2, two.getLargestPoolSize()); |
120 |
} catch(Exception e){ |
121 |
fail("unexpected exception"); |
122 |
} |
123 |
joinPool(two); |
124 |
} |
125 |
|
126 |
/** |
127 |
* getMaximumPoolSize gives correct values |
128 |
*/ |
129 |
public void testGetMaximumPoolSize(){ |
130 |
ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
131 |
assertEquals(2, two.getMaximumPoolSize()); |
132 |
joinPool(two); |
133 |
} |
134 |
|
135 |
/** |
136 |
* getPoolSize gives correct values |
137 |
*/ |
138 |
public void testGetPoolSize(){ |
139 |
ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
140 |
assertEquals(0, one.getPoolSize()); |
141 |
one.execute(new MediumRunnable()); |
142 |
assertEquals(1, one.getPoolSize()); |
143 |
joinPool(one); |
144 |
} |
145 |
|
146 |
/** |
147 |
* getTaskCount gives correct values |
148 |
*/ |
149 |
public void testGetTaskCount(){ |
150 |
ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
151 |
try { |
152 |
assertEquals(0, one.getTaskCount()); |
153 |
one.execute(new MediumRunnable()); |
154 |
Thread.sleep(SHORT_DELAY_MS); |
155 |
assertEquals(1, one.getTaskCount()); |
156 |
} catch(Exception e){ |
157 |
fail("unexpected exception"); |
158 |
} |
159 |
joinPool(one); |
160 |
} |
161 |
|
162 |
/** |
163 |
* isShutDown gives correct values |
164 |
*/ |
165 |
public void testIsShutdown(){ |
166 |
|
167 |
ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
168 |
assertFalse(one.isShutdown()); |
169 |
one.shutdown(); |
170 |
assertTrue(one.isShutdown()); |
171 |
joinPool(one); |
172 |
} |
173 |
|
174 |
|
175 |
/** |
176 |
* isTerminated gives correct values |
177 |
* Makes sure termination does not take an innapropriate |
178 |
* amount of time |
179 |
*/ |
180 |
public void testIsTerminated(){ |
181 |
ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
182 |
try { |
183 |
one.execute(new MediumRunnable()); |
184 |
} finally { |
185 |
one.shutdown(); |
186 |
} |
187 |
try { |
188 |
assertTrue(one.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
189 |
assertTrue(one.isTerminated()); |
190 |
} catch(Exception e){ |
191 |
fail("unexpected exception"); |
192 |
} |
193 |
} |
194 |
|
195 |
/** |
196 |
* purge correctly removes cancelled tasks |
197 |
* from the queue |
198 |
*/ |
199 |
public void testPurge(){ |
200 |
ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
201 |
CancellableTask[] tasks = new CancellableTask[5]; |
202 |
for(int i = 0; i < 5; i++){ |
203 |
tasks[i] = new CancellableTask(new MediumPossiblyInterruptedRunnable()); |
204 |
one.execute(tasks[i]); |
205 |
} |
206 |
tasks[4].cancel(true); |
207 |
tasks[3].cancel(true); |
208 |
one.purge(); |
209 |
long count = one.getTaskCount(); |
210 |
assertTrue(count >= 2 && count < 5); |
211 |
joinPool(one); |
212 |
} |
213 |
|
214 |
/** |
215 |
* shutDownNow returns a list |
216 |
* containing the correct number of elements |
217 |
*/ |
218 |
public void testShutDownNow(){ |
219 |
ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
220 |
List l; |
221 |
try { |
222 |
for(int i = 0; i < 5; i++) |
223 |
one.execute(new MediumPossiblyInterruptedRunnable()); |
224 |
} |
225 |
finally { |
226 |
l = one.shutdownNow(); |
227 |
} |
228 |
assertTrue(one.isShutdown()); |
229 |
assertTrue(l.size() <= 4); |
230 |
} |
231 |
|
232 |
// Exception Tests |
233 |
|
234 |
|
235 |
/** Throws if corePoolSize argument is less than zero */ |
236 |
public void testConstructor1() { |
237 |
try{ |
238 |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
239 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
240 |
} |
241 |
catch (IllegalArgumentException success){} |
242 |
} |
243 |
|
244 |
/** Throws if maximumPoolSize is less than zero */ |
245 |
public void testConstructor2() { |
246 |
try{ |
247 |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
248 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
249 |
} |
250 |
catch (IllegalArgumentException success){} |
251 |
} |
252 |
|
253 |
/** Throws if maximumPoolSize is equal to zero */ |
254 |
public void testConstructor3() { |
255 |
try{ |
256 |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
257 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
258 |
} |
259 |
catch (IllegalArgumentException success){} |
260 |
} |
261 |
|
262 |
/** Throws if keepAliveTime is less than zero */ |
263 |
public void testConstructor4() { |
264 |
try{ |
265 |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
266 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
267 |
} |
268 |
catch (IllegalArgumentException success){} |
269 |
} |
270 |
|
271 |
/** Throws if corePoolSize is greater than the maximumPoolSize */ |
272 |
public void testConstructor5() { |
273 |
try{ |
274 |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
275 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
276 |
} |
277 |
catch (IllegalArgumentException success){} |
278 |
} |
279 |
|
280 |
/** Throws if workQueue is set to null */ |
281 |
public void testNullPointerException() { |
282 |
try{ |
283 |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null); |
284 |
fail("ThreadPoolExecutor constructor should throw a NullPointerException"); |
285 |
} |
286 |
catch (NullPointerException success){} |
287 |
} |
288 |
|
289 |
|
290 |
|
291 |
/** Throws if corePoolSize argument is less than zero */ |
292 |
public void testConstructor6() { |
293 |
try{ |
294 |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory()); |
295 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
296 |
} catch (IllegalArgumentException success){} |
297 |
} |
298 |
|
299 |
/** Throws if maximumPoolSize is less than zero */ |
300 |
public void testConstructor7() { |
301 |
try{ |
302 |
new ThreadPoolExecutor(1,-1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory()); |
303 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
304 |
} |
305 |
catch (IllegalArgumentException success){} |
306 |
} |
307 |
|
308 |
/** Throws if maximumPoolSize is equal to zero */ |
309 |
public void testConstructor8() { |
310 |
try{ |
311 |
new ThreadPoolExecutor(1,0,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory()); |
312 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
313 |
} |
314 |
catch (IllegalArgumentException success){} |
315 |
} |
316 |
|
317 |
/** Throws if keepAliveTime is less than zero */ |
318 |
public void testConstructor9() { |
319 |
try{ |
320 |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory()); |
321 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
322 |
} |
323 |
catch (IllegalArgumentException success){} |
324 |
} |
325 |
|
326 |
/** Throws if corePoolSize is greater than the maximumPoolSize */ |
327 |
public void testConstructor10() { |
328 |
try{ |
329 |
new ThreadPoolExecutor(2,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory()); |
330 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
331 |
} |
332 |
catch (IllegalArgumentException success){} |
333 |
} |
334 |
|
335 |
/** Throws if workQueue is set to null */ |
336 |
public void testNullPointerException2() { |
337 |
try{ |
338 |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null,new MyThreadFactory()); |
339 |
fail("ThreadPoolExecutor constructor should throw a NullPointerException"); |
340 |
} |
341 |
catch (NullPointerException success){} |
342 |
} |
343 |
|
344 |
/** Throws if threadFactory is set to null */ |
345 |
public void testNullPointerException3() { |
346 |
try{ |
347 |
ThreadFactory f = null; |
348 |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
349 |
fail("ThreadPoolExecutor constructor should throw a NullPointerException"); |
350 |
} |
351 |
catch (NullPointerException success){} |
352 |
} |
353 |
|
354 |
|
355 |
/** Throws if corePoolSize argument is less than zero */ |
356 |
public void testConstructor11() { |
357 |
try{ |
358 |
new ThreadPoolExecutor(-1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler()); |
359 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
360 |
} |
361 |
catch (IllegalArgumentException success){} |
362 |
} |
363 |
|
364 |
/** Throws if maximumPoolSize is less than zero */ |
365 |
public void testConstructor12() { |
366 |
try{ |
367 |
new ThreadPoolExecutor(1,-1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler()); |
368 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
369 |
} |
370 |
catch (IllegalArgumentException success){} |
371 |
} |
372 |
|
373 |
/** Throws if maximumPoolSize is equal to zero */ |
374 |
public void testConstructor13() { |
375 |
try{ |
376 |
new ThreadPoolExecutor(1,0,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler()); |
377 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
378 |
} |
379 |
catch (IllegalArgumentException success){} |
380 |
} |
381 |
|
382 |
/** Throws if keepAliveTime is less than zero */ |
383 |
public void testConstructor14() { |
384 |
try{ |
385 |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler()); |
386 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
387 |
} |
388 |
catch (IllegalArgumentException success){} |
389 |
} |
390 |
|
391 |
/** Throws if corePoolSize is greater than the maximumPoolSize */ |
392 |
public void testConstructor15() { |
393 |
try{ |
394 |
new ThreadPoolExecutor(2,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler()); |
395 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
396 |
} |
397 |
catch (IllegalArgumentException success){} |
398 |
} |
399 |
|
400 |
/** Throws if workQueue is set to null */ |
401 |
public void testNullPointerException4() { |
402 |
try{ |
403 |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null,new MyREHandler()); |
404 |
fail("ThreadPoolExecutor constructor should throw a NullPointerException"); |
405 |
} |
406 |
catch (NullPointerException success){} |
407 |
} |
408 |
|
409 |
/** Throws if handler is set to null */ |
410 |
public void testNullPointerException5() { |
411 |
try{ |
412 |
RejectedExecutionHandler r = null; |
413 |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
414 |
fail("ThreadPoolExecutor constructor should throw a NullPointerException"); |
415 |
} |
416 |
catch (NullPointerException success){} |
417 |
} |
418 |
|
419 |
|
420 |
/** Throws if corePoolSize argument is less than zero */ |
421 |
public void testConstructor16() { |
422 |
try{ |
423 |
new ThreadPoolExecutor(-1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),new MyREHandler()); |
424 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
425 |
} |
426 |
catch (IllegalArgumentException success){} |
427 |
} |
428 |
|
429 |
/** Throws if maximumPoolSize is less than zero */ |
430 |
public void testConstructor17() { |
431 |
try{ |
432 |
new ThreadPoolExecutor(1,-1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),new MyREHandler()); |
433 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
434 |
} |
435 |
catch (IllegalArgumentException success){} |
436 |
} |
437 |
|
438 |
/** Throws if maximumPoolSize is equal to zero */ |
439 |
public void testConstructor18() { |
440 |
try{ |
441 |
new ThreadPoolExecutor(1,0,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),new MyREHandler()); |
442 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
443 |
} |
444 |
catch (IllegalArgumentException success){} |
445 |
} |
446 |
|
447 |
/** Throws if keepAliveTime is less than zero */ |
448 |
public void testConstructor19() { |
449 |
try{ |
450 |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),new MyREHandler()); |
451 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
452 |
} |
453 |
catch (IllegalArgumentException success){} |
454 |
} |
455 |
|
456 |
/** Throws if corePoolSize is greater than the maximumPoolSize */ |
457 |
public void testConstructor20() { |
458 |
try{ |
459 |
new ThreadPoolExecutor(2,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),new MyREHandler()); |
460 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
461 |
} |
462 |
catch (IllegalArgumentException success){} |
463 |
} |
464 |
|
465 |
/** Throws if workQueue is set to null */ |
466 |
public void testNullPointerException6() { |
467 |
try{ |
468 |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null,new MyThreadFactory(),new MyREHandler()); |
469 |
fail("ThreadPoolExecutor constructor should throw a NullPointerException"); |
470 |
} |
471 |
catch (NullPointerException success){} |
472 |
} |
473 |
|
474 |
/** Throws if handler is set to null */ |
475 |
public void testNullPointerException7() { |
476 |
try{ |
477 |
RejectedExecutionHandler r = null; |
478 |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),r); |
479 |
fail("ThreadPoolExecutor constructor should throw a NullPointerException"); |
480 |
} |
481 |
catch (NullPointerException success){} |
482 |
} |
483 |
|
484 |
/** Throws if ThreadFactory is set top null */ |
485 |
public void testNullPointerException8() { |
486 |
try{ |
487 |
ThreadFactory f = null; |
488 |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new MyREHandler()); |
489 |
fail("ThreadPoolExecutor constructor should throw a NullPointerException"); |
490 |
} |
491 |
catch (NullPointerException successdn8){} |
492 |
} |
493 |
|
494 |
|
495 |
/** |
496 |
* execute will throw RejectedExcutionException |
497 |
* ThreadPoolExecutor will throw one when more runnables are |
498 |
* executed then will fit in the Queue. |
499 |
*/ |
500 |
public void testRejectedExecutionException(){ |
501 |
ThreadPoolExecutor tpe = null; |
502 |
try{ |
503 |
tpe = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
504 |
} catch(Exception e){} |
505 |
tpe.shutdown(); |
506 |
try{ |
507 |
tpe.execute(new NoOpRunnable()); |
508 |
fail("ThreadPoolExecutor - void execute(Runnable) should throw RejectedExecutionException"); |
509 |
} catch(RejectedExecutionException success){} |
510 |
|
511 |
joinPool(tpe); |
512 |
} |
513 |
|
514 |
/** |
515 |
* setCorePoolSize will throw IllegalArgumentException |
516 |
* when given a negative |
517 |
*/ |
518 |
public void testCorePoolSizeIllegalArgumentException(){ |
519 |
ThreadPoolExecutor tpe = null; |
520 |
try{ |
521 |
tpe = new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
522 |
} catch(Exception e){} |
523 |
try{ |
524 |
tpe.setCorePoolSize(-1); |
525 |
fail("ThreadPoolExecutor - void setCorePoolSize(int) should throw IllegalArgumentException"); |
526 |
} catch(IllegalArgumentException success){ |
527 |
} finally { |
528 |
tpe.shutdown(); |
529 |
} |
530 |
joinPool(tpe); |
531 |
} |
532 |
|
533 |
|
534 |
/** |
535 |
* setMaximumPoolSize(int) will throw IllegalArgumentException |
536 |
* if given a value less the it's actual core pool size |
537 |
*/ |
538 |
public void testMaximumPoolSizeIllegalArgumentException(){ |
539 |
ThreadPoolExecutor tpe = null; |
540 |
try{ |
541 |
tpe = new ThreadPoolExecutor(2,3,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
542 |
} catch(Exception e){} |
543 |
try{ |
544 |
tpe.setMaximumPoolSize(1); |
545 |
fail("ThreadPoolExecutor - void setMaximumPoolSize(int) should throw IllegalArgumentException"); |
546 |
} catch(IllegalArgumentException success){ |
547 |
} finally { |
548 |
tpe.shutdown(); |
549 |
} |
550 |
joinPool(tpe); |
551 |
} |
552 |
|
553 |
/** |
554 |
* setMaximumPoolSize will throw IllegalArgumentException |
555 |
* if given a negative number |
556 |
*/ |
557 |
public void testMaximumPoolSizeIllegalArgumentException2(){ |
558 |
ThreadPoolExecutor tpe = null; |
559 |
try{ |
560 |
tpe = new ThreadPoolExecutor(2,3,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
561 |
} catch(Exception e){} |
562 |
try{ |
563 |
tpe.setMaximumPoolSize(-1); |
564 |
fail("ThreadPoolExecutor - void setMaximumPoolSize(int) should throw IllegalArgumentException"); |
565 |
} catch(IllegalArgumentException success){ |
566 |
} finally { |
567 |
tpe.shutdown(); |
568 |
} |
569 |
joinPool(tpe); |
570 |
} |
571 |
|
572 |
|
573 |
/** |
574 |
* setKeepAliveTime will throw IllegalArgumentException |
575 |
* when given a negative value |
576 |
*/ |
577 |
public void testKeepAliveTimeIllegalArgumentException(){ |
578 |
ThreadPoolExecutor tpe = null; |
579 |
try{ |
580 |
tpe = new ThreadPoolExecutor(2,3,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
581 |
} catch(Exception e){} |
582 |
|
583 |
try{ |
584 |
tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS); |
585 |
fail("ThreadPoolExecutor - void setKeepAliveTime(long, TimeUnit) should throw IllegalArgumentException"); |
586 |
} catch(IllegalArgumentException success){ |
587 |
} finally { |
588 |
tpe.shutdown(); |
589 |
} |
590 |
joinPool(tpe); |
591 |
} |
592 |
|
593 |
} |