Path: redb-route/tests/redb.Route.Tests.RabbitMQ/RabbitMQReconnectTests.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/RabbitMQReconnectTests.cs
----
using System.Collections.Concurrent;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.RabbitMQ;
using redb.Route.Tests.Conformance;
namespace redb.Route.Tests.RabbitMQ;
///
/// A consumer outlives a broker restart without the client's automatic recovery: the consumer opens a new connection
/// and subscribes again itself, the same route reads the queue again and the endpoint counts one reconnect. With
/// automatic recovery (the default) the case is RabbitMQConformanceTests'. The restart is
/// on : it runs only where REDB_TEST_RABBITMQ_CONTAINER
/// names the broker's container, elsewhere it is skipped. Run it with
/// tests/docker/run-fault-suite.sh --broker rabbitmq. The broker keeps nothing over the restart (its data
/// folder is a tmpfs), so the queue is declared again by whoever brings the subscription back.
///
[Trait("Category", "Integration")]
[Collection(BrokerRestartCollection.Name)]
public sealed class RabbitMQReconnectTests
{
private static readonly RabbitMQStand Stand = RabbitMQStand.Instance;
[BrokerFaultTheory(RabbitMQStand.ContainerVariable)]
[InlineData(false)]
public async Task A_consumer_reads_the_queue_again_after_the_broker_restarts(bool automaticRecovery)
{
var queue = $"test-restart-{Guid.NewGuid():N}";
var received = new ConcurrentQueue();
await using var context = new RouteContext();
context.AddComponent(new RabbitMQComponent());
var uri = $"rabbitmq://{queue}?host=localhost&port=5672&username=admin&password=admin&declare=true"
+ $"&automaticRecovery={automaticRecovery.ToString().ToLowerInvariant()}&recoveryInterval=1";
context.AddRoutes(r => r.From(uri).Process(e => received.Enqueue(
e.In.Body is byte[] bytes ? System.Text.Encoding.UTF8.GetString(bytes) : e.In.Body?.ToString() ?? "")));
await context.Start();
await Stand.PutAsync(queue, "before");
(await Until(() => received.Contains("before"), Stand.Wait)).Should().BeTrue("the route reads the queue before the restart");
await Stand.Fault!.RestartAsync();
var statistics = (IEndpointStatistics)context.GetEndpoint(uri);
(await Until(() => statistics.Reconnects > 0, Stand.Wait)).Should().BeTrue(
"the consumer's supervisor counts the reconnect once the subscription is back");
(await BrokerFault.UntilAsync(() => RabbitMQStand.QueueExistsAsync(queue), Stand.Wait, TimeSpan.FromMilliseconds(500)))
.Should().BeTrue("the queue is declared again with the subscription");
await Stand.PutAsync(queue, "after");
(await Until(() => received.Contains("after"), Stand.Wait)).Should().BeTrue(
"the consumer reads what was put after the restart");
statistics.Reconnects.Should().Be(1, "one restart is one reconnect");
statistics.HealthStatus.Should().Be(EndpointHealthStatus.Healthy, "the subscription is back");
}
private static async Task Until(Func condition, TimeSpan within)
{
var deadline = DateTime.UtcNow + within;
while (!condition())
{
if (DateTime.UtcNow > deadline)
return false;
await Task.Delay(100);
}
return true;
}
}
/// A broker restart cuts every connection of the suite, so nothing runs beside the restart tests.
[CollectionDefinition(Name, DisableParallelization = true)]
public sealed class BrokerRestartCollection
{
/// The collection's name.
public const string Name = "RabbitMQ broker restart";
}