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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/transport/bisocket/CallbackDisconnectTestCase.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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.
 
6
*
 
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.
 
11
*
 
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.
 
16
*
 
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.
 
21
*/
 
22
package org.jboss.test.remoting.transport.bisocket;
 
23
 
 
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;
 
30
import java.util.Map;
 
31
import java.util.Set;
 
32
 
 
33
import javax.management.MBeanServer;
 
34
 
 
35
import junit.framework.TestCase;
 
36
 
 
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;
 
54
 
 
55
 
 
56
/**
 
57
 * Unit test for JBREM-767.
 
58
 * 
 
59
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
60
 * @version $Revision: 2732 $
 
61
 * <p>
 
62
 * Copyright Aug 4, 2007
 
63
 * </p>
 
64
 */
 
65
public class CallbackDisconnectTestCase extends TestCase
 
66
{
 
67
   public static int port;
 
68
   private static Logger log = Logger.getLogger(CallbackDisconnectTestCase.class);
 
69
   private static final String CALLBACK_TEST = "callbackTest";
 
70
   
 
71
   private static boolean firstTime = true;
 
72
   
 
73
   // remoting server connector
 
74
   private Connector connector;
 
75
   private InvokerLocator serverLocator;
 
76
   private TestInvocationHandler invocationHandler;
 
77
 
 
78
   
 
79
   /**
 
80
    * Sets up target remoting server.
 
81
    */
 
82
   public void setUp() throws Exception
 
83
   {
 
84
      if (firstTime)
 
85
      {
 
86
         firstTime = false;
 
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);  
 
93
      }
 
94
   }
 
95
 
 
96
   
 
97
   public void tearDown()
 
98
   {
 
99
   }
 
100
   
 
101
   
 
102
   /**
 
103
    * Verifies that BisocketClientInvoker.handleDisconnect() wakes up any threads
 
104
    * blocked in BisocketClientInvoker.createSocket().
 
105
    */
 
106
   public void testWakeupsInHandleDisconnect() throws Throwable
 
107
   {
 
108
      log.info("entering " + getName());
 
109
      
 
110
      // Start server.
 
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);
 
121
      connector.create();
 
122
      invocationHandler = new TestInvocationHandler();
 
123
      connector.addInvocationHandler("sample", invocationHandler);
 
124
      connector.start();
 
125
      
 
126
      // Create client.
 
127
      HashMap clientConfig = new HashMap();
 
128
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
129
      addExtraClientConfig(clientConfig);
 
130
      final Client client = new Client(serverLocator, clientConfig);
 
131
      client.connect();
 
132
      assertEquals("abc", client.invoke("abc"));
 
133
      log.info("client is connected");
 
134
      
 
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");
 
141
      
 
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());
 
157
      pool.clear();
 
158
      
 
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();
 
175
      
 
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
 
179
      {
 
180
         public boolean done;
 
181
         public Throwable throwable;
 
182
         public void run()
 
183
         {
 
184
            try
 
185
            {
 
186
               log.info("calling client.invoke(CALLBACK_TEST)");
 
187
               client.invoke(CALLBACK_TEST);
 
188
               log.info("called client.invoke(CALLBACK_TEST)");
 
189
            }
 
190
            catch (Throwable t)
 
191
            {
 
192
               log.info("TestThread got expected exception: " + t.getMessage());
 
193
               throwable = t;
 
194
            }
 
195
            done = true;
 
196
         }
 
197
      }
 
198
      
 
199
      TestThread testThread = new TestThread();
 
200
      testThread.start();
 
201
      Thread.sleep(2000);
 
202
      callbackClient.disconnect();
 
203
      Thread.sleep(2000);
 
204
      assertTrue(testThread.done);
 
205
      assertNotNull(testThread.throwable);
 
206
      assertEquals("Error handling callback", testThread.throwable.getMessage());
 
207
      
 
208
      client.removeListener(callbackHandler);
 
209
      client.disconnect();
 
210
      connector.stop();
 
211
   }
 
212
   
 
213
   
 
214
   protected String getTransport()
 
215
   {
 
216
      return "bisocket";
 
217
   }
 
218
   
 
219
   
 
220
   protected void addExtraClientConfig(Map config) {}
 
221
   protected void addExtraServerConfig(Map config) {}
 
222
   
 
223
 
 
224
   static class TestInvocationHandler implements ServerInvocationHandler
 
225
   {
 
226
      public Set listeners = new HashSet();
 
227
      
 
228
      public void addListener(InvokerCallbackHandler callbackHandler)
 
229
      {
 
230
         listeners.add(callbackHandler);
 
231
      }
 
232
      public Object invoke(final InvocationRequest invocation) throws Throwable
 
233
      {
 
234
         if (CALLBACK_TEST.equals(invocation.getParameter()))
 
235
         {
 
236
            Iterator it = listeners.iterator();
 
237
            while (it.hasNext())
 
238
            {
 
239
               InvokerCallbackHandler handler = (InvokerCallbackHandler) it.next();
 
240
               handler.handleCallback(new Callback("test"));
 
241
            }
 
242
         }
 
243
         return invocation.getParameter();
 
244
      }
 
245
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
246
      public void setMBeanServer(MBeanServer server) {}
 
247
      public void setInvoker(ServerInvoker invoker) {}
 
248
   }
 
249
   
 
250
   static class TestInvokerCallbackHandler implements InvokerCallbackHandler
 
251
   {
 
252
      public int counter;
 
253
      
 
254
      public void handleCallback(Callback callback) throws HandleCallbackException
 
255
      {
 
256
         counter++;
 
257
      }
 
258
   }
 
259
}
 
 
b'\\ No newline at end of file'