ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.8
Committed: Sun Oct 5 23:00:40 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.7: +373 -67 lines
Log Message:
Added tests and documentation

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.1
181     /**
182 dl 1.6 * getLargestPoolSize increases, but doesn't overestimate, when
183     * multiple threads active
184 dl 1.1 */
185 dl 1.5 public void testGetLargestPoolSize() {
186 dl 1.8 ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
187 dl 1.1 try {
188 dl 1.5 assertEquals(0, p2.getLargestPoolSize());
189     p2.execute(new MediumRunnable());
190     p2.execute(new MediumRunnable());
191 dl 1.3 Thread.sleep(SHORT_DELAY_MS);
192 dl 1.5 assertEquals(2, p2.getLargestPoolSize());
193 dl 1.3 } catch(Exception e){
194 dl 1.5 unexpectedException();
195 dl 1.3 }
196 dl 1.5 joinPool(p2);
197 dl 1.1 }
198    
199     /**
200 dl 1.6 * getMaximumPoolSize returns value given in constructor if not
201     * otherwise set
202 dl 1.1 */
203 dl 1.5 public void testGetMaximumPoolSize() {
204 dl 1.8 ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
205 dl 1.5 assertEquals(2, p2.getMaximumPoolSize());
206     joinPool(p2);
207 dl 1.1 }
208    
209     /**
210 dl 1.6 * getPoolSize increases, but doesn't overestimate, when threads
211     * become active
212 dl 1.1 */
213 dl 1.5 public void testGetPoolSize() {
214 dl 1.8 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
215 dl 1.5 assertEquals(0, p1.getPoolSize());
216     p1.execute(new MediumRunnable());
217     assertEquals(1, p1.getPoolSize());
218     joinPool(p1);
219 dl 1.1 }
220    
221     /**
222 dl 1.8 * getTaskCount increases, but doesn't overestimate, when tasks submitted
223 dl 1.1 */
224 dl 1.5 public void testGetTaskCount() {
225 dl 1.8 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
226 dl 1.1 try {
227 dl 1.5 assertEquals(0, p1.getTaskCount());
228     p1.execute(new MediumRunnable());
229 dl 1.3 Thread.sleep(SHORT_DELAY_MS);
230 dl 1.5 assertEquals(1, p1.getTaskCount());
231 dl 1.3 } catch(Exception e){
232 dl 1.5 unexpectedException();
233 dl 1.3 }
234 dl 1.5 joinPool(p1);
235 dl 1.1 }
236    
237     /**
238 dl 1.6 * isShutDown is false before shutdown, true after
239 dl 1.1 */
240 dl 1.5 public void testIsShutdown() {
241 dl 1.1
242 dl 1.8 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
243 dl 1.5 assertFalse(p1.isShutdown());
244     p1.shutdown();
245     assertTrue(p1.isShutdown());
246     joinPool(p1);
247 dl 1.1 }
248    
249    
250     /**
251 dl 1.6 * isTerminated is false before termination, true after
252 dl 1.1 */
253 dl 1.5 public void testIsTerminated() {
254 dl 1.8 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
255 dl 1.6 assertFalse(p1.isTerminated());
256 dl 1.1 try {
257 dl 1.5 p1.execute(new MediumRunnable());
258 dl 1.1 } finally {
259 dl 1.5 p1.shutdown();
260 dl 1.1 }
261 dl 1.3 try {
262 dl 1.5 assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
263     assertTrue(p1.isTerminated());
264 dl 1.3 } catch(Exception e){
265 dl 1.5 unexpectedException();
266     }
267     }
268    
269     /**
270 dl 1.6 * isTerminating is not true when running or when terminated
271 dl 1.5 */
272     public void testIsTerminating() {
273 dl 1.8 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
274 dl 1.5 assertFalse(p1.isTerminating());
275     try {
276     p1.execute(new SmallRunnable());
277     assertFalse(p1.isTerminating());
278     } finally {
279     p1.shutdown();
280     }
281     try {
282     assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
283     assertTrue(p1.isTerminated());
284     assertFalse(p1.isTerminating());
285     } catch(Exception e){
286     unexpectedException();
287 dl 1.3 }
288 dl 1.1 }
289    
290     /**
291 dl 1.8 * getQueue returns the work queue, which contains queued tasks
292     */
293     public void testGetQueue() {
294     BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
295     ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q);
296     CancellableTask[] tasks = new CancellableTask[5];
297     for(int i = 0; i < 5; i++){
298     tasks[i] = new CancellableTask(new MediumPossiblyInterruptedRunnable());
299     p1.execute(tasks[i]);
300     }
301     try {
302     Thread.sleep(SHORT_DELAY_MS);
303     BlockingQueue<Runnable> wq = p1.getQueue();
304     assertSame(q, wq);
305     assertFalse(wq.contains(tasks[0]));
306     assertTrue(wq.contains(tasks[4]));
307     p1.shutdownNow();
308     } catch(Exception e) {
309     unexpectedException();
310     } finally {
311     joinPool(p1);
312     }
313     }
314    
315     /**
316     * remove(task) removes queued task, and fails to remove active task
317     */
318     public void testRemove() {
319     BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
320     ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q);
321     CancellableTask[] tasks = new CancellableTask[5];
322     for(int i = 0; i < 5; i++){
323     tasks[i] = new CancellableTask(new MediumPossiblyInterruptedRunnable());
324     p1.execute(tasks[i]);
325     }
326     try {
327     Thread.sleep(SHORT_DELAY_MS);
328     assertFalse(p1.remove(tasks[0]));
329     assertTrue(q.contains(tasks[4]));
330     assertTrue(q.contains(tasks[3]));
331     assertTrue(p1.remove(tasks[4]));
332     assertFalse(p1.remove(tasks[4]));
333     assertFalse(q.contains(tasks[4]));
334     assertTrue(q.contains(tasks[3]));
335     assertTrue(p1.remove(tasks[3]));
336     assertFalse(q.contains(tasks[3]));
337     p1.shutdownNow();
338     } catch(Exception e) {
339     unexpectedException();
340     } finally {
341     joinPool(p1);
342     }
343     }
344    
345     /**
346 dl 1.6 * purge removes cancelled tasks from the queue
347 dl 1.1 */
348 dl 1.5 public void testPurge() {
349 dl 1.8 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
350 dl 1.3 CancellableTask[] tasks = new CancellableTask[5];
351     for(int i = 0; i < 5; i++){
352     tasks[i] = new CancellableTask(new MediumPossiblyInterruptedRunnable());
353 dl 1.5 p1.execute(tasks[i]);
354 dl 1.3 }
355     tasks[4].cancel(true);
356     tasks[3].cancel(true);
357 dl 1.5 p1.purge();
358     long count = p1.getTaskCount();
359 dl 1.3 assertTrue(count >= 2 && count < 5);
360 dl 1.8 p1.shutdownNow();
361 dl 1.5 joinPool(p1);
362 dl 1.1 }
363    
364     /**
365 dl 1.6 * shutDownNow returns a list containing tasks that were not run
366 dl 1.1 */
367 dl 1.5 public void testShutDownNow() {
368 dl 1.8 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
369 dl 1.1 List l;
370     try {
371     for(int i = 0; i < 5; i++)
372 dl 1.5 p1.execute(new MediumPossiblyInterruptedRunnable());
373 dl 1.1 }
374     finally {
375 dl 1.5 l = p1.shutdownNow();
376 dl 1.1 }
377 dl 1.5 assertTrue(p1.isShutdown());
378 dl 1.1 assertTrue(l.size() <= 4);
379     }
380    
381     // Exception Tests
382    
383    
384 dl 1.6 /**
385     * Constructor throws if corePoolSize argument is less than zero
386     */
387 dl 1.1 public void testConstructor1() {
388 dl 1.5 try {
389 dl 1.3 new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
390 dl 1.5 shouldThrow();
391 dl 1.1 }
392 dl 1.3 catch (IllegalArgumentException success){}
393 dl 1.1 }
394    
395 dl 1.6 /**
396     * Constructor throws if maximumPoolSize is less than zero
397     */
398 dl 1.1 public void testConstructor2() {
399 dl 1.5 try {
400 dl 1.3 new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
401 dl 1.5 shouldThrow();
402 dl 1.1 }
403 dl 1.3 catch (IllegalArgumentException success){}
404 dl 1.1 }
405    
406 dl 1.6 /**
407     * Constructor throws if maximumPoolSize is equal to zero
408     */
409 dl 1.1 public void testConstructor3() {
410 dl 1.5 try {
411 dl 1.3 new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
412 dl 1.5 shouldThrow();
413 dl 1.1 }
414 dl 1.3 catch (IllegalArgumentException success){}
415 dl 1.1 }
416    
417 dl 1.6 /**
418     * Constructor throws if keepAliveTime is less than zero
419     */
420 dl 1.1 public void testConstructor4() {
421 dl 1.5 try {
422 dl 1.1 new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
423 dl 1.5 shouldThrow();
424 dl 1.1 }
425 dl 1.3 catch (IllegalArgumentException success){}
426 dl 1.1 }
427    
428 dl 1.6 /**
429     * Constructor throws if corePoolSize is greater than the maximumPoolSize
430     */
431 dl 1.1 public void testConstructor5() {
432 dl 1.5 try {
433 dl 1.3 new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
434 dl 1.5 shouldThrow();
435 dl 1.1 }
436 dl 1.3 catch (IllegalArgumentException success){}
437 dl 1.1 }
438    
439 dl 1.6 /**
440     * Constructor throws if workQueue is set to null
441     */
442 dl 1.8 public void testConstructorNullPointerException() {
443 dl 1.5 try {
444 dl 1.8 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null);
445 dl 1.5 shouldThrow();
446 dl 1.1 }
447 dl 1.3 catch (NullPointerException success){}
448 dl 1.1 }
449    
450    
451    
452 dl 1.6 /**
453     * Constructor throws if corePoolSize argument is less than zero
454     */
455 dl 1.1 public void testConstructor6() {
456 dl 1.5 try {
457 dl 1.7 new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
458 dl 1.5 shouldThrow();
459 dl 1.3 } catch (IllegalArgumentException success){}
460 dl 1.1 }
461    
462 dl 1.6 /**
463     * Constructor throws if maximumPoolSize is less than zero
464     */
465 dl 1.1 public void testConstructor7() {
466 dl 1.5 try {
467 dl 1.8 new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
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 maximumPoolSize is equal to zero
475     */
476 dl 1.1 public void testConstructor8() {
477 dl 1.5 try {
478 dl 1.8 new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
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 keepAliveTime is less than zero
486     */
487 dl 1.1 public void testConstructor9() {
488 dl 1.5 try {
489 dl 1.7 new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
490 dl 1.5 shouldThrow();
491 dl 1.1 }
492 dl 1.3 catch (IllegalArgumentException success){}
493 dl 1.1 }
494    
495 dl 1.6 /**
496     * Constructor throws if corePoolSize is greater than the maximumPoolSize
497     */
498 dl 1.1 public void testConstructor10() {
499 dl 1.5 try {
500 dl 1.8 new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
501 dl 1.5 shouldThrow();
502 dl 1.1 }
503 dl 1.3 catch (IllegalArgumentException success){}
504 dl 1.1 }
505    
506 dl 1.6 /**
507     * Constructor throws if workQueue is set to null
508     */
509 dl 1.8 public void testConstructorNullPointerException2() {
510 dl 1.5 try {
511 dl 1.8 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory());
512 dl 1.5 shouldThrow();
513 dl 1.1 }
514 dl 1.3 catch (NullPointerException success){}
515 dl 1.1 }
516    
517 dl 1.6 /**
518     * Constructor throws if threadFactory is set to null
519     */
520 dl 1.8 public void testConstructorNullPointerException3() {
521 dl 1.5 try {
522 dl 1.1 ThreadFactory f = null;
523 dl 1.8 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
524 dl 1.5 shouldThrow();
525 dl 1.1 }
526 dl 1.3 catch (NullPointerException success){}
527 dl 1.1 }
528    
529    
530 dl 1.6 /**
531     * Constructor throws if corePoolSize argument is less than zero
532     */
533 dl 1.1 public void testConstructor11() {
534 dl 1.5 try {
535 dl 1.8 new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
536 dl 1.5 shouldThrow();
537 dl 1.1 }
538 dl 1.3 catch (IllegalArgumentException success){}
539 dl 1.1 }
540    
541 dl 1.6 /**
542     * Constructor throws if maximumPoolSize is less than zero
543     */
544 dl 1.1 public void testConstructor12() {
545 dl 1.5 try {
546 dl 1.8 new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
547 dl 1.5 shouldThrow();
548 dl 1.1 }
549 dl 1.3 catch (IllegalArgumentException success){}
550 dl 1.1 }
551    
552 dl 1.6 /**
553     * Constructor throws if maximumPoolSize is equal to zero
554     */
555 dl 1.1 public void testConstructor13() {
556 dl 1.5 try {
557 dl 1.8 new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
558 dl 1.5 shouldThrow();
559 dl 1.1 }
560 dl 1.3 catch (IllegalArgumentException success){}
561 dl 1.1 }
562    
563 dl 1.6 /**
564     * Constructor throws if keepAliveTime is less than zero
565     */
566 dl 1.1 public void testConstructor14() {
567 dl 1.5 try {
568 dl 1.7 new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
569 dl 1.5 shouldThrow();
570 dl 1.1 }
571 dl 1.3 catch (IllegalArgumentException success){}
572 dl 1.1 }
573    
574 dl 1.6 /**
575     * Constructor throws if corePoolSize is greater than the maximumPoolSize
576     */
577 dl 1.1 public void testConstructor15() {
578 dl 1.5 try {
579 dl 1.8 new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
580 dl 1.5 shouldThrow();
581 dl 1.1 }
582 dl 1.3 catch (IllegalArgumentException success){}
583 dl 1.1 }
584    
585 dl 1.6 /**
586     * Constructor throws if workQueue is set to null
587     */
588 dl 1.8 public void testConstructorNullPointerException4() {
589 dl 1.5 try {
590 dl 1.8 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler());
591 dl 1.5 shouldThrow();
592 dl 1.1 }
593 dl 1.3 catch (NullPointerException success){}
594 dl 1.1 }
595    
596 dl 1.6 /**
597     * Constructor throws if handler is set to null
598     */
599 dl 1.8 public void testConstructorNullPointerException5() {
600 dl 1.5 try {
601 dl 1.1 RejectedExecutionHandler r = null;
602 dl 1.8 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
603 dl 1.5 shouldThrow();
604 dl 1.1 }
605 dl 1.3 catch (NullPointerException success){}
606 dl 1.1 }
607    
608    
609 dl 1.6 /**
610     * Constructor throws if corePoolSize argument is less than zero
611     */
612 dl 1.1 public void testConstructor16() {
613 dl 1.5 try {
614 dl 1.8 new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
615 dl 1.5 shouldThrow();
616 dl 1.1 }
617 dl 1.3 catch (IllegalArgumentException success){}
618 dl 1.1 }
619    
620 dl 1.6 /**
621     * Constructor throws if maximumPoolSize is less than zero
622     */
623 dl 1.1 public void testConstructor17() {
624 dl 1.5 try {
625 dl 1.8 new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
626 dl 1.5 shouldThrow();
627 dl 1.1 }
628 dl 1.3 catch (IllegalArgumentException success){}
629 dl 1.1 }
630    
631 dl 1.6 /**
632     * Constructor throws if maximumPoolSize is equal to zero
633     */
634 dl 1.1 public void testConstructor18() {
635 dl 1.5 try {
636 dl 1.8 new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
637 dl 1.5 shouldThrow();
638 dl 1.1 }
639 dl 1.3 catch (IllegalArgumentException success){}
640 dl 1.1 }
641    
642 dl 1.6 /**
643     * Constructor throws if keepAliveTime is less than zero
644     */
645 dl 1.1 public void testConstructor19() {
646 dl 1.5 try {
647 dl 1.7 new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
648 dl 1.5 shouldThrow();
649 dl 1.1 }
650 dl 1.3 catch (IllegalArgumentException success){}
651 dl 1.1 }
652    
653 dl 1.6 /**
654     * Constructor throws if corePoolSize is greater than the maximumPoolSize
655     */
656 dl 1.1 public void testConstructor20() {
657 dl 1.5 try {
658 dl 1.8 new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
659 dl 1.5 shouldThrow();
660 dl 1.1 }
661 dl 1.3 catch (IllegalArgumentException success){}
662 dl 1.1 }
663    
664 dl 1.6 /**
665     * Constructor throws if workQueue is set to null
666     */
667 dl 1.8 public void testConstructorNullPointerException6() {
668 dl 1.5 try {
669 dl 1.8 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
670 dl 1.5 shouldThrow();
671 dl 1.1 }
672 dl 1.3 catch (NullPointerException success){}
673 dl 1.1 }
674    
675 dl 1.6 /**
676     * Constructor throws if handler is set to null
677     */
678 dl 1.8 public void testConstructorNullPointerException7() {
679 dl 1.5 try {
680 dl 1.1 RejectedExecutionHandler r = null;
681 dl 1.8 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
682 dl 1.5 shouldThrow();
683 dl 1.1 }
684 dl 1.3 catch (NullPointerException success){}
685 dl 1.1 }
686    
687 dl 1.6 /**
688     * Constructor throws if ThreadFactory is set top null
689     */
690 dl 1.8 public void testConstructorNullPointerException8() {
691 dl 1.5 try {
692 dl 1.1 ThreadFactory f = null;
693 dl 1.8 new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
694 dl 1.5 shouldThrow();
695 dl 1.1 }
696 dl 1.3 catch (NullPointerException successdn8){}
697 dl 1.1 }
698    
699    
700     /**
701 dl 1.8 * execute throws RejectedExecutionException
702     * if saturated.
703     */
704     public void testSaturatedExecute() {
705     ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
706     try {
707    
708     for(int i = 0; i < 5; ++i){
709     p.execute(new MediumRunnable());
710     }
711     shouldThrow();
712     } catch(RejectedExecutionException success){}
713     joinPool(p);
714     }
715    
716     /**
717     * executor using CallerRunsPolicy runs task if saturated.
718     */
719     public void testSaturatedExecute2() {
720     RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
721     ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
722     try {
723    
724     TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
725     for(int i = 0; i < 5; ++i){
726     tasks[i] = new TrackedNoOpRunnable();
727     }
728     TrackedLongRunnable mr = new TrackedLongRunnable();
729     p.execute(mr);
730     for(int i = 0; i < 5; ++i){
731     p.execute(tasks[i]);
732     }
733     for(int i = 1; i < 5; ++i) {
734     assertTrue(tasks[i].done);
735     }
736     p.shutdownNow();
737     } catch(RejectedExecutionException ex){
738     unexpectedException();
739     } finally {
740     joinPool(p);
741     }
742     }
743    
744     /**
745     * executor using DiscardPolicy drops task if saturated.
746     */
747     public void testSaturatedExecute3() {
748     RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
749     ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
750     try {
751    
752     TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
753     for(int i = 0; i < 5; ++i){
754     tasks[i] = new TrackedNoOpRunnable();
755     }
756     p.execute(new TrackedLongRunnable());
757     for(int i = 0; i < 5; ++i){
758     p.execute(tasks[i]);
759     }
760     for(int i = 0; i < 5; ++i){
761     assertFalse(tasks[i].done);
762     }
763     p.shutdownNow();
764     } catch(RejectedExecutionException ex){
765     unexpectedException();
766     } finally {
767     joinPool(p);
768     }
769     }
770    
771     /**
772     * executor using DiscardOldestPolicy drops oldest task if saturated.
773     */
774     public void testSaturatedExecute4() {
775     RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
776     ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
777     try {
778     p.execute(new TrackedLongRunnable());
779     TrackedLongRunnable r2 = new TrackedLongRunnable();
780     p.execute(r2);
781     assertTrue(p.getQueue().contains(r2));
782     TrackedNoOpRunnable r3 = new TrackedNoOpRunnable();
783     p.execute(r3);
784     assertFalse(p.getQueue().contains(r2));
785     assertTrue(p.getQueue().contains(r3));
786     p.shutdownNow();
787     } catch(RejectedExecutionException ex){
788     unexpectedException();
789     } finally {
790     joinPool(p);
791     }
792     }
793    
794     /**
795 dl 1.6 * execute throws RejectedExecutionException if shutdown
796 dl 1.1 */
797 dl 1.8 public void testRejectedExecutionExceptionOnShutdown() {
798     ThreadPoolExecutor tpe =
799     new ThreadPoolExecutor(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
800 dl 1.1 tpe.shutdown();
801 dl 1.5 try {
802 dl 1.3 tpe.execute(new NoOpRunnable());
803 dl 1.5 shouldThrow();
804 dl 1.2 } catch(RejectedExecutionException success){}
805 dl 1.1
806 dl 1.3 joinPool(tpe);
807 dl 1.1 }
808 dl 1.6
809     /**
810 dl 1.8 * execute using CallerRunsPolicy drops task on shutdown
811     */
812     public void testCallerRunsOnShutdown() {
813     RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
814     ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
815    
816     p.shutdown();
817     try {
818     TrackedNoOpRunnable r = new TrackedNoOpRunnable();
819     p.execute(r);
820     assertFalse(r.done);
821     } catch(RejectedExecutionException success){
822     unexpectedException();
823     } finally {
824     joinPool(p);
825     }
826     }
827    
828     /**
829     * execute using DiscardPolicy drops task on shutdown
830     */
831     public void testDiscardOnShutdown() {
832     RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
833     ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
834    
835     p.shutdown();
836     try {
837     TrackedNoOpRunnable r = new TrackedNoOpRunnable();
838     p.execute(r);
839     assertFalse(r.done);
840     } catch(RejectedExecutionException success){
841     unexpectedException();
842     } finally {
843     joinPool(p);
844     }
845     }
846    
847    
848     /**
849     * execute using DiscardOldestPolicy drops task on shutdown
850 dl 1.6 */
851 dl 1.8 public void testDiscardOldestOnShutdown() {
852     RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
853     ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
854    
855     p.shutdown();
856     try {
857     TrackedNoOpRunnable r = new TrackedNoOpRunnable();
858     p.execute(r);
859     assertFalse(r.done);
860     } catch(RejectedExecutionException success){
861     unexpectedException();
862     } finally {
863     joinPool(p);
864     }
865 dl 1.6 }
866    
867 dl 1.8
868 dl 1.6 /**
869     * execute (null) throws NPE
870     */
871     public void testExecuteNull() {
872     ThreadPoolExecutor tpe = null;
873     try {
874 dl 1.8 tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
875 dl 1.6 tpe.execute(null);
876     shouldThrow();
877     } catch(NullPointerException success){}
878    
879     joinPool(tpe);
880     }
881 dl 1.1
882     /**
883 dl 1.8 * setCorePoolSize of negative value throws IllegalArgumentException
884 dl 1.1 */
885 dl 1.5 public void testCorePoolSizeIllegalArgumentException() {
886 dl 1.1 ThreadPoolExecutor tpe = null;
887 dl 1.5 try {
888 dl 1.8 tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
889 dl 1.2 } catch(Exception e){}
890 dl 1.5 try {
891 dl 1.1 tpe.setCorePoolSize(-1);
892 dl 1.5 shouldThrow();
893 dl 1.2 } catch(IllegalArgumentException success){
894 dl 1.1 } finally {
895     tpe.shutdown();
896     }
897 dl 1.3 joinPool(tpe);
898 dl 1.1 }
899    
900     /**
901 dl 1.6 * setMaximumPoolSize(int) throws IllegalArgumentException if
902     * given a value less the core pool size
903 dl 1.1 */
904 dl 1.5 public void testMaximumPoolSizeIllegalArgumentException() {
905 dl 1.1 ThreadPoolExecutor tpe = null;
906 dl 1.5 try {
907 dl 1.8 tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
908 dl 1.2 } catch(Exception e){}
909 dl 1.5 try {
910 dl 1.1 tpe.setMaximumPoolSize(1);
911 dl 1.5 shouldThrow();
912 dl 1.2 } catch(IllegalArgumentException success){
913 dl 1.1 } finally {
914     tpe.shutdown();
915     }
916 dl 1.3 joinPool(tpe);
917 dl 1.1 }
918    
919     /**
920 dl 1.6 * setMaximumPoolSize throws IllegalArgumentException
921     * if given a negative value
922 dl 1.1 */
923 dl 1.5 public void testMaximumPoolSizeIllegalArgumentException2() {
924 dl 1.1 ThreadPoolExecutor tpe = null;
925 dl 1.5 try {
926 dl 1.8 tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
927 dl 1.2 } catch(Exception e){}
928 dl 1.5 try {
929 dl 1.1 tpe.setMaximumPoolSize(-1);
930 dl 1.5 shouldThrow();
931 dl 1.2 } catch(IllegalArgumentException success){
932 dl 1.1 } finally {
933     tpe.shutdown();
934     }
935 dl 1.3 joinPool(tpe);
936 dl 1.1 }
937    
938    
939     /**
940 dl 1.6 * setKeepAliveTime throws IllegalArgumentException
941 dl 1.1 * when given a negative value
942     */
943 dl 1.5 public void testKeepAliveTimeIllegalArgumentException() {
944 dl 1.1 ThreadPoolExecutor tpe = null;
945 dl 1.5 try {
946 dl 1.8 tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
947 dl 1.2 } catch(Exception e){}
948 dl 1.1
949 dl 1.5 try {
950 dl 1.1 tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS);
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 dl 1.8
959     /**
960     * terminated() is called on termination
961     */
962     public void testTerminated() {
963     ExtendedTPE tpe = new ExtendedTPE();
964     tpe.shutdown();
965     assertTrue(tpe.terminatedCalled);
966     joinPool(tpe);
967     }
968    
969     /**
970     * beforeExecute and afterExecute are called when executing task
971     */
972     public void testBeforeAfter() {
973     ExtendedTPE tpe = new ExtendedTPE();
974     try {
975     TrackedNoOpRunnable r = new TrackedNoOpRunnable();
976     tpe.execute(r);
977     Thread.sleep(SHORT_DELAY_MS);
978     assertTrue(r.done);
979     assertTrue(tpe.beforeCalled);
980     assertTrue(tpe.afterCalled);
981     tpe.shutdown();
982     }
983     catch(Exception ex) {
984     unexpectedException();
985     } finally {
986     joinPool(tpe);
987     }
988     }
989 dl 1.1 }