ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.20
Committed: Mon Apr 12 12:03:10 2004 UTC (20 years, 1 month ago) by dl
Branch: MAIN
Changes since 1.19: +1 -1 lines
Log Message:
Change testFailingThreadFactory to only check null return case

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