summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRajith Muditha Attapattu <rajith@apache.org>2010-05-06 00:10:07 +0000
committerRajith Muditha Attapattu <rajith@apache.org>2010-05-06 00:10:07 +0000
commit3568faee6faf404958d34e4b8e703ecee7ec2b55 (patch)
tree15420561c605941180ec569160170dac59e438fc
parent90221b57782499ad38e6ae9cc1522727af49b4d1 (diff)
downloadqpid-python-3568faee6faf404958d34e4b8e703ecee7ec2b55.tar.gz
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
-rw-r--r--java/client/src/main/java/org/apache/qpid/client/AMQConnectionDelegate_0_10.java2
-rw-r--r--java/systests/src/main/java/org/apache/qpid/test/unit/client/AMQConnectionTest.java105
-rw-r--r--java/systests/src/main/java/org/apache/qpid/test/utils/QpidTestCase.java4
-rwxr-xr-xjava/test-profiles/kill-broker25
4 files changed, 133 insertions, 3 deletions
diff --git a/java/client/src/main/java/org/apache/qpid/client/AMQConnectionDelegate_0_10.java b/java/client/src/main/java/org/apache/qpid/client/AMQConnectionDelegate_0_10.java
index 4717a9495b..32c7bb33b0 100644
--- a/java/client/src/main/java/org/apache/qpid/client/AMQConnectionDelegate_0_10.java
+++ b/java/client/src/main/java/org/apache/qpid/client/AMQConnectionDelegate_0_10.java
@@ -396,7 +396,7 @@ public class AMQConnectionDelegate_0_10 implements AMQConnectionDelegate, Connec
{
heartbeat = Integer.getInteger(ClientProperties.HEARTBEAT,ClientProperties.HEARTBEAT_DEFAULT);
}
- return 0;
+ return heartbeat;
}
protected org.apache.qpid.transport.Connection getQpidConnection()
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<Integer, Process> _brokers = new HashMap<Integer, Process>();
+ protected Map<Integer, Process> _brokers = new HashMap<Integer, Process>();
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))
{
diff --git a/java/test-profiles/kill-broker b/java/test-profiles/kill-broker
new file mode 100755
index 0000000000..e31d3811b3
--- /dev/null
+++ b/java/test-profiles/kill-broker
@@ -0,0 +1,25 @@
+
+#!/bin/bash
+#
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+#
+
+
+kill -9 $1