Path: redb-route/tests/redb.Route.Tests.RabbitMQ/RabbitMQStand.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/RabbitMQStand.cs
----
using System.Text;
using RabbitMQ.Client;
using RabbitMQ.Client.Exceptions;
using redb.Route.Tests.Conformance;
namespace redb.Route.Tests.RabbitMQ;
///
/// The RabbitMQ stand: a broker at localhost:5672 (admin/admin), reached through the broker's own .NET client. Its
/// container, when names it, is broken with . The broker is
/// ready when it takes connections on its AMQP port; the readiness command runs the broker's CLI only once the broker
/// has written its Erlang cookie, because a CLI started as root before that writes the cookie itself, with an owner the
/// broker cannot read, and the broker stops with eacces.
///
public sealed class RabbitMQStand : IConnectorStand
{
/// The variable that names the RabbitMQ container a test may restart.
public const string ContainerVariable = "REDB_TEST_RABBITMQ_CONTAINER";
/// The stand of this process.
public static RabbitMQStand Instance { get; } = new();
private RabbitMQStand()
{
}
///
public BrokerFault? Fault { get; } = BrokerFault.FromEnvironment(ContainerVariable, "sh", "-c",
"test -f /var/lib/rabbitmq/.erlang.cookie && rabbitmq-diagnostics -q check_port_connectivity");
///
public TimeSpan Wait { get; } = BrokerFault.DefaultWait;
/// Opens a connection of the broker's own client, without recovery.
public static Task ConnectAsync(CancellationToken ct = default)
=> new ConnectionFactory { HostName = "localhost", Port = 5672, UserName = "admin", Password = "admin" }
.CreateConnectionAsync(ct);
/// Whether exists on the broker; false while the broker does not answer.
public static async Task QueueExistsAsync(string queue, CancellationToken ct = default)
{
try
{
await using var connection = await ConnectAsync(ct);
await using var channel = await connection.CreateChannelAsync(cancellationToken: ct);
await channel.QueueDeclarePassiveAsync(queue, ct);
return true;
}
catch (Exception ex) when (ex is OperationInterruptedException or BrokerUnreachableException or IOException)
{
return false;
}
}
///
public async Task PutAsync(string destination, string body, CancellationToken ct = default)
{
await using var connection = await ConnectAsync(ct);
await using var channel = await connection.CreateChannelAsync(cancellationToken: ct);
await channel.BasicPublishAsync("", destination, Encoding.UTF8.GetBytes(body), ct);
}
///
public async Task> DrainAsync(string destination, TimeSpan wait, CancellationToken ct = default)
{
await using var connection = await ConnectAsync(ct);
await using var channel = await connection.CreateChannelAsync(cancellationToken: ct);
var texts = new List();
var deadline = DateTime.UtcNow + wait;
while (true)
{
if (await channel.BasicGetAsync(destination, autoAck: true, ct) is { } message)
{
texts.Add(Encoding.UTF8.GetString(message.Body.Span));
continue;
}
if (texts.Count > 0 || DateTime.UtcNow >= deadline)
return texts;
await Task.Delay(100, ct);
}
}
}