ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java
Revision: 1.88
Committed: Tue Oct 6 00:41:47 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.87: +1 -1 lines
Log Message:
bump up timeout

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