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.connectionpool;
24
import java.lang.reflect.Field;
25
import java.net.InetAddress;
26
import java.util.HashMap;
27
import java.util.LinkedList;
30
import javax.management.MBeanServer;
32
import junit.framework.TestCase;
34
import org.apache.log4j.ConsoleAppender;
35
import org.apache.log4j.Level;
36
import org.apache.log4j.Logger;
37
import org.apache.log4j.PatternLayout;
38
import org.jboss.logging.XLevel;
39
import org.jboss.remoting.Client;
40
import org.jboss.remoting.InvocationRequest;
41
import org.jboss.remoting.InvokerLocator;
42
import org.jboss.remoting.ServerInvocationHandler;
43
import org.jboss.remoting.ServerInvoker;
44
import org.jboss.remoting.callback.InvokerCallbackHandler;
45
import org.jboss.remoting.transport.Connector;
46
import org.jboss.remoting.transport.PortUtil;
47
import org.jboss.remoting.transport.socket.MicroSocketClientInvoker;
48
import org.jboss.remoting.transport.socket.SocketWrapper;
52
* Unit test for JBREM-786 for case when maxPoolSize == 1.
54
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
55
* @version $Revision: 1.1 $
57
* Copyright Jan 21, 2008
60
public class ConnectionPoolSingleConnectionTestCase extends TestCase
62
protected static Logger log = Logger.getLogger(ConnectionPoolSingleConnectionTestCase.class);
63
protected static String DELAY = "delay";
64
private static boolean firstTime = true;
66
protected String host;
68
protected String locatorURI;
69
protected InvokerLocator serverLocator;
70
protected Connector connector;
71
protected TestInvocationHandler invocationHandler;
74
public void setUp() throws Exception
79
Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
80
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
81
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
82
PatternLayout layout = new PatternLayout(pattern);
83
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
84
Logger.getRootLogger().addAppender(consoleAppender);
89
public void tearDown()
95
* Verifies that the "create new socket on last retry" feature does not apply when
96
* maxPoolSize == 1. Verifies that the same socket is reused multiple times.
98
public void testMaxPoolSizeOne() throws Throwable
100
log.info("entering " + getName());
102
// Start server - no connection checking.
105
// Create first client.
106
HashMap config = new HashMap();
107
config.put(InvokerLocator.FORCE_REMOTE, "true");
108
config.put("maxPoolSize", "1");
109
addExtraClientConfig(config);
110
Client client = new Client(serverLocator, config);
113
assertEquals("abc", client.invoke("abc"));
114
log.info("connection is good");
116
// Get connection pool.
117
assertTrue(client.getInvoker() instanceof MicroSocketClientInvoker);
118
MicroSocketClientInvoker invoker = (MicroSocketClientInvoker) client.getInvoker();
119
Field field = MicroSocketClientInvoker.class.getDeclaredField("pool");
120
field.setAccessible(true);
121
LinkedList pool = (LinkedList) field.get(invoker);
122
assertEquals(1, pool.size());
123
SocketWrapper socketWrapper = (SocketWrapper) pool.get(0);
125
for (int i = 0; i < 10000; i++)
127
client.invoke("xyz");
130
log.info("made 10000 invocations");
131
assertEquals(1, pool.size());
132
assertEquals(socketWrapper, pool.get(0));
136
log.info(getName() + " PASSES");
140
protected String getTransport()
146
protected void addExtraClientConfig(Map config) {}
147
protected void addExtraServerConfig(Map config) {}
150
protected void setupServer() throws Exception
152
host = InetAddress.getLocalHost().getHostAddress();
153
port = PortUtil.findFreePort(host);
154
locatorURI = getTransport() + "://" + host + ":" + port;
155
serverLocator = new InvokerLocator(locatorURI);
156
log.info("Starting remoting server with locator uri of: " + locatorURI);
157
HashMap config = new HashMap();
158
config.put(InvokerLocator.FORCE_REMOTE, "true");
159
addExtraServerConfig(config);
160
connector = new Connector(serverLocator, config);
162
invocationHandler = new TestInvocationHandler();
163
connector.addInvocationHandler("test", invocationHandler);
168
protected void shutdownServer() throws Exception
170
if (connector != null)
175
static class TestInvocationHandler implements ServerInvocationHandler
177
public void addListener(InvokerCallbackHandler callbackHandler) {}
178
public Object invoke(final InvocationRequest invocation) throws Throwable
180
return invocation.getParameter();
182
public void removeListener(InvokerCallbackHandler callbackHandler) {}
183
public void setMBeanServer(MBeanServer server) {}
184
public void setInvoker(ServerInvoker invoker) {}
b'\\ No newline at end of file'