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

Improve Nats tests #5783

Merged
merged 5 commits into from
Oct 19, 2024
Merged
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
43 changes: 15 additions & 28 deletions tests/Aspire.Hosting.Nats.Tests/NatsFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
using Aspire.Hosting.Utils;
using Xunit;
using Xunit.Abstractions;
using Polly;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using NATS.Client.Core;
using NATS.Client.JetStream;
using NATS.Client.JetStream.Models;
using Aspire.Hosting.ApplicationModel;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Aspire.Hosting.Tests.Utils;
namespace Aspire.Hosting.Nats.Tests;

public class NatsFunctionalTests(ITestOutputHelper testOutputHelper)
Expand All @@ -24,11 +24,6 @@ public class NatsFunctionalTests(ITestOutputHelper testOutputHelper)
[RequiresDocker]
public async Task VerifyNatsResource()
{
var cts = new CancellationTokenSource(TimeSpan.FromMinutes(5));
var pipeline = new ResiliencePipelineBuilder()
.AddRetry(new() { MaxRetryAttempts = 10, Delay = TimeSpan.FromSeconds(1), ShouldHandle = new PredicateBuilder().Handle<NatsException>() })
.Build();

using var builder = TestDistributedApplicationBuilder.CreateWithTestContainerRegistry(testOutputHelper);

var nats = builder.AddNats("nats")
Expand All @@ -38,6 +33,8 @@ public async Task VerifyNatsResource()

await app.StartAsync();

await app.WaitForTextAsync("Listening for client connections", nats.Resource.Name);

var hb = Host.CreateApplicationBuilder();

hb.Configuration[$"ConnectionStrings:{nats.Resource.Name}"] = await nats.Resource.ConnectionStringExpression.GetValueAsync(default);
Expand All @@ -54,13 +51,10 @@ public async Task VerifyNatsResource()

await host.StartAsync();

await pipeline.ExecuteAsync(async token =>
{
var jetStream = host.Services.GetRequiredService<INatsJSContext>();
var jetStream = host.Services.GetRequiredService<INatsJSContext>();

await CreateTestData(jetStream, token);
await ConsumeTestData(jetStream, token);
}, cts.Token);
await CreateTestData(jetStream, default);
await ConsumeTestData(jetStream, default);
}

[Theory]
Expand All @@ -69,10 +63,6 @@ await pipeline.ExecuteAsync(async token =>
[RequiresDocker]
public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume)
{
var cts = new CancellationTokenSource(TimeSpan.FromMinutes(3));
var pipeline = new ResiliencePipelineBuilder()
.AddRetry(new() { MaxRetryAttempts = 10, Delay = TimeSpan.FromSeconds(1), ShouldHandle = new PredicateBuilder().Handle<NatsException>() })
.Build();
string? volumeName = null;
string? bindMountPath = null;

Expand Down Expand Up @@ -100,6 +90,8 @@ public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume)
using (var app = builder1.Build())
{
await app.StartAsync();

await app.WaitForTextAsync("Listening for client connections", nats1.Resource.Name);
try
{
var hb = Host.CreateApplicationBuilder();
Expand All @@ -118,13 +110,9 @@ public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume)
{
await host.StartAsync();

await pipeline.ExecuteAsync(async token =>
{
var jetStream = host.Services.GetRequiredService<INatsJSContext>();
await CreateTestData(jetStream, token);
await ConsumeTestData(jetStream, token);

}, cts.Token);
var jetStream = host.Services.GetRequiredService<INatsJSContext>();
await CreateTestData(jetStream, default);
await ConsumeTestData(jetStream, default);
}
}
finally
Expand All @@ -150,6 +138,8 @@ await pipeline.ExecuteAsync(async token =>
using (var app = builder2.Build())
{
await app.StartAsync();

await app.WaitForTextAsync("Listening for client connections", nats2.Resource.Name);
try
{
var hb = Host.CreateApplicationBuilder();
Expand All @@ -167,11 +157,8 @@ await pipeline.ExecuteAsync(async token =>
{
await host.StartAsync();

await pipeline.ExecuteAsync(async token =>
{
var jetStream = host.Services.GetRequiredService<INatsJSContext>();
await ConsumeTestData(jetStream, token);
});
var jetStream = host.Services.GetRequiredService<INatsJSContext>();
await ConsumeTestData(jetStream, default);
}
}
finally
Expand Down