ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java
Revision: 1.56
Committed: Sun Oct 4 01:30:27 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.55: +7 -3 lines
Log Message:
improve testGetKeepAliveTime

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