diff --git a/src/DNS-BLM.Infrastructure/Services/RetryService.cs b/src/DNS-BLM.Infrastructure/Services/RetryService.cs index fedc3c6..70ad38d 100644 --- a/src/DNS-BLM.Infrastructure/Services/RetryService.cs +++ b/src/DNS-BLM.Infrastructure/Services/RetryService.cs @@ -9,20 +9,20 @@ public class RetryService(ILogger logger) /// /// The return type of the function. /// The asynchronous function to execute. - /// The maximum number of attempts to make. Must be 1 or higher. Defaults to 3 + /// The maximum number of attempts to make. Must be 1 or higher. Defaults to 3 /// A to observe while waiting for the task to complete. /// The result of the function if successful, or the result of the last attempt if all retries fail. /// - /// This method retries the provided function up to times. + /// This method retries the provided function up to times. /// It swallows exceptions on intermediate attempts and applies an exponential backoff delay before retrying. /// - public async Task Retry(Func?>> func, int maxAttempts = 3, CancellationToken cancellationToken = default) + public async Task Retry(Func?>> func, int maxRetrys = 3, CancellationToken cancellationToken = default) { - if (maxAttempts <= 0) - throw new ArgumentOutOfRangeException(nameof(maxAttempts)); + if (maxRetrys <= 0) + throw new ArgumentOutOfRangeException(nameof(maxRetrys)); RetryResult? result = new() { }; - for (int attempt = 1; attempt <= maxAttempts; attempt++) + for (int attempt = 0; attempt <= maxRetrys; attempt++) { try { @@ -33,12 +33,12 @@ public class RetryService(ILogger logger) return result.Result; } } - catch when (attempt < maxAttempts) + catch when (attempt < maxRetrys) { // Swallow exception and retry } - if (attempt < maxAttempts) + if (attempt < maxRetrys) { var delay = CalculateBackoffTimeSeconds(attempt); logger.LogDebug("Retry not successful - Delay for {Delay} seconds", delay); @@ -57,7 +57,7 @@ public class RetryService(ILogger logger) /// private int CalculateBackoffTimeSeconds(int numberOfAttempts) { - numberOfAttempts += 1; // Increase attempt to skip small delays + numberOfAttempts += 2; // Increase attempt to skip small delays int totalSeconds = 0; for (int attempt = 1; attempt <= numberOfAttempts; attempt++) diff --git a/test/Tests/Test/RetryServiceTest.cs b/test/Tests/Test/RetryServiceTest.cs index ada9652..40659e9 100644 --- a/test/Tests/Test/RetryServiceTest.cs +++ b/test/Tests/Test/RetryServiceTest.cs @@ -27,7 +27,7 @@ public async Task Retry_ExecutesFunctionSuccessfullyOnFirstAttempt() }; // Act - var result = await _retryService.Retry(func, 3); + var result = await _retryService.Retry(func, 1); // Assert Assert.Equal(expectedResult, result); @@ -52,7 +52,7 @@ public async Task Retry_ExecutesFunctionSuccessfullyAfterRetries() }; // Act - var result = await _retryService.Retry(func, 3); + var result = await _retryService.Retry(func, 2); // Assert Assert.Equal(expectedResult, result); @@ -72,9 +72,9 @@ public async Task Retry_ThrowsExceptionOnLastAttemptIfAllFailViaException() }; // Act & Assert - var exception = await Assert.ThrowsAsync(() => _retryService.Retry(func, 3)); + var exception = await Assert.ThrowsAsync(() => _retryService.Retry(func, 1)); Assert.Equal(expectedExceptionMessage, exception.Message); - Assert.Equal(3, callCount); + Assert.Equal(2, callCount); } [Fact] @@ -89,11 +89,11 @@ public async Task Retry_ReturnsDefaultOnLastAttemptIfAllFailViaIsSuccessFalse() }; // Act - var result = await _retryService.Retry(func, 3); + var result = await _retryService.Retry(func, 1); // Assert Assert.Null(result); // Default for string is null - Assert.Equal(3, callCount); // Called maxAttempts times based on the retry logic + Assert.Equal(2, callCount); // Called maxAttempts times based on the retry logic } [Fact] @@ -115,7 +115,7 @@ public async Task Retry_NoDelayOnLastAttemptOrSuccess() }; var startTime = DateTime.UtcNow; - var result = await _retryService.Retry(func , 2); // 1 unsuccessful, 1 successful attempt + var result = await _retryService.Retry(func , 1); // 1 unsuccessful, 1 successful attempt // Assert Assert.Equal(expectedResult, result); @@ -138,7 +138,7 @@ public async Task Retry_WhenFuncReturnsIsSuccessTrueOnFirstTry_NoFurtherCalls() return Task.FromResult?>(new RetryResult { Result = "Result", IsSuccess = true }); }; // Act - var result = await _retryService.Retry(func, 5); + var result = await _retryService.Retry(func, 1); // Assert Assert.NotNull(result); Assert.Equal("Result", result); @@ -159,7 +159,7 @@ public async Task Retry_WhenFuncReturnsIsSuccessFalseOnFirstTry_RetriesUntilIsSu }; // Act - var result = await _retryService.Retry(func, 3); + var result = await _retryService.Retry(func, 1); // Assert Assert.Equal("Final Result", result); @@ -178,11 +178,11 @@ public async Task Retry_WhenFuncAlwaysReturnsIsSuccessFalse_ReturnsDefaultOnMaxA }; // Act - var result = await _retryService.Retry(func, 3); + var result = await _retryService.Retry(func, 1); // Assert Assert.Null(result); // Default for string - Assert.Equal(3, callCount); // Called maxAttempts times, always returning IsSuccess=false. + Assert.Equal(2, callCount); // Called maxAttempts times, always returning IsSuccess=false. } [Fact] @@ -202,7 +202,7 @@ public async Task Retry_WhenFuncReturnsNullRetryResult_RetriesUntilNonNullReturn }; // Act - var result = await _retryService.Retry(func, 3); + var result = await _retryService.Retry(func, 2); // Assert Assert.Equal("Actual Result", result);