Path: redb-route/tests/redb.Route.Tests.RabbitMQ/RabbitMQConformanceTests.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.RabbitMQ/RabbitMQConformanceTests.cs
----
using RabbitMQ.Client;
using redb.Route.Abstractions;
using redb.Route.RabbitMQ;
using redb.Route.Tests.Conformance;
namespace redb.Route.Tests.RabbitMQ;
///
/// The RabbitMQ connector against : every case of it runs here. Each test
/// gets its own durable queue (RabbitMQ 4.3 refuses transient non-exclusive ones), declared by the endpoint too
/// (declare=true, the same arguments), so a broker restart that loses the queue does not lose the test. A dead
/// letter queue is a direct exchange <queue>-dlx with the queue <queue>-dlq bound by
/// failed. Runs alone: the restart case breaks the broker for every connection of the suite. Expects RabbitMQ
/// at localhost:5672 (admin/admin).
///
[Trait("Category", "Integration")]
[Collection(BrokerRestartCollection.Name)]
public sealed class RabbitMQConformanceTests : ConnectorConformance
{
private const string Login = "username=admin&password=admin";
private const string DeadLetterKey = "failed";
protected override RabbitMQStand Stand => RabbitMQStand.Instance;
protected override ConnectorTraits Traits { get; } = new()
{
AckOnReceipt = true, Transactions = true, DeadLetter = true, Concurrency = true, Batch = true,
};
protected override int BrokerPort => 5672;
protected override IComponent CreateComponent() => new RabbitMQComponent();
protected override async Task CreateQueueAsync(bool deadLetter)
{
var name = $"conformance-{Guid.NewGuid():N}";
var queue = new ConformanceQueue(name, deadLetter ? $"{name}-dlq" : null);
await using var connection = await RabbitMQStand.ConnectAsync();
await using var channel = await connection.CreateChannelAsync();
if (queue.DeadLetter is { } deadLetterQueue)
{
await channel.ExchangeDeclareAsync(DeadLetterExchange(queue), ExchangeType.Direct, durable: true, autoDelete: false);
await channel.QueueDeclareAsync(deadLetterQueue, durable: true, exclusive: false, autoDelete: false);
await channel.QueueBindAsync(deadLetterQueue, DeadLetterExchange(queue), DeadLetterKey);
}
await channel.QueueDeclareAsync(name, durable: true, exclusive: false, autoDelete: false, arguments: Arguments(queue));
return queue;
}
protected override async Task DeleteQueueAsync(ConformanceQueue queue)
{
await using var connection = await RabbitMQStand.ConnectAsync();
await using var channel = await connection.CreateChannelAsync();
await channel.QueueDeleteAsync(queue.Name);
if (queue.DeadLetter is { } deadLetterQueue)
{
await channel.QueueDeleteAsync(deadLetterQueue);
await channel.ExchangeDeleteAsync(DeadLetterExchange(queue));
}
}
protected override string ConsumerUri(ConformanceQueue queue, ConsumerSettings settings)
{
var uri = $"rabbitmq://{queue.Name}?host=localhost&port={settings.Port ?? BrokerPort}&{Login}&declare=true&retryDelay=100";
if (settings.Attempts is { } attempts)
uri += $"&maximumRetryAttempts={attempts}";
if (settings.AckOnReceipt)
uri += "&ackMode=auto";
if (settings.Concurrency is { } concurrency)
uri += $"&concurrentConsumers={concurrency}";
if (settings.BatchSize is { } size)
uri += $"&batching=true&maxBatchMessages={size}&pollTimeoutMs={(int)settings.BatchWait.TotalMilliseconds}";
if (queue.DeadLetter is not null)
uri += $"&deadLetterExchange={DeadLetterExchange(queue)}&deadLetterRoutingKey={DeadLetterKey}";
return uri;
}
protected override string ProducerUri(ConformanceQueue queue, bool transacted)
=> $"rabbitmq://{queue.Name}?host=localhost&port={BrokerPort}&{Login}" + (transacted ? "&transacted=true" : "");
// The broker keeps nothing over a restart (its data folder is a tmpfs): the queue is back once the endpoint or the
// client's topology recovery declared it again.
protected override Task QueueIsBackAsync(ConformanceQueue queue) => RabbitMQStand.QueueExistsAsync(queue.Name);
private static string DeadLetterExchange(ConformanceQueue queue) => $"{queue.Name}-dlx";
// The arguments the endpoint declares with deadLetterExchange and deadLetterRoutingKey: the same, or the broker
// refuses the endpoint's declare with 406.
private static Dictionary? Arguments(ConformanceQueue queue) => queue.DeadLetter is null
? null
: new Dictionary
{
["x-dead-letter-exchange"] = DeadLetterExchange(queue),
["x-dead-letter-routing-key"] = DeadLetterKey,
};
}