Skip to content

Commit

Permalink
Update retry logic and clean up comments
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
Afischbacher committed Sep 22, 2024
1 parent 67a50d0 commit 4f97959
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
27 changes: 22 additions & 5 deletions Nhl.Api.Tests/Helpers/Attributes/TestMethodWithRetryAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Linq;
using System.Linq;
using System.Threading;

namespace Nhl.Api.Tests.Helpers.Attributes;
Expand All @@ -16,19 +16,36 @@ public class TestMethodWithRetryAttribute : TestMethodAttribute
/// <summary>
/// A delay time between each test execution retry attempt
/// </summary>
public int RetryDelayInSeconds { get; set; } = 0;
public int RetryDelayInSeconds { get; set; } = 1;

/// <summary>
/// The backoff coefficient to apply to the retry delay
/// </summary>
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))
{
Expand Down
2 changes: 1 addition & 1 deletion Nhl.Api.Tests/TeamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4f97959

Please sign in to comment.