KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > thread > impl > test > ResourceLimitingThreadPoolTestCase


1 /*
2  * Copyright 2002-2004 Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.avalon.excalibur.thread.impl.test;
18
19 import junit.framework.TestCase;
20
21 import org.apache.avalon.excalibur.thread.impl.ResourceLimitingThreadPool;
22
23 /**
24  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
25  * @version CVS $Revision: 1.5 $ $Date: 2004/03/29 17:22:49 $
26  * @since 4.1
27  */

28 public final class ResourceLimitingThreadPoolTestCase
29     extends TestCase
30 {
31     private volatile int m_completeCount;
32
33     /*---------------------------------------------------------------
34      * Constructors
35      *-------------------------------------------------------------*/

36     public ResourceLimitingThreadPoolTestCase()
37     {
38         this( "ResourceLimitingThreadPool Test Case" );
39     }
40
41     public ResourceLimitingThreadPoolTestCase( final String JavaDoc name )
42     {
43         super( name );
44     }
45
46     /*---------------------------------------------------------------
47      * Suite
48      *-------------------------------------------------------------*/

49     public void test1Worker1Task()
50     {
51         commonTest( 1, 1, 0L, 200L, 1, true, true, -1, -1 );
52     }
53
54     public void test1Worker5Tasks()
55     {
56         // One will start immediately, 4 will have to wait 200ms each in turn.
57
commonTest( 5, 1, 800L, 1000L, 1, true, true, -1, -1 );
58     }
59
60     public void test5Workers10Tasks()
61     {
62         // 5 will start immediately, 5 will have to wait 200ms for the first 5 to complete.
63
commonTest( 10, 5, 200L, 400L, 5, true, true, -1, -1 );
64     }
65
66     public void test10Workers100Tasks()
67     {
68         // 10 will start immediately, next 10 will have to wait 200ms for the
69
// first 10 to complete and so on.
70
commonTest( 100, 10, 1800L, 2000L, 10, true, true, -1, -1 );
71     }
72
73     public void test5Workers6TasksNoBlock()
74     {
75         commonTest( 6, 5, 0L, 200L, 5, true, false, -1, -1 );
76     }
77
78     public void test5Workers10TasksNotStrict()
79     {
80         commonTest( 10, 10, 0L, 200L, 5, false, false, -1, -1 );
81     }
82
83     protected void incCompleteCount()
84     {
85         synchronized( this )
86         {
87             m_completeCount++;
88         }
89     }
90
91     private void commonTest( int taskCount,
92                              int firstSize,
93                              long firstTime,
94                              long totalTime,
95                              int max,
96                              boolean maxStrict,
97                              boolean blocking,
98                              long blockTimeout,
99                              long trimInterval )
100     {
101         BufferedLogger logger = new BufferedLogger();
102         ResourceLimitingThreadPool pool = new ResourceLimitingThreadPool(
103             "Test Worker Pool", max, maxStrict, blocking, blockTimeout, trimInterval );
104         pool.enableLogging( logger );
105
106         Runnable JavaDoc runner = new Runnable JavaDoc()
107         {
108             public void run()
109             {
110                 try
111                 {
112                     Thread.sleep( 200 );
113                 }
114                 catch( InterruptedException JavaDoc e )
115                 {
116                 }
117
118                 incCompleteCount();
119             }
120         };
121
122         long start = System.currentTimeMillis();
123         m_completeCount = 0;
124         for( int i = 0; i < taskCount; i++ )
125         {
126             if( maxStrict && ( !blocking ) && i >= max )
127             {
128                 // This request shoudl throw an exception.
129
try
130                 {
131                     pool.execute( runner );
132                     fail( "Should have failed when requesting more than max resources." );
133                 }
134                 catch( Exception JavaDoc e )
135                 {
136                     // Ok
137
incCompleteCount();
138                 }
139             }
140             else
141             {
142                 pool.execute( runner );
143             }
144         }
145         long dur = System.currentTimeMillis() - start;
146
147         // Make sure that the size of the pool is what is expected.
148
assertEquals( "The pool size was not what it should be.", firstSize, pool.getSize() );
149
150         // Make sure this took about the right amount of time to get here.
151
//System.out.println( "First time: " + dur );
152
if( Math.abs( dur - firstTime ) > 500 )
153         {
154             fail( "Time to start all tasks, " + dur +
155                   ", was not within 500ms of the expected time, " + firstTime );
156         }
157
158         // Wait for all worker threads to complete.
159
while( m_completeCount < taskCount )
160         {
161             try
162             {
163                 Thread.sleep( 10 );
164             }
165             catch( InterruptedException JavaDoc e )
166             {
167             }
168         }
169
170         dur = System.currentTimeMillis() - start;
171
172         // Make sure this took about the right amount of time to get here.
173
//System.out.println( "Total time: " + dur );
174
if( Math.abs( dur - totalTime ) > 500 )
175         {
176             fail( "Time to complete all tasks, " + dur +
177                   ", was not within 500ms of the expected time, " + totalTime );
178         }
179
180         //System.out.println( logger.toString() );
181
}
182 }
183
184
Popular Tags