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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/transport/bisocket/timertask/TimerTaskTestCase.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.timertask;
 
23
 
 
24
import java.lang.reflect.Field;
 
25
import java.lang.reflect.Method;
 
26
import java.net.InetAddress;
 
27
import java.security.AccessController;
 
28
import java.security.PrivilegedActionException;
 
29
import java.security.PrivilegedExceptionAction;
 
30
import java.util.HashMap;
 
31
import java.util.Map;
 
32
import java.util.Set;
 
33
import java.util.Timer;
 
34
import java.util.TimerTask;
 
35
 
 
36
import javax.management.MBeanServer;
 
37
 
 
38
import junit.framework.TestCase;
 
39
 
 
40
import org.apache.log4j.ConsoleAppender;
 
41
import org.apache.log4j.Level;
 
42
import org.apache.log4j.Logger;
 
43
import org.apache.log4j.PatternLayout;
 
44
import org.jboss.logging.XLevel;
 
45
import org.jboss.remoting.Client;
 
46
import org.jboss.remoting.InvocationRequest;
 
47
import org.jboss.remoting.InvokerLocator;
 
48
import org.jboss.remoting.ServerInvocationHandler;
 
49
import org.jboss.remoting.ServerInvoker;
 
50
import org.jboss.remoting.callback.Callback;
 
51
import org.jboss.remoting.callback.HandleCallbackException;
 
52
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
53
import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
 
54
import org.jboss.remoting.transport.Connector;
 
55
import org.jboss.remoting.transport.PortUtil;
 
56
import org.jboss.remoting.transport.bisocket.Bisocket;
 
57
import org.jboss.remoting.transport.bisocket.BisocketClientInvoker;
 
58
import org.jboss.remoting.transport.bisocket.BisocketServerInvoker;
 
59
import org.jboss.remoting.util.SecurityUtility;
 
60
 
 
61
 
 
62
/**
 
63
 * Unit test for JBREM-1005.
 
64
 * 
 
65
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
66
 * @version $Revision: 1.1 $
 
67
 * <p>
 
68
 * Copyright Jun 26, 2008
 
69
 * </p>
 
70
 */
 
71
public class TimerTaskTestCase extends TestCase
 
72
{
 
73
   private static Logger log = Logger.getLogger(TimerTaskTestCase.class);
 
74
   
 
75
   private static boolean firstTime = true;
 
76
   private static boolean purgeMethodAvailable;
 
77
   
 
78
   protected String host;
 
79
   protected int port;
 
80
   protected String locatorURI;
 
81
   protected InvokerLocator serverLocator;
 
82
   protected Connector connector;
 
83
   protected TestInvocationHandler invocationHandler;
 
84
 
 
85
   
 
86
   public void setUp() throws Exception
 
87
   {
 
88
      if (firstTime)
 
89
      {
 
90
         firstTime = false;
 
91
         Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
 
92
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
 
93
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
 
94
         PatternLayout layout = new PatternLayout(pattern);
 
95
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
 
96
         Logger.getRootLogger().addAppender(consoleAppender);
 
97
         
 
98
         try
 
99
         {
 
100
            getDeclaredMethod(Timer.class, "purge", new Class[]{});
 
101
            purgeMethodAvailable = true;
 
102
         }
 
103
         catch (Exception e)
 
104
         {
 
105
            log.info("Timer.purge() is not available: must be running with jdk 1.4");
 
106
         }
 
107
      }
 
108
   }
 
109
 
 
110
   
 
111
   public void tearDown()
 
112
   {
 
113
   }
 
114
   
 
115
   
 
116
   public void testZeroPingFrequency() throws Throwable
 
117
   {
 
118
      log.info("entering " + getName());
 
119
      
 
120
      if (!purgeMethodAvailable)
 
121
      {
 
122
         log.info(getName() + " PASSES (trivially)");
 
123
         return;
 
124
      }
 
125
      
 
126
      // Start server.
 
127
      setupServer("0");
 
128
      
 
129
      // Create client.
 
130
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
 
131
      HashMap clientConfig = new HashMap();
 
132
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
133
      addExtraClientConfig(clientConfig);
 
134
      Client client = new Client(clientLocator, clientConfig);
 
135
      client.connect();
 
136
      
 
137
      // Test connections.
 
138
      assertEquals("abc", client.invoke("abc"));
 
139
      log.info("connection is good");
 
140
      
 
141
      // Set up callback handler.
 
142
      TestCallbackHandler callbackHandler = new TestCallbackHandler();
 
143
      HashMap metadata = new HashMap();
 
144
      metadata.put(Bisocket.IS_CALLBACK_SERVER, "true");
 
145
      client.addListener(callbackHandler, metadata);
 
146
      assertEquals(1, callbackHandler.counter);
 
147
     
 
148
      // Verify ControlMonitorTimerTask is not created.
 
149
      Set callbackConnectors = client.getCallbackConnectors(callbackHandler);
 
150
      assertEquals(1, callbackConnectors.size());
 
151
      Connector callbackConnector = (Connector) callbackConnectors.iterator().next();
 
152
      Field field = BisocketServerInvoker.class.getDeclaredField("controlMonitorTimerTask");
 
153
      field.setAccessible(true);      
 
154
      BisocketServerInvoker serverInvoker = (BisocketServerInvoker) callbackConnector.getServerInvoker();
 
155
      TimerTask timerTask = (TimerTask) field.get(serverInvoker);
 
156
      assertNull(timerTask);
 
157
      log.info("ControlMonitorTimerTask was not created");
 
158
      
 
159
      // Verify PingTimerTask is not created.
 
160
      field = ServerInvoker.class.getDeclaredField("callbackHandlers");
 
161
      field.setAccessible(true);
 
162
      serverInvoker = (BisocketServerInvoker) connector.getServerInvoker();
 
163
      Map callbackHandlers = (Map) field.get(serverInvoker);
 
164
      assertEquals(1, callbackHandlers.size());
 
165
      Object o = callbackHandlers.values().iterator().next();
 
166
      Client callbackClient = ((ServerInvokerCallbackHandler) o).getCallbackClient();
 
167
      BisocketClientInvoker clientInvoker = (BisocketClientInvoker) callbackClient.getInvoker();
 
168
      field = BisocketClientInvoker.class.getDeclaredField("pingTimerTask");
 
169
      field.setAccessible(true);
 
170
      timerTask = (TimerTask) field.get(clientInvoker);
 
171
      assertNull(timerTask);
 
172
      log.info("PingTimerTask was not created");
 
173
      
 
174
      client.disconnect();
 
175
      shutdownServer();
 
176
      log.info(getName() + " PASSES");
 
177
   }
 
178
   
 
179
   
 
180
   public void testNonZeroPingFrequency() throws Throwable
 
181
   {
 
182
      log.info("entering " + getName());
 
183
      
 
184
      if (!purgeMethodAvailable)
 
185
      {
 
186
         log.info(getName() + " PASSES (trivially)");
 
187
         return;
 
188
      }
 
189
      
 
190
      // Start server.
 
191
      setupServer("20");
 
192
      
 
193
      // Create client.
 
194
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
 
195
      HashMap clientConfig = new HashMap();
 
196
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
197
      addExtraClientConfig(clientConfig);
 
198
      Client client = new Client(clientLocator, clientConfig);
 
199
      client.connect();
 
200
      
 
201
      // Test connections.
 
202
      assertEquals("abc", client.invoke("abc"));
 
203
      log.info("connection is good");
 
204
      
 
205
      // Set up callback handler.
 
206
      TestCallbackHandler callbackHandler = new TestCallbackHandler();
 
207
      HashMap metadata = new HashMap();
 
208
      metadata.put(Bisocket.IS_CALLBACK_SERVER, "true");
 
209
      client.addListener(callbackHandler, metadata);
 
210
      assertEquals(1, callbackHandler.counter);
 
211
     
 
212
      // Verify ControlMonitorTimerTask is created.
 
213
      Set callbackConnectors = client.getCallbackConnectors(callbackHandler);
 
214
      assertEquals(1, callbackConnectors.size());
 
215
      Connector callbackConnector = (Connector) callbackConnectors.iterator().next();
 
216
      Field field = BisocketServerInvoker.class.getDeclaredField("controlMonitorTimerTask");
 
217
      field.setAccessible(true);      
 
218
      BisocketServerInvoker serverInvoker = (BisocketServerInvoker) callbackConnector.getServerInvoker();
 
219
      TimerTask timerTask = (TimerTask) field.get(serverInvoker);
 
220
      assertNotNull(timerTask);
 
221
      log.info("ControlMonitorTimerTask was created");
 
222
      
 
223
      // Verify PingTimerTask is created.
 
224
      field = ServerInvoker.class.getDeclaredField("callbackHandlers");
 
225
      field.setAccessible(true);
 
226
      BisocketServerInvoker callbackServerInvoker = (BisocketServerInvoker) connector.getServerInvoker();
 
227
      Map callbackHandlers = (Map) field.get(callbackServerInvoker);
 
228
      assertEquals(1, callbackHandlers.size());
 
229
      Object o = callbackHandlers.values().iterator().next();
 
230
      Client callbackClient = ((ServerInvokerCallbackHandler) o).getCallbackClient();
 
231
      BisocketClientInvoker clientInvoker = (BisocketClientInvoker) callbackClient.getInvoker();
 
232
      field = BisocketClientInvoker.class.getDeclaredField("pingTimerTask");
 
233
      field.setAccessible(true);
 
234
      timerTask = (TimerTask) field.get(clientInvoker);
 
235
      assertNotNull(timerTask);
 
236
      log.info("PingTimerTask was created");
 
237
      
 
238
      // Get count of ControlMonitorTimerTasks.
 
239
      field = BisocketServerInvoker.class.getDeclaredField("timer");
 
240
      field.setAccessible(true);
 
241
      Timer timer = (Timer) field.get(null);
 
242
      field = Timer.class.getDeclaredField("queue");
 
243
      field.setAccessible(true);
 
244
      Object taskQueue = field.get(timer);
 
245
      Field serverTimerfield = taskQueue.getClass().getDeclaredField("size");
 
246
      serverTimerfield.setAccessible(true);
 
247
      int serverSize = ((Integer) serverTimerfield.get(taskQueue)).intValue();
 
248
      log.info("ControlMonitorTimerTasks: " + serverSize);
 
249
      
 
250
      // Get count of PingTimerTasks.
 
251
      field = BisocketClientInvoker.class.getDeclaredField("timer");
 
252
      field.setAccessible(true);
 
253
      timer = (Timer) field.get(null);
 
254
      field = Timer.class.getDeclaredField("queue");
 
255
      field.setAccessible(true);
 
256
      taskQueue = field.get(timer);
 
257
      Field clientTimerfield = taskQueue.getClass().getDeclaredField("size");
 
258
      clientTimerfield.setAccessible(true);
 
259
      int clientSize = ((Integer) clientTimerfield.get(taskQueue)).intValue();
 
260
      log.info("PingTimerTasks: " + clientSize);
 
261
 
 
262
      // Shut down callback connection and verify TimerTasks are removed from 
 
263
      // Timer queue.
 
264
      client.removeListener(callbackHandler);
 
265
      serverSize = ((Integer) serverTimerfield.get(taskQueue)).intValue();
 
266
      log.info("ControlMonitorTimerTasks: " + serverSize);
 
267
      assertEquals(0, serverSize);
 
268
      clientSize = ((Integer) clientTimerfield.get(taskQueue)).intValue();
 
269
      log.info("PingTimerTasks: " + clientSize);
 
270
      assertEquals(0, clientSize);
 
271
      
 
272
      client.disconnect();
 
273
      shutdownServer();
 
274
      log.info(getName() + " PASSES");
 
275
   }
 
276
   
 
277
   
 
278
   protected String getTransport()
 
279
   {
 
280
      return "bisocket";
 
281
   }
 
282
   
 
283
   
 
284
   protected void addExtraClientConfig(Map config) {}
 
285
   protected void addExtraServerConfig(Map config) {}
 
286
   
 
287
 
 
288
   protected void setupServer(String pingFrequency) throws Exception
 
289
   {
 
290
      host = InetAddress.getLocalHost().getHostAddress();
 
291
      port = PortUtil.findFreePort(host);
 
292
      locatorURI = getTransport() + "://" + host + ":" + port; 
 
293
      locatorURI += "/?" + Bisocket.PING_FREQUENCY + "=" + pingFrequency;
 
294
      serverLocator = new InvokerLocator(locatorURI);
 
295
      log.info("Starting remoting server with locator uri of: " + locatorURI);
 
296
      HashMap config = new HashMap();
 
297
      config.put(InvokerLocator.FORCE_REMOTE, "true");
 
298
      addExtraServerConfig(config);
 
299
      connector = new Connector(serverLocator, config);
 
300
      connector.create();
 
301
      invocationHandler = new TestInvocationHandler();
 
302
      connector.addInvocationHandler("test", invocationHandler);
 
303
      connector.start();
 
304
   }
 
305
   
 
306
   
 
307
   protected void shutdownServer() throws Exception
 
308
   {
 
309
      if (connector != null)
 
310
         connector.stop();
 
311
   }
 
312
   
 
313
   
 
314
   static class TestInvocationHandler implements ServerInvocationHandler
 
315
   {
 
316
      public void addListener(InvokerCallbackHandler callbackHandler)
 
317
      {
 
318
         try
 
319
         {
 
320
            callbackHandler.handleCallback(new Callback("callback"));
 
321
         }
 
322
         catch (HandleCallbackException e)
 
323
         {
 
324
            log.error("Unable to send callback", e);
 
325
         }
 
326
      }
 
327
      public Object invoke(final InvocationRequest invocation) throws Throwable
 
328
      {
 
329
         return invocation.getParameter();
 
330
      }
 
331
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
332
      public void setMBeanServer(MBeanServer server) {}
 
333
      public void setInvoker(ServerInvoker invoker) {}
 
334
   }
 
335
   
 
336
   
 
337
   static class TestCallbackHandler implements InvokerCallbackHandler
 
338
   {
 
339
      public int counter;
 
340
      public void handleCallback(Callback callback) throws HandleCallbackException
 
341
      {
 
342
         counter++;
 
343
         log.info("received callback");
 
344
      }  
 
345
   }
 
346
   
 
347
   static private Method getDeclaredMethod(final Class c, final String name, final Class[] parameterTypes)
 
348
   throws NoSuchMethodException
 
349
   {
 
350
      if (SecurityUtility.skipAccessControl())
 
351
      {
 
352
         Method m = c.getDeclaredMethod(name, parameterTypes);
 
353
         m.setAccessible(true);
 
354
         return m;
 
355
      }
 
356
 
 
357
      try
 
358
      {
 
359
         return (Method) AccessController.doPrivileged( new PrivilegedExceptionAction()
 
360
         {
 
361
            public Object run() throws NoSuchMethodException
 
362
            {
 
363
               Method m = c.getDeclaredMethod(name, parameterTypes);
 
364
               m.setAccessible(true);
 
365
               return m;
 
366
            }
 
367
         });
 
368
      }
 
369
      catch (PrivilegedActionException e)
 
370
      {
 
371
         throw (NoSuchMethodException) e.getCause();
 
372
      }
 
373
   }
 
374
}
 
 
b'\\ No newline at end of file'