Path: redb-route/tests/redb.Route.Tests.Sqs/SqsErrorDetailTests.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.Sqs/SqsErrorDetailTests.cs
----
using System.Reflection;
using Amazon.SQS;
using Amazon.SQS.Model;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Sqs;
using SqsMessage = Amazon.SQS.Model.Message;
namespace redb.Route.Tests.Sqs;
///
/// A transport failure the SQS connector records on its endpoint carries its text, so the dashboard's last error says
/// what went wrong: a failed receive, single and batch, a batch that failed to settle, the entries SQS refused in a
/// batch delete or a batch send. Against a client stub; no queue.
///
public sealed class SqsErrorDetailTests
{
private const string QueueUrl = "http://sqs.local/000000000000/orders";
private static readonly TimeSpan Wait = TimeSpan.FromSeconds(10);
private static string Uri(string extra = "") =>
$"sqs://orders?region=us-east-1&accessKey=t&secretKey=t&queueUrl={System.Uri.EscapeDataString(QueueUrl)}" +
"&pollTimeoutMs=1000" + extra;
private static IAmazonSQS Stub(SqsEndpoint endpoint)
{
var client = Substitute.For();
typeof(SqsEndpoint).GetField("_client", BindingFlags.Instance | BindingFlags.NonPublic)!.SetValue(endpoint, client);
return client;
}
/// The first receive throws ; later ones return nothing after a short wait.
private static void FailFirstReceive(IAmazonSQS client, Exception failure)
{
var calls = 0;
client.ReceiveMessageAsync(Arg.Any(), Arg.Any()).Returns(async _ =>
{
if (Interlocked.Increment(ref calls) == 1)
throw failure;
await Task.Delay(50);
return new ReceiveMessageResponse { Messages = [] };
});
}
/// The first receive returns one message; later ones return nothing after a short wait.
private static void OneMessage(IAmazonSQS client)
{
var calls = 0;
client.ReceiveMessageAsync(Arg.Any(), Arg.Any()).Returns(async _ =>
{
if (Interlocked.Increment(ref calls) == 1)
return new ReceiveMessageResponse { Messages = [new SqsMessage { MessageId = "m1", ReceiptHandle = "h1", Body = "b" }] };
await Task.Delay(50);
return new ReceiveMessageResponse { Messages = [] };
});
}
private static async Task LastErrorOf(SqsEndpoint endpoint, IConsumer consumer)
{
await consumer.Start();
try
{
var deadline = DateTime.UtcNow + Wait;
while (endpoint.Errors == 0 && DateTime.UtcNow < deadline)
await Task.Delay(20);
endpoint.Errors.Should().BeGreaterThan(0, "the failure is recorded on the endpoint");
return endpoint.LastErrorMessage;
}
finally
{
await consumer.Stop();
}
}
[Fact]
public async Task A_failed_receive_is_recorded_with_its_text()
{
var endpoint = (SqsEndpoint)new SqsComponent().CreateEndpoint(EndpointUriParser.Parse(Uri()));
FailFirstReceive(Stub(endpoint), new InvalidOperationException("receive refused by the test"));
var lastError = await LastErrorOf(endpoint, endpoint.CreateConsumer(Substitute.For()));
lastError.Should().Contain("receive refused by the test");
}
[Fact]
public async Task A_failed_batch_receive_is_recorded_with_its_text()
{
var endpoint = (SqsEndpoint)new SqsComponent().CreateEndpoint(EndpointUriParser.Parse(Uri("&batching=true&maxBatchMessages=10")));
FailFirstReceive(Stub(endpoint), new InvalidOperationException("batch receive refused by the test"));
var lastError = await LastErrorOf(endpoint, endpoint.CreateConsumer(Substitute.For()));
lastError.Should().Contain("batch receive refused by the test");
}
[Fact]
public async Task A_batch_that_failed_to_settle_is_recorded_with_its_text()
{
var endpoint = (SqsEndpoint)new SqsComponent().CreateEndpoint(EndpointUriParser.Parse(Uri("&batching=true&maxBatchMessages=10")));
var client = Stub(endpoint);
OneMessage(client);
client.DeleteMessageBatchAsync(Arg.Any(), Arg.Any())
.Returns(_ => throw new InvalidOperationException("delete failed in the test"));
var lastError = await LastErrorOf(endpoint, endpoint.CreateConsumer(Substitute.For()));
lastError.Should().Contain("delete failed in the test");
}
[Fact]
public async Task Entries_refused_in_a_batch_delete_are_recorded_with_their_code()
{
var endpoint = (SqsEndpoint)new SqsComponent().CreateEndpoint(EndpointUriParser.Parse(Uri("&batching=true&maxBatchMessages=10")));
var client = Stub(endpoint);
OneMessage(client);
client.DeleteMessageBatchAsync(Arg.Any(), Arg.Any())
.Returns(new DeleteMessageBatchResponse
{
Successful = [],
Failed = [new BatchResultErrorEntry { Id = "0", Code = "ReceiptHandleIsInvalid", Message = "handle expired", SenderFault = true }],
});
var lastError = await LastErrorOf(endpoint, endpoint.CreateConsumer(Substitute.For()));
lastError.Should().Contain("ReceiptHandleIsInvalid").And.Contain("m1");
}
[Fact]
public async Task Entries_refused_in_a_batch_send_are_recorded_with_their_code()
{
var endpoint = (SqsEndpoint)new SqsComponent().CreateEndpoint(EndpointUriParser.Parse(Uri()));
var client = Stub(endpoint);
client.SendMessageBatchAsync(Arg.Any(), Arg.Any())
.Returns(new SendMessageBatchResponse
{
Successful = [],
Failed = [new BatchResultErrorEntry { Id = "0", Code = "InvalidParameterValue", Message = "body too long", SenderFault = true }],
});
var producer = endpoint.CreateProducer();
await producer.Start();
await producer.Process(new Exchange(new redb.Route.Core.Message(new List { new redb.Route.Core.Message("a") })));
await producer.Stop();
endpoint.Errors.Should().Be(1);
endpoint.LastErrorMessage.Should().Contain("InvalidParameterValue").And.Contain("body too long");
}
}