-
Notifications
You must be signed in to change notification settings - Fork 738
Extract Aspire.Hosting.Kafka.Tests project #4910
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
68869e3
Extract Aspire.Hosting.Kafka.Tests project
eerhardt b5eb4db
Fix build
eerhardt 7818ff5
fixup .sln file
eerhardt 6b30787
Merge remote-tracking branch 'upstream/main' into ExtractKafkaHosting…
eerhardt f6d9eeb
Merge remote-tracking branch 'upstream/main' into ExtractKafkaHosting…
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
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
6 changes: 4 additions & 2 deletions
6
...pire.Hosting.Tests/Kafka/AddKafkaTests.cs → ...pire.Hosting.Kafka.Tests/AddKafkaTests.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
18 changes: 18 additions & 0 deletions
18
tests/Aspire.Hosting.Kafka.Tests/Aspire.Hosting.Kafka.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,18 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>$(NetCurrent)</TargetFramework> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\src\Aspire.Hosting.Kafka\Aspire.Hosting.Kafka.csproj" /> | ||
| <ProjectReference Include="..\..\src\Components\Aspire.Confluent.Kafka\Aspire.Confluent.Kafka.csproj" /> | ||
| <ProjectReference Include="..\Aspire.Hosting.Tests\Aspire.Hosting.Tests.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="$(RepoRoot)src\Aspire.Hosting.Kafka\KafkaContainerImageTags.cs" /> | ||
| <Compile Include="$(SharedDir)VolumeNameGenerator.cs" Link="Utils\VolumeNameGenerator.cs" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
211 changes: 211 additions & 0 deletions
211
tests/Aspire.Hosting.Kafka.Tests/KafkaFunctionalTests.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,211 @@ | ||
| // 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 Confluent.Kafka; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Microsoft.Extensions.Logging; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
|
|
||
| namespace Aspire.Hosting.Kafka.Tests; | ||
|
|
||
| public class KafkaFunctionalTests(ITestOutputHelper testOutputHelper) | ||
| { | ||
| [Fact] | ||
| [RequiresDocker] | ||
| public async Task VerifyKafkaResource() | ||
| { | ||
| var cts = new CancellationTokenSource(TimeSpan.FromMinutes(3)); | ||
|
|
||
| var builder = CreateDistributedApplicationBuilder(); | ||
|
|
||
| var kafka = builder.AddKafka("kafka"); | ||
|
|
||
| using var app = builder.Build(); | ||
|
|
||
| await app.StartAsync(); | ||
|
|
||
| var hb = Host.CreateApplicationBuilder(); | ||
|
|
||
| hb.Configuration.AddInMemoryCollection(new Dictionary<string, string?> | ||
| { | ||
| [$"ConnectionStrings:{kafka.Resource.Name}"] = await kafka.Resource.ConnectionStringExpression.GetValueAsync(default) | ||
| }); | ||
|
|
||
| hb.AddKafkaProducer<string, string>("kafka"); | ||
| hb.AddKafkaConsumer<string, string>("kafka", consumerBuilder => | ||
| { | ||
| consumerBuilder.Config.GroupId = "aspire-consumer-group"; | ||
| consumerBuilder.Config.AutoOffsetReset = AutoOffsetReset.Earliest; | ||
| }); | ||
|
|
||
| using var host = hb.Build(); | ||
|
|
||
| await host.StartAsync(); | ||
|
|
||
| var topic = "test-topic"; | ||
| var producer = host.Services.GetRequiredService<IProducer<string, string>>(); | ||
| for (var i = 0; i < 10; i++) | ||
| { | ||
| await producer.ProduceAsync(topic, new Message<string, string> { Key = "test-key", Value = $"test-value{i}" }); | ||
| } | ||
|
|
||
| var consumer = host.Services.GetRequiredService<IConsumer<string, string>>(); | ||
| consumer.Subscribe(topic); | ||
| for (var i = 0; i < 10; i++) | ||
| { | ||
| var result = consumer.Consume(cts.Token); | ||
|
|
||
| Assert.Equal($"test-key", result.Message.Key); | ||
| Assert.Equal($"test-value{i}", result.Message.Value); | ||
| } | ||
| } | ||
|
|
||
| [Theory] | ||
| [ActiveIssue("https://github.com/dotnet/aspire/issues/4909")] | ||
| [InlineData(true)] | ||
| [InlineData(false)] | ||
| [RequiresDocker] | ||
| public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume) | ||
| { | ||
| var cts = new CancellationTokenSource(TimeSpan.FromMinutes(3)); | ||
| var topic = "test-topic"; | ||
| string? volumeName = null; | ||
| string? bindMountPath = null; | ||
|
|
||
| try | ||
| { | ||
| var builder1 = CreateDistributedApplicationBuilder(); | ||
| var kafka1 = builder1.AddKafka("kafka"); | ||
|
|
||
| if (useVolume) | ||
| { | ||
| // Use a deterministic volume name to prevent them from exhausting the machines if deletion fails | ||
| volumeName = VolumeNameGenerator.CreateVolumeName(kafka1, nameof(WithDataShouldPersistStateBetweenUsages)); | ||
|
|
||
| // if the volume already exists (because of a crashing previous run), try to delete it | ||
| DockerUtils.AttemptDeleteDockerVolume(volumeName); | ||
| kafka1.WithDataVolume(volumeName); | ||
| } | ||
| else | ||
| { | ||
| bindMountPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); | ||
| kafka1.WithDataBindMount(bindMountPath); | ||
| } | ||
|
|
||
| using (var app = builder1.Build()) | ||
| { | ||
| await app.StartAsync(); | ||
| try | ||
| { | ||
| var hb = Host.CreateApplicationBuilder(); | ||
|
|
||
| hb.Configuration.AddInMemoryCollection(new Dictionary<string, string?> | ||
| { | ||
| [$"ConnectionStrings:{kafka1.Resource.Name}"] = await kafka1.Resource.ConnectionStringExpression.GetValueAsync(default) | ||
| }); | ||
|
|
||
| hb.AddKafkaProducer<string, string>("kafka"); | ||
|
|
||
| using (var host = hb.Build()) | ||
| { | ||
| await host.StartAsync(); | ||
|
|
||
| var producer = host.Services.GetRequiredService<IProducer<string, string>>(); | ||
| for (var i = 0; i < 10; i++) | ||
| { | ||
| await producer.ProduceAsync(topic, new Message<string, string> { Key = "test-key", Value = $"test-value{i}" }); | ||
| } | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| // Stops the container, or the Volume/mount would still be in use | ||
| await app.StopAsync(); | ||
| } | ||
| } | ||
|
|
||
| var builder2 = CreateDistributedApplicationBuilder(); | ||
| var kafka2 = builder2.AddKafka("kafka"); | ||
|
|
||
| if (useVolume) | ||
| { | ||
| kafka2.WithDataVolume(volumeName); | ||
| } | ||
| else | ||
| { | ||
| kafka2.WithDataBindMount(bindMountPath!); | ||
| } | ||
|
|
||
| using (var app = builder2.Build()) | ||
| { | ||
| await app.StartAsync(); | ||
| try | ||
| { | ||
| var hb = Host.CreateApplicationBuilder(); | ||
|
|
||
| hb.Configuration.AddInMemoryCollection(new Dictionary<string, string?> | ||
| { | ||
| [$"ConnectionStrings:{kafka2.Resource.Name}"] = await kafka2.Resource.ConnectionStringExpression.GetValueAsync(default) | ||
| }); | ||
|
|
||
| hb.AddKafkaConsumer<string, string>("kafka", consumerBuilder => | ||
| { | ||
| consumerBuilder.Config.GroupId = "aspire-consumer-group"; | ||
| consumerBuilder.Config.AutoOffsetReset = AutoOffsetReset.Earliest; | ||
| }); | ||
|
|
||
| using (var host = hb.Build()) | ||
| { | ||
| await host.StartAsync(); | ||
|
|
||
| var consumer = host.Services.GetRequiredService<IConsumer<string, string>>(); | ||
| consumer.Subscribe(topic); | ||
| for (var i = 0; i < 10; i++) | ||
| { | ||
| var result = consumer.Consume(cts.Token); | ||
|
|
||
| Assert.Equal($"test-key", result.Message.Key); | ||
| Assert.Equal($"test-value{i}", result.Message.Value); | ||
| } | ||
| } | ||
| } | ||
| 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 | ||
| { | ||
| File.Delete(bindMountPath); | ||
| } | ||
| 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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this test adds value over the
AddKafkaTests.VerifyManifesttest. That test verifies it is a container.v0 resource.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok :)