Skip to content
This repository was archived by the owner on Aug 18, 2021. It is now read-only.

Commit 6767f8f

Browse files
author
Ying Feifan
committed
fix(PollyTests): bug fix for the test IsBackOffFalse and small correction.
1 parent 192329a commit 6767f8f

File tree

3 files changed

+91
-83
lines changed

3 files changed

+91
-83
lines changed

src/Liquid.Runtime/Polly/Polly.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,18 @@ public override async Task<HttpResponseMessage> ResilientRequest(string url, Htt
1717
PollyConfiguration pollyConfig = (PollyConfiguration)pollyconfiguration;
1818
AsyncRetryPolicy<HttpResponseMessage> retryPolicy = null;
1919

20-
// TODO: Please let's remove this ternary
2120
if (pollyConfig.IsBackOff == true)
2221
{
23-
retryPolicy = Policy.HandleResult<HttpResponseMessage>(r => r.StatusCode.Equals(HttpStatusCode.InternalServerError))
24-
.Or<WebException>().Or<HttpRequestException>()
22+
retryPolicy = Policy.HandleResult<HttpResponseMessage>(r => r.StatusCode.Equals(HttpStatusCode.InternalServerError)).Or<WebException>().Or<HttpRequestException>()
2523
.WaitAndRetryAsync(pollyConfig.Retry, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (result, timeSpan, retryCount, context) =>
2624
{
2725
CallbackError<HttpResponseMessage>(result, timeSpan, retryCount);
2826
});
2927
}
3028
else
3129
{
32-
Policy.HandleResult<HttpResponseMessage>(r => r.StatusCode.Equals(HttpStatusCode.InternalServerError)).Or<WebException>().Or<HttpRequestException>()
33-
.WaitAndRetryAsync(pollyConfig.Retry, retryAttempt => TimeSpan.FromSeconds(pollyConfig.Wait), (result, timeSpan, retryCount, context) =>
30+
retryPolicy = Policy.HandleResult<HttpResponseMessage>(r => r.StatusCode.Equals(HttpStatusCode.InternalServerError)).Or<WebException>().Or<HttpRequestException>()
31+
.WaitAndRetryAsync(pollyConfig.Retry, retryAttempt => TimeSpan.FromSeconds(pollyConfig.Wait), (result, timeSpan, retryCount, context) =>
3432
{
3533
CallbackError<HttpResponseMessage>(result, timeSpan, retryCount);
3634
});

test/Liquid.Runtime.Tests/PollyTest.cs

Lines changed: 0 additions & 78 deletions
This file was deleted.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System.Net.Http;
2+
using System.Threading.Tasks;
3+
using Liquid.Runtime.Polly;
4+
using Xunit;
5+
6+
7+
8+
9+
10+
namespace Liquid.Runtime.Tests
11+
{
12+
public class PollyTests
13+
{
14+
private readonly LightPolly _sut;
15+
private HttpClient _httpClient;
16+
private WorkBenchServiceHttp _http;
17+
18+
19+
20+
21+
public PollyTests()
22+
{
23+
_sut = new Polly.Polly();
24+
_httpClient = new HttpClient();
25+
_http = WorkBenchServiceHttp.GET;
26+
}
27+
28+
29+
30+
31+
[Fact]
32+
public async Task ResilientRequestWhenInvalidUrlAfterRetriedConfiguredAttempsThrowHttpRequest()
33+
{
34+
//Arrange
35+
PollyConfiguration _pollyConfiguration = new PollyConfiguration { IsBackOff = true, Retry = 3, Wait = 5 };
36+
var url = "https://www.aipdhapsidh.com";
37+
HttpResponseMessage result;
38+
39+
40+
41+
//Action/Assert
42+
await Assert.ThrowsAnyAsync<HttpRequestException>(async () => result = await _sut.ResilientRequest(url, _httpClient, _http, _pollyConfiguration).ConfigureAwait(true));
43+
}
44+
45+
46+
47+
48+
[Fact]
49+
public async Task ResilientRequestShouldUseMathPowAsRetryAttemptWhenIsBackOffTrue()
50+
{
51+
52+
53+
54+
//Arrange
55+
PollyConfiguration _pollyConfiguration = new PollyConfiguration { IsBackOff = true, Retry = 3, Wait = 5 };
56+
var url = "https://www.google.com";
57+
58+
59+
60+
//Action
61+
using (HttpResponseMessage result = await _sut.ResilientRequest(url, _httpClient, _http, _pollyConfiguration).ConfigureAwait(true))
62+
{
63+
//Assert
64+
Assert.True(result.IsSuccessStatusCode);
65+
}
66+
}
67+
68+
69+
70+
71+
[Fact]
72+
public async Task ResilientRequestShouldUseDefaultPollyConfigAsRetryAttemptWhenIsBackOffFalse()
73+
{
74+
75+
76+
77+
//Arrange
78+
PollyConfiguration _pollyConfiguration = new PollyConfiguration { IsBackOff = false, Retry = 3, Wait = 5 };
79+
var url = "https://www.google.com";
80+
//Action
81+
using (HttpResponseMessage result = await _sut.ResilientRequest(url, _httpClient, _http, _pollyConfiguration).ConfigureAwait(false))
82+
{
83+
//Assert
84+
Assert.True(result.IsSuccessStatusCode);
85+
}
86+
}
87+
}
88+
}

0 commit comments

Comments
 (0)