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;
24
import java.net.InetAddress;
25
import java.util.HashMap;
27
import javax.management.MBeanServer;
29
import junit.framework.TestCase;
31
import org.apache.log4j.ConsoleAppender;
32
import org.apache.log4j.Level;
33
import org.apache.log4j.Logger;
34
import org.apache.log4j.PatternLayout;
35
import org.jboss.remoting.Client;
36
import org.jboss.remoting.InvocationRequest;
37
import org.jboss.remoting.InvokerLocator;
38
import org.jboss.remoting.ServerInvocationHandler;
39
import org.jboss.remoting.ServerInvoker;
40
import org.jboss.remoting.callback.Callback;
41
import org.jboss.remoting.callback.HandleCallbackException;
42
import org.jboss.remoting.callback.InvokerCallbackHandler;
43
import org.jboss.remoting.transport.Connector;
44
import org.jboss.remoting.transport.PortUtil;
45
import org.jboss.remoting.transport.bisocket.Bisocket;
48
* This class can be profiled for memory leaks. See JBREM-721.
50
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
51
* @version $Revision: 2316 $
53
* Copyright March 15, 2007
56
public class BisocketLeakTest extends TestCase
58
private static Logger log = Logger.getLogger(BisocketLeakTest.class);
59
private static String transport = "bisocket";
60
private static int LOOPS = 1000;
63
public void testConnectionCreation() throws Throwable
65
Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
66
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
67
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
68
PatternLayout layout = new PatternLayout(pattern);
69
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
70
Logger.getRootLogger().addAppender(consoleAppender);
72
String host = InetAddress.getLocalHost().getHostName();
73
int port = PortUtil.findFreePort(host);
74
String locatorURI = transport + "://" + host + ":" + port;
75
InvokerLocator serverLocator = new InvokerLocator(locatorURI);
76
log.info("Starting remoting server with locator uri of: " + locatorURI);
77
HashMap serverConfig = new HashMap();
78
serverConfig.put(InvokerLocator.FORCE_REMOTE, "true");
79
serverConfig.put(Bisocket.IS_CALLBACK_SERVER, "false");
80
Connector connector = new Connector(serverLocator, serverConfig);
82
SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
83
connector.addInvocationHandler("sample", invocationHandler);
85
log.info("Started remoting server with locator uri of: " + locatorURI);
87
HashMap clientConfig = new HashMap();
88
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
89
clientConfig.put(Bisocket.IS_CALLBACK_SERVER, "true");
90
log.info("client onnecting to: " + serverLocator);
91
Client client = new Client(serverLocator, clientConfig);
93
log.info("client is connected");
94
SampleCallbackHandler callbackHandler = new SampleCallbackHandler();
96
long start = System.currentTimeMillis();
97
for (int i = 0; i < LOOPS; i++)
99
client.invoke("test");
100
client.addListener(callbackHandler, new HashMap(), null, true);
101
client.removeListener(callbackHandler);
102
assertEquals(i+1, callbackHandler.counter);
103
if ((i+1) % 100 == 0)
104
log.info("connections: " + (i+1));
106
long finish = System.currentTimeMillis();
107
log.info("time to create " + LOOPS + " connections: " + (finish - start));
115
* Can pass transport and port to be used as parameters.
119
public static void main(String[] args)
121
if(args != null && args.length > 0)
125
LOOPS = Integer.parseInt(args[1]);
127
BisocketLeakTest testCase = new BisocketLeakTest();
131
testCase.testConnectionCreation();
142
* Simple invocation handler implementation. When callback client's are registered, will
143
* generate callbacks periodically.
145
static class SampleInvocationHandler implements ServerInvocationHandler
147
public void addListener(InvokerCallbackHandler callbackHandler)
149
log.debug("Adding callback listener.");
152
callbackHandler.handleCallback(new Callback("callback"));
154
catch (HandleCallbackException e)
156
log.error("Unable to send callback");
160
public Object invoke(InvocationRequest invocation) throws Throwable {return null;}
161
public void removeListener(InvokerCallbackHandler callbackHandler) {}
162
public void setMBeanServer(MBeanServer server) {}
163
public void setInvoker(ServerInvoker invoker) {}
167
static class SampleCallbackHandler implements InvokerCallbackHandler
171
public void handleCallback(Callback callback) throws HandleCallbackException
174
if ((counter + 1) % 1000 == 0)
176
System.out.println("received callback " + (counter + 1));