Skip to content

Commit

Permalink
Use Polly
Browse files Browse the repository at this point in the history
  • Loading branch information
FriggaHel committed Mar 18, 2024
1 parent d8792f6 commit 83d755f
Show file tree
Hide file tree
Showing 2 changed files with 14 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 @@ -30,7 +30,7 @@
</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
35 changes: 13 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 Down Expand Up @@ -56,6 +56,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()

Check failure on line 60 in visual-dotnet/SauceLabs.Visual/VisualClient.cs

View workflow job for this annotation

GitHub Actions / build

The name '_retryPipeline' does not exist in the current context

Check failure on line 60 in visual-dotnet/SauceLabs.Visual/VisualClient.cs

View workflow job for this annotation

GitHub Actions / build

The name '_retryPipeline' does not exist in the current context

Check failure on line 60 in visual-dotnet/SauceLabs.Visual/VisualClient.cs

View workflow job for this annotation

GitHub Actions / build

The name '_retryPipeline' does not exist in the current context

Check failure on line 60 in visual-dotnet/SauceLabs.Visual/VisualClient.cs

View workflow job for this annotation

GitHub Actions / build

The name '_retryPipeline' does not exist in the current context
.AddRetry(new RetryStrategyOptions()
{
Name = "VisualRetryPolicy",
Delay = TimeSpan.FromSeconds(1),
MaxRetryAttempts = 10
})
.AddTimeout(TimeSpan.FromSeconds(15))
.Build();
}

/// <summary>
Expand Down Expand Up @@ -133,26 +143,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));

Check failure on line 146 in visual-dotnet/SauceLabs.Visual/VisualClient.cs

View workflow job for this annotation

GitHub Actions / build

The name '_retryPipeline' does not exist in the current context

Check failure on line 146 in visual-dotnet/SauceLabs.Visual/VisualClient.cs

View workflow job for this annotation

GitHub Actions / build

The name '_retryPipeline' does not exist in the current context

Check failure on line 146 in visual-dotnet/SauceLabs.Visual/VisualClient.cs

View workflow job for this annotation

GitHub Actions / build

The name '_retryPipeline' does not exist in the current context

Check failure on line 146 in visual-dotnet/SauceLabs.Visual/VisualClient.cs

View workflow job for this annotation

GitHub Actions / build

The name '_retryPipeline' does not exist in the current context
}

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

0 comments on commit 83d755f

Please sign in to comment.