Path: redb-route/tests/redb.Route.Tests.Amqp/AmqpReconnectTests.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.Amqp/AmqpReconnectTests.cs
----
using System.Collections.Concurrent;
using redb.Route.Abstractions;
using redb.Route.Amqp;
using redb.Route.Core;
using redb.Route.Tests.Conformance;
namespace redb.Route.Tests.Amqp;
///
/// A consumer outlives a broker restart: the restart closes its connection and with it every receiver link, and once
/// the broker is back the same route reads the address again, single and batch, and the endpoint counts one reconnect.
/// The restart is on : it runs only where
/// REDB_TEST_AMQP_CONTAINER names the broker's container, elsewhere it is skipped. Run it with
/// tests/docker/run-fault-suite.sh --broker artemis.
///
[Trait("Category", "Integration")]
[Collection(AmqpBrokerRestartCollection.Name)]
public sealed class AmqpReconnectTests
{
// ANYCAST: the stand puts to the queue of the address; without it Artemis auto-creates a MULTICAST address for the
// receiver and the put goes past it.
private const string Broker = "host=localhost&port=5673&user=admin&password=admin&routingType=ANYCAST";
private static readonly AmqpStand Stand = AmqpStand.Instance;
[BrokerFaultTheory(AmqpStand.ContainerVariable)]
[InlineData("")]
[InlineData("&batching=true&maxBatchMessages=10&pollTimeoutMs=200")]
public async Task A_consumer_reads_the_address_again_after_the_broker_restarts(string extra)
{
var address = $"restart.{Guid.NewGuid():N}";
var received = new ConcurrentQueue();
await using var context = new RouteContext();
context.AddComponent(new AmqpComponent());
var uri = $"amqp://{address}?{Broker}{extra}";
context.AddRoutes(r => r.From(uri).RouteId("restarted").Process(e =>
{
// The consumer hands the route the body as UTF-8 bytes.
if (e.In.Body is IEnumerable batch)
foreach (var message in batch)
received.Enqueue(Text(message.Body));
else
received.Enqueue(Text(e.In.Body));
}));
await context.Start();
await Stand.PutAsync(address, "before");
(await Until(() => received.Contains("before"))).Should().BeTrue("the route reads the address before the restart");
await Stand.Fault!.RestartAsync();
(await BrokerFault.UntilAsync(async () =>
{
try { await Stand.PutAsync(address, "after"); return true; }
catch (Exception) { return false; }
}, Stand.Wait, TimeSpan.FromMilliseconds(500))).Should().BeTrue("the broker takes a message once it is back");
(await Until(() => received.Contains("after"))).Should().BeTrue("the consumer reads what was put after the restart");
var statistics = (IEndpointStatistics)context.GetEndpoint(uri);
statistics.Reconnects.Should().Be(1, "one restart is one reconnect");
statistics.HealthStatus.Should().Be(EndpointHealthStatus.Healthy, "the receiver link is back");
context.GetRoute("restarted")!.Status.Should().Be(RouteStatus.Started, "a lost connection does not stop the route");
}
private static string Text(object? body)
=> body is byte[] bytes ? System.Text.Encoding.UTF8.GetString(bytes) : body?.ToString() ?? "";
private static async Task Until(Func condition)
{
var deadline = DateTime.UtcNow + Stand.Wait;
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 AmqpBrokerRestartCollection
{
/// The collection's name.
public const string Name = "AMQP broker restart";
}