diff --git a/src/Identity/test/Identity.Test/IdentityUIScriptsTest.cs b/src/Identity/test/Identity.Test/IdentityUIScriptsTest.cs index 8976366b43eb4bc1209206df6550cec2c917f323..68f9091c793448f2c5d95963766aaa5d9909c749 100644 --- a/src/Identity/test/Identity.Test/IdentityUIScriptsTest.cs +++ b/src/Identity/test/Identity.Test/IdentityUIScriptsTest.cs @@ -9,7 +9,6 @@ using System.IO; using System.Linq; using System.Net.Http; using System.Security.Cryptography; -using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Testing; using Xunit; @@ -26,7 +25,7 @@ namespace Microsoft.AspNetCore.Identity.Test public IdentityUIScriptsTest(ITestOutputHelper output) { _output = output; - _httpClient = new HttpClient(new RetryHandler(new HttpClientHandler() { })); + _httpClient = new HttpClient(new RetryHandler(new HttpClientHandler() { }, output, TimeSpan.FromSeconds(1), 5)); } public static IEnumerable<object[]> ScriptWithIntegrityData @@ -188,25 +187,5 @@ namespace Microsoft.AspNetCore.Identity.Test throw new InvalidOperationException($"Solution root could not be located using application root {applicationPath}."); } - - class RetryHandler : DelegatingHandler - { - public RetryHandler(HttpMessageHandler innerHandler) : base(innerHandler) { } - - protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) - { - HttpResponseMessage result = null; - for (var i = 0; i < 10; i++) - { - result = await base.SendAsync(request, cancellationToken); - if (result.IsSuccessStatusCode) - { - return result; - } - await Task.Delay(1000); - } - return result; - } - } } } diff --git a/src/Identity/test/Identity.Test/RetryHandler.cs b/src/Identity/test/Identity.Test/RetryHandler.cs new file mode 100644 index 0000000000000000000000000000000000000000..f17446d0d3b2e0d1aca24cb3497dc87c8989f8fe --- /dev/null +++ b/src/Identity/test/Identity.Test/RetryHandler.cs @@ -0,0 +1,65 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; +using Xunit.Abstractions; + +namespace Microsoft.AspNetCore.Identity.Test +{ + internal class RetryHandler : DelegatingHandler + { + private readonly ITestOutputHelper _output; + private readonly int _maxRetries; + private TimeSpan _waitIntervalBeforeRetry; + + public RetryHandler( + HttpClientHandler httpClientHandler, + ITestOutputHelper output, + TimeSpan initialWaitTime, + int maxAttempts) : base(httpClientHandler) + { + _waitIntervalBeforeRetry = initialWaitTime; + _output = output; + _maxRetries = maxAttempts; + } + + protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + HttpResponseMessage result = null; + var url = request.RequestUri; + var method = request.Method; + + for (var i = 0; i < _maxRetries; i++) + { + try + { + _output.WriteLine($"Sending request '{method} - {url}' {i+1} attempt."); + result = await base.SendAsync(request, cancellationToken); + if (result.IsSuccessStatusCode) + { + return result; + } + else + { + _output.WriteLine($"Request '{method} - {url}' failed with {result.StatusCode}."); + } + } + catch (Exception e) + { + _output.WriteLine($"Request '{method} - {url}' failed with {e.ToString()}"); + } + finally + { + await Task.Delay(_waitIntervalBeforeRetry, cancellationToken); + _waitIntervalBeforeRetry = _waitIntervalBeforeRetry * 2; + } + } + + // Try one last time to show the actual error. + return await base.SendAsync(request, cancellationToken); + } + } +}