KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > thread > pool > ThreadPoolManagerTest


1 /*
2  * CoadunationLib: The coadunation core library.
3  * Copyright (C) 2007 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * ThreadPoolManagerTest.java
20  */

21
22 package com.rift.coad.lib.thread.pool;
23
24 import junit.framework.*;
25 import java.util.Vector JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Date JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.concurrent.atomic.AtomicInteger JavaDoc;
32 import java.net.URLClassLoader JavaDoc;
33 import java.net.URL JavaDoc;
34
35 import org.apache.log4j.Logger;
36 import org.apache.log4j.BasicConfigurator;
37
38 import com.rift.coad.lib.common.ClassUtil;
39 import com.rift.coad.lib.thread.CoadunationThread;
40 import com.rift.coad.lib.thread.ThreadStateMonitor;
41 import com.rift.coad.lib.security.UserSession;
42 import com.rift.coad.lib.security.user.UserSessionManager;
43 import com.rift.coad.lib.configuration.ConfigurationFactory;
44 import com.rift.coad.lib.configuration.Configuration;
45 import com.rift.coad.lib.security.user.UserStoreManager;
46 import com.rift.coad.lib.security.ThreadsPermissionContainer;
47 import com.rift.coad.lib.security.login.handlers.PasswordInfoHandler;
48 import com.rift.coad.lib.security.SessionManager;
49 import com.rift.coad.lib.security.RoleManager;
50 import com.rift.coad.lib.security.Validator;
51 import com.rift.coad.lib.security.login.LoginManager;
52 import com.rift.coad.lib.thread.CoadunationThread;
53 import com.rift.coad.lib.thread.CoadunationThreadGroup;
54 import com.rift.coad.lib.thread.ThreadGroupManager;
55 import com.rift.coad.lib.security.ThreadPermissionSession;
56
57 /**
58  * The test of the thread pool manager.
59  *
60  * @author Brett Chaldecott
61  */

62 public class ThreadPoolManagerTest extends TestCase {
63     
64     /**
65      * This object is called by the test task object.
66      */

67     public static class TestMonitor {
68         
69         private int waitCount = 0;
70         private int callCount = 0;
71         
72         /**
73          * This class is used to monitor the test
74          */

75         public TestMonitor() {
76             
77         }
78         
79         
80         /**
81          * This method is called to by the threads to wait indefinitly.
82          */

83         public synchronized void threadWait() {
84             waitCount++;
85             callCount++;
86             try {
87                 wait();
88             } catch (Exception JavaDoc ex) {
89                 System.out.println("Failed to wait : " + ex.getMessage());
90                 ex.printStackTrace(System.out);
91             }
92             waitCount--;
93         }
94         
95         
96         /**
97          * This returns the wait count
98          */

99         public synchronized int getWaitCount() {
100             return waitCount;
101         }
102         
103         
104         /**
105          * This method returns the call count
106          */

107         public synchronized int getCallCount() {
108             return callCount;
109         }
110         
111         /**
112          * This method resets the called count
113          */

114         public synchronized void resetCalledCount() {
115             callCount = 0;
116         }
117         
118         
119         /**
120          * This method is called to release all waiting threads
121          */

122         public synchronized void notifyAllWaitingThreads() {
123             notifyAll();
124         }
125     }
126     
127     
128     
129     /**
130      * This class is responsible for processing as a task.
131      */

132     public static class TestTask implements Task {
133         
134         /**
135          * The process method used by the task object.
136          */

137         public void process(ThreadPoolManager pool) throws Exception JavaDoc {
138             testMonitor.threadWait();
139         }
140         
141     }
142     
143     public static TestMonitor testMonitor = new TestMonitor();
144     
145     public ThreadPoolManagerTest(String JavaDoc testName) {
146         super(testName);
147         BasicConfigurator.configure();
148     }
149
150     protected void setUp() throws Exception JavaDoc {
151     }
152
153     protected void tearDown() throws Exception JavaDoc {
154     }
155
156     /**
157      * Test com.rift.coad.lib.thread.pool.ThreadPoolManager.
158      */

159     public void testThreadPoolManager() throws Exception JavaDoc {
160         System.out.println("ThreadPoolManager");
161         
162         // initialize the thread permissions
163
ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
164         SessionManager.init(permissions);
165         UserStoreManager userStoreManager = new UserStoreManager();
166         UserSessionManager sessionManager = new UserSessionManager(permissions,
167                 userStoreManager);
168         LoginManager.init(sessionManager,userStoreManager);
169         
170         // add a user to the session for the current thread
171
RoleManager.getInstance();
172         
173         // instanciate the thread manager
174
CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(
175                 sessionManager,userStoreManager);
176         ClassLoader JavaDoc loader = new URLClassLoader JavaDoc(new URL JavaDoc[0],
177                 this.getClass().getClassLoader());
178         Thread.currentThread().setContextClassLoader(loader);
179         ThreadGroupManager.getInstance().initThreadGroup(threadGroup);
180         
181         // add a new user object and add to the permission
182
Set JavaDoc set = new HashSet JavaDoc();
183         set.add("test");
184         UserSession user = new UserSession("test1", set);
185         permissions.putSession(new Long JavaDoc(Thread.currentThread().getId()),
186                 new ThreadPermissionSession(
187                 new Long JavaDoc(Thread.currentThread().getId()),user));
188         
189         // start the thread pool
190
ThreadPoolManager instance1 = new ThreadPoolManager(2, TestTask.class,
191                 "test");
192         ThreadPoolManager instance2 = new ThreadPoolManager(3,5, TestTask.class,
193                 "test");
194         
195         assertEquals(2, instance1.getSize());
196         instance1.setSize(3);
197         assertEquals(3, instance1.getSize());
198         assertEquals(3, instance2.getMinSize());
199         instance2.setMinSize(4);
200         assertEquals(4, instance2.getMinSize());
201         try {
202             instance2.setMinSize(6);
203             fail("Should not be able to set the pool size to greater than max.");
204         } catch (PoolException ex) {
205             // ignore correct
206
}
207         assertEquals(5, instance2.getMaxSize());
208         try {
209             instance2.setMaxSize(3);
210             fail("Should not be able to set the pool size max to smaller " +
211                     "than max.");
212         } catch (PoolException ex) {
213             // ignore correct
214
}
215         
216         assertEquals(2,testMonitor.getWaitCount());
217         assertEquals(2,testMonitor.getCallCount());
218         
219         instance1.releaseThread();
220         instance2.releaseThread();
221         Thread.sleep(500);
222         
223         assertEquals(4,testMonitor.getCallCount());
224         assertEquals(4,testMonitor.getWaitCount());
225         
226         instance1.releaseThread();
227         instance1.releaseThread();
228         instance2.releaseThread();
229         instance2.releaseThread();
230         instance2.releaseThread();
231         instance2.releaseThread();
232         Thread.sleep(500);
233         
234         assertEquals(8,testMonitor.getCallCount());
235         assertEquals(8,testMonitor.getWaitCount());
236         
237         testMonitor.notifyAllWaitingThreads();
238         
239         instance1.releaseThread();
240         instance1.releaseThread();
241         instance2.releaseThread();
242         instance2.releaseThread();
243         Thread.sleep(500);
244         
245         assertEquals(6,testMonitor.getWaitCount());
246         assertEquals(14,testMonitor.getCallCount());
247         
248         instance1.terminate();
249         instance2.terminate();
250     }
251
252 }
253
Popular Tags