Path: redb-route/tests/redb.Route.Tests.Sqs/SqsTransactedSendTests.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/SqsTransactedSendTests.cs
----
using Amazon.Runtime;
using Amazon.SQS;
using Amazon.SQS.Model;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Processors;
using redb.Route.Sqs;
using redb.Route.Transactions;
using Message = redb.Route.Core.Message;
using SnsDsl = redb.Route.Sqs.Fluent.Sns;
using SqsDsl = redb.Route.Sqs.Fluent.Sqs;
namespace redb.Route.Tests.Sqs;
///
/// SQS sends and SNS publishes inside .Transacted(): unset, they join the block and leave only after the database
/// commits; transacted=false sends at once; transacted=true outside a block is refused.
///
[Trait("Category", "Integration")]
public sealed class SqsTransactedSendTests
{
private const string ServiceUrl = "http://localhost:4566";
private const string Region = "us-east-1";
private static string UniqueName(string prefix) => $"{prefix}-{Guid.NewGuid():N}";
private static IAmazonSQS RawSqs() =>
new AmazonSQSClient(new BasicAWSCredentials("test", "test"),
new AmazonSQSConfig { ServiceURL = ServiceUrl, AuthenticationRegion = Region });
private static IProducer SqsProducer(string queue, bool? transacted) => SqsEndpoint(queue, transacted).CreateProducer();
private static IEndpoint SqsEndpoint(string queue, bool? transacted)
{
var builder = SqsDsl.Queue(queue).ServiceUrl(ServiceUrl).Region(Region).Credentials("test", "test").AutoCreateQueue();
if (transacted is { } value) builder = builder.Transacted(value);
return new SqsComponent().CreateEndpoint(EndpointUriParser.Parse(builder.Build()));
}
private static Task InTransaction(IExchange exchange, Func body) =>
new TransactedProcessor(new DelegateProcessor(body), new TransactionPolicy()).Process(exchange);
private static async Task SendFromAFailedBlock(IProducer producer, string body)
{
await producer.Start();
var failed = () => InTransaction(new Exchange(new Message(body)), async (ex, ct) =>
{
await producer.Process(ex, ct);
throw new InvalidOperationException("the unit of work failed");
});
await failed.Should().ThrowAsync();
await producer.Stop();
}
private static async Task> ReceiveAll(IAmazonSQS sqs, string queueUrl)
{
var response = await sqs.ReceiveMessageAsync(new ReceiveMessageRequest
{
QueueUrl = queueUrl,
MaxNumberOfMessages = 10,
WaitTimeSeconds = 3,
});
return (response.Messages ?? []).Select(m => m.Body).ToList();
}
[Fact]
public async Task Sqs_send_without_transacted_joins_the_block_and_leaves_nothing_when_it_fails()
{
var queue = UniqueName("tx-join");
using var sqs = RawSqs();
var queueUrl = (await sqs.CreateQueueAsync(queue)).QueueUrl;
await SendFromAFailedBlock(SqsProducer(queue, transacted: null), "sent-from-a-failed-block");
(await ReceiveAll(sqs, queueUrl)).Should().BeEmpty(
"inside .Transacted() a send joins the transaction unless transacted=false opts out");
}
[Fact]
public async Task Sqs_send_with_transacted_false_goes_out_at_once_even_from_a_failed_block()
{
var queue = UniqueName("tx-optout");
using var sqs = RawSqs();
var queueUrl = (await sqs.CreateQueueAsync(queue)).QueueUrl;
await SendFromAFailedBlock(SqsProducer(queue, transacted: false), "sent-from-a-failed-block");
(await ReceiveAll(sqs, queueUrl)).Should().Equal("sent-from-a-failed-block");
}
[Fact]
public async Task Sqs_send_inside_a_block_goes_out_after_the_commit()
{
var queue = UniqueName("tx-commit");
using var sqs = RawSqs();
var queueUrl = (await sqs.CreateQueueAsync(queue)).QueueUrl;
var endpoint = SqsEndpoint(queue, transacted: null);
var producer = endpoint.CreateProducer();
await producer.Start();
List? insideTheBlock = null;
long bytesOutInsideTheBlock = -1;
await InTransaction(new Exchange(new Message("committed")), async (ex, ct) =>
{
await producer.Process(ex, ct);
insideTheBlock = await ReceiveAll(sqs, queueUrl);
bytesOutInsideTheBlock = ((IEndpointStatistics)endpoint).BytesOut;
});
insideTheBlock.Should().BeEmpty("nothing leaves before the database has committed");
bytesOutInsideTheBlock.Should().Be(0, "a deferred send counts when it goes out, after the commit");
((IEndpointStatistics)endpoint).BytesOut.Should().Be(9, "the committed send counts its UTF-8 body");
(await ReceiveAll(sqs, queueUrl)).Should().Equal("committed");
await producer.Stop();
}
[Fact]
public async Task Sqs_send_with_transacted_true_outside_a_block_is_refused()
{
var producer = SqsProducer(UniqueName("tx-none"), transacted: true);
await producer.Start();
var act = () => producer.Process(new Exchange(new Message("nobody would commit this")));
await act.Should().ThrowAsync().WithMessage("*Transacted()*");
await producer.Stop();
}
[Fact]
public async Task Sns_publish_without_transacted_joins_the_block_and_leaves_nothing_when_it_fails()
{
var (sqs, queueUrl, producer, _) = await SnsSubscribedToAQueue(transacted: null);
using (sqs)
{
await SendFromAFailedBlock(producer, "published-from-a-failed-block");
(await ReceiveAll(sqs, queueUrl)).Should().BeEmpty(
"inside .Transacted() a publish joins the transaction unless transacted=false opts out");
}
}
[Fact]
public async Task Sns_publish_with_transacted_false_goes_out_at_once_even_from_a_failed_block()
{
var (sqs, queueUrl, producer, endpoint) = await SnsSubscribedToAQueue(transacted: false);
using (sqs)
{
await SendFromAFailedBlock(producer, "published-from-a-failed-block");
(await ReceiveAll(sqs, queueUrl)).Should().ContainSingle().Which.Should().Be("published-from-a-failed-block");
((IEndpointStatistics)endpoint).BytesOut.Should().Be(29, "a publish that went out at once counts its UTF-8 body");
}
}
[Fact]
public async Task Sqs_consumer_does_not_delete_a_rolled_back_unit_of_work()
{
var queue = UniqueName("tx-rbonly");
using var sqs = RawSqs();
var queueUrl = (await sqs.CreateQueueAsync(queue)).QueueUrl;
await sqs.SendMessageAsync(queueUrl, "rolled-back");
// What .RollbackAll() does inside .Transacted(): mark the unit of work rollback-only and stop the route.
var deliveries = 0;
var deliveredAgain = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
var route = new TransactedProcessor(new DelegateProcessor(ex =>
{
if (Interlocked.Increment(ref deliveries) >= 2) deliveredAgain.TrySetResult();
ex.MarkRollbackOnly();
ex.Stop();
}), new TransactionPolicy());
var builder = SqsDsl.Queue(queue).ServiceUrl(ServiceUrl).Region(Region).Credentials("test", "test")
.WaitTimeSeconds(1).ResetVisibilityOnFailure();
var consumer = new SqsComponent().CreateEndpoint(EndpointUriParser.Parse(builder.Build())).CreateConsumer(route);
await consumer.Start();
await Task.WhenAny(deliveredAgain.Task, Task.Delay(15_000));
await consumer.Stop();
deliveries.Should().BeGreaterThanOrEqualTo(2,
"a rolled-back unit of work is not deleted, so the queue delivers the message again");
}
private static async Task<(IAmazonSQS Sqs, string QueueUrl, IProducer Producer, IEndpoint Endpoint)> SnsSubscribedToAQueue(bool? transacted)
{
var sqs = RawSqs();
var queueUrl = (await sqs.CreateQueueAsync(UniqueName("snsq"))).QueueUrl;
var arn = (await sqs.GetQueueAttributesAsync(new GetQueueAttributesRequest
{
QueueUrl = queueUrl,
AttributeNames = ["QueueArn"],
})).Attributes["QueueArn"];
var builder = SnsDsl.Topic(UniqueName("evt"))
.ServiceUrl(ServiceUrl).Region(Region).Credentials("test", "test")
.AutoCreateTopic().SubscribeSnsToSqs(arn).RawMessageDelivery();
if (transacted is { } value) builder = builder.Transacted(value);
var endpoint = new SnsComponent().CreateEndpoint(EndpointUriParser.Parse(builder.Build()));
var producer = endpoint.CreateProducer();
return (sqs, queueUrl, producer, endpoint);
}
}