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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/transport/bisocket/CallbackClientWithZeroTimeoutTestCase.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.io.IOException;
 
25
import java.lang.reflect.Field;
 
26
import java.net.InetAddress;
 
27
import java.net.Socket;
 
28
import java.util.HashMap;
 
29
import java.util.Map;
 
30
import java.util.Set;
 
31
 
 
32
import javax.management.MBeanServer;
 
33
 
 
34
import junit.framework.TestCase;
 
35
 
 
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;
 
55
 
 
56
 
 
57
/**
 
58
 * Unit test for JBREM-845.
 
59
 * 
 
60
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
61
 * @version $Revision: 1.1 $
 
62
 * <p>
 
63
 * Copyright Dec 11, 2007
 
64
 * </p>
 
65
 */
 
66
public class CallbackClientWithZeroTimeoutTestCase extends TestCase
 
67
{
 
68
   private static Logger log = Logger.getLogger(CallbackClientWithZeroTimeoutTestCase.class);
 
69
   
 
70
   private static boolean firstTime = true;
 
71
   private static String CALLBACK = "callback";
 
72
   protected String host;
 
73
   protected int port;
 
74
   protected String locatorURI;
 
75
   protected InvokerLocator serverLocator;
 
76
   protected Connector connector;
 
77
   protected TestInvocationHandler invocationHandler;
 
78
 
 
79
   
 
80
   public void setUp() throws Exception
 
81
   {
 
82
      if (firstTime)
 
83
      {
 
84
         firstTime = false;
 
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);  
 
91
      }
 
92
   }
 
93
 
 
94
   
 
95
   public void tearDown()
 
96
   {
 
97
   }
 
98
   
 
99
   
 
100
   public void testCallbackSocketFailure() throws Throwable
 
101
   {
 
102
      log.info("entering " + getName());
 
103
      
 
104
      // Start server.
 
105
      setupServer();
 
106
      
 
107
      // Create client.
 
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);
 
113
      client.connect();
 
114
      log.info("client is connected");
 
115
      
 
116
      // Test connections.
 
117
      assertEquals("abc", client.invoke("abc"));
 
118
      log.info("connection is good");
 
119
      
 
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");
 
126
      
 
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");
 
132
      
 
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);
 
145
  
 
146
      Class controlConnectionThreadClass = null;
 
147
      Class[] classes = BisocketServerInvoker.class.getDeclaredClasses();
 
148
      for (int i = 0; i < classes.length; i++)
 
149
      {
 
150
         log.info(classes[i]);
 
151
         if (classes[i].getName().indexOf("ControlConnectionThread") >= 0)
 
152
         {
 
153
            controlConnectionThreadClass = classes[i];
 
154
            break;
 
155
         }
 
156
      }
 
157
      assertNotNull(controlConnectionThreadClass);
 
158
      field = controlConnectionThreadClass.getDeclaredField("controlSocket");
 
159
      field.setAccessible(true);
 
160
      Socket controlSocket = (Socket) field.get(controlConnectionThread);
 
161
      assertNotNull(controlSocket);
 
162
      
 
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");
 
171
      
 
172
      // 3. Try to do callback.
 
173
      client.invokeOneway(CALLBACK);
 
174
      log.info("requested CALLBACK");
 
175
      
 
176
      // 4. Close client side control socket so PING fails.
 
177
      controlSocket.close();
 
178
      log.info("closed controlSocket");
 
179
      
 
180
      // 5. Test that attempt to create a socket for callback threw exception.
 
181
      Thread.sleep(5000);
 
182
      assertTrue(invocationHandler.ok);
 
183
      log.info("got expected Exception doing callback");
 
184
 
 
185
      client.removeListener(callbackHandler);
 
186
      client.disconnect();
 
187
      shutdownServer();
 
188
      log.info(getName() + " PASSES");
 
189
   }
 
190
   
 
191
   
 
192
   protected String getTransport()
 
193
   {
 
194
      return "bisocket";
 
195
   }
 
196
   
 
197
   
 
198
   protected void addExtraClientConfig(Map config) {}
 
199
   protected void addExtraServerConfig(Map config) {}
 
200
   
 
201
 
 
202
   protected void setupServer() throws Exception
 
203
   {
 
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);
 
216
      connector.create();
 
217
      invocationHandler = new TestInvocationHandler();
 
218
      connector.addInvocationHandler("test", invocationHandler);
 
219
      connector.start();
 
220
   }
 
221
   
 
222
   
 
223
   protected void shutdownServer() throws Exception
 
224
   {
 
225
      if (connector != null)
 
226
         connector.stop();
 
227
   }
 
228
   
 
229
   
 
230
   static class TestInvocationHandler implements ServerInvocationHandler
 
231
   {
 
232
      public boolean ok;
 
233
      public ServerInvokerCallbackHandler callbackHandler;
 
234
      
 
235
      public void addListener(InvokerCallbackHandler callbackHandler)
 
236
      {
 
237
         this.callbackHandler = (ServerInvokerCallbackHandler) callbackHandler;
 
238
      }
 
239
      public Object invoke(final InvocationRequest invocation) throws Throwable
 
240
      {
 
241
         if (CALLBACK.equals(invocation.getParameter()))
 
242
         {
 
243
            try
 
244
            {
 
245
               log.info("calling handleCallback()");
 
246
               callbackHandler.handleCallback(new Callback(CALLBACK));
 
247
               log.info("called handleCallback()");
 
248
            }
 
249
            catch (HandleCallbackException e)
 
250
            {
 
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()))
 
257
               {
 
258
                  ok = true;
 
259
               }
 
260
            }
 
261
         }
 
262
         return invocation.getParameter();
 
263
      }
 
264
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
265
      public void setMBeanServer(MBeanServer server) {}
 
266
      public void setInvoker(ServerInvoker invoker) {}
 
267
   }
 
268
   
 
269
   
 
270
   static class TestCallbackHandler implements InvokerCallbackHandler
 
271
   {
 
272
      public boolean ok;
 
273
      
 
274
      public void handleCallback(Callback callback) throws HandleCallbackException
 
275
      {
 
276
         ok = true;
 
277
         log.info("received callback");
 
278
      }  
 
279
   }
 
280
}
 
 
b'\\ No newline at end of file'