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.io.IOException;
25
import java.lang.reflect.Field;
26
import java.net.InetAddress;
27
import java.net.Socket;
28
import java.util.HashMap;
32
import javax.management.MBeanServer;
34
import junit.framework.TestCase;
36
import org.apache.log4j.ConsoleAppender;
37
import org.apache.log4j.Level;
38
import org.apache.log4j.Logger;
39
import org.apache.log4j.PatternLayout;
40
import org.jboss.logging.XLevel;
41
import org.jboss.remoting.Client;
42
import org.jboss.remoting.InvocationRequest;
43
import org.jboss.remoting.InvokerLocator;
44
import org.jboss.remoting.ServerInvocationHandler;
45
import org.jboss.remoting.ServerInvoker;
46
import org.jboss.remoting.callback.Callback;
47
import org.jboss.remoting.callback.HandleCallbackException;
48
import org.jboss.remoting.callback.InvokerCallbackHandler;
49
import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
50
import org.jboss.remoting.transport.Connector;
51
import org.jboss.remoting.transport.PortUtil;
52
import org.jboss.remoting.transport.bisocket.Bisocket;
53
import org.jboss.remoting.transport.bisocket.BisocketServerInvoker;
54
import org.jboss.remoting.transport.socket.SocketClientInvoker;
58
* Unit test for JBREM-845.
60
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
61
* @version $Revision: 1.1 $
63
* Copyright Dec 11, 2007
66
public class CallbackClientWithZeroTimeoutTestCase extends TestCase
68
private static Logger log = Logger.getLogger(CallbackClientWithZeroTimeoutTestCase.class);
70
private static boolean firstTime = true;
71
private static String CALLBACK = "callback";
72
protected String host;
74
protected String locatorURI;
75
protected InvokerLocator serverLocator;
76
protected Connector connector;
77
protected TestInvocationHandler invocationHandler;
80
public void setUp() throws Exception
85
Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
86
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
87
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
88
PatternLayout layout = new PatternLayout(pattern);
89
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
90
Logger.getRootLogger().addAppender(consoleAppender);
95
public void tearDown()
100
public void testCallbackSocketFailure() throws Throwable
102
log.info("entering " + getName());
108
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
109
HashMap clientConfig = new HashMap();
110
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
111
addExtraClientConfig(clientConfig);
112
Client client = new Client(clientLocator, clientConfig);
114
log.info("client is connected");
117
assertEquals("abc", client.invoke("abc"));
118
log.info("connection is good");
120
// Add callback handler.
121
TestCallbackHandler callbackHandler = new TestCallbackHandler();
122
Map metadata = new HashMap();
123
metadata.put(Bisocket.IS_CALLBACK_SERVER, "true");
124
client.addListener(callbackHandler, metadata);
125
log.info("callback handler is installed");
127
// Make sure callback client invoker has timeout == 0.
128
Client callbackClient = invocationHandler.callbackHandler.getCallbackClient();
129
SocketClientInvoker callbackClientInvoker = (SocketClientInvoker) callbackClient.getInvoker();
130
assertEquals(0, callbackClientInvoker.getTimeout());
131
log.info("timeout == 0");
133
// Verify failing PingTimerTask kicks thread out of BisocketClientInvoker.createSocket().
134
// 1. Get client side control socket.
135
Set callbackConnectors = client.getCallbackConnectors(callbackHandler);
136
assertEquals(1, callbackConnectors.size());
137
Connector callbackConnector = (Connector) callbackConnectors.iterator().next();
138
BisocketServerInvoker callbackServerInvoker = (BisocketServerInvoker) callbackConnector.getServerInvoker();
139
Field field = BisocketServerInvoker.class.getDeclaredField("controlConnectionThreadMap");
140
field.setAccessible(true);
141
Map controlConnectionThreadMap = (Map) field.get(callbackServerInvoker);
142
assertEquals(1, controlConnectionThreadMap.size());
143
Thread controlConnectionThread = (Thread) controlConnectionThreadMap.values().iterator().next();
144
assertNotNull(controlConnectionThread);
146
Class controlConnectionThreadClass = null;
147
Class[] classes = BisocketServerInvoker.class.getDeclaredClasses();
148
for (int i = 0; i < classes.length; i++)
150
log.info(classes[i]);
151
if (classes[i].getName().indexOf("ControlConnectionThread") >= 0)
153
controlConnectionThreadClass = classes[i];
157
assertNotNull(controlConnectionThreadClass);
158
field = controlConnectionThreadClass.getDeclaredField("controlSocket");
159
field.setAccessible(true);
160
Socket controlSocket = (Socket) field.get(controlConnectionThread);
161
assertNotNull(controlSocket);
163
// 2. Get server side ControlConnectionThread and stop it.
164
field = BisocketServerInvoker.class.getDeclaredField("secondaryServerSocketThreads");
165
field.setAccessible(true);
166
Set secondaryServerSocketThreads = (Set) field.get(connector.getServerInvoker());
167
Thread secondaryServerSocketThread = (Thread) secondaryServerSocketThreads.iterator().next();
168
assertNotNull(secondaryServerSocketThread);
169
secondaryServerSocketThread.stop();
170
log.info("stopped secondaryServerSocketThread");
172
// 3. Try to do callback.
173
client.invokeOneway(CALLBACK);
174
log.info("requested CALLBACK");
176
// 4. Close client side control socket so PING fails.
177
controlSocket.close();
178
log.info("closed controlSocket");
180
// 5. Test that attempt to create a socket for callback threw exception.
182
assertTrue(invocationHandler.ok);
183
log.info("got expected Exception doing callback");
185
client.removeListener(callbackHandler);
188
log.info(getName() + " PASSES");
192
protected String getTransport()
198
protected void addExtraClientConfig(Map config) {}
199
protected void addExtraServerConfig(Map config) {}
202
protected void setupServer() throws Exception
204
host = InetAddress.getLocalHost().getHostAddress();
205
port = PortUtil.findFreePort(host);
206
locatorURI = getTransport() + "://" + host + ":" + port + "/?timeout=0";
207
locatorURI += "&" + Bisocket.PING_FREQUENCY + "=1000";
208
locatorURI += "&" + Bisocket.MAX_RETRIES + "=1";
209
locatorURI += "&numberOfCallRetries=1";
210
serverLocator = new InvokerLocator(locatorURI);
211
log.info("Starting remoting server with locator uri of: " + locatorURI);
212
HashMap config = new HashMap();
213
config.put(InvokerLocator.FORCE_REMOTE, "true");
214
addExtraServerConfig(config);
215
connector = new Connector(serverLocator, config);
217
invocationHandler = new TestInvocationHandler();
218
connector.addInvocationHandler("test", invocationHandler);
223
protected void shutdownServer() throws Exception
225
if (connector != null)
230
static class TestInvocationHandler implements ServerInvocationHandler
233
public ServerInvokerCallbackHandler callbackHandler;
235
public void addListener(InvokerCallbackHandler callbackHandler)
237
this.callbackHandler = (ServerInvokerCallbackHandler) callbackHandler;
239
public Object invoke(final InvocationRequest invocation) throws Throwable
241
if (CALLBACK.equals(invocation.getParameter()))
245
log.info("calling handleCallback()");
246
callbackHandler.handleCallback(new Callback(CALLBACK));
247
log.info("called handleCallback()");
249
catch (HandleCallbackException e)
251
log.info("error: " + e.getMessage(), e);
252
Throwable cause = e.getCause();
253
log.info("cause: " + cause);
254
cause = cause.getCause();
255
log.info("cause: " + cause);
256
if (cause instanceof IOException && "Unable to create socket".equals(cause.getMessage()))
262
return invocation.getParameter();
264
public void removeListener(InvokerCallbackHandler callbackHandler) {}
265
public void setMBeanServer(MBeanServer server) {}
266
public void setInvoker(ServerInvoker invoker) {}
270
static class TestCallbackHandler implements InvokerCallbackHandler
274
public void handleCallback(Callback callback) throws HandleCallbackException
277
log.info("received callback");
b'\\ No newline at end of file'