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

Make Elasticsearch Function tests more stable #7086

Closed
wants to merge 1 commit into from
Closed
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 @@ -8,7 +8,6 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using Polly;
using Xunit;
using Xunit.Abstractions;

Expand All @@ -30,9 +29,6 @@ public class ElasticsearchFunctionalTests(ITestOutputHelper testOutputHelper)
public async Task VerifyElasticsearchResource()
{
var cts = new CancellationTokenSource(TimeSpan.FromMinutes(10));
var pipeline = new ResiliencePipelineBuilder()
.AddRetry(new() { MaxRetryAttempts = 10, Delay = TimeSpan.FromSeconds(10) })
.Build();

using var builder = TestDistributedApplicationBuilder.CreateWithTestContainerRegistry(testOutputHelper);

Expand All @@ -42,6 +38,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 All @@ -52,14 +51,9 @@ public async Task VerifyElasticsearchResource()

await host.StartAsync();

await pipeline.ExecuteAsync(
async token =>
{

var elasticsearchClient = host.Services.GetRequiredService<ElasticsearchClient>();
var elasticsearchClient = host.Services.GetRequiredService<ElasticsearchClient>();

await CreateTestData(elasticsearchClient, testOutputHelper, token);
}, cts.Token);
await CreateTestData(elasticsearchClient, testOutputHelper, cts.Token);
}

[Theory]
Expand All @@ -69,9 +63,6 @@ await pipeline.ExecuteAsync(
public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume)
{
var cts = new CancellationTokenSource(TimeSpan.FromMinutes(10));
var pipeline = new ResiliencePipelineBuilder()
.AddRetry(new() { MaxRetryAttempts = 10, Delay = TimeSpan.FromSeconds(10) })
.Build();

string? volumeName = null;
string? bindMountPath = null;
Expand Down Expand Up @@ -103,6 +94,9 @@ public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume)
{
await app.StartAsync();

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

try
{
var hb = Host.CreateApplicationBuilder();
Expand All @@ -111,17 +105,11 @@ public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume)

hb.AddElasticsearchClient(elasticsearch1.Resource.Name);

using (var host = hb.Build())
{
await host.StartAsync();

await pipeline.ExecuteAsync(
async token =>
{
var elasticsearchClient = host.Services.GetRequiredService<ElasticsearchClient>();
await CreateTestData(elasticsearchClient, testOutputHelper, token);
}, cts.Token);
}
using var host = hb.Build();
await host.StartAsync();

var elasticsearchClient = host.Services.GetRequiredService<ElasticsearchClient>();
await CreateTestData(elasticsearchClient, testOutputHelper, cts.Token);
}
finally
{
Expand All @@ -148,6 +136,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 @@ -156,22 +147,16 @@ await pipeline.ExecuteAsync(

hb.AddElasticsearchClient(elasticsearch2.Resource.Name);

using (var host = hb.Build())
{
await host.StartAsync();
await pipeline.ExecuteAsync(
async token =>
{
var elasticsearchClient = host.Services.GetRequiredService<ElasticsearchClient>();
using var host = hb.Build();
await host.StartAsync();

var getResponse = await elasticsearchClient.GetAsync<Person>(IndexName, s_person.Id, token);
var elasticsearchClient = host.Services.GetRequiredService<ElasticsearchClient>();

Assert.True(getResponse.IsSuccess());
Assert.NotNull(getResponse.Source);
Assert.Equal(s_person.Id, getResponse.Source?.Id);
}, cts.Token);
var getResponse = await elasticsearchClient.GetAsync<Person>(IndexName, s_person.Id, cts.Token);

}
Assert.True(getResponse.IsSuccess());
Assert.NotNull(getResponse.Source);
Assert.Equal(s_person.Id, getResponse.Source?.Id);
}
finally
{
Expand Down
Loading