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 |
for(int i = 0; i < 5; i++) |
154 |
one.execute(new MediumRunnable()); |
155 |
Thread.sleep(SHORT_DELAY_MS); |
156 |
assertEquals(5, one.getTaskCount()); |
157 |
} catch(Exception e){ |
158 |
fail("unexpected exception"); |
159 |
} |
160 |
joinPool(one); |
161 |
} |
162 |
|
163 |
/** |
164 |
* isShutDown gives correct values |
165 |
*/ |
166 |
public void testIsShutdown(){ |
167 |
|
168 |
ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
169 |
assertFalse(one.isShutdown()); |
170 |
one.shutdown(); |
171 |
assertTrue(one.isShutdown()); |
172 |
joinPool(one); |
173 |
} |
174 |
|
175 |
|
176 |
/** |
177 |
* isTerminated gives correct values |
178 |
* Makes sure termination does not take an innapropriate |
179 |
* amount of time |
180 |
*/ |
181 |
public void testIsTerminated(){ |
182 |
ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
183 |
try { |
184 |
one.execute(new MediumRunnable()); |
185 |
} finally { |
186 |
one.shutdown(); |
187 |
} |
188 |
try { |
189 |
assertTrue(one.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
190 |
assertTrue(one.isTerminated()); |
191 |
} catch(Exception e){ |
192 |
fail("unexpected exception"); |
193 |
} |
194 |
} |
195 |
|
196 |
/** |
197 |
* purge correctly removes cancelled tasks |
198 |
* from the queue |
199 |
*/ |
200 |
public void testPurge(){ |
201 |
ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
202 |
CancellableTask[] tasks = new CancellableTask[5]; |
203 |
for(int i = 0; i < 5; i++){ |
204 |
tasks[i] = new CancellableTask(new MediumPossiblyInterruptedRunnable()); |
205 |
one.execute(tasks[i]); |
206 |
} |
207 |
tasks[4].cancel(true); |
208 |
tasks[3].cancel(true); |
209 |
one.purge(); |
210 |
long count = one.getTaskCount(); |
211 |
assertTrue(count >= 2 && count < 5); |
212 |
joinPool(one); |
213 |
} |
214 |
|
215 |
/** |
216 |
* shutDownNow returns a list |
217 |
* containing the correct number of elements |
218 |
*/ |
219 |
public void testShutDownNow(){ |
220 |
ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
221 |
List l; |
222 |
try { |
223 |
for(int i = 0; i < 5; i++) |
224 |
one.execute(new MediumPossiblyInterruptedRunnable()); |
225 |
} |
226 |
finally { |
227 |
l = one.shutdownNow(); |
228 |
} |
229 |
assertTrue(one.isShutdown()); |
230 |
assertTrue(l.size() <= 4); |
231 |
} |
232 |
|
233 |
// Exception Tests |
234 |
|
235 |
|
236 |
/** Throws if corePoolSize argument is less than zero */ |
237 |
public void testConstructor1() { |
238 |
try{ |
239 |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
240 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
241 |
} |
242 |
catch (IllegalArgumentException success){} |
243 |
} |
244 |
|
245 |
/** Throws if maximumPoolSize is less than zero */ |
246 |
public void testConstructor2() { |
247 |
try{ |
248 |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
249 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
250 |
} |
251 |
catch (IllegalArgumentException success){} |
252 |
} |
253 |
|
254 |
/** Throws if maximumPoolSize is equal to zero */ |
255 |
public void testConstructor3() { |
256 |
try{ |
257 |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
258 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
259 |
} |
260 |
catch (IllegalArgumentException success){} |
261 |
} |
262 |
|
263 |
/** Throws if keepAliveTime is less than zero */ |
264 |
public void testConstructor4() { |
265 |
try{ |
266 |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
267 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
268 |
} |
269 |
catch (IllegalArgumentException success){} |
270 |
} |
271 |
|
272 |
/** Throws if corePoolSize is greater than the maximumPoolSize */ |
273 |
public void testConstructor5() { |
274 |
try{ |
275 |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
276 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
277 |
} |
278 |
catch (IllegalArgumentException success){} |
279 |
} |
280 |
|
281 |
/** Throws if workQueue is set to null */ |
282 |
public void testNullPointerException() { |
283 |
try{ |
284 |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null); |
285 |
fail("ThreadPoolExecutor constructor should throw a NullPointerException"); |
286 |
} |
287 |
catch (NullPointerException success){} |
288 |
} |
289 |
|
290 |
|
291 |
|
292 |
/** Throws if corePoolSize argument is less than zero */ |
293 |
public void testConstructor6() { |
294 |
try{ |
295 |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory()); |
296 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
297 |
} catch (IllegalArgumentException success){} |
298 |
} |
299 |
|
300 |
/** Throws if maximumPoolSize is less than zero */ |
301 |
public void testConstructor7() { |
302 |
try{ |
303 |
new ThreadPoolExecutor(1,-1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory()); |
304 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
305 |
} |
306 |
catch (IllegalArgumentException success){} |
307 |
} |
308 |
|
309 |
/** Throws if maximumPoolSize is equal to zero */ |
310 |
public void testConstructor8() { |
311 |
try{ |
312 |
new ThreadPoolExecutor(1,0,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory()); |
313 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
314 |
} |
315 |
catch (IllegalArgumentException success){} |
316 |
} |
317 |
|
318 |
/** Throws if keepAliveTime is less than zero */ |
319 |
public void testConstructor9() { |
320 |
try{ |
321 |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory()); |
322 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
323 |
} |
324 |
catch (IllegalArgumentException success){} |
325 |
} |
326 |
|
327 |
/** Throws if corePoolSize is greater than the maximumPoolSize */ |
328 |
public void testConstructor10() { |
329 |
try{ |
330 |
new ThreadPoolExecutor(2,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory()); |
331 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
332 |
} |
333 |
catch (IllegalArgumentException success){} |
334 |
} |
335 |
|
336 |
/** Throws if workQueue is set to null */ |
337 |
public void testNullPointerException2() { |
338 |
try{ |
339 |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null,new MyThreadFactory()); |
340 |
fail("ThreadPoolExecutor constructor should throw a NullPointerException"); |
341 |
} |
342 |
catch (NullPointerException success){} |
343 |
} |
344 |
|
345 |
/** Throws if threadFactory is set to null */ |
346 |
public void testNullPointerException3() { |
347 |
try{ |
348 |
ThreadFactory f = null; |
349 |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
350 |
fail("ThreadPoolExecutor constructor should throw a NullPointerException"); |
351 |
} |
352 |
catch (NullPointerException success){} |
353 |
} |
354 |
|
355 |
|
356 |
/** Throws if corePoolSize argument is less than zero */ |
357 |
public void testConstructor11() { |
358 |
try{ |
359 |
new ThreadPoolExecutor(-1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler()); |
360 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
361 |
} |
362 |
catch (IllegalArgumentException success){} |
363 |
} |
364 |
|
365 |
/** Throws if maximumPoolSize is less than zero */ |
366 |
public void testConstructor12() { |
367 |
try{ |
368 |
new ThreadPoolExecutor(1,-1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler()); |
369 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
370 |
} |
371 |
catch (IllegalArgumentException success){} |
372 |
} |
373 |
|
374 |
/** Throws if maximumPoolSize is equal to zero */ |
375 |
public void testConstructor13() { |
376 |
try{ |
377 |
new ThreadPoolExecutor(1,0,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler()); |
378 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
379 |
} |
380 |
catch (IllegalArgumentException success){} |
381 |
} |
382 |
|
383 |
/** Throws if keepAliveTime is less than zero */ |
384 |
public void testConstructor14() { |
385 |
try{ |
386 |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler()); |
387 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
388 |
} |
389 |
catch (IllegalArgumentException success){} |
390 |
} |
391 |
|
392 |
/** Throws if corePoolSize is greater than the maximumPoolSize */ |
393 |
public void testConstructor15() { |
394 |
try{ |
395 |
new ThreadPoolExecutor(2,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler()); |
396 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
397 |
} |
398 |
catch (IllegalArgumentException success){} |
399 |
} |
400 |
|
401 |
/** Throws if workQueue is set to null */ |
402 |
public void testNullPointerException4() { |
403 |
try{ |
404 |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null,new MyREHandler()); |
405 |
fail("ThreadPoolExecutor constructor should throw a NullPointerException"); |
406 |
} |
407 |
catch (NullPointerException success){} |
408 |
} |
409 |
|
410 |
/** Throws if handler is set to null */ |
411 |
public void testNullPointerException5() { |
412 |
try{ |
413 |
RejectedExecutionHandler r = null; |
414 |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
415 |
fail("ThreadPoolExecutor constructor should throw a NullPointerException"); |
416 |
} |
417 |
catch (NullPointerException success){} |
418 |
} |
419 |
|
420 |
|
421 |
/** Throws if corePoolSize argument is less than zero */ |
422 |
public void testConstructor16() { |
423 |
try{ |
424 |
new ThreadPoolExecutor(-1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),new MyREHandler()); |
425 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
426 |
} |
427 |
catch (IllegalArgumentException success){} |
428 |
} |
429 |
|
430 |
/** Throws if maximumPoolSize is less than zero */ |
431 |
public void testConstructor17() { |
432 |
try{ |
433 |
new ThreadPoolExecutor(1,-1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),new MyREHandler()); |
434 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
435 |
} |
436 |
catch (IllegalArgumentException success){} |
437 |
} |
438 |
|
439 |
/** Throws if maximumPoolSize is equal to zero */ |
440 |
public void testConstructor18() { |
441 |
try{ |
442 |
new ThreadPoolExecutor(1,0,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),new MyREHandler()); |
443 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
444 |
} |
445 |
catch (IllegalArgumentException success){} |
446 |
} |
447 |
|
448 |
/** Throws if keepAliveTime is less than zero */ |
449 |
public void testConstructor19() { |
450 |
try{ |
451 |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),new MyREHandler()); |
452 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
453 |
} |
454 |
catch (IllegalArgumentException success){} |
455 |
} |
456 |
|
457 |
/** Throws if corePoolSize is greater than the maximumPoolSize */ |
458 |
public void testConstructor20() { |
459 |
try{ |
460 |
new ThreadPoolExecutor(2,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),new MyREHandler()); |
461 |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
462 |
} |
463 |
catch (IllegalArgumentException success){} |
464 |
} |
465 |
|
466 |
/** Throws if workQueue is set to null */ |
467 |
public void testNullPointerException6() { |
468 |
try{ |
469 |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null,new MyThreadFactory(),new MyREHandler()); |
470 |
fail("ThreadPoolExecutor constructor should throw a NullPointerException"); |
471 |
} |
472 |
catch (NullPointerException success){} |
473 |
} |
474 |
|
475 |
/** Throws if handler is set to null */ |
476 |
public void testNullPointerException7() { |
477 |
try{ |
478 |
RejectedExecutionHandler r = null; |
479 |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),r); |
480 |
fail("ThreadPoolExecutor constructor should throw a NullPointerException"); |
481 |
} |
482 |
catch (NullPointerException success){} |
483 |
} |
484 |
|
485 |
/** Throws if ThreadFactory is set top null */ |
486 |
public void testNullPointerException8() { |
487 |
try{ |
488 |
ThreadFactory f = null; |
489 |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new MyREHandler()); |
490 |
fail("ThreadPoolExecutor constructor should throw a NullPointerException"); |
491 |
} |
492 |
catch (NullPointerException successdn8){} |
493 |
} |
494 |
|
495 |
|
496 |
/** |
497 |
* execute will throw RejectedExcutionException |
498 |
* ThreadPoolExecutor will throw one when more runnables are |
499 |
* executed then will fit in the Queue. |
500 |
*/ |
501 |
public void testRejectedExecutionException(){ |
502 |
ThreadPoolExecutor tpe = null; |
503 |
try{ |
504 |
tpe = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
505 |
} catch(Exception e){} |
506 |
tpe.shutdown(); |
507 |
try{ |
508 |
tpe.execute(new NoOpRunnable()); |
509 |
fail("ThreadPoolExecutor - void execute(Runnable) should throw RejectedExecutionException"); |
510 |
} catch(RejectedExecutionException success){} |
511 |
|
512 |
joinPool(tpe); |
513 |
} |
514 |
|
515 |
/** |
516 |
* setCorePoolSize will throw IllegalArgumentException |
517 |
* when given a negative |
518 |
*/ |
519 |
public void testCorePoolSizeIllegalArgumentException(){ |
520 |
ThreadPoolExecutor tpe = null; |
521 |
try{ |
522 |
tpe = new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
523 |
} catch(Exception e){} |
524 |
try{ |
525 |
tpe.setCorePoolSize(-1); |
526 |
fail("ThreadPoolExecutor - void setCorePoolSize(int) should throw IllegalArgumentException"); |
527 |
} catch(IllegalArgumentException success){ |
528 |
} finally { |
529 |
tpe.shutdown(); |
530 |
} |
531 |
joinPool(tpe); |
532 |
} |
533 |
|
534 |
|
535 |
/** |
536 |
* setMaximumPoolSize(int) will throw IllegalArgumentException |
537 |
* if given a value less the it's actual core pool size |
538 |
*/ |
539 |
public void testMaximumPoolSizeIllegalArgumentException(){ |
540 |
ThreadPoolExecutor tpe = null; |
541 |
try{ |
542 |
tpe = new ThreadPoolExecutor(2,3,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
543 |
} catch(Exception e){} |
544 |
try{ |
545 |
tpe.setMaximumPoolSize(1); |
546 |
fail("ThreadPoolExecutor - void setMaximumPoolSize(int) should throw IllegalArgumentException"); |
547 |
} catch(IllegalArgumentException success){ |
548 |
} finally { |
549 |
tpe.shutdown(); |
550 |
} |
551 |
joinPool(tpe); |
552 |
} |
553 |
|
554 |
/** |
555 |
* setMaximumPoolSize will throw IllegalArgumentException |
556 |
* if given a negative number |
557 |
*/ |
558 |
public void testMaximumPoolSizeIllegalArgumentException2(){ |
559 |
ThreadPoolExecutor tpe = null; |
560 |
try{ |
561 |
tpe = new ThreadPoolExecutor(2,3,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
562 |
} catch(Exception e){} |
563 |
try{ |
564 |
tpe.setMaximumPoolSize(-1); |
565 |
fail("ThreadPoolExecutor - void setMaximumPoolSize(int) should throw IllegalArgumentException"); |
566 |
} catch(IllegalArgumentException success){ |
567 |
} finally { |
568 |
tpe.shutdown(); |
569 |
} |
570 |
joinPool(tpe); |
571 |
} |
572 |
|
573 |
|
574 |
/** |
575 |
* setKeepAliveTime will throw IllegalArgumentException |
576 |
* when given a negative value |
577 |
*/ |
578 |
public void testKeepAliveTimeIllegalArgumentException(){ |
579 |
ThreadPoolExecutor tpe = null; |
580 |
try{ |
581 |
tpe = new ThreadPoolExecutor(2,3,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
582 |
} catch(Exception e){} |
583 |
|
584 |
try{ |
585 |
tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS); |
586 |
fail("ThreadPoolExecutor - void setKeepAliveTime(long, TimeUnit) should throw IllegalArgumentException"); |
587 |
} catch(IllegalArgumentException success){ |
588 |
} finally { |
589 |
tpe.shutdown(); |
590 |
} |
591 |
joinPool(tpe); |
592 |
} |
593 |
|
594 |
} |