Skip to content
代码片段 群组 项目
未验证 提交 3368ea6f 编辑于 作者: Javier Calvarro Nelson's avatar Javier Calvarro Nelson 提交者: GitHub
浏览文件

[Identity][Infrastructure] Improve test reliability (#13646)

* Catches HttpClient exceptions and retries on IdentityUI_ScriptTags_SubresourceIntegrityCheck and IdentityUI_ScriptTags_FallbackSourceContent_Matches_CDNContent
上级 ddf987eb
No related branches found
No related tags found
无相关合并请求
......@@ -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;
}
}
}
}
// 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);
}
}
}
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册