ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java
Revision: 1.62
Committed: Sun Oct 4 02:04:56 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.61: +6 -8 lines
Log Message:
improve testGetLargestPoolSize

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     final CountDownLatch threadStarted = new CountDownLatch(1);
256     final CountDownLatch done = new CountDownLatch(1);
257 jsr166 1.51 try (PoolCleaner cleaner = cleaner(p)) {
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     assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
266     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.52 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 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.53 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 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.21 ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
507     assertEquals(2, p.getMaximumPoolSize());
508     joinPool(p);
509 dl 1.1 }
510 jsr166 1.2
511 dl 1.1 /**
512 jsr166 1.19 * getPoolSize increases, but doesn't overestimate, when threads
513     * become active
514 dl 1.1 */
515 jsr166 1.21 public void testGetPoolSize() throws InterruptedException {
516     final ThreadPoolExecutor p =
517     new CustomTPE(1, 1,
518     LONG_DELAY_MS, MILLISECONDS,
519     new ArrayBlockingQueue<Runnable>(10));
520     final CountDownLatch threadStarted = new CountDownLatch(1);
521     final CountDownLatch done = new CountDownLatch(1);
522     try {
523     assertEquals(0, p.getPoolSize());
524     p.execute(new CheckedRunnable() {
525     public void realRun() throws InterruptedException {
526     threadStarted.countDown();
527     assertEquals(1, p.getPoolSize());
528     done.await();
529     }});
530     assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
531     assertEquals(1, p.getPoolSize());
532     } finally {
533     done.countDown();
534     joinPool(p);
535     }
536 dl 1.1 }
537 jsr166 1.2
538 dl 1.1 /**
539 jsr166 1.19 * getTaskCount increases, but doesn't overestimate, when tasks submitted
540 dl 1.1 */
541 jsr166 1.5 public void testGetTaskCount() throws InterruptedException {
542 jsr166 1.21 final ThreadPoolExecutor p =
543     new CustomTPE(1, 1,
544     LONG_DELAY_MS, MILLISECONDS,
545     new ArrayBlockingQueue<Runnable>(10));
546     final CountDownLatch threadStarted = new CountDownLatch(1);
547     final CountDownLatch done = new CountDownLatch(1);
548     try {
549     assertEquals(0, p.getTaskCount());
550     p.execute(new CheckedRunnable() {
551     public void realRun() throws InterruptedException {
552     threadStarted.countDown();
553     assertEquals(1, p.getTaskCount());
554     done.await();
555     }});
556     assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
557     assertEquals(1, p.getTaskCount());
558     } finally {
559     done.countDown();
560     joinPool(p);
561     }
562 dl 1.1 }
563 jsr166 1.2
564 dl 1.1 /**
565 jsr166 1.28 * isShutdown is false before shutdown, true after
566 dl 1.1 */
567     public void testIsShutdown() {
568 jsr166 1.2
569 jsr166 1.21 ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
570     assertFalse(p.isShutdown());
571     try { p.shutdown(); } catch (SecurityException ok) { return; }
572     assertTrue(p.isShutdown());
573     joinPool(p);
574 dl 1.1 }
575    
576     /**
577 jsr166 1.19 * isTerminated is false before termination, true after
578 dl 1.1 */
579 jsr166 1.5 public void testIsTerminated() throws InterruptedException {
580 jsr166 1.21 final ThreadPoolExecutor p =
581     new CustomTPE(1, 1,
582     LONG_DELAY_MS, MILLISECONDS,
583     new ArrayBlockingQueue<Runnable>(10));
584     final CountDownLatch threadStarted = new CountDownLatch(1);
585     final CountDownLatch done = new CountDownLatch(1);
586     try {
587     assertFalse(p.isTerminating());
588     p.execute(new CheckedRunnable() {
589     public void realRun() throws InterruptedException {
590 jsr166 1.24 assertFalse(p.isTerminating());
591 jsr166 1.21 threadStarted.countDown();
592     done.await();
593     }});
594     assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
595     assertFalse(p.isTerminating());
596     done.countDown();
597     } finally {
598     try { p.shutdown(); } catch (SecurityException ok) { return; }
599     }
600     assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
601     assertTrue(p.isTerminated());
602     assertFalse(p.isTerminating());
603 dl 1.1 }
604    
605     /**
606 jsr166 1.19 * isTerminating is not true when running or when terminated
607 dl 1.1 */
608 jsr166 1.5 public void testIsTerminating() throws InterruptedException {
609 jsr166 1.21 final ThreadPoolExecutor p =
610     new CustomTPE(1, 1,
611     LONG_DELAY_MS, MILLISECONDS,
612     new ArrayBlockingQueue<Runnable>(10));
613     final CountDownLatch threadStarted = new CountDownLatch(1);
614     final CountDownLatch done = new CountDownLatch(1);
615     try {
616     assertFalse(p.isTerminating());
617     p.execute(new CheckedRunnable() {
618     public void realRun() throws InterruptedException {
619 jsr166 1.23 assertFalse(p.isTerminating());
620 jsr166 1.21 threadStarted.countDown();
621     done.await();
622     }});
623     assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
624     assertFalse(p.isTerminating());
625     done.countDown();
626     } finally {
627     try { p.shutdown(); } catch (SecurityException ok) { return; }
628     }
629     assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
630     assertTrue(p.isTerminated());
631     assertFalse(p.isTerminating());
632 dl 1.1 }
633    
634     /**
635     * getQueue returns the work queue, which contains queued tasks
636     */
637 jsr166 1.5 public void testGetQueue() throws InterruptedException {
638 jsr166 1.21 final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
639     final ThreadPoolExecutor p =
640     new CustomTPE(1, 1,
641     LONG_DELAY_MS, MILLISECONDS,
642     q);
643     final CountDownLatch threadStarted = new CountDownLatch(1);
644     final CountDownLatch done = new CountDownLatch(1);
645     try {
646     FutureTask[] tasks = new FutureTask[5];
647     for (int i = 0; i < tasks.length; i++) {
648     Callable task = new CheckedCallable<Boolean>() {
649     public Boolean realCall() throws InterruptedException {
650     threadStarted.countDown();
651     assertSame(q, p.getQueue());
652     done.await();
653     return Boolean.TRUE;
654     }};
655     tasks[i] = new FutureTask(task);
656     p.execute(tasks[i]);
657     }
658     assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
659     assertSame(q, p.getQueue());
660     assertFalse(q.contains(tasks[0]));
661     assertTrue(q.contains(tasks[tasks.length - 1]));
662     assertEquals(tasks.length - 1, q.size());
663 dl 1.1 } finally {
664 jsr166 1.21 done.countDown();
665     joinPool(p);
666 dl 1.1 }
667     }
668    
669     /**
670     * remove(task) removes queued task, and fails to remove active task
671     */
672 jsr166 1.5 public void testRemove() throws InterruptedException {
673 dl 1.1 BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
674 jsr166 1.21 final ThreadPoolExecutor p =
675     new CustomTPE(1, 1,
676     LONG_DELAY_MS, MILLISECONDS,
677     q);
678     Runnable[] tasks = new Runnable[6];
679     final CountDownLatch threadStarted = new CountDownLatch(1);
680     final CountDownLatch done = new CountDownLatch(1);
681     try {
682     for (int i = 0; i < tasks.length; i++) {
683     tasks[i] = new CheckedRunnable() {
684     public void realRun() throws InterruptedException {
685     threadStarted.countDown();
686     done.await();
687     }};
688     p.execute(tasks[i]);
689     }
690     assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
691     assertFalse(p.remove(tasks[0]));
692 dl 1.1 assertTrue(q.contains(tasks[4]));
693     assertTrue(q.contains(tasks[3]));
694 jsr166 1.21 assertTrue(p.remove(tasks[4]));
695     assertFalse(p.remove(tasks[4]));
696 dl 1.1 assertFalse(q.contains(tasks[4]));
697     assertTrue(q.contains(tasks[3]));
698 jsr166 1.21 assertTrue(p.remove(tasks[3]));
699 dl 1.1 assertFalse(q.contains(tasks[3]));
700     } finally {
701 jsr166 1.21 done.countDown();
702     joinPool(p);
703 dl 1.1 }
704     }
705    
706     /**
707 jsr166 1.19 * purge removes cancelled tasks from the queue
708 dl 1.1 */
709 jsr166 1.21 public void testPurge() throws InterruptedException {
710     final CountDownLatch threadStarted = new CountDownLatch(1);
711     final CountDownLatch done = new CountDownLatch(1);
712     final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
713     final ThreadPoolExecutor p =
714     new CustomTPE(1, 1,
715     LONG_DELAY_MS, MILLISECONDS,
716     q);
717 dl 1.1 FutureTask[] tasks = new FutureTask[5];
718 jsr166 1.21 try {
719     for (int i = 0; i < tasks.length; i++) {
720     Callable task = new CheckedCallable<Boolean>() {
721     public Boolean realCall() throws InterruptedException {
722     threadStarted.countDown();
723     done.await();
724     return Boolean.TRUE;
725     }};
726     tasks[i] = new FutureTask(task);
727     p.execute(tasks[i]);
728     }
729     assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
730     assertEquals(tasks.length, p.getTaskCount());
731     assertEquals(tasks.length - 1, q.size());
732     assertEquals(1L, p.getActiveCount());
733     assertEquals(0L, p.getCompletedTaskCount());
734     tasks[4].cancel(true);
735     tasks[3].cancel(false);
736     p.purge();
737     assertEquals(tasks.length - 3, q.size());
738     assertEquals(tasks.length - 2, p.getTaskCount());
739     p.purge(); // Nothing to do
740     assertEquals(tasks.length - 3, q.size());
741     assertEquals(tasks.length - 2, p.getTaskCount());
742     } finally {
743     done.countDown();
744     joinPool(p);
745     }
746 dl 1.1 }
747    
748     /**
749 jsr166 1.40 * shutdownNow returns a list containing tasks that were not run,
750     * and those tasks are drained from the queue
751 dl 1.1 */
752 jsr166 1.41 public void testShutdownNow() throws InterruptedException {
753     final int poolSize = 2;
754     final int count = 5;
755     final AtomicInteger ran = new AtomicInteger(0);
756     ThreadPoolExecutor p =
757     new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS,
758     new ArrayBlockingQueue<Runnable>(10));
759     CountDownLatch threadsStarted = new CountDownLatch(poolSize);
760 jsr166 1.43 Runnable waiter = new CheckedRunnable() { public void realRun() {
761 jsr166 1.41 threadsStarted.countDown();
762 dl 1.1 try {
763 jsr166 1.41 MILLISECONDS.sleep(2 * LONG_DELAY_MS);
764     } catch (InterruptedException success) {}
765     ran.getAndIncrement();
766     }};
767     for (int i = 0; i < count; i++)
768     p.execute(waiter);
769     assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
770 jsr166 1.42 assertEquals(poolSize, p.getActiveCount());
771     assertEquals(0, p.getCompletedTaskCount());
772 jsr166 1.41 final List<Runnable> queuedTasks;
773     try {
774     queuedTasks = p.shutdownNow();
775     } catch (SecurityException ok) {
776     return; // Allowed in case test doesn't have privs
777 dl 1.1 }
778 jsr166 1.21 assertTrue(p.isShutdown());
779 jsr166 1.40 assertTrue(p.getQueue().isEmpty());
780 jsr166 1.41 assertEquals(count - poolSize, queuedTasks.size());
781     assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
782     assertTrue(p.isTerminated());
783     assertEquals(poolSize, ran.get());
784 jsr166 1.42 assertEquals(poolSize, p.getCompletedTaskCount());
785 dl 1.1 }
786    
787     // Exception Tests
788    
789 jsr166 1.2 /**
790     * Constructor throws if corePoolSize argument is less than zero
791 dl 1.1 */
792     public void testConstructor1() {
793     try {
794 jsr166 1.36 new CustomTPE(-1, 1, 1L, SECONDS,
795     new ArrayBlockingQueue<Runnable>(10));
796 dl 1.1 shouldThrow();
797 jsr166 1.5 } catch (IllegalArgumentException success) {}
798 dl 1.1 }
799 jsr166 1.2
800     /**
801     * Constructor throws if maximumPoolSize is less than zero
802 dl 1.1 */
803     public void testConstructor2() {
804     try {
805 jsr166 1.36 new CustomTPE(1, -1, 1L, SECONDS,
806     new ArrayBlockingQueue<Runnable>(10));
807 dl 1.1 shouldThrow();
808 jsr166 1.5 } catch (IllegalArgumentException success) {}
809 dl 1.1 }
810 jsr166 1.2
811     /**
812     * Constructor throws if maximumPoolSize is equal to zero
813 dl 1.1 */
814     public void testConstructor3() {
815     try {
816 jsr166 1.36 new CustomTPE(1, 0, 1L, SECONDS,
817     new ArrayBlockingQueue<Runnable>(10));
818 dl 1.1 shouldThrow();
819 jsr166 1.5 } catch (IllegalArgumentException success) {}
820 dl 1.1 }
821    
822 jsr166 1.2 /**
823     * Constructor throws if keepAliveTime is less than zero
824 dl 1.1 */
825     public void testConstructor4() {
826     try {
827 jsr166 1.36 new CustomTPE(1, 2, -1L, SECONDS,
828     new ArrayBlockingQueue<Runnable>(10));
829 dl 1.1 shouldThrow();
830 jsr166 1.5 } catch (IllegalArgumentException success) {}
831 dl 1.1 }
832    
833 jsr166 1.2 /**
834     * Constructor throws if corePoolSize is greater than the maximumPoolSize
835 dl 1.1 */
836     public void testConstructor5() {
837     try {
838 jsr166 1.36 new CustomTPE(2, 1, 1L, SECONDS,
839     new ArrayBlockingQueue<Runnable>(10));
840 dl 1.1 shouldThrow();
841 jsr166 1.5 } catch (IllegalArgumentException success) {}
842 dl 1.1 }
843 jsr166 1.2
844     /**
845     * Constructor throws if workQueue is set to null
846 dl 1.1 */
847     public void testConstructorNullPointerException() {
848     try {
849 jsr166 1.36 new CustomTPE(1, 2, 1L, SECONDS, null);
850 dl 1.1 shouldThrow();
851 jsr166 1.5 } catch (NullPointerException success) {}
852 dl 1.1 }
853    
854 jsr166 1.2 /**
855     * Constructor throws if corePoolSize argument is less than zero
856 dl 1.1 */
857     public void testConstructor6() {
858     try {
859 jsr166 1.36 new CustomTPE(-1, 1, 1L, SECONDS,
860     new ArrayBlockingQueue<Runnable>(10),
861     new SimpleThreadFactory());
862 dl 1.1 shouldThrow();
863 jsr166 1.4 } catch (IllegalArgumentException success) {}
864 dl 1.1 }
865 jsr166 1.2
866     /**
867     * Constructor throws if maximumPoolSize is less than zero
868 dl 1.1 */
869     public void testConstructor7() {
870     try {
871 jsr166 1.36 new CustomTPE(1,-1, 1L, SECONDS,
872     new ArrayBlockingQueue<Runnable>(10),
873     new SimpleThreadFactory());
874 dl 1.1 shouldThrow();
875 jsr166 1.5 } catch (IllegalArgumentException success) {}
876 dl 1.1 }
877    
878 jsr166 1.2 /**
879     * Constructor throws if maximumPoolSize is equal to zero
880 dl 1.1 */
881     public void testConstructor8() {
882     try {
883 jsr166 1.36 new CustomTPE(1, 0, 1L, SECONDS,
884     new ArrayBlockingQueue<Runnable>(10),
885     new SimpleThreadFactory());
886 dl 1.1 shouldThrow();
887 jsr166 1.5 } catch (IllegalArgumentException success) {}
888 dl 1.1 }
889    
890 jsr166 1.2 /**
891     * Constructor throws if keepAliveTime is less than zero
892 dl 1.1 */
893     public void testConstructor9() {
894     try {
895 jsr166 1.36 new CustomTPE(1, 2, -1L, SECONDS,
896     new ArrayBlockingQueue<Runnable>(10),
897     new SimpleThreadFactory());
898 dl 1.1 shouldThrow();
899 jsr166 1.5 } catch (IllegalArgumentException success) {}
900 dl 1.1 }
901    
902 jsr166 1.2 /**
903     * Constructor throws if corePoolSize is greater than the maximumPoolSize
904 dl 1.1 */
905     public void testConstructor10() {
906     try {
907 jsr166 1.36 new CustomTPE(2, 1, 1L, SECONDS,
908     new ArrayBlockingQueue<Runnable>(10),
909     new SimpleThreadFactory());
910 dl 1.1 shouldThrow();
911 jsr166 1.5 } catch (IllegalArgumentException success) {}
912 dl 1.1 }
913    
914 jsr166 1.2 /**
915     * Constructor throws if workQueue is set to null
916 dl 1.1 */
917     public void testConstructorNullPointerException2() {
918     try {
919 jsr166 1.36 new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
920 dl 1.1 shouldThrow();
921 jsr166 1.5 } catch (NullPointerException success) {}
922 dl 1.1 }
923    
924 jsr166 1.2 /**
925     * Constructor throws if threadFactory is set to null
926 dl 1.1 */
927     public void testConstructorNullPointerException3() {
928     try {
929 jsr166 1.36 new CustomTPE(1, 2, 1L, SECONDS,
930     new ArrayBlockingQueue<Runnable>(10),
931     (ThreadFactory) null);
932 dl 1.1 shouldThrow();
933 jsr166 1.5 } catch (NullPointerException success) {}
934 dl 1.1 }
935 jsr166 1.2
936     /**
937     * Constructor throws if corePoolSize argument is less than zero
938 dl 1.1 */
939     public void testConstructor11() {
940     try {
941 jsr166 1.36 new CustomTPE(-1, 1, 1L, SECONDS,
942     new ArrayBlockingQueue<Runnable>(10),
943     new NoOpREHandler());
944 dl 1.1 shouldThrow();
945 jsr166 1.5 } catch (IllegalArgumentException success) {}
946 dl 1.1 }
947    
948 jsr166 1.2 /**
949     * Constructor throws if maximumPoolSize is less than zero
950 dl 1.1 */
951     public void testConstructor12() {
952     try {
953 jsr166 1.36 new CustomTPE(1, -1, 1L, SECONDS,
954     new ArrayBlockingQueue<Runnable>(10),
955     new NoOpREHandler());
956 dl 1.1 shouldThrow();
957 jsr166 1.5 } catch (IllegalArgumentException success) {}
958 dl 1.1 }
959    
960 jsr166 1.2 /**
961     * Constructor throws if maximumPoolSize is equal to zero
962 dl 1.1 */
963     public void testConstructor13() {
964     try {
965 jsr166 1.36 new CustomTPE(1, 0, 1L, SECONDS,
966     new ArrayBlockingQueue<Runnable>(10),
967     new NoOpREHandler());
968 dl 1.1 shouldThrow();
969 jsr166 1.5 } catch (IllegalArgumentException success) {}
970 dl 1.1 }
971    
972 jsr166 1.2 /**
973     * Constructor throws if keepAliveTime is less than zero
974 dl 1.1 */
975     public void testConstructor14() {
976     try {
977 jsr166 1.36 new CustomTPE(1, 2, -1L, SECONDS,
978     new ArrayBlockingQueue<Runnable>(10),
979     new NoOpREHandler());
980 dl 1.1 shouldThrow();
981 jsr166 1.5 } catch (IllegalArgumentException success) {}
982 dl 1.1 }
983    
984 jsr166 1.2 /**
985     * Constructor throws if corePoolSize is greater than the maximumPoolSize
986 dl 1.1 */
987     public void testConstructor15() {
988     try {
989 jsr166 1.36 new CustomTPE(2, 1, 1L, SECONDS,
990     new ArrayBlockingQueue<Runnable>(10),
991     new NoOpREHandler());
992 dl 1.1 shouldThrow();
993 jsr166 1.5 } catch (IllegalArgumentException success) {}
994 dl 1.1 }
995    
996 jsr166 1.2 /**
997     * Constructor throws if workQueue is set to null
998 dl 1.1 */
999     public void testConstructorNullPointerException4() {
1000     try {
1001 jsr166 1.36 new CustomTPE(1, 2, 1L, SECONDS,
1002     null,
1003     new NoOpREHandler());
1004 dl 1.1 shouldThrow();
1005 jsr166 1.5 } catch (NullPointerException success) {}
1006 dl 1.1 }
1007    
1008 jsr166 1.2 /**
1009     * Constructor throws if handler is set to null
1010 dl 1.1 */
1011     public void testConstructorNullPointerException5() {
1012     try {
1013 jsr166 1.36 new CustomTPE(1, 2, 1L, SECONDS,
1014     new ArrayBlockingQueue<Runnable>(10),
1015     (RejectedExecutionHandler) null);
1016 dl 1.1 shouldThrow();
1017 jsr166 1.5 } catch (NullPointerException success) {}
1018 dl 1.1 }
1019    
1020 jsr166 1.2 /**
1021     * Constructor throws if corePoolSize argument is less than zero
1022 dl 1.1 */
1023     public void testConstructor16() {
1024     try {
1025 jsr166 1.36 new CustomTPE(-1, 1, 1L, SECONDS,
1026     new ArrayBlockingQueue<Runnable>(10),
1027     new SimpleThreadFactory(),
1028     new NoOpREHandler());
1029 dl 1.1 shouldThrow();
1030 jsr166 1.5 } catch (IllegalArgumentException success) {}
1031 dl 1.1 }
1032    
1033 jsr166 1.2 /**
1034     * Constructor throws if maximumPoolSize is less than zero
1035 dl 1.1 */
1036     public void testConstructor17() {
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 equal to zero
1048 dl 1.1 */
1049     public void testConstructor18() {
1050     try {
1051 jsr166 1.36 new CustomTPE(1, 0, 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 keepAliveTime is less than zero
1061 dl 1.1 */
1062     public void testConstructor19() {
1063     try {
1064 jsr166 1.36 new CustomTPE(1, 2, -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 corePoolSize is greater than the maximumPoolSize
1074 dl 1.1 */
1075     public void testConstructor20() {
1076     try {
1077 jsr166 1.36 new CustomTPE(2, 1, 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 jsr166 1.20 * Constructor throws if workQueue is null
1087 dl 1.1 */
1088     public void testConstructorNullPointerException6() {
1089     try {
1090 jsr166 1.36 new CustomTPE(1, 2, 1L, SECONDS,
1091     null,
1092     new SimpleThreadFactory(),
1093     new NoOpREHandler());
1094 dl 1.1 shouldThrow();
1095 jsr166 1.5 } catch (NullPointerException success) {}
1096 dl 1.1 }
1097    
1098 jsr166 1.2 /**
1099 jsr166 1.20 * Constructor throws if handler is null
1100 dl 1.1 */
1101     public void testConstructorNullPointerException7() {
1102     try {
1103 jsr166 1.36 new CustomTPE(1, 2, 1L, SECONDS,
1104     new ArrayBlockingQueue<Runnable>(10),
1105     new SimpleThreadFactory(),
1106     (RejectedExecutionHandler) null);
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 ThreadFactory is null
1113 dl 1.1 */
1114     public void testConstructorNullPointerException8() {
1115     try {
1116 jsr166 1.36 new CustomTPE(1, 2, 1L, SECONDS,
1117 jsr166 1.21 new ArrayBlockingQueue<Runnable>(10),
1118     (ThreadFactory) null,
1119     new NoOpREHandler());
1120 dl 1.1 shouldThrow();
1121 jsr166 1.5 } catch (NullPointerException success) {}
1122 dl 1.1 }
1123 jsr166 1.2
1124 dl 1.1 /**
1125 jsr166 1.19 * execute throws RejectedExecutionException if saturated.
1126 dl 1.1 */
1127     public void testSaturatedExecute() {
1128 jsr166 1.21 ThreadPoolExecutor p =
1129     new CustomTPE(1, 1,
1130     LONG_DELAY_MS, MILLISECONDS,
1131     new ArrayBlockingQueue<Runnable>(1));
1132     final CountDownLatch done = new CountDownLatch(1);
1133     try {
1134     Runnable task = new CheckedRunnable() {
1135     public void realRun() throws InterruptedException {
1136     done.await();
1137     }};
1138     for (int i = 0; i < 2; ++i)
1139     p.execute(task);
1140     for (int i = 0; i < 2; ++i) {
1141     try {
1142     p.execute(task);
1143     shouldThrow();
1144     } catch (RejectedExecutionException success) {}
1145     assertTrue(p.getTaskCount() <= 2);
1146 dl 1.1 }
1147 jsr166 1.21 } finally {
1148     done.countDown();
1149     joinPool(p);
1150     }
1151 dl 1.1 }
1152    
1153     /**
1154 jsr166 1.19 * executor using CallerRunsPolicy runs task if saturated.
1155 dl 1.1 */
1156     public void testSaturatedExecute2() {
1157     RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1158 jsr166 1.21 ThreadPoolExecutor p = new CustomTPE(1, 1,
1159     LONG_DELAY_MS, MILLISECONDS,
1160     new ArrayBlockingQueue<Runnable>(1),
1161     h);
1162 dl 1.1 try {
1163     TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1164 jsr166 1.21 for (int i = 0; i < tasks.length; ++i)
1165 dl 1.1 tasks[i] = new TrackedNoOpRunnable();
1166     TrackedLongRunnable mr = new TrackedLongRunnable();
1167     p.execute(mr);
1168 jsr166 1.21 for (int i = 0; i < tasks.length; ++i)
1169 dl 1.1 p.execute(tasks[i]);
1170 jsr166 1.21 for (int i = 1; i < tasks.length; ++i)
1171 dl 1.1 assertTrue(tasks[i].done);
1172 jsr166 1.3 try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1173 dl 1.1 } finally {
1174     joinPool(p);
1175     }
1176     }
1177    
1178     /**
1179 jsr166 1.19 * executor using DiscardPolicy drops task if saturated.
1180 dl 1.1 */
1181     public void testSaturatedExecute3() {
1182     RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1183 jsr166 1.21 ThreadPoolExecutor p =
1184     new CustomTPE(1, 1,
1185     LONG_DELAY_MS, MILLISECONDS,
1186     new ArrayBlockingQueue<Runnable>(1),
1187     h);
1188 dl 1.1 try {
1189     TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1190 jsr166 1.21 for (int i = 0; i < tasks.length; ++i)
1191 dl 1.1 tasks[i] = new TrackedNoOpRunnable();
1192     p.execute(new TrackedLongRunnable());
1193 jsr166 1.21 for (TrackedNoOpRunnable task : tasks)
1194     p.execute(task);
1195     for (TrackedNoOpRunnable task : tasks)
1196     assertFalse(task.done);
1197 jsr166 1.3 try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1198 dl 1.1 } finally {
1199     joinPool(p);
1200     }
1201     }
1202    
1203     /**
1204 jsr166 1.19 * executor using DiscardOldestPolicy drops oldest task if saturated.
1205 dl 1.1 */
1206     public void testSaturatedExecute4() {
1207     RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
1208 jsr166 1.9 ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1209 dl 1.1 try {
1210     p.execute(new TrackedLongRunnable());
1211     TrackedLongRunnable r2 = new TrackedLongRunnable();
1212     p.execute(r2);
1213     assertTrue(p.getQueue().contains(r2));
1214     TrackedNoOpRunnable r3 = new TrackedNoOpRunnable();
1215     p.execute(r3);
1216     assertFalse(p.getQueue().contains(r2));
1217     assertTrue(p.getQueue().contains(r3));
1218 jsr166 1.3 try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1219 dl 1.1 } finally {
1220     joinPool(p);
1221     }
1222     }
1223    
1224     /**
1225 jsr166 1.19 * execute throws RejectedExecutionException if shutdown
1226 dl 1.1 */
1227     public void testRejectedExecutionExceptionOnShutdown() {
1228 jsr166 1.21 ThreadPoolExecutor p =
1229 jsr166 1.9 new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1230 jsr166 1.21 try { p.shutdown(); } catch (SecurityException ok) { return; }
1231 jsr166 1.8 try {
1232 jsr166 1.21 p.execute(new NoOpRunnable());
1233 jsr166 1.8 shouldThrow();
1234     } catch (RejectedExecutionException success) {}
1235 jsr166 1.2
1236 jsr166 1.21 joinPool(p);
1237 dl 1.1 }
1238    
1239     /**
1240 jsr166 1.19 * execute using CallerRunsPolicy drops task on shutdown
1241 dl 1.1 */
1242     public void testCallerRunsOnShutdown() {
1243     RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1244 jsr166 1.9 ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1245 dl 1.1
1246 jsr166 1.3 try { p.shutdown(); } catch (SecurityException ok) { return; }
1247 jsr166 1.8 try {
1248 dl 1.1 TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1249 jsr166 1.8 p.execute(r);
1250 dl 1.1 assertFalse(r.done);
1251     } finally {
1252     joinPool(p);
1253     }
1254     }
1255    
1256     /**
1257 jsr166 1.19 * execute using DiscardPolicy drops task on shutdown
1258 dl 1.1 */
1259     public void testDiscardOnShutdown() {
1260     RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1261 jsr166 1.9 ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1262 dl 1.1
1263 jsr166 1.3 try { p.shutdown(); } catch (SecurityException ok) { return; }
1264 jsr166 1.8 try {
1265 dl 1.1 TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1266 jsr166 1.8 p.execute(r);
1267 dl 1.1 assertFalse(r.done);
1268     } finally {
1269     joinPool(p);
1270     }
1271     }
1272    
1273     /**
1274 jsr166 1.19 * execute using DiscardOldestPolicy drops task on shutdown
1275 dl 1.1 */
1276     public void testDiscardOldestOnShutdown() {
1277     RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
1278 jsr166 1.9 ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1279 dl 1.1
1280 jsr166 1.3 try { p.shutdown(); } catch (SecurityException ok) { return; }
1281 jsr166 1.8 try {
1282 dl 1.1 TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1283 jsr166 1.8 p.execute(r);
1284 dl 1.1 assertFalse(r.done);
1285     } finally {
1286     joinPool(p);
1287     }
1288     }
1289    
1290     /**
1291 jsr166 1.18 * execute(null) throws NPE
1292 dl 1.1 */
1293     public void testExecuteNull() {
1294 jsr166 1.37 ThreadPoolExecutor p =
1295     new CustomTPE(1, 2, 1L, SECONDS,
1296     new ArrayBlockingQueue<Runnable>(10));
1297 dl 1.1 try {
1298 jsr166 1.21 p.execute(null);
1299 dl 1.1 shouldThrow();
1300 jsr166 1.8 } catch (NullPointerException success) {}
1301 jsr166 1.2
1302 jsr166 1.21 joinPool(p);
1303 dl 1.1 }
1304 jsr166 1.2
1305 dl 1.1 /**
1306 jsr166 1.19 * setCorePoolSize of negative value throws IllegalArgumentException
1307 dl 1.1 */
1308     public void testCorePoolSizeIllegalArgumentException() {
1309 jsr166 1.21 ThreadPoolExecutor p =
1310 jsr166 1.9 new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1311 jsr166 1.8 try {
1312 jsr166 1.21 p.setCorePoolSize(-1);
1313 jsr166 1.8 shouldThrow();
1314     } catch (IllegalArgumentException success) {
1315 dl 1.1 } finally {
1316 jsr166 1.21 try { p.shutdown(); } catch (SecurityException ok) { return; }
1317 dl 1.1 }
1318 jsr166 1.21 joinPool(p);
1319 jsr166 1.2 }
1320 dl 1.1
1321     /**
1322 jsr166 1.19 * setMaximumPoolSize(int) throws IllegalArgumentException
1323     * if given a value less the core pool size
1324 jsr166 1.2 */
1325 dl 1.1 public void testMaximumPoolSizeIllegalArgumentException() {
1326 jsr166 1.21 ThreadPoolExecutor p =
1327 jsr166 1.10 new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1328 dl 1.1 try {
1329 jsr166 1.21 p.setMaximumPoolSize(1);
1330 dl 1.1 shouldThrow();
1331 jsr166 1.4 } catch (IllegalArgumentException success) {
1332 dl 1.1 } finally {
1333 jsr166 1.21 try { p.shutdown(); } catch (SecurityException ok) { return; }
1334 dl 1.1 }
1335 jsr166 1.21 joinPool(p);
1336 dl 1.1 }
1337 jsr166 1.2
1338 dl 1.1 /**
1339 jsr166 1.19 * setMaximumPoolSize throws IllegalArgumentException
1340     * if given a negative value
1341 dl 1.1 */
1342     public void testMaximumPoolSizeIllegalArgumentException2() {
1343 jsr166 1.21 ThreadPoolExecutor p =
1344 jsr166 1.10 new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1345 dl 1.1 try {
1346 jsr166 1.21 p.setMaximumPoolSize(-1);
1347 dl 1.1 shouldThrow();
1348 jsr166 1.4 } catch (IllegalArgumentException success) {
1349 dl 1.1 } finally {
1350 jsr166 1.21 try { p.shutdown(); } catch (SecurityException ok) { return; }
1351 dl 1.1 }
1352 jsr166 1.21 joinPool(p);
1353 dl 1.1 }
1354 jsr166 1.2
1355 dl 1.1 /**
1356 jsr166 1.19 * setKeepAliveTime throws IllegalArgumentException
1357     * when given a negative value
1358 dl 1.1 */
1359     public void testKeepAliveTimeIllegalArgumentException() {
1360 jsr166 1.21 ThreadPoolExecutor p =
1361 jsr166 1.10 new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1362 jsr166 1.2
1363 jsr166 1.8 try {
1364 jsr166 1.21 p.setKeepAliveTime(-1,MILLISECONDS);
1365 dl 1.1 shouldThrow();
1366 jsr166 1.4 } catch (IllegalArgumentException success) {
1367 dl 1.1 } finally {
1368 jsr166 1.21 try { p.shutdown(); } catch (SecurityException ok) { return; }
1369 dl 1.1 }
1370 jsr166 1.21 joinPool(p);
1371 dl 1.1 }
1372    
1373     /**
1374     * terminated() is called on termination
1375     */
1376     public void testTerminated() {
1377 jsr166 1.21 CustomTPE p = new CustomTPE();
1378     try { p.shutdown(); } catch (SecurityException ok) { return; }
1379 jsr166 1.30 assertTrue(p.terminatedCalled());
1380 jsr166 1.21 joinPool(p);
1381 dl 1.1 }
1382    
1383     /**
1384     * beforeExecute and afterExecute are called when executing task
1385     */
1386 jsr166 1.5 public void testBeforeAfter() throws InterruptedException {
1387 jsr166 1.21 CustomTPE p = new CustomTPE();
1388 dl 1.1 try {
1389 jsr166 1.30 final CountDownLatch done = new CountDownLatch(1);
1390 jsr166 1.32 p.execute(new CheckedRunnable() {
1391 jsr166 1.30 public void realRun() {
1392     done.countDown();
1393 jsr166 1.32 }});
1394 jsr166 1.30 await(p.afterCalled);
1395     assertEquals(0, done.getCount());
1396     assertTrue(p.afterCalled());
1397     assertTrue(p.beforeCalled());
1398 jsr166 1.21 try { p.shutdown(); } catch (SecurityException ok) { return; }
1399 dl 1.1 } finally {
1400 jsr166 1.21 joinPool(p);
1401 dl 1.1 }
1402     }
1403    
1404     /**
1405     * completed submit of callable returns result
1406     */
1407 jsr166 1.5 public void testSubmitCallable() throws Exception {
1408 jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1409 dl 1.1 try {
1410     Future<String> future = e.submit(new StringTask());
1411     String result = future.get();
1412     assertSame(TEST_STRING, result);
1413     } finally {
1414     joinPool(e);
1415     }
1416     }
1417    
1418     /**
1419     * completed submit of runnable returns successfully
1420     */
1421 jsr166 1.5 public void testSubmitRunnable() throws Exception {
1422 jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1423 dl 1.1 try {
1424     Future<?> future = e.submit(new NoOpRunnable());
1425     future.get();
1426     assertTrue(future.isDone());
1427     } finally {
1428     joinPool(e);
1429     }
1430     }
1431    
1432     /**
1433     * completed submit of (runnable, result) returns result
1434     */
1435 jsr166 1.5 public void testSubmitRunnable2() throws Exception {
1436 jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1437 dl 1.1 try {
1438     Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1439     String result = future.get();
1440     assertSame(TEST_STRING, result);
1441     } finally {
1442     joinPool(e);
1443     }
1444     }
1445    
1446     /**
1447     * invokeAny(null) throws NPE
1448     */
1449 jsr166 1.5 public void testInvokeAny1() throws Exception {
1450 jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1451 dl 1.1 try {
1452     e.invokeAny(null);
1453 jsr166 1.5 shouldThrow();
1454 dl 1.1 } catch (NullPointerException success) {
1455     } finally {
1456     joinPool(e);
1457     }
1458     }
1459    
1460     /**
1461     * invokeAny(empty collection) throws IAE
1462     */
1463 jsr166 1.5 public void testInvokeAny2() throws Exception {
1464 jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1465 dl 1.1 try {
1466     e.invokeAny(new ArrayList<Callable<String>>());
1467 jsr166 1.5 shouldThrow();
1468 dl 1.1 } catch (IllegalArgumentException success) {
1469     } finally {
1470     joinPool(e);
1471     }
1472     }
1473    
1474     /**
1475     * invokeAny(c) throws NPE if c has null elements
1476     */
1477 jsr166 1.5 public void testInvokeAny3() throws Exception {
1478 jsr166 1.17 CountDownLatch latch = new CountDownLatch(1);
1479 jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1480 jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1481     l.add(latchAwaitingStringTask(latch));
1482     l.add(null);
1483 dl 1.1 try {
1484     e.invokeAny(l);
1485 jsr166 1.5 shouldThrow();
1486 dl 1.1 } catch (NullPointerException success) {
1487     } finally {
1488 jsr166 1.5 latch.countDown();
1489 dl 1.1 joinPool(e);
1490     }
1491     }
1492    
1493     /**
1494     * invokeAny(c) throws ExecutionException if no task completes
1495     */
1496 jsr166 1.5 public void testInvokeAny4() throws Exception {
1497 jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1498 jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1499     l.add(new NPETask());
1500 dl 1.1 try {
1501     e.invokeAny(l);
1502 jsr166 1.5 shouldThrow();
1503 dl 1.1 } catch (ExecutionException success) {
1504 jsr166 1.13 assertTrue(success.getCause() instanceof NullPointerException);
1505 dl 1.1 } finally {
1506     joinPool(e);
1507     }
1508     }
1509    
1510     /**
1511     * invokeAny(c) returns result of some task
1512     */
1513 jsr166 1.5 public void testInvokeAny5() throws Exception {
1514 jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1515 dl 1.1 try {
1516 jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1517 dl 1.1 l.add(new StringTask());
1518     l.add(new StringTask());
1519     String result = e.invokeAny(l);
1520     assertSame(TEST_STRING, result);
1521     } finally {
1522     joinPool(e);
1523     }
1524     }
1525    
1526     /**
1527     * invokeAll(null) throws NPE
1528     */
1529 jsr166 1.5 public void testInvokeAll1() throws Exception {
1530 jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1531 dl 1.1 try {
1532     e.invokeAll(null);
1533 jsr166 1.5 shouldThrow();
1534 dl 1.1 } catch (NullPointerException success) {
1535     } finally {
1536     joinPool(e);
1537     }
1538     }
1539    
1540     /**
1541     * invokeAll(empty collection) returns empty collection
1542     */
1543 jsr166 1.5 public void testInvokeAll2() throws Exception {
1544 jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1545 dl 1.1 try {
1546     List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1547     assertTrue(r.isEmpty());
1548     } finally {
1549     joinPool(e);
1550     }
1551     }
1552    
1553     /**
1554     * invokeAll(c) throws NPE if c has null elements
1555     */
1556 jsr166 1.5 public void testInvokeAll3() throws Exception {
1557 jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1558 jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1559     l.add(new StringTask());
1560     l.add(null);
1561 dl 1.1 try {
1562     e.invokeAll(l);
1563 jsr166 1.5 shouldThrow();
1564 dl 1.1 } catch (NullPointerException success) {
1565     } finally {
1566     joinPool(e);
1567     }
1568     }
1569    
1570     /**
1571     * get of element of invokeAll(c) throws exception on failed task
1572     */
1573 jsr166 1.5 public void testInvokeAll4() throws Exception {
1574 jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1575 jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1576     l.add(new NPETask());
1577     List<Future<String>> futures = e.invokeAll(l);
1578     assertEquals(1, futures.size());
1579 dl 1.1 try {
1580 jsr166 1.17 futures.get(0).get();
1581 jsr166 1.5 shouldThrow();
1582 jsr166 1.3 } catch (ExecutionException success) {
1583 jsr166 1.15 assertTrue(success.getCause() instanceof NullPointerException);
1584 dl 1.1 } finally {
1585     joinPool(e);
1586     }
1587     }
1588    
1589     /**
1590     * invokeAll(c) returns results of all completed tasks
1591     */
1592 jsr166 1.5 public void testInvokeAll5() throws Exception {
1593 jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1594 dl 1.1 try {
1595 jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1596 dl 1.1 l.add(new StringTask());
1597     l.add(new StringTask());
1598 jsr166 1.17 List<Future<String>> futures = e.invokeAll(l);
1599     assertEquals(2, futures.size());
1600     for (Future<String> future : futures)
1601 jsr166 1.6 assertSame(TEST_STRING, future.get());
1602 dl 1.1 } finally {
1603     joinPool(e);
1604     }
1605     }
1606    
1607     /**
1608     * timed invokeAny(null) throws NPE
1609     */
1610 jsr166 1.5 public void testTimedInvokeAny1() throws Exception {
1611 jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1612 dl 1.1 try {
1613 jsr166 1.9 e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1614 jsr166 1.5 shouldThrow();
1615 dl 1.1 } catch (NullPointerException success) {
1616     } finally {
1617     joinPool(e);
1618     }
1619     }
1620    
1621     /**
1622     * timed invokeAny(,,null) throws NPE
1623     */
1624 jsr166 1.5 public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1625 jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1626 jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1627     l.add(new StringTask());
1628 dl 1.1 try {
1629     e.invokeAny(l, MEDIUM_DELAY_MS, null);
1630 jsr166 1.5 shouldThrow();
1631 dl 1.1 } catch (NullPointerException success) {
1632     } finally {
1633     joinPool(e);
1634     }
1635     }
1636    
1637     /**
1638     * timed invokeAny(empty collection) throws IAE
1639     */
1640 jsr166 1.5 public void testTimedInvokeAny2() throws Exception {
1641 jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1642 dl 1.1 try {
1643 jsr166 1.9 e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1644 jsr166 1.5 shouldThrow();
1645 dl 1.1 } catch (IllegalArgumentException success) {
1646     } finally {
1647     joinPool(e);
1648     }
1649     }
1650    
1651     /**
1652     * timed invokeAny(c) throws NPE if c has null elements
1653     */
1654 jsr166 1.5 public void testTimedInvokeAny3() throws Exception {
1655 jsr166 1.17 CountDownLatch latch = new CountDownLatch(1);
1656 jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1657 jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1658     l.add(latchAwaitingStringTask(latch));
1659     l.add(null);
1660 dl 1.1 try {
1661 jsr166 1.9 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1662 jsr166 1.5 shouldThrow();
1663 dl 1.1 } catch (NullPointerException success) {
1664     } finally {
1665 jsr166 1.12 latch.countDown();
1666 dl 1.1 joinPool(e);
1667     }
1668     }
1669    
1670     /**
1671     * timed invokeAny(c) throws ExecutionException if no task completes
1672     */
1673 jsr166 1.5 public void testTimedInvokeAny4() throws Exception {
1674 jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1675 jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1676     l.add(new NPETask());
1677 dl 1.1 try {
1678 jsr166 1.9 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1679 jsr166 1.5 shouldThrow();
1680 jsr166 1.3 } catch (ExecutionException success) {
1681 jsr166 1.13 assertTrue(success.getCause() instanceof NullPointerException);
1682 dl 1.1 } finally {
1683     joinPool(e);
1684     }
1685     }
1686    
1687     /**
1688     * timed invokeAny(c) returns result of some task
1689     */
1690 jsr166 1.5 public void testTimedInvokeAny5() throws Exception {
1691 jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1692 dl 1.1 try {
1693 jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1694 dl 1.1 l.add(new StringTask());
1695     l.add(new StringTask());
1696 jsr166 1.9 String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1697 dl 1.1 assertSame(TEST_STRING, result);
1698     } finally {
1699     joinPool(e);
1700     }
1701     }
1702    
1703     /**
1704     * timed invokeAll(null) throws NPE
1705     */
1706 jsr166 1.5 public void testTimedInvokeAll1() throws Exception {
1707 jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1708 dl 1.1 try {
1709 jsr166 1.9 e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1710 jsr166 1.5 shouldThrow();
1711 dl 1.1 } catch (NullPointerException success) {
1712     } finally {
1713     joinPool(e);
1714     }
1715     }
1716    
1717     /**
1718     * timed invokeAll(,,null) throws NPE
1719     */
1720 jsr166 1.5 public void testTimedInvokeAllNullTimeUnit() throws Exception {
1721 jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1722 jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1723     l.add(new StringTask());
1724 dl 1.1 try {
1725     e.invokeAll(l, MEDIUM_DELAY_MS, null);
1726 jsr166 1.5 shouldThrow();
1727 dl 1.1 } catch (NullPointerException success) {
1728     } finally {
1729     joinPool(e);
1730     }
1731     }
1732    
1733     /**
1734     * timed invokeAll(empty collection) returns empty collection
1735     */
1736 jsr166 1.5 public void testTimedInvokeAll2() throws Exception {
1737 jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1738 dl 1.1 try {
1739 jsr166 1.9 List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1740 dl 1.1 assertTrue(r.isEmpty());
1741     } finally {
1742     joinPool(e);
1743     }
1744     }
1745    
1746     /**
1747     * timed invokeAll(c) throws NPE if c has null elements
1748     */
1749 jsr166 1.5 public void testTimedInvokeAll3() throws Exception {
1750 jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1751 jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1752     l.add(new StringTask());
1753     l.add(null);
1754 dl 1.1 try {
1755 jsr166 1.9 e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1756 jsr166 1.5 shouldThrow();
1757 dl 1.1 } catch (NullPointerException success) {
1758     } finally {
1759     joinPool(e);
1760     }
1761     }
1762    
1763     /**
1764     * get of element of invokeAll(c) throws exception on failed task
1765     */
1766 jsr166 1.5 public void testTimedInvokeAll4() throws Exception {
1767 jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1768 jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1769     l.add(new NPETask());
1770     List<Future<String>> futures =
1771     e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1772     assertEquals(1, futures.size());
1773 dl 1.1 try {
1774 jsr166 1.17 futures.get(0).get();
1775 jsr166 1.5 shouldThrow();
1776 jsr166 1.3 } catch (ExecutionException success) {
1777 jsr166 1.6 assertTrue(success.getCause() instanceof NullPointerException);
1778 dl 1.1 } finally {
1779     joinPool(e);
1780     }
1781     }
1782    
1783     /**
1784     * timed invokeAll(c) returns results of all completed tasks
1785     */
1786 jsr166 1.5 public void testTimedInvokeAll5() throws Exception {
1787 jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1788 dl 1.1 try {
1789 jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1790 dl 1.1 l.add(new StringTask());
1791     l.add(new StringTask());
1792 jsr166 1.17 List<Future<String>> futures =
1793 jsr166 1.45 e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1794 jsr166 1.17 assertEquals(2, futures.size());
1795     for (Future<String> future : futures)
1796 jsr166 1.6 assertSame(TEST_STRING, future.get());
1797 dl 1.1 } finally {
1798     joinPool(e);
1799     }
1800     }
1801    
1802     /**
1803     * timed invokeAll(c) cancels tasks not completed by timeout
1804     */
1805 jsr166 1.5 public void testTimedInvokeAll6() throws Exception {
1806 jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1807 dl 1.1 try {
1808 jsr166 1.39 for (long timeout = timeoutMillis();;) {
1809     List<Callable<String>> tasks = new ArrayList<>();
1810     tasks.add(new StringTask("0"));
1811     tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1812     tasks.add(new StringTask("2"));
1813     long startTime = System.nanoTime();
1814     List<Future<String>> futures =
1815     e.invokeAll(tasks, timeout, MILLISECONDS);
1816     assertEquals(tasks.size(), futures.size());
1817     assertTrue(millisElapsedSince(startTime) >= timeout);
1818     for (Future future : futures)
1819     assertTrue(future.isDone());
1820     assertTrue(futures.get(1).isCancelled());
1821     try {
1822     assertEquals("0", futures.get(0).get());
1823     assertEquals("2", futures.get(2).get());
1824     break;
1825     } catch (CancellationException retryWithLongerTimeout) {
1826     timeout *= 2;
1827     if (timeout >= LONG_DELAY_MS / 2)
1828     fail("expected exactly one task to be cancelled");
1829     }
1830     }
1831 dl 1.1 } finally {
1832     joinPool(e);
1833     }
1834     }
1835    
1836     /**
1837     * Execution continues if there is at least one thread even if
1838     * thread factory fails to create more
1839     */
1840 jsr166 1.5 public void testFailingThreadFactory() throws InterruptedException {
1841 jsr166 1.21 final ExecutorService e =
1842     new CustomTPE(100, 100,
1843     LONG_DELAY_MS, MILLISECONDS,
1844     new LinkedBlockingQueue<Runnable>(),
1845     new FailingThreadFactory());
1846     try {
1847     final int TASKS = 100;
1848     final CountDownLatch done = new CountDownLatch(TASKS);
1849     for (int k = 0; k < TASKS; ++k)
1850     e.execute(new CheckedRunnable() {
1851     public void realRun() {
1852     done.countDown();
1853     }});
1854     assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1855 dl 1.1 } finally {
1856     joinPool(e);
1857     }
1858     }
1859    
1860     /**
1861     * allowsCoreThreadTimeOut is by default false.
1862     */
1863     public void testAllowsCoreThreadTimeOut() {
1864 jsr166 1.21 ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1865     assertFalse(p.allowsCoreThreadTimeOut());
1866     joinPool(p);
1867 dl 1.1 }
1868    
1869     /**
1870     * allowCoreThreadTimeOut(true) causes idle threads to time out
1871     */
1872 jsr166 1.21 public void testAllowCoreThreadTimeOut_true() throws Exception {
1873 jsr166 1.39 long keepAliveTime = timeoutMillis();
1874 jsr166 1.21 final ThreadPoolExecutor p =
1875     new CustomTPE(2, 10,
1876 jsr166 1.39 keepAliveTime, MILLISECONDS,
1877 jsr166 1.21 new ArrayBlockingQueue<Runnable>(10));
1878     final CountDownLatch threadStarted = new CountDownLatch(1);
1879     try {
1880     p.allowCoreThreadTimeOut(true);
1881     p.execute(new CheckedRunnable() {
1882 jsr166 1.39 public void realRun() {
1883 jsr166 1.21 threadStarted.countDown();
1884     assertEquals(1, p.getPoolSize());
1885     }});
1886 jsr166 1.29 await(threadStarted);
1887 jsr166 1.39 delay(keepAliveTime);
1888 jsr166 1.29 long startTime = System.nanoTime();
1889     while (p.getPoolSize() > 0
1890     && millisElapsedSince(startTime) < LONG_DELAY_MS)
1891     Thread.yield();
1892     assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1893 jsr166 1.21 assertEquals(0, p.getPoolSize());
1894 dl 1.1 } finally {
1895 jsr166 1.21 joinPool(p);
1896 dl 1.1 }
1897     }
1898    
1899     /**
1900     * allowCoreThreadTimeOut(false) causes idle threads not to time out
1901     */
1902 jsr166 1.21 public void testAllowCoreThreadTimeOut_false() throws Exception {
1903 jsr166 1.39 long keepAliveTime = timeoutMillis();
1904 jsr166 1.21 final ThreadPoolExecutor p =
1905     new CustomTPE(2, 10,
1906 jsr166 1.39 keepAliveTime, MILLISECONDS,
1907 jsr166 1.21 new ArrayBlockingQueue<Runnable>(10));
1908     final CountDownLatch threadStarted = new CountDownLatch(1);
1909     try {
1910     p.allowCoreThreadTimeOut(false);
1911     p.execute(new CheckedRunnable() {
1912     public void realRun() throws InterruptedException {
1913     threadStarted.countDown();
1914     assertTrue(p.getPoolSize() >= 1);
1915     }});
1916 jsr166 1.39 delay(2 * keepAliveTime);
1917 jsr166 1.21 assertTrue(p.getPoolSize() >= 1);
1918 dl 1.1 } finally {
1919 jsr166 1.21 joinPool(p);
1920 dl 1.1 }
1921     }
1922    
1923 jsr166 1.44 /**
1924     * get(cancelled task) throws CancellationException
1925     * (in part, a test of CustomTPE itself)
1926     */
1927     public void testGet_cancelled() throws Exception {
1928     final ExecutorService e =
1929     new CustomTPE(1, 1,
1930     LONG_DELAY_MS, MILLISECONDS,
1931     new LinkedBlockingQueue<Runnable>());
1932     try {
1933     final CountDownLatch blockerStarted = new CountDownLatch(1);
1934     final CountDownLatch done = new CountDownLatch(1);
1935     final List<Future<?>> futures = new ArrayList<>();
1936     for (int i = 0; i < 2; i++) {
1937     Runnable r = new CheckedRunnable() { public void realRun()
1938     throws Throwable {
1939     blockerStarted.countDown();
1940     assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
1941     }};
1942     futures.add(e.submit(r));
1943     }
1944     assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
1945     for (Future<?> future : futures) future.cancel(false);
1946     for (Future<?> future : futures) {
1947     try {
1948     future.get();
1949     shouldThrow();
1950     } catch (CancellationException success) {}
1951     try {
1952     future.get(LONG_DELAY_MS, MILLISECONDS);
1953     shouldThrow();
1954     } catch (CancellationException success) {}
1955     assertTrue(future.isCancelled());
1956     assertTrue(future.isDone());
1957     }
1958     done.countDown();
1959     } finally {
1960     joinPool(e);
1961     }
1962     }
1963    
1964 dl 1.1 }