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

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