Path: redb-route/tests/redb.Route.Tests.Sqs/SqsBatchStopTests.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/SqsBatchStopTests.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; /// /// The SQS batch consumer's stop, against a client stub: a receive in flight is not abandoned (an abandoned long poll /// stays open at the queue and would take the messages the stop gives back); it runs to its end, and the stop gives /// the whole batch, its messages included, back before it returns. /// public sealed class SqsBatchStopTests { private const string QueueUrl = "http://sqs.local/000000000000/orders"; private static string Uri => $"sqs://orders?region=us-east-1&accessKey=t&secretKey=t&queueUrl={System.Uri.EscapeDataString(QueueUrl)}" + "&batching=true&maxBatchMessages=10&pollTimeoutMs=60000"; private static SqsMessage Msg(string id) => new() { MessageId = id, ReceiptHandle = "h" + id, Body = id }; /// /// A client whose first receive returns two messages and whose second blocks until release lets it return a /// third, the way a long poll answers when its message comes or its wait ends. /// private static IAmazonSQS Stub(SqsEndpoint endpoint, TaskCompletionSource inFlight, TaskCompletionSource release, List tokens) { var client = Substitute.For(); typeof(SqsEndpoint).GetField("_client", BindingFlags.Instance | BindingFlags.NonPublic)!.SetValue(endpoint, client); var calls = 0; client.ReceiveMessageAsync(Arg.Any(), Arg.Any()).Returns(async call => { lock (tokens) tokens.Add(call.Arg()); switch (Interlocked.Increment(ref calls)) { case 1: return new ReceiveMessageResponse { Messages = [Msg("1"), Msg("2")] }; case 2: inFlight.TrySetResult(); await release.Task; return new ReceiveMessageResponse { Messages = [Msg("3")] }; default: throw new InvalidOperationException("no receive after the stop"); } }); client.ChangeMessageVisibilityBatchAsync(Arg.Any(), Arg.Any()) .Returns(new ChangeMessageVisibilityBatchResponse { Successful = [], Failed = [] }); return client; } private static async Task ExpectAllGivenBack(IAmazonSQS client, List tokens) { await client.Received(1).ChangeMessageVisibilityBatchAsync( Arg.Is(r => r.Entries.Select(e => e.ReceiptHandle).SequenceEqual(new[] { "h1", "h2", "h3" }) && r.Entries.All(e => e.VisibilityTimeout == 0)), Arg.Any()); tokens.Should().OnlyContain(t => !t.CanBeCanceled, "a receive is never cancelled by the stop"); } [Fact] public async Task A_stop_during_collection_lets_the_receive_end_and_gives_every_collected_message_back() { var endpoint = (SqsEndpoint)new SqsComponent().CreateEndpoint(EndpointUriParser.Parse(Uri)); var inFlight = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var tokens = new List(); var client = Stub(endpoint, inFlight, release, tokens); var processor = Substitute.For(); var consumer = endpoint.CreateConsumer(processor); await consumer.Start(); await inFlight.Task.WaitAsync(TimeSpan.FromSeconds(10)); var stop = consumer.Stop(); await Task.Delay(200); stop.IsCompleted.Should().BeFalse("the stop waits for the receive in flight"); release.SetResult(); await stop.WaitAsync(TimeSpan.FromSeconds(10)); await processor.DidNotReceive().Process(Arg.Any(), Arg.Any()); await ExpectAllGivenBack(client, tokens); } [Fact] public async Task A_route_contexts_stop_during_collection_gives_every_collected_message_back() { var context = new RouteContext(); context.AddComponent(new SqsComponent()); var endpoint = (SqsEndpoint)context.GetEndpoint(Uri); var inFlight = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var tokens = new List(); var client = Stub(endpoint, inFlight, release, tokens); var routed = 0; context.AddRoutes(r => r.From(Uri).Process(_ => Interlocked.Increment(ref routed))); await context.Start(); await inFlight.Task.WaitAsync(TimeSpan.FromSeconds(10)); var stop = context.DisposeAsync().AsTask(); release.SetResult(); await stop.WaitAsync(TimeSpan.FromSeconds(30)); routed.Should().Be(0); await ExpectAllGivenBack(client, tokens); } }