-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added retry logic to HttpRemoteQueryRunner
- Loading branch information
1 parent
a9216c2
commit 433a33d
Showing
2 changed files
with
104 additions
and
3 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
...S.Extensions.FastEndpoints.UnitTests/HttpRemoteQueryRunners/HttpRemoteQueryRunnerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System.Net; | ||
using System.Text.Json; | ||
using FluentAssertions; | ||
using Moq; | ||
using RestSharp; | ||
using Wemogy.CQRS.Common.ValueObjects; | ||
using Wemogy.CQRS.Extensions.FastEndpoints.Common; | ||
using Wemogy.CQRS.Extensions.FastEndpoints.RemoteQueryRunners; | ||
using Wemogy.CQRS.Extensions.FastEndpoints.UnitTests.TestApplication.Queries.RequestTestContext; | ||
using Wemogy.CQRS.Extensions.FastEndpoints.UnitTests.TestApplication.ValueObjects; | ||
|
||
namespace Wemogy.CQRS.Extensions.FastEndpoints.UnitTests.HttpRemoteQueryRunners; | ||
|
||
public class HttpRemoteQueryRunnerTests | ||
{ | ||
[Fact] | ||
public async Task QueryAsync_ShouldRetryRequestAndReturnResult() | ||
{ | ||
// Arrange | ||
var query = new RequestTestContextQuery(); | ||
var testContext = new TestContext() | ||
{ | ||
UserId = Guid.NewGuid().ToString() | ||
}; | ||
var resultContent = JsonSerializer.Serialize(testContext, JsonOptions.JsonSerializerOptions); | ||
var responses = new Queue<RestResponse>(); | ||
responses.Enqueue(new RestResponse() | ||
{ | ||
StatusCode = HttpStatusCode.ServiceUnavailable | ||
}); | ||
responses.Enqueue(new RestResponse() | ||
{ | ||
StatusCode = HttpStatusCode.OK, | ||
Content = resultContent, | ||
|
||
// Set IsSuccessful to true | ||
IsSuccessStatusCode = true, | ||
ResponseStatus = ResponseStatus.Completed | ||
}); | ||
|
||
var restClientMock = new Mock<IRestClient>(); | ||
restClientMock | ||
.Setup( | ||
m => m.ExecuteAsync( | ||
It.IsAny<RestRequest>(), | ||
It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(responses.Dequeue); | ||
|
||
var httpRemoteQueryRunner = new HttpRemoteQueryRunner<RequestTestContextQuery, TestContext>(restClientMock.Object, string.Empty); | ||
var request = new QueryRequest<RequestTestContextQuery>(query, new List<CommandQueryDependency>()); | ||
|
||
// Act | ||
var result = await httpRemoteQueryRunner.QueryAsync(request, CancellationToken.None); | ||
|
||
// Assert | ||
result.Should().BeEquivalentTo(testContext); | ||
responses.Should().BeEmpty(); | ||
} | ||
|
||
[Fact] | ||
public async Task QueryAsync_ShouldThrowWithoutResultAfterMaxRetryReachedRequest() | ||
{ | ||
// Arrange | ||
var query = new RequestTestContextQuery(); | ||
var testContext = new TestContext() | ||
{ | ||
UserId = Guid.NewGuid().ToString() | ||
}; | ||
var restClientMock = new Mock<IRestClient>(); | ||
restClientMock | ||
.Setup( | ||
m => m.ExecuteAsync( | ||
It.IsAny<RestRequest>(), | ||
It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(() => new RestResponse() | ||
{ | ||
StatusCode = HttpStatusCode.ServiceUnavailable | ||
}); | ||
|
||
var httpRemoteQueryRunner = new HttpRemoteQueryRunner<RequestTestContextQuery, TestContext>(restClientMock.Object, string.Empty); | ||
var request = new QueryRequest<RequestTestContextQuery>(query, new List<CommandQueryDependency>()); | ||
|
||
// Act | ||
var exception = await Record.ExceptionAsync(() => httpRemoteQueryRunner.QueryAsync(request, CancellationToken.None)); | ||
|
||
// Assert | ||
exception.Should().NotBeNull(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters