~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/TimerReuseTestCase.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.Map;
 
28
import java.util.Set;
 
29
import java.util.Timer;
 
30
import java.util.TimerTask;
 
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.remoting.Client;
 
41
import org.jboss.remoting.InvocationRequest;
 
42
import org.jboss.remoting.InvokerLocator;
 
43
import org.jboss.remoting.ServerInvocationHandler;
 
44
import org.jboss.remoting.ServerInvoker;
 
45
import org.jboss.remoting.callback.Callback;
 
46
import org.jboss.remoting.callback.HandleCallbackException;
 
47
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
48
import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
 
49
import org.jboss.remoting.transport.Connector;
 
50
import org.jboss.remoting.transport.PortUtil;
 
51
import org.jboss.remoting.transport.bisocket.Bisocket;
 
52
import org.jboss.remoting.transport.bisocket.BisocketClientInvoker;
 
53
import org.jboss.remoting.transport.bisocket.BisocketServerInvoker;
 
54
import org.jboss.test.remoting.transport.bisocket.ServerTimerReuseTestCase.TestCallbackHandler;
 
55
import org.jboss.test.remoting.transport.bisocket.ServerTimerReuseTestCase.TestInvocationHandler;
 
56
 
 
57
 
 
58
/**
 
59
 * TimerReuseTestCase verifies that it is safe to add a callback BisocketClientInvoker
 
60
 * after the static Timer used by the PingTimerTasks has shut down because all previous
 
61
 * PingTimerTasks have ended.  
 
62
 *  
 
63
 * See JBREM-748.
 
64
 * 
 
65
 * The phenomenon described in JBREM-748 seems to be platform dependent.  It's not clear,
 
66
 * for example, how long it takes for a Timer to shut down after all of its tasks have
 
67
 * ended.  Therefore, there are multiple test methods that wait varying amounts of
 
68
 * time before installing a second InvokerCallbackHandler.
 
69
 * 
 
70
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
71
 * @version $Revision: 2737 $
 
72
 * <p>
 
73
 * Copyright May 31, 2007
 
74
 * </p>
 
75
 */
 
76
public class TimerReuseTestCase extends TestCase
 
77
{
 
78
   public static int port;
 
79
  
 
80
   private static Logger log = Logger.getLogger(TimerReuseTestCase.class);
 
81
   private static boolean firstTime = true;
 
82
   
 
83
   // remoting server connector
 
84
   private Connector connector;
 
85
   private InvokerLocator serverLocator;
 
86
   private SampleInvocationHandler invocationHandler;
 
87
 
 
88
 
 
89
   public void setUp() throws Exception
 
90
   {
 
91
      if (firstTime)
 
92
      {
 
93
         firstTime = false;
 
94
         Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
 
95
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
 
96
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
 
97
         PatternLayout layout = new PatternLayout(pattern);
 
98
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
 
99
         Logger.getRootLogger().addAppender(consoleAppender);  
 
100
      }
 
101
   }
 
102
 
 
103
   
 
104
   public void tearDown()
 
105
   {
 
106
   }
 
107
   
 
108
   
 
109
   public void testTimerRestartImmdiately() throws Throwable
 
110
   {
 
111
      log.info("entering " + getName());
 
112
      doTestTimerReuse(1);
 
113
   }
 
114
   
 
115
   
 
116
   public void testTimerRestartAfter1Second() throws Throwable
 
117
   {
 
118
      log.info("entering " + getName());
 
119
      doTestTimerReuse(1000);
 
120
   }
 
121
   
 
122
   
 
123
   public void testTimerRestartAfter5Seconds() throws Throwable
 
124
   {
 
125
      log.info("entering " + getName());
 
126
      doTestTimerReuse(5000);
 
127
   }
 
128
   
 
129
   
 
130
   public void testTimerRestartAfter30Seconds() throws Throwable
 
131
   {
 
132
      log.info("entering " + getName());
 
133
      doTestTimerReuse(30000);
 
134
   }
 
135
   
 
136
   public void testNewControlConnectionImmdiately() throws Throwable
 
137
   {
 
138
      log.info("entering " + getName());
 
139
      doTestNewControlConnectionTimerReuse(1);
 
140
   }
 
141
   
 
142
   public void testNewControlConnectionAfter1Second() throws Throwable
 
143
   {
 
144
      log.info("entering " + getName());
 
145
      doTestNewControlConnectionTimerReuse(1000);
 
146
   }
 
147
   
 
148
   
 
149
   public void testNewControlConnectionAfter5Seconds() throws Throwable
 
150
   {
 
151
      log.info("entering " + getName());
 
152
      doTestNewControlConnectionTimerReuse(5000);
 
153
   }
 
154
   
 
155
   
 
156
   public void testNewControlConnectionAfter30Seconds() throws Throwable
 
157
   {
 
158
      log.info("entering " + getName());
 
159
      doTestNewControlConnectionTimerReuse(30000);
 
160
   }
 
161
   
 
162
   
 
163
   public void doTestTimerReuse(int waitPeriod) throws Throwable
 
164
   {
 
165
      // Start server.
 
166
      String host = InetAddress.getLocalHost().getHostAddress();
 
167
      port = PortUtil.findFreePort(host);
 
168
      String locatorURI = getTransport() + "://" + host + ":" + port; 
 
169
      serverLocator = new InvokerLocator(locatorURI);
 
170
      log.info("Starting remoting server with locator uri of: " + locatorURI);
 
171
      HashMap config = new HashMap();
 
172
      config.put(InvokerLocator.FORCE_REMOTE, "true");
 
173
      addExtraServerConfig(config);
 
174
      connector = new Connector(serverLocator, config);
 
175
      connector.create();
 
176
      invocationHandler = new SampleInvocationHandler();
 
177
      connector.addInvocationHandler("sample", invocationHandler);
 
178
      connector.start();
 
179
      
 
180
      // Create client.
 
181
      HashMap clientConfig = new HashMap();
 
182
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
183
      addExtraClientConfig(clientConfig);
 
184
      Client client = new Client(serverLocator, clientConfig);
 
185
      client.connect();
 
186
      log.info("Client is connected");
 
187
      TestCallbackHandler callbackHandler = new TestCallbackHandler();
 
188
      client.addListener(callbackHandler, new HashMap());
 
189
      log.info("First callback handler is installed.");
 
190
      
 
191
      // Reinstall callback handler.  Test passes if no exception is thrown.
 
192
      client.removeListener(callbackHandler);
 
193
      Thread.sleep(waitPeriod);
 
194
      client.addListener(callbackHandler, new HashMap());
 
195
      log.info("Second callback handler is installed.");
 
196
 
 
197
      client.removeListener(callbackHandler);
 
198
      client.disconnect();
 
199
      connector.stop();
 
200
   }
 
201
   
 
202
   
 
203
   /**
 
204
    * Verifies that BisocketClientInvoker.replaceControlSocket() reuses static
 
205
    * Timer, if possible.
 
206
    */
 
207
   public void doTestNewControlConnectionTimerReuse(int waitPeriod) throws Throwable
 
208
   {
 
209
      log.info("entering " + getName());
 
210
      
 
211
      // Start server.
 
212
      String host = InetAddress.getLocalHost().getHostAddress();
 
213
      int port = PortUtil.findFreePort(host);
 
214
      String locatorURI = getTransport() + "://" + host + ":" + port; 
 
215
      serverLocator = new InvokerLocator(locatorURI);
 
216
      log.info("Starting remoting server with locator uri of: " + locatorURI);
 
217
      HashMap config = new HashMap();
 
218
      config.put(InvokerLocator.FORCE_REMOTE, "true");
 
219
      addExtraServerConfig(config);
 
220
      connector = new Connector(serverLocator, config);
 
221
      connector.create();
 
222
      invocationHandler = new SampleInvocationHandler();
 
223
      connector.addInvocationHandler("sample", invocationHandler);
 
224
      connector.start();
 
225
      
 
226
      // Create client.
 
227
      InvokerLocator clientLocator1 = new InvokerLocator(locatorURI);
 
228
      HashMap clientConfig = new HashMap();
 
229
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
230
      addExtraClientConfig(clientConfig);
 
231
      Client client = new Client(clientLocator1, clientConfig);
 
232
      client.connect();
 
233
      log.info("client is connected");
 
234
      
 
235
      // Test connection.
 
236
      assertEquals("abc", client.invoke("abc"));
 
237
      log.info("connection is good");
 
238
      
 
239
      // Add callback handler.
 
240
      TestCallbackHandler callbackHandler = new TestCallbackHandler();
 
241
      HashMap metadata = new HashMap();
 
242
      metadata.put(Bisocket.IS_CALLBACK_SERVER, "true");
 
243
      client.addListener(callbackHandler, metadata);
 
244
      
 
245
      // Stop PingTimerTask on server.  This allows the possibility that the Timer
 
246
      // will be replaced.
 
247
      BisocketServerInvoker serverInvoker = (BisocketServerInvoker) connector.getServerInvoker();
 
248
      Field field = ServerInvoker.class.getDeclaredField("callbackHandlers");
 
249
      field.setAccessible(true);
 
250
      Map callbackHandlers = (Map) field.get(serverInvoker);
 
251
      assertEquals(1, callbackHandlers.size());
 
252
      ServerInvokerCallbackHandler serverInvokerCallbackHandler;
 
253
      serverInvokerCallbackHandler = (ServerInvokerCallbackHandler) callbackHandlers.values().iterator().next();
 
254
      field = ServerInvokerCallbackHandler.class.getDeclaredField("callBackClient");
 
255
      field.setAccessible(true);
 
256
      Client callbackClient = (Client) field.get(serverInvokerCallbackHandler);
 
257
      assertNotNull(callbackClient);
 
258
      BisocketClientInvoker callbackClientInvoker = (BisocketClientInvoker) callbackClient.getInvoker();
 
259
      field = BisocketClientInvoker.class.getDeclaredField("pingTimerTask");
 
260
      field.setAccessible(true);
 
261
      TimerTask pingTimerTask1 = (TimerTask) field.get(callbackClientInvoker);
 
262
      BisocketClientInvoker ci = (BisocketClientInvoker) client.getInvoker();
 
263
      assertNotNull(pingTimerTask1);
 
264
      pingTimerTask1.cancel();
 
265
      
 
266
      // Cause recreation of PingTimerTask on server.  If call below to
 
267
      // createControlConnection() doesn't throw an exception, then the test passes.
 
268
      Thread.sleep(waitPeriod);
 
269
      field = Client.class.getDeclaredField("callbackConnectors");
 
270
      field.setAccessible(true);
 
271
      Map callbackConnectors = (Map) field.get(client);
 
272
      assertEquals(1, callbackConnectors.size());
 
273
      Set connectors = (Set) callbackConnectors.values().iterator().next();
 
274
      assertEquals(1, connectors.size());
 
275
      Connector callbackConnector = (Connector) connectors.iterator().next();
 
276
      BisocketServerInvoker callbackInvoker = (BisocketServerInvoker) callbackConnector.getServerInvoker();
 
277
      field = BisocketServerInvoker.class.getDeclaredField("controlConnectionThreadMap");
 
278
      field.setAccessible(true);
 
279
      Map controlConnectionThreadMap = (Map) field.get(callbackInvoker);
 
280
      assertEquals(1, controlConnectionThreadMap.size());
 
281
      Thread controlConnectionThread = (Thread) controlConnectionThreadMap.values().iterator().next();
 
282
      Class[] classes = BisocketServerInvoker.class.getDeclaredClasses();
 
283
      Class ControlConnectionThreadClass = null;
 
284
      
 
285
      for (int i = 0; i < classes.length; i++)
 
286
      {
 
287
         if (classes[i].toString().indexOf("ControlConnectionThread") != -1)
 
288
         {
 
289
            ControlConnectionThreadClass = classes[i];
 
290
            break;
 
291
         }
 
292
      }
 
293
      
 
294
      assertNotNull(ControlConnectionThreadClass);
 
295
      field = ControlConnectionThreadClass.getDeclaredField("listenerId");
 
296
      field.setAccessible(true);
 
297
      String listenerId = (String) field.get(controlConnectionThread);
 
298
      assertNotNull(listenerId);
 
299
      callbackInvoker.createControlConnection(listenerId, false);
 
300
      Thread.sleep(2000);
 
301
      
 
302
      // Get PingTimerTask and verify it has changed.
 
303
      field = BisocketClientInvoker.class.getDeclaredField("pingTimerTask");
 
304
      field.setAccessible(true);
 
305
      TimerTask pingTimerTask2 = (TimerTask) field.get(callbackClientInvoker);
 
306
      assertNotSame(pingTimerTask1, pingTimerTask2);
 
307
      
 
308
      // Shut down.
 
309
      client.removeListener(callbackHandler);
 
310
      client.disconnect();
 
311
      connector.stop();
 
312
      log.info(getName() + " PASSES");
 
313
   }
 
314
   
 
315
   
 
316
   protected String getTransport()
 
317
   {
 
318
      return "bisocket";
 
319
   }
 
320
   
 
321
   
 
322
   protected void addExtraClientConfig(Map config) {}
 
323
   protected void addExtraServerConfig(Map config) {}
 
324
   
 
325
 
 
326
   static class SampleInvocationHandler implements ServerInvocationHandler
 
327
   {
 
328
      public void addListener(InvokerCallbackHandler callbackHandler)
 
329
      {
 
330
         try
 
331
         {
 
332
            callbackHandler.handleCallback(new Callback("callback"));
 
333
         }
 
334
         catch (HandleCallbackException e)
 
335
         {
 
336
            log.error("unable to send callback", e);
 
337
         }
 
338
      }
 
339
      public Object invoke(final InvocationRequest invocation) throws Throwable
 
340
      {
 
341
         return invocation.getParameter();
 
342
      }
 
343
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
344
      public void setMBeanServer(MBeanServer server) {}
 
345
      public void setInvoker(ServerInvoker invoker) {}
 
346
   }
 
347
   
 
348
   static class TestCallbackHandler implements InvokerCallbackHandler
 
349
   {
 
350
      public int count;
 
351
      public void handleCallback(Callback callback) throws HandleCallbackException
 
352
      {
 
353
         count++;
 
354
      }
 
355
   }
 
356
}
 
 
b'\\ No newline at end of file'