001: /*
002: * CoadunationLib: The coadunation core library.
003: * Copyright (C) 2007 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * ThreadPoolManagerTest.java
020: */
021:
022: package com.rift.coad.lib.thread.pool;
023:
024: import junit.framework.*;
025: import java.util.Vector;
026: import java.util.HashSet;
027: import java.util.List;
028: import java.util.Date;
029: import java.util.ArrayList;
030: import java.util.Set;
031: import java.util.concurrent.atomic.AtomicInteger;
032: import java.net.URLClassLoader;
033: import java.net.URL;
034:
035: import org.apache.log4j.Logger;
036: import org.apache.log4j.BasicConfigurator;
037:
038: import com.rift.coad.lib.common.ClassUtil;
039: import com.rift.coad.lib.thread.CoadunationThread;
040: import com.rift.coad.lib.thread.ThreadStateMonitor;
041: import com.rift.coad.lib.security.UserSession;
042: import com.rift.coad.lib.security.user.UserSessionManager;
043: import com.rift.coad.lib.configuration.ConfigurationFactory;
044: import com.rift.coad.lib.configuration.Configuration;
045: import com.rift.coad.lib.security.user.UserStoreManager;
046: import com.rift.coad.lib.security.ThreadsPermissionContainer;
047: import com.rift.coad.lib.security.login.handlers.PasswordInfoHandler;
048: import com.rift.coad.lib.security.SessionManager;
049: import com.rift.coad.lib.security.RoleManager;
050: import com.rift.coad.lib.security.Validator;
051: import com.rift.coad.lib.security.login.LoginManager;
052: import com.rift.coad.lib.thread.CoadunationThread;
053: import com.rift.coad.lib.thread.CoadunationThreadGroup;
054: import com.rift.coad.lib.thread.ThreadGroupManager;
055: import com.rift.coad.lib.security.ThreadPermissionSession;
056:
057: /**
058: * The test of the thread pool manager.
059: *
060: * @author Brett Chaldecott
061: */
062: public class ThreadPoolManagerTest extends TestCase {
063:
064: /**
065: * This object is called by the test task object.
066: */
067: public static class TestMonitor {
068:
069: private int waitCount = 0;
070: private int callCount = 0;
071:
072: /**
073: * This class is used to monitor the test
074: */
075: public TestMonitor() {
076:
077: }
078:
079: /**
080: * This method is called to by the threads to wait indefinitly.
081: */
082: public synchronized void threadWait() {
083: waitCount++;
084: callCount++;
085: try {
086: wait();
087: } catch (Exception ex) {
088: System.out.println("Failed to wait : "
089: + ex.getMessage());
090: ex.printStackTrace(System.out);
091: }
092: waitCount--;
093: }
094:
095: /**
096: * This returns the wait count
097: */
098: public synchronized int getWaitCount() {
099: return waitCount;
100: }
101:
102: /**
103: * This method returns the call count
104: */
105: public synchronized int getCallCount() {
106: return callCount;
107: }
108:
109: /**
110: * This method resets the called count
111: */
112: public synchronized void resetCalledCount() {
113: callCount = 0;
114: }
115:
116: /**
117: * This method is called to release all waiting threads
118: */
119: public synchronized void notifyAllWaitingThreads() {
120: notifyAll();
121: }
122: }
123:
124: /**
125: * This class is responsible for processing as a task.
126: */
127: public static class TestTask implements Task {
128:
129: /**
130: * The process method used by the task object.
131: */
132: public void process(ThreadPoolManager pool) throws Exception {
133: testMonitor.threadWait();
134: }
135:
136: }
137:
138: public static TestMonitor testMonitor = new TestMonitor();
139:
140: public ThreadPoolManagerTest(String testName) {
141: super (testName);
142: BasicConfigurator.configure();
143: }
144:
145: protected void setUp() throws Exception {
146: }
147:
148: protected void tearDown() throws Exception {
149: }
150:
151: /**
152: * Test com.rift.coad.lib.thread.pool.ThreadPoolManager.
153: */
154: public void testThreadPoolManager() throws Exception {
155: System.out.println("ThreadPoolManager");
156:
157: // initialize the thread permissions
158: ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
159: SessionManager.init(permissions);
160: UserStoreManager userStoreManager = new UserStoreManager();
161: UserSessionManager sessionManager = new UserSessionManager(
162: permissions, userStoreManager);
163: LoginManager.init(sessionManager, userStoreManager);
164:
165: // add a user to the session for the current thread
166: RoleManager.getInstance();
167:
168: // instanciate the thread manager
169: CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(
170: sessionManager, userStoreManager);
171: ClassLoader loader = new URLClassLoader(new URL[0], this
172: .getClass().getClassLoader());
173: Thread.currentThread().setContextClassLoader(loader);
174: ThreadGroupManager.getInstance().initThreadGroup(threadGroup);
175:
176: // add a new user object and add to the permission
177: Set set = new HashSet();
178: set.add("test");
179: UserSession user = new UserSession("test1", set);
180: permissions.putSession(
181: new Long(Thread.currentThread().getId()),
182: new ThreadPermissionSession(new Long(Thread
183: .currentThread().getId()), user));
184:
185: // start the thread pool
186: ThreadPoolManager instance1 = new ThreadPoolManager(2,
187: TestTask.class, "test");
188: ThreadPoolManager instance2 = new ThreadPoolManager(3, 5,
189: TestTask.class, "test");
190:
191: assertEquals(2, instance1.getSize());
192: instance1.setSize(3);
193: assertEquals(3, instance1.getSize());
194: assertEquals(3, instance2.getMinSize());
195: instance2.setMinSize(4);
196: assertEquals(4, instance2.getMinSize());
197: try {
198: instance2.setMinSize(6);
199: fail("Should not be able to set the pool size to greater than max.");
200: } catch (PoolException ex) {
201: // ignore correct
202: }
203: assertEquals(5, instance2.getMaxSize());
204: try {
205: instance2.setMaxSize(3);
206: fail("Should not be able to set the pool size max to smaller "
207: + "than max.");
208: } catch (PoolException ex) {
209: // ignore correct
210: }
211:
212: assertEquals(2, testMonitor.getWaitCount());
213: assertEquals(2, testMonitor.getCallCount());
214:
215: instance1.releaseThread();
216: instance2.releaseThread();
217: Thread.sleep(500);
218:
219: assertEquals(4, testMonitor.getCallCount());
220: assertEquals(4, testMonitor.getWaitCount());
221:
222: instance1.releaseThread();
223: instance1.releaseThread();
224: instance2.releaseThread();
225: instance2.releaseThread();
226: instance2.releaseThread();
227: instance2.releaseThread();
228: Thread.sleep(500);
229:
230: assertEquals(8, testMonitor.getCallCount());
231: assertEquals(8, testMonitor.getWaitCount());
232:
233: testMonitor.notifyAllWaitingThreads();
234:
235: instance1.releaseThread();
236: instance1.releaseThread();
237: instance2.releaseThread();
238: instance2.releaseThread();
239: Thread.sleep(500);
240:
241: assertEquals(6, testMonitor.getWaitCount());
242: assertEquals(14, testMonitor.getCallCount());
243:
244: instance1.terminate();
245: instance2.terminate();
246: }
247:
248: }
|