Skip to content

Commit 03dd1e1

Browse files
committed
feat(tests): add resilience pipeline to readiness health check
- Introduced a resilience pipeline to handle retries and timeouts for the readiness health check. - Ensures robust behavior by retrying up to 10 times for 404 errors and imposing a timeout of 30 seconds.
1 parent 6e03a41 commit 03dd1e1

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

tests/ES.Kubernetes.Reflector.Tests/Integration/HealthCheckIntegrationTests.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.Extensions.Diagnostics.HealthChecks;
1010
using Polly.Retry;
1111
using Polly;
12+
using k8s.Autorest;
1213

1314
[assembly: AssemblyFixture(typeof(ReflectorIntegrationFixture))]
1415

@@ -21,6 +22,21 @@ public class HealthCheckIntegrationTests(ReflectorIntegrationFixture integration
2122
private readonly ReflectorIntegrationFixture _integrationFixture = integrationFixture;
2223

2324

25+
private static readonly ResiliencePipeline<bool> HealthCheckResiliencePipeline =
26+
new ResiliencePipelineBuilder<bool>()
27+
.AddRetry(new RetryStrategyOptions<bool>
28+
{
29+
ShouldHandle = new PredicateBuilder<bool>()
30+
.Handle<HttpOperationException>(ex =>
31+
ex.Response.StatusCode == HttpStatusCode.NotFound)
32+
.HandleResult(false),
33+
MaxRetryAttempts = 10,
34+
Delay = TimeSpan.FromSeconds(1)
35+
})
36+
.AddTimeout(TimeSpan.FromSeconds(30))
37+
.Build();
38+
39+
2440
[Fact]
2541
public async Task LivenessHealthCheck_Should_Return_Healthy()
2642
{
@@ -38,14 +54,17 @@ public async Task LivenessHealthCheck_Should_Return_Healthy()
3854
[Fact]
3955
public async Task ReadinessHealthCheck_Should_Return_Healthy()
4056
{
41-
var httpClient = _integrationFixture.Reflector.CreateClient();
4257
var settings = _integrationFixture.Reflector.Services.GetRequiredService<IgniteSettings>();
43-
var response = await httpClient.GetAsync(settings.AspNetCore.HealthChecks.ReadinessEndpointPath,
44-
TestContext.Current.CancellationToken);
45-
var content = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);
58+
var httpClient = _integrationFixture.Reflector.CreateClient();
4659

47-
//Assert.Equal(HttpStatusCode.OK, response.StatusCode);
48-
//Assert.Equal("text/plain", response.Content.Headers.ContentType?.MediaType);
49-
Assert.Equal("Healthy", content);
60+
await HealthCheckResiliencePipeline.ExecuteAsync(async token =>
61+
{
62+
var response = await httpClient.GetAsync(settings.AspNetCore.HealthChecks.ReadinessEndpointPath,
63+
TestContext.Current.CancellationToken);
64+
response.EnsureSuccessStatusCode();
65+
return response.StatusCode == HttpStatusCode.OK;
66+
}, TestContext.Current.CancellationToken);
67+
68+
5069
}
5170
}

0 commit comments

Comments
 (0)