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

File Contents

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