ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java
Revision: 1.47
Committed: Sat Oct 3 18:17:51 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.46: +1 -1 lines
Log Message:
compare longs against 0L, not 0

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