ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java
Revision: 1.49
Committed: Sun Oct 4 00:30:50 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.48: +5 -5 lines
Log Message:
rejigger pool closing infrastructure

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