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

Ron Sigal ron_sigal at yahoo.com
Thu Nov 2 23:02:45 EST 2006


  User: rsigal  
  Date: 06/11/02 23:02:45

  Added:       src/tests/org/jboss/test/remoting/transport/socket/socketpool   
                        SocketPoolTestServer.java SocketPoolTestCase.java
                        SocketPoolTestClient.java
  Log:
  JBREM-625:  Created unit test.
  
  Revision  Changes    Path
  1.1      date: 2006/11/03 04:02:45;  author: rsigal;  state: Exp;JBossRemoting/src/tests/org/jboss/test/remoting/transport/socket/socketpool/SocketPoolTestServer.java
  
  Index: SocketPoolTestServer.java
  ===================================================================
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  package org.jboss.test.remoting.transport.socket.socketpool;
  
  import org.jboss.jrunit.extensions.ServerTestCase;
  import org.jboss.logging.Logger;
  import org.jboss.remoting.InvocationRequest;
  import org.jboss.remoting.InvokerLocator;
  import org.jboss.remoting.ServerInvocationHandler;
  import org.jboss.remoting.ServerInvoker;
  import org.jboss.remoting.callback.InvokerCallbackHandler;
  import org.jboss.remoting.transport.Connector;
  
  import javax.management.MBeanServer;
  
  /** 
   * See SocketPoolTestCase for description.
   *  
   * @author <a href="ron.sigal at jboss.com">Ron Sigal</a>
   * @author <a href="mailto:tom.elrod at jboss.com">Tom Elrod</a>
   * 
   * @version $Revision: 1.1 $
   * <p>
   * Copyright Nov 2, 2006
   * </p>
   */
  public class SocketPoolTestServer extends ServerTestCase
  {
     public final static int NUMBER_OF_CALLS = 5;
     private static Logger log = Logger.getLogger(SocketPoolTestServer.class);
     private static Object lock = new Object();
     private static Object stopLock = new Object();
     private static int callCounter;
     private static boolean done;
     
     // Default locator values
     private static String transport = "socket";
     private static String host = "localhost";
     private static int port = 6413;
  
     private Connector connector = null;
  
     public void setupServer(String locatorURI) throws Exception
     {
        log.warn("EXCEPTIONS ARE EXPECTED");
        log.info("Starting remoting server with locator uri of: " + locatorURI);
        
        InvokerLocator locator = new InvokerLocator(locatorURI);
        connector = new Connector(locator);
        connector.create();
        SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
        connector.addInvocationHandler("sample", invocationHandler);
        connector.start();
        
        // This thread will stop the Connector when NUMBER_OF_CALLS
        // invocations have been received.
        new Thread()
        {
           public void run()
           {
              synchronized (lock)
              {
                 while (!done)
                 {
                    try
                    {
                       lock.wait();
                    }
                    catch (InterruptedException e)
                    {
                       log.error(e);
                       e.printStackTrace();
                    }
                 }
                 
                 if(connector != null)
                 {
                    connector.stop();
                    connector.destroy();
                    log.info("Connector stopped");
                    
                    synchronized(stopLock)
                    {
                       stopLock.notify();
                    }
                 }
              }
           }
        }.start();   
     }
  
     public void tearDown()
     {
     }
  
     public void setUp() throws Exception
     {
        String locatorURI = transport + "://" + host + ":" + port;
        setupServer(locatorURI);
     }
  
     /**
      * Can pass transport and port to be used as parameters.
      * Valid transports are 'rmi' and 'socket'.
      *
      * @param args
      */
     public static void main(String[] args)
     {
        if(args != null && args.length == 2)
        {
           host = args[0];
           port = Integer.parseInt(args[1]);
        }
        SocketPoolTestServer server = new SocketPoolTestServer();
        try
        {
           server.setUp();
           
           synchronized (stopLock)
           {
              while (!done)
              {
                 try
                 {
                    stopLock.wait();
                    break;
                 }
                 catch (InterruptedException ignored) {}
              }
           }
        }
        catch(Exception e)
        {
           e.printStackTrace();
        }
     }
  
     /**
      * Simple invocation handler implementation.
      * This is the code that will be called with the invocation payload from the client.
      */
     public static class SampleInvocationHandler implements ServerInvocationHandler
     {
        public Object invoke(InvocationRequest invocation) throws Throwable
        {
           synchronized (lock)
           {
              ++callCounter;
              log.info("callCounter: " + callCounter);
           }
   
           // Waiting for 2 seconds will cause the client to timeout.
           Thread.sleep(2000);
           
           synchronized (lock)
           {
              if (callCounter == NUMBER_OF_CALLS)
              {
                 done = true;
                 lock.notify();
              }
           }
           return "response";
        }
  
        public void addListener(InvokerCallbackHandler callbackHandler)
        {
        }
        public void removeListener(InvokerCallbackHandler callbackHandler)
        {
        }
        public void setMBeanServer(MBeanServer server)
        {
        }
        public void setInvoker(ServerInvoker invoker)
        {
        }
     }
  }
  
  
  1.1      date: 2006/11/03 04:02:45;  author: rsigal;  state: Exp;JBossRemoting/src/tests/org/jboss/test/remoting/transport/socket/socketpool/SocketPoolTestCase.java
  
  Index: SocketPoolTestCase.java
  ===================================================================
  /*
  * JBoss, a division of Red Hat
  * Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  package org.jboss.test.remoting.transport.socket.socketpool;
  
  import org.apache.log4j.Level;
  import org.jboss.test.remoting.transport.InvokerTestDriver;
  
  /**
   *  This unit test is introduced in response to JIRA issue JBREM-625.
   *  It verifies that when a socket is discarded, the count of sockets
   *  in use is decremented.
   *  
   * @author <a href="ron.sigal at jboss.com">Ron Sigal</a>
   * @author <a href="mailto:tom at jboss.org">Tom Elrod</a>
   *  
   * @version $Revision: 1.1 $
   * <p>
   * Copyright Nov 2, 2006
   * </p>
   */
  public class SocketPoolTestCase extends InvokerTestDriver
  {
     public void declareTestClasses()
     {  
        addTestClasses(SocketPoolTestClient.class.getName(),
                       1,
                       SocketPoolTestServer.class.getName());
     }
  
     protected Level getTestHarnessLogLevel()
     {
        return Level.DEBUG;
     }
  
     protected Level getTestLogLevel()
     {
        return Level.DEBUG;
     }
  
     /**
      * How long to wait for test results to be returned from the client(s).  If goes longer than the
      * specified limit, will throw an exception and kill the running test cases.  Default value is
      * RESULTS_TIMEOUT.
      *
      * @return
      */
     protected long getResultsTimeout()
     {
        return 600000;
     }
  
     /**
      * How long for the server test case to wait for tear down message.  If exceeds timeout,
      * will throw exception.  The default value is TEARDOWN_TIMEOUT.
      *
      * @return
      */
     protected long getTearDownTimeout()
     {
        return 600000;
     }
  
     /**
      * How long to allow each of the test cases to run their tests.  If exceeds this timeout
      * will throw exception and kill tests.  The default value is RUN_TEST_TIMEOUT.
      *
      * @return
      */
     protected long getRunTestTimeout()
     {
        return 600000;
     }
  
  }
  
  
  
  1.1      date: 2006/11/03 04:02:45;  author: rsigal;  state: Exp;JBossRemoting/src/tests/org/jboss/test/remoting/transport/socket/socketpool/SocketPoolTestClient.java
  
  Index: SocketPoolTestClient.java
  ===================================================================
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  package org.jboss.test.remoting.transport.socket.socketpool;
  
  import junit.framework.TestCase;
  
  import org.jboss.logging.Logger;
  import org.jboss.remoting.Client;
  import org.jboss.remoting.InvokerLocator;
  import org.jboss.remoting.transport.socket.SocketClientInvoker;
  
  /**
   * See SocketPoolTestCase for description.
   *  
   * @author <a href="ron.sigal at jboss.com">Ron Sigal</a>
   * @author <a href="mailto:tom.elrod at jboss.com">Tom Elrod</a>
   * 
   * @version $Revision: 1.1 $
   * <p>
   * Copyright Nov 2, 2006
   * </p>
   */
  public class SocketPoolTestClient extends TestCase
  {
     private static Logger log = Logger.getLogger(SocketPoolTestClient.class);
     
     // Default locator values
     private static String transport = "socket";
     private static String host = "localhost";
     private static int port = 6413;
  
  
     public void testSocketPool() throws Throwable
     {
        Client client = null;
  
        try
        {
           log.warn("EXCEPTIONS ARE EXPECTED");
  
           String locatorURI = transport + "://" + host + ":" + port + "/?timeout=1000";
           log.info("Calling remoting server with locator uri of: " + locatorURI);
  
           // create InvokerLocator with the url type string
           // indicating the target remoting server to call upon.
           InvokerLocator locator = new InvokerLocator(locatorURI);
  
           client = new Client(locator);
           client.connect();
           SocketClientInvoker invoker = (SocketClientInvoker) client.getInvoker();
          
           // No sockets are in use.
           assertEquals(0, invoker.usedPooled);
           log.info("usedPool: " + invoker.usedPooled);
     
           // Make SocketPoolTestServer.NUMBER_OF_CALLS invocations.
           for (int i = 0; i < SocketPoolTestServer.NUMBER_OF_CALLS; i++)
           {
              new Invoker(client, i).start();
           }
           
           Thread.sleep(500);
           
           // SocketPoolTestServer.NUMBER_OF_CALLS sockets are in use.
           log.info("usedPool: " + invoker.usedPooled);
           assertEquals(SocketPoolTestServer.NUMBER_OF_CALLS, invoker.usedPooled);
           
           Thread.sleep(2000);
           
           // All invocations have timed out.  All sockets should be closed and discarded.
           log.info("usedPool: " + invoker.usedPooled);
           assertEquals(0, invoker.usedPooled);
        }
        finally
        {
           if(client != null)
           {
              client.disconnect();
           }
        }
  
     }
  
     /**
      * Can pass transport and port to be used as parameters.
      * Valid transports are 'rmi' and 'socket'.
      *
      * @param args
      */
     public static void main(String[] args)
     {
        if(args != null && args.length == 2)
        {
           host = args[0];
           port = Integer.parseInt(args[1]);
        }
        SocketPoolTestClient client = new SocketPoolTestClient();
        try
        {
           client.testSocketPool();
        }
        catch(Throwable e)
        {
           e.printStackTrace();
        }
     }
  
     class Invoker extends Thread
     {
        private Client client;
        private int id;
        
        Invoker(Client client, int id)
        {
           this.client = client;
           this.id = id;
        }
        
        public void run()
        {
           try
           {
              client.invoke("payload");
              log.info("invoke succeeded: " + id);
           }
           catch (Throwable e)
           {
              log.info("invoke failed: " + id);
           }
        }
     }
  }
  
  



More information about the jboss-cvs-commits mailing list