[jboss-cvs] JBossRemoting/src/tests/org/jboss/test/remoting/transport/bisocket ...

Ron Sigal ron_sigal at yahoo.com
Fri Aug 31 02:47:30 EDT 2007


  User: rsigal  
  Date: 07/08/31 02:47:30

  Modified:    src/tests/org/jboss/test/remoting/transport/bisocket  Tag:
                        remoting_2_x TimerReuseTestCase.java
  Log:
  Synchronizing with branch remoting_2_2_0_GA: JBREM-779:  Added unit tests for replaceControlSocket(); in doTestNewControlConnectionTimerReuse(), wait to make sure PingTimerTask has been recreated;  added new test methods.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.2   +170 -4    JBossRemoting/src/tests/org/jboss/test/remoting/transport/bisocket/Attic/TimerReuseTestCase.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: TimerReuseTestCase.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossRemoting/src/tests/org/jboss/test/remoting/transport/bisocket/Attic/TimerReuseTestCase.java,v
  retrieving revision 1.1.2.1
  retrieving revision 1.1.2.2
  diff -u -b -r1.1.2.1 -r1.1.2.2
  --- TimerReuseTestCase.java	2 Jun 2007 05:32:14 -0000	1.1.2.1
  +++ TimerReuseTestCase.java	31 Aug 2007 06:47:30 -0000	1.1.2.2
  @@ -21,9 +21,13 @@
   */
   package org.jboss.test.remoting.transport.bisocket;
   
  +import java.lang.reflect.Field;
   import java.net.InetAddress;
   import java.util.HashMap;
   import java.util.Map;
  +import java.util.Set;
  +import java.util.Timer;
  +import java.util.TimerTask;
   
   import javax.management.MBeanServer;
   
  @@ -41,8 +45,14 @@
   import org.jboss.remoting.callback.Callback;
   import org.jboss.remoting.callback.HandleCallbackException;
   import org.jboss.remoting.callback.InvokerCallbackHandler;
  +import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
   import org.jboss.remoting.transport.Connector;
   import org.jboss.remoting.transport.PortUtil;
  +import org.jboss.remoting.transport.bisocket.Bisocket;
  +import org.jboss.remoting.transport.bisocket.BisocketClientInvoker;
  +import org.jboss.remoting.transport.bisocket.BisocketServerInvoker;
  +import org.jboss.test.remoting.transport.bisocket.ServerTimerReuseTestCase.TestCallbackHandler;
  +import org.jboss.test.remoting.transport.bisocket.ServerTimerReuseTestCase.TestInvocationHandler;
   
   
   /**
  @@ -58,7 +68,7 @@
    * time before installing a second InvokerCallbackHandler.
    * 
    * @author <a href="ron.sigal at jboss.com">Ron Sigal</a>
  - * @version $Revision: 1.1.2.1 $
  + * @version $Revision: 1.1.2.2 $
    * <p>
    * Copyright May 31, 2007
    * </p>
  @@ -123,6 +133,32 @@
         doTestTimerReuse(30000);
      }
      
  +   public void testNewControlConnectionImmdiately() throws Throwable
  +   {
  +      log.info("entering " + getName());
  +      doTestNewControlConnectionTimerReuse(1);
  +   }
  +   
  +   public void testNewControlConnectionAfter1Second() throws Throwable
  +   {
  +      log.info("entering " + getName());
  +      doTestNewControlConnectionTimerReuse(1000);
  +   }
  +   
  +   
  +   public void testNewControlConnectionAfter5Seconds() throws Throwable
  +   {
  +      log.info("entering " + getName());
  +      doTestNewControlConnectionTimerReuse(5000);
  +   }
  +   
  +   
  +   public void testNewControlConnectionAfter30Seconds() throws Throwable
  +   {
  +      log.info("entering " + getName());
  +      doTestNewControlConnectionTimerReuse(30000);
  +   }
  +   
      
      public void doTestTimerReuse(int waitPeriod) throws Throwable
      {
  @@ -164,6 +200,119 @@
      }
      
      
  +   /**
  +    * Verifies that BisocketClientInvoker.replaceControlSocket() reuses static
  +    * Timer, if possible.
  +    */
  +   public void doTestNewControlConnectionTimerReuse(int waitPeriod) throws Throwable
  +   {
  +      log.info("entering " + getName());
  +      
  +      // Start server.
  +      String host = InetAddress.getLocalHost().getHostAddress();
  +      int port = PortUtil.findFreePort(host);
  +      String locatorURI = getTransport() + "://" + host + ":" + port; 
  +      serverLocator = new InvokerLocator(locatorURI);
  +      log.info("Starting remoting server with locator uri of: " + locatorURI);
  +      HashMap config = new HashMap();
  +      config.put(InvokerLocator.FORCE_REMOTE, "true");
  +      addExtraServerConfig(config);
  +      connector = new Connector(serverLocator, config);
  +      connector.create();
  +      invocationHandler = new SampleInvocationHandler();
  +      connector.addInvocationHandler("sample", invocationHandler);
  +      connector.start();
  +      
  +      // Create client.
  +      InvokerLocator clientLocator1 = new InvokerLocator(locatorURI);
  +      HashMap clientConfig = new HashMap();
  +      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
  +      addExtraClientConfig(clientConfig);
  +      Client client = new Client(clientLocator1, clientConfig);
  +      client.connect();
  +      log.info("client is connected");
  +      
  +      // Test connection.
  +      assertEquals("abc", client.invoke("abc"));
  +      log.info("connection is good");
  +      
  +      // Add callback handler.
  +      TestCallbackHandler callbackHandler = new TestCallbackHandler();
  +      HashMap metadata = new HashMap();
  +      metadata.put(Bisocket.IS_CALLBACK_SERVER, "true");
  +      client.addListener(callbackHandler, metadata);
  +      
  +      // Stop PingTimerTask on server.  This allows the possibility that the Timer
  +      // will be replaced.
  +      BisocketServerInvoker serverInvoker = (BisocketServerInvoker) connector.getServerInvoker();
  +      Field field = ServerInvoker.class.getDeclaredField("callbackHandlers");
  +      field.setAccessible(true);
  +      Map callbackHandlers = (Map) field.get(serverInvoker);
  +      assertEquals(1, callbackHandlers.size());
  +      ServerInvokerCallbackHandler serverInvokerCallbackHandler;
  +      serverInvokerCallbackHandler = (ServerInvokerCallbackHandler) callbackHandlers.values().iterator().next();
  +      field = ServerInvokerCallbackHandler.class.getDeclaredField("callBackClient");
  +      field.setAccessible(true);
  +      Client callbackClient = (Client) field.get(serverInvokerCallbackHandler);
  +      assertNotNull(callbackClient);
  +      BisocketClientInvoker callbackClientInvoker = (BisocketClientInvoker) callbackClient.getInvoker();
  +      field = BisocketClientInvoker.class.getDeclaredField("pingTimerTask");
  +      field.setAccessible(true);
  +      TimerTask pingTimerTask1 = (TimerTask) field.get(callbackClientInvoker);
  +      BisocketClientInvoker ci = (BisocketClientInvoker) client.getInvoker();
  +      assertNotNull(pingTimerTask1);
  +      pingTimerTask1.cancel();
  +      
  +      // Cause recreation of PingTimerTask on server.  If call below to
  +      // createControlConnection() doesn't throw an exception, then the test passes.
  +      Thread.sleep(waitPeriod);
  +      field = Client.class.getDeclaredField("callbackConnectors");
  +      field.setAccessible(true);
  +      Map callbackConnectors = (Map) field.get(client);
  +      assertEquals(1, callbackConnectors.size());
  +      Set connectors = (Set) callbackConnectors.values().iterator().next();
  +      assertEquals(1, connectors.size());
  +      Connector callbackConnector = (Connector) connectors.iterator().next();
  +      BisocketServerInvoker callbackInvoker = (BisocketServerInvoker) callbackConnector.getServerInvoker();
  +      field = BisocketServerInvoker.class.getDeclaredField("controlConnectionThreadMap");
  +      field.setAccessible(true);
  +      Map controlConnectionThreadMap = (Map) field.get(callbackInvoker);
  +      assertEquals(1, controlConnectionThreadMap.size());
  +      Thread controlConnectionThread = (Thread) controlConnectionThreadMap.values().iterator().next();
  +      Class[] classes = BisocketServerInvoker.class.getDeclaredClasses();
  +      Class ControlConnectionThreadClass = null;
  +      
  +      for (int i = 0; i < classes.length; i++)
  +      {
  +         if (classes[i].toString().indexOf("ControlConnectionThread") != -1)
  +         {
  +            ControlConnectionThreadClass = classes[i];
  +            break;
  +         }
  +      }
  +      
  +      assertNotNull(ControlConnectionThreadClass);
  +      field = ControlConnectionThreadClass.getDeclaredField("listenerId");
  +      field.setAccessible(true);
  +      String listenerId = (String) field.get(controlConnectionThread);
  +      assertNotNull(listenerId);
  +      callbackInvoker.createControlConnection(listenerId, false);
  +      Thread.sleep(2000);
  +      
  +      // Get PingTimerTask and verify it has changed.
  +      field = BisocketClientInvoker.class.getDeclaredField("pingTimerTask");
  +      field.setAccessible(true);
  +      TimerTask pingTimerTask2 = (TimerTask) field.get(callbackClientInvoker);
  +      assertNotSame(pingTimerTask1, pingTimerTask2);
  +      
  +      // Shut down.
  +      client.removeListener(callbackHandler);
  +      client.disconnect();
  +      connector.stop();
  +      log.info(getName() + " PASSES");
  +   }
  +   
  +   
      protected String getTransport()
      {
         return "bisocket";
  @@ -176,8 +325,21 @@
   
      static class SampleInvocationHandler implements ServerInvocationHandler
      {
  -      public void addListener(InvokerCallbackHandler callbackHandler) {}
  -      public Object invoke(final InvocationRequest invocation) throws Throwable {return null;}
  +      public void addListener(InvokerCallbackHandler callbackHandler)
  +      {
  +         try
  +         {
  +            callbackHandler.handleCallback(new Callback("callback"));
  +         }
  +         catch (HandleCallbackException e)
  +         {
  +            log.error("unable to send callback", e);
  +         }
  +      }
  +      public Object invoke(final InvocationRequest invocation) throws Throwable
  +      {
  +         return invocation.getParameter();
  +      }
         public void removeListener(InvokerCallbackHandler callbackHandler) {}
         public void setMBeanServer(MBeanServer server) {}
         public void setInvoker(ServerInvoker invoker) {}
  @@ -185,6 +347,10 @@
      
      static class TestCallbackHandler implements InvokerCallbackHandler
      {
  -      public void handleCallback(Callback callback) throws HandleCallbackException {}
  +      public int count;
  +      public void handleCallback(Callback callback) throws HandleCallbackException
  +      {
  +         count++;
  +      }
      }
   }
  \ No newline at end of file
  
  
  



More information about the jboss-cvs-commits mailing list