From 3f61d7d8bf4ff4beb7c12de21b46eaac47572818 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20P?= Date: Mon, 18 Mar 2024 12:59:47 +0100 Subject: [PATCH] Use Polly instead of AnyRetry (#3) --- .../SauceLabs.Visual/SauceLabs.Visual.csproj | 2 +- .../SauceLabs.Visual/VisualClient.cs | 36 ++++++++----------- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/visual-dotnet/SauceLabs.Visual/SauceLabs.Visual.csproj b/visual-dotnet/SauceLabs.Visual/SauceLabs.Visual.csproj index a88ee2f2..a8deab3f 100644 --- a/visual-dotnet/SauceLabs.Visual/SauceLabs.Visual.csproj +++ b/visual-dotnet/SauceLabs.Visual/SauceLabs.Visual.csproj @@ -28,7 +28,7 @@ true - + diff --git a/visual-dotnet/SauceLabs.Visual/VisualClient.cs b/visual-dotnet/SauceLabs.Visual/VisualClient.cs index 252acc74..ff603100 100644 --- a/visual-dotnet/SauceLabs.Visual/VisualClient.cs +++ b/visual-dotnet/SauceLabs.Visual/VisualClient.cs @@ -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; @@ -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; /// /// Creates a new instance of VisualClient @@ -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(); } /// @@ -133,26 +144,7 @@ public void Dispose() /// public async Task> 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> FetchVisualResults(string buildId)