ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.9
Committed: Sun Oct 5 23:55:03 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.8: +40 -0 lines
Log Message:
test null-check setRejectedExecutionHandler

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by members of JCP JSR-166 Expert Group and released to the
3     * public domain. Use, modify, and redistribute this code in any way
4     * without acknowledgement. Other contributors include Andrew Wright,
5     * Jeffrey Hayes, Pat Fischer, Mike Judd.
6     */
7    
8     import java.util.concurrent.*;
9     import junit.framework.*;
10     import java.util.List;
11    
12 dl 1.3 public class ThreadPoolExecutorTest extends JSR166TestCase {
13 dl 1.1 public static void main(String[] args) {
14     junit.textui.TestRunner.run (suite());
15     }
16     public static Test suite() {
17     return new TestSuite(ThreadPoolExecutorTest.class);
18     }
19    
20 dl 1.8 static class ExtendedTPE extends ThreadPoolExecutor {
21     volatile boolean beforeCalled = false;
22     volatile boolean afterCalled = false;
23     volatile boolean terminatedCalled = false;
24     public ExtendedTPE() {
25     super(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>());
26     }
27     protected void beforeExecute(Thread t, Runnable r) {
28     beforeCalled = true;
29     }
30     protected void afterExecute(Runnable r, Throwable t) {
31     afterCalled = true;
32     }
33     protected void terminated() {
34     terminatedCalled = true;
35     }
36     }
37 dl 1.1
38 dl 1.3 /**
39 dl 1.8 * execute successfully executes a runnable
40 dl 1.1 */
41 dl 1.5 public void testExecute() {
42 dl 1.8 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
43 dl 1.1 try {
44 dl 1.5 p1.execute(new Runnable() {
45     public void run() {
46     try {
47 dl 1.1 Thread.sleep(SHORT_DELAY_MS);
48 dl 1.2 } catch(InterruptedException e){
49 dl 1.5 threadUnexpectedException();
50 dl 1.1 }
51     }
52     });
53 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
54 dl 1.2 } catch(InterruptedException e){
55 dl 1.5 unexpectedException();
56 dl 1.3 }
57 dl 1.5 joinPool(p1);
58 dl 1.1 }
59    
60     /**
61 dl 1.6 * getActiveCount increases but doesn't overestimate, when a
62     * thread becomes active
63 dl 1.1 */
64 dl 1.5 public void testGetActiveCount() {
65 dl 1.8 ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
66 dl 1.5 assertEquals(0, p2.getActiveCount());
67     p2.execute(new MediumRunnable());
68     try {
69 dl 1.3 Thread.sleep(SHORT_DELAY_MS);
70     } catch(Exception e){
71 dl 1.5 unexpectedException();
72 dl 1.1 }
73 dl 1.5 assertEquals(1, p2.getActiveCount());
74     joinPool(p2);
75 dl 1.1 }
76 dl 1.8
77     /**
78     * prestartCoreThread starts a thread if under corePoolSize, else doesn't
79     */
80     public void testPrestartCoreThread() {
81     ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
82     assertEquals(0, p2.getPoolSize());
83     assertTrue(p2.prestartCoreThread());
84     assertEquals(1, p2.getPoolSize());
85     assertTrue(p2.prestartCoreThread());
86     assertEquals(2, p2.getPoolSize());
87     assertFalse(p2.prestartCoreThread());
88     assertEquals(2, p2.getPoolSize());
89     joinPool(p2);
90     }
91    
92     /**
93     * prestartAllCoreThreads starts all corePoolSize threads
94     */
95     public void testPrestartAllCoreThreads() {
96     ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
97     assertEquals(0, p2.getPoolSize());
98     p2.prestartAllCoreThreads();
99     assertEquals(2, p2.getPoolSize());
100     p2.prestartAllCoreThreads();
101     assertEquals(2, p2.getPoolSize());
102     joinPool(p2);
103     }
104 dl 1.1
105     /**
106 dl 1.6 * getCompletedTaskCount increases, but doesn't overestimate,
107     * when tasks complete
108 dl 1.1 */
109 dl 1.5 public void testGetCompletedTaskCount() {
110 dl 1.8 ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
111 dl 1.5 assertEquals(0, p2.getCompletedTaskCount());
112     p2.execute(new ShortRunnable());
113     try {
114 dl 1.8 Thread.sleep(SMALL_DELAY_MS);
115 dl 1.3 } catch(Exception e){
116 dl 1.5 unexpectedException();
117 dl 1.1 }
118 dl 1.5 assertEquals(1, p2.getCompletedTaskCount());
119     p2.shutdown();
120     joinPool(p2);
121 dl 1.1 }
122    
123     /**
124 dl 1.6 * getCorePoolSize returns size given in constructor if not otherwise set
125 dl 1.1 */
126 dl 1.5 public void testGetCorePoolSize() {
127 dl 1.8 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
128 dl 1.5 assertEquals(1, p1.getCorePoolSize());
129     joinPool(p1);
130 dl 1.1 }
131    
132     /**
133 dl 1.6 * getKeepAliveTime returns value given in constructor if not otherwise set
134 dl 1.1 */
135 dl 1.5 public void testGetKeepAliveTime() {
136     ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
137     assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS));
138     joinPool(p2);
139 dl 1.1 }
140 dl 1.8
141    
142     /**
143     * getThreadFactory returns factory in constructor if not set
144     */
145     public void testGetThreadFactory() {
146     ThreadFactory tf = new SimpleThreadFactory();
147     ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
148     assertSame(tf, p.getThreadFactory());
149     p.shutdown();
150     joinPool(p);
151     }
152    
153     /**
154     * setThreadFactory sets the thread factory returned by getThreadFactory
155     */
156     public void testSetThreadFactory() {
157     ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
158     ThreadFactory tf = new SimpleThreadFactory();
159     p.setThreadFactory(tf);
160     assertSame(tf, p.getThreadFactory());
161     p.shutdown();
162     joinPool(p);
163     }
164    
165    
166     /**
167     * setThreadFactory(null) throws NPE
168     */
169     public void testSetThreadFactoryNull() {
170     ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
171     try {
172     p.setThreadFactory(null);
173     shouldThrow();
174     } catch (NullPointerException success) {
175     } finally {
176     joinPool(p);
177     }
178     }
179    
180 dl 1.9
181     /**
182     * getRejectedExecutionHandler returns handler in constructor if not set
183     */
184     public void testGetRejectedExecutionHandler() {
185     RejectedExecutionHandler h = new NoOpREHandler();
186     ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
187     assertSame(h, p.getRejectedExecutionHandler());
188     p.shutdown();
189     joinPool(p);
190     }
191    
192     /**
193     * setRejectedExecutionHandler sets the handler returned by
194     * getRejectedExecutionHandler
195     */
196     public void testSetRejectedExecutionHandler() {
197     ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
198     RejectedExecutionHandler h = new NoOpREHandler();
199     p.setRejectedExecutionHandler(h);
200     assertSame(h, p.getRejectedExecutionHandler());
201     p.shutdown();
202     joinPool(p);
203     }
204    
205    
206     /**
207     * setRejectedExecutionHandler(null) throws NPE
208     */
209     public void testSetRejectedExecutionHandlerNull() {
210     ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
211     try {
212     p.setRejectedExecutionHandler(null);
213     shouldThrow();
214     } catch (NullPointerException success) {
215     } finally {
216     joinPool(p);
217     }
218     }
219    
220 dl 1.1
221     /**
222 dl 1.6 * getLargestPoolSize increases, but doesn't overestimate, when
223     * multiple threads active
224 dl 1.1 */
225 dl 1.5 public void testGetLargestPoolSize() {
226 dl 1.8 ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
227 dl 1.1 try {
228 dl 1.5 assertEquals(0, p2.getLargestPoolSize());
229     p2.execute(new MediumRunnable());
230     p2.execute(new MediumRunnable());
231 dl 1.3 Thread.sleep(SHORT_DELAY_MS);
232 dl 1.5 assertEquals(2, p2.getLargestPoolSize());
233 dl 1.3 } catch(Exception e){
234 dl 1.5 unexpectedException();
235 dl 1.3 }
236 dl 1.5 joinPool(p2);
237 dl 1.1 }
238    
239     /**
240 dl 1.6 * getMaximumPoolSize returns value given in constructor if not
241     * otherwise set
242 dl 1.1 */
243 dl 1.5 public void testGetMaximumPoolSize() {
244 dl 1.8 ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
245 dl 1.5 assertEquals(2, p2.getMaximumPoolSize());
246     joinPool(p2);
247 dl 1.1 }
248    
249     /**
250 dl 1.6 * getPoolSize increases, but doesn't overestimate, when threads
251     * become active
252 dl 1.1 */
253 dl 1.5 public void testGetPoolSize() {
254 dl 1.8 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
255 dl 1.5 assertEquals(0, p1.getPoolSize());
256     p1.execute(new MediumRunnable());
257     assertEquals(1, p1.getPoolSize());
258     joinPool(p1);
259 dl 1.1 }
260    
261     /**
262 dl 1.8 * getTaskCount increases, but doesn't overestimate, when tasks submitted
263 dl 1.1 */
264 dl 1.5 public void testGetTaskCount() {
265 dl 1.8 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
266 dl 1.1 try {
267 dl 1.5 assertEquals(0, p1.getTaskCount());
268     p1.execute(new MediumRunnable());
269 dl 1.3 Thread.sleep(SHORT_DELAY_MS);
270 dl 1.5 assertEquals(1, p1.getTaskCount());
271 dl 1.3 } catch(Exception e){
272 dl 1.5 unexpectedException();
273 dl 1.3 }
274 dl 1.5 joinPool(p1);
275 dl 1.1 }
276    
277     /**
278 dl 1.6 * isShutDown is false before shutdown, true after
279 dl 1.1 */
280 dl 1.5 public void testIsShutdown() {
281 dl 1.1
282 dl 1.8 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
283 dl 1.5 assertFalse(p1.isShutdown());
284     p1.shutdown();
285     assertTrue(p1.isShutdown());
286     joinPool(p1);
287 dl 1.1 }
288    
289    
290     /**
291 dl 1.6 * isTerminated is false before termination, true after
292 dl 1.1 */
293 dl 1.5 public void testIsTerminated() {
294 dl 1.8 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
295 dl 1.6 assertFalse(p1.isTerminated());
296 dl 1.1 try {
297 dl 1.5 p1.execute(new MediumRunnable());
298 dl 1.1 } finally {
299 dl 1.5 p1.shutdown();
300 dl 1.1 }
301 dl 1.3 try {
302 dl 1.5 assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
303     assertTrue(p1.isTerminated());
304 dl 1.3 } catch(Exception e){
305 dl 1.5 unexpectedException();
306     }
307     }
308    
309     /**
310 dl 1.6 * isTerminating is not true when running or when terminated
311 dl 1.5 */
312     public void testIsTerminating() {
313 dl 1.8 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
314 dl 1.5 assertFalse(p1.isTerminating());
315     try {
316     p1.execute(new SmallRunnable());
317     assertFalse(p1.isTerminating());
318     } finally {
319     p1.shutdown();
320     }
321     try {
322     assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
323     assertTrue(p1.isTerminated());
324     assertFalse(p1.isTerminating());
325     } catch(Exception e){
326     unexpectedException();
327 dl 1.3 }
328 dl 1.1 }
329    
330     /**
331 dl 1.8 * getQueue returns the work queue, which contains queued tasks
332     */
333     public void testGetQueue() {
334     BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
335     ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q);
336     CancellableTask[] tasks = new CancellableTask[5];
337     for(int i = 0; i < 5; i++){
338     tasks[i] = new CancellableTask(new MediumPossiblyInterruptedRunnable());
339     p1.execute(tasks[i]);
340     }
341     try {
342     Thread.sleep(SHORT_DELAY_MS);
343     BlockingQueue<Runnable> wq = p1.getQueue();
344     assertSame(q, wq);
345     assertFalse(wq.contains(tasks[0]));
346     assertTrue(wq.contains(tasks[4]));
347     p1.shutdownNow();
348     } catch(Exception e) {
349     unexpectedException();
350     } finally {
351     joinPool(p1);
352     }
353     }
354    
355     /**
356     * remove(task) removes queued task, and fails to remove active task
357     */
358     public void testRemove() {
359     BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
360     ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q);
361     CancellableTask[] tasks = new CancellableTask[5];
362     for(int i = 0; i < 5; i++){
363     tasks[i] = new CancellableTask(new MediumPossiblyInterruptedRunnable());
364     p1.execute(tasks[i]);
365     }
366     try {
367     Thread.sleep(SHORT_DELAY_MS);
368     assertFalse(p1.remove(tasks[0]));
369     assertTrue(q.contains(tasks[4]));
370     assertTrue(q.contains(tasks[3]));
371     assertTrue(p1.remove(tasks[4]));
372     assertFalse(p1.remove(tasks[4]));
373     assertFalse(q.contains(tasks[4]));
374     assertTrue(q.contains(tasks[3]));
375     assertTrue(p1.remove(tasks[3]));
376     assertFalse(q.contains(tasks[3]));
377     p1.shutdownNow();
378     } catch(Exception e) {
379     unexpectedException();
380     } finally {
381     joinPool(p1);
382     }
383     }
384    
385     /**
386 dl 1.6 * purge removes cancelled tasks from the queue
387 dl 1.1 */
388 dl 1.5 public void testPurge() {
389 dl 1.8 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
390 dl 1.3 CancellableTask[] tasks = new CancellableTask[5];
391     for(int i = 0; i < 5; i++){
392     tasks[i] = new CancellableTask(new MediumPossiblyInterruptedRunnable());
393 dl 1.5 p1.execute(tasks[i]);
394 dl 1.3 }
395     tasks[4].cancel(true);
396     tasks[3].cancel(true);
397 dl 1.5 p1.purge();
398     long count = p1.getTaskCount();
399 dl 1.3 assertTrue(count >= 2 && count < 5);
400 dl 1.8 p1.shutdownNow();
401 dl 1.5 joinPool(p1);
402 dl 1.1 }
403    
404     /**
405 dl 1.6 * shutDownNow returns a list containing tasks that were not run
406 dl 1.1 */
407 dl 1.5 public void testShutDownNow() {
408 dl 1.8 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
409 dl 1.1 List l;
410     try {
411     for(int i = 0; i < 5; i++)
412 dl 1.5 p1.execute(new MediumPossiblyInterruptedRunnable());
413 dl 1.1 }
414     finally {
415 dl 1.5 l = p1.shutdownNow();
416 dl 1.1 }
417 dl 1.5 assertTrue(p1.isShutdown());
418 dl 1.1 assertTrue(l.size() <= 4);
419     }
420    
421     // Exception Tests
422    
423    
424 dl 1.6 /**
425     * Constructor throws if corePoolSize argument is less than zero
426     */
427 dl 1.1 public void testConstructor1() {
428 dl 1.5 try {
429 dl 1.3 new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
430 dl 1.5 shouldThrow();
431 dl 1.1 }
432 dl 1.3 catch (IllegalArgumentException success){}
433 dl 1.1 }
434    
435 dl 1.6 /**
436     * Constructor throws if maximumPoolSize is less than zero
437     */
438 dl 1.1 public void testConstructor2() {
439 dl 1.5 try {
440 dl 1.3 new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
441 dl 1.5 shouldThrow();
442 dl 1.1 }
443 dl 1.3 catch (IllegalArgumentException success){}
444 dl 1.1 }
445    
446 dl 1.6 /**
447     * Constructor throws if maximumPoolSize is equal to zero
448     */
449 dl 1.1 public void testConstructor3() {
450 dl 1.5 try {
451 dl 1.3 new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
452 dl 1.5 shouldThrow();
453 dl 1.1 }
454 dl 1.3 catch (IllegalArgumentException success){}
455 dl 1.1 }
456    
457 dl 1.6 /**
458     * Constructor throws if keepAliveTime is less than zero
459     */
460 dl 1.1 public void testConstructor4() {
461 dl 1.5 try {
462 dl 1.1 new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
463 dl 1.5 shouldThrow();
464 dl 1.1 }
465 dl 1.3 catch (IllegalArgumentException success){}
466 dl 1.1 }
467    
468 dl 1.6 /**
469     * Constructor throws if corePoolSize is greater than the maximumPoolSize
470     */
471 dl 1.1 public void testConstructor5() {
472 dl 1.5 try {
473 dl 1.3 new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
474 dl 1.5 shouldThrow();
475 dl 1.1 }
476 dl 1.3 catch (IllegalArgumentException success){}
477 dl 1.1 }
478    
479 dl 1.6 /**
480     * Constructor throws if workQueue is set to null
481     */
482 dl 1.8 public void testConstructorNullPointerException() {
483 dl 1.5 try {
484 dl 1.8 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null);
485 dl 1.5 shouldThrow();
486 dl 1.1 }
487 dl 1.3 catch (NullPointerException success){}
488 dl 1.1 }
489    
490    
491    
492 dl 1.6 /**
493     * Constructor throws if corePoolSize argument is less than zero
494     */
495 dl 1.1 public void testConstructor6() {
496 dl 1.5 try {
497 dl 1.7 new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
498 dl 1.5 shouldThrow();
499 dl 1.3 } catch (IllegalArgumentException success){}
500 dl 1.1 }
501    
502 dl 1.6 /**
503     * Constructor throws if maximumPoolSize is less than zero
504     */
505 dl 1.1 public void testConstructor7() {
506 dl 1.5 try {
507 dl 1.8 new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
508 dl 1.5 shouldThrow();
509 dl 1.1 }
510 dl 1.3 catch (IllegalArgumentException success){}
511 dl 1.1 }
512    
513 dl 1.6 /**
514     * Constructor throws if maximumPoolSize is equal to zero
515     */
516 dl 1.1 public void testConstructor8() {
517 dl 1.5 try {
518 dl 1.8 new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
519 dl 1.5 shouldThrow();
520 dl 1.1 }
521 dl 1.3 catch (IllegalArgumentException success){}
522 dl 1.1 }
523    
524 dl 1.6 /**
525     * Constructor throws if keepAliveTime is less than zero
526     */
527 dl 1.1 public void testConstructor9() {
528 dl 1.5 try {
529 dl 1.7 new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
530 dl 1.5 shouldThrow();
531 dl 1.1 }
532 dl 1.3 catch (IllegalArgumentException success){}
533 dl 1.1 }
534    
535 dl 1.6 /**
536     * Constructor throws if corePoolSize is greater than the maximumPoolSize
537     */
538 dl 1.1 public void testConstructor10() {
539 dl 1.5 try {
540 dl 1.8 new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
541 dl 1.5 shouldThrow();
542 dl 1.1 }
543 dl 1.3 catch (IllegalArgumentException success){}
544 dl 1.1 }
545    
546 dl 1.6 /**
547     * Constructor throws if workQueue is set to null
548     */
549 dl 1.8 public void testConstructorNullPointerException2() {
550 dl 1.5 try {
551 dl 1.8 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory());
552 dl 1.5 shouldThrow();
553 dl 1.1 }
554 dl 1.3 catch (NullPointerException success){}
555 dl 1.1 }
556    
557 dl 1.6 /**
558     * Constructor throws if threadFactory is set to null
559     */
560 dl 1.8 public void testConstructorNullPointerException3() {
561 dl 1.5 try {
562 dl 1.1 ThreadFactory f = null;
563 dl 1.8 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
564 dl 1.5 shouldThrow();
565 dl 1.1 }
566 dl 1.3 catch (NullPointerException success){}
567 dl 1.1 }
568    
569    
570 dl 1.6 /**
571     * Constructor throws if corePoolSize argument is less than zero
572     */
573 dl 1.1 public void testConstructor11() {
574 dl 1.5 try {
575 dl 1.8 new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
576 dl 1.5 shouldThrow();
577 dl 1.1 }
578 dl 1.3 catch (IllegalArgumentException success){}
579 dl 1.1 }
580    
581 dl 1.6 /**
582     * Constructor throws if maximumPoolSize is less than zero
583     */
584 dl 1.1 public void testConstructor12() {
585 dl 1.5 try {
586 dl 1.8 new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
587 dl 1.5 shouldThrow();
588 dl 1.1 }
589 dl 1.3 catch (IllegalArgumentException success){}
590 dl 1.1 }
591    
592 dl 1.6 /**
593     * Constructor throws if maximumPoolSize is equal to zero
594     */
595 dl 1.1 public void testConstructor13() {
596 dl 1.5 try {
597 dl 1.8 new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
598 dl 1.5 shouldThrow();
599 dl 1.1 }
600 dl 1.3 catch (IllegalArgumentException success){}
601 dl 1.1 }
602    
603 dl 1.6 /**
604     * Constructor throws if keepAliveTime is less than zero
605     */
606 dl 1.1 public void testConstructor14() {
607 dl 1.5 try {
608 dl 1.7 new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
609 dl 1.5 shouldThrow();
610 dl 1.1 }
611 dl 1.3 catch (IllegalArgumentException success){}
612 dl 1.1 }
613    
614 dl 1.6 /**
615     * Constructor throws if corePoolSize is greater than the maximumPoolSize
616     */
617 dl 1.1 public void testConstructor15() {
618 dl 1.5 try {
619 dl 1.8 new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
620 dl 1.5 shouldThrow();
621 dl 1.1 }
622 dl 1.3 catch (IllegalArgumentException success){}
623 dl 1.1 }
624    
625 dl 1.6 /**
626     * Constructor throws if workQueue is set to null
627     */
628 dl 1.8 public void testConstructorNullPointerException4() {
629 dl 1.5 try {
630 dl 1.8 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler());
631 dl 1.5 shouldThrow();
632 dl 1.1 }
633 dl 1.3 catch (NullPointerException success){}
634 dl 1.1 }
635    
636 dl 1.6 /**
637     * Constructor throws if handler is set to null
638     */
639 dl 1.8 public void testConstructorNullPointerException5() {
640 dl 1.5 try {
641 dl 1.1 RejectedExecutionHandler r = null;
642 dl 1.8 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
643 dl 1.5 shouldThrow();
644 dl 1.1 }
645 dl 1.3 catch (NullPointerException success){}
646 dl 1.1 }
647    
648    
649 dl 1.6 /**
650     * Constructor throws if corePoolSize argument is less than zero
651     */
652 dl 1.1 public void testConstructor16() {
653 dl 1.5 try {
654 dl 1.8 new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
655 dl 1.5 shouldThrow();
656 dl 1.1 }
657 dl 1.3 catch (IllegalArgumentException success){}
658 dl 1.1 }
659    
660 dl 1.6 /**
661     * Constructor throws if maximumPoolSize is less than zero
662     */
663 dl 1.1 public void testConstructor17() {
664 dl 1.5 try {
665 dl 1.8 new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
666 dl 1.5 shouldThrow();
667 dl 1.1 }
668 dl 1.3 catch (IllegalArgumentException success){}
669 dl 1.1 }
670    
671 dl 1.6 /**
672     * Constructor throws if maximumPoolSize is equal to zero
673     */
674 dl 1.1 public void testConstructor18() {
675 dl 1.5 try {
676 dl 1.8 new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
677 dl 1.5 shouldThrow();
678 dl 1.1 }
679 dl 1.3 catch (IllegalArgumentException success){}
680 dl 1.1 }
681    
682 dl 1.6 /**
683     * Constructor throws if keepAliveTime is less than zero
684     */
685 dl 1.1 public void testConstructor19() {
686 dl 1.5 try {
687 dl 1.7 new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
688 dl 1.5 shouldThrow();
689 dl 1.1 }
690 dl 1.3 catch (IllegalArgumentException success){}
691 dl 1.1 }
692    
693 dl 1.6 /**
694     * Constructor throws if corePoolSize is greater than the maximumPoolSize
695     */
696 dl 1.1 public void testConstructor20() {
697 dl 1.5 try {
698 dl 1.8 new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
699 dl 1.5 shouldThrow();
700 dl 1.1 }
701 dl 1.3 catch (IllegalArgumentException success){}
702 dl 1.1 }
703    
704 dl 1.6 /**
705     * Constructor throws if workQueue is set to null
706     */
707 dl 1.8 public void testConstructorNullPointerException6() {
708 dl 1.5 try {
709 dl 1.8 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
710 dl 1.5 shouldThrow();
711 dl 1.1 }
712 dl 1.3 catch (NullPointerException success){}
713 dl 1.1 }
714    
715 dl 1.6 /**
716     * Constructor throws if handler is set to null
717     */
718 dl 1.8 public void testConstructorNullPointerException7() {
719 dl 1.5 try {
720 dl 1.1 RejectedExecutionHandler r = null;
721 dl 1.8 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
722 dl 1.5 shouldThrow();
723 dl 1.1 }
724 dl 1.3 catch (NullPointerException success){}
725 dl 1.1 }
726    
727 dl 1.6 /**
728     * Constructor throws if ThreadFactory is set top null
729     */
730 dl 1.8 public void testConstructorNullPointerException8() {
731 dl 1.5 try {
732 dl 1.1 ThreadFactory f = null;
733 dl 1.8 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
734 dl 1.5 shouldThrow();
735 dl 1.1 }
736 dl 1.3 catch (NullPointerException successdn8){}
737 dl 1.1 }
738    
739    
740     /**
741 dl 1.8 * execute throws RejectedExecutionException
742     * if saturated.
743     */
744     public void testSaturatedExecute() {
745     ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
746     try {
747    
748     for(int i = 0; i < 5; ++i){
749     p.execute(new MediumRunnable());
750     }
751     shouldThrow();
752     } catch(RejectedExecutionException success){}
753     joinPool(p);
754     }
755    
756     /**
757     * executor using CallerRunsPolicy runs task if saturated.
758     */
759     public void testSaturatedExecute2() {
760     RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
761     ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
762     try {
763    
764     TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
765     for(int i = 0; i < 5; ++i){
766     tasks[i] = new TrackedNoOpRunnable();
767     }
768     TrackedLongRunnable mr = new TrackedLongRunnable();
769     p.execute(mr);
770     for(int i = 0; i < 5; ++i){
771     p.execute(tasks[i]);
772     }
773     for(int i = 1; i < 5; ++i) {
774     assertTrue(tasks[i].done);
775     }
776     p.shutdownNow();
777     } catch(RejectedExecutionException ex){
778     unexpectedException();
779     } finally {
780     joinPool(p);
781     }
782     }
783    
784     /**
785     * executor using DiscardPolicy drops task if saturated.
786     */
787     public void testSaturatedExecute3() {
788     RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
789     ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
790     try {
791    
792     TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
793     for(int i = 0; i < 5; ++i){
794     tasks[i] = new TrackedNoOpRunnable();
795     }
796     p.execute(new TrackedLongRunnable());
797     for(int i = 0; i < 5; ++i){
798     p.execute(tasks[i]);
799     }
800     for(int i = 0; i < 5; ++i){
801     assertFalse(tasks[i].done);
802     }
803     p.shutdownNow();
804     } catch(RejectedExecutionException ex){
805     unexpectedException();
806     } finally {
807     joinPool(p);
808     }
809     }
810    
811     /**
812     * executor using DiscardOldestPolicy drops oldest task if saturated.
813     */
814     public void testSaturatedExecute4() {
815     RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
816     ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
817     try {
818     p.execute(new TrackedLongRunnable());
819     TrackedLongRunnable r2 = new TrackedLongRunnable();
820     p.execute(r2);
821     assertTrue(p.getQueue().contains(r2));
822     TrackedNoOpRunnable r3 = new TrackedNoOpRunnable();
823     p.execute(r3);
824     assertFalse(p.getQueue().contains(r2));
825     assertTrue(p.getQueue().contains(r3));
826     p.shutdownNow();
827     } catch(RejectedExecutionException ex){
828     unexpectedException();
829     } finally {
830     joinPool(p);
831     }
832     }
833    
834     /**
835 dl 1.6 * execute throws RejectedExecutionException if shutdown
836 dl 1.1 */
837 dl 1.8 public void testRejectedExecutionExceptionOnShutdown() {
838     ThreadPoolExecutor tpe =
839     new ThreadPoolExecutor(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
840 dl 1.1 tpe.shutdown();
841 dl 1.5 try {
842 dl 1.3 tpe.execute(new NoOpRunnable());
843 dl 1.5 shouldThrow();
844 dl 1.2 } catch(RejectedExecutionException success){}
845 dl 1.1
846 dl 1.3 joinPool(tpe);
847 dl 1.1 }
848 dl 1.6
849     /**
850 dl 1.8 * execute using CallerRunsPolicy drops task on shutdown
851     */
852     public void testCallerRunsOnShutdown() {
853     RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
854     ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
855    
856     p.shutdown();
857     try {
858     TrackedNoOpRunnable r = new TrackedNoOpRunnable();
859     p.execute(r);
860     assertFalse(r.done);
861     } catch(RejectedExecutionException success){
862     unexpectedException();
863     } finally {
864     joinPool(p);
865     }
866     }
867    
868     /**
869     * execute using DiscardPolicy drops task on shutdown
870     */
871     public void testDiscardOnShutdown() {
872     RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
873     ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
874    
875     p.shutdown();
876     try {
877     TrackedNoOpRunnable r = new TrackedNoOpRunnable();
878     p.execute(r);
879     assertFalse(r.done);
880     } catch(RejectedExecutionException success){
881     unexpectedException();
882     } finally {
883     joinPool(p);
884     }
885     }
886    
887    
888     /**
889     * execute using DiscardOldestPolicy drops task on shutdown
890 dl 1.6 */
891 dl 1.8 public void testDiscardOldestOnShutdown() {
892     RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
893     ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
894    
895     p.shutdown();
896     try {
897     TrackedNoOpRunnable r = new TrackedNoOpRunnable();
898     p.execute(r);
899     assertFalse(r.done);
900     } catch(RejectedExecutionException success){
901     unexpectedException();
902     } finally {
903     joinPool(p);
904     }
905 dl 1.6 }
906    
907 dl 1.8
908 dl 1.6 /**
909     * execute (null) throws NPE
910     */
911     public void testExecuteNull() {
912     ThreadPoolExecutor tpe = null;
913     try {
914 dl 1.8 tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
915 dl 1.6 tpe.execute(null);
916     shouldThrow();
917     } catch(NullPointerException success){}
918    
919     joinPool(tpe);
920     }
921 dl 1.1
922     /**
923 dl 1.8 * setCorePoolSize of negative value throws IllegalArgumentException
924 dl 1.1 */
925 dl 1.5 public void testCorePoolSizeIllegalArgumentException() {
926 dl 1.1 ThreadPoolExecutor tpe = null;
927 dl 1.5 try {
928 dl 1.8 tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
929 dl 1.2 } catch(Exception e){}
930 dl 1.5 try {
931 dl 1.1 tpe.setCorePoolSize(-1);
932 dl 1.5 shouldThrow();
933 dl 1.2 } catch(IllegalArgumentException success){
934 dl 1.1 } finally {
935     tpe.shutdown();
936     }
937 dl 1.3 joinPool(tpe);
938 dl 1.1 }
939    
940     /**
941 dl 1.6 * setMaximumPoolSize(int) throws IllegalArgumentException if
942     * given a value less the core pool size
943 dl 1.1 */
944 dl 1.5 public void testMaximumPoolSizeIllegalArgumentException() {
945 dl 1.1 ThreadPoolExecutor tpe = null;
946 dl 1.5 try {
947 dl 1.8 tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
948 dl 1.2 } catch(Exception e){}
949 dl 1.5 try {
950 dl 1.1 tpe.setMaximumPoolSize(1);
951 dl 1.5 shouldThrow();
952 dl 1.2 } catch(IllegalArgumentException success){
953 dl 1.1 } finally {
954     tpe.shutdown();
955     }
956 dl 1.3 joinPool(tpe);
957 dl 1.1 }
958    
959     /**
960 dl 1.6 * setMaximumPoolSize throws IllegalArgumentException
961     * if given a negative value
962 dl 1.1 */
963 dl 1.5 public void testMaximumPoolSizeIllegalArgumentException2() {
964 dl 1.1 ThreadPoolExecutor tpe = null;
965 dl 1.5 try {
966 dl 1.8 tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
967 dl 1.2 } catch(Exception e){}
968 dl 1.5 try {
969 dl 1.1 tpe.setMaximumPoolSize(-1);
970 dl 1.5 shouldThrow();
971 dl 1.2 } catch(IllegalArgumentException success){
972 dl 1.1 } finally {
973     tpe.shutdown();
974     }
975 dl 1.3 joinPool(tpe);
976 dl 1.1 }
977    
978    
979     /**
980 dl 1.6 * setKeepAliveTime throws IllegalArgumentException
981 dl 1.1 * when given a negative value
982     */
983 dl 1.5 public void testKeepAliveTimeIllegalArgumentException() {
984 dl 1.1 ThreadPoolExecutor tpe = null;
985 dl 1.5 try {
986 dl 1.8 tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
987 dl 1.2 } catch(Exception e){}
988 dl 1.1
989 dl 1.5 try {
990 dl 1.1 tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS);
991 dl 1.5 shouldThrow();
992 dl 1.2 } catch(IllegalArgumentException success){
993 dl 1.1 } finally {
994     tpe.shutdown();
995     }
996 dl 1.3 joinPool(tpe);
997 dl 1.1 }
998 dl 1.8
999     /**
1000     * terminated() is called on termination
1001     */
1002     public void testTerminated() {
1003     ExtendedTPE tpe = new ExtendedTPE();
1004     tpe.shutdown();
1005     assertTrue(tpe.terminatedCalled);
1006     joinPool(tpe);
1007     }
1008    
1009     /**
1010     * beforeExecute and afterExecute are called when executing task
1011     */
1012     public void testBeforeAfter() {
1013     ExtendedTPE tpe = new ExtendedTPE();
1014     try {
1015     TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1016     tpe.execute(r);
1017     Thread.sleep(SHORT_DELAY_MS);
1018     assertTrue(r.done);
1019     assertTrue(tpe.beforeCalled);
1020     assertTrue(tpe.afterCalled);
1021     tpe.shutdown();
1022     }
1023     catch(Exception ex) {
1024     unexpectedException();
1025     } finally {
1026     joinPool(tpe);
1027     }
1028     }
1029 dl 1.1 }