ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java
Revision: 1.19
Committed: Sat Oct 9 19:30:35 2010 UTC (13 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.18: +36 -37 lines
Log Message:
whitespace

File Contents

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