Beispiel #1
0
 public RetryPolicyContext(ClientException exception, string httpStatusCode, int retryAttemptTimes,
                           string product, string version, string apiName, RetryCondition retryable)
 {
     Exception         = exception;
     HttpStatusCode    = httpStatusCode;
     RetryAttemptTimes = retryAttemptTimes;
     Product           = product;
     Version           = version;
     ApiName           = apiName;
     Retryable         = retryable;
 }
Beispiel #2
0
        public void PropertiesAreSetByConstrutor()
        {
            var condition = new RetryCondition(true, TimeSpan.FromSeconds(1));

            Assert.Equal(true, condition.RetryAllowed);
            Assert.Equal(TimeSpan.FromSeconds(1), condition.DelayBeforeRetry);

            condition = new RetryCondition(false, TimeSpan.Zero);
            Assert.Equal(false, condition.RetryAllowed);
            Assert.Equal(TimeSpan.Zero, condition.DelayBeforeRetry);
        }
Beispiel #3
0
        /// <summary>
        /// Returns the corresponding ShouldRetry delegate.
        /// </summary>
        /// <returns>The ShouldRetry delegate.</returns>
        public override ShouldRetry GetShouldRetry()
        {
            var retryCondition = new RetryCondition(false, TimeSpan.Zero);

            return(delegate(int currentRetryCount, Exception lastException)
            {
                if (currentRetryCount < this.retryCount)
                {
                    var retryInterval = TimeSpan.FromMilliseconds(this.initialInterval.TotalMilliseconds + (this.increment.TotalMilliseconds * currentRetryCount));

                    retryCondition = new RetryCondition(true, retryInterval);
                }

                return retryCondition;
            });
        }
Beispiel #4
0
        public void then_default_values_are_used()
        {
            Assert.AreEqual("name", retryStrategy.Name);

            RetryCondition c1 = shouldRetry(0, null);

            Assert.IsTrue(c1.RetryAllowed);
            Assert.AreEqual(TimeSpan.FromSeconds(5), c1.DelayBeforeRetry);

            RetryCondition c2 = shouldRetry(4, null);

            Assert.IsTrue(c2.RetryAllowed);
            Assert.AreEqual(TimeSpan.FromSeconds(13), c2.DelayBeforeRetry);

            RetryCondition c3 = shouldRetry(5, null);

            Assert.IsFalse(c3.RetryAllowed);
            Assert.AreEqual(TimeSpan.Zero, c3.DelayBeforeRetry);
        }
Beispiel #5
0
        /// <summary>
        /// Returns the corresponding ShouldRetry delegate.
        /// </summary>
        /// <returns>The ShouldRetry delegate.</returns>
        public override ShouldRetry GetShouldRetry()
        {
            return(delegate(int currentRetryCount, Exception lastException)
            {
                var retryCondition = new RetryCondition(true, TimeSpan.Zero);
                if (currentRetryCount < this.retryCount)
                {
                    var random = new Random();

                    var delta = (int)((Math.Pow(2.0, currentRetryCount) - 1.0) * random.Next((int)(this.deltaBackoff.TotalMilliseconds * 0.8), (int)(this.deltaBackoff.TotalMilliseconds * 1.2)));
                    var interval = (int)Math.Min(checked (this.minBackoff.TotalMilliseconds + delta), this.maxBackoff.TotalMilliseconds);

                    var retryInterval = TimeSpan.FromMilliseconds(interval);

                    retryCondition = new RetryCondition(true, retryInterval);
                }

                return retryCondition;
            });

            #endregion
        }
        /// <summary>
        /// Returns the corresponding ShouldRetry delegate.
        /// </summary>
        /// <returns>The ShouldRetry delegate.</returns>
        public override ShouldRetry GetShouldRetry()
        {
            var retryCondition = new RetryCondition(false, TimeSpan.Zero);

            if (this.retryCount == 0)
            {
                return(delegate(int currentRetryCount, Exception lastException)
                {
                    return retryCondition;
                });
            }

            return(delegate(int currentRetryCount, Exception lastException)
            {
                if (currentRetryCount < this.retryCount)
                {
                    retryCondition = new RetryCondition(true, this.retryInterval);
                }

                return retryCondition;
            });
        }
Beispiel #7
0
        public void then_default_values_are_used()
        {
            Assert.IsNull(retryStrategy.Name);

            RetryCondition c1 = shouldRetry(0, null);

            Assert.IsTrue(c1.RetryAllowed);
            Assert.AreEqual(TimeSpan.FromSeconds(1), c1.DelayBeforeRetry);

            RetryCondition c2 = shouldRetry(5, null);

            Assert.IsTrue(c2.RetryAllowed);
            Assert.IsTrue(c2.DelayBeforeRetry >= TimeSpan.FromSeconds(1) && c2.DelayBeforeRetry <= TimeSpan.FromSeconds(30));

            RetryCondition c3 = shouldRetry(9, null);

            Assert.IsTrue(c3.RetryAllowed);
            Assert.AreEqual(TimeSpan.FromSeconds(30), c3.DelayBeforeRetry);

            RetryCondition c4 = shouldRetry(10, null);

            Assert.IsFalse(c4.RetryAllowed);
            Assert.AreEqual(TimeSpan.Zero, c4.DelayBeforeRetry);
        }