ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java
Revision: 1.85
Committed: Mon Oct 5 21:54:33 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.84: +2 -3 lines
Log Message:
improve testPurge

File Contents

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