ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.5
Committed: Sat Sep 20 18:20:08 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.4: +179 -158 lines
Log Message:
Documentation scaffolding

File Contents

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