Skip to content

Commit

Permalink
Added test coverage on correct construction of global vs tenant aware…
Browse files Browse the repository at this point in the history
… senders and listeners when Rabbit MQ is multi-tenanted
  • Loading branch information
jeremydmiller committed Dec 2, 2024
1 parent 9becd10 commit 1419085
Showing 1 changed file with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
using JasperFx.Core;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using NSubstitute;
using NSubstitute.ReceivedExtensions;
using Oakton.Resources;
using Shouldly;
using Wolverine.ComplianceTests.Compliance;
using Wolverine.Configuration;
using Wolverine.RabbitMQ.Internal;
using Wolverine.Tracking;
using Wolverine.Transports;
using Wolverine.Transports.Sending;
using Xunit;

Expand Down Expand Up @@ -49,6 +53,14 @@ public async Task InitializeAsync()
// Listen for multiples
opts.ListenToRabbitQueue("multi_response");

opts.ListenToRabbitQueue("global_response").GlobalListener();

// Really just using this to test the construction of senders and listeners
opts.PublishMessage<Message1>().ToRabbitQueue("message1");
opts.PublishMessage<Message2>().ToRabbitQueue("message2").GlobalSender();
opts.PublishMessage<Message3>().ToRabbitExchange("message3");
opts.PublishMessage<Message4>().ToRabbitExchange("message4").GlobalSender();

opts.PublishMessage<MultiTenantMessage>().ToRabbitQueue("multi_incoming");

opts.Services.AddResourceSetupOnStartup();
Expand Down Expand Up @@ -151,6 +163,90 @@ public async Task send_message_to_a_specific_tenant()
response.Envelope.Message.ShouldBeOfType<MultiTenantResponse>()
.Id.ShouldBe(message.Id);
}

/*
opts.PublishMessage<Message3>().ToRabbitExchange("message3");
opts.PublishMessage<Message4>().ToRabbitExchange("message4").GlobalSender();
*/

[Fact]
public void build_compound_sender_for_tenant_aware_exchange()
{
var runtime = _fixture.Main.GetRuntime();
var transport = runtime.Options.Transports.GetOrCreate<RabbitMqTransport>();
var exchange = transport.Exchanges["message3"];
exchange.TenancyBehavior.ShouldBe(TenancyBehavior.TenantAware);

var sender = exchange.ResolveSender(runtime);
sender.ShouldBeOfType<TenantedSender>();
}

[Fact]
public void build_simple_sender_for_global_exchange()
{
var runtime = _fixture.Main.GetRuntime();
var transport = runtime.Options.Transports.GetOrCreate<RabbitMqTransport>();
var exchange = transport.Exchanges["message4"];
exchange.TenancyBehavior.ShouldBe(TenancyBehavior.Global);

var sender = exchange.ResolveSender(runtime);
sender.ShouldBeOfType<RabbitMqSender>();
}

[Fact]
public void build_compound_sender_for_tenant_aware_queue()
{
var runtime = _fixture.Main.GetRuntime();
var transport = runtime.Options.Transports.GetOrCreate<RabbitMqTransport>();
var queue = transport.Queues["message1"];
queue.TenancyBehavior.ShouldBe(TenancyBehavior.TenantAware);

var sender = queue.ResolveSender(runtime);
sender.ShouldBeOfType<TenantedSender>();
}

[Fact]
public void build_simple_sender_for_global_queue()
{
var runtime = _fixture.Main.GetRuntime();
var transport = runtime.Options.Transports.GetOrCreate<RabbitMqTransport>();
var queue = transport.Queues["message2"];
queue.TenancyBehavior.ShouldBe(TenancyBehavior.Global);

var sender = queue.ResolveSender(runtime);
sender.ShouldBeOfType<RabbitMqSender>();
}

[Fact]
public async Task opt_into_global_listener_for_queue()
{
var runtime = _fixture.Main.GetRuntime();
var transport = runtime.Options.Transports.GetOrCreate<RabbitMqTransport>();
var queue = transport.Queues["global_response"];
queue.TenancyBehavior.ShouldBe(TenancyBehavior.Global);

var receiver = Substitute.For<IReceiver>();
var listener = await queue.BuildListenerAsync(runtime, receiver);

// Not parallel
listener.ShouldBeOfType<RabbitMqListener>();
}

[Fact]
public async Task use_tenanted_for_listener_when_appropriate()
{
var runtime = _fixture.Main.GetRuntime();
var transport = runtime.Options.Transports.GetOrCreate<RabbitMqTransport>();
var queue = transport.Queues["multi_response"];
queue.TenancyBehavior.ShouldBe(TenancyBehavior.TenantAware);

var receiver = Substitute.For<IReceiver>();
var listener = await queue.BuildListenerAsync(runtime, receiver);

// Not parallel
listener.ShouldBeOfType<CompoundListener>();
}
}

public static class MultiTenantedRabbitMqSamples
Expand Down Expand Up @@ -198,6 +294,13 @@ public static async Task Configure()
// brokers
opts.ListenToRabbitQueue("incoming");

opts.ListenToRabbitQueue("incoming_global")

// This opts this queue out from being per-tenant, such that
// there will only be the single "incoming_global" queue for the default
// broker connection
.GlobalListener();

// More on this in the docs....
opts.PublishMessage<Message1>()
.ToRabbitQueue("outgoing");
Expand Down

0 comments on commit 1419085

Please sign in to comment.