Skip to content

Commit

Permalink
Improve Elasticsearch functional tests reliability (#7014)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros authored Jan 14, 2025
1 parent 66465d1 commit 3cf908b
Showing 1 changed file with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public async Task VerifyElasticsearchResource()

await app.StartAsync();

var rns = app.Services.GetRequiredService<ResourceNotificationService>();
await rns.WaitForResourceHealthyAsync(elasticsearch.Resource.Name, cts.Token);

var hb = Host.CreateApplicationBuilder();

hb.Configuration[$"ConnectionStrings:{elasticsearch.Resource.Name}"] = await elasticsearch.Resource.ConnectionStringExpression.GetValueAsync(default);
Expand Down Expand Up @@ -101,7 +104,10 @@ public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume)

using (var app = builder1.Build())
{
await app.StartAsync();
await app.StartAsync(cts.Token);

var rns = app.Services.GetRequiredService<ResourceNotificationService>();
await rns.WaitForResourceHealthyAsync(elasticsearch1.Resource.Name, cts.Token);

try
{
Expand All @@ -121,6 +127,17 @@ await pipeline.ExecuteAsync(
var elasticsearchClient = host.Services.GetRequiredService<ElasticsearchClient>();
await CreateTestData(elasticsearchClient, testOutputHelper, token);
}, cts.Token);

await app.StopAsync();

// Wait for the container to be stopped and to release the volume files before continuing
await pipeline.ExecuteAsync(
async token =>
{
var elasticsearchClient = host.Services.GetRequiredService<ElasticsearchClient>();
var getResponse = await elasticsearchClient.GetAsync<Person>(IndexName, s_person.Id, token);
Assert.False(getResponse.IsSuccess());
}, cts.Token);
}
}
finally
Expand Down Expand Up @@ -148,6 +165,9 @@ await pipeline.ExecuteAsync(
{
await app.StartAsync();

var rns = app.Services.GetRequiredService<ResourceNotificationService>();
await rns.WaitForResourceHealthyAsync(elasticsearch2.Resource.Name, cts.Token);

try
{
var hb = Host.CreateApplicationBuilder();
Expand All @@ -171,6 +191,16 @@ await pipeline.ExecuteAsync(
Assert.Equal(s_person.Id, getResponse.Source?.Id);
}, cts.Token);

await app.StopAsync();

// Wait for the container to be stopped and to release the volume files before continuing
await pipeline.ExecuteAsync(
async token =>
{
var elasticsearchClient = host.Services.GetRequiredService<ElasticsearchClient>();
var getResponse = await elasticsearchClient.GetAsync<Person>(IndexName, s_person.Id, token);
Assert.False(getResponse.IsSuccess());
}, cts.Token);
}
}
finally
Expand Down Expand Up @@ -219,7 +249,7 @@ public async Task VerifyWaitForOnElasticsearchBlocksDependentResources()
var resource = builder.AddElasticsearch("resource")
.WithHealthCheck("blocking_check");

var dependentResource = builder.AddElasticsearch("dependentresource")
var dependentResource = builder.AddContainer("nginx", "mcr.microsoft.com/cbl-mariner/base/nginx", "1.22")
.WaitFor(resource);

using var app = builder.Build();
Expand All @@ -234,7 +264,7 @@ public async Task VerifyWaitForOnElasticsearchBlocksDependentResources()

healthCheckTcs.SetResult(HealthCheckResult.Healthy());

await rns.WaitForResourceAsync(resource.Resource.Name, (re => re.Snapshot.HealthStatus == Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus.Healthy), cts.Token);
await rns.WaitForResourceHealthyAsync(resource.Resource.Name, cts.Token);

await rns.WaitForResourceAsync(dependentResource.Resource.Name, KnownResourceStates.Running, cts.Token);

Expand Down

0 comments on commit 3cf908b

Please sign in to comment.