You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@activemq.apache.org by GitBox <gi...@apache.org> on 2022/08/16 06:38:15 UTC

[GitHub] [activemq-nms-amqp] Havret commented on a diff in pull request #83: Implement redelivery policy.

Havret commented on code in PR #83:
URL: https://github.com/apache/activemq-nms-amqp/pull/83#discussion_r945576579


##########
src/NMS.AMQP/NmsMessageConsumer.cs:
##########
@@ -428,6 +429,17 @@ private bool IsRedeliveryExceeded(InboundMessageDispatch envelope)
 
             return false;
         }
+        
+        private int RedeliveryDelay(InboundMessageDispatch envelope)

Review Comment:
   ```suggestion
           private int GetRedeliveryDelay(InboundMessageDispatch envelope)
   ```



##########
src/NMS.AMQP/NmsMessageConsumer.cs:
##########
@@ -428,6 +429,17 @@ private bool IsRedeliveryExceeded(InboundMessageDispatch envelope)
 
             return false;
         }
+        
+        private int RedeliveryDelay(InboundMessageDispatch envelope)
+        {
+            Tracer.DebugFormat("Checking if envelope is redelivered");
+            IRedeliveryPolicy redeliveryPolicy = Session.Connection.RedeliveryPolicy;
+            if (redeliveryPolicy == null || envelope.RedeliveryCount <= 0) return 0;

Review Comment:
   Please break the line for the return statement, so this is more readable. 



##########
test/Apache-NMS-AMQP-Test/Integration/MessageRedeliveryPolicyIntegrationTest.cs:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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.
+ */
+
+using System;
+using System.Threading;
+using Amqp.Framing;
+using Apache.NMS;
+using Apache.NMS.Policies;
+using NMS.AMQP.Test.TestAmqp;
+using NUnit.Framework;
+
+namespace NMS.AMQP.Test.Integration
+{
+    [TestFixture]
+    public class MessageRedeliveryPolicyIntegrationTest : IntegrationTestFixture
+    {
+        [Test, Timeout(20_000)]
+        public void TestIncomingDeliveryCountExceededMessageGetsRejected()
+        {
+            using (TestAmqpPeer testPeer = new TestAmqpPeer())
+            {
+                IConnection connection = EstablishConnection(testPeer);
+                int initialRedeliveryDelay = 1000;
+                int clockResolution = 15;
+                connection.RedeliveryPolicy = new RedeliveryPolicy() { MaximumRedeliveries = 1, InitialRedeliveryDelay = initialRedeliveryDelay};
+                connection.Start();
+
+                testPeer.ExpectBegin();
+
+                ISession session = connection.CreateSession(AcknowledgementMode.IndividualAcknowledge);
+                IQueue queue = session.GetQueue("myQueue");
+
+                testPeer.ExpectReceiverAttach();
+                testPeer.ExpectLinkFlowRespondWithTransfer(message: new Amqp.Message() { BodySection = new AmqpValue() { Value = "hello" } });
+                testPeer.ExpectDispositionThatIsRejectedAndSettled();
+
+                IMessageConsumer consumer = session.CreateConsumer(queue);
+
+                IMessage m = consumer.Receive(TimeSpan.FromMilliseconds(3000));
+                Assert.NotNull(m, "Message should have been received");
+                Assert.IsInstanceOf<ITextMessage>(m);
+                session.Recover();
+
+                DateTime startTimer = DateTime.UtcNow;
+                m = consumer.Receive(TimeSpan.FromMilliseconds(3000));
+                Assert.That(DateTime.UtcNow.Subtract(startTimer).TotalMilliseconds, Is.GreaterThanOrEqualTo(initialRedeliveryDelay - clockResolution));
+
+                Assert.NotNull(m, "Message should have been received");
+                Assert.IsInstanceOf<ITextMessage>(m);
+                session.Recover();
+                
+                // Verify the message is no longer there. Will drain to be sure there are no messages.
+                Assert.IsNull(consumer.Receive(TimeSpan.FromMilliseconds(10)), "Message should not have been received");
+
+                testPeer.ExpectClose();
+                connection.Close();
+
+                testPeer.WaitForAllMatchersToComplete(3000);
+            }
+        }
+        
+        [Test, Timeout(20_000)]
+        public void TestIncomingDeliveryCountExceededMessageGetsRejectedAsync()
+        {
+            using (TestAmqpPeer testPeer = new TestAmqpPeer())
+            {
+                IConnection connection = EstablishConnection(testPeer);
+                int initialRedeliveryDelay = 1000;
+                connection.RedeliveryPolicy = new RedeliveryPolicy() { MaximumRedeliveries = 1, InitialRedeliveryDelay = initialRedeliveryDelay};
+                connection.Start();
+
+                testPeer.ExpectBegin();
+
+                ISession session = connection.CreateSession(AcknowledgementMode.IndividualAcknowledge);
+                IQueue queue = session.GetQueue("myQueue");
+                
+                
+                testPeer.ExpectReceiverAttach();
+                testPeer.ExpectLinkFlowRespondWithTransfer(message: new Amqp.Message() { BodySection = new AmqpValue() { Value = "hello" } });
+                testPeer.ExpectDispositionThatIsRejectedAndSettled();
+
+                IMessageConsumer consumer = session.CreateConsumer(queue);
+
+                CountdownEvent success = new CountdownEvent(2);
+
+                consumer.Listener += m =>
+                {
+                        session.Recover();
+                        success.Signal();
+                };
+                
+                Assert.IsTrue(success.Wait(TimeSpan.FromSeconds(5)), "Didn't get expected messages");

Review Comment:
   Waiting for 5 seconds is a bit too long.



##########
src/NMS.AMQP/NmsMessageConsumer.cs:
##########
@@ -428,6 +429,17 @@ private bool IsRedeliveryExceeded(InboundMessageDispatch envelope)
 
             return false;
         }
+        
+        private int RedeliveryDelay(InboundMessageDispatch envelope)
+        {
+            Tracer.DebugFormat("Checking if envelope is redelivered");
+            IRedeliveryPolicy redeliveryPolicy = Session.Connection.RedeliveryPolicy;
+            if (redeliveryPolicy == null || envelope.RedeliveryCount <= 0) return 0;
+            
+            var redeliveryDelay = redeliveryPolicy.RedeliveryDelay(envelope.RedeliveryCount);
+            Tracer.DebugFormat("Envelope has been redelivered, apply redelivery policy wait {0} milliseconds", redeliveryDelay);

Review Comment:
   This should probably be logged only when `redeliveryDelay > 0`. Ideally you could place it inside of the `ApplyRedeliveryPolicy` method. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org