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;
53
* Unit test for JBREM-911.
55
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
56
* @version $Revision: 1.1 $
58
* Copyright Feb 27, 2008
61
public class ConnectionPoolSingleRetryTestCase extends TestCase
63
protected static Logger log = Logger.getLogger(ConnectionPoolSingleRetryTestCase.class);
64
protected static String DELAY = "delay";
65
private static boolean firstTime = true;
67
protected String host;
69
protected String locatorURI;
70
protected InvokerLocator serverLocator;
71
protected Connector connector;
72
protected TestInvocationHandler invocationHandler;
75
public void setUp() throws Exception
80
Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
81
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
82
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
83
PatternLayout layout = new PatternLayout(pattern);
84
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
85
Logger.getRootLogger().addAppender(consoleAppender);
90
public void tearDown()
96
* Verifies that the "create new socket on last retry" feature does not apply when
97
* maxPoolSize == 1. Verifies that the same socket is reused multiple times.
99
public void testMaxPoolSizeOne() throws Throwable
101
log.info("entering " + getName());
103
// Start server - no connection checking.
106
// Create first client.
107
HashMap config = new HashMap();
108
config.put(InvokerLocator.FORCE_REMOTE, "true");
109
config.put("numberOfCallRetries", "1");
110
addExtraClientConfig(config);
111
Client client = new Client(serverLocator, config);
114
assertEquals("abc", client.invoke("abc"));
115
log.info("connection is good");
117
// Verify numberOfCallRetries == 1.
118
assertTrue(client.getInvoker() instanceof MicroSocketClientInvoker);
119
MicroSocketClientInvoker invoker = (MicroSocketClientInvoker) client.getInvoker();
120
assertEquals(1, invoker.getNumberOfCallRetries());
122
// Get connection pool.
123
Field field = MicroSocketClientInvoker.class.getDeclaredField("pool");
124
field.setAccessible(true);
125
LinkedList pool = (LinkedList) field.get(invoker);
126
assertEquals(1, pool.size());
127
SocketWrapper socketWrapper = (SocketWrapper) pool.get(0);
129
for (int i = 0; i < 10000; i++)
131
client.invoke("xyz");
134
log.info("made 10000 invocations");
135
assertEquals(1, pool.size());
136
assertEquals(socketWrapper, pool.get(0));
140
log.info(getName() + " PASSES");
144
protected String getTransport()
150
protected void addExtraClientConfig(Map config) {}
151
protected void addExtraServerConfig(Map config) {}
154
protected void setupServer() throws Exception
156
host = InetAddress.getLocalHost().getHostAddress();
157
port = PortUtil.findFreePort(host);
158
locatorURI = getTransport() + "://" + host + ":" + port;
159
serverLocator = new InvokerLocator(locatorURI);
160
log.info("Starting remoting server with locator uri of: " + locatorURI);
161
HashMap config = new HashMap();
162
config.put(InvokerLocator.FORCE_REMOTE, "true");
163
addExtraServerConfig(config);
164
connector = new Connector(serverLocator, config);
166
invocationHandler = new TestInvocationHandler();
167
connector.addInvocationHandler("test", invocationHandler);
172
protected void shutdownServer() throws Exception
174
if (connector != null)
179
static class TestInvocationHandler implements ServerInvocationHandler
181
public void addListener(InvokerCallbackHandler callbackHandler) {}
182
public Object invoke(final InvocationRequest invocation) throws Throwable
184
return invocation.getParameter();
186
public void removeListener(InvokerCallbackHandler callbackHandler) {}
187
public void setMBeanServer(MBeanServer server) {}
188
public void setInvoker(ServerInvoker invoker) {}
b'\\ No newline at end of file'