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

Ron Sigal ron_sigal at yahoo.com
Sat Jun 30 03:30:18 EDT 2007


  User: rsigal  
  Date: 07/06/30 03:30:18

  Added:       src/tests/org/jboss/test/remoting/transport/socket/oneway 
                        Tag: remoting_2_x
                        OnewayConnectionManagerTestCase.java
  Log:
  JBREM-641:  Unit test.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.1   +639 -0    JBossRemoting/src/tests/org/jboss/test/remoting/transport/socket/oneway/Attic/OnewayConnectionManagerTestCase.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: OnewayConnectionManagerTestCase.java
  ===================================================================
  RCS file: OnewayConnectionManagerTestCase.java
  diff -N OnewayConnectionManagerTestCase.java
  --- /dev/null	1 Jan 1970 00:00:00 -0000
  +++ OnewayConnectionManagerTestCase.java	30 Jun 2007 07:30:18 -0000	1.1.2.1
  @@ -0,0 +1,639 @@
  +/*
  +* 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.oneway;
  +
  +import java.lang.reflect.Field;
  +import java.net.InetAddress;
  +import java.util.HashMap;
  +import java.util.LinkedList;
  +import java.util.Map;
  +
  +import javax.management.MBeanServer;
  +
  +import junit.framework.TestCase;
  +
  +import org.apache.log4j.ConsoleAppender;
  +import org.apache.log4j.Level;
  +import org.apache.log4j.Logger;
  +import org.apache.log4j.PatternLayout;
  +import org.jboss.logging.XLevel;
  +import org.jboss.remoting.Client;
  +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.ClientInvoker;
  +import org.jboss.remoting.transport.Connector;
  +import org.jboss.remoting.transport.PortUtil;
  +import org.jboss.remoting.transport.socket.MicroSocketClientInvoker;
  +
  +
  +/**
  + * Tests the oneway connection timer task in MicroSocketClientInvoker.
  + * 
  + * @author <a href="ron.sigal at jboss.com">Ron Sigal</a>
  + * @version $Revision: 1.1.2.1 $
  + * <p>
  + * Copyright Jun 23, 2007
  + * </p>
  + */
  +public class OnewayConnectionManagerTestCase extends TestCase
  +{
  +   protected static String FAST = "fast";
  +   protected static String SLOW = "slow";
  +   protected static String DELAY = "delay";
  +   
  +   protected static Logger log = Logger.getLogger(OnewayConnectionManagerTestCase.class);
  +   protected static boolean firstTime = true;
  +   protected static boolean go;
  +   
  +   public void setUp() throws Exception
  +   {
  +      if (firstTime)
  +      {
  +         firstTime = false;
  +         Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
  +         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
  +         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
  +         PatternLayout layout = new PatternLayout(pattern);
  +         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
  +         Logger.getRootLogger().addAppender(consoleAppender);  
  +      }
  +   }
  +   
  +   
  +   /**
  +    * Connections time out.  Uses default parameters.
  +    */
  +   public void testDefaultParametersWithTimeouts() throws Throwable
  +   {
  +      log.info("entering " + getName());
  +      String host = InetAddress.getLocalHost().getHostAddress();
  +      int port = PortUtil.findFreePort(host);
  +      String locatorURI = "socket://" + host + ":" + port;
  +      InvokerLocator locator = new InvokerLocator(locatorURI);
  +      Connector connector = new Connector(locator);
  +      connector.create();
  +      TestHandler handler = new TestHandler();
  +      connector.addInvocationHandler("test", handler);
  +      connector.start();
  +      
  +      HashMap config = new HashMap();
  +      config.put(InvokerLocator.FORCE_REMOTE, "true");
  +      config.put(Client.MAX_NUM_ONEWAY_THREADS, "5");
  +      config.put(Client.MAX_ONEWAY_THREAD_POOL_QUEUE_SIZE, "1");
  +      Client client = new Client(locator, config);
  +      client.connect();
  +      
  +      ClientInvoker invoker = client.getInvoker();
  +      assertTrue(invoker instanceof MicroSocketClientInvoker); 
  +      Field field = MicroSocketClientInvoker.class.getDeclaredField("pool");
  +      field.setAccessible(true);
  +      LinkedList pool = (LinkedList) field.get(invoker);
  +      assertEquals(0, pool.size());
  +      field = MicroSocketClientInvoker.class.getDeclaredField("usedPooled");
  +      field.setAccessible(true);
  +      long usedPool = ((Long) field.get(invoker)).longValue();
  +      assertEquals(0, usedPool);
  +      
  +      int INVOCATIONS = 5;
  +      HashMap metadata = new HashMap();
  +      metadata.put(DELAY, "14000");
  +      long start = System.currentTimeMillis();
  +      for (int i = 0; i < INVOCATIONS; i++)
  +      {
  +         client.invokeOneway(SLOW + i, metadata, true);
  +      }
  +
  +      assertTrue((System.currentTimeMillis() - start < 1000));
  +      Thread.sleep(1000);
  +      usedPool = ((Long) field.get(invoker)).longValue();
  +      assertEquals(INVOCATIONS, usedPool);
  +      assertEquals(0, pool.size());
  +      assertEquals(INVOCATIONS, handler.startedCount);
  +      Thread.sleep(10000);
  +      
  +      // All sockets should have timed out by now.  Each timeout takes 1 second.
  +      usedPool = ((Long) field.get(invoker)).longValue();
  +      assertEquals(0, usedPool);
  +      assertEquals(0, pool.size());
  +      assertEquals(0, handler.finishedCount);
  +      Thread.sleep(5000);
  +      
  +      // All invocations should be done by now.
  +      usedPool = ((Long) field.get(invoker)).longValue();
  +      assertEquals(0, usedPool);
  +      assertEquals(0, pool.size());
  +      assertEquals(INVOCATIONS, handler.finishedCount);
  +      
  +      client.disconnect();
  +      connector.stop();
  +      log.info(getName() + " PASSES");
  +   }
  +   
  +   
  +   /**
  +    * Connections don't time out.  Uses default parameters.
  +    */
  +   public void testDefaultParametersWithNoTimeouts() throws Throwable
  +   {
  +      log.info("entering " + getName());
  +      String host = InetAddress.getLocalHost().getHostAddress();
  +      int port = PortUtil.findFreePort(host);
  +      String locatorURI = "socket://" + host + ":" + port;
  +      InvokerLocator locator = new InvokerLocator(locatorURI);
  +      Connector connector = new Connector(locator);
  +      connector.create();
  +      TestHandler handler = new TestHandler();
  +      connector.addInvocationHandler("test", handler);
  +      connector.start();
  +      
  +      HashMap config = new HashMap();
  +      config.put(InvokerLocator.FORCE_REMOTE, "true");
  +      config.put(Client.MAX_NUM_ONEWAY_THREADS, "5");
  +      config.put(Client.MAX_ONEWAY_THREAD_POOL_QUEUE_SIZE, "1");
  +      Client client = new Client(locator, config);
  +      client.connect();
  +      
  +      ClientInvoker invoker = client.getInvoker();
  +      assertTrue(invoker instanceof MicroSocketClientInvoker); 
  +      Field field = MicroSocketClientInvoker.class.getDeclaredField("pool");
  +      field.setAccessible(true);
  +      LinkedList pool = (LinkedList) field.get(invoker);
  +      assertEquals(0, pool.size());
  +      field = MicroSocketClientInvoker.class.getDeclaredField("usedPooled");
  +      field.setAccessible(true);
  +      long usedPool = ((Long) field.get(invoker)).longValue();
  +      assertEquals(0, usedPool);
  +      
  +      int INVOCATIONS = 5;
  +      HashMap metadata = new HashMap();
  +      metadata.put(DELAY, "2000");
  +      long start = System.currentTimeMillis();
  +      for (int i = 0; i < INVOCATIONS; i++)
  +      {
  +         client.invokeOneway(SLOW + i, metadata, true);
  +      }
  +
  +      assertTrue((System.currentTimeMillis() - start < 1000));
  +      Thread.sleep(1000);
  +      usedPool = ((Long) field.get(invoker)).longValue();
  +      assertEquals(INVOCATIONS, usedPool);
  +      assertEquals(0, pool.size());
  +      assertEquals(INVOCATIONS, handler.startedCount);
  +      Thread.sleep(6000);
  +      
  +      // All invocations should have finished by now, and all sockets should
  +      // have been returned to the pool.
  +      assertEquals(INVOCATIONS, handler.finishedCount);
  +      usedPool = ((Long) field.get(invoker)).longValue();
  +      assertEquals(0, usedPool);
  +      assertEquals(INVOCATIONS, pool.size());
  +      
  +      client.disconnect();
  +      connector.stop();
  +      log.info(getName() + " PASSES");
  +   }
  +   
  +
  +   /**
  +    * Connections time out.  Uses configured parameters.
  +    */
  +   public void testConfiguredParametersWithTimeouts() throws Throwable
  +   {
  +      log.info("entering " + getName());
  +      String host = InetAddress.getLocalHost().getHostAddress();
  +      int port = PortUtil.findFreePort(host);
  +      String locatorURI = "socket://" + host + ":" + port;
  +      InvokerLocator locator = new InvokerLocator(locatorURI);
  +      Connector connector = new Connector(locator);
  +      connector.create();
  +      TestHandler handler = new TestHandler();
  +      connector.addInvocationHandler("test", handler);
  +      connector.start();
  +      
  +      HashMap config = new HashMap();
  +      config.put(InvokerLocator.FORCE_REMOTE, "true");
  +      config.put(Client.MAX_NUM_ONEWAY_THREADS, "5");
  +      config.put(Client.MAX_ONEWAY_THREAD_POOL_QUEUE_SIZE, "1");
  +      config.put(MicroSocketClientInvoker.ONEWAY_CONNECTION_DELAY, "1000");
  +      config.put(MicroSocketClientInvoker.ONEWAY_CONNECTION_TIMEOUT, "100");
  +      Client client = new Client(locator, config);
  +      client.connect();
  +      
  +      ClientInvoker invoker = client.getInvoker();
  +      assertTrue(invoker instanceof MicroSocketClientInvoker); 
  +      Field field = MicroSocketClientInvoker.class.getDeclaredField("pool");
  +      field.setAccessible(true);
  +      LinkedList pool = (LinkedList) field.get(invoker);
  +      assertEquals(0, pool.size());
  +      field = MicroSocketClientInvoker.class.getDeclaredField("usedPooled");
  +      field.setAccessible(true);
  +      long usedPool = ((Long) field.get(invoker)).longValue();
  +      assertEquals(0, usedPool);
  +      
  +      int INVOCATIONS = 5;
  +      HashMap metadata = new HashMap();
  +      metadata.put(DELAY, "2000");
  +      long start = System.currentTimeMillis();
  +      for (int i = 0; i < INVOCATIONS; i++)
  +      {
  +         client.invokeOneway(SLOW + i, metadata, true);
  +      }
  +
  +      assertTrue((System.currentTimeMillis() - start < 1000));
  +      Thread.sleep(500);
  +      usedPool = ((Long) field.get(invoker)).longValue();
  +      assertEquals(INVOCATIONS, usedPool);
  +      assertEquals(0, pool.size());
  +      assertEquals(INVOCATIONS, handler.startedCount);
  +      Thread.sleep(2000);
  +      
  +      // All sockets should have timed out by now.  Each timeout takes 100 ms.
  +      usedPool = ((Long) field.get(invoker)).longValue();
  +      assertEquals(0, usedPool);
  +      assertEquals(0, pool.size());
  +      Thread.sleep(1000);
  +      
  +      // All invocations should be done by now.
  +      assertEquals(INVOCATIONS, handler.finishedCount);
  +      usedPool = ((Long) field.get(invoker)).longValue();
  +      assertEquals(0, usedPool);
  +      assertEquals(0, pool.size());
  +      
  +      client.disconnect();
  +      connector.stop();
  +      log.info(getName() + " PASSES");
  +   }
  +   
  +   
  +   /**
  +    * Connections time out.  Uses configured parameters.
  +    */
  +   public void testConfiguredParametersWithNoTimeouts() throws Throwable
  +   {
  +      log.info("entering " + getName());
  +      String host = InetAddress.getLocalHost().getHostAddress();
  +      int port = PortUtil.findFreePort(host);
  +      String locatorURI = "socket://" + host + ":" + port;
  +      InvokerLocator locator = new InvokerLocator(locatorURI);
  +      Connector connector = new Connector(locator);
  +      connector.create();
  +      TestHandler handler = new TestHandler();
  +      connector.addInvocationHandler("test", handler);
  +      connector.start();
  +      
  +      HashMap config = new HashMap();
  +      config.put(InvokerLocator.FORCE_REMOTE, "true");
  +      config.put(Client.MAX_NUM_ONEWAY_THREADS, "5");
  +      config.put(Client.MAX_ONEWAY_THREAD_POOL_QUEUE_SIZE, "1");
  +      config.put(MicroSocketClientInvoker.ONEWAY_CONNECTION_DELAY, "5000");
  +      config.put(MicroSocketClientInvoker.ONEWAY_CONNECTION_TIMEOUT, "5000");
  +      Client client = new Client(locator, config);
  +      client.connect();
  +      
  +      ClientInvoker invoker = client.getInvoker();
  +      assertTrue(invoker instanceof MicroSocketClientInvoker); 
  +      Field field = MicroSocketClientInvoker.class.getDeclaredField("pool");
  +      field.setAccessible(true);
  +      LinkedList pool = (LinkedList) field.get(invoker);
  +      assertEquals(0, pool.size());
  +      field = MicroSocketClientInvoker.class.getDeclaredField("usedPooled");
  +      field.setAccessible(true);
  +      long usedPool = ((Long) field.get(invoker)).longValue();
  +      assertEquals(0, usedPool);
  +      
  +      int INVOCATIONS = 5;
  +      HashMap metadata = new HashMap();
  +      metadata.put(DELAY, "8000");
  +      long start = System.currentTimeMillis();
  +      for (int i = 0; i < INVOCATIONS; i++)
  +      {
  +         client.invokeOneway(SLOW + i, metadata, true);
  +      }
  +
  +      assertTrue((System.currentTimeMillis() - start < 1000));
  +      Thread.sleep(1000);
  +      usedPool = ((Long) field.get(invoker)).longValue();
  +      assertEquals(INVOCATIONS, usedPool);
  +      assertEquals(0, pool.size());
  +      assertEquals(INVOCATIONS, handler.startedCount);
  +      Thread.sleep(11000);
  +      
  +      // All invocations should have finished by now, and all sockets should
  +      // have been returned to the pool.
  +      assertEquals(INVOCATIONS, handler.finishedCount);
  +      usedPool = ((Long) field.get(invoker)).longValue();
  +      assertEquals(0, usedPool);
  +      assertEquals(INVOCATIONS, pool.size());
  +      
  +      client.disconnect();
  +      connector.stop();
  +      log.info(getName() + " PASSES");
  +   }
  +   
  +   
  +   /**
  +    * Does server side oneway invocations.  Connections don't have to wait
  +    * for invocation to complete.
  +    */
  +   public void testOnewayServerSide() throws Throwable
  +   {
  +      log.info("entering " + getName());
  +      String host = InetAddress.getLocalHost().getHostAddress();
  +      int port = PortUtil.findFreePort(host);
  +      String locatorURI = "socket://" + host + ":" + port;
  +      InvokerLocator locator = new InvokerLocator(locatorURI);
  +      Connector connector = new Connector(locator);
  +      connector.create();
  +      TestHandler handler = new TestHandler();
  +      connector.addInvocationHandler("test", handler);
  +      connector.start();
  +      
  +      HashMap config = new HashMap();
  +      config.put(InvokerLocator.FORCE_REMOTE, "true");
  +      config.put(Client.MAX_NUM_ONEWAY_THREADS, "5");
  +      config.put(Client.MAX_ONEWAY_THREAD_POOL_QUEUE_SIZE, "1");
  +      Client client = new Client(locator, config);
  +      client.connect();
  +      
  +      ClientInvoker invoker = client.getInvoker();
  +      assertTrue(invoker instanceof MicroSocketClientInvoker); 
  +      Field field = MicroSocketClientInvoker.class.getDeclaredField("pool");
  +      field.setAccessible(true);
  +      LinkedList pool = (LinkedList) field.get(invoker);
  +      assertEquals(0, pool.size());
  +      field = MicroSocketClientInvoker.class.getDeclaredField("usedPooled");
  +      field.setAccessible(true);
  +      long usedPool = ((Long) field.get(invoker)).longValue();
  +      assertEquals(0, usedPool);
  +      
  +      int INVOCATIONS = 5;
  +      HashMap metadata = new HashMap();
  +      metadata.put(DELAY, "5000");
  +      for (int i = 0; i < INVOCATIONS; i++)
  +      {
  +         client.invokeOneway(SLOW + i, metadata, false);
  +         Thread.sleep(500);
  +      }
  +
  +      Thread.sleep(2000);
  +      
  +      // Ony one connection should have been created, and it should have been
  +      // returned to the pool by now.
  +      usedPool = ((Long) field.get(invoker)).longValue();
  +      assertEquals(0, usedPool);
  +      assertEquals(1, pool.size());
  +      assertEquals(INVOCATIONS, handler.startedCount);
  +      Thread.sleep(5000);
  +      
  +      // All invocations should be done by now.
  +      assertEquals(INVOCATIONS, handler.finishedCount);
  +      usedPool = ((Long) field.get(invoker)).longValue();
  +      assertEquals(0, usedPool);
  +      assertEquals(1, pool.size());
  +      
  +      client.disconnect();
  +      connector.stop();
  +      log.info(getName() + " PASSES");
  +   }
  +   
  +   
  +   /**
  +    * Verify connection management works under heavy load, with some timeouts.
  +    */
  +   public void testHeavyLoadWithTimeouts() throws Throwable
  +   {
  +      log.info("entering " + getName());
  +      String host = InetAddress.getLocalHost().getHostAddress();
  +      int port = PortUtil.findFreePort(host);
  +      String locatorURI = "socket://" + host + ":" + port;
  +      InvokerLocator locator = new InvokerLocator(locatorURI);
  +      HashMap serverConfig = new HashMap();
  +      serverConfig.put("MaxPoolSize", "300");
  +      Connector connector = new Connector(locator, serverConfig);
  +      connector.create();
  +      TestHandler handler = new TestHandler();
  +      connector.addInvocationHandler("test", handler);
  +      connector.start();
  +      
  +      HashMap clientConfig = new HashMap();
  +      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
  +      clientConfig.put(Client.MAX_NUM_ONEWAY_THREADS, "100");
  +      clientConfig.put(Client.MAX_ONEWAY_THREAD_POOL_QUEUE_SIZE, "100");
  +      clientConfig.put(MicroSocketClientInvoker.MAX_POOL_SIZE_FLAG, "200");
  +      clientConfig.put(MicroSocketClientInvoker.ONEWAY_CONNECTION_DELAY, "1000");
  +      Client client = new Client(locator, clientConfig);
  +      client.connect();
  +      
  +      ClientInvoker invoker = client.getInvoker();
  +      assertTrue(invoker instanceof MicroSocketClientInvoker); 
  +      Field field = MicroSocketClientInvoker.class.getDeclaredField("pool");
  +      field.setAccessible(true);
  +      LinkedList pool = (LinkedList) field.get(invoker);
  +      assertEquals(0, pool.size());
  +      field = MicroSocketClientInvoker.class.getDeclaredField("usedPooled");
  +      field.setAccessible(true);
  +      long usedPool = ((Long) field.get(invoker)).longValue();
  +      assertEquals(0, usedPool);
  +      
  +      int INVOCATIONS = 500;
  +      OnewayThread[] threads = new OnewayThread[INVOCATIONS];
  +      
  +      for (int i = 0; i < INVOCATIONS; i++)
  +      {
  +         threads[i] = new OnewayThread(client, i, "4000");
  +         threads[i].start();
  +      }
  +      
  +      go = true;
  +      Thread.sleep(2000);
  +      
  +      // Verify first set of invocations were received.
  +      assertEquals(200, handler.startedCount);
  +      Thread.sleep(12000);
  +      
  +      // Verify rest of invocations were received.
  +      assertEquals(INVOCATIONS, handler.startedCount);
  +      
  +      // All invocations should be complete by now.
  +      for (int i = 0; i < INVOCATIONS; i++)
  +      {
  +         assertTrue("failure in thread: " + i, threads[i].ok);
  +      }
  +      
  +      Thread.sleep(4000);
  +      usedPool = ((Long) field.get(invoker)).longValue();
  +      assertEquals(0, usedPool);
  +      
  +      client.disconnect();
  +      connector.stop();
  +      log.info(getName() + " PASSES");
  +   }
  +   
  +   
  +   /**
  +    * Verify connection management works under heavy load, with no timeouts.
  +    */
  +   public void testHeavyLoadWithoutTimeouts() throws Throwable
  +   {
  +      log.info("entering " + getName());
  +      String host = InetAddress.getLocalHost().getHostAddress();
  +      int port = PortUtil.findFreePort(host);
  +      String locatorURI = "socket://" + host + ":" + port;
  +      InvokerLocator locator = new InvokerLocator(locatorURI);
  +      HashMap serverConfig = new HashMap();
  +      serverConfig.put("MaxPoolSize", "300");
  +      Connector connector = new Connector(locator, serverConfig);
  +      connector.create();
  +      TestHandler handler = new TestHandler();
  +      connector.addInvocationHandler("test", handler);
  +      connector.start();
  +      
  +      HashMap clientConfig = new HashMap();
  +      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
  +      clientConfig.put(Client.MAX_NUM_ONEWAY_THREADS, "100");
  +      clientConfig.put(Client.MAX_ONEWAY_THREAD_POOL_QUEUE_SIZE, "100");
  +      clientConfig.put(MicroSocketClientInvoker.MAX_POOL_SIZE_FLAG, "200");
  +      clientConfig.put(MicroSocketClientInvoker.ONEWAY_CONNECTION_DELAY, "1000");
  +      Client client = new Client(locator, clientConfig);
  +      client.connect();
  +      
  +      ClientInvoker invoker = client.getInvoker();
  +      assertTrue(invoker instanceof MicroSocketClientInvoker); 
  +      Field field = MicroSocketClientInvoker.class.getDeclaredField("pool");
  +      field.setAccessible(true);
  +      LinkedList pool = (LinkedList) field.get(invoker);
  +      assertEquals(0, pool.size());
  +      field = MicroSocketClientInvoker.class.getDeclaredField("usedPooled");
  +      field.setAccessible(true);
  +      long usedPool = ((Long) field.get(invoker)).longValue();
  +      assertEquals(0, usedPool);
  +      
  +      int INVOCATIONS = 500;
  +      OnewayThread[] threads = new OnewayThread[INVOCATIONS];
  +      
  +      for (int i = 0; i < INVOCATIONS; i++)
  +      {
  +         threads[i] = new OnewayThread(client, i, "100");
  +         threads[i].start();
  +      }
  +      
  +      go = true;
  +      Thread.sleep(8000);
  +      
  +      // Verify invocations were received.
  +      assertEquals(INVOCATIONS, handler.startedCount);
  +      
  +      // All invocations should be complete by now.
  +      for (int i = 0; i < INVOCATIONS; i++)
  +      {
  +         assertTrue("failure in thread: " + i, threads[i].ok);
  +      }
  +      
  +      usedPool = ((Long) field.get(invoker)).longValue();
  +      assertEquals(0, usedPool);
  +      
  +      client.disconnect();
  +      connector.stop();
  +      log.info(getName() + " PASSES");
  +   }
  +   
  +   
  +   public class OnewayThread extends Thread
  +   {
  +      boolean ok;
  +      Client client;
  +      int id;
  +      String delay;
  +      
  +      public OnewayThread(Client client, int id, String delay)
  +      {
  +         this.client = client;
  +         this.id = id;
  +         this.delay = delay;
  +      }
  +      
  +      public void run()
  +      {
  +         try
  +         {
  +            while (!go)
  +            {
  +               try {Thread.sleep(10);} catch (InterruptedException e) {}
  +            }
  +            
  +            HashMap metadata = new HashMap();
  +            metadata.put(DELAY, delay);
  +            client.invokeOneway(SLOW + id, metadata, true);
  +            ok = true;
  +         }
  +         catch (Throwable e)
  +         {
  +            e.printStackTrace();
  +         }
  +      }
  +   }
  +   
  +   
  +   public class TestHandler implements ServerInvocationHandler
  +   {
  +      public int startedCount;
  +      public int finishedCount;
  +      public Object lock = new Object();
  +      
  +      public void setMBeanServer(MBeanServer server) {}
  +      public void setInvoker(ServerInvoker invoker) {}
  +
  +      public Object invoke(InvocationRequest invocation) throws Throwable
  +      {
  +         log.debug("invocation: " + invocation.getParameter());
  +         synchronized (lock)
  +         {
  +            startedCount++;
  +            log.debug("startedCount: " + startedCount);
  +         }
  +         
  +         String command = (String) invocation.getParameter();
  +         
  +         if (command.startsWith(SLOW))
  +         {
  +            Map metadata = invocation.getRequestPayload();
  +            String delayString = (String) metadata.get(DELAY);
  +            int delay = Integer.valueOf(delayString).intValue();
  +            Thread.sleep(delay);
  +         }
  +         
  +         synchronized (lock)
  +         {
  +            finishedCount++;
  +            log.debug("invocation done: " + invocation.getParameter());
  +            log.debug("finishedCount: " + finishedCount);
  +         }
  +         return null;
  +      }
  +
  +      public void addListener(InvokerCallbackHandler callbackHandler) {}
  +      public void removeListener(InvokerCallbackHandler callbackHandler) {}
  +   }
  +}
  \ No newline at end of file
  
  
  



More information about the jboss-cvs-commits mailing list