ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java
Revision: 1.5
Committed: Wed Nov 18 16:51:26 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.4: +111 -240 lines
Log Message:
sync with ThreadPoolExecutorTest.java

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