This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new ea56d94  CAMEL-11996: RabbitConsumer could hang when RabbitMQ 
connection is lost and autoAck=false. Thanks to Joel Mongård for the unit test.
ea56d94 is described below

commit ea56d94fd347cb29530ed3ab8d9e4154c0db868b
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Fri Feb 2 09:47:48 2018 +0100

    CAMEL-11996: RabbitConsumer could hang when RabbitMQ connection is lost and 
autoAck=false. Thanks to Joel Mongård for the unit test.
---
 .../camel/component/rabbitmq/RabbitConsumer.java   |  4 ++
 .../component/rabbitmq/RabbitConsumerHangTest.java | 54 ++++++++++++++++++++++
 2 files changed, 58 insertions(+)

diff --git 
a/components/camel-rabbitmq/src/main/java/org/apache/camel/component/rabbitmq/RabbitConsumer.java
 
b/components/camel-rabbitmq/src/main/java/org/apache/camel/component/rabbitmq/RabbitConsumer.java
index ffef62d..8727673 100644
--- 
a/components/camel-rabbitmq/src/main/java/org/apache/camel/component/rabbitmq/RabbitConsumer.java
+++ 
b/components/camel-rabbitmq/src/main/java/org/apache/camel/component/rabbitmq/RabbitConsumer.java
@@ -67,6 +67,10 @@ class RabbitConsumer implements com.rabbitmq.client.Consumer 
{
             }
             //Channel might be open because while we were waiting for the 
lock, stop() has been succesfully called.
             if (!channel.isOpen()) {
+                // we could not open the channel so release the lock
+                if (!consumer.getEndpoint().isAutoAck()) {
+                    lock.release();
+                }
                 return;
             }
 
diff --git 
a/components/camel-rabbitmq/src/test/java/org/apache/camel/component/rabbitmq/RabbitConsumerHangTest.java
 
b/components/camel-rabbitmq/src/test/java/org/apache/camel/component/rabbitmq/RabbitConsumerHangTest.java
new file mode 100644
index 0000000..b4bf955
--- /dev/null
+++ 
b/components/camel-rabbitmq/src/test/java/org/apache/camel/component/rabbitmq/RabbitConsumerHangTest.java
@@ -0,0 +1,54 @@
+/**
+ * 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.
+ */
+package org.apache.camel.component.rabbitmq;
+
+import com.rabbitmq.client.Channel;
+import com.rabbitmq.client.Connection;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import static org.junit.Assert.fail;
+
+public class RabbitConsumerHangTest {
+
+    private RabbitMQConsumer consumer = Mockito.mock(RabbitMQConsumer.class);
+    private RabbitMQEndpoint endpoint = Mockito.mock(RabbitMQEndpoint.class);
+    private Connection conn = Mockito.mock(Connection.class);
+    private Channel channel = Mockito.mock(Channel.class);
+
+    @Test(timeout = 5000)
+    public void testHandleDeliveryShouldNotHangForeverIfChanelWasClosed() 
throws Exception {
+        Mockito.when(consumer.getEndpoint()).thenReturn(endpoint);
+        Mockito.when(consumer.getConnection()).thenReturn(conn);
+        Mockito.when(conn.createChannel()).thenReturn(channel);
+        Mockito.when(channel.isOpen()).thenReturn(false).thenReturn(true);
+        Mockito.when(consumer.getEndpoint()).thenReturn(endpoint);
+
+        RabbitConsumer rabbitConsumer = new RabbitConsumer(consumer);
+
+        rabbitConsumer.handleDelivery(null, null, null, null);
+        // will now fail with some NPE which is expected as we have not mocked 
all the inner details
+        try {
+            rabbitConsumer.handleDelivery(null, null, null, null);
+            fail("Should have thrown NPE");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        rabbitConsumer.stop();
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
davscl...@apache.org.

Reply via email to