activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r831103 - in /activemq/activemq-dotnet: Apache.NMS.ActiveMQ/trunk/src/main/csharp/ Apache.NMS/trunk/src/main/csharp/ Apache.NMS/trunk/src/main/csharp/policies/ Apache.NMS/trunk/src/test/csharp/
Date Thu, 29 Oct 2009 21:44:37 GMT
Author: tabish
Date: Thu Oct 29 21:44:36 2009
New Revision: 831103

URL: http://svn.apache.org/viewvc?rev=831103&view=rev
Log:
https://issues.apache.org/activemq/browse/AMQNET-137

Added IRedeliveryPolicy to NMS along with a default Redelivery policy for NMS providers to
use in their IConnectionFactory implementations.  Added the use of the default policy to NMS.ActiveMQ
and used that policy to control the redelivery delay in the MessageConsumers rollback method.

Added:
    activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/IRedeliveryPolicy.cs   (with
props)
    activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/policies/
    activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/policies/RedeliveryPolicy.cs
  (with props)
    activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/RedeliveryPolicyTest.cs   (with
props)
Modified:
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Connection.cs
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/ConnectionFactory.cs
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/MessageConsumer.cs
    activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/IConnectionFactory.cs
    activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/NMSConnectionFactory.cs

Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Connection.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Connection.cs?rev=831103&r1=831102&r2=831103&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Connection.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Connection.cs Thu Oct
29 21:44:36 2009
@@ -20,6 +20,7 @@
 using System.Threading;
 using Apache.NMS.ActiveMQ.Commands;
 using Apache.NMS.ActiveMQ.Transport;
+using Apache.NMS;
 using Apache.NMS.Util;
 
 namespace Apache.NMS.ActiveMQ
@@ -54,6 +55,7 @@
         private readonly Atomic<bool> started = new Atomic<bool>(false);
         private ConnectionMetaData metaData = null;
         private bool disposed = false;
+        private IRedeliveryPolicy redeliveryPolicy;
 
         public Connection(Uri connectionUri, ITransport transport, ConnectionInfo info)
         {
@@ -234,6 +236,12 @@
         {
             get { return brokerWireFormatInfo; }
         }
+
+        public IRedeliveryPolicy RedeliveryPolicy
+        {
+            get { return this.redeliveryPolicy; }
+            set { this.redeliveryPolicy = value; }
+        }
         
         #endregion
 

Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/ConnectionFactory.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/ConnectionFactory.cs?rev=831103&r1=831102&r2=831103&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/ConnectionFactory.cs
(original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/ConnectionFactory.cs
Thu Oct 29 21:44:36 2009
@@ -18,7 +18,9 @@
 using System;
 using Apache.NMS.ActiveMQ.Commands;
 using Apache.NMS.ActiveMQ.Transport;
+using Apache.NMS;
 using Apache.NMS.Util;
+using Apache.NMS.Policies;
 
 namespace Apache.NMS.ActiveMQ
 {
@@ -35,6 +37,7 @@
 		private string connectionUserName;
 		private string connectionPassword;
 		private string clientId;
+        private IRedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
 
 		static ConnectionFactory()
 		{
@@ -99,6 +102,7 @@
 			URISupport.CompositeData c = URISupport.parseComposite(uri);
 			URISupport.SetProperties(connection, c.Parameters, "connection.");
 
+            connection.RedeliveryPolicy = this.RedeliveryPolicy;
 			connection.ITransport.Start();
 			return connection;
 		}
@@ -132,6 +136,18 @@
 			set { clientId = value; }
 		}
 
+        public IRedeliveryPolicy RedeliveryPolicy
+        {
+            get { return this.redeliveryPolicy; }
+            set 
+            {
+                if(value != null)
+                {
+                    this.redeliveryPolicy = value; 
+                }
+            }
+        }
+
 		public event ExceptionListener OnException
 		{
 			add { onException += value; }

Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/MessageConsumer.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/MessageConsumer.cs?rev=831103&r1=831102&r2=831103&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/MessageConsumer.cs
(original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/MessageConsumer.cs
Thu Oct 29 21:44:36 2009
@@ -63,12 +63,15 @@
         private const int DEFAULT_MAX_REDELIVERIES = 5;
 
         private event MessageListener listener;
+
+        private IRedeliveryPolicy redeliveryPolicy;
         
 		// Constructor internal to prevent clients from creating an instance.
 		internal MessageConsumer(Session session, ConsumerInfo info)
 		{
 			this.session = session;
 			this.info = info;
+            this.redeliveryPolicy = this.session.Connection.RedeliveryPolicy;
 		}
 
 		~MessageConsumer()
@@ -105,6 +108,12 @@
             get { return this.info.PrefetchSize; }
         }
 
+        public IRedeliveryPolicy RedeliveryPolicy
+        {
+            get { return this.redeliveryPolicy; }
+            set { this.redeliveryPolicy = value; }
+        }
+
         #endregion
 
 		#region IMessageConsumer Members
@@ -812,8 +821,7 @@
                     
                     if(currentRedeliveryCount > 0) 
                     {
-                        redeliveryDelay = 1000;
-                        //redeliveryDelay = redeliveryPolicy.getRedeliveryDelay(redeliveryDelay);
+                        redeliveryDelay = this.redeliveryPolicy.RedeliveryDelay(currentRedeliveryCount);
                     }
                     
                     MessageId firstMsgId = this.dispatchedMessages.Last.Value.Message.MessageId;
@@ -824,14 +832,10 @@
                         dispatch.Message.OnMessageRollback();
                     }
         
-                    //if(redeliveryPolicy.getMaximumRedeliveries() != RedeliveryPolicy.NO_MAXIMUM_REDELIVERIES
-                    //    && lastMd.getMessage().getRedeliveryCounter() > redeliveryPolicy.getMaximumRedeliveries())
{
-                    if(lastMd.Message.RedeliveryCounter > MessageConsumer.DEFAULT_MAX_REDELIVERIES)
-                    {
-                        // We need to NACK the messages so that they get sent to the
-                        // DLQ.
-                        // Acknowledge the last message.
-                        
+                    if(this.redeliveryPolicy.MaximumRedeliveries >= 0 &&
+                       lastMd.Message.RedeliveryCounter > this.redeliveryPolicy.MaximumRedeliveries)
+                    {
+                        // We need to NACK the messages so that they get sent to the DLQ.
                         MessageAck ack = new MessageAck();
 
                         ack.AckType = (byte)AckType.PoisonAck;
@@ -846,11 +850,11 @@
                         // Adjust the window size.
                         additionalWindowSize = Math.Max(0, this.additionalWindowSize - this.dispatchedMessages.Count);
                         
-                        //redeliveryDelay = 0;                        
+                        this.redeliveryDelay = 0;
                     } 
                     else
                     {                        
-                        // only redelivery_ack after first delivery
+                        // We only send a RedeliveryAck after the first redelivery
                         if(currentRedeliveryCount > 0)
                         {
                             MessageAck ack = new MessageAck();
@@ -877,7 +881,9 @@
                         {
                             DateTime deadline = DateTime.Now.AddMilliseconds(redeliveryDelay);
                             ThreadPool.QueueUserWorkItem(this.RollbackHelper, deadline);
-                        } else {
+                        } 
+                        else 
+                        {
                             Start();
                         }
                     }

Modified: activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/IConnectionFactory.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/IConnectionFactory.cs?rev=831103&r1=831102&r2=831103&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/IConnectionFactory.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/IConnectionFactory.cs Thu Oct
29 21:44:36 2009
@@ -38,5 +38,11 @@
 		/// Get/or set the broker Uri.
 		/// </summary>
 		Uri BrokerUri { get; set; }
+
+        /// <summary>
+        /// Get/or set the redelivery policy that new IConnection objects are
+        /// assigned upon creation.
+        /// </summary>
+        IRedeliveryPolicy RedeliveryPolicy{ get; set; }
 	}
 }

Added: activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/IRedeliveryPolicy.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/IRedeliveryPolicy.cs?rev=831103&view=auto
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/IRedeliveryPolicy.cs (added)
+++ activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/IRedeliveryPolicy.cs Thu Oct
29 21:44:36 2009
@@ -0,0 +1,75 @@
+/*
+ * 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;
+
+namespace Apache.NMS
+{
+    public interface IRedeliveryPolicy : ICloneable
+    {
+        /// <summary>
+        /// Gets or sets the collision avoidance percent.  This causes the redelivery delay
+        /// to be adjusted in order to avoid possible collision when messages are redelivered
+        /// to concurrent consumers.
+        /// </summary>
+        /// <value>The collision avoidance factor.</value>
+        int CollisionAvoidancePercent{ get; set; }
+
+        /// <summary>
+        /// Gets or sets a value indicating whether to [use collision avoidance].
+        /// </summary>
+        /// <value>
+        ///     <c>true</c> if [use collision avoidance]; otherwise, <c>false</c>.
+        /// </value>
+        bool UseCollisionAvoidance{ get; set; }
+
+        /// <summary>
+        /// The time in milliseconds to initially delay a redelivery
+        /// </summary>
+        /// <value>The initial redelivery delay.</value>
+        int InitialRedeliveryDelay{ get; set; }
+
+        /// <summary>
+        /// Gets or sets the maximum redeliveries.  A value less than zero indicates
+        /// that there is no maximum and the NMS provider should retry forever.
+        /// </summary>
+        /// <value>The maximum redeliveries.</value>
+        int MaximumRedeliveries{ get; set; }
+
+        /// <summary>
+        /// The time in milliseconds to delay a redelivery
+        /// </summary>
+        /// <param name="redeliveredCounter">The redelivered counter.</param>
+        /// <returns></returns>
+        int RedeliveryDelay(int redeliveredCounter);
+
+        /// <summary>
+        /// Gets or sets a value indicating whether [use exponential back off].
+        /// </summary>
+        /// <value>
+        ///     <c>true</c> if [use exponential back off]; otherwise, <c>false</c>.
+        /// </value>
+        bool UseExponentialBackOff{ get; set; }
+
+        /// <summary>
+        /// Gets or sets the back off multiplier.
+        /// </summary>
+        /// <value>The back off multiplier.</value>
+        int BackOffMultiplier{ get; set; }
+        
+    }
+}

Propchange: activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/IRedeliveryPolicy.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/NMSConnectionFactory.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/NMSConnectionFactory.cs?rev=831103&r1=831102&r2=831103&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/NMSConnectionFactory.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/NMSConnectionFactory.cs Thu
Oct 29 21:44:36 2009
@@ -45,7 +45,7 @@
 	public class NMSConnectionFactory : IConnectionFactory
 	{
 		protected readonly IConnectionFactory factory;
-		protected static readonly Dictionary<string, ProviderFactoryInfo> schemaProviderFactoryMap;
+		protected static readonly Dictionary<string, ProviderFactoryInfo> schemaProviderFactoryMap;
 
 		/// <summary>
 		/// Static class constructor
@@ -343,6 +343,17 @@
 		public IConnectionFactory ConnectionFactory
 		{
 			get { return factory; }
-		}
+		}
+
+        /// <summary>
+        /// Get/or Set the IRedeliveryPolicy instance using the IConnectionFactory implementation
+        /// that is being used.
+        /// </summary>
+        public IRedeliveryPolicy RedeliveryPolicy
+        {
+            get { return this.factory.RedeliveryPolicy; }
+            set { this.factory.RedeliveryPolicy = value; }
+        }
+        
 	}
 }

Added: activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/policies/RedeliveryPolicy.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/policies/RedeliveryPolicy.cs?rev=831103&view=auto
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/policies/RedeliveryPolicy.cs
(added)
+++ activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/policies/RedeliveryPolicy.cs
Thu Oct 29 21:44:36 2009
@@ -0,0 +1,156 @@
+/*
+ * 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.Collections.Generic;
+using System.Text;
+
+namespace Apache.NMS.Policies
+{
+    /// <summary>
+    /// A policy used to customize exactly how you want the redelivery to work.
+    /// </summary>
+    public class RedeliveryPolicy : IRedeliveryPolicy
+    {
+        private static readonly object syncObject = new object();
+        
+        private double collisionAvoidanceFactor = .15;
+        private int initialRedeliveryDelay = 1000;
+        private int maximumRedeliveries = 6;
+        private int backOffMultiplier = 5;
+        private bool useCollisionAvoidance = false;
+        private bool useExponentialBackOff = false;
+        
+        private static Random randomNumberGenerator;
+        private static bool nextBool = false;
+
+        #region IRedeliveryPolicy Members
+
+        public int CollisionAvoidancePercent
+        {
+            get { return Convert.ToInt32(Math.Round(collisionAvoidanceFactor * 100)); }
+            set { collisionAvoidanceFactor = Convert.ToDouble(value) * .01; }
+        }
+
+        public bool UseCollisionAvoidance
+        {
+            get { return this.useCollisionAvoidance; }
+            set { this.useCollisionAvoidance = value; }
+        }
+
+        public int InitialRedeliveryDelay
+        {
+            get { return this.initialRedeliveryDelay; }
+            set { this.initialRedeliveryDelay = value; }
+        }
+
+        public int MaximumRedeliveries
+        {
+            get { return this.maximumRedeliveries; }
+            set { this.maximumRedeliveries = value; }
+        }
+
+        public int RedeliveryDelay(int redeliveredCounter)
+        {
+            int delay = 0;
+            
+            if(UseExponentialBackOff && BackOffMultiplier > 1)
+            {
+                delay = Convert.ToInt32(initialRedeliveryDelay * (Math.Pow(BackOffMultiplier,
redeliveredCounter)));    
+            }
+            else
+            {
+                delay = InitialRedeliveryDelay;
+            }
+
+            if(UseCollisionAvoidance)
+            {
+                Random random = RandomNumberGenerator;
+                var variance = (NextBool ? collisionAvoidanceFactor : collisionAvoidanceFactor
*= -1)*random.NextDouble();
+                delay += Convert.ToInt32(Convert.ToDouble(delay) * variance);
+            }
+
+            return delay;
+        }
+
+        public bool UseExponentialBackOff
+        {
+            get { return this.useExponentialBackOff; }
+            set { this.useExponentialBackOff = value; }
+        }
+       
+        public int BackOffMultiplier
+        {
+            get { return backOffMultiplier; }
+            set { backOffMultiplier = value; }
+        }
+
+        #endregion
+
+        /// <summary>
+        /// Gets the random number generator.
+        /// </summary>
+        /// <value>The random number generator.</value>
+        protected static Random RandomNumberGenerator
+        {
+            get
+            {
+                if(randomNumberGenerator == null)
+                {
+                    lock(syncObject)
+                    {
+                        if(randomNumberGenerator == null)
+                        {
+                            randomNumberGenerator = new Random(DateTime.Now.Second);
+                        }
+                    }
+                }
+
+                return randomNumberGenerator;
+            }
+        }
+
+        /// <summary>
+        /// Gets the next boolean
+        /// </summary>
+        /// <value><c>true</c> if [next bool]; otherwise, <c>false</c>.</value>
+        protected static bool NextBool
+        {
+            get
+            {
+                lock(syncObject)
+                {
+                    nextBool = !nextBool;
+                    return nextBool;
+                }
+            }
+        }
+
+        /// <summery>
+        /// Clone this object and return a new instance that the caller now owns.
+        /// </summery>
+        public Object Clone()
+        {
+            // Since we are a derived class use the base's Clone()
+            // to perform the shallow copy. Since it is shallow it
+            // will include our derived class. Since we are derived,
+            // this method is an override.
+            return this.MemberwiseClone();
+        }
+        
+    }
+}

Propchange: activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/policies/RedeliveryPolicy.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/RedeliveryPolicyTest.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/RedeliveryPolicyTest.cs?rev=831103&view=auto
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/RedeliveryPolicyTest.cs (added)
+++ activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/RedeliveryPolicyTest.cs Thu
Oct 29 21:44:36 2009
@@ -0,0 +1,107 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Apache.NMS;
+using Apache.NMS.Policies;
+using NUnit.Framework;
+
+namespace Apache.NMS.Test
+{
+    [TestFixture]
+    public class RedeliveryPolicyTest
+    {
+        [Test]
+        public void Executes_redelivery_policy_with_backoff_enabled_correctly()
+        {
+            RedeliveryPolicy policy = new RedeliveryPolicy
+                             {BackOffMultiplier = 2, InitialRedeliveryDelay = 5, UseExponentialBackOff
= true};
+
+            // simulate a retry of 10 times
+            Assert.IsTrue(policy.RedeliveryDelay(0) == 5, "redelivery delay not 5 is " +
policy.RedeliveryDelay(0));
+            Assert.IsTrue(policy.RedeliveryDelay(1) == 10, "redelivery delay not 10 is "
+ policy.RedeliveryDelay(1));
+            Assert.IsTrue(policy.RedeliveryDelay(2) == 20, "redelivery delay not 20 is "
+ policy.RedeliveryDelay(2));
+            Assert.IsTrue(policy.RedeliveryDelay(3) == 40, "redelivery delay not 40 is "
+ policy.RedeliveryDelay(3));
+            Assert.IsTrue(policy.RedeliveryDelay(4) == 80, "redelivery delay not 80 is "
+ policy.RedeliveryDelay(4));
+            Assert.IsTrue(policy.RedeliveryDelay(5) == 160, "redelivery delay not 160 is
" + policy.RedeliveryDelay(5));
+            Assert.IsTrue(policy.RedeliveryDelay(6) == 320, "redelivery delay not 320 is
" + policy.RedeliveryDelay(6));
+            Assert.IsTrue(policy.RedeliveryDelay(7) == 640, "redelivery delay not 640 is
" + policy.RedeliveryDelay(7));
+            Assert.IsTrue(policy.RedeliveryDelay(8) == 1280, "redelivery delay not 1280 is
" + policy.RedeliveryDelay(8));
+            Assert.IsTrue(policy.RedeliveryDelay(9) == 2560, "redelivery delay not 2560 is
" + policy.RedeliveryDelay(9));
+        }
+
+        [Test]
+        public void Executes_redelivery_policy_with_backoff_of_3_enabled_correctly()
+        {
+            RedeliveryPolicy policy = new RedeliveryPolicy { BackOffMultiplier = 3, InitialRedeliveryDelay
= 3, UseExponentialBackOff = true };
+
+            // simulate a retry of 10 times
+            Assert.IsTrue(policy.RedeliveryDelay(0) == 3, "redelivery delay not 5 is " +
policy.RedeliveryDelay(0));
+            Assert.IsTrue(policy.RedeliveryDelay(1) == 9, "redelivery delay not 10 is " +
policy.RedeliveryDelay(1));
+            Assert.IsTrue(policy.RedeliveryDelay(2) == 27, "redelivery delay not 20 is "
+ policy.RedeliveryDelay(2));
+            Assert.IsTrue(policy.RedeliveryDelay(3) == 81, "redelivery delay not 40 is "
+ policy.RedeliveryDelay(3));
+            Assert.IsTrue(policy.RedeliveryDelay(4) == 243, "redelivery delay not 80 is "
+ policy.RedeliveryDelay(4));
+            Assert.IsTrue(policy.RedeliveryDelay(5) == 729, "redelivery delay not 160 is
" + policy.RedeliveryDelay(5));
+            Assert.IsTrue(policy.RedeliveryDelay(6) == 2187, "redelivery delay not 320 is
" + policy.RedeliveryDelay(6));
+            Assert.IsTrue(policy.RedeliveryDelay(7) == 6561, "redelivery delay not 640 is
" + policy.RedeliveryDelay(7));
+            Assert.IsTrue(policy.RedeliveryDelay(8) == 19683, "redelivery delay not 1280
is " + policy.RedeliveryDelay(8));
+            Assert.IsTrue(policy.RedeliveryDelay(9) == 59049, "redelivery delay not 2560
is " + policy.RedeliveryDelay(9));
+        }
+
+        [Test]
+        public void Executes_redelivery_policy_without_backoff_enabled_correctly()
+        {
+            RedeliveryPolicy policy = new RedeliveryPolicy { InitialRedeliveryDelay = 5 };
+
+            // simulate a retry of 10 times
+            Assert.IsTrue(policy.RedeliveryDelay(0) == 5, "redelivery delay not 5 is " +
policy.RedeliveryDelay(0));
+            Assert.IsTrue(policy.RedeliveryDelay(1) == 5, "redelivery delay not 10 is " +
policy.RedeliveryDelay(1));
+            Assert.IsTrue(policy.RedeliveryDelay(2) == 5, "redelivery delay not 20 is " +
policy.RedeliveryDelay(2));
+            Assert.IsTrue(policy.RedeliveryDelay(3) == 5, "redelivery delay not 40 is " +
policy.RedeliveryDelay(3));
+            Assert.IsTrue(policy.RedeliveryDelay(4) == 5, "redelivery delay not 80 is " +
policy.RedeliveryDelay(4));
+            Assert.IsTrue(policy.RedeliveryDelay(5) == 5, "redelivery delay not 160 is "
+ policy.RedeliveryDelay(5));
+            Assert.IsTrue(policy.RedeliveryDelay(6) == 5, "redelivery delay not 320 is "
+ policy.RedeliveryDelay(6));
+            Assert.IsTrue(policy.RedeliveryDelay(7) == 5, "redelivery delay not 640 is "
+ policy.RedeliveryDelay(7));
+            Assert.IsTrue(policy.RedeliveryDelay(8) == 5, "redelivery delay not 1280 is "
+ policy.RedeliveryDelay(8));
+            Assert.IsTrue(policy.RedeliveryDelay(9) == 5, "redelivery delay not 2560 is "
+ policy.RedeliveryDelay(9));
+        }
+
+        [Test]
+        public void Should_get_collision_percent_correctly()
+        {
+            RedeliveryPolicy policy = new RedeliveryPolicy();
+
+            policy.CollisionAvoidancePercent = 45;
+
+            Assert.IsTrue(policy.CollisionAvoidancePercent == 45);            
+        }
+
+        [Test]
+        public void Executes_redelivery_policy_with_collision_enabled_correctly()
+        {
+            RedeliveryPolicy policy = new RedeliveryPolicy { BackOffMultiplier = 2, InitialRedeliveryDelay
= 5, UseExponentialBackOff = true, UseCollisionAvoidance = true, CollisionAvoidancePercent
= 10 };
+
+            // simulate a retry of 10 times
+            var delay = policy.RedeliveryDelay(0);
+            Assert.IsTrue(delay >= 4.5 && delay <= 5.5, "not delay >= 4.5
&& delay <= 5.5 is " + policy.RedeliveryDelay(0));
+            delay = policy.RedeliveryDelay(1);
+            Assert.IsTrue(delay >= 9 && delay <= 11, "not delay >= 9 &&
delay <= 11 is " + policy.RedeliveryDelay(1));
+            delay = policy.RedeliveryDelay(2);
+            Assert.IsTrue(delay >= 18 && delay <= 22, "not delay >= 18 &&
delay <= 22 is " + policy.RedeliveryDelay(2));
+            delay = policy.RedeliveryDelay(3);
+            Assert.IsTrue(delay >= 36 && delay <= 44, "not delay >= 36 &&
delay <= 44 is " + policy.RedeliveryDelay(3));
+            delay = policy.RedeliveryDelay(4);
+            Assert.IsTrue(delay >= 72 && delay <= 88, "not delay >= 72 &&
delay <= 88 is " + policy.RedeliveryDelay(4));
+            delay = policy.RedeliveryDelay(5);
+            Assert.IsTrue(delay >= 144 && delay <= 176, "not delay >= 144
&& delay <= 176 is " + policy.RedeliveryDelay(5));
+            delay = policy.RedeliveryDelay(6);
+            Assert.IsTrue(delay >= 288 && delay <= 352, "not delay >= 288
&& delay <= 352 is " + policy.RedeliveryDelay(6));
+            delay = policy.RedeliveryDelay(7);
+            Assert.IsTrue(delay >= 576 && delay <= 704, "not delay >= 576
&& delay <= 704 is " + policy.RedeliveryDelay(7));
+            delay = policy.RedeliveryDelay(8);
+            Assert.IsTrue(delay >= 1152 && delay <= 1408, "not delay >=
1152 && delay <= 1408 is " + policy.RedeliveryDelay(8));
+            delay = policy.RedeliveryDelay(9);
+            Assert.IsTrue(delay >= 2304 && delay <= 2816, "not delay >=
2304 && delay <= 2816 is " + policy.RedeliveryDelay(9));
+        }
+    }
+}

Propchange: activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/RedeliveryPolicyTest.cs
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message