Skip to content

Commit

Permalink
Use Polly instead of AnyRetry (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
FriggaHel authored Mar 18, 2024
1 parent 9dd8488 commit 3f61d7d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
2 changes: 1 addition & 1 deletion visual-dotnet/SauceLabs.Visual/SauceLabs.Visual.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AnyRetry" Version="1.0.31" />
<PackageReference Include="Polly" Version="8.3.1" />
<PackageReference Include="GraphQL.Client" Version="6.0.2" />
<PackageReference Include="GraphQL.Client.Abstractions" Version="6.0.2" />
<PackageReference Include="GraphQL.Client.Serializer.Newtonsoft" Version="6.0.2" />
Expand Down
36 changes: 14 additions & 22 deletions visual-dotnet/SauceLabs.Visual/VisualClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AnyRetry;
using AnyRetry.Math;
using OpenQA.Selenium;
using Polly;
using Polly.Retry;
using SauceLabs.Visual.GraphQL;
using SauceLabs.Visual.Models;
using SauceLabs.Visual.Utils;
Expand All @@ -24,6 +24,7 @@ public class VisualClient : IDisposable
public VisualBuild Build { get; }
private readonly bool _externalBuild;
public bool CaptureDom { get; set; } = false;
private ResiliencePipeline _retryPipeline;

/// <summary>
/// Creates a new instance of <c>VisualClient</c>
Expand Down Expand Up @@ -56,6 +57,16 @@ public VisualClient(WebDriver wd, Region region, string username, string accessK
var createBuildResponse = CreateBuild(buildOptions).Result;
Build = new VisualBuild(createBuildResponse.Id, createBuildResponse.Url);
_externalBuild = false;

_retryPipeline = new ResiliencePipelineBuilder()
.AddRetry(new RetryStrategyOptions()
{
Name = "VisualRetryPolicy",
Delay = TimeSpan.FromSeconds(1),
MaxRetryAttempts = 10
})
.AddTimeout(TimeSpan.FromSeconds(15))
.Build();
}

/// <summary>
Expand Down Expand Up @@ -133,26 +144,7 @@ public void Dispose()
/// <exception cref="VisualClientException"></exception>
public async Task<Dictionary<DiffStatus, int>> VisualResults()
{
var policyOptions = new RetryPolicyOptions
{
EasingFunction = EasingFunction.ExponentialEaseOut,
MaxRetryInterval = TimeSpan.FromSeconds(5),
MaxRetrySteps = 10
};
var result = await Retry.Do(async () => await FetchVisualResults(Build.Id),
retryInterval: TimeSpan.FromMilliseconds(100),
retryLimit: 10,
retryPolicy: RetryPolicy.ExponentialBackoff,
retryPolicyOptions: policyOptions,
onFailure: null,
mustReturnTrueBeforeFail: null,
exceptionTypes: typeof(VisualClientException)
);
if (result == null)
{
throw new VisualClientException("diff results were not available in time");
}
return result;
return await _retryPipeline.ExecuteAsync(async token => await FetchVisualResults(Build.Id));
}

private async Task<Dictionary<DiffStatus, int>> FetchVisualResults(string buildId)
Expand Down

0 comments on commit 3f61d7d

Please sign in to comment.