1
package org.jboss.test.remoting.callback.push.bidirectional.multiplex;
3
import junit.framework.TestCase;
5
import org.jboss.remoting.Client;
6
import org.jboss.remoting.InvokerLocator;
7
import org.jboss.remoting.callback.Callback;
8
import org.jboss.remoting.callback.HandleCallbackException;
9
import org.jboss.remoting.callback.InvokerCallbackHandler;
10
import org.jboss.remoting.transport.Connector;
11
import org.jboss.remoting.transport.multiplex.MultiplexClientInvoker;
12
import org.jboss.remoting.transport.multiplex.MultiplexServerInvoker;
13
import org.jboss.remoting.transport.multiplex.MultiplexingManager;
14
import org.jboss.remoting.transport.multiplex.VirtualServerSocket;
15
import org.jboss.remoting.transport.multiplex.VirtualSocket;
16
import org.jboss.remoting.transport.multiplex.MultiplexServerInvoker.SocketGroupInfo;
17
import org.jboss.remoting.transport.socket.SocketServerInvoker;
19
import java.lang.reflect.Field;
20
import java.util.HashMap;
25
* @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
27
public class MultiplexCallbackTestClient extends TestCase
29
private String locatorUri = "multiplex://localhost:8999";
30
private boolean gotCallback = false;
32
public void testCallback() throws Throwable
34
Client client = new Client(new InvokerLocator(locatorUri));
36
InvokerCallbackHandler testCallbackHandler = new MultiplexCallbackTestClient.TestCallbackHandler();
37
client.addListener(testCallbackHandler, new HashMap(), "foobar");
38
client.invoke("foobar");
42
client.removeListener(testCallbackHandler);
45
assertTrue(gotCallback);
51
* This test verifies that when Client creates an anonymous callback Connector,
52
* the callback connection from server to client uses the same underlying
53
* socket as the client to server connection.
57
public void testSharedCallbackConnection() throws Throwable
59
// Wait for any existing MultiplexingManagers to close.
62
// There should be 0 MultiplexingManagers before Client is connected.
63
Field field = MultiplexingManager.class.getDeclaredField("allManagers");
64
field.setAccessible(true);
65
Set allManagers = (Set) field.get(null);
66
assertNotNull(allManagers);
67
assertEquals(0, allManagers.size());
69
// Create and connect Client.
70
Client client = new Client(new InvokerLocator(locatorUri));
73
// There should be 1 MultiplexingManager now that Client is connected.
74
assertEquals(1, allManagers.size());
76
// Add a push CallbackHandler.
77
InvokerCallbackHandler testCallbackHandler = new TestCallbackHandler();
78
client.addListener(testCallbackHandler, new HashMap(), "foobar");
80
// There should still be 1 MultiplexingManager, since client invoker and
81
// server invoker should be using the same connection.
82
assertEquals(1, allManagers.size());
84
// Show connection works.
85
client.invoke("foobar");
87
assertTrue(gotCallback);
89
// Get client invoker's MultiplexingManager.
90
MultiplexClientInvoker clientInvoker = (MultiplexClientInvoker) client.getInvoker();
91
field = MultiplexClientInvoker.class.getDeclaredField("socketGroupInfo");
92
field.setAccessible(true);
93
SocketGroupInfo sgi = (SocketGroupInfo) field.get(clientInvoker);
94
VirtualSocket socket = sgi.getPrimingSocket();
95
field = VirtualSocket.class.getDeclaredField("manager");
96
field.setAccessible(true);
97
MultiplexingManager clientManager = (MultiplexingManager) field.get(socket);
98
assertNotNull(clientManager);
100
// Get server invoker's MultiplexingManager.
101
field = Client.class.getDeclaredField("callbackConnectors");
102
field.setAccessible(true);
103
Map callbackConnectors = (Map) field.get(client);
104
assertEquals(1, callbackConnectors.size());
105
Set callbackConnectorSet = (Set) callbackConnectors.values().iterator().next();
106
assertEquals(1, callbackConnectorSet.size());
107
Connector connector = (Connector) callbackConnectorSet.iterator().next();
108
MultiplexServerInvoker serverInvoker = (MultiplexServerInvoker) connector.getServerInvoker();
109
field = SocketServerInvoker.class.getDeclaredField("serverSocket");
110
field.setAccessible(true);
111
VirtualServerSocket serverSocket = (VirtualServerSocket) field.get(serverInvoker);
112
field = VirtualServerSocket.class.getDeclaredField("manager");
113
field.setAccessible(true);
114
MultiplexingManager serverManager = (MultiplexingManager) field.get(serverSocket);
116
// Show client and server invokers are using the same MultiplexingManager.
117
assertEquals(clientManager, serverManager);
119
client.removeListener(testCallbackHandler);
124
// The connection should be closed and there should be 0 MultiplexingManagers.
125
assertEquals(0, allManagers.size());
128
public class TestCallbackHandler implements InvokerCallbackHandler
131
public void handleCallback(Callback callback) throws HandleCallbackException
133
System.out.println("callback = " + callback);
134
Object handle = callback.getCallbackHandleObject();
135
if ("foobar".equals(handle))