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

Ron Sigal ron_sigal at yahoo.com
Fri Jan 19 19:42:30 EST 2007


  User: rsigal  
  Date: 07/01/19 19:42:30

  Added:       src/tests/org/jboss/test/remoting/shutdown      Tag:
                        remoting_2_x AbstractClient.java
                        ShutdownTestServer.java ShutdownTestCase.java
                        HangingClient.java ClosingClient.java
  Log:
  JBREM-674:  Unit test to verify all Remoting threads shut down.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.1   +105 -0    JBossRemoting/src/tests/org/jboss/test/remoting/shutdown/Attic/AbstractClient.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: AbstractClient.java
  ===================================================================
  RCS file: AbstractClient.java
  diff -N AbstractClient.java
  --- /dev/null	1 Jan 1970 00:00:00 -0000
  +++ AbstractClient.java	20 Jan 2007 00:42:29 -0000	1.1.2.1
  @@ -0,0 +1,105 @@
  +/*
  + * 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.shutdown;
  +
  +import java.net.InetAddress;
  +import java.util.HashMap;
  +
  +import junit.framework.TestCase;
  +
  +import org.jboss.remoting.Client;
  +import org.jboss.remoting.InvokerLocator;
  +import org.jboss.remoting.callback.Callback;
  +import org.jboss.remoting.callback.HandleCallbackException;
  +import org.jboss.remoting.callback.InvokerCallbackHandler;
  +
  +/** 
  + * @author <a href="ron.sigal at jboss.com">Ron Sigal</a>
  + * @version $Revision: 1.1.2.1 $
  + * <p>
  + * Copyright Jan 19, 2007
  + * </p>
  + */
  +public abstract class AbstractClient extends TestCase
  +{    
  +   public void testShutdown() throws Throwable
  +   {
  +      try
  +      {
  +         String host = InetAddress.getLocalHost().getHostAddress();
  +         String locatorURI = "socket://" + host + ":" + ShutdownTestServer.port;
  +         InvokerLocator locator = new InvokerLocator(locatorURI);
  +         HashMap clientConfig = new HashMap();
  +         Client client = new Client(locator, clientConfig);
  +         client.connect();
  +         System.out.println("client connected");
  +         Integer i = (Integer) client.invoke(new Integer(17));
  +         if (18 != i.intValue())
  +            throw new Exception("invocation failed");
  +         System.out.println("invocation successful");
  +         TestCallbackHandler callbackHandler = new TestCallbackHandler();
  +         client.addListener(callbackHandler, null, null, true);
  +         if (!callbackHandler.receivedCallback)
  +            throw new Exception("callback failed");
  +         System.out.println("callback successful");
  +         client.removeListener(callbackHandler);
  +         client.disconnect();
  +         Thread t = new Thread()
  +         {
  +            public void run()
  +            {
  +               try
  +               {
  +                  Thread.sleep(10000);
  +               }
  +               catch (InterruptedException e)
  +               {
  +                  System.out.println("interrupted");
  +               }
  +            }
  +         };
  +         t.setDaemon(daemon());
  +         t.start();
  +         System.out.println("client disconnected");
  +      }
  +      catch (Exception e)
  +      {
  +         System.out.println("exception in client: " + e);
  +         System.exit(1);
  +      }
  +   }
  +   
  +   
  +   abstract protected boolean daemon();
  +   
  +   
  +   public class TestCallbackHandler implements InvokerCallbackHandler
  +   {  
  +      public boolean receivedCallback;
  +      
  +      public void handleCallback(Callback callback) throws HandleCallbackException
  +      {
  +         receivedCallback = true;
  +         System.out.println("received callback: " + callback);
  +      }  
  +   }
  +}
  
  
  
  1.1.2.1   +117 -0    JBossRemoting/src/tests/org/jboss/test/remoting/shutdown/Attic/ShutdownTestServer.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ShutdownTestServer.java
  ===================================================================
  RCS file: ShutdownTestServer.java
  diff -N ShutdownTestServer.java
  --- /dev/null	1 Jan 1970 00:00:00 -0000
  +++ ShutdownTestServer.java	20 Jan 2007 00:42:29 -0000	1.1.2.1
  @@ -0,0 +1,117 @@
  +/*
  +* 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.shutdown;
  +
  +import java.net.InetAddress;
  +import java.util.HashMap;
  +
  +import javax.management.MBeanServer;
  +
  +import org.jboss.jrunit.extensions.ServerTestCase;
  +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.Callback;
  +import org.jboss.remoting.callback.HandleCallbackException;
  +import org.jboss.remoting.callback.InvokerCallbackHandler;
  +import org.jboss.remoting.transport.Connector;
  +
  +/** 
  + * @author <a href="ron.sigal at jboss.com">Ron Sigal</a>
  + * @version $Revision: 1.1.2.1 $
  + * <p>
  + * Copyright Jan 19, 2007
  + * </p>
  + */
  +public class ShutdownTestServer extends ServerTestCase
  +{
  +   public static int port = 9876;
  +   private Connector connector;
  +   
  +   
  +   public void setUp() throws Exception
  +   {
  +      String host = InetAddress.getLocalHost().getHostAddress();
  +      String locatorURI = "socket://" + host + ":" + port;
  +      InvokerLocator locator = new InvokerLocator(locatorURI);
  +      HashMap serverConfig = new HashMap();
  +      connector = new Connector(locator, serverConfig);
  +      connector.create();
  +      connector.addInvocationHandler("test", new TestHandler());
  +      connector.start();
  +      System.out.println("server started at: " + locatorURI);
  +   }
  +   
  +   
  +   public void tearDown()
  +   {
  +      if (connector != null)
  +      {
  +         connector.stop();
  +         System.out.println("server shut down");
  +      }
  +   }
  +   
  +   
  +   public static void main(String[] args)
  +   {
  +      ShutdownTestServer server = new ShutdownTestServer();
  +      try
  +      {
  +         server.setUp();
  +         Thread.sleep(4000);
  +         server.tearDown();
  +      }
  +      catch(Exception e)
  +      {
  +         e.printStackTrace();
  +      }
  +   }
  +
  +   
  +   public class TestHandler implements ServerInvocationHandler
  +   {
  +      public void setMBeanServer(MBeanServer server) {}
  +      public void setInvoker(ServerInvoker invoker) {}
  +
  +      public Object invoke(InvocationRequest invocation) throws Throwable
  +      {
  +         Integer i = (Integer) invocation.getParameter();
  +         return new Integer(i.intValue() + 1);
  +      }
  +
  +      public void addListener(InvokerCallbackHandler callbackHandler)
  +      {
  +         try
  +         {
  +            callbackHandler.handleCallback(new Callback("callback"));
  +         }
  +         catch (HandleCallbackException e)
  +         {
  +            System.out.println("error handling callback");
  +         }
  +      }
  +      
  +      public void removeListener(InvokerCallbackHandler callbackHandler) {}
  +   }
  +}
  
  
  
  1.1.2.1   +184 -0    JBossRemoting/src/tests/org/jboss/test/remoting/shutdown/Attic/ShutdownTestCase.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ShutdownTestCase.java
  ===================================================================
  RCS file: ShutdownTestCase.java
  diff -N ShutdownTestCase.java
  --- /dev/null	1 Jan 1970 00:00:00 -0000
  +++ ShutdownTestCase.java	20 Jan 2007 00:42:29 -0000	1.1.2.1
  @@ -0,0 +1,184 @@
  +/*
  +* 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.shutdown;
  +
  +import java.io.BufferedReader;
  +import java.io.IOException;
  +import java.io.InputStreamReader;
  +
  +import junit.framework.TestCase;
  +
  +import org.apache.log4j.Logger;
  +
  +/** 
  + * This unit test is meant to guard against the possibility of accidentally
  + * creating non-daemon threads that prevent the Remoting subsystem from 
  + * terminating.
  + * 
  + * See JIRA issue JBREM-674 "add test case for client exiting correctly".
  + * (http://jira.jboss.com/jira/browse/JBREM-674)
  + * 
  + * @author <a href="ron.sigal at jboss.com">Ron Sigal</a>
  + * @version $Revision: 1.1.2.1 $
  + * <p>
  + * Copyright Jan 19, 2007
  + * </p>
  + */
  +public class ShutdownTestCase extends TestCase
  +{
  +   private Logger log = Logger.getLogger(ShutdownTestCase.class);
  +   
  +   
  +   /**
  +    * This test case verifies that the test really works.  In particular,
  +    * a non-daemon thread is created in the client JVM that prevents it
  +    * from shutting down in the expected time frame.
  +    * 
  +    * @throws Throwable
  +    */
  +   public void testHangingClient() throws Throwable
  +   {
  +      log.info("entering testShutdown()");
  +      
  +      String command = "java -cp \"" +  System.getProperty("java.class.path") + "\" ";
  +      String serverCommand = command + ShutdownTestServer.class.getName();
  +      String clientCommand = command + HangingClient.class.getName();
  +      Executor serverExecutor = new Executor(serverCommand);
  +      Executor clientExecutor = new Executor(clientCommand);
  +      serverExecutor.start();
  +      Thread.sleep(1000);
  +      clientExecutor.start();
  +      Thread.sleep(5000);
  +      assertTrue(serverExecutor.successful());
  +      assertFalse(clientExecutor.successful());
  +      log.info(getName() + " PASSES");
  +   }
  +   
  +   
  +   /**
  +    * This unit verifies that a JVM with a client and callback server, and another
  +    * JVM with a server and a callback client, both terminate.
  +    * 
  +    * @throws Throwable
  +    */
  +   public void testClosingClient() throws Throwable
  +   {
  +      log.info("entering testShutdown()");
  +      
  +      String command = "java -cp \"" +  System.getProperty("java.class.path") + "\" ";
  +      String serverCommand = command + ShutdownTestServer.class.getName();
  +      String clientCommand = command + ClosingClient.class.getName();
  +      Executor serverExecutor = new Executor(serverCommand);
  +      Executor clientExecutor = new Executor(clientCommand);
  +      serverExecutor.start();
  +      Thread.sleep(1000);
  +      clientExecutor.start();
  +      Thread.sleep(5000);
  +      assertTrue(serverExecutor.successful());
  +      assertTrue(clientExecutor.successful());
  +      log.info(getName() + " PASSES");
  +   }
  +   
  +   
  +   class Executor
  +   {
  +      private String command;
  +      private boolean successful;
  +      
  +      public Executor(String command)
  +      {
  +         this.command = command;
  +      }
  +      
  +      public boolean successful()
  +      {
  +         return successful;
  +      }
  +      
  +      public void start() throws Exception
  +      {
  +         executeCommand(command);
  +      }
  +      
  +      private void executeCommand(String command) throws Exception
  +      {
  +         final String finalCommand = command;
  +         
  +         new Thread()
  +         {
  +            public void run()
  +            {
  +               try
  +               {
  +                  log.info("executing: " + finalCommand);
  +                  final Process local = Runtime.getRuntime().exec(finalCommand);
  +                  
  +                  final BufferedReader errStream = new BufferedReader(new InputStreamReader(local.getErrorStream()));
  +                  final BufferedReader inStream = new BufferedReader(new InputStreamReader(local.getInputStream()));
  +                  new Thread()
  +                  {
  +                     public void run()
  +                     {
  +                        try
  +                        {
  +                           String errOut = null;
  +                           while((errOut = errStream.readLine()) != null)
  +                           {
  +                              System.err.println(errOut);
  +                           }
  +                        }
  +                        catch(IOException e)
  +                        {
  +                        }
  +                     }
  +                  }.start();
  +                  new Thread()
  +                  {
  +                     public void run()
  +                     {
  +                        try
  +                        {
  +                           String stdOut = null;
  +                           while((stdOut = inStream.readLine()) != null)
  +                           {
  +                              System.out.println(stdOut);
  +                           }
  +                        }
  +                        catch(IOException e)
  +                        {
  +                        }
  +                     }
  +                  }.start();
  +                  
  +                  local.waitFor();
  +                  log.info("exit value: " + local.exitValue());
  +                  successful = (local.exitValue() == 0);
  +               }
  +               catch(Exception e)
  +               {
  +                  log.error("Error starting process: " + finalCommand, e);
  +               }
  +            }
  +         }.start();
  +      }
  +   }
  +}
  
  
  
  1.1.2.1   +52 -0     JBossRemoting/src/tests/org/jboss/test/remoting/shutdown/Attic/HangingClient.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: HangingClient.java
  ===================================================================
  RCS file: HangingClient.java
  diff -N HangingClient.java
  --- /dev/null	1 Jan 1970 00:00:00 -0000
  +++ HangingClient.java	20 Jan 2007 00:42:29 -0000	1.1.2.1
  @@ -0,0 +1,52 @@
  +/*
  +* 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.shutdown;
  +
  +/** 
  + * @author <a href="ron.sigal at jboss.com">Ron Sigal</a>
  + * @version $Revision: 1.1.2.1 $
  + * <p>
  + * Copyright Jan 19, 2007
  + * </p>
  + */
  +public class HangingClient extends AbstractClient
  +{
  +   public static void main(String[] args)
  +   {
  +      try
  +      {
  +         AbstractClient client = new HangingClient();
  +         client.testShutdown();
  +      }
  +      catch (Throwable t)
  +      {
  +         t.printStackTrace();
  +      }
  +   }
  +   
  +
  +   protected boolean daemon()
  +   {
  +      return false;
  +   }
  +
  +}
  
  
  
  1.1.2.1   +52 -0     JBossRemoting/src/tests/org/jboss/test/remoting/shutdown/Attic/ClosingClient.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ClosingClient.java
  ===================================================================
  RCS file: ClosingClient.java
  diff -N ClosingClient.java
  --- /dev/null	1 Jan 1970 00:00:00 -0000
  +++ ClosingClient.java	20 Jan 2007 00:42:29 -0000	1.1.2.1
  @@ -0,0 +1,52 @@
  +/*
  +* 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.shutdown;
  +
  +/** 
  + * @author <a href="ron.sigal at jboss.com">Ron Sigal</a>
  + * @version $Revision: 1.1.2.1 $
  + * <p>
  + * Copyright Jan 19, 2007
  + * </p>
  + */
  +public class ClosingClient extends AbstractClient
  +{
  +   public static void main(String[] args)
  +   {
  +      try
  +      {
  +         AbstractClient client = new ClosingClient();
  +         client.testShutdown();
  +      }
  +      catch (Throwable t)
  +      {
  +         t.printStackTrace();
  +      }
  +   }
  +   
  +
  +   protected boolean daemon()
  +   {
  +      return true;
  +   }
  +
  +}
  
  
  



More information about the jboss-cvs-commits mailing list