ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.49
Committed: Wed Sep 25 07:39:17 2013 UTC (10 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.48: +2 -3 lines
Log Message:
cosmetic changes

File Contents

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