ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.36
Committed: Sat Oct 9 22:27:16 2010 UTC (13 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.35: +3 -3 lines
Log Message:
typos

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     * http://creativecommons.org/licenses/publicdomain
5 jsr166 1.24 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9     import java.util.concurrent.*;
10 jsr166 1.27 import static java.util.concurrent.TimeUnit.MILLISECONDS;
11 dl 1.23 import java.util.concurrent.atomic.*;
12 dl 1.1 import junit.framework.*;
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     volatile boolean beforeCalled = false;
25     volatile boolean afterCalled = false;
26     volatile boolean terminatedCalled = false;
27     public ExtendedTPE() {
28 jsr166 1.27 super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
29 dl 1.8 }
30     protected void beforeExecute(Thread t, Runnable r) {
31     beforeCalled = true;
32     }
33     protected void afterExecute(Runnable r, Throwable t) {
34     afterCalled = true;
35     }
36     protected void terminated() {
37     terminatedCalled = true;
38     }
39     }
40 dl 1.1
41 jsr166 1.26 static class FailingThreadFactory implements ThreadFactory {
42 dl 1.19 int calls = 0;
43 jsr166 1.26 public Thread newThread(Runnable r) {
44 dl 1.20 if (++calls > 1) return null;
45 dl 1.19 return new Thread(r);
46 jsr166 1.24 }
47 dl 1.19 }
48 jsr166 1.24
49 dl 1.19
50 dl 1.3 /**
51 jsr166 1.35 * execute successfully executes a runnable
52 dl 1.1 */
53 jsr166 1.27 public void testExecute() throws InterruptedException {
54     ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
55 dl 1.1 try {
56 jsr166 1.27 p1.execute(new ShortRunnable());
57 jsr166 1.31 Thread.sleep(SMALL_DELAY_MS);
58 jsr166 1.27 } finally {
59     joinPool(p1);
60 jsr166 1.24 }
61 dl 1.1 }
62    
63     /**
64 jsr166 1.35 * getActiveCount increases but doesn't overestimate, when a
65     * thread becomes active
66 dl 1.1 */
67 jsr166 1.27 public void testGetActiveCount() throws InterruptedException {
68     ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
69 dl 1.5 assertEquals(0, p2.getActiveCount());
70     p2.execute(new MediumRunnable());
71 jsr166 1.27 Thread.sleep(SHORT_DELAY_MS);
72 dl 1.5 assertEquals(1, p2.getActiveCount());
73     joinPool(p2);
74 dl 1.1 }
75 dl 1.8
76     /**
77 jsr166 1.35 * prestartCoreThread starts a thread if under corePoolSize, else doesn't
78 dl 1.8 */
79     public void testPrestartCoreThread() {
80 jsr166 1.27 ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
81 dl 1.8 assertEquals(0, p2.getPoolSize());
82     assertTrue(p2.prestartCoreThread());
83     assertEquals(1, p2.getPoolSize());
84     assertTrue(p2.prestartCoreThread());
85     assertEquals(2, p2.getPoolSize());
86     assertFalse(p2.prestartCoreThread());
87     assertEquals(2, p2.getPoolSize());
88     joinPool(p2);
89     }
90    
91     /**
92 jsr166 1.35 * prestartAllCoreThreads starts all corePoolSize threads
93 dl 1.8 */
94     public void testPrestartAllCoreThreads() {
95 jsr166 1.27 ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
96 dl 1.8 assertEquals(0, p2.getPoolSize());
97     p2.prestartAllCoreThreads();
98     assertEquals(2, p2.getPoolSize());
99     p2.prestartAllCoreThreads();
100     assertEquals(2, p2.getPoolSize());
101     joinPool(p2);
102     }
103 jsr166 1.24
104 dl 1.1 /**
105 jsr166 1.35 * getCompletedTaskCount increases, but doesn't overestimate,
106     * when tasks complete
107 dl 1.1 */
108 jsr166 1.27 public void testGetCompletedTaskCount() throws InterruptedException {
109     ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
110 dl 1.5 assertEquals(0, p2.getCompletedTaskCount());
111     p2.execute(new ShortRunnable());
112 jsr166 1.27 Thread.sleep(SMALL_DELAY_MS);
113 dl 1.5 assertEquals(1, p2.getCompletedTaskCount());
114 jsr166 1.25 try { p2.shutdown(); } catch (SecurityException ok) { return; }
115 dl 1.5 joinPool(p2);
116 dl 1.1 }
117 jsr166 1.24
118 dl 1.1 /**
119 jsr166 1.35 * getCorePoolSize returns size given in constructor if not otherwise set
120 dl 1.1 */
121 dl 1.5 public void testGetCorePoolSize() {
122 jsr166 1.27 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
123 dl 1.5 assertEquals(1, p1.getCorePoolSize());
124     joinPool(p1);
125 dl 1.1 }
126 jsr166 1.24
127 dl 1.1 /**
128 jsr166 1.35 * getKeepAliveTime returns value given in constructor if not otherwise set
129 dl 1.1 */
130 dl 1.5 public void testGetKeepAliveTime() {
131 jsr166 1.27 ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
132 dl 1.5 assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS));
133     joinPool(p2);
134 dl 1.1 }
135 dl 1.8
136    
137 jsr166 1.24 /**
138 dl 1.8 * getThreadFactory returns factory in constructor if not set
139     */
140     public void testGetThreadFactory() {
141     ThreadFactory tf = new SimpleThreadFactory();
142 jsr166 1.27 ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
143 dl 1.8 assertSame(tf, p.getThreadFactory());
144     joinPool(p);
145     }
146    
147 jsr166 1.24 /**
148 dl 1.8 * setThreadFactory sets the thread factory returned by getThreadFactory
149     */
150     public void testSetThreadFactory() {
151 jsr166 1.27 ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
152 dl 1.8 ThreadFactory tf = new SimpleThreadFactory();
153     p.setThreadFactory(tf);
154     assertSame(tf, p.getThreadFactory());
155     joinPool(p);
156     }
157    
158    
159 jsr166 1.24 /**
160 dl 1.8 * setThreadFactory(null) throws NPE
161     */
162     public void testSetThreadFactoryNull() {
163 jsr166 1.27 ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
164 dl 1.8 try {
165     p.setThreadFactory(null);
166     shouldThrow();
167     } catch (NullPointerException success) {
168     } finally {
169     joinPool(p);
170     }
171     }
172    
173 jsr166 1.24 /**
174 dl 1.9 * getRejectedExecutionHandler returns handler in constructor if not set
175     */
176     public void testGetRejectedExecutionHandler() {
177     RejectedExecutionHandler h = new NoOpREHandler();
178 jsr166 1.27 ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
179 dl 1.9 assertSame(h, p.getRejectedExecutionHandler());
180     joinPool(p);
181     }
182    
183 jsr166 1.24 /**
184 dl 1.9 * setRejectedExecutionHandler sets the handler returned by
185     * getRejectedExecutionHandler
186     */
187     public void testSetRejectedExecutionHandler() {
188 jsr166 1.27 ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
189 dl 1.9 RejectedExecutionHandler h = new NoOpREHandler();
190     p.setRejectedExecutionHandler(h);
191     assertSame(h, p.getRejectedExecutionHandler());
192     joinPool(p);
193     }
194    
195    
196 jsr166 1.24 /**
197 dl 1.9 * setRejectedExecutionHandler(null) throws NPE
198     */
199     public void testSetRejectedExecutionHandlerNull() {
200 jsr166 1.27 ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
201 dl 1.9 try {
202     p.setRejectedExecutionHandler(null);
203     shouldThrow();
204     } catch (NullPointerException success) {
205     } finally {
206     joinPool(p);
207     }
208     }
209    
210 jsr166 1.24
211 dl 1.1 /**
212 jsr166 1.35 * getLargestPoolSize increases, but doesn't overestimate, when
213     * multiple threads active
214 dl 1.1 */
215 jsr166 1.27 public void testGetLargestPoolSize() throws InterruptedException {
216     ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
217     assertEquals(0, p2.getLargestPoolSize());
218     p2.execute(new MediumRunnable());
219     p2.execute(new MediumRunnable());
220     Thread.sleep(SHORT_DELAY_MS);
221     assertEquals(2, p2.getLargestPoolSize());
222 dl 1.5 joinPool(p2);
223 dl 1.1 }
224 jsr166 1.24
225 dl 1.1 /**
226 jsr166 1.35 * getMaximumPoolSize returns value given in constructor if not
227     * otherwise set
228 dl 1.1 */
229 dl 1.5 public void testGetMaximumPoolSize() {
230 jsr166 1.27 ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
231 dl 1.5 assertEquals(2, p2.getMaximumPoolSize());
232     joinPool(p2);
233 dl 1.1 }
234 jsr166 1.24
235 dl 1.1 /**
236 jsr166 1.35 * getPoolSize increases, but doesn't overestimate, when threads
237     * become active
238 dl 1.1 */
239 dl 1.5 public void testGetPoolSize() {
240 jsr166 1.27 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
241 dl 1.5 assertEquals(0, p1.getPoolSize());
242     p1.execute(new MediumRunnable());
243     assertEquals(1, p1.getPoolSize());
244     joinPool(p1);
245 dl 1.1 }
246 jsr166 1.24
247 dl 1.1 /**
248 jsr166 1.35 * getTaskCount increases, but doesn't overestimate, when tasks submitted
249 dl 1.1 */
250 jsr166 1.27 public void testGetTaskCount() throws InterruptedException {
251     ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
252     assertEquals(0, p1.getTaskCount());
253     p1.execute(new MediumRunnable());
254     Thread.sleep(SHORT_DELAY_MS);
255     assertEquals(1, p1.getTaskCount());
256 dl 1.5 joinPool(p1);
257 dl 1.1 }
258 jsr166 1.24
259 dl 1.1 /**
260 jsr166 1.35 * isShutDown is false before shutdown, true after
261 dl 1.1 */
262 dl 1.5 public void testIsShutdown() {
263 jsr166 1.24
264 jsr166 1.31 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
265 dl 1.5 assertFalse(p1.isShutdown());
266 jsr166 1.25 try { p1.shutdown(); } catch (SecurityException ok) { return; }
267 jsr166 1.31 assertTrue(p1.isShutdown());
268 dl 1.5 joinPool(p1);
269 dl 1.1 }
270    
271 jsr166 1.24
272 dl 1.1 /**
273 jsr166 1.35 * isTerminated is false before termination, true after
274 dl 1.1 */
275 jsr166 1.27 public void testIsTerminated() throws InterruptedException {
276 jsr166 1.31 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
277 dl 1.6 assertFalse(p1.isTerminated());
278 dl 1.1 try {
279 dl 1.5 p1.execute(new MediumRunnable());
280 dl 1.1 } finally {
281 jsr166 1.25 try { p1.shutdown(); } catch (SecurityException ok) { return; }
282 dl 1.1 }
283 jsr166 1.27 assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
284     assertTrue(p1.isTerminated());
285 dl 1.5 }
286    
287     /**
288 jsr166 1.35 * isTerminating is not true when running or when terminated
289 dl 1.5 */
290 jsr166 1.27 public void testIsTerminating() throws InterruptedException {
291 jsr166 1.31 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
292 dl 1.5 assertFalse(p1.isTerminating());
293     try {
294     p1.execute(new SmallRunnable());
295     assertFalse(p1.isTerminating());
296     } finally {
297 jsr166 1.25 try { p1.shutdown(); } catch (SecurityException ok) { return; }
298 dl 1.5 }
299 jsr166 1.27 assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
300     assertTrue(p1.isTerminated());
301     assertFalse(p1.isTerminating());
302 dl 1.1 }
303    
304     /**
305 dl 1.8 * getQueue returns the work queue, which contains queued tasks
306     */
307 jsr166 1.27 public void testGetQueue() throws InterruptedException {
308 dl 1.8 BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
309 jsr166 1.27 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
310 dl 1.11 FutureTask[] tasks = new FutureTask[5];
311 jsr166 1.26 for (int i = 0; i < 5; i++) {
312 dl 1.11 tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
313 dl 1.8 p1.execute(tasks[i]);
314     }
315     try {
316     Thread.sleep(SHORT_DELAY_MS);
317     BlockingQueue<Runnable> wq = p1.getQueue();
318     assertSame(q, wq);
319     assertFalse(wq.contains(tasks[0]));
320     assertTrue(wq.contains(tasks[4]));
321 dl 1.22 for (int i = 1; i < 5; ++i)
322     tasks[i].cancel(true);
323     p1.shutdownNow();
324 dl 1.8 } finally {
325     joinPool(p1);
326     }
327     }
328    
329     /**
330     * remove(task) removes queued task, and fails to remove active task
331     */
332 jsr166 1.27 public void testRemove() throws InterruptedException {
333 dl 1.8 BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
334 jsr166 1.27 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
335 dl 1.11 FutureTask[] tasks = new FutureTask[5];
336 jsr166 1.26 for (int i = 0; i < 5; i++) {
337 dl 1.11 tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
338 dl 1.8 p1.execute(tasks[i]);
339     }
340     try {
341     Thread.sleep(SHORT_DELAY_MS);
342     assertFalse(p1.remove(tasks[0]));
343     assertTrue(q.contains(tasks[4]));
344     assertTrue(q.contains(tasks[3]));
345     assertTrue(p1.remove(tasks[4]));
346     assertFalse(p1.remove(tasks[4]));
347     assertFalse(q.contains(tasks[4]));
348     assertTrue(q.contains(tasks[3]));
349     assertTrue(p1.remove(tasks[3]));
350     assertFalse(q.contains(tasks[3]));
351     } finally {
352     joinPool(p1);
353     }
354     }
355    
356     /**
357 jsr166 1.35 * purge removes cancelled tasks from the queue
358 dl 1.1 */
359 dl 1.5 public void testPurge() {
360 jsr166 1.27 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
361 dl 1.11 FutureTask[] tasks = new FutureTask[5];
362 jsr166 1.26 for (int i = 0; i < 5; i++) {
363 dl 1.11 tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
364 dl 1.5 p1.execute(tasks[i]);
365 dl 1.3 }
366     tasks[4].cancel(true);
367     tasks[3].cancel(true);
368 dl 1.5 p1.purge();
369     long count = p1.getTaskCount();
370 dl 1.3 assertTrue(count >= 2 && count < 5);
371 dl 1.5 joinPool(p1);
372 dl 1.1 }
373    
374     /**
375 jsr166 1.35 * shutDownNow returns a list containing tasks that were not run
376 dl 1.1 */
377 dl 1.5 public void testShutDownNow() {
378 jsr166 1.31 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
379 dl 1.1 List l;
380     try {
381 jsr166 1.25 for (int i = 0; i < 5; i++)
382 dl 1.5 p1.execute(new MediumPossiblyInterruptedRunnable());
383 dl 1.1 }
384     finally {
385 dl 1.17 try {
386     l = p1.shutdownNow();
387     } catch (SecurityException ok) { return; }
388 dl 1.1 }
389 jsr166 1.31 assertTrue(p1.isShutdown());
390     assertTrue(l.size() <= 4);
391 dl 1.1 }
392    
393     // Exception Tests
394    
395 jsr166 1.24
396     /**
397     * Constructor throws if corePoolSize argument is less than zero
398 dl 1.6 */
399 dl 1.1 public void testConstructor1() {
400 dl 1.5 try {
401 jsr166 1.27 new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
402 dl 1.5 shouldThrow();
403 jsr166 1.28 } catch (IllegalArgumentException success) {}
404 dl 1.1 }
405 jsr166 1.24
406     /**
407     * Constructor throws if maximumPoolSize is less than zero
408 dl 1.6 */
409 dl 1.1 public void testConstructor2() {
410 dl 1.5 try {
411 jsr166 1.27 new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
412 dl 1.5 shouldThrow();
413 jsr166 1.28 } catch (IllegalArgumentException success) {}
414 dl 1.1 }
415 jsr166 1.24
416     /**
417     * Constructor throws if maximumPoolSize is equal to zero
418 dl 1.6 */
419 dl 1.1 public void testConstructor3() {
420 dl 1.5 try {
421 jsr166 1.27 new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
422 dl 1.5 shouldThrow();
423 jsr166 1.28 } catch (IllegalArgumentException success) {}
424 dl 1.1 }
425    
426 jsr166 1.24 /**
427     * Constructor throws if keepAliveTime is less than zero
428 dl 1.6 */
429 dl 1.1 public void testConstructor4() {
430 dl 1.5 try {
431 jsr166 1.27 new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
432 dl 1.5 shouldThrow();
433 jsr166 1.28 } catch (IllegalArgumentException success) {}
434 dl 1.1 }
435    
436 jsr166 1.24 /**
437     * Constructor throws if corePoolSize is greater than the maximumPoolSize
438 dl 1.6 */
439 dl 1.1 public void testConstructor5() {
440 dl 1.5 try {
441 jsr166 1.27 new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
442 dl 1.5 shouldThrow();
443 jsr166 1.28 } catch (IllegalArgumentException success) {}
444 dl 1.1 }
445 jsr166 1.24
446     /**
447     * Constructor throws if workQueue is set to null
448 dl 1.6 */
449 dl 1.8 public void testConstructorNullPointerException() {
450 dl 1.5 try {
451 jsr166 1.27 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null);
452 dl 1.5 shouldThrow();
453 jsr166 1.28 } catch (NullPointerException success) {}
454 dl 1.1 }
455    
456 jsr166 1.24
457    
458     /**
459     * Constructor throws if corePoolSize argument is less than zero
460 dl 1.6 */
461 dl 1.1 public void testConstructor6() {
462 dl 1.5 try {
463 jsr166 1.27 new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
464 dl 1.5 shouldThrow();
465 jsr166 1.28 } catch (IllegalArgumentException success) {}
466 dl 1.1 }
467 jsr166 1.24
468     /**
469     * Constructor throws if maximumPoolSize is less than zero
470 dl 1.6 */
471 dl 1.1 public void testConstructor7() {
472 dl 1.5 try {
473 jsr166 1.27 new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
474 dl 1.5 shouldThrow();
475 jsr166 1.28 } catch (IllegalArgumentException success) {}
476 dl 1.1 }
477    
478 jsr166 1.24 /**
479     * Constructor throws if maximumPoolSize is equal to zero
480 dl 1.6 */
481 dl 1.1 public void testConstructor8() {
482 dl 1.5 try {
483 jsr166 1.27 new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
484 dl 1.5 shouldThrow();
485 jsr166 1.28 } catch (IllegalArgumentException success) {}
486 dl 1.1 }
487    
488 jsr166 1.24 /**
489     * Constructor throws if keepAliveTime is less than zero
490 dl 1.6 */
491 dl 1.1 public void testConstructor9() {
492 dl 1.5 try {
493 jsr166 1.27 new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
494 dl 1.5 shouldThrow();
495 jsr166 1.28 } catch (IllegalArgumentException success) {}
496 dl 1.1 }
497    
498 jsr166 1.24 /**
499     * Constructor throws if corePoolSize is greater than the maximumPoolSize
500 dl 1.6 */
501 dl 1.1 public void testConstructor10() {
502 dl 1.5 try {
503 jsr166 1.27 new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
504 dl 1.5 shouldThrow();
505 jsr166 1.28 } catch (IllegalArgumentException success) {}
506 dl 1.1 }
507    
508 jsr166 1.24 /**
509     * Constructor throws if workQueue is set to null
510 dl 1.6 */
511 dl 1.8 public void testConstructorNullPointerException2() {
512 dl 1.5 try {
513 jsr166 1.27 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
514 dl 1.5 shouldThrow();
515 jsr166 1.28 } catch (NullPointerException success) {}
516 dl 1.1 }
517    
518 jsr166 1.24 /**
519     * Constructor throws if threadFactory is set to null
520 dl 1.6 */
521 dl 1.8 public void testConstructorNullPointerException3() {
522 dl 1.5 try {
523 dl 1.1 ThreadFactory f = null;
524 jsr166 1.27 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
525 dl 1.5 shouldThrow();
526 jsr166 1.28 } catch (NullPointerException success) {}
527 dl 1.1 }
528 jsr166 1.24
529    
530     /**
531     * Constructor throws if corePoolSize argument is less than zero
532 dl 1.6 */
533 dl 1.1 public void testConstructor11() {
534 dl 1.5 try {
535 jsr166 1.27 new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
536 dl 1.5 shouldThrow();
537 jsr166 1.28 } catch (IllegalArgumentException success) {}
538 dl 1.1 }
539    
540 jsr166 1.24 /**
541     * Constructor throws if maximumPoolSize is less than zero
542 dl 1.6 */
543 dl 1.1 public void testConstructor12() {
544 dl 1.5 try {
545 jsr166 1.27 new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
546 dl 1.5 shouldThrow();
547 jsr166 1.28 } catch (IllegalArgumentException success) {}
548 dl 1.1 }
549    
550 jsr166 1.24 /**
551     * Constructor throws if maximumPoolSize is equal to zero
552 dl 1.6 */
553 dl 1.1 public void testConstructor13() {
554 dl 1.5 try {
555 jsr166 1.27 new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
556 dl 1.5 shouldThrow();
557 jsr166 1.28 } catch (IllegalArgumentException success) {}
558 dl 1.1 }
559    
560 jsr166 1.24 /**
561     * Constructor throws if keepAliveTime is less than zero
562 dl 1.6 */
563 dl 1.1 public void testConstructor14() {
564 dl 1.5 try {
565 jsr166 1.27 new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
566 dl 1.5 shouldThrow();
567 jsr166 1.28 } catch (IllegalArgumentException success) {}
568 dl 1.1 }
569    
570 jsr166 1.24 /**
571     * Constructor throws if corePoolSize is greater than the maximumPoolSize
572 dl 1.6 */
573 dl 1.1 public void testConstructor15() {
574 dl 1.5 try {
575 jsr166 1.27 new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
576 dl 1.5 shouldThrow();
577 jsr166 1.28 } catch (IllegalArgumentException success) {}
578 dl 1.1 }
579    
580 jsr166 1.24 /**
581     * Constructor throws if workQueue is set to null
582 dl 1.6 */
583 dl 1.8 public void testConstructorNullPointerException4() {
584 dl 1.5 try {
585 jsr166 1.27 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
586 dl 1.5 shouldThrow();
587 jsr166 1.28 } catch (NullPointerException success) {}
588 dl 1.1 }
589    
590 jsr166 1.24 /**
591     * Constructor throws if handler is set to null
592 dl 1.6 */
593 dl 1.8 public void testConstructorNullPointerException5() {
594 dl 1.5 try {
595 dl 1.1 RejectedExecutionHandler r = null;
596 jsr166 1.27 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
597 dl 1.5 shouldThrow();
598 jsr166 1.28 } catch (NullPointerException success) {}
599 dl 1.1 }
600    
601 jsr166 1.24
602     /**
603     * Constructor throws if corePoolSize argument is less than zero
604 dl 1.6 */
605 dl 1.1 public void testConstructor16() {
606 dl 1.5 try {
607 jsr166 1.27 new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
608 dl 1.5 shouldThrow();
609 jsr166 1.28 } catch (IllegalArgumentException success) {}
610 dl 1.1 }
611    
612 jsr166 1.24 /**
613     * Constructor throws if maximumPoolSize is less than zero
614 dl 1.6 */
615 dl 1.1 public void testConstructor17() {
616 dl 1.5 try {
617 jsr166 1.27 new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
618 dl 1.5 shouldThrow();
619 jsr166 1.28 } catch (IllegalArgumentException success) {}
620 dl 1.1 }
621    
622 jsr166 1.24 /**
623     * Constructor throws if maximumPoolSize is equal to zero
624 dl 1.6 */
625 dl 1.1 public void testConstructor18() {
626 dl 1.5 try {
627 jsr166 1.27 new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
628 dl 1.5 shouldThrow();
629 jsr166 1.28 } catch (IllegalArgumentException success) {}
630 dl 1.1 }
631    
632 jsr166 1.24 /**
633     * Constructor throws if keepAliveTime is less than zero
634 dl 1.6 */
635 dl 1.1 public void testConstructor19() {
636 dl 1.5 try {
637 jsr166 1.27 new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
638 dl 1.5 shouldThrow();
639 jsr166 1.28 } catch (IllegalArgumentException success) {}
640 dl 1.1 }
641    
642 jsr166 1.24 /**
643     * Constructor throws if corePoolSize is greater than the maximumPoolSize
644 dl 1.6 */
645 dl 1.1 public void testConstructor20() {
646 dl 1.5 try {
647 jsr166 1.27 new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
648 dl 1.5 shouldThrow();
649 jsr166 1.28 } catch (IllegalArgumentException success) {}
650 dl 1.1 }
651    
652 jsr166 1.24 /**
653 jsr166 1.36 * Constructor throws if workQueue is null
654 dl 1.6 */
655 dl 1.8 public void testConstructorNullPointerException6() {
656 dl 1.5 try {
657 jsr166 1.27 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
658 dl 1.5 shouldThrow();
659 jsr166 1.28 } catch (NullPointerException success) {}
660 dl 1.1 }
661    
662 jsr166 1.24 /**
663 jsr166 1.36 * Constructor throws if handler is null
664 dl 1.6 */
665 dl 1.8 public void testConstructorNullPointerException7() {
666 dl 1.5 try {
667 dl 1.1 RejectedExecutionHandler r = null;
668 jsr166 1.27 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
669 dl 1.5 shouldThrow();
670 jsr166 1.28 } catch (NullPointerException success) {}
671 dl 1.1 }
672    
673 jsr166 1.24 /**
674 jsr166 1.36 * Constructor throws if ThreadFactory is null
675 dl 1.6 */
676 dl 1.8 public void testConstructorNullPointerException8() {
677 dl 1.5 try {
678 dl 1.1 ThreadFactory f = null;
679 jsr166 1.27 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
680 dl 1.5 shouldThrow();
681 jsr166 1.28 } catch (NullPointerException success) {}
682 dl 1.1 }
683 jsr166 1.24
684 dl 1.1
685     /**
686 jsr166 1.35 * execute throws RejectedExecutionException if saturated.
687 dl 1.8 */
688     public void testSaturatedExecute() {
689 jsr166 1.27 ThreadPoolExecutor p =
690     new ThreadPoolExecutor(1, 1,
691     LONG_DELAY_MS, MILLISECONDS,
692     new ArrayBlockingQueue<Runnable>(1));
693 dl 1.8 try {
694 jsr166 1.27 for (int i = 0; i < 2; ++i)
695 dl 1.8 p.execute(new MediumRunnable());
696 jsr166 1.27 for (int i = 0; i < 2; ++i) {
697     try {
698     p.execute(new MediumRunnable());
699     shouldThrow();
700     } catch (RejectedExecutionException success) {}
701 dl 1.8 }
702 jsr166 1.27 } finally {
703     joinPool(p);
704     }
705 dl 1.8 }
706    
707     /**
708 jsr166 1.35 * executor using CallerRunsPolicy runs task if saturated.
709 dl 1.8 */
710     public void testSaturatedExecute2() {
711     RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
712 jsr166 1.27 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
713 dl 1.8 try {
714 jsr166 1.24
715 dl 1.8 TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
716 jsr166 1.26 for (int i = 0; i < 5; ++i) {
717 dl 1.8 tasks[i] = new TrackedNoOpRunnable();
718     }
719     TrackedLongRunnable mr = new TrackedLongRunnable();
720     p.execute(mr);
721 jsr166 1.26 for (int i = 0; i < 5; ++i) {
722 dl 1.8 p.execute(tasks[i]);
723     }
724 jsr166 1.25 for (int i = 1; i < 5; ++i) {
725 dl 1.8 assertTrue(tasks[i].done);
726     }
727 jsr166 1.25 try { p.shutdownNow(); } catch (SecurityException ok) { return; }
728 dl 1.8 } finally {
729     joinPool(p);
730     }
731     }
732    
733     /**
734 jsr166 1.35 * executor using DiscardPolicy drops task if saturated.
735 dl 1.8 */
736     public void testSaturatedExecute3() {
737     RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
738 jsr166 1.27 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
739 dl 1.8 try {
740 jsr166 1.24
741 dl 1.8 TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
742 jsr166 1.26 for (int i = 0; i < 5; ++i) {
743 dl 1.8 tasks[i] = new TrackedNoOpRunnable();
744     }
745     p.execute(new TrackedLongRunnable());
746 jsr166 1.26 for (int i = 0; i < 5; ++i) {
747 dl 1.8 p.execute(tasks[i]);
748     }
749 jsr166 1.26 for (int i = 0; i < 5; ++i) {
750 dl 1.8 assertFalse(tasks[i].done);
751     }
752 jsr166 1.25 try { p.shutdownNow(); } catch (SecurityException ok) { return; }
753 dl 1.8 } finally {
754     joinPool(p);
755     }
756     }
757    
758     /**
759 jsr166 1.35 * executor using DiscardOldestPolicy drops oldest task if saturated.
760 dl 1.8 */
761     public void testSaturatedExecute4() {
762     RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
763 jsr166 1.27 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
764 dl 1.8 try {
765     p.execute(new TrackedLongRunnable());
766     TrackedLongRunnable r2 = new TrackedLongRunnable();
767     p.execute(r2);
768     assertTrue(p.getQueue().contains(r2));
769     TrackedNoOpRunnable r3 = new TrackedNoOpRunnable();
770     p.execute(r3);
771     assertFalse(p.getQueue().contains(r2));
772     assertTrue(p.getQueue().contains(r3));
773 jsr166 1.25 try { p.shutdownNow(); } catch (SecurityException ok) { return; }
774 dl 1.8 } finally {
775     joinPool(p);
776     }
777     }
778    
779     /**
780 jsr166 1.35 * execute throws RejectedExecutionException if shutdown
781 dl 1.1 */
782 dl 1.8 public void testRejectedExecutionExceptionOnShutdown() {
783 jsr166 1.24 ThreadPoolExecutor tpe =
784 jsr166 1.27 new ThreadPoolExecutor(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
785 jsr166 1.25 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
786 jsr166 1.31 try {
787     tpe.execute(new NoOpRunnable());
788     shouldThrow();
789     } catch (RejectedExecutionException success) {}
790 jsr166 1.24
791 jsr166 1.31 joinPool(tpe);
792 dl 1.1 }
793 dl 1.6
794     /**
795 jsr166 1.35 * execute using CallerRunsPolicy drops task on shutdown
796 dl 1.8 */
797     public void testCallerRunsOnShutdown() {
798     RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
799 jsr166 1.27 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
800 dl 1.8
801 jsr166 1.25 try { p.shutdown(); } catch (SecurityException ok) { return; }
802 jsr166 1.31 try {
803 dl 1.8 TrackedNoOpRunnable r = new TrackedNoOpRunnable();
804 jsr166 1.31 p.execute(r);
805 dl 1.8 assertFalse(r.done);
806     } finally {
807     joinPool(p);
808     }
809     }
810    
811     /**
812 jsr166 1.35 * execute using DiscardPolicy drops task on shutdown
813 dl 1.8 */
814     public void testDiscardOnShutdown() {
815     RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
816 jsr166 1.27 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
817 dl 1.8
818 jsr166 1.25 try { p.shutdown(); } catch (SecurityException ok) { return; }
819 jsr166 1.31 try {
820 dl 1.8 TrackedNoOpRunnable r = new TrackedNoOpRunnable();
821 jsr166 1.31 p.execute(r);
822 dl 1.8 assertFalse(r.done);
823     } finally {
824     joinPool(p);
825     }
826     }
827    
828    
829     /**
830 jsr166 1.35 * execute using DiscardOldestPolicy drops task on shutdown
831 dl 1.6 */
832 dl 1.8 public void testDiscardOldestOnShutdown() {
833     RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
834 jsr166 1.27 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
835 dl 1.8
836 jsr166 1.25 try { p.shutdown(); } catch (SecurityException ok) { return; }
837 jsr166 1.31 try {
838 dl 1.8 TrackedNoOpRunnable r = new TrackedNoOpRunnable();
839 jsr166 1.31 p.execute(r);
840 dl 1.8 assertFalse(r.done);
841     } finally {
842     joinPool(p);
843     }
844 dl 1.6 }
845    
846 dl 1.8
847 dl 1.6 /**
848 jsr166 1.34 * execute(null) throws NPE
849 dl 1.6 */
850     public void testExecuteNull() {
851 jsr166 1.28 ThreadPoolExecutor tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
852 dl 1.6 try {
853 jsr166 1.31 tpe.execute(null);
854 dl 1.6 shouldThrow();
855 jsr166 1.31 } catch (NullPointerException success) {}
856 jsr166 1.24
857 jsr166 1.31 joinPool(tpe);
858 dl 1.6 }
859 jsr166 1.24
860 dl 1.1 /**
861 jsr166 1.34 * setCorePoolSize of negative value throws IllegalArgumentException
862 dl 1.1 */
863 dl 1.5 public void testCorePoolSizeIllegalArgumentException() {
864 jsr166 1.31 ThreadPoolExecutor tpe =
865 jsr166 1.27 new ThreadPoolExecutor(1, 2,
866     LONG_DELAY_MS, MILLISECONDS,
867     new ArrayBlockingQueue<Runnable>(10));
868 jsr166 1.31 try {
869     tpe.setCorePoolSize(-1);
870     shouldThrow();
871     } catch (IllegalArgumentException success) {
872 dl 1.1 } finally {
873 jsr166 1.25 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
874 dl 1.1 }
875 dl 1.3 joinPool(tpe);
876 jsr166 1.24 }
877 dl 1.1
878     /**
879 jsr166 1.35 * setMaximumPoolSize(int) throws IllegalArgumentException if
880     * given a value less the core pool size
881 jsr166 1.24 */
882 dl 1.5 public void testMaximumPoolSizeIllegalArgumentException() {
883 jsr166 1.27 ThreadPoolExecutor tpe =
884     new ThreadPoolExecutor(2, 3,
885     LONG_DELAY_MS, MILLISECONDS,
886     new ArrayBlockingQueue<Runnable>(10));
887 dl 1.5 try {
888 dl 1.1 tpe.setMaximumPoolSize(1);
889 dl 1.5 shouldThrow();
890 jsr166 1.26 } catch (IllegalArgumentException success) {
891 dl 1.1 } finally {
892 jsr166 1.25 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
893 dl 1.1 }
894 dl 1.3 joinPool(tpe);
895 dl 1.1 }
896 jsr166 1.24
897 dl 1.1 /**
898 jsr166 1.35 * setMaximumPoolSize throws IllegalArgumentException
899     * if given a negative value
900 dl 1.1 */
901 dl 1.5 public void testMaximumPoolSizeIllegalArgumentException2() {
902 jsr166 1.27 ThreadPoolExecutor tpe =
903     new ThreadPoolExecutor(2, 3,
904     LONG_DELAY_MS, MILLISECONDS,
905     new ArrayBlockingQueue<Runnable>(10));
906 dl 1.5 try {
907 dl 1.1 tpe.setMaximumPoolSize(-1);
908 dl 1.5 shouldThrow();
909 jsr166 1.26 } catch (IllegalArgumentException success) {
910 dl 1.1 } finally {
911 jsr166 1.25 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
912 dl 1.1 }
913 dl 1.3 joinPool(tpe);
914 dl 1.1 }
915 jsr166 1.24
916 dl 1.1
917     /**
918 jsr166 1.35 * setKeepAliveTime throws IllegalArgumentException
919     * when given a negative value
920 dl 1.1 */
921 dl 1.5 public void testKeepAliveTimeIllegalArgumentException() {
922 jsr166 1.31 ThreadPoolExecutor tpe =
923 jsr166 1.27 new ThreadPoolExecutor(2, 3,
924     LONG_DELAY_MS, MILLISECONDS,
925     new ArrayBlockingQueue<Runnable>(10));
926 jsr166 1.31 try {
927 jsr166 1.27 tpe.setKeepAliveTime(-1,MILLISECONDS);
928 dl 1.5 shouldThrow();
929 jsr166 1.26 } catch (IllegalArgumentException success) {
930 dl 1.1 } finally {
931 jsr166 1.25 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
932 dl 1.1 }
933 dl 1.3 joinPool(tpe);
934 dl 1.1 }
935 dl 1.8
936     /**
937     * terminated() is called on termination
938     */
939     public void testTerminated() {
940     ExtendedTPE tpe = new ExtendedTPE();
941 jsr166 1.25 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
942 dl 1.8 assertTrue(tpe.terminatedCalled);
943     joinPool(tpe);
944     }
945    
946     /**
947     * beforeExecute and afterExecute are called when executing task
948     */
949 jsr166 1.27 public void testBeforeAfter() throws InterruptedException {
950 dl 1.8 ExtendedTPE tpe = new ExtendedTPE();
951     try {
952     TrackedNoOpRunnable r = new TrackedNoOpRunnable();
953     tpe.execute(r);
954     Thread.sleep(SHORT_DELAY_MS);
955     assertTrue(r.done);
956     assertTrue(tpe.beforeCalled);
957     assertTrue(tpe.afterCalled);
958 jsr166 1.25 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
959 dl 1.8 } finally {
960     joinPool(tpe);
961     }
962     }
963 dl 1.12
964     /**
965     * completed submit of callable returns result
966     */
967 jsr166 1.27 public void testSubmitCallable() throws Exception {
968     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
969 dl 1.12 try {
970     Future<String> future = e.submit(new StringTask());
971     String result = future.get();
972     assertSame(TEST_STRING, result);
973     } finally {
974     joinPool(e);
975     }
976     }
977    
978     /**
979     * completed submit of runnable returns successfully
980     */
981 jsr166 1.27 public void testSubmitRunnable() throws Exception {
982     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
983 dl 1.12 try {
984     Future<?> future = e.submit(new NoOpRunnable());
985     future.get();
986     assertTrue(future.isDone());
987     } finally {
988     joinPool(e);
989     }
990     }
991    
992     /**
993     * completed submit of (runnable, result) returns result
994     */
995 jsr166 1.27 public void testSubmitRunnable2() throws Exception {
996     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
997 dl 1.12 try {
998     Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
999     String result = future.get();
1000     assertSame(TEST_STRING, result);
1001     } finally {
1002     joinPool(e);
1003     }
1004     }
1005    
1006    
1007     /**
1008     * invokeAny(null) throws NPE
1009     */
1010 jsr166 1.27 public void testInvokeAny1() throws Exception {
1011     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1012 dl 1.12 try {
1013     e.invokeAny(null);
1014 jsr166 1.27 shouldThrow();
1015 dl 1.12 } catch (NullPointerException success) {
1016     } finally {
1017     joinPool(e);
1018     }
1019     }
1020    
1021     /**
1022     * invokeAny(empty collection) throws IAE
1023     */
1024 jsr166 1.27 public void testInvokeAny2() throws Exception {
1025     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1026 dl 1.12 try {
1027     e.invokeAny(new ArrayList<Callable<String>>());
1028 jsr166 1.27 shouldThrow();
1029 dl 1.12 } catch (IllegalArgumentException success) {
1030     } finally {
1031     joinPool(e);
1032     }
1033     }
1034    
1035     /**
1036     * invokeAny(c) throws NPE if c has null elements
1037     */
1038 jsr166 1.27 public void testInvokeAny3() throws Exception {
1039 jsr166 1.33 CountDownLatch latch = new CountDownLatch(1);
1040 jsr166 1.27 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1041 jsr166 1.33 List<Callable<String>> l = new ArrayList<Callable<String>>();
1042     l.add(latchAwaitingStringTask(latch));
1043     l.add(null);
1044 dl 1.12 try {
1045     e.invokeAny(l);
1046 jsr166 1.27 shouldThrow();
1047 dl 1.12 } catch (NullPointerException success) {
1048     } finally {
1049 jsr166 1.27 latch.countDown();
1050 dl 1.12 joinPool(e);
1051     }
1052     }
1053    
1054     /**
1055     * invokeAny(c) throws ExecutionException if no task completes
1056     */
1057 jsr166 1.27 public void testInvokeAny4() throws Exception {
1058     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1059 jsr166 1.33 List<Callable<String>> l = new ArrayList<Callable<String>>();
1060     l.add(new NPETask());
1061 dl 1.12 try {
1062     e.invokeAny(l);
1063 jsr166 1.27 shouldThrow();
1064 dl 1.12 } catch (ExecutionException success) {
1065 jsr166 1.27 assertTrue(success.getCause() instanceof NullPointerException);
1066 dl 1.12 } finally {
1067     joinPool(e);
1068     }
1069     }
1070    
1071     /**
1072     * invokeAny(c) returns result of some task
1073     */
1074 jsr166 1.27 public void testInvokeAny5() throws Exception {
1075     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1076 dl 1.12 try {
1077 jsr166 1.33 List<Callable<String>> l = new ArrayList<Callable<String>>();
1078 dl 1.12 l.add(new StringTask());
1079     l.add(new StringTask());
1080     String result = e.invokeAny(l);
1081     assertSame(TEST_STRING, result);
1082     } finally {
1083     joinPool(e);
1084     }
1085     }
1086    
1087     /**
1088     * invokeAll(null) throws NPE
1089     */
1090 jsr166 1.27 public void testInvokeAll1() throws Exception {
1091     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1092 dl 1.12 try {
1093     e.invokeAll(null);
1094 jsr166 1.27 shouldThrow();
1095 dl 1.12 } catch (NullPointerException success) {
1096     } finally {
1097     joinPool(e);
1098     }
1099     }
1100    
1101     /**
1102     * invokeAll(empty collection) returns empty collection
1103     */
1104 jsr166 1.27 public void testInvokeAll2() throws InterruptedException {
1105     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1106 dl 1.12 try {
1107     List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1108     assertTrue(r.isEmpty());
1109     } finally {
1110     joinPool(e);
1111     }
1112     }
1113    
1114     /**
1115     * invokeAll(c) throws NPE if c has null elements
1116     */
1117 jsr166 1.27 public void testInvokeAll3() throws Exception {
1118     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1119 jsr166 1.33 List<Callable<String>> l = new ArrayList<Callable<String>>();
1120     l.add(new StringTask());
1121     l.add(null);
1122 dl 1.12 try {
1123     e.invokeAll(l);
1124 jsr166 1.27 shouldThrow();
1125 dl 1.12 } catch (NullPointerException success) {
1126     } finally {
1127     joinPool(e);
1128     }
1129     }
1130    
1131     /**
1132     * get of element of invokeAll(c) throws exception on failed task
1133     */
1134 jsr166 1.27 public void testInvokeAll4() throws Exception {
1135     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1136 dl 1.12 try {
1137 jsr166 1.33 List<Callable<String>> l = new ArrayList<Callable<String>>();
1138 dl 1.12 l.add(new NPETask());
1139 jsr166 1.33 List<Future<String>> futures = e.invokeAll(l);
1140     assertEquals(1, futures.size());
1141     try {
1142     futures.get(0).get();
1143     shouldThrow();
1144     } catch (ExecutionException success) {
1145     assertTrue(success.getCause() instanceof NullPointerException);
1146 jsr166 1.27 }
1147 dl 1.12 } finally {
1148     joinPool(e);
1149     }
1150     }
1151    
1152     /**
1153     * invokeAll(c) returns results of all completed tasks
1154     */
1155 jsr166 1.27 public void testInvokeAll5() throws Exception {
1156     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1157 dl 1.12 try {
1158 jsr166 1.33 List<Callable<String>> l = new ArrayList<Callable<String>>();
1159 dl 1.12 l.add(new StringTask());
1160     l.add(new StringTask());
1161 jsr166 1.33 List<Future<String>> futures = e.invokeAll(l);
1162     assertEquals(2, futures.size());
1163     for (Future<String> future : futures)
1164 jsr166 1.27 assertSame(TEST_STRING, future.get());
1165 dl 1.12 } finally {
1166     joinPool(e);
1167     }
1168     }
1169    
1170    
1171 dl 1.13
1172     /**
1173     * timed invokeAny(null) throws NPE
1174     */
1175 jsr166 1.27 public void testTimedInvokeAny1() throws Exception {
1176     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1177 dl 1.13 try {
1178 jsr166 1.27 e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1179     shouldThrow();
1180 dl 1.13 } catch (NullPointerException success) {
1181     } finally {
1182     joinPool(e);
1183     }
1184     }
1185    
1186     /**
1187     * timed invokeAny(,,null) throws NPE
1188     */
1189 jsr166 1.27 public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1190     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1191 jsr166 1.33 List<Callable<String>> l = new ArrayList<Callable<String>>();
1192     l.add(new StringTask());
1193 dl 1.13 try {
1194     e.invokeAny(l, MEDIUM_DELAY_MS, null);
1195 jsr166 1.27 shouldThrow();
1196 dl 1.13 } catch (NullPointerException success) {
1197     } finally {
1198     joinPool(e);
1199     }
1200     }
1201    
1202     /**
1203     * timed invokeAny(empty collection) throws IAE
1204     */
1205 jsr166 1.27 public void testTimedInvokeAny2() throws Exception {
1206     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1207 dl 1.13 try {
1208 jsr166 1.27 e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1209     shouldThrow();
1210 dl 1.13 } catch (IllegalArgumentException success) {
1211     } finally {
1212     joinPool(e);
1213     }
1214     }
1215    
1216     /**
1217     * timed invokeAny(c) throws NPE if c has null elements
1218     */
1219 jsr166 1.27 public void testTimedInvokeAny3() throws Exception {
1220 jsr166 1.33 CountDownLatch latch = new CountDownLatch(1);
1221 jsr166 1.27 ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1222 jsr166 1.33 List<Callable<String>> l = new ArrayList<Callable<String>>();
1223     l.add(latchAwaitingStringTask(latch));
1224     l.add(null);
1225 dl 1.13 try {
1226 jsr166 1.27 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1227     shouldThrow();
1228 dl 1.13 } catch (NullPointerException success) {
1229     } finally {
1230 jsr166 1.30 latch.countDown();
1231 dl 1.13 joinPool(e);
1232     }
1233     }
1234    
1235     /**
1236     * timed invokeAny(c) throws ExecutionException if no task completes
1237     */
1238 jsr166 1.27 public void testTimedInvokeAny4() throws Exception {
1239     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1240 jsr166 1.33 List<Callable<String>> l = new ArrayList<Callable<String>>();
1241     l.add(new NPETask());
1242 dl 1.13 try {
1243 jsr166 1.27 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1244     shouldThrow();
1245 jsr166 1.25 } catch (ExecutionException success) {
1246 jsr166 1.27 assertTrue(success.getCause() instanceof NullPointerException);
1247 dl 1.13 } finally {
1248     joinPool(e);
1249     }
1250     }
1251    
1252     /**
1253     * timed invokeAny(c) returns result of some task
1254     */
1255 jsr166 1.27 public void testTimedInvokeAny5() throws Exception {
1256     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1257 dl 1.13 try {
1258 jsr166 1.33 List<Callable<String>> l = new ArrayList<Callable<String>>();
1259 dl 1.13 l.add(new StringTask());
1260     l.add(new StringTask());
1261 jsr166 1.27 String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1262 dl 1.13 assertSame(TEST_STRING, result);
1263     } finally {
1264     joinPool(e);
1265     }
1266     }
1267    
1268     /**
1269     * timed invokeAll(null) throws NPE
1270     */
1271 jsr166 1.27 public void testTimedInvokeAll1() throws Exception {
1272     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1273 dl 1.13 try {
1274 jsr166 1.27 e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1275     shouldThrow();
1276 dl 1.13 } catch (NullPointerException success) {
1277     } finally {
1278     joinPool(e);
1279     }
1280     }
1281    
1282     /**
1283     * timed invokeAll(,,null) throws NPE
1284     */
1285 jsr166 1.27 public void testTimedInvokeAllNullTimeUnit() throws Exception {
1286     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1287 jsr166 1.33 List<Callable<String>> l = new ArrayList<Callable<String>>();
1288     l.add(new StringTask());
1289 dl 1.13 try {
1290     e.invokeAll(l, MEDIUM_DELAY_MS, null);
1291 jsr166 1.27 shouldThrow();
1292 dl 1.13 } catch (NullPointerException success) {
1293     } finally {
1294     joinPool(e);
1295     }
1296     }
1297    
1298     /**
1299     * timed invokeAll(empty collection) returns empty collection
1300     */
1301 jsr166 1.27 public void testTimedInvokeAll2() throws InterruptedException {
1302     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1303 dl 1.13 try {
1304 jsr166 1.27 List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1305 dl 1.13 assertTrue(r.isEmpty());
1306     } finally {
1307     joinPool(e);
1308     }
1309     }
1310    
1311     /**
1312     * timed invokeAll(c) throws NPE if c has null elements
1313     */
1314 jsr166 1.27 public void testTimedInvokeAll3() throws Exception {
1315     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1316 jsr166 1.33 List<Callable<String>> l = new ArrayList<Callable<String>>();
1317     l.add(new StringTask());
1318     l.add(null);
1319 dl 1.13 try {
1320 jsr166 1.27 e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1321     shouldThrow();
1322 dl 1.13 } catch (NullPointerException success) {
1323     } finally {
1324     joinPool(e);
1325     }
1326     }
1327    
1328     /**
1329     * get of element of invokeAll(c) throws exception on failed task
1330     */
1331 jsr166 1.27 public void testTimedInvokeAll4() throws Exception {
1332     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1333 jsr166 1.33 List<Callable<String>> l = new ArrayList<Callable<String>>();
1334     l.add(new NPETask());
1335     List<Future<String>> futures =
1336     e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1337     assertEquals(1, futures.size());
1338 dl 1.13 try {
1339 jsr166 1.33 futures.get(0).get();
1340 jsr166 1.27 shouldThrow();
1341 jsr166 1.25 } catch (ExecutionException success) {
1342 jsr166 1.27 assertTrue(success.getCause() instanceof NullPointerException);
1343 dl 1.13 } finally {
1344     joinPool(e);
1345     }
1346     }
1347    
1348     /**
1349     * timed invokeAll(c) returns results of all completed tasks
1350     */
1351 jsr166 1.27 public void testTimedInvokeAll5() throws Exception {
1352     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1353 dl 1.13 try {
1354 jsr166 1.33 List<Callable<String>> l = new ArrayList<Callable<String>>();
1355 dl 1.13 l.add(new StringTask());
1356     l.add(new StringTask());
1357 jsr166 1.33 List<Future<String>> futures =
1358     e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1359     assertEquals(2, futures.size());
1360     for (Future<String> future : futures)
1361     assertSame(TEST_STRING, future.get());
1362 dl 1.13 } finally {
1363     joinPool(e);
1364     }
1365     }
1366    
1367     /**
1368     * timed invokeAll(c) cancels tasks not completed by timeout
1369     */
1370 jsr166 1.27 public void testTimedInvokeAll6() throws Exception {
1371     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1372 dl 1.13 try {
1373 jsr166 1.33 List<Callable<String>> l = new ArrayList<Callable<String>>();
1374 dl 1.13 l.add(new StringTask());
1375 dl 1.14 l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1376 dl 1.16 l.add(new StringTask());
1377 jsr166 1.33 List<Future<String>> futures =
1378     e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1379     assertEquals(3, futures.size());
1380     Iterator<Future<String>> it = futures.iterator();
1381 dl 1.13 Future<String> f1 = it.next();
1382     Future<String> f2 = it.next();
1383 dl 1.16 Future<String> f3 = it.next();
1384 dl 1.13 assertTrue(f1.isDone());
1385 dl 1.16 assertTrue(f2.isDone());
1386     assertTrue(f3.isDone());
1387 dl 1.13 assertFalse(f1.isCancelled());
1388     assertTrue(f2.isCancelled());
1389     } finally {
1390     joinPool(e);
1391     }
1392     }
1393    
1394 dl 1.19 /**
1395     * Execution continues if there is at least one thread even if
1396     * thread factory fails to create more
1397     */
1398 jsr166 1.27 public void testFailingThreadFactory() throws InterruptedException {
1399     ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1400 dl 1.19 try {
1401 jsr166 1.33 List<Callable<String>> l = new ArrayList<Callable<String>>();
1402 dl 1.19 for (int k = 0; k < 100; ++k) {
1403     e.execute(new NoOpRunnable());
1404     }
1405     Thread.sleep(LONG_DELAY_MS);
1406     } finally {
1407     joinPool(e);
1408     }
1409     }
1410 dl 1.21
1411     /**
1412     * allowsCoreThreadTimeOut is by default false.
1413     */
1414     public void testAllowsCoreThreadTimeOut() {
1415 jsr166 1.27 ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1416 dl 1.21 assertFalse(tpe.allowsCoreThreadTimeOut());
1417     joinPool(tpe);
1418     }
1419    
1420     /**
1421     * allowCoreThreadTimeOut(true) causes idle threads to time out
1422     */
1423 jsr166 1.27 public void testAllowCoreThreadTimeOut_true() throws InterruptedException {
1424     ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1425 dl 1.21 tpe.allowCoreThreadTimeOut(true);
1426     tpe.execute(new NoOpRunnable());
1427     try {
1428     Thread.sleep(MEDIUM_DELAY_MS);
1429     assertEquals(0, tpe.getPoolSize());
1430     } finally {
1431     joinPool(tpe);
1432     }
1433     }
1434    
1435     /**
1436     * allowCoreThreadTimeOut(false) causes idle threads not to time out
1437     */
1438 jsr166 1.27 public void testAllowCoreThreadTimeOut_false() throws InterruptedException {
1439     ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1440 dl 1.21 tpe.allowCoreThreadTimeOut(false);
1441     tpe.execute(new NoOpRunnable());
1442     try {
1443     Thread.sleep(MEDIUM_DELAY_MS);
1444     assertTrue(tpe.getPoolSize() >= 1);
1445     } finally {
1446     joinPool(tpe);
1447     }
1448     }
1449    
1450 dl 1.23 /**
1451     * execute allows the same task to be submitted multiple times, even
1452     * if rejected
1453     */
1454 jsr166 1.27 public void testRejectedRecycledTask() throws InterruptedException {
1455 dl 1.23 final int nTasks = 1000;
1456     final AtomicInteger nRun = new AtomicInteger(0);
1457     final Runnable recycledTask = new Runnable() {
1458     public void run() {
1459     nRun.getAndIncrement();
1460     } };
1461 jsr166 1.24 final ThreadPoolExecutor p =
1462     new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
1463 dl 1.23 new ArrayBlockingQueue(30));
1464     try {
1465     for (int i = 0; i < nTasks; ++i) {
1466     for (;;) {
1467     try {
1468     p.execute(recycledTask);
1469     break;
1470     }
1471     catch (RejectedExecutionException ignore) {
1472     }
1473     }
1474     }
1475     Thread.sleep(5000); // enough time to run all tasks
1476     assertEquals(nRun.get(), nTasks);
1477     } finally {
1478     p.shutdown();
1479     }
1480     }
1481 jsr166 1.24
1482 dl 1.1 }