ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.29
Committed: Fri Nov 20 16:02:10 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.28: +5 -3 lines
Log Message:
still fixing those flaky testInvokeAny3 tests

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