ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.21
Committed: Fri Dec 31 14:52:40 2004 UTC (19 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.20: +44 -0 lines
Log Message:
Add ThreadPoolExecutor.allowCoreThreadTimeOut

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