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.lang.reflect.Field;
25
import java.net.InetAddress;
26
import java.util.HashMap;
28
import java.util.Timer;
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.remoting.Client;
39
import org.jboss.remoting.InvocationRequest;
40
import org.jboss.remoting.InvokerLocator;
41
import org.jboss.remoting.ServerInvocationHandler;
42
import org.jboss.remoting.ServerInvoker;
43
import org.jboss.remoting.callback.Callback;
44
import org.jboss.remoting.callback.HandleCallbackException;
45
import org.jboss.remoting.callback.InvokerCallbackHandler;
46
import org.jboss.remoting.transport.Connector;
47
import org.jboss.remoting.transport.PortUtil;
48
import org.jboss.remoting.transport.bisocket.Bisocket;
49
import org.jboss.remoting.transport.bisocket.BisocketServerInvoker;
53
* ServerTimerReuseTestCase verifies that BisocketServerInvoker.start() reuses an
54
* existing static Timer if possible. See JBREM-778.
56
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
57
* @version $Revision: 2900 $
59
* Copyright Jul 20, 2007
62
public class ServerTimerReuseTestCase extends TestCase
64
private static Logger log = Logger.getLogger(ServerTimerReuseTestCase.class);
66
private static boolean firstTime = true;
68
// remoting server connector
69
private Connector connector;
70
private InvokerLocator serverLocator;
71
private TestInvocationHandler invocationHandler;
75
* Sets up target remoting server.
77
public void setUp() throws Exception
82
Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
83
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
84
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
85
PatternLayout layout = new PatternLayout(pattern);
86
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
87
Logger.getRootLogger().addAppender(consoleAppender);
92
public void tearDown()
98
* Verifies that the Timer used by callback BisocketServerInvoker's is reused.
100
public void testTimerReuse() throws Throwable
102
log.info("entering " + getName());
105
String host = InetAddress.getLocalHost().getHostAddress();
106
int port = PortUtil.findFreePort(host);
107
String locatorURI = getTransport() + "://" + host + ":" + port;
108
serverLocator = new InvokerLocator(locatorURI);
109
log.info("Starting remoting server with locator uri of: " + locatorURI);
110
HashMap config = new HashMap();
111
config.put(InvokerLocator.FORCE_REMOTE, "true");
112
addExtraServerConfig(config);
113
connector = new Connector(serverLocator, config);
115
invocationHandler = new TestInvocationHandler();
116
connector.addInvocationHandler("sample", invocationHandler);
120
InvokerLocator clientLocator1 = new InvokerLocator(locatorURI + "/?timeout=5000");
121
HashMap clientConfig = new HashMap();
122
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
123
addExtraClientConfig(clientConfig);
124
Client client = new Client(clientLocator1, clientConfig);
126
log.info("client is connected");
129
assertEquals("abc", client.invoke("abc"));
130
log.info("connection is good");
132
// Add first callback handler and get Timer.
133
TestCallbackHandler callbackHandler1 = new TestCallbackHandler();
134
HashMap metadata = new HashMap();
135
metadata.put(Bisocket.IS_CALLBACK_SERVER, "true");
136
client.addListener(callbackHandler1, metadata);
137
Field field = BisocketServerInvoker.class.getDeclaredField("timer");
138
field.setAccessible(true);
139
Timer timer1 = (Timer) field.get(null);
141
// Add second callback handler and verify Timer is unchanged.
142
TestCallbackHandler callbackHandler2 = new TestCallbackHandler();
144
metadata.put(Bisocket.IS_CALLBACK_SERVER, "true");
145
client.addListener(callbackHandler2, metadata);
146
Timer timer2 = (Timer) field.get(null);
147
assertEquals(timer1, timer2);
150
client.removeListener(callbackHandler1);
151
client.removeListener(callbackHandler2);
154
log.info(getName() + " PASSES");
158
protected String getTransport()
164
protected void addExtraClientConfig(Map config) {}
165
protected void addExtraServerConfig(Map config) {}
168
static class TestInvocationHandler implements ServerInvocationHandler
170
public void addListener(InvokerCallbackHandler callbackHandler) {}
171
public Object invoke(final InvocationRequest invocation) throws Throwable
173
return invocation.getParameter();
175
public void removeListener(InvokerCallbackHandler callbackHandler) {}
176
public void setMBeanServer(MBeanServer server) {}
177
public void setInvoker(ServerInvoker invoker) {}
181
static class TestCallbackHandler implements InvokerCallbackHandler
183
public void handleCallback(Callback callback) throws HandleCallbackException
185
log.info("received callback");
b'\\ No newline at end of file'