< Summary

Class:Microsoft.Azure.Batch.Common.ExponentialRetry
Assembly:Microsoft.Azure.Batch
File(s):C:\Git\azure-sdk-for-net\sdk\batch\Microsoft.Azure.Batch\src\Common\ExponentialRetry.cs
Covered lines:23
Uncovered lines:2
Coverable lines:25
Total lines:86
Line coverage:92% (23 of 25)
Covered branches:19
Total branches:24
Branch coverage:79.1% (19 of 24)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_DeltaBackoff()-100%100%
get_MaximumRetries()-100%100%
get_MaxBackoff()-100%100%
.ctor(...)-87.5%75%
ShouldRetryAsync(...)-92.86%80%

File(s)

C:\Git\azure-sdk-for-net\sdk\batch\Microsoft.Azure.Batch\src\Common\ExponentialRetry.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License. See License.txt in the project root for license information.
 3
 4namespace Microsoft.Azure.Batch.Common
 5{
 6    using System;
 7    using System.Threading.Tasks;
 8
 9    /// <summary>
 10    /// Represents a retry policy that performs a specified number of retries, using an exponential backoff scheme to de
 11    /// </summary>
 12    public class ExponentialRetry : IRetryPolicy
 13    {
 14        /// <summary>
 15        /// Gets the backoff interval between retries, where the resulting backoff is 2^n * deltaBackoff (where n is the
 16        /// </summary>
 67717        public TimeSpan DeltaBackoff { get; private set; }
 18
 19        /// <summary>
 20        /// Gets the maximum number of retry attempts.
 21        /// </summary>
 68222        public int MaximumRetries { get; private set; }
 23
 24        /// <summary>
 25        /// The maximum duration to wait between retries.
 26        /// </summary>
 68327        public TimeSpan? MaxBackoff { get; private set; }
 28
 29        /// <summary>
 30        /// Initializes a new instance of the <see cref="ExponentialRetry"/> class using the specified delta and maximum
 31        /// </summary>
 32        /// <param name="deltaBackoff">The backoff interval between retries, where the resulting backoff is 2^n * deltaB
 33        /// <param name="maxRetries">The maximum number of retry attempts.</param>
 34        /// <param name="maxBackoff">The maximum duration to wait between retries.</param>
 66535        public ExponentialRetry(TimeSpan deltaBackoff, int maxRetries, TimeSpan? maxBackoff = null)
 36        {
 66537            RetryPolicyCommon.ValidateArguments(deltaBackoff, maxRetries);
 38
 66339            if (maxBackoff < TimeSpan.Zero)
 40            {
 041                throw new ArgumentOutOfRangeException(nameof(maxBackoff));
 42            }
 43
 66344            this.DeltaBackoff = deltaBackoff;
 66345            this.MaximumRetries = maxRetries;
 66346            this.MaxBackoff = maxBackoff;
 66347        }
 48
 49        /// <summary>
 50        /// Determines if the operation should be retried and how long to wait until the next retry.
 51        /// </summary>
 52        /// <param name="exception">An <see cref="Exception"/> object that represents the last exception encountered.</p
 53        /// <param name="operationContext">An <see cref="OperationContext"/> object for tracking the current operation.<
 54        /// <returns>A <see cref="RetryDecision"/> object which specifies if a retry should be performed.</returns>
 55        public Task<RetryDecision> ShouldRetryAsync(Exception exception, OperationContext operationContext)
 56        {
 2657            if (operationContext == null)
 58            {
 059                throw new ArgumentNullException("operationContext");
 60            }
 61
 2662            bool shouldRetry = RetryPolicyCommon.ShouldRetry(exception, operationContext, this.MaximumRetries);
 2663            TimeSpan? retryAfter = (exception as BatchException)?.RequestInformation?.RetryAfter;
 2664            int currentRetryCount = operationContext.RequestResults.Count - 1;
 65
 66            RetryDecision decision;
 2667            if (shouldRetry)
 68            {
 2169                double deltaBackoffMilliseconds = this.DeltaBackoff.TotalMilliseconds;
 2170                double millisecondsBackoff = Math.Pow(2, currentRetryCount) * deltaBackoffMilliseconds;
 71
 2172                var delay = TimeSpan.FromMilliseconds(millisecondsBackoff);
 2173                delay = retryAfter.HasValue && delay > retryAfter ? retryAfter.Value : delay;
 2174                delay = this.MaxBackoff.HasValue && delay > this.MaxBackoff ? this.MaxBackoff.Value : delay;
 75
 2176                decision = RetryDecision.RetryWithDelay(delay);
 77            }
 78            else
 79            {
 580                decision = RetryDecision.NoRetry;
 81            }
 82
 2683            return Task.FromResult(decision);
 84        }
 85    }
 86}