~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/callback/push/bidirectional/multiplex/MultiplexCallbackTestClient.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: package-import@ubuntu.com-20110909140103-o8ucrolqt5g25k57
Tags: upstream-2.5.3.SP1
ImportĀ upstreamĀ versionĀ 2.5.3.SP1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.jboss.test.remoting.callback.push.bidirectional.multiplex;
 
2
 
 
3
import junit.framework.TestCase;
 
4
 
 
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;
 
18
 
 
19
import java.lang.reflect.Field;
 
20
import java.util.HashMap;
 
21
import java.util.Map;
 
22
import java.util.Set;
 
23
 
 
24
/**
 
25
 * @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
 
26
 */
 
27
public class MultiplexCallbackTestClient extends TestCase
 
28
{
 
29
   private String locatorUri = "multiplex://localhost:8999";
 
30
   private boolean gotCallback = false;
 
31
 
 
32
   public void testCallback() throws Throwable
 
33
   {
 
34
      Client client = new Client(new InvokerLocator(locatorUri));
 
35
      client.connect();
 
36
      InvokerCallbackHandler testCallbackHandler = new MultiplexCallbackTestClient.TestCallbackHandler();
 
37
      client.addListener(testCallbackHandler, new HashMap(), "foobar");
 
38
      client.invoke("foobar");
 
39
 
 
40
      Thread.sleep(5000);
 
41
 
 
42
      client.removeListener(testCallbackHandler);
 
43
      client.disconnect();
 
44
 
 
45
      assertTrue(gotCallback);
 
46
   }
 
47
   
 
48
 
 
49
   
 
50
   /**
 
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.
 
54
    * 
 
55
    * @throws Throwable
 
56
    */
 
57
   public void testSharedCallbackConnection() throws Throwable
 
58
   {
 
59
      // Wait for any existing MultiplexingManagers to close.
 
60
      Thread.sleep(5000);
 
61
      
 
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());
 
68
      
 
69
      // Create and connect Client.
 
70
      Client client = new Client(new InvokerLocator(locatorUri));
 
71
      client.connect();
 
72
 
 
73
      // There should be 1 MultiplexingManager now that Client is connected.
 
74
      assertEquals(1, allManagers.size());
 
75
      
 
76
      // Add a push CallbackHandler.
 
77
      InvokerCallbackHandler testCallbackHandler = new TestCallbackHandler();
 
78
      client.addListener(testCallbackHandler, new HashMap(), "foobar");
 
79
      
 
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());
 
83
      
 
84
      // Show connection works.
 
85
      client.invoke("foobar");
 
86
      Thread.sleep(5000);
 
87
      assertTrue(gotCallback);
 
88
      
 
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);
 
99
      
 
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);
 
115
      
 
116
      // Show client and server invokers are using the same MultiplexingManager.
 
117
      assertEquals(clientManager, serverManager);
 
118
      
 
119
      client.removeListener(testCallbackHandler);
 
120
      client.disconnect();
 
121
      
 
122
      Thread.sleep(5000);
 
123
      
 
124
      // The connection should be closed and there should be 0 MultiplexingManagers.
 
125
      assertEquals(0, allManagers.size());
 
126
   }
 
127
 
 
128
   public class TestCallbackHandler implements InvokerCallbackHandler
 
129
   {
 
130
 
 
131
      public void handleCallback(Callback callback) throws HandleCallbackException
 
132
      {
 
133
         System.out.println("callback = " + callback);
 
134
         Object handle = callback.getCallbackHandleObject();
 
135
         if ("foobar".equals(handle))
 
136
         {
 
137
            gotCallback = true;
 
138
         }
 
139
      }
 
140
   }
 
141
}