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;
27
import java.util.HashSet;
28
import java.util.Iterator;
29
import java.util.LinkedList;
33
import javax.management.MBeanServer;
35
import junit.framework.TestCase;
37
import org.apache.log4j.ConsoleAppender;
38
import org.apache.log4j.Level;
39
import org.apache.log4j.Logger;
40
import org.apache.log4j.PatternLayout;
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.BisocketServerInvoker;
53
import org.jboss.remoting.transport.socket.MicroSocketClientInvoker;
57
* Unit test for JBREM-767.
59
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
60
* @version $Revision: 2732 $
62
* Copyright Aug 4, 2007
65
public class CallbackDisconnectTestCase extends TestCase
67
public static int port;
68
private static Logger log = Logger.getLogger(CallbackDisconnectTestCase.class);
69
private static final String CALLBACK_TEST = "callbackTest";
71
private static boolean firstTime = true;
73
// remoting server connector
74
private Connector connector;
75
private InvokerLocator serverLocator;
76
private TestInvocationHandler invocationHandler;
80
* Sets up target remoting server.
82
public void setUp() throws Exception
87
Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
88
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
89
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
90
PatternLayout layout = new PatternLayout(pattern);
91
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
92
Logger.getRootLogger().addAppender(consoleAppender);
97
public void tearDown()
103
* Verifies that BisocketClientInvoker.handleDisconnect() wakes up any threads
104
* blocked in BisocketClientInvoker.createSocket().
106
public void testWakeupsInHandleDisconnect() throws Throwable
108
log.info("entering " + getName());
111
String host = InetAddress.getLocalHost().getHostAddress();
112
port = PortUtil.findFreePort(host);
113
String locatorURI = getTransport() + "://" + host + ":" + port;
114
serverLocator = new InvokerLocator(locatorURI);
115
log.info("Starting remoting server with locator uri of: " + locatorURI);
116
HashMap config = new HashMap();
117
config.put(InvokerLocator.FORCE_REMOTE, "true");
118
config.put("timeout", "0");
119
addExtraServerConfig(config);
120
connector = new Connector(serverLocator, config);
122
invocationHandler = new TestInvocationHandler();
123
connector.addInvocationHandler("sample", invocationHandler);
127
HashMap clientConfig = new HashMap();
128
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
129
addExtraClientConfig(clientConfig);
130
final Client client = new Client(serverLocator, clientConfig);
132
assertEquals("abc", client.invoke("abc"));
133
log.info("client is connected");
135
// Add callback handler.
136
TestInvokerCallbackHandler callbackHandler = new TestInvokerCallbackHandler();
137
client.addListener(callbackHandler, new HashMap());
138
client.invoke(CALLBACK_TEST);
139
assertEquals(1, callbackHandler.counter);
140
log.info("callback handler is installed");
142
// Remove existing connections for callback clients.
143
ServerInvoker serverInvoker = connector.getServerInvoker();
144
Field field = ServerInvoker.class.getDeclaredField("callbackHandlers");
145
field.setAccessible(true);
146
Map callbackHandlers = (Map) field.get(serverInvoker);
147
assertEquals(1, callbackHandlers.size());
148
ServerInvokerCallbackHandler serverInvokerCallbackHandler;
149
serverInvokerCallbackHandler = (ServerInvokerCallbackHandler) callbackHandlers.values().iterator().next();
150
Client callbackClient = serverInvokerCallbackHandler.getCallbackClient();
151
assertTrue(callbackClient.getInvoker() instanceof MicroSocketClientInvoker);
152
MicroSocketClientInvoker callbackClientInvoker = (MicroSocketClientInvoker) callbackClient.getInvoker();
153
field = MicroSocketClientInvoker.class.getDeclaredField("pool");
154
field.setAccessible(true);
155
LinkedList pool = (LinkedList) field.get(callbackClientInvoker);
156
assertEquals(1, pool.size());
159
// Prevent creation of new connections for callback clients.
160
field = Client.class.getDeclaredField("callbackConnectors");
161
field.setAccessible(true);
162
Map callbackConnectors = (Map) field.get(client);
163
assertEquals(1, callbackConnectors.size());
164
Set callbackConnectorSet = (Set) callbackConnectors.values().iterator().next();
165
assertEquals(1, callbackConnectorSet.size());
166
Connector callbackConnector = (Connector) callbackConnectorSet.iterator().next();
167
assertTrue(callbackConnector.getServerInvoker() instanceof BisocketServerInvoker);
168
BisocketServerInvoker callbackServerInvoker = (BisocketServerInvoker) callbackConnector.getServerInvoker();
169
field = BisocketServerInvoker.class.getDeclaredField("controlConnectionThreadMap");
170
field.setAccessible(true);
171
Map controlConnectionThreadMap = (Map) field.get(callbackServerInvoker);
172
assertEquals(1, controlConnectionThreadMap.size());
173
Thread controlConnectionThread = (Thread) controlConnectionThreadMap.values().iterator().next();
174
controlConnectionThread.stop();
176
// Verify that the call to sockets.wait() in BisocketClientInvoker will
177
// wake up, even though the timeout value is set to 0.
178
class TestThread extends Thread
181
public Throwable throwable;
186
log.info("calling client.invoke(CALLBACK_TEST)");
187
client.invoke(CALLBACK_TEST);
188
log.info("called client.invoke(CALLBACK_TEST)");
192
log.info("TestThread got expected exception: " + t.getMessage());
199
TestThread testThread = new TestThread();
202
callbackClient.disconnect();
204
assertTrue(testThread.done);
205
assertNotNull(testThread.throwable);
206
assertEquals("Error handling callback", testThread.throwable.getMessage());
208
client.removeListener(callbackHandler);
214
protected String getTransport()
220
protected void addExtraClientConfig(Map config) {}
221
protected void addExtraServerConfig(Map config) {}
224
static class TestInvocationHandler implements ServerInvocationHandler
226
public Set listeners = new HashSet();
228
public void addListener(InvokerCallbackHandler callbackHandler)
230
listeners.add(callbackHandler);
232
public Object invoke(final InvocationRequest invocation) throws Throwable
234
if (CALLBACK_TEST.equals(invocation.getParameter()))
236
Iterator it = listeners.iterator();
239
InvokerCallbackHandler handler = (InvokerCallbackHandler) it.next();
240
handler.handleCallback(new Callback("test"));
243
return invocation.getParameter();
245
public void removeListener(InvokerCallbackHandler callbackHandler) {}
246
public void setMBeanServer(MBeanServer server) {}
247
public void setInvoker(ServerInvoker invoker) {}
250
static class TestInvokerCallbackHandler implements InvokerCallbackHandler
254
public void handleCallback(Callback callback) throws HandleCallbackException
b'\\ No newline at end of file'