Path: redb-route/tests/redb.Route.Tests.Amqp/AmqpPoisonMessageTests.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/AmqpPoisonMessageTests.cs ---- using System.Text; using Amqp; using Amqp.Framing; using redb.Route.Abstractions; using redb.Route.Amqp; using redb.Route.Core; using AmqpMessage = global::Amqp.Message; namespace redb.Route.Tests.Amqp; /// /// A message the route always fails is bounded by the broker: the consumer gives a failed delivery back as a failed /// one (modified with delivery-failed), the broker counts each attempt, and at its maximum delivery count /// (Artemis: max-delivery-attempts, 10 by default) moves the message to its dead letter address. A /// released delivery is not counted, and the message came back without end. Single and batch. Expects ActiveMQ /// Artemis at localhost:5673 (admin/admin) with its default address settings (dead letter address DLQ). /// [Trait("Category", "Integration")] public sealed class AmqpPoisonMessageTests { private const string Broker = "host=localhost&port=5673&user=admin&password=admin"; private const string BrokerAddress = "amqp://admin:admin@localhost:5673"; [Theory] [InlineData("")] [InlineData("&batching=true&maxBatchMessages=10&pollTimeoutMs=200")] public async Task A_message_that_always_fails_reaches_the_brokers_delivery_limit_and_its_dead_letter_address(string extra) { var address = $"poison.{Guid.NewGuid():N}"; var body = $"poison-{Guid.NewGuid():N}"; await SendAsync(address, body); var deliveries = 0; var processor = Substitute.For(); processor.Process(Arg.Any(), Arg.Any()).Returns(call => { Interlocked.Increment(ref deliveries); throw new InvalidOperationException("the route fails every delivery"); }); var endpoint = (AmqpEndpoint)new AmqpComponent().CreateEndpoint(EndpointUriParser.Parse($"amqp://{address}?{Broker}{extra}")); var consumer = endpoint.CreateConsumer(processor); await consumer.Start(); try { // The count settles when the broker stops redelivering; a released message never lets it settle. var last = -1; var deadline = DateTime.UtcNow.AddSeconds(30); while (DateTime.UtcNow < deadline) { await Task.Delay(2000); var now = Volatile.Read(ref deliveries); if (now > 0 && now == last) break; last = now; } } finally { await consumer.Stop(); await endpoint.Stop(); } deliveries.Should().BeInRange(1, 20, "the broker stops redelivering at its maximum delivery count"); (await DeadLettered(body)).Should().BeTrue("the broker moves the message to its dead letter address"); } private static async Task SendAsync(string address, string body) { var connection = await Connection.Factory.CreateAsync(new Address(BrokerAddress)); try { var session = new Session(connection); var sender = new SenderLink(session, $"test-sender-{Guid.NewGuid():N}", new Target { Address = address, Capabilities = [new global::Amqp.Types.Symbol("queue")] }, null); await sender.SendAsync(new AmqpMessage(body) { Properties = new Properties { MessageId = Guid.NewGuid().ToString("N") } }); await sender.CloseAsync(); } finally { await connection.CloseAsync(); } } /// Looks for the body on the DLQ queue; takes it when found and leaves every other message there. private static async Task DeadLettered(string body) { var connection = await Connection.Factory.CreateAsync(new Address(BrokerAddress)); try { var session = new Session(connection); var receiver = new ReceiverLink(session, $"test-dlq-{Guid.NewGuid():N}", new Source { Address = "DLQ", Capabilities = [new global::Amqp.Types.Symbol("queue")] }, null); var deadline = DateTime.UtcNow.AddSeconds(10); var others = new List(); try { while (DateTime.UtcNow < deadline) { var msg = await receiver.ReceiveAsync(TimeSpan.FromSeconds(1)); if (msg is null) continue; var text = msg.Body as string ?? (msg.Body is byte[] b ? Encoding.UTF8.GetString(b) : msg.Body?.ToString()); if (text == body) { receiver.Accept(msg); return true; } others.Add(msg); } return false; } finally { foreach (var other in others) receiver.Release(other); await receiver.CloseAsync(); } } finally { await connection.CloseAsync(); } } }