ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.27
Committed: Wed Nov 18 05:39:51 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.26: +233 -328 lines
Log Message:
improve exception handling

File Contents

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/licenses/publicdomain
5 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 * Pat Fisher, Mike Judd.
7 */
8
9 import java.util.concurrent.*;
10 import static java.util.concurrent.TimeUnit.MILLISECONDS;
11 import java.util.concurrent.atomic.*;
12 import junit.framework.*;
13 import java.util.*;
14
15 public class ThreadPoolExecutorTest extends JSR166TestCase {
16 public static void main(String[] args) {
17 junit.textui.TestRunner.run (suite());
18 }
19 public static Test suite() {
20 return new TestSuite(ThreadPoolExecutorTest.class);
21 }
22
23 static class ExtendedTPE extends ThreadPoolExecutor {
24 volatile boolean beforeCalled = false;
25 volatile boolean afterCalled = false;
26 volatile boolean terminatedCalled = false;
27 public ExtendedTPE() {
28 super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
29 }
30 protected void beforeExecute(Thread t, Runnable r) {
31 beforeCalled = true;
32 }
33 protected void afterExecute(Runnable r, Throwable t) {
34 afterCalled = true;
35 }
36 protected void terminated() {
37 terminatedCalled = true;
38 }
39 }
40
41 static class FailingThreadFactory implements ThreadFactory {
42 int calls = 0;
43 public Thread newThread(Runnable r) {
44 if (++calls > 1) return null;
45 return new Thread(r);
46 }
47 }
48
49
50 /**
51 * execute successfully executes a runnable
52 */
53 public void testExecute() throws InterruptedException {
54 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
55 try {
56 p1.execute(new ShortRunnable());
57 Thread.sleep(SMALL_DELAY_MS);
58 } finally {
59 joinPool(p1);
60 }
61 }
62
63 /**
64 * getActiveCount increases but doesn't overestimate, when a
65 * thread becomes active
66 */
67 public void testGetActiveCount() throws InterruptedException {
68 ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
69 assertEquals(0, p2.getActiveCount());
70 p2.execute(new MediumRunnable());
71 Thread.sleep(SHORT_DELAY_MS);
72 assertEquals(1, p2.getActiveCount());
73 joinPool(p2);
74 }
75
76 /**
77 * prestartCoreThread starts a thread if under corePoolSize, else doesn't
78 */
79 public void testPrestartCoreThread() {
80 ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
81 assertEquals(0, p2.getPoolSize());
82 assertTrue(p2.prestartCoreThread());
83 assertEquals(1, p2.getPoolSize());
84 assertTrue(p2.prestartCoreThread());
85 assertEquals(2, p2.getPoolSize());
86 assertFalse(p2.prestartCoreThread());
87 assertEquals(2, p2.getPoolSize());
88 joinPool(p2);
89 }
90
91 /**
92 * prestartAllCoreThreads starts all corePoolSize threads
93 */
94 public void testPrestartAllCoreThreads() {
95 ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
96 assertEquals(0, p2.getPoolSize());
97 p2.prestartAllCoreThreads();
98 assertEquals(2, p2.getPoolSize());
99 p2.prestartAllCoreThreads();
100 assertEquals(2, p2.getPoolSize());
101 joinPool(p2);
102 }
103
104 /**
105 * getCompletedTaskCount increases, but doesn't overestimate,
106 * when tasks complete
107 */
108 public void testGetCompletedTaskCount() throws InterruptedException {
109 ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
110 assertEquals(0, p2.getCompletedTaskCount());
111 p2.execute(new ShortRunnable());
112 Thread.sleep(SMALL_DELAY_MS);
113 assertEquals(1, p2.getCompletedTaskCount());
114 try { p2.shutdown(); } catch (SecurityException ok) { return; }
115 joinPool(p2);
116 }
117
118 /**
119 * getCorePoolSize returns size given in constructor if not otherwise set
120 */
121 public void testGetCorePoolSize() {
122 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
123 assertEquals(1, p1.getCorePoolSize());
124 joinPool(p1);
125 }
126
127 /**
128 * getKeepAliveTime returns value given in constructor if not otherwise set
129 */
130 public void testGetKeepAliveTime() {
131 ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
132 assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS));
133 joinPool(p2);
134 }
135
136
137 /**
138 * getThreadFactory returns factory in constructor if not set
139 */
140 public void testGetThreadFactory() {
141 ThreadFactory tf = new SimpleThreadFactory();
142 ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
143 assertSame(tf, p.getThreadFactory());
144 joinPool(p);
145 }
146
147 /**
148 * setThreadFactory sets the thread factory returned by getThreadFactory
149 */
150 public void testSetThreadFactory() {
151 ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
152 ThreadFactory tf = new SimpleThreadFactory();
153 p.setThreadFactory(tf);
154 assertSame(tf, p.getThreadFactory());
155 joinPool(p);
156 }
157
158
159 /**
160 * setThreadFactory(null) throws NPE
161 */
162 public void testSetThreadFactoryNull() {
163 ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
164 try {
165 p.setThreadFactory(null);
166 shouldThrow();
167 } catch (NullPointerException success) {
168 } finally {
169 joinPool(p);
170 }
171 }
172
173 /**
174 * getRejectedExecutionHandler returns handler in constructor if not set
175 */
176 public void testGetRejectedExecutionHandler() {
177 RejectedExecutionHandler h = new NoOpREHandler();
178 ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
179 assertSame(h, p.getRejectedExecutionHandler());
180 joinPool(p);
181 }
182
183 /**
184 * setRejectedExecutionHandler sets the handler returned by
185 * getRejectedExecutionHandler
186 */
187 public void testSetRejectedExecutionHandler() {
188 ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
189 RejectedExecutionHandler h = new NoOpREHandler();
190 p.setRejectedExecutionHandler(h);
191 assertSame(h, p.getRejectedExecutionHandler());
192 joinPool(p);
193 }
194
195
196 /**
197 * setRejectedExecutionHandler(null) throws NPE
198 */
199 public void testSetRejectedExecutionHandlerNull() {
200 ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
201 try {
202 p.setRejectedExecutionHandler(null);
203 shouldThrow();
204 } catch (NullPointerException success) {
205 } finally {
206 joinPool(p);
207 }
208 }
209
210
211 /**
212 * getLargestPoolSize increases, but doesn't overestimate, when
213 * multiple threads active
214 */
215 public void testGetLargestPoolSize() throws InterruptedException {
216 ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
217 assertEquals(0, p2.getLargestPoolSize());
218 p2.execute(new MediumRunnable());
219 p2.execute(new MediumRunnable());
220 Thread.sleep(SHORT_DELAY_MS);
221 assertEquals(2, p2.getLargestPoolSize());
222 joinPool(p2);
223 }
224
225 /**
226 * getMaximumPoolSize returns value given in constructor if not
227 * otherwise set
228 */
229 public void testGetMaximumPoolSize() {
230 ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
231 assertEquals(2, p2.getMaximumPoolSize());
232 joinPool(p2);
233 }
234
235 /**
236 * getPoolSize increases, but doesn't overestimate, when threads
237 * become active
238 */
239 public void testGetPoolSize() {
240 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
241 assertEquals(0, p1.getPoolSize());
242 p1.execute(new MediumRunnable());
243 assertEquals(1, p1.getPoolSize());
244 joinPool(p1);
245 }
246
247 /**
248 * getTaskCount increases, but doesn't overestimate, when tasks submitted
249 */
250 public void testGetTaskCount() throws InterruptedException {
251 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
252 assertEquals(0, p1.getTaskCount());
253 p1.execute(new MediumRunnable());
254 Thread.sleep(SHORT_DELAY_MS);
255 assertEquals(1, p1.getTaskCount());
256 joinPool(p1);
257 }
258
259 /**
260 * isShutDown is false before shutdown, true after
261 */
262 public void testIsShutdown() {
263
264 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
265 assertFalse(p1.isShutdown());
266 try { p1.shutdown(); } catch (SecurityException ok) { return; }
267 assertTrue(p1.isShutdown());
268 joinPool(p1);
269 }
270
271
272 /**
273 * isTerminated is false before termination, true after
274 */
275 public void testIsTerminated() throws InterruptedException {
276 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
277 assertFalse(p1.isTerminated());
278 try {
279 p1.execute(new MediumRunnable());
280 } finally {
281 try { p1.shutdown(); } catch (SecurityException ok) { return; }
282 }
283 assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
284 assertTrue(p1.isTerminated());
285 }
286
287 /**
288 * isTerminating is not true when running or when terminated
289 */
290 public void testIsTerminating() throws InterruptedException {
291 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
292 assertFalse(p1.isTerminating());
293 try {
294 p1.execute(new SmallRunnable());
295 assertFalse(p1.isTerminating());
296 } finally {
297 try { p1.shutdown(); } catch (SecurityException ok) { return; }
298 }
299 assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
300 assertTrue(p1.isTerminated());
301 assertFalse(p1.isTerminating());
302 }
303
304 /**
305 * getQueue returns the work queue, which contains queued tasks
306 */
307 public void testGetQueue() throws InterruptedException {
308 BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
309 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
310 FutureTask[] tasks = new FutureTask[5];
311 for (int i = 0; i < 5; i++) {
312 tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
313 p1.execute(tasks[i]);
314 }
315 try {
316 Thread.sleep(SHORT_DELAY_MS);
317 BlockingQueue<Runnable> wq = p1.getQueue();
318 assertSame(q, wq);
319 assertFalse(wq.contains(tasks[0]));
320 assertTrue(wq.contains(tasks[4]));
321 for (int i = 1; i < 5; ++i)
322 tasks[i].cancel(true);
323 p1.shutdownNow();
324 } finally {
325 joinPool(p1);
326 }
327 }
328
329 /**
330 * remove(task) removes queued task, and fails to remove active task
331 */
332 public void testRemove() throws InterruptedException {
333 BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
334 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, 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 assertFalse(p1.remove(tasks[0]));
343 assertTrue(q.contains(tasks[4]));
344 assertTrue(q.contains(tasks[3]));
345 assertTrue(p1.remove(tasks[4]));
346 assertFalse(p1.remove(tasks[4]));
347 assertFalse(q.contains(tasks[4]));
348 assertTrue(q.contains(tasks[3]));
349 assertTrue(p1.remove(tasks[3]));
350 assertFalse(q.contains(tasks[3]));
351 } finally {
352 joinPool(p1);
353 }
354 }
355
356 /**
357 * purge removes cancelled tasks from the queue
358 */
359 public void testPurge() {
360 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
361 FutureTask[] tasks = new FutureTask[5];
362 for (int i = 0; i < 5; i++) {
363 tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
364 p1.execute(tasks[i]);
365 }
366 tasks[4].cancel(true);
367 tasks[3].cancel(true);
368 p1.purge();
369 long count = p1.getTaskCount();
370 assertTrue(count >= 2 && count < 5);
371 joinPool(p1);
372 }
373
374 /**
375 * shutDownNow returns a list containing tasks that were not run
376 */
377 public void testShutDownNow() {
378 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
379 List l;
380 try {
381 for (int i = 0; i < 5; i++)
382 p1.execute(new MediumPossiblyInterruptedRunnable());
383 }
384 finally {
385 try {
386 l = p1.shutdownNow();
387 } catch (SecurityException ok) { return; }
388
389 }
390 assertTrue(p1.isShutdown());
391 assertTrue(l.size() <= 4);
392 }
393
394 // Exception Tests
395
396
397 /**
398 * Constructor throws if corePoolSize argument is less than zero
399 */
400 public void testConstructor1() {
401 try {
402 new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
403 shouldThrow();
404 }
405 catch (IllegalArgumentException success) {}
406 }
407
408 /**
409 * Constructor throws if maximumPoolSize is less than zero
410 */
411 public void testConstructor2() {
412 try {
413 new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
414 shouldThrow();
415 }
416 catch (IllegalArgumentException success) {}
417 }
418
419 /**
420 * Constructor throws if maximumPoolSize is equal to zero
421 */
422 public void testConstructor3() {
423 try {
424 new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
425 shouldThrow();
426 }
427 catch (IllegalArgumentException success) {}
428 }
429
430 /**
431 * Constructor throws if keepAliveTime is less than zero
432 */
433 public void testConstructor4() {
434 try {
435 new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
436 shouldThrow();
437 }
438 catch (IllegalArgumentException success) {}
439 }
440
441 /**
442 * Constructor throws if corePoolSize is greater than the maximumPoolSize
443 */
444 public void testConstructor5() {
445 try {
446 new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
447 shouldThrow();
448 }
449 catch (IllegalArgumentException success) {}
450 }
451
452 /**
453 * Constructor throws if workQueue is set to null
454 */
455 public void testConstructorNullPointerException() {
456 try {
457 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null);
458 shouldThrow();
459 }
460 catch (NullPointerException success) {}
461 }
462
463
464
465 /**
466 * Constructor throws if corePoolSize argument is less than zero
467 */
468 public void testConstructor6() {
469 try {
470 new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
471 shouldThrow();
472 }
473 catch (IllegalArgumentException success) {}
474 }
475
476 /**
477 * Constructor throws if maximumPoolSize is less than zero
478 */
479 public void testConstructor7() {
480 try {
481 new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
482 shouldThrow();
483 }
484 catch (IllegalArgumentException success) {}
485 }
486
487 /**
488 * Constructor throws if maximumPoolSize is equal to zero
489 */
490 public void testConstructor8() {
491 try {
492 new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
493 shouldThrow();
494 }
495 catch (IllegalArgumentException success) {}
496 }
497
498 /**
499 * Constructor throws if keepAliveTime is less than zero
500 */
501 public void testConstructor9() {
502 try {
503 new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
504 shouldThrow();
505 }
506 catch (IllegalArgumentException success) {}
507 }
508
509 /**
510 * Constructor throws if corePoolSize is greater than the maximumPoolSize
511 */
512 public void testConstructor10() {
513 try {
514 new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
515 shouldThrow();
516 }
517 catch (IllegalArgumentException success) {}
518 }
519
520 /**
521 * Constructor throws if workQueue is set to null
522 */
523 public void testConstructorNullPointerException2() {
524 try {
525 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
526 shouldThrow();
527 }
528 catch (NullPointerException success) {}
529 }
530
531 /**
532 * Constructor throws if threadFactory is set to null
533 */
534 public void testConstructorNullPointerException3() {
535 try {
536 ThreadFactory f = null;
537 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
538 shouldThrow();
539 }
540 catch (NullPointerException success) {}
541 }
542
543
544 /**
545 * Constructor throws if corePoolSize argument is less than zero
546 */
547 public void testConstructor11() {
548 try {
549 new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
550 shouldThrow();
551 }
552 catch (IllegalArgumentException success) {}
553 }
554
555 /**
556 * Constructor throws if maximumPoolSize is less than zero
557 */
558 public void testConstructor12() {
559 try {
560 new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
561 shouldThrow();
562 }
563 catch (IllegalArgumentException success) {}
564 }
565
566 /**
567 * Constructor throws if maximumPoolSize is equal to zero
568 */
569 public void testConstructor13() {
570 try {
571 new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
572 shouldThrow();
573 }
574 catch (IllegalArgumentException success) {}
575 }
576
577 /**
578 * Constructor throws if keepAliveTime is less than zero
579 */
580 public void testConstructor14() {
581 try {
582 new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
583 shouldThrow();
584 }
585 catch (IllegalArgumentException success) {}
586 }
587
588 /**
589 * Constructor throws if corePoolSize is greater than the maximumPoolSize
590 */
591 public void testConstructor15() {
592 try {
593 new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
594 shouldThrow();
595 }
596 catch (IllegalArgumentException success) {}
597 }
598
599 /**
600 * Constructor throws if workQueue is set to null
601 */
602 public void testConstructorNullPointerException4() {
603 try {
604 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
605 shouldThrow();
606 }
607 catch (NullPointerException success) {}
608 }
609
610 /**
611 * Constructor throws if handler is set to null
612 */
613 public void testConstructorNullPointerException5() {
614 try {
615 RejectedExecutionHandler r = null;
616 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
617 shouldThrow();
618 }
619 catch (NullPointerException success) {}
620 }
621
622
623 /**
624 * Constructor throws if corePoolSize argument is less than zero
625 */
626 public void testConstructor16() {
627 try {
628 new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
629 shouldThrow();
630 }
631 catch (IllegalArgumentException success) {}
632 }
633
634 /**
635 * Constructor throws if maximumPoolSize is less than zero
636 */
637 public void testConstructor17() {
638 try {
639 new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
640 shouldThrow();
641 }
642 catch (IllegalArgumentException success) {}
643 }
644
645 /**
646 * Constructor throws if maximumPoolSize is equal to zero
647 */
648 public void testConstructor18() {
649 try {
650 new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
651 shouldThrow();
652 }
653 catch (IllegalArgumentException success) {}
654 }
655
656 /**
657 * Constructor throws if keepAliveTime is less than zero
658 */
659 public void testConstructor19() {
660 try {
661 new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
662 shouldThrow();
663 }
664 catch (IllegalArgumentException success) {}
665 }
666
667 /**
668 * Constructor throws if corePoolSize is greater than the maximumPoolSize
669 */
670 public void testConstructor20() {
671 try {
672 new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
673 shouldThrow();
674 }
675 catch (IllegalArgumentException success) {}
676 }
677
678 /**
679 * Constructor throws if workQueue is set to null
680 */
681 public void testConstructorNullPointerException6() {
682 try {
683 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
684 shouldThrow();
685 }
686 catch (NullPointerException success) {}
687 }
688
689 /**
690 * Constructor throws if handler is set to null
691 */
692 public void testConstructorNullPointerException7() {
693 try {
694 RejectedExecutionHandler r = null;
695 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
696 shouldThrow();
697 }
698 catch (NullPointerException success) {}
699 }
700
701 /**
702 * Constructor throws if ThreadFactory is set top null
703 */
704 public void testConstructorNullPointerException8() {
705 try {
706 ThreadFactory f = null;
707 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
708 shouldThrow();
709 }
710 catch (NullPointerException success) {}
711 }
712
713
714 /**
715 * execute throws RejectedExecutionException
716 * if saturated.
717 */
718 public void testSaturatedExecute() {
719 ThreadPoolExecutor p =
720 new ThreadPoolExecutor(1, 1,
721 LONG_DELAY_MS, MILLISECONDS,
722 new ArrayBlockingQueue<Runnable>(1));
723 try {
724 for (int i = 0; i < 2; ++i)
725 p.execute(new MediumRunnable());
726 for (int i = 0; i < 2; ++i) {
727 try {
728 p.execute(new MediumRunnable());
729 shouldThrow();
730 } catch (RejectedExecutionException success) {}
731 }
732 } finally {
733 joinPool(p);
734 }
735 }
736
737 /**
738 * executor using CallerRunsPolicy runs task if saturated.
739 */
740 public void testSaturatedExecute2() {
741 RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
742 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
743 try {
744
745 TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
746 for (int i = 0; i < 5; ++i) {
747 tasks[i] = new TrackedNoOpRunnable();
748 }
749 TrackedLongRunnable mr = new TrackedLongRunnable();
750 p.execute(mr);
751 for (int i = 0; i < 5; ++i) {
752 p.execute(tasks[i]);
753 }
754 for (int i = 1; i < 5; ++i) {
755 assertTrue(tasks[i].done);
756 }
757 try { p.shutdownNow(); } catch (SecurityException ok) { return; }
758 } finally {
759 joinPool(p);
760 }
761 }
762
763 /**
764 * executor using DiscardPolicy drops task if saturated.
765 */
766 public void testSaturatedExecute3() {
767 RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
768 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
769 try {
770
771 TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
772 for (int i = 0; i < 5; ++i) {
773 tasks[i] = new TrackedNoOpRunnable();
774 }
775 p.execute(new TrackedLongRunnable());
776 for (int i = 0; i < 5; ++i) {
777 p.execute(tasks[i]);
778 }
779 for (int i = 0; i < 5; ++i) {
780 assertFalse(tasks[i].done);
781 }
782 try { p.shutdownNow(); } catch (SecurityException ok) { return; }
783 } finally {
784 joinPool(p);
785 }
786 }
787
788 /**
789 * executor using DiscardOldestPolicy drops oldest task if saturated.
790 */
791 public void testSaturatedExecute4() {
792 RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
793 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
794 try {
795 p.execute(new TrackedLongRunnable());
796 TrackedLongRunnable r2 = new TrackedLongRunnable();
797 p.execute(r2);
798 assertTrue(p.getQueue().contains(r2));
799 TrackedNoOpRunnable r3 = new TrackedNoOpRunnable();
800 p.execute(r3);
801 assertFalse(p.getQueue().contains(r2));
802 assertTrue(p.getQueue().contains(r3));
803 try { p.shutdownNow(); } catch (SecurityException ok) { return; }
804 } finally {
805 joinPool(p);
806 }
807 }
808
809 /**
810 * execute throws RejectedExecutionException if shutdown
811 */
812 public void testRejectedExecutionExceptionOnShutdown() {
813 ThreadPoolExecutor tpe =
814 new ThreadPoolExecutor(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
815 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
816 try {
817 tpe.execute(new NoOpRunnable());
818 shouldThrow();
819 } catch (RejectedExecutionException success) {}
820
821 joinPool(tpe);
822 }
823
824 /**
825 * execute using CallerRunsPolicy drops task on shutdown
826 */
827 public void testCallerRunsOnShutdown() {
828 RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
829 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
830
831 try { p.shutdown(); } catch (SecurityException ok) { return; }
832 try {
833 TrackedNoOpRunnable r = new TrackedNoOpRunnable();
834 p.execute(r);
835 assertFalse(r.done);
836 } finally {
837 joinPool(p);
838 }
839 }
840
841 /**
842 * execute using DiscardPolicy drops task on shutdown
843 */
844 public void testDiscardOnShutdown() {
845 RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
846 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
847
848 try { p.shutdown(); } catch (SecurityException ok) { return; }
849 try {
850 TrackedNoOpRunnable r = new TrackedNoOpRunnable();
851 p.execute(r);
852 assertFalse(r.done);
853 } finally {
854 joinPool(p);
855 }
856 }
857
858
859 /**
860 * execute using DiscardOldestPolicy drops task on shutdown
861 */
862 public void testDiscardOldestOnShutdown() {
863 RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
864 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
865
866 try { p.shutdown(); } catch (SecurityException ok) { return; }
867 try {
868 TrackedNoOpRunnable r = new TrackedNoOpRunnable();
869 p.execute(r);
870 assertFalse(r.done);
871 } finally {
872 joinPool(p);
873 }
874 }
875
876
877 /**
878 * execute (null) throws NPE
879 */
880 public void testExecuteNull() {
881 ThreadPoolExecutor tpe = null;
882 try {
883 tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
884 tpe.execute(null);
885 shouldThrow();
886 } catch (NullPointerException success) {}
887
888 joinPool(tpe);
889 }
890
891 /**
892 * setCorePoolSize of negative value throws IllegalArgumentException
893 */
894 public void testCorePoolSizeIllegalArgumentException() {
895 ThreadPoolExecutor tpe =
896 new ThreadPoolExecutor(1, 2,
897 LONG_DELAY_MS, MILLISECONDS,
898 new ArrayBlockingQueue<Runnable>(10));
899 try {
900 tpe.setCorePoolSize(-1);
901 shouldThrow();
902 } catch (IllegalArgumentException success) {
903 } finally {
904 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
905 }
906 joinPool(tpe);
907 }
908
909 /**
910 * setMaximumPoolSize(int) throws IllegalArgumentException if
911 * given a value less the core pool size
912 */
913 public void testMaximumPoolSizeIllegalArgumentException() {
914 ThreadPoolExecutor tpe =
915 new ThreadPoolExecutor(2, 3,
916 LONG_DELAY_MS, MILLISECONDS,
917 new ArrayBlockingQueue<Runnable>(10));
918 try {
919 tpe.setMaximumPoolSize(1);
920 shouldThrow();
921 } catch (IllegalArgumentException success) {
922 } finally {
923 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
924 }
925 joinPool(tpe);
926 }
927
928 /**
929 * setMaximumPoolSize throws IllegalArgumentException
930 * if given a negative value
931 */
932 public void testMaximumPoolSizeIllegalArgumentException2() {
933 ThreadPoolExecutor tpe =
934 new ThreadPoolExecutor(2, 3,
935 LONG_DELAY_MS, MILLISECONDS,
936 new ArrayBlockingQueue<Runnable>(10));
937 try {
938 tpe.setMaximumPoolSize(-1);
939 shouldThrow();
940 } catch (IllegalArgumentException success) {
941 } finally {
942 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
943 }
944 joinPool(tpe);
945 }
946
947
948 /**
949 * setKeepAliveTime throws IllegalArgumentException
950 * when given a negative value
951 */
952 public void testKeepAliveTimeIllegalArgumentException() {
953 ThreadPoolExecutor tpe =
954 new ThreadPoolExecutor(2, 3,
955 LONG_DELAY_MS, MILLISECONDS,
956 new ArrayBlockingQueue<Runnable>(10));
957 try {
958 tpe.setKeepAliveTime(-1,MILLISECONDS);
959 shouldThrow();
960 } catch (IllegalArgumentException success) {
961 } finally {
962 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
963 }
964 joinPool(tpe);
965 }
966
967 /**
968 * terminated() is called on termination
969 */
970 public void testTerminated() {
971 ExtendedTPE tpe = new ExtendedTPE();
972 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
973 assertTrue(tpe.terminatedCalled);
974 joinPool(tpe);
975 }
976
977 /**
978 * beforeExecute and afterExecute are called when executing task
979 */
980 public void testBeforeAfter() throws InterruptedException {
981 ExtendedTPE tpe = new ExtendedTPE();
982 try {
983 TrackedNoOpRunnable r = new TrackedNoOpRunnable();
984 tpe.execute(r);
985 Thread.sleep(SHORT_DELAY_MS);
986 assertTrue(r.done);
987 assertTrue(tpe.beforeCalled);
988 assertTrue(tpe.afterCalled);
989 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
990 } finally {
991 joinPool(tpe);
992 }
993 }
994
995 /**
996 * completed submit of callable returns result
997 */
998 public void testSubmitCallable() throws Exception {
999 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1000 try {
1001 Future<String> future = e.submit(new StringTask());
1002 String result = future.get();
1003 assertSame(TEST_STRING, result);
1004 } finally {
1005 joinPool(e);
1006 }
1007 }
1008
1009 /**
1010 * completed submit of runnable returns successfully
1011 */
1012 public void testSubmitRunnable() throws Exception {
1013 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1014 try {
1015 Future<?> future = e.submit(new NoOpRunnable());
1016 future.get();
1017 assertTrue(future.isDone());
1018 } finally {
1019 joinPool(e);
1020 }
1021 }
1022
1023 /**
1024 * completed submit of (runnable, result) returns result
1025 */
1026 public void testSubmitRunnable2() throws Exception {
1027 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1028 try {
1029 Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1030 String result = future.get();
1031 assertSame(TEST_STRING, result);
1032 } finally {
1033 joinPool(e);
1034 }
1035 }
1036
1037
1038 /**
1039 * invokeAny(null) throws NPE
1040 */
1041 public void testInvokeAny1() throws Exception {
1042 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1043 try {
1044 e.invokeAny(null);
1045 shouldThrow();
1046 } catch (NullPointerException success) {
1047 } finally {
1048 joinPool(e);
1049 }
1050 }
1051
1052 /**
1053 * invokeAny(empty collection) throws IAE
1054 */
1055 public void testInvokeAny2() throws Exception {
1056 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1057 try {
1058 e.invokeAny(new ArrayList<Callable<String>>());
1059 shouldThrow();
1060 } catch (IllegalArgumentException success) {
1061 } finally {
1062 joinPool(e);
1063 }
1064 }
1065
1066 /**
1067 * invokeAny(c) throws NPE if c has null elements
1068 */
1069 public void testInvokeAny3() throws Exception {
1070 final CountDownLatch latch = new CountDownLatch(1);
1071 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1072 try {
1073 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1074 l.add(new CheckedCallable<String>() {
1075 public String realCall() throws InterruptedException {
1076 latch.await();
1077 return TEST_STRING;
1078 }});
1079 l.add(null);
1080 e.invokeAny(l);
1081 shouldThrow();
1082 } catch (NullPointerException success) {
1083 } finally {
1084 latch.countDown();
1085 joinPool(e);
1086 }
1087 }
1088
1089 /**
1090 * invokeAny(c) throws ExecutionException if no task completes
1091 */
1092 public void testInvokeAny4() throws Exception {
1093 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1094 try {
1095 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1096 l.add(new NPETask());
1097 e.invokeAny(l);
1098 shouldThrow();
1099 } catch (ExecutionException success) {
1100 assertTrue(success.getCause() instanceof NullPointerException);
1101 } finally {
1102 joinPool(e);
1103 }
1104 }
1105
1106 /**
1107 * invokeAny(c) returns result of some task
1108 */
1109 public void testInvokeAny5() throws Exception {
1110 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1111 try {
1112 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1113 l.add(new StringTask());
1114 l.add(new StringTask());
1115 String result = e.invokeAny(l);
1116 assertSame(TEST_STRING, result);
1117 } finally {
1118 joinPool(e);
1119 }
1120 }
1121
1122 /**
1123 * invokeAll(null) throws NPE
1124 */
1125 public void testInvokeAll1() throws Exception {
1126 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1127 try {
1128 e.invokeAll(null);
1129 shouldThrow();
1130 } catch (NullPointerException success) {
1131 } finally {
1132 joinPool(e);
1133 }
1134 }
1135
1136 /**
1137 * invokeAll(empty collection) returns empty collection
1138 */
1139 public void testInvokeAll2() throws InterruptedException {
1140 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1141 try {
1142 List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1143 assertTrue(r.isEmpty());
1144 } finally {
1145 joinPool(e);
1146 }
1147 }
1148
1149 /**
1150 * invokeAll(c) throws NPE if c has null elements
1151 */
1152 public void testInvokeAll3() throws Exception {
1153 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1154 try {
1155 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1156 l.add(new StringTask());
1157 l.add(null);
1158 e.invokeAll(l);
1159 shouldThrow();
1160 } catch (NullPointerException success) {
1161 } finally {
1162 joinPool(e);
1163 }
1164 }
1165
1166 /**
1167 * get of element of invokeAll(c) throws exception on failed task
1168 */
1169 public void testInvokeAll4() throws Exception {
1170 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1171 try {
1172 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1173 l.add(new NPETask());
1174 List<Future<String>> result = e.invokeAll(l);
1175 assertEquals(1, result.size());
1176 for (Future<String> future : result) {
1177 try {
1178 future.get();
1179 shouldThrow();
1180 } catch (ExecutionException success) {
1181 Throwable cause = success.getCause();
1182 assertTrue(cause instanceof NullPointerException);
1183 }
1184 }
1185 } finally {
1186 joinPool(e);
1187 }
1188 }
1189
1190 /**
1191 * invokeAll(c) returns results of all completed tasks
1192 */
1193 public void testInvokeAll5() throws Exception {
1194 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1195 try {
1196 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1197 l.add(new StringTask());
1198 l.add(new StringTask());
1199 List<Future<String>> result = e.invokeAll(l);
1200 assertEquals(2, result.size());
1201 for (Future<String> future : result)
1202 assertSame(TEST_STRING, future.get());
1203 } finally {
1204 joinPool(e);
1205 }
1206 }
1207
1208
1209
1210 /**
1211 * timed invokeAny(null) throws NPE
1212 */
1213 public void testTimedInvokeAny1() throws Exception {
1214 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1215 try {
1216 e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1217 shouldThrow();
1218 } catch (NullPointerException success) {
1219 } finally {
1220 joinPool(e);
1221 }
1222 }
1223
1224 /**
1225 * timed invokeAny(,,null) throws NPE
1226 */
1227 public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1228 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1229 try {
1230 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1231 l.add(new StringTask());
1232 e.invokeAny(l, MEDIUM_DELAY_MS, null);
1233 shouldThrow();
1234 } catch (NullPointerException success) {
1235 } finally {
1236 joinPool(e);
1237 }
1238 }
1239
1240 /**
1241 * timed invokeAny(empty collection) throws IAE
1242 */
1243 public void testTimedInvokeAny2() throws Exception {
1244 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1245 try {
1246 e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1247 shouldThrow();
1248 } catch (IllegalArgumentException success) {
1249 } finally {
1250 joinPool(e);
1251 }
1252 }
1253
1254 /**
1255 * timed invokeAny(c) throws NPE if c has null elements
1256 */
1257 public void testTimedInvokeAny3() throws Exception {
1258 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1259 try {
1260 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1261 l.add(new StringTask());
1262 l.add(null);
1263 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1264 shouldThrow();
1265 } catch (NullPointerException success) {
1266 } finally {
1267 joinPool(e);
1268 }
1269 }
1270
1271 /**
1272 * timed invokeAny(c) throws ExecutionException if no task completes
1273 */
1274 public void testTimedInvokeAny4() throws Exception {
1275 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1276 try {
1277 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1278 l.add(new NPETask());
1279 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1280 shouldThrow();
1281 } catch (ExecutionException success) {
1282 assertTrue(success.getCause() instanceof NullPointerException);
1283 } finally {
1284 joinPool(e);
1285 }
1286 }
1287
1288 /**
1289 * timed invokeAny(c) returns result of some task
1290 */
1291 public void testTimedInvokeAny5() throws Exception {
1292 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1293 try {
1294 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1295 l.add(new StringTask());
1296 l.add(new StringTask());
1297 String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1298 assertSame(TEST_STRING, result);
1299 } finally {
1300 joinPool(e);
1301 }
1302 }
1303
1304 /**
1305 * timed invokeAll(null) throws NPE
1306 */
1307 public void testTimedInvokeAll1() throws Exception {
1308 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1309 try {
1310 e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1311 shouldThrow();
1312 } catch (NullPointerException success) {
1313 } finally {
1314 joinPool(e);
1315 }
1316 }
1317
1318 /**
1319 * timed invokeAll(,,null) throws NPE
1320 */
1321 public void testTimedInvokeAllNullTimeUnit() throws Exception {
1322 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1323 try {
1324 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1325 l.add(new StringTask());
1326 e.invokeAll(l, MEDIUM_DELAY_MS, null);
1327 shouldThrow();
1328 } catch (NullPointerException success) {
1329 } finally {
1330 joinPool(e);
1331 }
1332 }
1333
1334 /**
1335 * timed invokeAll(empty collection) returns empty collection
1336 */
1337 public void testTimedInvokeAll2() throws InterruptedException {
1338 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1339 try {
1340 List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1341 assertTrue(r.isEmpty());
1342 } finally {
1343 joinPool(e);
1344 }
1345 }
1346
1347 /**
1348 * timed invokeAll(c) throws NPE if c has null elements
1349 */
1350 public void testTimedInvokeAll3() throws Exception {
1351 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1352 try {
1353 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1354 l.add(new StringTask());
1355 l.add(null);
1356 e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1357 shouldThrow();
1358 } catch (NullPointerException success) {
1359 } finally {
1360 joinPool(e);
1361 }
1362 }
1363
1364 /**
1365 * get of element of invokeAll(c) throws exception on failed task
1366 */
1367 public void testTimedInvokeAll4() throws Exception {
1368 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1369 try {
1370 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1371 l.add(new NPETask());
1372 List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1373 assertEquals(1, result.size());
1374 for (Future<String> future : result)
1375 future.get();
1376 shouldThrow();
1377 } catch (ExecutionException success) {
1378 assertTrue(success.getCause() instanceof NullPointerException);
1379 } finally {
1380 joinPool(e);
1381 }
1382 }
1383
1384 /**
1385 * timed invokeAll(c) returns results of all completed tasks
1386 */
1387 public void testTimedInvokeAll5() throws Exception {
1388 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1389 try {
1390 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1391 l.add(new StringTask());
1392 l.add(new StringTask());
1393 List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1394 assertEquals(2, result.size());
1395 for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1396 assertSame(TEST_STRING, it.next().get());
1397 } finally {
1398 joinPool(e);
1399 }
1400 }
1401
1402 /**
1403 * timed invokeAll(c) cancels tasks not completed by timeout
1404 */
1405 public void testTimedInvokeAll6() throws Exception {
1406 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1407 try {
1408 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1409 l.add(new StringTask());
1410 l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1411 l.add(new StringTask());
1412 List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1413 assertEquals(3, result.size());
1414 Iterator<Future<String>> it = result.iterator();
1415 Future<String> f1 = it.next();
1416 Future<String> f2 = it.next();
1417 Future<String> f3 = it.next();
1418 assertTrue(f1.isDone());
1419 assertTrue(f2.isDone());
1420 assertTrue(f3.isDone());
1421 assertFalse(f1.isCancelled());
1422 assertTrue(f2.isCancelled());
1423 } finally {
1424 joinPool(e);
1425 }
1426 }
1427
1428 /**
1429 * Execution continues if there is at least one thread even if
1430 * thread factory fails to create more
1431 */
1432 public void testFailingThreadFactory() throws InterruptedException {
1433 ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1434 try {
1435 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1436 for (int k = 0; k < 100; ++k) {
1437 e.execute(new NoOpRunnable());
1438 }
1439 Thread.sleep(LONG_DELAY_MS);
1440 } finally {
1441 joinPool(e);
1442 }
1443 }
1444
1445 /**
1446 * allowsCoreThreadTimeOut is by default false.
1447 */
1448 public void testAllowsCoreThreadTimeOut() {
1449 ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1450 assertFalse(tpe.allowsCoreThreadTimeOut());
1451 joinPool(tpe);
1452 }
1453
1454 /**
1455 * allowCoreThreadTimeOut(true) causes idle threads to time out
1456 */
1457 public void testAllowCoreThreadTimeOut_true() throws InterruptedException {
1458 ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1459 tpe.allowCoreThreadTimeOut(true);
1460 tpe.execute(new NoOpRunnable());
1461 try {
1462 Thread.sleep(MEDIUM_DELAY_MS);
1463 assertEquals(0, tpe.getPoolSize());
1464 } finally {
1465 joinPool(tpe);
1466 }
1467 }
1468
1469 /**
1470 * allowCoreThreadTimeOut(false) causes idle threads not to time out
1471 */
1472 public void testAllowCoreThreadTimeOut_false() throws InterruptedException {
1473 ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1474 tpe.allowCoreThreadTimeOut(false);
1475 tpe.execute(new NoOpRunnable());
1476 try {
1477 Thread.sleep(MEDIUM_DELAY_MS);
1478 assertTrue(tpe.getPoolSize() >= 1);
1479 } finally {
1480 joinPool(tpe);
1481 }
1482 }
1483
1484 /**
1485 * execute allows the same task to be submitted multiple times, even
1486 * if rejected
1487 */
1488 public void testRejectedRecycledTask() throws InterruptedException {
1489 final int nTasks = 1000;
1490 final AtomicInteger nRun = new AtomicInteger(0);
1491 final Runnable recycledTask = new Runnable() {
1492 public void run() {
1493 nRun.getAndIncrement();
1494 } };
1495 final ThreadPoolExecutor p =
1496 new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
1497 new ArrayBlockingQueue(30));
1498 try {
1499 for (int i = 0; i < nTasks; ++i) {
1500 for (;;) {
1501 try {
1502 p.execute(recycledTask);
1503 break;
1504 }
1505 catch (RejectedExecutionException ignore) {
1506 }
1507 }
1508 }
1509 Thread.sleep(5000); // enough time to run all tasks
1510 assertEquals(nRun.get(), nTasks);
1511 } finally {
1512 p.shutdown();
1513 }
1514 }
1515
1516 }