Path: redb-route/tests/redb.Route.Tests.RabbitMQ/RabbitMQConcurrencyLeakAutoAckTests.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/RabbitMQConcurrencyLeakAutoAckTests.cs
----
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.RabbitMQ;
namespace redb.Route.Tests.RabbitMQ;
///
/// End-to-end regression test for a 3.2.2 RabbitMQ fix against a real broker (expects RabbitMQ at localhost:5672,
/// admin/admin): a per-route Stop/Start leaked one consume channel each cycle. The concurrency bound, auto ack and
/// the drain are cases of RabbitMQConformanceTests.
///
[Trait("Category", "Integration")]
public sealed class RabbitMQConcurrencyLeakAutoAckTests
{
private const string Host = "localhost";
private const int Port = 5672;
private static RabbitMQEndpoint CreateEndpoint(string queue, string? extraParams = null)
{
var qs = $"host={Host}&port={Port}&username=admin&password=admin&declare=true";
if (extraParams is not null) qs += $"&{extraParams}";
var uri = EndpointUriParser.Parse($"rabbitmq://{queue}?{qs}");
var component = new RabbitMQComponent();
return (RabbitMQEndpoint)component.CreateEndpoint(uri);
}
[Fact]
public async Task Consumer_StopStartCycles_DoNotLeakChannels()
{
// Reproduces the prod leak: RabbitMQConsumer.Stop() used to leave its consume channel open and
// registered on the endpoint (endpoint.Stop() — the only place channels were closed — is not
// called on a per-route Stop/Start). Each cycle therefore leaked one idle channel. With the fix
// the consumer releases its own channel on Stop, so the endpoint's tracked-channel count returns
// to 0 after every Stop instead of climbing.
var queue = $"test-leak-{Guid.NewGuid():N}";
var ep = CreateEndpoint(queue);
var processor = Substitute.For();
processor.Process(Arg.Any(), Arg.Any()).Returns(Task.CompletedTask);
var consumer = (RabbitMQConsumer)ep.CreateConsumer(processor);
const int cycles = 5;
for (int i = 0; i < cycles; i++)
{
await consumer.Start();
ep.TrackedChannelCount.Should().BeGreaterThanOrEqualTo(1,
$"cycle {i}: the running consumer holds its consume channel");
await consumer.Stop();
ep.TrackedChannelCount.Should().Be(0,
$"cycle {i}: the consumer must release its channel on Stop (no leak)");
}
await ep.Stop();
ep.TrackedChannelCount.Should().Be(0);
}
}