-
Notifications
You must be signed in to change notification settings - Fork 739
Extract Aspire.Hosting.Nats.Tests project #5002
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bae2e92
Extract Aspire.Hosting.Nats.Tests project
Alirexaa f2eee91
Merge branch 'main' into nats-tests
radical 34cae72
Apply suggested changes
Alirexaa 30d5969
Merge branch 'main' into nats-tests
Alirexaa a974ea4
apply suggested changes
Alirexaa 8db8534
Merge remote-tracking branch 'upstream/main' into nats-tests
eerhardt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...Aspire.Hosting.Tests/Nats/AddNatsTests.cs → ...Aspire.Hosting.Nats.Tests/AddNatsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace Aspire.Hosting.Nats.Tests; | ||
|
|
||
| public record AppEvent(string Subject, string Name, string Description, decimal Priority); | ||
|
|
||
| [JsonSerializable(typeof(AppEvent))] | ||
| [JsonSourceGenerationOptions(PropertyNameCaseInsensitive = true)] | ||
| public partial class AppJsonContext : JsonSerializerContext | ||
| { | ||
| } |
19 changes: 19 additions & 0 deletions
19
tests/Aspire.Hosting.Nats.Tests/Aspire.Hosting.Nats.Tests.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>$(NetCurrent)</TargetFramework> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\src\Aspire.Hosting.AppHost\Aspire.Hosting.AppHost.csproj" /> | ||
| <ProjectReference Include="..\..\src\Components\Aspire.NATS.Net\Aspire.NATS.Net.csproj" /> | ||
| <ProjectReference Include="..\Aspire.Hosting.Tests\Aspire.Hosting.Tests.csproj" /> | ||
| <ProjectReference Include="..\..\src\Aspire.Hosting.Nats\Aspire.Hosting.Nats.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="$(RepoRoot)src\Aspire.Hosting.Nats\NatsContainerImageTags.cs" /> | ||
| <Compile Include="$(SharedDir)VolumeNameGenerator.cs" Link="Utils\VolumeNameGenerator.cs" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,244 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Aspire.Components.Common.Tests; | ||
| using Aspire.Hosting.Utils; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
| using Microsoft.Extensions.Logging; | ||
| using Polly; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using NATS.Client.Core; | ||
| using NATS.Client.JetStream; | ||
| using NATS.Client.JetStream.Models; | ||
| namespace Aspire.Hosting.Nats.Tests; | ||
|
|
||
| public class NatsFunctionalTests(ITestOutputHelper testOutputHelper) | ||
| { | ||
| [Fact] | ||
| [RequiresDocker] | ||
| public async Task VerifyNatsResource() | ||
| { | ||
| var cts = new CancellationTokenSource(TimeSpan.FromMinutes(5)); | ||
| var pipeline = new ResiliencePipelineBuilder() | ||
| .AddRetry(new() { MaxRetryAttempts = 10, Delay = TimeSpan.FromSeconds(2) }) | ||
| .Build(); | ||
|
|
||
| var builder = CreateDistributedApplicationBuilder(); | ||
|
|
||
| var nats = builder.AddNats("nats"); | ||
|
|
||
| using var app = builder.Build(); | ||
|
|
||
| await app.StartAsync(); | ||
|
|
||
| var hb = Host.CreateApplicationBuilder(); | ||
|
|
||
| hb.Configuration.AddInMemoryCollection(new Dictionary<string, string?> | ||
| { | ||
| [$"ConnectionStrings:{nats.Resource.Name}"] = await nats.Resource.ConnectionStringExpression.GetValueAsync(default) | ||
| }); | ||
|
|
||
| hb.AddNatsClient(nats.Resource.Name); | ||
|
|
||
| using var host = hb.Build(); | ||
|
|
||
| await host.StartAsync(); | ||
|
|
||
| await pipeline.ExecuteAsync(async token => | ||
| { | ||
| var natsConnection = host.Services.GetRequiredService<INatsConnection>(); | ||
|
|
||
| var rtt = await natsConnection.PingAsync(token); | ||
Alirexaa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, cts.Token); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(true)] | ||
| [InlineData(false)] | ||
| [RequiresDocker] | ||
| public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume) | ||
| { | ||
| var cts = new CancellationTokenSource(TimeSpan.FromMinutes(3)); | ||
| var streamName = "test-stream"; | ||
| var subjectName = "test-subject"; | ||
| string? volumeName = null; | ||
| string? bindMountPath = null; | ||
|
|
||
| try | ||
| { | ||
| var builder1 = CreateDistributedApplicationBuilder(); | ||
| var nats1 = builder1.AddNats("nats") | ||
| .WithJetStream(); | ||
|
|
||
| if (useVolume) | ||
| { | ||
| // Use a deterministic volume name to prevent them from exhausting the machines if deletion fails | ||
| volumeName = VolumeNameGenerator.CreateVolumeName(nats1, nameof(WithDataShouldPersistStateBetweenUsages)); | ||
|
|
||
| // if the volume already exists (because of a crashing previous run), try to delete it | ||
| DockerUtils.AttemptDeleteDockerVolume(volumeName); | ||
| nats1.WithDataVolume(volumeName); | ||
| } | ||
| else | ||
| { | ||
| bindMountPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); | ||
Alirexaa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| nats1.WithDataBindMount(bindMountPath); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious - |
||
| } | ||
|
|
||
| using (var app = builder1.Build()) | ||
| { | ||
| await app.StartAsync(); | ||
| try | ||
| { | ||
| var hb = Host.CreateApplicationBuilder(); | ||
|
|
||
| hb.Configuration.AddInMemoryCollection(new Dictionary<string, string?> | ||
| { | ||
| [$"ConnectionStrings:{nats1.Resource.Name}"] = await nats1.Resource.ConnectionStringExpression.GetValueAsync(default) | ||
| }); | ||
|
|
||
| hb.AddNatsClient("nats", configureOptions: opts => | ||
| { | ||
| var jsonRegistry = new NatsJsonContextSerializerRegistry(AppJsonContext.Default); | ||
| return opts with { SerializerRegistry = jsonRegistry }; | ||
| }); | ||
|
|
||
| hb.AddNatsJetStream(); | ||
|
|
||
| using (var host = hb.Build()) | ||
| { | ||
| await host.StartAsync(); | ||
|
|
||
| var pipeline = new ResiliencePipelineBuilder() | ||
| .AddRetry(new() { MaxRetryAttempts = 10, Delay = TimeSpan.FromSeconds(1), ShouldHandle = new PredicateBuilder().Handle<NatsException>() }) | ||
| .Build(); | ||
|
|
||
| await pipeline.ExecuteAsync(async token => | ||
| { | ||
| var jetStream = host.Services.GetRequiredService<INatsJSContext>(); | ||
|
|
||
| var stream = await jetStream.CreateStreamAsync(new StreamConfig(streamName, [subjectName]), cancellationToken: token); | ||
| var name = stream.Info.Config.Name; | ||
| Assert.Equal(streamName, stream.Info.Config.Name); | ||
|
|
||
| for (var i = 0; i < 10; i++) | ||
| { | ||
| var appEvent = new AppEvent(subjectName, $"test-event-{i}", $"test-event-description-{i}", i); | ||
| var ack = await jetStream.PublishAsync(appEvent.Subject, appEvent, cancellationToken: token); | ||
| ack.EnsureSuccess(); | ||
| } | ||
|
|
||
| }, cts.Token); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| // Stops the container, or the Volume/mount would still be in use | ||
| await app.StopAsync(); | ||
| } | ||
| } | ||
|
|
||
| var builder2 = CreateDistributedApplicationBuilder(); | ||
| var nats2 = builder2.AddNats("nats") | ||
| .WithJetStream(); | ||
|
|
||
| if (useVolume) | ||
| { | ||
| nats2.WithDataVolume(volumeName); | ||
| } | ||
| else | ||
| { | ||
| nats2.WithDataBindMount(bindMountPath!); | ||
| } | ||
|
|
||
| using (var app = builder2.Build()) | ||
| { | ||
| await app.StartAsync(); | ||
| try | ||
| { | ||
| var hb = Host.CreateApplicationBuilder(); | ||
|
|
||
| hb.Configuration.AddInMemoryCollection(new Dictionary<string, string?> | ||
| { | ||
| [$"ConnectionStrings:{nats2.Resource.Name}"] = await nats2.Resource.ConnectionStringExpression.GetValueAsync(default) | ||
| }); | ||
|
|
||
| hb.AddNatsClient("nats", configureOptions: opts => | ||
| { | ||
| var jsonRegistry = new NatsJsonContextSerializerRegistry(AppJsonContext.Default); | ||
| return opts with { SerializerRegistry = jsonRegistry }; | ||
| }); | ||
|
|
||
| hb.AddNatsJetStream(); | ||
|
|
||
| using (var host = hb.Build()) | ||
| { | ||
| await host.StartAsync(); | ||
|
|
||
| var pipeline = new ResiliencePipelineBuilder() | ||
| .AddRetry(new() { MaxRetryAttempts = 10, Delay = TimeSpan.FromSeconds(1), ShouldHandle = new PredicateBuilder().Handle<NatsException>() }) | ||
| .Build(); | ||
| await pipeline.ExecuteAsync(async token => | ||
| { | ||
| var jetStream = host.Services.GetRequiredService<INatsJSContext>(); | ||
|
|
||
| var stream = await jetStream.GetStreamAsync(streamName, cancellationToken: token); | ||
| var consumer = await stream.CreateOrderedConsumerAsync(cancellationToken: token); | ||
|
|
||
| var events = new List<AppEvent>(); | ||
| await foreach (var msg in consumer.ConsumeAsync<AppEvent>(cancellationToken: token)) | ||
| { | ||
| events.Add(msg.Data!); | ||
|
|
||
| if (msg.Metadata?.NumPending == 0) | ||
| { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| foreach (var item in events.Select((appEvent, i) => new { i, appEvent })) | ||
| { | ||
| Assert.Equal($"test-event-{item.i}", item.appEvent.Name); | ||
| Assert.Equal($"test-event-description-{item.i}", item.appEvent.Description); | ||
| } | ||
Alirexaa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| // Stops the container, or the Volume/mount would still be in use | ||
| await app.StopAsync(); | ||
| } | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| if (volumeName is not null) | ||
| { | ||
| DockerUtils.AttemptDeleteDockerVolume(volumeName); | ||
| } | ||
|
|
||
| if (bindMountPath is not null) | ||
| { | ||
| try | ||
| { | ||
| Directory.Delete(bindMountPath, recursive: true); | ||
| } | ||
| catch | ||
| { | ||
| // Don't fail test if we can't clean the temporary folder | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private TestDistributedApplicationBuilder CreateDistributedApplicationBuilder() | ||
| { | ||
| var builder = TestDistributedApplicationBuilder.CreateWithTestContainerRegistry(); | ||
| builder.Services.AddXunitLogging(testOutputHelper); | ||
| return builder; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.