From 3568faee6faf404958d34e4b8e703ecee7ec2b55 Mon Sep 17 00:00:00 2001 From: Rajith Muditha Attapattu Date: Thu, 6 May 2010 00:10:07 +0000 Subject: The heartbeat wasn't being set properly and these mistakes went uncaught due to lack of proper test. I have added a test case to AMQConnectionTest called testHeartBeat. This test fails once in a while even when I have an external script to clean the broker instance. It seems once it's wedged with kill -STOP, it somestimes doesn't get cleaned up properly with kill -9. Despite the occasional failure, I think it's worth to have this test as a lot of our users rely on heartbeat functionality. git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@941553 13f79535-47bb-0310-9956-ffa450edef68 --- .../qpid/test/unit/client/AMQConnectionTest.java | 105 +++++++++++++++++++++ .../org/apache/qpid/test/utils/QpidTestCase.java | 4 +- 2 files changed, 107 insertions(+), 2 deletions(-) (limited to 'java/systests') diff --git a/java/systests/src/main/java/org/apache/qpid/test/unit/client/AMQConnectionTest.java b/java/systests/src/main/java/org/apache/qpid/test/unit/client/AMQConnectionTest.java index 78240dde71..7f6267c210 100644 --- a/java/systests/src/main/java/org/apache/qpid/test/unit/client/AMQConnectionTest.java +++ b/java/systests/src/main/java/org/apache/qpid/test/unit/client/AMQConnectionTest.java @@ -20,6 +20,16 @@ */ package org.apache.qpid.test.unit.client; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.InputStreamReader; +import java.io.LineNumberReader; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.util.concurrent.atomic.AtomicBoolean; + +import javax.jms.Connection; +import javax.jms.ExceptionListener; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; @@ -37,6 +47,8 @@ import org.apache.qpid.client.AMQTopic; import org.apache.qpid.configuration.ClientProperties; import org.apache.qpid.framing.AMQShortString; import org.apache.qpid.test.utils.QpidTestCase; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class AMQConnectionTest extends QpidTestCase { @@ -45,6 +57,7 @@ public class AMQConnectionTest extends QpidTestCase private static AMQQueue _queue; private static QueueSession _queueSession; private static TopicSession _topicSession; + protected static final Logger _logger = LoggerFactory.getLogger(AMQConnectionTest.class); protected void setUp() throws Exception { @@ -262,6 +275,98 @@ public class AMQConnectionTest extends QpidTestCase } } + /** + * Test Strategy : Kill -STOP the broker and see + * if the client terminates the connection with a + * read timeout. + * The broker process is cleaned up in the test itself + * and avoids using process.waitFor() as it hangs. + */ + public void testHeartBeat() throws Exception + { + boolean windows = + ((String) System.getProperties().get("os.name")).matches("(?i).*windows.*"); + + if (!isCppBroker() || windows) + { + return; + } + + Process process = null; + int port = getPort(0); + try + { + _connection.close(); + System.setProperty("qpid.heartbeat", "1"); + Connection con = getConnection(); + final AtomicBoolean lock = new AtomicBoolean(false); + + String cmd = "/usr/bin/pgrep -f " + port; + process = Runtime.getRuntime().exec("/bin/bash"); + LineNumberReader reader = new LineNumberReader(new InputStreamReader(process.getInputStream())); + PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(process.getOutputStream())), true); + out.println(cmd); + String pid = reader.readLine(); + try + { + Integer.parseInt(pid); + } + catch (NumberFormatException e) + { + // Error! try to read further to gather the error msg. + String line; + _logger.debug(pid); + while ((line = reader.readLine()) != null ) + { + _logger.debug(line); + } + throw new Exception( "Unable to get the brokers pid " + pid); + } + _logger.debug("pid : " + pid); + + con.setExceptionListener(new ExceptionListener(){ + + public void onException(JMSException e) + { + synchronized(lock) { + lock.set(true); + lock.notifyAll(); + } + } + }); + + out.println("kill -STOP " + pid); + + synchronized(lock){ + lock.wait(2500); + } + out.close(); + reader.close(); + assertTrue("Client did not terminate the connection, check log for details",lock.get()); + } + catch(Exception e) + { + throw e; + } + finally + { + System.setProperty("qpid.heartbeat", ""); + if (process != null) + { + process.destroy(); + } + + Runtime.getRuntime().exec(System.getProperty("broker.kill")); + + Process brokerProcess = _brokers.remove(port); + if (process != null) + { + brokerProcess.destroy(); + } + cleanBroker(); + } + } + public static junit.framework.Test suite() { return new junit.framework.TestSuite(AMQConnectionTest.class); diff --git a/java/systests/src/main/java/org/apache/qpid/test/utils/QpidTestCase.java b/java/systests/src/main/java/org/apache/qpid/test/utils/QpidTestCase.java index 6608bc1d7d..54628ab4b8 100644 --- a/java/systests/src/main/java/org/apache/qpid/test/utils/QpidTestCase.java +++ b/java/systests/src/main/java/org/apache/qpid/test/utils/QpidTestCase.java @@ -201,7 +201,7 @@ public class QpidTestCase extends TestCase protected PrintStream _brokerOutputStream; - private Map _brokers = new HashMap(); + protected Map _brokers = new HashMap(); private InitialContext _initialContext; protected AMQConnectionFactory _connectionFactory; @@ -453,7 +453,7 @@ public class QpidTestCase extends TestCase return getPort(0); } - private int getPort(int port) + protected int getPort(int port) { if (_broker.equals(VM)) { -- cgit v1.2.1