-
Notifications
You must be signed in to change notification settings - Fork 739
Extract Aspire.Hosting.RabbitMQ.Tests project #5130
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
2 commits
Select commit
Hold shift + click to select a range
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
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
19 changes: 19 additions & 0 deletions
19
tests/Aspire.Hosting.RabbitMQ.Tests/Aspire.Hosting.RabbitMQ.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\Aspire.Hosting.RabbitMQ\Aspire.Hosting.RabbitMQ.csproj" /> | ||
| <ProjectReference Include="..\..\src\Components\Aspire.RabbitMQ.Client\Aspire.RabbitMQ.Client.csproj" /> | ||
| <ProjectReference Include="..\Aspire.Hosting.Tests\Aspire.Hosting.Tests.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="$(RepoRoot)src\Aspire.Hosting.RabbitMQ\RabbitMQContainerImageTags.cs" /> | ||
| <Compile Include="$(SharedDir)VolumeNameGenerator.cs" Link="Utils\VolumeNameGenerator.cs" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
195 changes: 195 additions & 0 deletions
195
tests/Aspire.Hosting.RabbitMQ.Tests/RabbitMQFunctionalTests.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,195 @@ | ||
| // 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; | ||
| using Aspire.Components.Common.Tests; | ||
| using Aspire.Hosting.Utils; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Microsoft.Extensions.Logging; | ||
| using RabbitMQ.Client; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
|
|
||
| namespace Aspire.Hosting.RabbitMQ.Tests; | ||
|
|
||
| public class RabbitMQFunctionalTests(ITestOutputHelper testOutputHelper) | ||
| { | ||
| [Fact] | ||
| [RequiresDocker] | ||
| public async Task VerifyRabbitMQResource() | ||
| { | ||
| var builder = CreateDistributedApplicationBuilder(); | ||
|
|
||
| var rabbitMQ = builder.AddRabbitMQ("rabbitMQ"); | ||
|
|
||
| using var app = builder.Build(); | ||
|
|
||
| await app.StartAsync(); | ||
|
|
||
| var hb = Host.CreateApplicationBuilder(); | ||
| hb.Configuration[$"ConnectionStrings:{rabbitMQ.Resource.Name}"] = await rabbitMQ.Resource.ConnectionStringExpression.GetValueAsync(default); | ||
| hb.AddRabbitMQClient(rabbitMQ.Resource.Name); | ||
|
|
||
| using var host = hb.Build(); | ||
|
|
||
| await host.StartAsync(); | ||
|
|
||
| var connection = host.Services.GetRequiredService<IConnection>(); | ||
|
|
||
| using var channel = connection.CreateModel(); | ||
| const string queueName = "hello"; | ||
| channel.QueueDeclare(queueName, durable: false, exclusive: false, autoDelete: false, arguments: null); | ||
|
|
||
| const string message = "Hello World!"; | ||
| var body = Encoding.UTF8.GetBytes(message); | ||
|
|
||
| channel.BasicPublish(exchange: string.Empty, routingKey: queueName, basicProperties: null, body: body); | ||
|
|
||
| var result = channel.BasicGet(queueName, true); | ||
| Assert.Equal(message, Encoding.UTF8.GetString(result.Body.Span)); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(true)] | ||
| [InlineData(false)] | ||
| [RequiresDocker] | ||
| public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume) | ||
| { | ||
| var cts = new CancellationTokenSource(TimeSpan.FromMinutes(3)); | ||
|
|
||
| string? volumeName = null; | ||
| string? bindMountPath = null; | ||
|
|
||
| try | ||
| { | ||
| var builder1 = CreateDistributedApplicationBuilder(); | ||
| var rabbitMQ1 = builder1.AddRabbitMQ("rabbitMQ"); | ||
| var password = rabbitMQ1.Resource.PasswordParameter.Value; | ||
|
|
||
| if (useVolume) | ||
| { | ||
| // Use a deterministic volume name to prevent them from exhausting the machines if deletion fails | ||
| volumeName = VolumeNameGenerator.CreateVolumeName(rabbitMQ1, nameof(WithDataShouldPersistStateBetweenUsages)); | ||
|
|
||
| // if the volume already exists (because of a crashing previous run), try to delete it | ||
| DockerUtils.AttemptDeleteDockerVolume(volumeName); | ||
| rabbitMQ1.WithDataVolume(volumeName); | ||
| } | ||
| else | ||
| { | ||
| bindMountPath = Directory.CreateTempSubdirectory().FullName; | ||
| rabbitMQ1.WithDataBindMount(bindMountPath); | ||
| } | ||
|
|
||
| using (var app = builder1.Build()) | ||
| { | ||
| await app.StartAsync(); | ||
| try | ||
| { | ||
| var hb = Host.CreateApplicationBuilder(); | ||
| hb.Configuration[$"ConnectionStrings:{rabbitMQ1.Resource.Name}"] = await rabbitMQ1.Resource.ConnectionStringExpression.GetValueAsync(default); | ||
| hb.AddRabbitMQClient(rabbitMQ1.Resource.Name); | ||
|
|
||
| using (var host = hb.Build()) | ||
| { | ||
| await host.StartAsync(); | ||
|
|
||
| var connection = host.Services.GetRequiredService<IConnection>(); | ||
|
|
||
| using var channel = connection.CreateModel(); | ||
| const string queueName = "hello"; | ||
| channel.QueueDeclare(queueName, durable: true, exclusive: false); | ||
|
|
||
| const string message = "Hello World!"; | ||
| var body = Encoding.UTF8.GetBytes(message); | ||
|
|
||
| var props = channel.CreateBasicProperties(); | ||
| props.Persistent = true; // or props.DeliveryMode = 2; | ||
| channel.BasicPublish( | ||
| exchange: string.Empty, | ||
| queueName, | ||
| props, | ||
| body); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| // Stops the container, or the Volume/mount would still be in use | ||
| await app.StopAsync(); | ||
| } | ||
| } | ||
|
|
||
| var builder2 = CreateDistributedApplicationBuilder(); | ||
| var passwordParameter2 = builder2.AddParameter("pwd"); | ||
| builder2.Configuration["Parameters:pwd"] = password; | ||
|
|
||
| var rabbitMQ2 = builder2.AddRabbitMQ("rabbitMQ", password: passwordParameter2); | ||
|
|
||
| if (useVolume) | ||
| { | ||
| rabbitMQ2.WithDataVolume(volumeName); | ||
| } | ||
| else | ||
| { | ||
| rabbitMQ2.WithDataBindMount(bindMountPath!); | ||
| } | ||
|
|
||
| using (var app = builder2.Build()) | ||
| { | ||
| await app.StartAsync(); | ||
| try | ||
| { | ||
| var hb = Host.CreateApplicationBuilder(); | ||
| hb.Configuration[$"ConnectionStrings:{rabbitMQ2.Resource.Name}"] = await rabbitMQ2.Resource.ConnectionStringExpression.GetValueAsync(default); | ||
| hb.AddRabbitMQClient(rabbitMQ2.Resource.Name); | ||
|
|
||
| using (var host = hb.Build()) | ||
| { | ||
| await host.StartAsync(); | ||
|
|
||
| var connection = host.Services.GetRequiredService<IConnection>(); | ||
|
|
||
| using var channel = connection.CreateModel(); | ||
| const string queueName = "hello"; | ||
| channel.QueueDeclare(queueName, durable: true, exclusive: false); | ||
|
|
||
| var result = channel.BasicGet(queueName, true); | ||
| Assert.Equal("Hello World!", Encoding.UTF8.GetString(result.Body.Span)); | ||
| } | ||
| } | ||
| 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.Create(); | ||
| 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
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.
Uh oh!
There was an error while loading. Please reload this page.