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.net.InetAddress;
25
import java.util.HashMap;
28
import javax.management.MBeanServer;
30
import junit.framework.TestCase;
32
import org.apache.log4j.ConsoleAppender;
33
import org.apache.log4j.Level;
34
import org.apache.log4j.Logger;
35
import org.apache.log4j.PatternLayout;
36
import org.jboss.logging.XLevel;
37
import org.jboss.remoting.Client;
38
import org.jboss.remoting.InvocationRequest;
39
import org.jboss.remoting.InvokerLocator;
40
import org.jboss.remoting.ServerInvocationHandler;
41
import org.jboss.remoting.ServerInvoker;
42
import org.jboss.remoting.callback.InvokerCallbackHandler;
43
import org.jboss.remoting.transport.Connector;
44
import org.jboss.remoting.transport.PortUtil;
48
* Unit test for JBREM-786, JBREM-890.
50
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
51
* @version $Revision: 1.1 $
53
* Copyright Dec 21, 2007
56
public class ConnectionPoolRetryTestCase extends TestCase
58
protected static Logger log = Logger.getLogger(ConnectionPoolRetryTestCase.class);
59
protected static String DELAY = "delay";
60
private static boolean firstTime = true;
62
protected String host;
64
protected String locatorURI;
65
protected InvokerLocator serverLocator;
66
protected Connector connector;
67
protected TestInvocationHandler invocationHandler;
70
public void setUp() throws Exception
75
Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
76
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
77
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
78
PatternLayout layout = new PatternLayout(pattern);
79
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
80
Logger.getRootLogger().addAppender(consoleAppender);
85
public void tearDown()
91
* Verifies a new connection is created on the last attempt to make
92
* an invocation. Tries to create a situation in which server threads are
93
* frequently evicted and reused with a new socket, leaving the matching
94
* connection in the client side connection pool unusable. If the probability of
95
* getting an unusable connection is high, it is likely that the number
96
* of retries would be exhausted and an exception thrown. The new facility
97
* for always creating a new connection on the last attempt should make it
98
* possible to avoid running out of retries.
100
public void testNewConnectionOnLastRetry() throws Throwable
102
log.info("entering " + getName());
104
// Start server - no connection checking.
107
// Create first client.
108
InvokerLocator clientLocator1 = new InvokerLocator(locatorURI);
109
HashMap clientConfig1 = new HashMap();
110
clientConfig1.put(InvokerLocator.FORCE_REMOTE, "true");
111
clientConfig1.put("numberOfCallRetries", "2");
112
addExtraClientConfig(clientConfig1);
113
Client client1 = new Client(clientLocator1, clientConfig1);
117
int INVOCATIONS = 5000;
118
BangerThread[] bangers = new BangerThread[BANGERS];
119
for (int i = 0; i < BANGERS; i++)
121
bangers[i] = new BangerThread(client1, INVOCATIONS, "banger:" + i);
124
String newLocatorURI = locatorURI + "/?timeout=100000";
125
InvokerLocator clientLocator2 = new InvokerLocator(newLocatorURI);
126
HashMap clientConfig2 = new HashMap();
127
clientConfig2.put(InvokerLocator.FORCE_REMOTE, "true");
128
clientConfig1.put("numberOfCallRetries", "4");
129
addExtraClientConfig(clientConfig2);
130
Client client2 = new Client(clientLocator2, clientConfig2);
133
for (int i = 0; i < BANGERS; i++)
138
for (int i = 0; i < BANGERS; i++)
143
for (int i = 0; i < BANGERS; i++)
145
log.info("banger " + i + " done: " + bangers[i].done);
146
log.info("banger " + i + " ok: " + bangers[i].ok);
149
for (int i = 0; i < BANGERS; i++)
151
assertTrue("banger " + i + " not done", bangers[i].done);
152
assertTrue("banger " + i + " experienced error", bangers[i].ok);
155
client1.disconnect();
156
client2.disconnect();
158
log.info(getName() + " PASSES");
162
protected String getTransport()
168
protected void addExtraClientConfig(Map config) {}
169
protected void addExtraServerConfig(Map config) {}
172
protected void setupServer() throws Exception
174
host = InetAddress.getLocalHost().getHostAddress();
175
port = PortUtil.findFreePort(host);
176
locatorURI = getTransport() + "://" + host + ":" + port;
177
serverLocator = new InvokerLocator(locatorURI);
178
log.info("Starting remoting server with locator uri of: " + locatorURI);
179
HashMap config = new HashMap();
180
config.put(InvokerLocator.FORCE_REMOTE, "true");
181
config.put("maxPoolSize", "2");
182
addExtraServerConfig(config);
183
connector = new Connector(serverLocator, config);
185
invocationHandler = new TestInvocationHandler();
186
connector.addInvocationHandler("test", invocationHandler);
191
protected void shutdownServer() throws Exception
193
if (connector != null)
198
static class TestInvocationHandler implements ServerInvocationHandler
200
public void addListener(InvokerCallbackHandler callbackHandler) {}
201
public Object invoke(final InvocationRequest invocation) throws Throwable
203
return invocation.getParameter();
205
public void removeListener(InvokerCallbackHandler callbackHandler) {}
206
public void setMBeanServer(MBeanServer server) {}
207
public void setInvoker(ServerInvoker invoker) {}
211
static class BangerThread extends Thread
215
private Client client;
218
private boolean error;
220
public BangerThread(Client client, int count, String name)
222
this.client = client;
229
log.info(name + ": started");
230
for (int i = 0; i < count; i++)
234
long start = System.currentTimeMillis();
235
client.invoke(name + ":" + i);
236
long duration = System.currentTimeMillis() - start;
239
log.info(this + " invocation(" + i + ") took " + duration + " ms");
242
if ((i + 1) % 1000 == 0)
243
log.info(name + " got response: " + (i+1));
247
log.error("error in thread: " + name + ", invocation: " + i, e);
b'\\ No newline at end of file'