public async Task ExponentialRetryAbortsRetriesAfterMaxRetryCount() { const int delayInSeconds = 3; TimeSpan interval = TimeSpan.FromSeconds(delayInSeconds); const int maxRetries = 5; ExponentialRetry exponentialRetry = new ExponentialRetry(interval, maxRetries); TimeoutException timeoutException = new TimeoutException(); OperationContext context = new OperationContext(); RetryDecision retryDecision = null; int requestCount = 0; while (retryDecision == null || retryDecision.ShouldRetry) { context.RequestResults.Add(new RequestResult(new RequestInformation(), timeoutException)); retryDecision = await exponentialRetry.ShouldRetryAsync(timeoutException, context); Assert.NotNull(retryDecision); if (retryDecision.ShouldRetry) { TimeSpan expectedDelay = TimeSpan.FromSeconds(Math.Pow(2, requestCount) * delayInSeconds); Assert.Equal(expectedDelay, retryDecision.RetryDelay); } ++requestCount; } Assert.Equal(maxRetries, requestCount - 1); //request count is retry count + 1 }
public async Task ExponentialRetryRetriesOnNonBatchException() { TimeSpan interval = TimeSpan.FromSeconds(5); const int maxRetries = 10; ExponentialRetry exponentialRetry = new ExponentialRetry(interval, maxRetries); TimeoutException timeoutException = new TimeoutException(); OperationContext context = new OperationContext(); context.RequestResults.Add(new RequestResult(new RequestInformation(), timeoutException)); RetryDecision retryDecision = await exponentialRetry.ShouldRetryAsync(timeoutException, context); Assert.Equal(interval, retryDecision.RetryDelay); Assert.Equal(true, retryDecision.ShouldRetry); }
public async Task ExponentialRetryRetriesOnBatchException() { TimeSpan interval = TimeSpan.FromSeconds(5); const int maxRetries = 10; ExponentialRetry exponentialRetry = new ExponentialRetry(interval, maxRetries); RequestInformation reqInfo = new RequestInformation() { HttpStatusCode = HttpStatusCode.InternalServerError }; BatchException batchException = new BatchException(reqInfo, "Message", null); OperationContext context = new OperationContext(); context.RequestResults.Add(new RequestResult(new RequestInformation(), batchException)); RetryDecision retryDecision = await exponentialRetry.ShouldRetryAsync(batchException, context); Assert.Equal(interval, retryDecision.RetryDelay); Assert.Equal(true, retryDecision.ShouldRetry); }
public async Task ExponentialRetryHonorsRetryAfter() { TimeSpan interval = TimeSpan.FromSeconds(60); TimeSpan retryAfter = TimeSpan.FromSeconds(10); const int maxRetries = 10; ExponentialRetry exponentialRetry = new ExponentialRetry(interval, maxRetries); RequestInformation reqInfo = new RequestInformation() { HttpStatusCode = (HttpStatusCode)429, RetryAfter = retryAfter }; BatchException batchException = new BatchException(reqInfo, "Message", null); OperationContext context = new OperationContext(); context.RequestResults.Add(new RequestResult(new RequestInformation(), batchException)); RetryDecision retryDecision = await exponentialRetry.ShouldRetryAsync(batchException, context); Assert.Equal(retryAfter, retryDecision.RetryDelay); Assert.True(retryDecision.ShouldRetry); }