[jboss-cvs] JBossRemoting/src/tests/org/jboss/test/remoting/shutdown ...
Ron Sigal
ron_sigal at yahoo.com
Mon Jan 22 01:41:09 EST 2007
User: rsigal
Date: 07/01/22 01:41:09
Modified: src/tests/org/jboss/test/remoting/shutdown Tag:
remoting_2_x AbstractClient.java
ShutdownTestServer.java HangingClient.java
ClosingClient.java
Added: src/tests/org/jboss/test/remoting/shutdown Tag:
remoting_2_x log4j.xml ShutdownTestParent.java
Removed: src/tests/org/jboss/test/remoting/shutdown Tag:
remoting_2_x ShutdownTestCase.java
Log:
JBREM-674: Adding unit tests (one per transport) to verify that a Remoting application will shut down properly without any threads hanging it up.
Revision Changes Path
No revision
No revision
1.1.2.2 +58 -4 JBossRemoting/src/tests/org/jboss/test/remoting/shutdown/AbstractClient.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: AbstractClient.java
===================================================================
RCS file: /cvsroot/jboss/JBossRemoting/src/tests/org/jboss/test/remoting/shutdown/AbstractClient.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
--- AbstractClient.java 20 Jan 2007 00:42:29 -0000 1.1.2.1
+++ AbstractClient.java 22 Jan 2007 06:41:09 -0000 1.1.2.2
@@ -23,43 +23,80 @@
import java.net.InetAddress;
import java.util.HashMap;
+import java.util.Map;
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.CallbackPoller;
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 $
+ * @version $Revision: 1.1.2.2 $
* <p>
* Copyright Jan 19, 2007
* </p>
*/
public abstract class AbstractClient extends TestCase
{
+ private String transport;
+ private Map extraConfig;
+
+
+ public AbstractClient(String transport, Map config)
+ {
+ this.transport = transport;
+ this.extraConfig = new HashMap(config);
+ System.out.println("client transport: " + transport);
+ System.out.println("log4j.configuration: " + System.getProperty("log4j.configuration"));
+ Runtime.getRuntime().traceMethodCalls(true);
+ }
+
+
+ /**
+ * This test is used to verify that a JVM with a client connected to a server will shut
+ * down. To exercise as many threads as possible, it enables leasing, registers a
+ * connection listener, and registers a callback handler for polled callbacks.
+ *
+ * At the end of the method, it creates a Thread which runs longer that this test is
+ * supposed to last. According to the value returned by the overridden abstract
+ * method daemon(), it the Thread will be a daemon or non-daemon thread.
+ */
public void testShutdown() throws Throwable
{
try
{
String host = InetAddress.getLocalHost().getHostAddress();
- String locatorURI = "socket://" + host + ":" + ShutdownTestServer.port;
+ String locatorURI = transport + "://" + host + ":" + ShutdownTestServer.port;
InvokerLocator locator = new InvokerLocator(locatorURI);
- HashMap clientConfig = new HashMap();
+ HashMap clientConfig = new HashMap(extraConfig);
+ clientConfig.put(Client.ENABLE_LEASE, "true");
+ clientConfig.put(InvokerLocator.CLIENT_LEASE_PERIOD, "1000");
Client client = new Client(locator, clientConfig);
client.connect();
System.out.println("client connected");
+ client.addConnectionListener(new ShutdownTestServer.TestListener(), 1000);
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);
+ System.out.println("added connection listener");
+ System.out.println("adding listener");
+ HashMap metadata = new HashMap();
+ metadata.put(CallbackPoller.CALLBACK_POLL_PERIOD, "500");
+ client.addListener(callbackHandler, metadata, null, false);
+ System.out.println("added listener");
+ Thread.sleep(1000);
if (!callbackHandler.receivedCallback)
+ {
+ System.out.println("callback failed");
throw new Exception("callback failed");
+ }
System.out.println("callback successful");
client.removeListener(callbackHandler);
client.disconnect();
@@ -92,6 +129,23 @@
abstract protected boolean daemon();
+ protected static void getConfig(Map config, String configs)
+ {
+ int start = 0;
+ int ampersand = configs.indexOf('&');
+ while (ampersand > 0)
+ {
+ String s = configs.substring(start, ampersand);
+ int equals = s.indexOf('=');
+ String param = s.substring(0, equals);
+ String value = s.substring(equals + 1);
+ config.put(param, value);
+ start = ampersand + 1;
+ ampersand = configs.indexOf('&', start);
+ }
+ }
+
+
public class TestCallbackHandler implements InvokerCallbackHandler
{
public boolean receivedCallback;
1.1.2.2 +35 -3 JBossRemoting/src/tests/org/jboss/test/remoting/shutdown/ShutdownTestServer.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: ShutdownTestServer.java
===================================================================
RCS file: /cvsroot/jboss/JBossRemoting/src/tests/org/jboss/test/remoting/shutdown/ShutdownTestServer.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
--- ShutdownTestServer.java 20 Jan 2007 00:42:29 -0000 1.1.2.1
+++ ShutdownTestServer.java 22 Jan 2007 06:41:09 -0000 1.1.2.2
@@ -27,6 +27,8 @@
import javax.management.MBeanServer;
import org.jboss.jrunit.extensions.ServerTestCase;
+import org.jboss.remoting.Client;
+import org.jboss.remoting.ConnectionListener;
import org.jboss.remoting.InvocationRequest;
import org.jboss.remoting.InvokerLocator;
import org.jboss.remoting.ServerInvocationHandler;
@@ -37,8 +39,13 @@
import org.jboss.remoting.transport.Connector;
/**
+ * This is the server half of a unit test designed to verify that a Remoting application
+ * will shut down without any stray threads hanging it up. To exercise as many
+ * Remoting threads as possible, the server enables leasing and registers a
+ * connection listener.
+ *
* @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 Jan 19, 2007
* </p>
@@ -47,17 +54,26 @@
{
public static int port = 9876;
private Connector connector;
+ private String transport;
+
+
+ public ShutdownTestServer(String transport)
+ {
+ this.transport = transport;
+ }
public void setUp() throws Exception
{
String host = InetAddress.getLocalHost().getHostAddress();
- String locatorURI = "socket://" + host + ":" + port;
+ String locatorURI = transport + "://" + host + ":" + port;
InvokerLocator locator = new InvokerLocator(locatorURI);
HashMap serverConfig = new HashMap();
connector = new Connector(locator, serverConfig);
connector.create();
+ connector.setLeasePeriod(1000);
connector.addInvocationHandler("test", new TestHandler());
+ connector.addConnectionListener(new TestListener());
connector.start();
System.out.println("server started at: " + locatorURI);
}
@@ -75,7 +91,11 @@
public static void main(String[] args)
{
- ShutdownTestServer server = new ShutdownTestServer();
+ if (args.length == 0)
+ throw new RuntimeException();
+
+ String transport = args[0];
+ ShutdownTestServer server = new ShutdownTestServer(transport);
try
{
server.setUp();
@@ -104,14 +124,26 @@
{
try
{
+ System.out.println("sending callback");
callbackHandler.handleCallback(new Callback("callback"));
+ System.out.println("sent callback");
}
catch (HandleCallbackException e)
{
System.out.println("error handling callback");
+ e.printStackTrace();
}
}
public void removeListener(InvokerCallbackHandler callbackHandler) {}
}
+
+
+ public static class TestListener implements ConnectionListener
+ {
+ public void handleConnectionException(Throwable throwable, Client client)
+ {
+ System.out.println("got connection exception: " + throwable);
+ }
+ }
}
1.1.2.2 +25 -2 JBossRemoting/src/tests/org/jboss/test/remoting/shutdown/HangingClient.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: HangingClient.java
===================================================================
RCS file: /cvsroot/jboss/JBossRemoting/src/tests/org/jboss/test/remoting/shutdown/HangingClient.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
--- HangingClient.java 20 Jan 2007 00:42:29 -0000 1.1.2.1
+++ HangingClient.java 22 Jan 2007 06:41:09 -0000 1.1.2.2
@@ -21,20 +21,43 @@
*/
package org.jboss.test.remoting.shutdown;
+import java.util.HashMap;
+import java.util.Map;
+
/**
+ * This class is derived from AbstractClient, and its overriding method daemon() returns
+ * false. That is, AbstractClient will create a long running daemon thread which should
+ * prevent it from terminating.
+ *
* @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 Jan 19, 2007
* </p>
*/
public class HangingClient extends AbstractClient
{
+ public HangingClient(String transport, Map config)
+ {
+ super(transport, config);
+ }
+
+
public static void main(String[] args)
{
try
{
- AbstractClient client = new HangingClient();
+ if (args.length == 0)
+ throw new RuntimeException();
+
+ String transport = args[0];
+
+ HashMap config = new HashMap();
+ System.out.println("args.length: " + args.length);
+ if (args.length > 1)
+ getConfig(config, args[1]);
+
+ AbstractClient client = new HangingClient(transport, config);
client.testShutdown();
}
catch (Throwable t)
1.1.2.2 +25 -2 JBossRemoting/src/tests/org/jboss/test/remoting/shutdown/ClosingClient.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: ClosingClient.java
===================================================================
RCS file: /cvsroot/jboss/JBossRemoting/src/tests/org/jboss/test/remoting/shutdown/ClosingClient.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
--- ClosingClient.java 20 Jan 2007 00:42:29 -0000 1.1.2.1
+++ ClosingClient.java 22 Jan 2007 06:41:09 -0000 1.1.2.2
@@ -21,20 +21,43 @@
*/
package org.jboss.test.remoting.shutdown;
+import java.util.HashMap;
+import java.util.Map;
+
/**
+ * This class is derived from AbstractClient, and its overriding method daemon() returns
+ * true. That is, AbstractClient will create a long running daemon thread which should not
+ * prevent it from terminating.
+ *
* @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 Jan 19, 2007
* </p>
*/
public class ClosingClient extends AbstractClient
{
+ public ClosingClient(String transport, Map config)
+ {
+ super(transport, config);
+ }
+
+
public static void main(String[] args)
{
try
{
- AbstractClient client = new ClosingClient();
+ if (args.length == 0)
+ throw new RuntimeException();
+
+ String transport = args[0];
+
+ HashMap config = new HashMap();
+ System.out.println("args.length: " + args.length);
+ if (args.length > 1)
+ getConfig(config, args[1]);
+
+ AbstractClient client = new ClosingClient(transport, config);
client.testShutdown();
}
catch (Throwable t)
No revision
No revision
1.1.2.1 +99 -0 JBossRemoting/src/tests/org/jboss/test/remoting/shutdown/Attic/log4j.xml
(In the diff below, changes in quantity of whitespace are not shown.)
Index: log4j.xml
===================================================================
RCS file: log4j.xml
diff -N log4j.xml
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ log4j.xml 22 Jan 2007 06:41:09 -0000 1.1.2.1
@@ -0,0 +1,99 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
+
+<!-- ===================================================================== -->
+<!-- -->
+<!-- Log4j Configuration -->
+<!-- -->
+<!-- ===================================================================== -->
+
+<!-- $Id: log4j.xml,v 1.1.2.1 2007/01/22 06:41:09 rsigal Exp $ -->
+
+<!--
+ | For more configuration infromation and examples see the Jakarta Log4j
+ | owebsite: http://jakarta.apache.org/log4j
+ -->
+
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
+
+ <!-- ================================= -->
+ <!-- Preserve messages in a local file -->
+ <!-- ================================= -->
+
+ <!-- A time/date based rolling appender -->
+ <appender name="FILE" class="org.jboss.logging.appender.DailyRollingFileAppender">
+ <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
+ <param name="File" value="output/logs/client.log"/>
+ <param name="Append" value="false"/>
+
+ <!-- Rollover at midnight each day -->
+ <param name="DatePattern" value="'.'yyyy-MM-dd"/>
+
+ <!-- Rollover at the top of each hour
+ <param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
+ -->
+
+ <layout class="org.apache.log4j.PatternLayout">
+ <!-- The default pattern: Date Priority [Category] Message\n -->
+ <!--<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>-->
+ <param name="ConversionPattern" value="[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n"/>
+
+ <!-- The full pattern: Date MS Priority [Category] (Thread:NDC) Message\n
+ <param name="ConversionPattern" value="%d %-5r %-5p [%c] (%t:%x) %m%n"/>
+ -->
+ </layout>
+ </appender>
+
+ <!-- ============================== -->
+ <!-- Append messages to the console -->
+ <!-- ============================== -->
+
+ <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
+ <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
+ <param name="Target" value="System.out"/>
+ <!-- <param name="Threshold" value="INFO"/> -->
+ <param name="Threshold" value="TRACE#org.jboss.logging.XLevel"/>
+
+ <layout class="org.apache.log4j.PatternLayout">
+ <!-- The default pattern: Date Priority [Category] Message\n -->
+ <!--<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>-->
+ <param name="ConversionPattern" value="[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n"/>
+ </layout>
+ </appender>
+
+ <!-- ================ -->
+ <!-- Limit categories -->
+ <!-- ================ -->
+
+ <!-- Limit the org.apache category to INFO as its DEBUG is verbose -->
+ <category name="org.apache">
+ <priority value="ERROR"/>
+ </category>
+
+ <!-- Limit JBoss categories -->
+ <category name="org.jboss">
+ <priority value="ERROR"/>
+ </category>
+
+
+ <category name="org.jboss.remoting">
+ <priority value="trace" class="org.jboss.logging.XLevel"/>
+ </category>
+
+
+ <category name="org.jboss.test.remoting">
+ <priority value="trace" class="org.jboss.logging.XLevel"/>
+ </category>
+
+
+ <!-- ======================= -->
+ <!-- Setup the Root category -->
+ <!-- ======================= -->
+
+ <root>
+ <!-- --><appender-ref ref="CONSOLE"/><!-- -->
+ <!-- --><appender-ref ref="FILE"/> <!-- -->
+ </root>
+
+
+</log4j:configuration>
1.1.2.1 +223 -0 JBossRemoting/src/tests/org/jboss/test/remoting/shutdown/Attic/ShutdownTestParent.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: ShutdownTestParent.java
===================================================================
RCS file: ShutdownTestParent.java
diff -N ShutdownTestParent.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ShutdownTestParent.java 22 Jan 2007 06:41:09 -0000 1.1.2.1
@@ -0,0 +1,223 @@
+/*
+* 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 abstract class ShutdownTestParent extends TestCase
+{
+ private Logger log = Logger.getLogger(ShutdownTestParent.class);
+
+ protected boolean serverSuccessful;
+ protected boolean clientSuccessful;
+
+
+ public void setUp()
+ {
+ serverSuccessful = false;
+ clientSuccessful = false;
+ }
+
+ /**
+ * 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") + "\" ";
+ command += getJVMArguments() + " ";
+ String serverCommand = command + ShutdownTestServer.class.getName() + " " + getTransport();
+ serverCommand += " " + getServerArgs();
+ String clientCommand = command + HangingClient.class.getName() + " " + getTransport();
+ clientCommand += " " + getClientArgs();
+ new Executor(serverCommand, true).start();
+ Thread.sleep(1000);
+ new Executor(clientCommand, false).start();
+ Thread.sleep(5000);
+ assertTrue(serverSuccessful);
+ assertFalse(clientSuccessful);
+ log.info(getName() + " PASSES");
+ }
+
+
+ /**
+ * This unit verifies that a JVM with a client, and another JVM with a server,
+ * both terminate.
+ *
+ * @throws Throwable
+ */
+ public void testClosingClient() throws Throwable
+ {
+ log.info("entering testShutdown()");
+
+ String command = "java -cp \"" + System.getProperty("java.class.path") + "\" ";
+ command += getJVMArguments() + " ";
+ String serverCommand = command + ShutdownTestServer.class.getName() + " " + getTransport();
+ serverCommand += " " + getServerArgs();
+ String clientCommand = command + ClosingClient.class.getName() + " " + getTransport();
+ clientCommand += " " + getClientArgs();
+ new Executor(serverCommand, true).start();
+ Thread.sleep(1000);
+ new Executor(clientCommand, false).start();
+ Thread.sleep(5000);
+ assertTrue(serverSuccessful);
+ assertTrue(clientSuccessful);
+ log.info(getName() + " PASSES");
+ }
+
+
+ protected String getJVMArguments()
+ {
+ return "";
+ }
+
+
+ protected String getServerArgs()
+ {
+ return "";
+ }
+
+
+ protected String getClientArgs()
+ {
+ return "";
+ }
+
+
+ abstract protected String getTransport();
+
+
+ class Executor
+ {
+ private String command;
+ private boolean server;
+ private boolean successful;
+
+ public Executor(String command, boolean server)
+ {
+ this.server = server;
+ 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();
+ String clientOrServer = server ? "server" : "client";
+ log.info(clientOrServer + " exit value: " + local.exitValue());
+ successful = (local.exitValue() == 0);
+ if (server)
+ serverSuccessful = successful;
+ else
+ clientSuccessful = successful;
+ }
+ catch(Exception e)
+ {
+ log.error("Error starting process: " + finalCommand, e);
+ }
+ }
+ }.start();
+ }
+ }
+}
\ No newline at end of file
More information about the jboss-cvs-commits
mailing list