ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java
Revision: 1.1
Committed: Fri May 20 16:30:17 2005 UTC (19 years ago) by dl
Branch: MAIN
Log Message:
Add tests for new protected extenion methods

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