2
* JBoss, Home of Professional Open Source.
3
* Copyright 2009, Red Hat Middleware LLC, and individual contributors
4
* as indicated by the @author tags. See the copyright.txt file in the
5
* distribution for a 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.
23
package org.jboss.test.remoting.callback.leak;
25
import java.lang.reflect.Field;
26
import java.net.InetAddress;
27
import java.util.HashMap;
28
import java.util.HashSet;
29
import java.util.Iterator;
32
import java.util.TimerTask;
34
import javax.management.MBeanServer;
36
import junit.framework.TestCase;
38
import org.apache.log4j.ConsoleAppender;
39
import org.apache.log4j.Level;
40
import org.apache.log4j.Logger;
41
import org.apache.log4j.PatternLayout;
42
import org.jboss.remoting.Client;
43
import org.jboss.remoting.ConnectionListener;
44
import org.jboss.remoting.ConnectionNotifier;
45
import org.jboss.remoting.InvocationRequest;
46
import org.jboss.remoting.InvokerLocator;
47
import org.jboss.remoting.LeasePinger;
48
import org.jboss.remoting.MicroRemoteClientInvoker;
49
import org.jboss.remoting.ServerInvocationHandler;
50
import org.jboss.remoting.ServerInvoker;
51
import org.jboss.remoting.callback.Callback;
52
import org.jboss.remoting.callback.HandleCallbackException;
53
import org.jboss.remoting.callback.InvokerCallbackHandler;
54
import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
55
import org.jboss.remoting.transport.Connector;
56
import org.jboss.remoting.transport.PortUtil;
60
* Unit tests for JBREM-1113.
62
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
65
* Copyright Apr 13, 2009
68
public class ServerInvokerCallbackHandlerLeakTestCase extends TestCase
70
private static Logger log = Logger.getLogger(ServerInvokerCallbackHandlerLeakTestCase.class);
72
private static boolean firstTime = true;
73
private static int COUNT = 10;
74
private static Object lock = new Object();
76
protected String host;
78
protected String locatorURI;
79
protected InvokerLocator serverLocator;
80
protected Connector connector;
81
protected TestInvocationHandler invocationHandler;
84
public void setUp() throws Exception
89
Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
90
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
91
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
92
PatternLayout layout = new PatternLayout(pattern);
93
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
94
Logger.getRootLogger().addAppender(consoleAppender);
96
TestConnectionListener.count = 0;
100
public void tearDown()
105
public void testLeakWithCallbackHandlersListening() throws Throwable
111
public void testLeakWithoutCallbackHandlersListening() throws Throwable
117
public void doLeakTest(boolean registerCallbackListener) throws Throwable
119
log.info("entering " + getName());
120
setupServer(registerCallbackListener);
123
ServerInvoker serverInvoker = connector.getServerInvoker();
124
Field field = ServerInvoker.class.getDeclaredField("connectionNotifier");
125
field.setAccessible(true);
126
ConnectionNotifier connectionNotifier = (ConnectionNotifier) field.get(serverInvoker);
127
field = ServerInvoker.class.getDeclaredField("callbackHandlers");
128
field.setAccessible(true);
129
Map callbackHandlers = (Map) field.get(serverInvoker);
132
HashMap clientConfig = new HashMap();
133
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
134
addExtraClientConfig(clientConfig);
135
Client client = null;
137
for (int i = 0; i < COUNT; i++)
139
client = new Client(serverLocator, clientConfig);
141
log.info("client is connected");
144
assertEquals("abc", client.invoke("abc"));
145
log.info("connection is good");
147
TestCallbackHandler callbackHandler = new TestCallbackHandler();
148
client.addListener(callbackHandler, null, null, true);
151
field = MicroRemoteClientInvoker.class.getDeclaredField("leasePinger");
152
field.setAccessible(true);
153
LeasePinger pinger = (LeasePinger) field.get(client.getInvoker());
154
field = LeasePinger.class.getDeclaredField("timerTask");
155
field.setAccessible(true);
156
TimerTask timerTask = (TimerTask) field.get(pinger);
165
assertEquals(COUNT, TestConnectionListener.count);
166
assertEquals(1, connectionNotifier.size());
167
assertTrue(callbackHandlers.isEmpty());
169
log.info(getName() + " PASSES");
173
protected String getTransport()
179
protected void addExtraClientConfig(Map config) {}
180
protected void addExtraServerConfig(Map config) {}
183
protected void setupServer(boolean registerCallbackListener) throws Exception
185
host = InetAddress.getLocalHost().getHostAddress();
186
port = PortUtil.findFreePort(host);
187
locatorURI = getTransport() + "://" + host + ":" + port;
188
locatorURI += "/?leasing=true";
189
if (registerCallbackListener)
191
locatorURI += "&" + ServerInvoker.REGISTER_CALLBACK_LISTENER + "=true";
195
locatorURI += "&" + ServerInvoker.REGISTER_CALLBACK_LISTENER + "=false";
197
serverLocator = new InvokerLocator(locatorURI);
198
log.info("Starting remoting server with locator uri of: " + locatorURI);
199
HashMap config = new HashMap();
200
config.put(InvokerLocator.FORCE_REMOTE, "true");
201
addExtraServerConfig(config);
202
connector = new Connector(serverLocator, config);
204
invocationHandler = new TestInvocationHandler();
205
connector.addInvocationHandler("test", invocationHandler);
207
connector.setLeasePeriod(2000);
208
TestConnectionListener listener = new TestConnectionListener(!registerCallbackListener);
209
connector.addConnectionListener(listener);
213
protected void shutdownServer() throws Exception
215
if (connector != null)
220
static class TestInvocationHandler implements ServerInvocationHandler
222
static public Set callbackHandlers = new HashSet();
223
public void addListener(InvokerCallbackHandler callbackHandler)
225
callbackHandlers.add(callbackHandler);
227
public Object invoke(final InvocationRequest invocation) throws Throwable
229
return invocation.getParameter();
231
public void removeListener(InvokerCallbackHandler callbackHandler) {}
232
public void setMBeanServer(MBeanServer server) {}
233
public void setInvoker(ServerInvoker invoker) {}
237
static class TestCallbackHandler implements InvokerCallbackHandler
239
public void handleCallback(Callback callback) throws HandleCallbackException
241
log.info("received callback");
246
static class TestConnectionListener implements ConnectionListener
248
static public int count;
249
boolean shutdownCallbackHandlers;
251
public TestConnectionListener(boolean shutdownCallbackHandlers)
253
this.shutdownCallbackHandlers = shutdownCallbackHandlers;
256
public synchronized void handleConnectionException(Throwable throwable, Client client)
258
log.info("got connection exception");
261
if (shutdownCallbackHandlers)
263
Iterator it = TestInvocationHandler.callbackHandlers.iterator();
266
ServerInvokerCallbackHandler callbackHandler = (ServerInvokerCallbackHandler) it.next();
267
callbackHandler.shutdown();
268
log.info("shut down: " + callbackHandler);
270
TestInvocationHandler.callbackHandlers.clear();
b'\\ No newline at end of file'