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.socket.timeout;
24
import java.lang.reflect.Field;
25
import java.net.InetAddress;
26
import java.util.HashMap;
27
import java.util.LinkedList;
31
import javax.management.MBeanServer;
33
import junit.framework.TestCase;
35
import org.apache.log4j.ConsoleAppender;
36
import org.apache.log4j.Level;
37
import org.apache.log4j.Logger;
38
import org.apache.log4j.PatternLayout;
39
import org.jboss.logging.XLevel;
40
import org.jboss.remoting.Client;
41
import org.jboss.remoting.InvocationRequest;
42
import org.jboss.remoting.InvokerLocator;
43
import org.jboss.remoting.ServerInvocationHandler;
44
import org.jboss.remoting.ServerInvoker;
45
import org.jboss.remoting.callback.Callback;
46
import org.jboss.remoting.callback.HandleCallbackException;
47
import org.jboss.remoting.callback.InvokerCallbackHandler;
48
import org.jboss.remoting.transport.Connector;
49
import org.jboss.remoting.transport.PortUtil;
50
import org.jboss.remoting.transport.socket.LRUPool;
51
import org.jboss.remoting.transport.socket.ServerThread;
52
import org.jboss.remoting.transport.socket.SocketServerInvoker;
55
public class ServerThreadReuseAfterTimeoutTestCase extends TestCase
57
private static Logger log = Logger.getLogger(ServerThreadReuseAfterTimeoutTestCase.class);
59
private static boolean firstTime = true;
61
protected String host;
63
protected String locatorURI;
64
protected InvokerLocator serverLocator;
65
protected Connector connector;
66
protected TestInvocationHandler invocationHandler;
69
public void setUp() throws Exception
74
Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
75
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
76
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
77
PatternLayout layout = new PatternLayout(pattern);
78
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
79
Logger.getRootLogger().addAppender(consoleAppender);
84
public void tearDown()
89
public void testJavaSerializationDefault() throws Throwable
91
log.info("entering " + getName());
92
Map config = new HashMap();
93
doJavaSerializationTest(config);
94
log.info(getName() + " PASSES");
98
public void testJavaSerializationConfigured() throws Throwable
100
log.info("entering " + getName());
101
Map config = new HashMap();
102
config.put(ServerThread.CONTINUE_AFTER_TIMEOUT, "false");
103
doJavaSerializationTest(config);
104
log.info(getName() + " PASSES");
108
public void testJBossSerializationDefault() throws Throwable
110
log.info("entering " + getName());
111
Map config = new HashMap();
112
doJBossSerializationTest(config);
113
log.info(getName() + " PASSES");
117
public void testJBossSerializationConfigured() throws Throwable
119
log.info("entering " + getName());
120
Map config = new HashMap();
121
config.put(ServerThread.CONTINUE_AFTER_TIMEOUT, "true");
122
doJBossSerializationTest(config);
123
log.info(getName() + " PASSES");
127
public void doJavaSerializationTest(Map clientConfig) throws Throwable
129
log.info("entering " + getName());
135
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
136
clientConfig = new HashMap();
137
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
138
addExtraClientConfig(clientConfig);
139
Client client = new Client(clientLocator, clientConfig);
141
log.info("client is connected");
144
assertEquals("abc", client.invoke("abc"));
145
log.info("connection is good");
148
SocketServerInvoker invoker = (SocketServerInvoker) connector.getServerInvoker();
149
Field field = SocketServerInvoker.class.getDeclaredField("clientpool");
150
field.setAccessible(true);
151
LRUPool clientpool = (LRUPool) field.get(invoker);
152
assertEquals(1, clientpool.size());
153
Set s = clientpool.getContents();
154
ServerThread serverThread1 = (ServerThread) s.iterator().next();
157
field = SocketServerInvoker.class.getDeclaredField("threadpool");
158
field.setAccessible(true);
159
LinkedList threadpool = (LinkedList) field.get(invoker);
160
assertEquals(0, threadpool.size());
162
// Wait for ServerThread to time out.
164
for (int i = 0; i < 5; i++)
167
if (clientpool.size() == 0) break;
170
if (clientpool.size() > 0)
172
fail("expect clientpool.size() == 0");
175
// Verify original ServerThread was returned to threadpool.
176
assertEquals(1, threadpool.size());
177
assertEquals(serverThread1, threadpool.iterator().next());
179
// Make another invocation and verify ServerThread was reused.
180
client.invoke("xyz");
181
assertEquals(1, clientpool.size());
182
s = clientpool.getContents();
183
ServerThread serverThread2 = (ServerThread) s.iterator().next();
184
assertEquals(serverThread1, serverThread2);
188
log.info(getName() + " PASSES");
192
public void doJBossSerializationTest(Map clientConfig) throws Throwable
194
log.info("entering " + getName());
197
setupServer("jboss");
200
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
201
clientConfig = new HashMap();
202
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
203
addExtraClientConfig(clientConfig);
204
Client client = new Client(clientLocator, clientConfig);
206
log.info("client is connected");
209
assertEquals("abc", client.invoke("abc"));
210
log.info("connection is good");
212
// Get clientpool and ServerThread.
213
SocketServerInvoker invoker = (SocketServerInvoker) connector.getServerInvoker();
214
Field field = SocketServerInvoker.class.getDeclaredField("clientpool");
215
field.setAccessible(true);
216
LRUPool clientpool = (LRUPool) field.get(invoker);
217
assertEquals(1, clientpool.size());
218
Set clientpoolContents = clientpool.getContents();
219
ServerThread serverThread1 = (ServerThread) clientpoolContents.iterator().next();
222
field = SocketServerInvoker.class.getDeclaredField("threadpool");
223
field.setAccessible(true);
224
LinkedList threadpool = (LinkedList) field.get(invoker);
225
assertEquals(0, threadpool.size());
227
// Wait for ServerThread to time out.
230
// Verify original ServerThread remains in clientpool.
231
assertEquals(0, threadpool.size());
232
assertEquals(1, clientpool.size());
233
clientpoolContents = clientpool.getContents();
234
assertEquals(serverThread1, clientpoolContents.iterator().next());
236
// Make another invocation and verify ServerThread was reused.
237
client.invoke("xyz");
238
assertEquals(1, clientpool.size());
239
clientpoolContents = clientpool.getContents();
240
ServerThread serverThread2 = (ServerThread) clientpoolContents.iterator().next();
241
assertEquals(serverThread1, serverThread2);
245
log.info(getName() + " PASSES");
249
protected String getTransport()
255
protected void addExtraClientConfig(Map config) {}
256
protected void addExtraServerConfig(Map config) {}
259
protected void setupServer(String serializationType) throws Exception
261
host = InetAddress.getLocalHost().getHostAddress();
262
port = PortUtil.findFreePort(host);
263
locatorURI = getTransport() + "://" + host + ":" + port + "/?timeout=4000";
264
locatorURI += "&serializationtype=" + serializationType;
265
serverLocator = new InvokerLocator(locatorURI);
266
log.info("Starting remoting server with locator uri of: " + locatorURI);
267
HashMap config = new HashMap();
268
config.put(InvokerLocator.FORCE_REMOTE, "true");
269
addExtraServerConfig(config);
270
connector = new Connector(serverLocator, config);
272
invocationHandler = new TestInvocationHandler();
273
connector.addInvocationHandler("test", invocationHandler);
278
protected void shutdownServer() throws Exception
280
if (connector != null)
285
static class TestInvocationHandler implements ServerInvocationHandler
287
public void addListener(InvokerCallbackHandler callbackHandler) {}
288
public Object invoke(final InvocationRequest invocation) throws Throwable
290
return invocation.getParameter();
292
public void removeListener(InvokerCallbackHandler callbackHandler) {}
293
public void setMBeanServer(MBeanServer server) {}
294
public void setInvoker(ServerInvoker invoker) {}
298
static class TestCallbackHandler implements InvokerCallbackHandler
300
public void handleCallback(Callback callback) throws HandleCallbackException
302
log.info("received callback");
b'\\ No newline at end of file'