-
Notifications
You must be signed in to change notification settings - Fork 430
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract Aspire.Hosting.RabbitMQ.Tests project (#5130)
* Extract Aspire.Hosting.RabbitMQ.Tests project Contributes to #3185 Contributes to #4294 * Address PR feedback
- Loading branch information
Showing
14 changed files
with
228 additions
and
131 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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.CreateWithTestContainerRegistry(); | ||
builder.Services.AddXunitLogging(testOutputHelper); | ||
return builder; | ||
} | ||
} |
This file contains 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.