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

File Contents

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