ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.12
Committed: Mon Dec 22 00:48:56 2003 UTC (20 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.11: +240 -1 lines
Log Message:
Add and adjust tests reflecting API changes

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.*;
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 * getRejectedExecutionHandler returns handler in constructor if not set
182 */
183 public void testGetRejectedExecutionHandler() {
184 RejectedExecutionHandler h = new NoOpREHandler();
185 ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
186 assertSame(h, p.getRejectedExecutionHandler());
187 p.shutdown();
188 joinPool(p);
189 }
190
191 /**
192 * setRejectedExecutionHandler sets the handler returned by
193 * getRejectedExecutionHandler
194 */
195 public void testSetRejectedExecutionHandler() {
196 ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
197 RejectedExecutionHandler h = new NoOpREHandler();
198 p.setRejectedExecutionHandler(h);
199 assertSame(h, p.getRejectedExecutionHandler());
200 p.shutdown();
201 joinPool(p);
202 }
203
204
205 /**
206 * setRejectedExecutionHandler(null) throws NPE
207 */
208 public void testSetRejectedExecutionHandlerNull() {
209 ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
210 try {
211 p.setRejectedExecutionHandler(null);
212 shouldThrow();
213 } catch (NullPointerException success) {
214 } finally {
215 joinPool(p);
216 }
217 }
218
219
220 /**
221 * getLargestPoolSize increases, but doesn't overestimate, when
222 * multiple threads active
223 */
224 public void testGetLargestPoolSize() {
225 ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
226 try {
227 assertEquals(0, p2.getLargestPoolSize());
228 p2.execute(new MediumRunnable());
229 p2.execute(new MediumRunnable());
230 Thread.sleep(SHORT_DELAY_MS);
231 assertEquals(2, p2.getLargestPoolSize());
232 } catch(Exception e){
233 unexpectedException();
234 }
235 joinPool(p2);
236 }
237
238 /**
239 * getMaximumPoolSize returns value given in constructor if not
240 * otherwise set
241 */
242 public void testGetMaximumPoolSize() {
243 ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
244 assertEquals(2, p2.getMaximumPoolSize());
245 joinPool(p2);
246 }
247
248 /**
249 * getPoolSize increases, but doesn't overestimate, when threads
250 * become active
251 */
252 public void testGetPoolSize() {
253 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
254 assertEquals(0, p1.getPoolSize());
255 p1.execute(new MediumRunnable());
256 assertEquals(1, p1.getPoolSize());
257 joinPool(p1);
258 }
259
260 /**
261 * getTaskCount increases, but doesn't overestimate, when tasks submitted
262 */
263 public void testGetTaskCount() {
264 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
265 try {
266 assertEquals(0, p1.getTaskCount());
267 p1.execute(new MediumRunnable());
268 Thread.sleep(SHORT_DELAY_MS);
269 assertEquals(1, p1.getTaskCount());
270 } catch(Exception e){
271 unexpectedException();
272 }
273 joinPool(p1);
274 }
275
276 /**
277 * isShutDown is false before shutdown, true after
278 */
279 public void testIsShutdown() {
280
281 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
282 assertFalse(p1.isShutdown());
283 p1.shutdown();
284 assertTrue(p1.isShutdown());
285 joinPool(p1);
286 }
287
288
289 /**
290 * isTerminated is false before termination, true after
291 */
292 public void testIsTerminated() {
293 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
294 assertFalse(p1.isTerminated());
295 try {
296 p1.execute(new MediumRunnable());
297 } finally {
298 p1.shutdown();
299 }
300 try {
301 assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
302 assertTrue(p1.isTerminated());
303 } catch(Exception e){
304 unexpectedException();
305 }
306 }
307
308 /**
309 * isTerminating is not true when running or when terminated
310 */
311 public void testIsTerminating() {
312 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
313 assertFalse(p1.isTerminating());
314 try {
315 p1.execute(new SmallRunnable());
316 assertFalse(p1.isTerminating());
317 } finally {
318 p1.shutdown();
319 }
320 try {
321 assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
322 assertTrue(p1.isTerminated());
323 assertFalse(p1.isTerminating());
324 } catch(Exception e){
325 unexpectedException();
326 }
327 }
328
329 /**
330 * getQueue returns the work queue, which contains queued tasks
331 */
332 public void testGetQueue() {
333 BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
334 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q);
335 FutureTask[] tasks = new FutureTask[5];
336 for(int i = 0; i < 5; i++){
337 tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
338 p1.execute(tasks[i]);
339 }
340 try {
341 Thread.sleep(SHORT_DELAY_MS);
342 BlockingQueue<Runnable> wq = p1.getQueue();
343 assertSame(q, wq);
344 assertFalse(wq.contains(tasks[0]));
345 assertTrue(wq.contains(tasks[4]));
346 p1.shutdownNow();
347 } catch(Exception e) {
348 unexpectedException();
349 } finally {
350 joinPool(p1);
351 }
352 }
353
354 /**
355 * remove(task) removes queued task, and fails to remove active task
356 */
357 public void testRemove() {
358 BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
359 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q);
360 FutureTask[] tasks = new FutureTask[5];
361 for(int i = 0; i < 5; i++){
362 tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
363 p1.execute(tasks[i]);
364 }
365 try {
366 Thread.sleep(SHORT_DELAY_MS);
367 assertFalse(p1.remove(tasks[0]));
368 assertTrue(q.contains(tasks[4]));
369 assertTrue(q.contains(tasks[3]));
370 assertTrue(p1.remove(tasks[4]));
371 assertFalse(p1.remove(tasks[4]));
372 assertFalse(q.contains(tasks[4]));
373 assertTrue(q.contains(tasks[3]));
374 assertTrue(p1.remove(tasks[3]));
375 assertFalse(q.contains(tasks[3]));
376 p1.shutdownNow();
377 } catch(Exception e) {
378 unexpectedException();
379 } finally {
380 joinPool(p1);
381 }
382 }
383
384 /**
385 * purge removes cancelled tasks from the queue
386 */
387 public void testPurge() {
388 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
389 FutureTask[] tasks = new FutureTask[5];
390 for(int i = 0; i < 5; i++){
391 tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
392 p1.execute(tasks[i]);
393 }
394 tasks[4].cancel(true);
395 tasks[3].cancel(true);
396 p1.purge();
397 long count = p1.getTaskCount();
398 assertTrue(count >= 2 && count < 5);
399 p1.shutdownNow();
400 joinPool(p1);
401 }
402
403 /**
404 * shutDownNow returns a list containing tasks that were not run
405 */
406 public void testShutDownNow() {
407 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
408 List l;
409 try {
410 for(int i = 0; i < 5; i++)
411 p1.execute(new MediumPossiblyInterruptedRunnable());
412 }
413 finally {
414 l = p1.shutdownNow();
415 }
416 assertTrue(p1.isShutdown());
417 assertTrue(l.size() <= 4);
418 }
419
420 // Exception Tests
421
422
423 /**
424 * Constructor throws if corePoolSize argument is less than zero
425 */
426 public void testConstructor1() {
427 try {
428 new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
429 shouldThrow();
430 }
431 catch (IllegalArgumentException success){}
432 }
433
434 /**
435 * Constructor throws if maximumPoolSize is less than zero
436 */
437 public void testConstructor2() {
438 try {
439 new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
440 shouldThrow();
441 }
442 catch (IllegalArgumentException success){}
443 }
444
445 /**
446 * Constructor throws if maximumPoolSize is equal to zero
447 */
448 public void testConstructor3() {
449 try {
450 new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
451 shouldThrow();
452 }
453 catch (IllegalArgumentException success){}
454 }
455
456 /**
457 * Constructor throws if keepAliveTime is less than zero
458 */
459 public void testConstructor4() {
460 try {
461 new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
462 shouldThrow();
463 }
464 catch (IllegalArgumentException success){}
465 }
466
467 /**
468 * Constructor throws if corePoolSize is greater than the maximumPoolSize
469 */
470 public void testConstructor5() {
471 try {
472 new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
473 shouldThrow();
474 }
475 catch (IllegalArgumentException success){}
476 }
477
478 /**
479 * Constructor throws if workQueue is set to null
480 */
481 public void testConstructorNullPointerException() {
482 try {
483 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null);
484 shouldThrow();
485 }
486 catch (NullPointerException success){}
487 }
488
489
490
491 /**
492 * Constructor throws if corePoolSize argument is less than zero
493 */
494 public void testConstructor6() {
495 try {
496 new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
497 shouldThrow();
498 } catch (IllegalArgumentException success){}
499 }
500
501 /**
502 * Constructor throws if maximumPoolSize is less than zero
503 */
504 public void testConstructor7() {
505 try {
506 new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
507 shouldThrow();
508 }
509 catch (IllegalArgumentException success){}
510 }
511
512 /**
513 * Constructor throws if maximumPoolSize is equal to zero
514 */
515 public void testConstructor8() {
516 try {
517 new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
518 shouldThrow();
519 }
520 catch (IllegalArgumentException success){}
521 }
522
523 /**
524 * Constructor throws if keepAliveTime is less than zero
525 */
526 public void testConstructor9() {
527 try {
528 new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
529 shouldThrow();
530 }
531 catch (IllegalArgumentException success){}
532 }
533
534 /**
535 * Constructor throws if corePoolSize is greater than the maximumPoolSize
536 */
537 public void testConstructor10() {
538 try {
539 new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
540 shouldThrow();
541 }
542 catch (IllegalArgumentException success){}
543 }
544
545 /**
546 * Constructor throws if workQueue is set to null
547 */
548 public void testConstructorNullPointerException2() {
549 try {
550 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory());
551 shouldThrow();
552 }
553 catch (NullPointerException success){}
554 }
555
556 /**
557 * Constructor throws if threadFactory is set to null
558 */
559 public void testConstructorNullPointerException3() {
560 try {
561 ThreadFactory f = null;
562 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
563 shouldThrow();
564 }
565 catch (NullPointerException success){}
566 }
567
568
569 /**
570 * Constructor throws if corePoolSize argument is less than zero
571 */
572 public void testConstructor11() {
573 try {
574 new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
575 shouldThrow();
576 }
577 catch (IllegalArgumentException success){}
578 }
579
580 /**
581 * Constructor throws if maximumPoolSize is less than zero
582 */
583 public void testConstructor12() {
584 try {
585 new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
586 shouldThrow();
587 }
588 catch (IllegalArgumentException success){}
589 }
590
591 /**
592 * Constructor throws if maximumPoolSize is equal to zero
593 */
594 public void testConstructor13() {
595 try {
596 new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
597 shouldThrow();
598 }
599 catch (IllegalArgumentException success){}
600 }
601
602 /**
603 * Constructor throws if keepAliveTime is less than zero
604 */
605 public void testConstructor14() {
606 try {
607 new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
608 shouldThrow();
609 }
610 catch (IllegalArgumentException success){}
611 }
612
613 /**
614 * Constructor throws if corePoolSize is greater than the maximumPoolSize
615 */
616 public void testConstructor15() {
617 try {
618 new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
619 shouldThrow();
620 }
621 catch (IllegalArgumentException success){}
622 }
623
624 /**
625 * Constructor throws if workQueue is set to null
626 */
627 public void testConstructorNullPointerException4() {
628 try {
629 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler());
630 shouldThrow();
631 }
632 catch (NullPointerException success){}
633 }
634
635 /**
636 * Constructor throws if handler is set to null
637 */
638 public void testConstructorNullPointerException5() {
639 try {
640 RejectedExecutionHandler r = null;
641 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
642 shouldThrow();
643 }
644 catch (NullPointerException success){}
645 }
646
647
648 /**
649 * Constructor throws if corePoolSize argument is less than zero
650 */
651 public void testConstructor16() {
652 try {
653 new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
654 shouldThrow();
655 }
656 catch (IllegalArgumentException success){}
657 }
658
659 /**
660 * Constructor throws if maximumPoolSize is less than zero
661 */
662 public void testConstructor17() {
663 try {
664 new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
665 shouldThrow();
666 }
667 catch (IllegalArgumentException success){}
668 }
669
670 /**
671 * Constructor throws if maximumPoolSize is equal to zero
672 */
673 public void testConstructor18() {
674 try {
675 new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
676 shouldThrow();
677 }
678 catch (IllegalArgumentException success){}
679 }
680
681 /**
682 * Constructor throws if keepAliveTime is less than zero
683 */
684 public void testConstructor19() {
685 try {
686 new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
687 shouldThrow();
688 }
689 catch (IllegalArgumentException success){}
690 }
691
692 /**
693 * Constructor throws if corePoolSize is greater than the maximumPoolSize
694 */
695 public void testConstructor20() {
696 try {
697 new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
698 shouldThrow();
699 }
700 catch (IllegalArgumentException success){}
701 }
702
703 /**
704 * Constructor throws if workQueue is set to null
705 */
706 public void testConstructorNullPointerException6() {
707 try {
708 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
709 shouldThrow();
710 }
711 catch (NullPointerException success){}
712 }
713
714 /**
715 * Constructor throws if handler is set to null
716 */
717 public void testConstructorNullPointerException7() {
718 try {
719 RejectedExecutionHandler r = null;
720 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
721 shouldThrow();
722 }
723 catch (NullPointerException success){}
724 }
725
726 /**
727 * Constructor throws if ThreadFactory is set top null
728 */
729 public void testConstructorNullPointerException8() {
730 try {
731 ThreadFactory f = null;
732 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
733 shouldThrow();
734 }
735 catch (NullPointerException successdn8){}
736 }
737
738
739 /**
740 * execute throws RejectedExecutionException
741 * if saturated.
742 */
743 public void testSaturatedExecute() {
744 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
745 try {
746
747 for(int i = 0; i < 5; ++i){
748 p.execute(new MediumRunnable());
749 }
750 shouldThrow();
751 } catch(RejectedExecutionException success){}
752 joinPool(p);
753 }
754
755 /**
756 * executor using CallerRunsPolicy runs task if saturated.
757 */
758 public void testSaturatedExecute2() {
759 RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
760 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
761 try {
762
763 TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
764 for(int i = 0; i < 5; ++i){
765 tasks[i] = new TrackedNoOpRunnable();
766 }
767 TrackedLongRunnable mr = new TrackedLongRunnable();
768 p.execute(mr);
769 for(int i = 0; i < 5; ++i){
770 p.execute(tasks[i]);
771 }
772 for(int i = 1; i < 5; ++i) {
773 assertTrue(tasks[i].done);
774 }
775 p.shutdownNow();
776 } catch(RejectedExecutionException ex){
777 unexpectedException();
778 } finally {
779 joinPool(p);
780 }
781 }
782
783 /**
784 * executor using DiscardPolicy drops task if saturated.
785 */
786 public void testSaturatedExecute3() {
787 RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
788 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
789 try {
790
791 TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
792 for(int i = 0; i < 5; ++i){
793 tasks[i] = new TrackedNoOpRunnable();
794 }
795 p.execute(new TrackedLongRunnable());
796 for(int i = 0; i < 5; ++i){
797 p.execute(tasks[i]);
798 }
799 for(int i = 0; i < 5; ++i){
800 assertFalse(tasks[i].done);
801 }
802 p.shutdownNow();
803 } catch(RejectedExecutionException ex){
804 unexpectedException();
805 } finally {
806 joinPool(p);
807 }
808 }
809
810 /**
811 * executor using DiscardOldestPolicy drops oldest task if saturated.
812 */
813 public void testSaturatedExecute4() {
814 RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
815 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
816 try {
817 p.execute(new TrackedLongRunnable());
818 TrackedLongRunnable r2 = new TrackedLongRunnable();
819 p.execute(r2);
820 assertTrue(p.getQueue().contains(r2));
821 TrackedNoOpRunnable r3 = new TrackedNoOpRunnable();
822 p.execute(r3);
823 assertFalse(p.getQueue().contains(r2));
824 assertTrue(p.getQueue().contains(r3));
825 p.shutdownNow();
826 } catch(RejectedExecutionException ex){
827 unexpectedException();
828 } finally {
829 joinPool(p);
830 }
831 }
832
833 /**
834 * execute throws RejectedExecutionException if shutdown
835 */
836 public void testRejectedExecutionExceptionOnShutdown() {
837 ThreadPoolExecutor tpe =
838 new ThreadPoolExecutor(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
839 tpe.shutdown();
840 try {
841 tpe.execute(new NoOpRunnable());
842 shouldThrow();
843 } catch(RejectedExecutionException success){}
844
845 joinPool(tpe);
846 }
847
848 /**
849 * execute using CallerRunsPolicy drops task on shutdown
850 */
851 public void testCallerRunsOnShutdown() {
852 RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
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 * execute using DiscardPolicy drops task on shutdown
869 */
870 public void testDiscardOnShutdown() {
871 RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
872 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
873
874 p.shutdown();
875 try {
876 TrackedNoOpRunnable r = new TrackedNoOpRunnable();
877 p.execute(r);
878 assertFalse(r.done);
879 } catch(RejectedExecutionException success){
880 unexpectedException();
881 } finally {
882 joinPool(p);
883 }
884 }
885
886
887 /**
888 * execute using DiscardOldestPolicy drops task on shutdown
889 */
890 public void testDiscardOldestOnShutdown() {
891 RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
892 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
893
894 p.shutdown();
895 try {
896 TrackedNoOpRunnable r = new TrackedNoOpRunnable();
897 p.execute(r);
898 assertFalse(r.done);
899 } catch(RejectedExecutionException success){
900 unexpectedException();
901 } finally {
902 joinPool(p);
903 }
904 }
905
906
907 /**
908 * execute (null) throws NPE
909 */
910 public void testExecuteNull() {
911 ThreadPoolExecutor tpe = null;
912 try {
913 tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
914 tpe.execute(null);
915 shouldThrow();
916 } catch(NullPointerException success){}
917
918 joinPool(tpe);
919 }
920
921 /**
922 * setCorePoolSize of negative value throws IllegalArgumentException
923 */
924 public void testCorePoolSizeIllegalArgumentException() {
925 ThreadPoolExecutor tpe = null;
926 try {
927 tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
928 } catch(Exception e){}
929 try {
930 tpe.setCorePoolSize(-1);
931 shouldThrow();
932 } catch(IllegalArgumentException success){
933 } finally {
934 tpe.shutdown();
935 }
936 joinPool(tpe);
937 }
938
939 /**
940 * setMaximumPoolSize(int) throws IllegalArgumentException if
941 * given a value less the core pool size
942 */
943 public void testMaximumPoolSizeIllegalArgumentException() {
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 try {
949 tpe.setMaximumPoolSize(1);
950 shouldThrow();
951 } catch(IllegalArgumentException success){
952 } finally {
953 tpe.shutdown();
954 }
955 joinPool(tpe);
956 }
957
958 /**
959 * setMaximumPoolSize throws IllegalArgumentException
960 * if given a negative value
961 */
962 public void testMaximumPoolSizeIllegalArgumentException2() {
963 ThreadPoolExecutor tpe = null;
964 try {
965 tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
966 } catch(Exception e){}
967 try {
968 tpe.setMaximumPoolSize(-1);
969 shouldThrow();
970 } catch(IllegalArgumentException success){
971 } finally {
972 tpe.shutdown();
973 }
974 joinPool(tpe);
975 }
976
977
978 /**
979 * setKeepAliveTime throws IllegalArgumentException
980 * when given a negative value
981 */
982 public void testKeepAliveTimeIllegalArgumentException() {
983 ThreadPoolExecutor tpe = null;
984 try {
985 tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
986 } catch(Exception e){}
987
988 try {
989 tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS);
990 shouldThrow();
991 } catch(IllegalArgumentException success){
992 } finally {
993 tpe.shutdown();
994 }
995 joinPool(tpe);
996 }
997
998 /**
999 * terminated() is called on termination
1000 */
1001 public void testTerminated() {
1002 ExtendedTPE tpe = new ExtendedTPE();
1003 tpe.shutdown();
1004 assertTrue(tpe.terminatedCalled);
1005 joinPool(tpe);
1006 }
1007
1008 /**
1009 * beforeExecute and afterExecute are called when executing task
1010 */
1011 public void testBeforeAfter() {
1012 ExtendedTPE tpe = new ExtendedTPE();
1013 try {
1014 TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1015 tpe.execute(r);
1016 Thread.sleep(SHORT_DELAY_MS);
1017 assertTrue(r.done);
1018 assertTrue(tpe.beforeCalled);
1019 assertTrue(tpe.afterCalled);
1020 tpe.shutdown();
1021 }
1022 catch(Exception ex) {
1023 unexpectedException();
1024 } finally {
1025 joinPool(tpe);
1026 }
1027 }
1028
1029 /**
1030 * completed submit of callable returns result
1031 */
1032 public void testSubmitCallable() {
1033 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1034 try {
1035 Future<String> future = e.submit(new StringTask());
1036 String result = future.get();
1037 assertSame(TEST_STRING, result);
1038 }
1039 catch (ExecutionException ex) {
1040 unexpectedException();
1041 }
1042 catch (InterruptedException ex) {
1043 unexpectedException();
1044 } finally {
1045 joinPool(e);
1046 }
1047 }
1048
1049 /**
1050 * completed submit of runnable returns successfully
1051 */
1052 public void testSubmitRunnable() {
1053 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1054 try {
1055 Future<?> future = e.submit(new NoOpRunnable());
1056 future.get();
1057 assertTrue(future.isDone());
1058 }
1059 catch (ExecutionException ex) {
1060 unexpectedException();
1061 }
1062 catch (InterruptedException ex) {
1063 unexpectedException();
1064 } finally {
1065 joinPool(e);
1066 }
1067 }
1068
1069 /**
1070 * completed submit of (runnable, result) returns result
1071 */
1072 public void testSubmitRunnable2() {
1073 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1074 try {
1075 Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1076 String result = future.get();
1077 assertSame(TEST_STRING, result);
1078 }
1079 catch (ExecutionException ex) {
1080 unexpectedException();
1081 }
1082 catch (InterruptedException ex) {
1083 unexpectedException();
1084 } finally {
1085 joinPool(e);
1086 }
1087 }
1088
1089
1090
1091
1092
1093 /**
1094 * invokeAny(null) throws NPE
1095 */
1096 public void testInvokeAny1() {
1097 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1098 try {
1099 e.invokeAny(null);
1100 } catch (NullPointerException success) {
1101 } catch(Exception ex) {
1102 unexpectedException();
1103 } finally {
1104 joinPool(e);
1105 }
1106 }
1107
1108 /**
1109 * invokeAny(empty collection) throws IAE
1110 */
1111 public void testInvokeAny2() {
1112 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1113 try {
1114 e.invokeAny(new ArrayList<Callable<String>>());
1115 } catch (IllegalArgumentException success) {
1116 } catch(Exception ex) {
1117 unexpectedException();
1118 } finally {
1119 joinPool(e);
1120 }
1121 }
1122
1123 /**
1124 * invokeAny(c) throws NPE if c has null elements
1125 */
1126 public void testInvokeAny3() {
1127 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1128 try {
1129 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1130 l.add(new StringTask());
1131 l.add(null);
1132 e.invokeAny(l);
1133 } catch (NullPointerException success) {
1134 } catch(Exception ex) {
1135 unexpectedException();
1136 } finally {
1137 joinPool(e);
1138 }
1139 }
1140
1141 /**
1142 * invokeAny(c) throws ExecutionException if no task completes
1143 */
1144 public void testInvokeAny4() {
1145 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1146 try {
1147 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1148 l.add(new NPETask());
1149 e.invokeAny(l);
1150 } catch (ExecutionException success) {
1151 } catch(Exception ex) {
1152 unexpectedException();
1153 } finally {
1154 joinPool(e);
1155 }
1156 }
1157
1158 /**
1159 * invokeAny(c) returns result of some task
1160 */
1161 public void testInvokeAny5() {
1162 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1163 try {
1164 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1165 l.add(new StringTask());
1166 l.add(new StringTask());
1167 String result = e.invokeAny(l);
1168 assertSame(TEST_STRING, result);
1169 } catch (ExecutionException success) {
1170 } catch(Exception ex) {
1171 unexpectedException();
1172 } finally {
1173 joinPool(e);
1174 }
1175 }
1176
1177 /**
1178 * invokeAll(null) throws NPE
1179 */
1180 public void testInvokeAll1() {
1181 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1182 try {
1183 e.invokeAll(null);
1184 } catch (NullPointerException success) {
1185 } catch(Exception ex) {
1186 unexpectedException();
1187 } finally {
1188 joinPool(e);
1189 }
1190 }
1191
1192 /**
1193 * invokeAll(empty collection) returns empty collection
1194 */
1195 public void testInvokeAll2() {
1196 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1197 try {
1198 List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1199 assertTrue(r.isEmpty());
1200 } catch(Exception ex) {
1201 unexpectedException();
1202 } finally {
1203 joinPool(e);
1204 }
1205 }
1206
1207 /**
1208 * invokeAll(c) throws NPE if c has null elements
1209 */
1210 public void testInvokeAll3() {
1211 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1212 try {
1213 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1214 l.add(new StringTask());
1215 l.add(null);
1216 e.invokeAll(l);
1217 } catch (NullPointerException success) {
1218 } catch(Exception ex) {
1219 unexpectedException();
1220 } finally {
1221 joinPool(e);
1222 }
1223 }
1224
1225 /**
1226 * get of element of invokeAll(c) throws exception on failed task
1227 */
1228 public void testInvokeAll4() {
1229 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1230 try {
1231 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1232 l.add(new NPETask());
1233 List<Future<String>> result = e.invokeAll(l);
1234 assertEquals(1, result.size());
1235 for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1236 it.next().get();
1237 } catch(ExecutionException success) {
1238 } catch(Exception ex) {
1239 unexpectedException();
1240 } finally {
1241 joinPool(e);
1242 }
1243 }
1244
1245 /**
1246 * invokeAll(c) returns results of all completed tasks
1247 */
1248 public void testInvokeAll5() {
1249 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1250 try {
1251 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1252 l.add(new StringTask());
1253 l.add(new StringTask());
1254 List<Future<String>> result = e.invokeAll(l);
1255 assertEquals(2, result.size());
1256 for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1257 assertSame(TEST_STRING, it.next().get());
1258 } catch (ExecutionException success) {
1259 } catch(Exception ex) {
1260 unexpectedException();
1261 } finally {
1262 joinPool(e);
1263 }
1264 }
1265
1266
1267 }