2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
* by the @authors tag. See the copyright.txt in the distribution for a
5
* full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
22
package org.jboss.test.remoting.transport.bisocket.threadpool;
24
import java.lang.reflect.Field;
25
import java.net.InetAddress;
26
import java.net.Socket;
27
import java.util.HashMap;
28
import java.util.HashSet;
29
import java.util.Iterator;
30
import java.util.List;
34
import javax.management.MBeanServer;
36
import junit.framework.TestCase;
38
import org.apache.log4j.ConsoleAppender;
39
import org.apache.log4j.Level;
40
import org.apache.log4j.Logger;
41
import org.apache.log4j.PatternLayout;
42
import org.jboss.logging.XLevel;
43
import org.jboss.remoting.Client;
44
import org.jboss.remoting.InvocationRequest;
45
import org.jboss.remoting.InvokerLocator;
46
import org.jboss.remoting.ServerInvocationHandler;
47
import org.jboss.remoting.ServerInvoker;
48
import org.jboss.remoting.callback.Callback;
49
import org.jboss.remoting.callback.HandleCallbackException;
50
import org.jboss.remoting.callback.InvokerCallbackHandler;
51
import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
52
import org.jboss.remoting.transport.Connector;
53
import org.jboss.remoting.transport.PortUtil;
54
import org.jboss.remoting.transport.bisocket.Bisocket;
55
import org.jboss.remoting.transport.bisocket.BisocketServerInvoker;
56
import org.jboss.remoting.transport.socket.LRUPool;
57
import org.jboss.remoting.transport.socket.ServerThread;
58
import org.jboss.remoting.transport.socket.SocketServerInvoker;
62
* Unit test for JBREM-938.
64
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
65
* @version $Revision: 1.1 $
67
* Copyright Mar 22, 2008
70
public class ServerThreadReuseTestCase extends TestCase
72
private static Logger log = Logger.getLogger(ServerThreadReuseTestCase.class);
74
private static boolean firstTime = true;
76
protected String host;
78
protected String locatorURI;
79
protected InvokerLocator serverLocator;
80
protected Connector connector;
81
protected TestInvocationHandler invocationHandler;
84
public void setUp() throws Exception
89
Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
90
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
91
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
92
PatternLayout layout = new PatternLayout(pattern);
93
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
94
Logger.getRootLogger().addAppender(consoleAppender);
99
public void tearDown()
104
public void testServerThreadReuse() throws Throwable
106
log.info("entering " + getName());
112
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
113
HashMap clientConfig = new HashMap();
114
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
115
addExtraClientConfig(clientConfig);
116
Client client = new Client(clientLocator, clientConfig);
118
log.info("client is connected");
121
assertEquals("abc", client.invoke("abc"));
122
log.info("connection is good");
124
// Add callback handler.
125
TestCallbackHandler callbackHandler = new TestCallbackHandler();
126
HashMap metadata = new HashMap();
127
metadata.put(Bisocket.IS_CALLBACK_SERVER, "true");
128
client.addListener(callbackHandler, metadata, null, true);
129
client.invoke("callback");
130
assertEquals(1, callbackHandler.counter);
131
log.info("first callback successful");
133
// Get callback server thread pools.
134
Set callbackConnectors = client.getCallbackConnectors(callbackHandler);
135
assertEquals(1, callbackConnectors.size());
136
Connector callbackConnector = (Connector) callbackConnectors.iterator().next();
137
BisocketServerInvoker invoker = (BisocketServerInvoker) callbackConnector.getServerInvoker();
138
Field field = SocketServerInvoker.class.getDeclaredField("threadpool");
139
field.setAccessible(true);
140
List threadpool = (List) field.get(invoker);
141
field = SocketServerInvoker.class.getDeclaredField("clientpool");
142
field.setAccessible(true);
143
LRUPool clientpool = (LRUPool) field.get(invoker);
144
assertEquals(0, threadpool.size());
145
assertEquals(1, clientpool.size());
147
// Kill worker thread's socket.
148
Set clientpoolContents = clientpool.getContents();
149
ServerThread serverThread1 = (ServerThread) clientpoolContents.iterator().next();
150
field = ServerThread.class.getDeclaredField("socket");
151
field.setAccessible(true);
152
Socket socket = (Socket) field.get(serverThread1);
155
assertEquals(1, threadpool.size());
156
assertEquals(0, clientpool.size());
158
// Make second callback and verify that worker thread gets reused.
159
client.invoke("callback");
160
assertEquals(2, callbackHandler.counter);
161
log.info("second callback successful");
162
assertEquals(0, threadpool.size());
163
assertEquals(1, clientpool.size());
164
clientpoolContents = clientpool.getContents();
165
ServerThread serverThread2 = (ServerThread) clientpoolContents.iterator().next();
166
assertEquals(serverThread2, serverThread1);
167
log.info("ServerThread was reused");
169
client.removeListener(callbackHandler);
172
log.info(getName() + " PASSES");
176
protected String getTransport()
182
protected void addExtraClientConfig(Map config) {}
183
protected void addExtraServerConfig(Map config) {}
186
protected void setupServer() throws Exception
188
host = InetAddress.getLocalHost().getHostAddress();
189
port = PortUtil.findFreePort(host);
190
locatorURI = getTransport() + "://" + host + ":" + port;
191
serverLocator = new InvokerLocator(locatorURI);
192
log.info("Starting remoting server with locator uri of: " + locatorURI);
193
HashMap config = new HashMap();
194
config.put(InvokerLocator.FORCE_REMOTE, "true");
195
addExtraServerConfig(config);
196
connector = new Connector(serverLocator, config);
198
invocationHandler = new TestInvocationHandler();
199
connector.addInvocationHandler("test", invocationHandler);
204
protected void shutdownServer() throws Exception
206
if (connector != null)
211
static class TestInvocationHandler implements ServerInvocationHandler
213
HashSet callbackHandlers = new HashSet();
215
public void addListener(InvokerCallbackHandler callbackHandler)
217
callbackHandlers.add(callbackHandler);
220
public Object invoke(final InvocationRequest invocation) throws Throwable
222
String command = (String) invocation.getParameter();
224
if ("callback".equals(command))
226
Callback callback = new Callback("callback");
227
Iterator it = callbackHandlers.iterator();
230
ServerInvokerCallbackHandler handler;
231
handler = (ServerInvokerCallbackHandler) it.next();
232
handler.handleCallback(callback);
239
public void removeListener(InvokerCallbackHandler callbackHandler)
241
callbackHandlers.remove(callbackHandler);
245
public void setMBeanServer(MBeanServer server) {}
246
public void setInvoker(ServerInvoker invoker) {}
250
static class TestCallbackHandler implements InvokerCallbackHandler
254
public void handleCallback(Callback callback) throws HandleCallbackException
257
log.info("received callback");
b'\\ No newline at end of file'