ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.1
Committed: Sun Aug 31 19:24:56 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Log Message:
First check-in of tests to be in tck

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     public class ThreadPoolExecutorTest extends TestCase{
13     public static void main(String[] args) {
14     junit.textui.TestRunner.run (suite());
15     }
16    
17    
18     public static Test suite() {
19     return new TestSuite(ThreadPoolExecutorTest.class);
20     }
21    
22     private static long SHORT_DELAY_MS = 100;
23     private static long MEDIUM_DELAY_MS = 1000;
24     private static long LONG_DELAY_MS = 10000;
25    
26     //---- testThread class to implement ThreadFactory for use in constructors
27     static class testThread implements ThreadFactory{
28     public Thread newThread(Runnable r){
29     return new Thread(r);
30     }
31     }
32    
33     //---- testReject class to implement RejectedExecutionHandler for use in the constructors
34     static class testReject implements RejectedExecutionHandler{
35     public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){}
36     }
37    
38     public Runnable newRunnable(){
39     return new Runnable(){
40     public void run(){
41     try{Thread.sleep(MEDIUM_DELAY_MS);
42     } catch(Exception e){
43     }
44     }
45     };
46     }
47    
48    
49     /**
50     * Test to verify that execute successfully executes a runnable
51     */
52     public void testExecute(){
53     ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
54     try {
55     one.execute(new Runnable(){
56     public void run(){
57     try{
58     Thread.sleep(SHORT_DELAY_MS);
59     }catch(InterruptedException e){
60     fail("unexpected exception");
61     }
62     }
63     });
64     Thread.sleep(SHORT_DELAY_MS * 2);
65     }catch(InterruptedException e){
66     fail("unexpected exception");
67     } finally {
68     one.shutdown();
69     }
70     }
71    
72     /**
73     * Test to verify getActiveCount gives correct values
74     */
75     public void testGetActiveCount(){
76     ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
77     try {
78     assertEquals(0, two.getActiveCount());
79     two.execute(newRunnable());
80     try{Thread.sleep(10);}catch(Exception e){}
81     assertEquals(1, two.getActiveCount());
82     } finally {
83     two.shutdown();
84     }
85     }
86    
87     /**
88     * Test to verify getCompleteTaskCount gives correct values
89     */
90     public void testGetCompletedTaskCount(){
91     ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
92     try {
93     assertEquals(0, two.getCompletedTaskCount());
94     two.execute(newRunnable());
95     try{Thread.sleep(2000);}catch(Exception e){}
96     assertEquals(1, two.getCompletedTaskCount());
97     } finally {
98     two.shutdown();
99     }
100     }
101    
102     /**
103     * Test to verify getCorePoolSize gives correct values
104     */
105     public void testGetCorePoolSize(){
106     ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
107     try {
108     assertEquals(1, one.getCorePoolSize());
109     } finally {
110     one.shutdown();
111     }
112     }
113    
114     /**
115     * Test to verify getKeepAliveTime gives correct values
116     */
117     public void testGetKeepAliveTime(){
118     ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
119     try {
120     assertEquals(1, two.getKeepAliveTime(TimeUnit.SECONDS));
121     } finally {
122     two.shutdown();
123     }
124     }
125    
126     /**
127     * Test to verify getLargestPoolSize gives correct values
128     */
129     public void testGetLargestPoolSize(){
130     ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
131     try {
132     assertEquals(0, two.getLargestPoolSize());
133     two.execute(newRunnable());
134     two.execute(newRunnable());
135     try{Thread.sleep(SHORT_DELAY_MS);} catch(Exception e){}
136     assertEquals(2, two.getLargestPoolSize());
137     } finally {
138     two.shutdown();
139     }
140     }
141    
142     /**
143     * Test to verify getMaximumPoolSize gives correct values
144     */
145     public void testGetMaximumPoolSize(){
146     ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
147     try {
148     assertEquals(2, two.getMaximumPoolSize());
149     } finally {
150     two.shutdown();
151     }
152     }
153    
154     /**
155     * Test to verify getPoolSize gives correct values
156     */
157     public void testGetPoolSize(){
158     ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
159     try {
160     assertEquals(0, one.getPoolSize());
161     one.execute(newRunnable());
162     assertEquals(1, one.getPoolSize());
163     } finally {
164     one.shutdown();
165     }
166     }
167    
168     /**
169     * Test to verify getTaskCount gives correct values
170     */
171     public void testGetTaskCount(){
172     ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
173     try {
174     assertEquals(0, one.getTaskCount());
175     for(int i = 0; i < 5; i++)
176     one.execute(newRunnable());
177     try{Thread.sleep(SHORT_DELAY_MS);}catch(Exception e){}
178     assertEquals(5, one.getTaskCount());
179     } finally {
180     one.shutdown();
181     }
182     }
183    
184     /**
185     * Test to verify isShutDown gives correct values
186     */
187     public void testIsShutdown(){
188    
189     ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
190     try {
191     assertFalse(one.isShutdown());
192     }
193     finally {
194     one.shutdown();
195     }
196     assertTrue(one.isShutdown());
197     }
198    
199    
200     /**
201     * Test to verify isTerminated gives correct values
202     * Makes sure termination does not take an innapropriate
203     * amount of time
204     */
205     public void testIsTerminated(){
206     ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
207     try {
208     one.execute(newRunnable());
209     } finally {
210     one.shutdown();
211     }
212     boolean flag = false;
213     try{
214     flag = one.awaitTermination(10, TimeUnit.SECONDS);
215     }catch(Exception e){}
216     assertTrue(one.isTerminated());
217     if(!flag)
218     fail("ThreadPoolExecutor - thread pool did not terminate within suitable timeframe");
219     }
220    
221     /**
222     * Test to verify that purge correctly removes cancelled tasks
223     * from the queue
224     */
225     public void testPurge(){
226     ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
227     try {
228     CancellableTask[] tasks = new CancellableTask[5];
229     for(int i = 0; i < 5; i++){
230     tasks[i] = new CancellableTask(newRunnable());
231     one.execute(tasks[i]);
232     }
233     tasks[4].cancel(true);
234     tasks[3].cancel(true);
235     one.purge();
236     long count = one.getTaskCount();
237     assertTrue(count >= 2 && count < 5);
238     } finally {
239     one.shutdown();
240     }
241     }
242    
243     /**
244     * Test to verify shutDownNow returns a list
245     * containing the correct number of elements
246     */
247     public void testShutDownNow(){
248     ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
249     List l;
250     try {
251     for(int i = 0; i < 5; i++)
252     one.execute(newRunnable());
253     }
254     finally {
255     l = one.shutdownNow();
256     }
257     assertTrue(one.isShutdown());
258     assertTrue(l.size() <= 4);
259     }
260    
261    
262    
263    
264    
265    
266     // Exception Tests
267    
268    
269     //---- Tests if corePoolSize argument is less than zero
270     public void testConstructor1() {
271     try{
272     new ThreadPoolExecutor(-1,1,100L,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
273     fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");
274     }
275     catch (IllegalArgumentException i){}
276     }
277    
278     //---- Tests if maximumPoolSize is less than zero
279     public void testConstructor2() {
280     try{
281     new ThreadPoolExecutor(1,-1,100L,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
282     fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");
283     }
284     catch (IllegalArgumentException i2){}
285     }
286    
287     //---- Tests if maximumPoolSize is equal to zero
288     public void testConstructor3() {
289     try{
290     new ThreadPoolExecutor(1,0,100L,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
291     fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");
292     }
293     catch (IllegalArgumentException i3){}
294     }
295    
296     //---- Tests if keepAliveTime is less than zero
297     public void testConstructor4() {
298     try{
299     new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
300     fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");
301     }
302     catch (IllegalArgumentException i4){}
303     }
304    
305     //---- Tests if corePoolSize is greater than the maximumPoolSize
306     public void testConstructor5() {
307     try{
308     new ThreadPoolExecutor(2,1,100L,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
309     fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");
310     }
311     catch (IllegalArgumentException i5){}
312     }
313    
314     //---- Tests if workQueue is set to null
315     public void testNullPointerException() {
316     try{
317     new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,null);
318     fail("ThreadPoolExecutor constructor should throw a NullPointerException");
319     }
320     catch (NullPointerException n){}
321     }
322    
323    
324    
325     //---- Tests if corePoolSize argument is less than zero
326     public void testConstructor6() {
327     try{
328     new ThreadPoolExecutor(-1,1,100L,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread());
329     fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");
330     }catch (IllegalArgumentException i6){}
331     }
332    
333     //---- Tests if maximumPoolSize is less than zero
334     public void testConstructor7() {
335     try{
336     new ThreadPoolExecutor(1,-1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread());
337     fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");
338     }
339     catch (IllegalArgumentException i7){}
340     }
341    
342     //---- Tests if maximumPoolSize is equal to zero
343     public void testConstructor8() {
344     try{
345     new ThreadPoolExecutor(1,0,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread());
346     fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");
347     }
348     catch (IllegalArgumentException i8){}
349     }
350    
351     //---- Tests if keepAliveTime is less than zero
352     public void testConstructor9() {
353     try{
354     new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread());
355     fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");
356     }
357     catch (IllegalArgumentException i9){}
358     }
359    
360     //---- Tests if corePoolSize is greater than the maximumPoolSize
361     public void testConstructor10() {
362     try{
363     new ThreadPoolExecutor(2,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread());
364     fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");
365     }
366     catch (IllegalArgumentException i10){}
367     }
368    
369     //---- Tests if workQueue is set to null
370     public void testNullPointerException2() {
371     try{
372     new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,null,new testThread());
373     fail("ThreadPoolExecutor constructor should throw a NullPointerException");
374     }
375     catch (NullPointerException n2){}
376     }
377    
378     //---- Tests if threadFactory is set to null
379     public void testNullPointerException3() {
380     try{
381     ThreadFactory f = null;
382     new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
383     fail("ThreadPoolExecutor constructor should throw a NullPointerException");
384     }
385     catch (NullPointerException n3){}
386     }
387    
388    
389     //---- Tests if corePoolSize argument is less than zero
390     public void testConstructor11() {
391     try{
392     new ThreadPoolExecutor(-1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testReject());
393     fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");
394     }
395     catch (IllegalArgumentException i11){}
396     }
397    
398     //---- Tests if maximumPoolSize is less than zero
399     public void testConstructor12() {
400     try{
401     new ThreadPoolExecutor(1,-1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testReject());
402     fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");
403     }
404     catch (IllegalArgumentException i12){}
405     }
406    
407     //---- Tests if maximumPoolSize is equal to zero
408     public void testConstructor13() {
409     try{
410     new ThreadPoolExecutor(1,0,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testReject());
411     fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");
412     }
413     catch (IllegalArgumentException i13){}
414     }
415    
416     //---- Tests if keepAliveTime is less than zero
417     public void testConstructor14() {
418     try{
419     new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testReject());
420     fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");
421     }
422     catch (IllegalArgumentException i14){}
423     }
424    
425     //---- Tests if corePoolSize is greater than the maximumPoolSize
426     public void testConstructor15() {
427     try{
428     new ThreadPoolExecutor(2,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testReject());
429     fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");
430     }
431     catch (IllegalArgumentException i15){}
432     }
433    
434     //---- Tests if workQueue is set to null
435     public void testNullPointerException4() {
436     try{
437     new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,null,new testReject());
438     fail("ThreadPoolExecutor constructor should throw a NullPointerException");
439     }
440     catch (NullPointerException n4){}
441     }
442    
443     //---- Tests if handler is set to null
444     public void testNullPointerException5() {
445     try{
446     RejectedExecutionHandler r = null;
447     new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
448     fail("ThreadPoolExecutor constructor should throw a NullPointerException");
449     }
450     catch (NullPointerException n5){}
451     }
452    
453    
454     //---- Tests if corePoolSize argument is less than zero
455     public void testConstructor16() {
456     try{
457     new ThreadPoolExecutor(-1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread(),new testReject());
458     fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");
459     }
460     catch (IllegalArgumentException i16){}
461     }
462    
463     //---- Tests if maximumPoolSize is less than zero
464     public void testConstructor17() {
465     try{
466     new ThreadPoolExecutor(1,-1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread(),new testReject());
467     fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");
468     }
469     catch (IllegalArgumentException i17){}
470     }
471    
472     //---- Tests if maximumPoolSize is equal to zero
473     public void testConstructor18() {
474     try{
475     new ThreadPoolExecutor(1,0,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread(),new testReject());
476     fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");
477     }
478     catch (IllegalArgumentException i18){}
479     }
480    
481     //---- Tests if keepAliveTime is less than zero
482     public void testConstructor19() {
483     try{
484     new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread(),new testReject());
485     fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");
486     }
487     catch (IllegalArgumentException i19){}
488     }
489    
490     //---- Tests if corePoolSize is greater than the maximumPoolSize
491     public void testConstructor20() {
492     try{
493     new ThreadPoolExecutor(2,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread(),new testReject());
494     fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException");
495     }
496     catch (IllegalArgumentException i20){}
497     }
498    
499     //---- Tests if workQueue is set to null
500     public void testNullPointerException6() {
501     try{
502     new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,null,new testThread(),new testReject());
503     fail("ThreadPoolExecutor constructor should throw a NullPointerException");
504     }
505     catch (NullPointerException n6){}
506     }
507    
508     //---- Tests if handler is set to null
509     public void testNullPointerException7() {
510     try{
511     RejectedExecutionHandler r = null;
512     new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new testThread(),r);
513     fail("ThreadPoolExecutor constructor should throw a NullPointerException");
514     }
515     catch (NullPointerException n7){}
516     }
517    
518     //---- Tests if ThradFactory is set top null
519     public void testNullPointerException8() {
520     try{
521     ThreadFactory f = null;
522     new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new testReject());
523     fail("ThreadPoolExecutor constructor should throw a NullPointerException");
524     }
525     catch (NullPointerException n8){}
526     }
527    
528    
529     /**
530     * Test to verify execute will throw RejectedExcutionException
531     * ThreadPoolExecutor will throw one when more runnables are
532     * executed then will fit in the Queue.
533     */
534     public void testRejectedExecutedException(){
535     ThreadPoolExecutor tpe = null;
536     try{
537     tpe = new ThreadPoolExecutor(1,1,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
538     }catch(Exception e){}
539     tpe.shutdown();
540     try{
541     tpe.execute(new Runnable(){
542     public void run(){
543     try{
544     Thread.sleep(1000);
545     }catch(InterruptedException e){}
546     }
547     });
548     fail("ThreadPoolExecutor - void execute(Runnable) should throw RejectedExecutionException");
549     }catch(RejectedExecutionException sucess){}
550    
551    
552     }
553    
554     /**
555     * Test to verify setCorePoolSize will throw IllegalArgumentException
556     * when given a negative
557     */
558     public void testIllegalArgumentException1(){
559     ThreadPoolExecutor tpe = null;
560     try{
561     tpe = new ThreadPoolExecutor(1,2,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
562     }catch(Exception e){}
563     try{
564     tpe.setCorePoolSize(-1);
565     fail("ThreadPoolExecutor - void setCorePoolSize(int) should throw IllegalArgumentException");
566     }catch(IllegalArgumentException sucess){
567     } finally {
568     tpe.shutdown();
569     }
570     }
571    
572    
573     /**
574     * Test to verify setMaximumPoolSize(int) will throw IllegalArgumentException
575     * if given a value less the it's actual core pool size
576     */
577     public void testIllegalArgumentException2(){
578     ThreadPoolExecutor tpe = null;
579     try{
580     tpe = new ThreadPoolExecutor(2,3,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
581     }catch(Exception e){}
582     try{
583     tpe.setMaximumPoolSize(1);
584     fail("ThreadPoolExecutor - void setMaximumPoolSize(int) should throw IllegalArgumentException");
585     }catch(IllegalArgumentException sucess){
586     } finally {
587     tpe.shutdown();
588     }
589     }
590    
591     /**
592     * Test to verify that setMaximumPoolSize will throw IllegalArgumentException
593     * if given a negative number
594     */
595     public void testIllegalArgumentException2SP(){
596     ThreadPoolExecutor tpe = null;
597     try{
598     tpe = new ThreadPoolExecutor(2,3,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
599     }catch(Exception e){}
600     try{
601     tpe.setMaximumPoolSize(-1);
602     fail("ThreadPoolExecutor - void setMaximumPoolSize(int) should throw IllegalArgumentException");
603     }catch(IllegalArgumentException sucess){
604     } finally {
605     tpe.shutdown();
606     }
607     }
608    
609    
610     /**
611     * Test to verify setKeepAliveTime will throw IllegalArgumentException
612     * when given a negative value
613     */
614     public void testIllegalArgumentException3(){
615     ThreadPoolExecutor tpe = null;
616     try{
617     tpe = new ThreadPoolExecutor(2,3,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
618     }catch(Exception e){}
619    
620     try{
621     tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS);
622     fail("ThreadPoolExecutor - void setKeepAliveTime(long, TimeUnit) should throw IllegalArgumentException");
623     }catch(IllegalArgumentException sucess){
624     } finally {
625     tpe.shutdown();
626     }
627     }
628    
629    
630    
631     }