From 4f97959235792202355430ed308f20f23af9c10e Mon Sep 17 00:00:00 2001 From: Andre Fischbacher Date: Sun, 22 Sep 2024 13:18:19 -0400 Subject: [PATCH] Update retry logic and clean up comments - Added `System.Threading` using directive in `TestMethodWithRetryAttribute.cs`. - Changed default `RetryDelayInSeconds` to 1 second. - Updated `Execute` method to use instance properties `RetryCount` and `RetryDelayInSeconds`. - Removed unnecessary comment in `TeamTests` class. --- .../TestMethodWithRetryAttribute.cs | 27 +++++++++++++++---- Nhl.Api.Tests/TeamTests.cs | 2 +- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Nhl.Api.Tests/Helpers/Attributes/TestMethodWithRetryAttribute.cs b/Nhl.Api.Tests/Helpers/Attributes/TestMethodWithRetryAttribute.cs index 0ac9107a..8cd14d85 100644 --- a/Nhl.Api.Tests/Helpers/Attributes/TestMethodWithRetryAttribute.cs +++ b/Nhl.Api.Tests/Helpers/Attributes/TestMethodWithRetryAttribute.cs @@ -1,4 +1,4 @@ -using System.Linq; +using System.Linq; using System.Threading; namespace Nhl.Api.Tests.Helpers.Attributes; @@ -16,19 +16,36 @@ public class TestMethodWithRetryAttribute : TestMethodAttribute /// /// A delay time between each test execution retry attempt /// - public int RetryDelayInSeconds { get; set; } = 0; + public int RetryDelayInSeconds { get; set; } = 1; + + /// + /// The backoff coefficient to apply to the retry delay + /// + public decimal BackoffCoefficent { get; set; } = 1.0m; public override TestResult[] Execute(ITestMethod testMethod) { - var count = RetryCount; - var backOffDelay = RetryDelayInSeconds; + var count = this.RetryCount; + var backOffDelay = this.RetryDelayInSeconds; + var backOffWithCoefficient = (int)(backOffDelay * this.BackoffCoefficent); + const int oneThousandMilliseconds = 1000; TestResult[] result = null; while (count > 0) { try { - Thread.Sleep(backOffDelay * 1000); + if (this.BackoffCoefficent > 1.0m) + { + backOffWithCoefficient = (int)(backOffDelay * this.BackoffCoefficent); + this.BackoffCoefficent *= this.BackoffCoefficent; + Thread.Sleep(backOffWithCoefficient * oneThousandMilliseconds); + } + else + { + Thread.Sleep(backOffDelay * oneThousandMilliseconds); + } + result = base.Execute(testMethod); if (result.Any(r => r.TestFailureException != null)) { diff --git a/Nhl.Api.Tests/TeamTests.cs b/Nhl.Api.Tests/TeamTests.cs index ed5224ff..669e253d 100644 --- a/Nhl.Api.Tests/TeamTests.cs +++ b/Nhl.Api.Tests/TeamTests.cs @@ -423,7 +423,7 @@ public async Task GetTeamSeasonScheduleByYearAndMonthAsync_Get_Valid_Information [TestMethodWithRetry(RetryCount = 5)] public async Task GetTeamSeasonScheduleByDateTimeAsync_Get_Valid_Information_With_Id(int teamId, string date) { - // ArrangeF + // Arrange await using var nhlApi = new NhlApi(); // Act