Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(csharp): add retry strategy #2629

Merged
merged 2 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Algolia.Search.Clients;
using Algolia.Search.Http;

[assembly: InternalsVisibleTo("Algolia.Search.Test")]
[assembly: InternalsVisibleTo("Algolia.Search.Tests")]

namespace Algolia.Search.Transport;

Expand Down
192 changes: 192 additions & 0 deletions tests/output/csharp/src/tests/RetryStrategyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
using System.Text;
using Algolia.Search.Clients;
using Algolia.Search.Exceptions;
using Algolia.Search.Http;
using Algolia.Search.Models.Search;
using Algolia.Search.Transport;
using Moq;
using Newtonsoft.Json;
using Xunit;

namespace Algolia.Search.Tests.tests;

public class RetryStrategyTests
{
[Fact]
public async Task ShouldRetryOnAllEligibleHostFailed()
{
var httpMock = new Mock<IHttpRequester>();
var searchConfig = new SearchConfig("test-app-id", "test-api-key");
var client = new SearchClient(searchConfig, httpMock.Object);

// The `SearchSingleIndexAsync` use Host with CallType = Read
var eligibleHosts = searchConfig.DefaultHosts.Where(x => x.Accept.HasFlag(CallType.Read)).Select(x => x.Url);

IList<string> actualHosts = new List<string>();

httpMock
.Setup(c =>
c.SendRequestAsync(It.Is<Request>(r => r.Uri.AbsolutePath.EndsWith("/1/indexes/test-index/query")),
It.IsAny<TimeSpan>(),
It.IsAny<TimeSpan>(),
It.IsAny<CancellationToken>()
))
// Always return a 500 from Algolia server
.Returns(
Task.FromResult(
new AlgoliaHttpResponse
{
HttpStatusCode = 500,
Body = null
}
)
).Callback((Request rq, TimeSpan _, TimeSpan _, CancellationToken _) => actualHosts.Add(rq.Uri.Host));

// Do a simple search
await Assert.ThrowsAsync<AlgoliaUnreachableHostException>(async () =>
await client.SearchSingleIndexAsync<object>("test-index",
new SearchParams(new SearchParamsObject { Query = "" })));

Assert.Equal(eligibleHosts, actualHosts);
}


[Fact]
public async Task ShouldThrowsWhenAllRetriesFailed()
{
var httpMock = new Mock<IHttpRequester>();
var searchConfig = new SearchConfig("test-app-id", "test-api-key");
var client = new SearchClient(searchConfig, httpMock.Object);

httpMock
.SetupSequence(c =>
c.SendRequestAsync(
It.Is<Request>(r => r.Uri.AbsolutePath.EndsWith("/1/indexes/test-index/query")),
It.IsAny<TimeSpan>(),
It.IsAny<TimeSpan>(),
It.IsAny<CancellationToken>()
)
)
// First call return a 500 from Algolia server
.Returns(
Task.FromResult(
new AlgoliaHttpResponse
{
HttpStatusCode = 500,
Body = null
}
)
)
// Second call return a 300 from Algolia server
.Returns(
Task.FromResult(
new AlgoliaHttpResponse
{
HttpStatusCode = 300,
Body = null
}
)
).Returns(
Task.FromResult(
new AlgoliaHttpResponse
{
IsTimedOut = true,
Body = null
}
)
).Returns(
Task.FromResult(
new AlgoliaHttpResponse
{
IsNetworkError = true,
Error = "DNS server not responding",
Body = null
}
)
);

// Do a simple search and expect a AlgoliaUnreachableHostException after 4 retries
var exception = await Assert.ThrowsAsync<AlgoliaUnreachableHostException>(async () =>
await client.SearchSingleIndexAsync<object>("test-index",
new SearchParams(new SearchParamsObject { Query = "" })));

Assert.Equal("RetryStrategy failed to connect to Algolia. Reason: DNS server not responding", exception.Message);

// Verify that the request has been called 4 times
httpMock.Verify(
m =>
m.SendRequestAsync(
It.Is<Request>(r => r.Uri.AbsolutePath.EndsWith("/1/indexes/test-index/query")),
It.IsAny<TimeSpan>(),
It.IsAny<TimeSpan>(),
It.IsAny<CancellationToken>()
),
Times.Exactly(4)
);
}

[Fact]
public async Task CanOverrideHost()
{
var httpMock = new Mock<IHttpRequester>();
var searchConfig = new SearchConfig("test-app-id", "test-api-key")
{
CustomHosts = new List<StatefulHost>()
{
new()
{
Url = "myhost.com",
Accept = CallType.Read,
Up = true
}
}
};

var client = new SearchClient(searchConfig, httpMock.Object);

httpMock
.SetupSequence(c =>
c.SendRequestAsync(
It.Is<Request>(r => r.Uri.AbsoluteUri.Equals("https://myhost.com/1/indexes/test-index/query")),
It.IsAny<TimeSpan>(),
It.IsAny<TimeSpan>(),
It.IsAny<CancellationToken>()
)
)
.Returns(
Task.FromResult(
new AlgoliaHttpResponse
{
HttpStatusCode = 200,
Body = new MemoryStream(
Encoding.UTF8.GetBytes(
JsonConvert.SerializeObject(
new SearchResponse<object>()
{
HitsPerPage = 10, NbHits = 1, NbPages = 1, Page = 1, ProcessingTimeMS = 1,
Hits = new List<object>(), Query = "",
VarParams = ""
}
)
)
)
}
)
);

// Do a simple search and expect a AlgoliaUnreachableHostException after 4 retries
await client.SearchSingleIndexAsync<object>("test-index", new SearchParams(new SearchParamsObject { Query = "" }));

// Verify that the request has been called 1 times
httpMock.Verify(
m =>
m.SendRequestAsync(
It.Is<Request>(r => r.Uri.AbsoluteUri.Equals("https://myhost.com/1/indexes/test-index/query")),
It.IsAny<TimeSpan>(),
It.IsAny<TimeSpan>(),
It.IsAny<CancellationToken>()
),
Times.Exactly(1)
);
}
}
Loading