ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.27
Committed: Wed Nov 18 05:39:51 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.26: +233 -328 lines
Log Message:
improve exception handling

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.24 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 dl 1.8 * 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 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
58 jsr166 1.27 } finally {
59     joinPool(p1);
60 jsr166 1.24 }
61 dl 1.1 }
62    
63     /**
64 dl 1.6 * 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     * prestartCoreThread starts a thread if under corePoolSize, else doesn't
78     */
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     * prestartAllCoreThreads starts all corePoolSize threads
93     */
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 dl 1.6 * 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 dl 1.6 * 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 dl 1.6 * 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 dl 1.6 * 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 dl 1.6 * 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 dl 1.6 * 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 dl 1.8 * 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 dl 1.6 * 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.27 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 dl 1.5 assertTrue(p1.isShutdown());
268     joinPool(p1);
269 dl 1.1 }
270    
271 jsr166 1.24
272 dl 1.1 /**
273 dl 1.6 * isTerminated is false before termination, true after
274 dl 1.1 */
275 jsr166 1.27 public void testIsTerminated() throws InterruptedException {
276     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 dl 1.6 * isTerminating is not true when running or when terminated
289 dl 1.5 */
290 jsr166 1.27 public void testIsTerminating() throws InterruptedException {
291     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 dl 1.6 * 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 dl 1.6 * shutDownNow returns a list containing tasks that were not run
376 dl 1.1 */
377 dl 1.5 public void testShutDownNow() {
378 jsr166 1.27 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 jsr166 1.24
389 dl 1.1 }
390 dl 1.5 assertTrue(p1.isShutdown());
391 dl 1.1 assertTrue(l.size() <= 4);
392     }
393    
394     // Exception Tests
395    
396 jsr166 1.24
397     /**
398     * Constructor throws if corePoolSize argument is less than zero
399 dl 1.6 */
400 dl 1.1 public void testConstructor1() {
401 dl 1.5 try {
402 jsr166 1.27 new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
403 dl 1.5 shouldThrow();
404 dl 1.1 }
405 jsr166 1.26 catch (IllegalArgumentException success) {}
406 dl 1.1 }
407 jsr166 1.24
408     /**
409     * Constructor throws if maximumPoolSize is less than zero
410 dl 1.6 */
411 dl 1.1 public void testConstructor2() {
412 dl 1.5 try {
413 jsr166 1.27 new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
414 dl 1.5 shouldThrow();
415 dl 1.1 }
416 jsr166 1.26 catch (IllegalArgumentException success) {}
417 dl 1.1 }
418 jsr166 1.24
419     /**
420     * Constructor throws if maximumPoolSize is equal to zero
421 dl 1.6 */
422 dl 1.1 public void testConstructor3() {
423 dl 1.5 try {
424 jsr166 1.27 new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
425 dl 1.5 shouldThrow();
426 dl 1.1 }
427 jsr166 1.26 catch (IllegalArgumentException success) {}
428 dl 1.1 }
429    
430 jsr166 1.24 /**
431     * Constructor throws if keepAliveTime is less than zero
432 dl 1.6 */
433 dl 1.1 public void testConstructor4() {
434 dl 1.5 try {
435 jsr166 1.27 new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
436 dl 1.5 shouldThrow();
437 dl 1.1 }
438 jsr166 1.26 catch (IllegalArgumentException success) {}
439 dl 1.1 }
440    
441 jsr166 1.24 /**
442     * Constructor throws if corePoolSize is greater than the maximumPoolSize
443 dl 1.6 */
444 dl 1.1 public void testConstructor5() {
445 dl 1.5 try {
446 jsr166 1.27 new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
447 dl 1.5 shouldThrow();
448 dl 1.1 }
449 jsr166 1.26 catch (IllegalArgumentException success) {}
450 dl 1.1 }
451 jsr166 1.24
452     /**
453     * Constructor throws if workQueue is set to null
454 dl 1.6 */
455 dl 1.8 public void testConstructorNullPointerException() {
456 dl 1.5 try {
457 jsr166 1.27 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null);
458 dl 1.5 shouldThrow();
459 dl 1.1 }
460 jsr166 1.26 catch (NullPointerException success) {}
461 dl 1.1 }
462    
463 jsr166 1.24
464    
465     /**
466     * Constructor throws if corePoolSize argument is less than zero
467 dl 1.6 */
468 dl 1.1 public void testConstructor6() {
469 dl 1.5 try {
470 jsr166 1.27 new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
471 dl 1.5 shouldThrow();
472 jsr166 1.27 }
473     catch (IllegalArgumentException success) {}
474 dl 1.1 }
475 jsr166 1.24
476     /**
477     * Constructor throws if maximumPoolSize is less than zero
478 dl 1.6 */
479 dl 1.1 public void testConstructor7() {
480 dl 1.5 try {
481 jsr166 1.27 new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
482 dl 1.5 shouldThrow();
483 dl 1.1 }
484 jsr166 1.26 catch (IllegalArgumentException success) {}
485 dl 1.1 }
486    
487 jsr166 1.24 /**
488     * Constructor throws if maximumPoolSize is equal to zero
489 dl 1.6 */
490 dl 1.1 public void testConstructor8() {
491 dl 1.5 try {
492 jsr166 1.27 new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
493 dl 1.5 shouldThrow();
494 dl 1.1 }
495 jsr166 1.26 catch (IllegalArgumentException success) {}
496 dl 1.1 }
497    
498 jsr166 1.24 /**
499     * Constructor throws if keepAliveTime is less than zero
500 dl 1.6 */
501 dl 1.1 public void testConstructor9() {
502 dl 1.5 try {
503 jsr166 1.27 new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
504 dl 1.5 shouldThrow();
505 dl 1.1 }
506 jsr166 1.26 catch (IllegalArgumentException success) {}
507 dl 1.1 }
508    
509 jsr166 1.24 /**
510     * Constructor throws if corePoolSize is greater than the maximumPoolSize
511 dl 1.6 */
512 dl 1.1 public void testConstructor10() {
513 dl 1.5 try {
514 jsr166 1.27 new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
515 dl 1.5 shouldThrow();
516 dl 1.1 }
517 jsr166 1.26 catch (IllegalArgumentException success) {}
518 dl 1.1 }
519    
520 jsr166 1.24 /**
521     * Constructor throws if workQueue is set to null
522 dl 1.6 */
523 dl 1.8 public void testConstructorNullPointerException2() {
524 dl 1.5 try {
525 jsr166 1.27 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
526 dl 1.5 shouldThrow();
527 dl 1.1 }
528 jsr166 1.26 catch (NullPointerException success) {}
529 dl 1.1 }
530    
531 jsr166 1.24 /**
532     * Constructor throws if threadFactory is set to null
533 dl 1.6 */
534 dl 1.8 public void testConstructorNullPointerException3() {
535 dl 1.5 try {
536 dl 1.1 ThreadFactory f = null;
537 jsr166 1.27 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
538 dl 1.5 shouldThrow();
539 dl 1.1 }
540 jsr166 1.26 catch (NullPointerException success) {}
541 dl 1.1 }
542 jsr166 1.24
543    
544     /**
545     * Constructor throws if corePoolSize argument is less than zero
546 dl 1.6 */
547 dl 1.1 public void testConstructor11() {
548 dl 1.5 try {
549 jsr166 1.27 new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
550 dl 1.5 shouldThrow();
551 dl 1.1 }
552 jsr166 1.26 catch (IllegalArgumentException success) {}
553 dl 1.1 }
554    
555 jsr166 1.24 /**
556     * Constructor throws if maximumPoolSize is less than zero
557 dl 1.6 */
558 dl 1.1 public void testConstructor12() {
559 dl 1.5 try {
560 jsr166 1.27 new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
561 dl 1.5 shouldThrow();
562 dl 1.1 }
563 jsr166 1.26 catch (IllegalArgumentException success) {}
564 dl 1.1 }
565    
566 jsr166 1.24 /**
567     * Constructor throws if maximumPoolSize is equal to zero
568 dl 1.6 */
569 dl 1.1 public void testConstructor13() {
570 dl 1.5 try {
571 jsr166 1.27 new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
572 dl 1.5 shouldThrow();
573 dl 1.1 }
574 jsr166 1.26 catch (IllegalArgumentException success) {}
575 dl 1.1 }
576    
577 jsr166 1.24 /**
578     * Constructor throws if keepAliveTime is less than zero
579 dl 1.6 */
580 dl 1.1 public void testConstructor14() {
581 dl 1.5 try {
582 jsr166 1.27 new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
583 dl 1.5 shouldThrow();
584 dl 1.1 }
585 jsr166 1.26 catch (IllegalArgumentException success) {}
586 dl 1.1 }
587    
588 jsr166 1.24 /**
589     * Constructor throws if corePoolSize is greater than the maximumPoolSize
590 dl 1.6 */
591 dl 1.1 public void testConstructor15() {
592 dl 1.5 try {
593 jsr166 1.27 new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
594 dl 1.5 shouldThrow();
595 dl 1.1 }
596 jsr166 1.26 catch (IllegalArgumentException success) {}
597 dl 1.1 }
598    
599 jsr166 1.24 /**
600     * Constructor throws if workQueue is set to null
601 dl 1.6 */
602 dl 1.8 public void testConstructorNullPointerException4() {
603 dl 1.5 try {
604 jsr166 1.27 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
605 dl 1.5 shouldThrow();
606 dl 1.1 }
607 jsr166 1.26 catch (NullPointerException success) {}
608 dl 1.1 }
609    
610 jsr166 1.24 /**
611     * Constructor throws if handler is set to null
612 dl 1.6 */
613 dl 1.8 public void testConstructorNullPointerException5() {
614 dl 1.5 try {
615 dl 1.1 RejectedExecutionHandler r = null;
616 jsr166 1.27 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
617 dl 1.5 shouldThrow();
618 dl 1.1 }
619 jsr166 1.26 catch (NullPointerException success) {}
620 dl 1.1 }
621    
622 jsr166 1.24
623     /**
624     * Constructor throws if corePoolSize argument is less than zero
625 dl 1.6 */
626 dl 1.1 public void testConstructor16() {
627 dl 1.5 try {
628 jsr166 1.27 new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
629 dl 1.5 shouldThrow();
630 dl 1.1 }
631 jsr166 1.26 catch (IllegalArgumentException success) {}
632 dl 1.1 }
633    
634 jsr166 1.24 /**
635     * Constructor throws if maximumPoolSize is less than zero
636 dl 1.6 */
637 dl 1.1 public void testConstructor17() {
638 dl 1.5 try {
639 jsr166 1.27 new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
640 dl 1.5 shouldThrow();
641 dl 1.1 }
642 jsr166 1.26 catch (IllegalArgumentException success) {}
643 dl 1.1 }
644    
645 jsr166 1.24 /**
646     * Constructor throws if maximumPoolSize is equal to zero
647 dl 1.6 */
648 dl 1.1 public void testConstructor18() {
649 dl 1.5 try {
650 jsr166 1.27 new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
651 dl 1.5 shouldThrow();
652 dl 1.1 }
653 jsr166 1.26 catch (IllegalArgumentException success) {}
654 dl 1.1 }
655    
656 jsr166 1.24 /**
657     * Constructor throws if keepAliveTime is less than zero
658 dl 1.6 */
659 dl 1.1 public void testConstructor19() {
660 dl 1.5 try {
661 jsr166 1.27 new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
662 dl 1.5 shouldThrow();
663 dl 1.1 }
664 jsr166 1.26 catch (IllegalArgumentException success) {}
665 dl 1.1 }
666    
667 jsr166 1.24 /**
668     * Constructor throws if corePoolSize is greater than the maximumPoolSize
669 dl 1.6 */
670 dl 1.1 public void testConstructor20() {
671 dl 1.5 try {
672 jsr166 1.27 new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
673 dl 1.5 shouldThrow();
674 dl 1.1 }
675 jsr166 1.26 catch (IllegalArgumentException success) {}
676 dl 1.1 }
677    
678 jsr166 1.24 /**
679     * Constructor throws if workQueue is set to null
680 dl 1.6 */
681 dl 1.8 public void testConstructorNullPointerException6() {
682 dl 1.5 try {
683 jsr166 1.27 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
684 dl 1.5 shouldThrow();
685 dl 1.1 }
686 jsr166 1.26 catch (NullPointerException success) {}
687 dl 1.1 }
688    
689 jsr166 1.24 /**
690     * Constructor throws if handler is set to null
691 dl 1.6 */
692 dl 1.8 public void testConstructorNullPointerException7() {
693 dl 1.5 try {
694 dl 1.1 RejectedExecutionHandler r = null;
695 jsr166 1.27 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
696 dl 1.5 shouldThrow();
697 dl 1.1 }
698 jsr166 1.26 catch (NullPointerException success) {}
699 dl 1.1 }
700    
701 jsr166 1.24 /**
702     * Constructor throws if ThreadFactory is set top null
703 dl 1.6 */
704 dl 1.8 public void testConstructorNullPointerException8() {
705 dl 1.5 try {
706 dl 1.1 ThreadFactory f = null;
707 jsr166 1.27 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
708 dl 1.5 shouldThrow();
709 dl 1.1 }
710 jsr166 1.27 catch (NullPointerException success) {}
711 dl 1.1 }
712 jsr166 1.24
713 dl 1.1
714     /**
715 dl 1.8 * execute throws RejectedExecutionException
716     * if saturated.
717     */
718     public void testSaturatedExecute() {
719 jsr166 1.27 ThreadPoolExecutor p =
720     new ThreadPoolExecutor(1, 1,
721     LONG_DELAY_MS, MILLISECONDS,
722     new ArrayBlockingQueue<Runnable>(1));
723 dl 1.8 try {
724 jsr166 1.27 for (int i = 0; i < 2; ++i)
725 dl 1.8 p.execute(new MediumRunnable());
726 jsr166 1.27 for (int i = 0; i < 2; ++i) {
727     try {
728     p.execute(new MediumRunnable());
729     shouldThrow();
730     } catch (RejectedExecutionException success) {}
731 dl 1.8 }
732 jsr166 1.27 } finally {
733     joinPool(p);
734     }
735 dl 1.8 }
736    
737     /**
738     * executor using CallerRunsPolicy runs task if saturated.
739     */
740     public void testSaturatedExecute2() {
741     RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
742 jsr166 1.27 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
743 dl 1.8 try {
744 jsr166 1.24
745 dl 1.8 TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
746 jsr166 1.26 for (int i = 0; i < 5; ++i) {
747 dl 1.8 tasks[i] = new TrackedNoOpRunnable();
748     }
749     TrackedLongRunnable mr = new TrackedLongRunnable();
750     p.execute(mr);
751 jsr166 1.26 for (int i = 0; i < 5; ++i) {
752 dl 1.8 p.execute(tasks[i]);
753     }
754 jsr166 1.25 for (int i = 1; i < 5; ++i) {
755 dl 1.8 assertTrue(tasks[i].done);
756     }
757 jsr166 1.25 try { p.shutdownNow(); } catch (SecurityException ok) { return; }
758 dl 1.8 } finally {
759     joinPool(p);
760     }
761     }
762    
763     /**
764     * executor using DiscardPolicy drops task if saturated.
765     */
766     public void testSaturatedExecute3() {
767     RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
768 jsr166 1.27 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
769 dl 1.8 try {
770 jsr166 1.24
771 dl 1.8 TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
772 jsr166 1.26 for (int i = 0; i < 5; ++i) {
773 dl 1.8 tasks[i] = new TrackedNoOpRunnable();
774     }
775     p.execute(new TrackedLongRunnable());
776 jsr166 1.26 for (int i = 0; i < 5; ++i) {
777 dl 1.8 p.execute(tasks[i]);
778     }
779 jsr166 1.26 for (int i = 0; i < 5; ++i) {
780 dl 1.8 assertFalse(tasks[i].done);
781     }
782 jsr166 1.25 try { p.shutdownNow(); } catch (SecurityException ok) { return; }
783 dl 1.8 } finally {
784     joinPool(p);
785     }
786     }
787    
788     /**
789     * executor using DiscardOldestPolicy drops oldest task if saturated.
790     */
791     public void testSaturatedExecute4() {
792     RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
793 jsr166 1.27 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
794 dl 1.8 try {
795     p.execute(new TrackedLongRunnable());
796     TrackedLongRunnable r2 = new TrackedLongRunnable();
797     p.execute(r2);
798     assertTrue(p.getQueue().contains(r2));
799     TrackedNoOpRunnable r3 = new TrackedNoOpRunnable();
800     p.execute(r3);
801     assertFalse(p.getQueue().contains(r2));
802     assertTrue(p.getQueue().contains(r3));
803 jsr166 1.25 try { p.shutdownNow(); } catch (SecurityException ok) { return; }
804 dl 1.8 } finally {
805     joinPool(p);
806     }
807     }
808    
809     /**
810 dl 1.6 * execute throws RejectedExecutionException if shutdown
811 dl 1.1 */
812 dl 1.8 public void testRejectedExecutionExceptionOnShutdown() {
813 jsr166 1.24 ThreadPoolExecutor tpe =
814 jsr166 1.27 new ThreadPoolExecutor(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
815 jsr166 1.25 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
816 dl 1.5 try {
817 dl 1.3 tpe.execute(new NoOpRunnable());
818 dl 1.5 shouldThrow();
819 jsr166 1.26 } catch (RejectedExecutionException success) {}
820 jsr166 1.24
821 dl 1.3 joinPool(tpe);
822 dl 1.1 }
823 dl 1.6
824     /**
825 dl 1.8 * execute using CallerRunsPolicy drops task on shutdown
826     */
827     public void testCallerRunsOnShutdown() {
828     RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
829 jsr166 1.27 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
830 dl 1.8
831 jsr166 1.25 try { p.shutdown(); } catch (SecurityException ok) { return; }
832 dl 1.8 try {
833     TrackedNoOpRunnable r = new TrackedNoOpRunnable();
834     p.execute(r);
835     assertFalse(r.done);
836     } finally {
837     joinPool(p);
838     }
839     }
840    
841     /**
842     * execute using DiscardPolicy drops task on shutdown
843     */
844     public void testDiscardOnShutdown() {
845     RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
846 jsr166 1.27 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
847 dl 1.8
848 jsr166 1.25 try { p.shutdown(); } catch (SecurityException ok) { return; }
849 dl 1.8 try {
850     TrackedNoOpRunnable r = new TrackedNoOpRunnable();
851     p.execute(r);
852     assertFalse(r.done);
853     } finally {
854     joinPool(p);
855     }
856     }
857    
858    
859     /**
860     * execute using DiscardOldestPolicy drops task on shutdown
861 dl 1.6 */
862 dl 1.8 public void testDiscardOldestOnShutdown() {
863     RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
864 jsr166 1.27 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
865 dl 1.8
866 jsr166 1.25 try { p.shutdown(); } catch (SecurityException ok) { return; }
867 dl 1.8 try {
868     TrackedNoOpRunnable r = new TrackedNoOpRunnable();
869     p.execute(r);
870     assertFalse(r.done);
871     } finally {
872     joinPool(p);
873     }
874 dl 1.6 }
875    
876 dl 1.8
877 dl 1.6 /**
878     * execute (null) throws NPE
879     */
880     public void testExecuteNull() {
881     ThreadPoolExecutor tpe = null;
882     try {
883 jsr166 1.27 tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
884 dl 1.6 tpe.execute(null);
885     shouldThrow();
886 jsr166 1.26 } catch (NullPointerException success) {}
887 jsr166 1.24
888 dl 1.6 joinPool(tpe);
889     }
890 jsr166 1.24
891 dl 1.1 /**
892 dl 1.8 * setCorePoolSize of negative value throws IllegalArgumentException
893 dl 1.1 */
894 dl 1.5 public void testCorePoolSizeIllegalArgumentException() {
895 jsr166 1.27 ThreadPoolExecutor tpe =
896     new ThreadPoolExecutor(1, 2,
897     LONG_DELAY_MS, MILLISECONDS,
898     new ArrayBlockingQueue<Runnable>(10));
899 dl 1.5 try {
900 dl 1.1 tpe.setCorePoolSize(-1);
901 dl 1.5 shouldThrow();
902 jsr166 1.26 } catch (IllegalArgumentException success) {
903 dl 1.1 } finally {
904 jsr166 1.25 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
905 dl 1.1 }
906 dl 1.3 joinPool(tpe);
907 jsr166 1.24 }
908 dl 1.1
909     /**
910 dl 1.6 * setMaximumPoolSize(int) throws IllegalArgumentException if
911     * given a value less the core pool size
912 jsr166 1.24 */
913 dl 1.5 public void testMaximumPoolSizeIllegalArgumentException() {
914 jsr166 1.27 ThreadPoolExecutor tpe =
915     new ThreadPoolExecutor(2, 3,
916     LONG_DELAY_MS, MILLISECONDS,
917     new ArrayBlockingQueue<Runnable>(10));
918 dl 1.5 try {
919 dl 1.1 tpe.setMaximumPoolSize(1);
920 dl 1.5 shouldThrow();
921 jsr166 1.26 } catch (IllegalArgumentException success) {
922 dl 1.1 } finally {
923 jsr166 1.25 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
924 dl 1.1 }
925 dl 1.3 joinPool(tpe);
926 dl 1.1 }
927 jsr166 1.24
928 dl 1.1 /**
929 dl 1.6 * setMaximumPoolSize throws IllegalArgumentException
930     * if given a negative value
931 dl 1.1 */
932 dl 1.5 public void testMaximumPoolSizeIllegalArgumentException2() {
933 jsr166 1.27 ThreadPoolExecutor tpe =
934     new ThreadPoolExecutor(2, 3,
935     LONG_DELAY_MS, MILLISECONDS,
936     new ArrayBlockingQueue<Runnable>(10));
937 dl 1.5 try {
938 dl 1.1 tpe.setMaximumPoolSize(-1);
939 dl 1.5 shouldThrow();
940 jsr166 1.26 } catch (IllegalArgumentException success) {
941 dl 1.1 } finally {
942 jsr166 1.25 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
943 dl 1.1 }
944 dl 1.3 joinPool(tpe);
945 dl 1.1 }
946 jsr166 1.24
947 dl 1.1
948     /**
949 dl 1.6 * setKeepAliveTime throws IllegalArgumentException
950 dl 1.1 * when given a negative value
951     */
952 dl 1.5 public void testKeepAliveTimeIllegalArgumentException() {
953 jsr166 1.27 ThreadPoolExecutor tpe =
954     new ThreadPoolExecutor(2, 3,
955     LONG_DELAY_MS, MILLISECONDS,
956     new ArrayBlockingQueue<Runnable>(10));
957 dl 1.5 try {
958 jsr166 1.27 tpe.setKeepAliveTime(-1,MILLISECONDS);
959 dl 1.5 shouldThrow();
960 jsr166 1.26 } catch (IllegalArgumentException success) {
961 dl 1.1 } finally {
962 jsr166 1.25 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
963 dl 1.1 }
964 dl 1.3 joinPool(tpe);
965 dl 1.1 }
966 dl 1.8
967     /**
968     * terminated() is called on termination
969     */
970     public void testTerminated() {
971     ExtendedTPE tpe = new ExtendedTPE();
972 jsr166 1.25 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
973 dl 1.8 assertTrue(tpe.terminatedCalled);
974     joinPool(tpe);
975     }
976    
977     /**
978     * beforeExecute and afterExecute are called when executing task
979     */
980 jsr166 1.27 public void testBeforeAfter() throws InterruptedException {
981 dl 1.8 ExtendedTPE tpe = new ExtendedTPE();
982     try {
983     TrackedNoOpRunnable r = new TrackedNoOpRunnable();
984     tpe.execute(r);
985     Thread.sleep(SHORT_DELAY_MS);
986     assertTrue(r.done);
987     assertTrue(tpe.beforeCalled);
988     assertTrue(tpe.afterCalled);
989 jsr166 1.25 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
990 dl 1.8 } finally {
991     joinPool(tpe);
992     }
993     }
994 dl 1.12
995     /**
996     * completed submit of callable returns result
997     */
998 jsr166 1.27 public void testSubmitCallable() throws Exception {
999     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1000 dl 1.12 try {
1001     Future<String> future = e.submit(new StringTask());
1002     String result = future.get();
1003     assertSame(TEST_STRING, result);
1004     } finally {
1005     joinPool(e);
1006     }
1007     }
1008    
1009     /**
1010     * completed submit of runnable returns successfully
1011     */
1012 jsr166 1.27 public void testSubmitRunnable() throws Exception {
1013     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1014 dl 1.12 try {
1015     Future<?> future = e.submit(new NoOpRunnable());
1016     future.get();
1017     assertTrue(future.isDone());
1018     } finally {
1019     joinPool(e);
1020     }
1021     }
1022    
1023     /**
1024     * completed submit of (runnable, result) returns result
1025     */
1026 jsr166 1.27 public void testSubmitRunnable2() throws Exception {
1027     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1028 dl 1.12 try {
1029     Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1030     String result = future.get();
1031     assertSame(TEST_STRING, result);
1032     } finally {
1033     joinPool(e);
1034     }
1035     }
1036    
1037    
1038     /**
1039     * invokeAny(null) throws NPE
1040     */
1041 jsr166 1.27 public void testInvokeAny1() throws Exception {
1042     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1043 dl 1.12 try {
1044     e.invokeAny(null);
1045 jsr166 1.27 shouldThrow();
1046 dl 1.12 } catch (NullPointerException success) {
1047     } finally {
1048     joinPool(e);
1049     }
1050     }
1051    
1052     /**
1053     * invokeAny(empty collection) throws IAE
1054     */
1055 jsr166 1.27 public void testInvokeAny2() throws Exception {
1056     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1057 dl 1.12 try {
1058     e.invokeAny(new ArrayList<Callable<String>>());
1059 jsr166 1.27 shouldThrow();
1060 dl 1.12 } catch (IllegalArgumentException success) {
1061     } finally {
1062     joinPool(e);
1063     }
1064     }
1065    
1066     /**
1067     * invokeAny(c) throws NPE if c has null elements
1068     */
1069 jsr166 1.27 public void testInvokeAny3() throws Exception {
1070     final CountDownLatch latch = new CountDownLatch(1);
1071     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1072 dl 1.12 try {
1073     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1074 jsr166 1.27 l.add(new CheckedCallable<String>() {
1075     public String realCall() throws InterruptedException {
1076     latch.await();
1077     return TEST_STRING;
1078     }});
1079 dl 1.12 l.add(null);
1080     e.invokeAny(l);
1081 jsr166 1.27 shouldThrow();
1082 dl 1.12 } catch (NullPointerException success) {
1083     } finally {
1084 jsr166 1.27 latch.countDown();
1085 dl 1.12 joinPool(e);
1086     }
1087     }
1088    
1089     /**
1090     * invokeAny(c) throws ExecutionException if no task completes
1091     */
1092 jsr166 1.27 public void testInvokeAny4() throws Exception {
1093     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1094 dl 1.12 try {
1095     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1096     l.add(new NPETask());
1097     e.invokeAny(l);
1098 jsr166 1.27 shouldThrow();
1099 dl 1.12 } catch (ExecutionException success) {
1100 jsr166 1.27 assertTrue(success.getCause() instanceof NullPointerException);
1101 dl 1.12 } finally {
1102     joinPool(e);
1103     }
1104     }
1105    
1106     /**
1107     * invokeAny(c) returns result of some task
1108     */
1109 jsr166 1.27 public void testInvokeAny5() throws Exception {
1110     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1111 dl 1.12 try {
1112     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1113     l.add(new StringTask());
1114     l.add(new StringTask());
1115     String result = e.invokeAny(l);
1116     assertSame(TEST_STRING, result);
1117     } finally {
1118     joinPool(e);
1119     }
1120     }
1121    
1122     /**
1123     * invokeAll(null) throws NPE
1124     */
1125 jsr166 1.27 public void testInvokeAll1() throws Exception {
1126     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1127 dl 1.12 try {
1128     e.invokeAll(null);
1129 jsr166 1.27 shouldThrow();
1130 dl 1.12 } catch (NullPointerException success) {
1131     } finally {
1132     joinPool(e);
1133     }
1134     }
1135    
1136     /**
1137     * invokeAll(empty collection) returns empty collection
1138     */
1139 jsr166 1.27 public void testInvokeAll2() throws InterruptedException {
1140     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1141 dl 1.12 try {
1142     List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1143     assertTrue(r.isEmpty());
1144     } finally {
1145     joinPool(e);
1146     }
1147     }
1148    
1149     /**
1150     * invokeAll(c) throws NPE if c has null elements
1151     */
1152 jsr166 1.27 public void testInvokeAll3() throws Exception {
1153     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1154 dl 1.12 try {
1155     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1156     l.add(new StringTask());
1157     l.add(null);
1158     e.invokeAll(l);
1159 jsr166 1.27 shouldThrow();
1160 dl 1.12 } catch (NullPointerException success) {
1161     } finally {
1162     joinPool(e);
1163     }
1164     }
1165    
1166     /**
1167     * get of element of invokeAll(c) throws exception on failed task
1168     */
1169 jsr166 1.27 public void testInvokeAll4() throws Exception {
1170     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1171 dl 1.12 try {
1172     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1173     l.add(new NPETask());
1174     List<Future<String>> result = e.invokeAll(l);
1175     assertEquals(1, result.size());
1176 jsr166 1.27 for (Future<String> future : result) {
1177     try {
1178     future.get();
1179     shouldThrow();
1180     } catch (ExecutionException success) {
1181     Throwable cause = success.getCause();
1182     assertTrue(cause instanceof NullPointerException);
1183     }
1184     }
1185 dl 1.12 } finally {
1186     joinPool(e);
1187     }
1188     }
1189    
1190     /**
1191     * invokeAll(c) returns results of all completed tasks
1192     */
1193 jsr166 1.27 public void testInvokeAll5() throws Exception {
1194     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1195 dl 1.12 try {
1196     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1197     l.add(new StringTask());
1198     l.add(new StringTask());
1199     List<Future<String>> result = e.invokeAll(l);
1200     assertEquals(2, result.size());
1201 jsr166 1.27 for (Future<String> future : result)
1202     assertSame(TEST_STRING, future.get());
1203 dl 1.12 } finally {
1204     joinPool(e);
1205     }
1206     }
1207    
1208    
1209 dl 1.13
1210     /**
1211     * timed invokeAny(null) throws NPE
1212     */
1213 jsr166 1.27 public void testTimedInvokeAny1() throws Exception {
1214     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1215 dl 1.13 try {
1216 jsr166 1.27 e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1217     shouldThrow();
1218 dl 1.13 } catch (NullPointerException success) {
1219     } finally {
1220     joinPool(e);
1221     }
1222     }
1223    
1224     /**
1225     * timed invokeAny(,,null) throws NPE
1226     */
1227 jsr166 1.27 public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1228     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1229 dl 1.13 try {
1230     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1231     l.add(new StringTask());
1232     e.invokeAny(l, MEDIUM_DELAY_MS, null);
1233 jsr166 1.27 shouldThrow();
1234 dl 1.13 } catch (NullPointerException success) {
1235     } finally {
1236     joinPool(e);
1237     }
1238     }
1239    
1240     /**
1241     * timed invokeAny(empty collection) throws IAE
1242     */
1243 jsr166 1.27 public void testTimedInvokeAny2() throws Exception {
1244     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1245 dl 1.13 try {
1246 jsr166 1.27 e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1247     shouldThrow();
1248 dl 1.13 } catch (IllegalArgumentException success) {
1249     } finally {
1250     joinPool(e);
1251     }
1252     }
1253    
1254     /**
1255     * timed invokeAny(c) throws NPE if c has null elements
1256     */
1257 jsr166 1.27 public void testTimedInvokeAny3() throws Exception {
1258     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1259 dl 1.13 try {
1260     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1261     l.add(new StringTask());
1262     l.add(null);
1263 jsr166 1.27 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1264     shouldThrow();
1265 dl 1.13 } catch (NullPointerException success) {
1266     } finally {
1267     joinPool(e);
1268     }
1269     }
1270    
1271     /**
1272     * timed invokeAny(c) throws ExecutionException if no task completes
1273     */
1274 jsr166 1.27 public void testTimedInvokeAny4() throws Exception {
1275     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1276 dl 1.13 try {
1277     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1278     l.add(new NPETask());
1279 jsr166 1.27 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1280     shouldThrow();
1281 jsr166 1.25 } catch (ExecutionException success) {
1282 jsr166 1.27 assertTrue(success.getCause() instanceof NullPointerException);
1283 dl 1.13 } finally {
1284     joinPool(e);
1285     }
1286     }
1287    
1288     /**
1289     * timed invokeAny(c) returns result of some task
1290     */
1291 jsr166 1.27 public void testTimedInvokeAny5() throws Exception {
1292     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1293 dl 1.13 try {
1294     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1295     l.add(new StringTask());
1296     l.add(new StringTask());
1297 jsr166 1.27 String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1298 dl 1.13 assertSame(TEST_STRING, result);
1299     } finally {
1300     joinPool(e);
1301     }
1302     }
1303    
1304     /**
1305     * timed invokeAll(null) throws NPE
1306     */
1307 jsr166 1.27 public void testTimedInvokeAll1() throws Exception {
1308     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1309 dl 1.13 try {
1310 jsr166 1.27 e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1311     shouldThrow();
1312 dl 1.13 } catch (NullPointerException success) {
1313     } finally {
1314     joinPool(e);
1315     }
1316     }
1317    
1318     /**
1319     * timed invokeAll(,,null) throws NPE
1320     */
1321 jsr166 1.27 public void testTimedInvokeAllNullTimeUnit() throws Exception {
1322     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1323 dl 1.13 try {
1324     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1325     l.add(new StringTask());
1326     e.invokeAll(l, MEDIUM_DELAY_MS, null);
1327 jsr166 1.27 shouldThrow();
1328 dl 1.13 } catch (NullPointerException success) {
1329     } finally {
1330     joinPool(e);
1331     }
1332     }
1333    
1334     /**
1335     * timed invokeAll(empty collection) returns empty collection
1336     */
1337 jsr166 1.27 public void testTimedInvokeAll2() throws InterruptedException {
1338     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1339 dl 1.13 try {
1340 jsr166 1.27 List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1341 dl 1.13 assertTrue(r.isEmpty());
1342     } finally {
1343     joinPool(e);
1344     }
1345     }
1346    
1347     /**
1348     * timed invokeAll(c) throws NPE if c has null elements
1349     */
1350 jsr166 1.27 public void testTimedInvokeAll3() throws Exception {
1351     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1352 dl 1.13 try {
1353     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1354     l.add(new StringTask());
1355     l.add(null);
1356 jsr166 1.27 e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1357     shouldThrow();
1358 dl 1.13 } catch (NullPointerException success) {
1359     } finally {
1360     joinPool(e);
1361     }
1362     }
1363    
1364     /**
1365     * get of element of invokeAll(c) throws exception on failed task
1366     */
1367 jsr166 1.27 public void testTimedInvokeAll4() throws Exception {
1368     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1369 dl 1.13 try {
1370     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1371     l.add(new NPETask());
1372 jsr166 1.27 List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1373 dl 1.13 assertEquals(1, result.size());
1374 jsr166 1.27 for (Future<String> future : result)
1375     future.get();
1376     shouldThrow();
1377 jsr166 1.25 } catch (ExecutionException success) {
1378 jsr166 1.27 assertTrue(success.getCause() instanceof NullPointerException);
1379 dl 1.13 } finally {
1380     joinPool(e);
1381     }
1382     }
1383    
1384     /**
1385     * timed invokeAll(c) returns results of all completed tasks
1386     */
1387 jsr166 1.27 public void testTimedInvokeAll5() throws Exception {
1388     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1389 dl 1.13 try {
1390     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1391     l.add(new StringTask());
1392     l.add(new StringTask());
1393 jsr166 1.27 List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1394 dl 1.13 assertEquals(2, result.size());
1395 jsr166 1.24 for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1396 dl 1.13 assertSame(TEST_STRING, it.next().get());
1397     } finally {
1398     joinPool(e);
1399     }
1400     }
1401    
1402     /**
1403     * timed invokeAll(c) cancels tasks not completed by timeout
1404     */
1405 jsr166 1.27 public void testTimedInvokeAll6() throws Exception {
1406     ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1407 dl 1.13 try {
1408     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1409     l.add(new StringTask());
1410 dl 1.14 l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1411 dl 1.16 l.add(new StringTask());
1412 jsr166 1.27 List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1413 dl 1.16 assertEquals(3, result.size());
1414 jsr166 1.24 Iterator<Future<String>> it = result.iterator();
1415 dl 1.13 Future<String> f1 = it.next();
1416     Future<String> f2 = it.next();
1417 dl 1.16 Future<String> f3 = it.next();
1418 dl 1.13 assertTrue(f1.isDone());
1419 dl 1.16 assertTrue(f2.isDone());
1420     assertTrue(f3.isDone());
1421 dl 1.13 assertFalse(f1.isCancelled());
1422     assertTrue(f2.isCancelled());
1423     } finally {
1424     joinPool(e);
1425     }
1426     }
1427    
1428 dl 1.19 /**
1429     * Execution continues if there is at least one thread even if
1430     * thread factory fails to create more
1431     */
1432 jsr166 1.27 public void testFailingThreadFactory() throws InterruptedException {
1433     ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1434 dl 1.19 try {
1435     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1436     for (int k = 0; k < 100; ++k) {
1437     e.execute(new NoOpRunnable());
1438     }
1439     Thread.sleep(LONG_DELAY_MS);
1440     } finally {
1441     joinPool(e);
1442     }
1443     }
1444 dl 1.21
1445     /**
1446     * allowsCoreThreadTimeOut is by default false.
1447     */
1448     public void testAllowsCoreThreadTimeOut() {
1449 jsr166 1.27 ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1450 dl 1.21 assertFalse(tpe.allowsCoreThreadTimeOut());
1451     joinPool(tpe);
1452     }
1453    
1454     /**
1455     * allowCoreThreadTimeOut(true) causes idle threads to time out
1456     */
1457 jsr166 1.27 public void testAllowCoreThreadTimeOut_true() throws InterruptedException {
1458     ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1459 dl 1.21 tpe.allowCoreThreadTimeOut(true);
1460     tpe.execute(new NoOpRunnable());
1461     try {
1462     Thread.sleep(MEDIUM_DELAY_MS);
1463     assertEquals(0, tpe.getPoolSize());
1464     } finally {
1465     joinPool(tpe);
1466     }
1467     }
1468    
1469     /**
1470     * allowCoreThreadTimeOut(false) causes idle threads not to time out
1471     */
1472 jsr166 1.27 public void testAllowCoreThreadTimeOut_false() throws InterruptedException {
1473     ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1474 dl 1.21 tpe.allowCoreThreadTimeOut(false);
1475     tpe.execute(new NoOpRunnable());
1476     try {
1477     Thread.sleep(MEDIUM_DELAY_MS);
1478     assertTrue(tpe.getPoolSize() >= 1);
1479     } finally {
1480     joinPool(tpe);
1481     }
1482     }
1483    
1484 dl 1.23 /**
1485     * execute allows the same task to be submitted multiple times, even
1486     * if rejected
1487     */
1488 jsr166 1.27 public void testRejectedRecycledTask() throws InterruptedException {
1489 dl 1.23 final int nTasks = 1000;
1490     final AtomicInteger nRun = new AtomicInteger(0);
1491     final Runnable recycledTask = new Runnable() {
1492     public void run() {
1493     nRun.getAndIncrement();
1494     } };
1495 jsr166 1.24 final ThreadPoolExecutor p =
1496     new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
1497 dl 1.23 new ArrayBlockingQueue(30));
1498     try {
1499     for (int i = 0; i < nTasks; ++i) {
1500     for (;;) {
1501     try {
1502     p.execute(recycledTask);
1503     break;
1504     }
1505     catch (RejectedExecutionException ignore) {
1506     }
1507     }
1508     }
1509     Thread.sleep(5000); // enough time to run all tasks
1510     assertEquals(nRun.get(), nTasks);
1511     } finally {
1512     p.shutdown();
1513     }
1514     }
1515 jsr166 1.24
1516 dl 1.1 }