Path: redb-route/tests/redb.Route.Tests.AzureServiceBus/AzureServiceBusBatchCommitTests.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.AzureServiceBus/AzureServiceBusBatchCommitTests.cs
----
using System.Collections.Concurrent;
using System.Text;
using redb.Route.Abstractions;
using redb.Route.AzureServiceBus;
using redb.Route.Core;
using redb.Route.Processors;
using redb.Route.Transactions;
namespace redb.Route.Tests.AzureServiceBus;
///
/// batchCommit=true: the sends a producer defers in a .Transacted() block leave as one Service Bus batch —
/// the entity takes it whole or not at all — and a block whose messages do not fit in one batch sends nothing. The batch
/// body no longer drops the items over its limits without a word.
///
[Trait("Category", "Integration")]
[Collection(AsbEmulatorQueue.Name)]
public sealed class AzureServiceBusBatchCommitTests
{
private const string ConnectionString =
"Endpoint=sb://localhost:5300;SharedAccessKeyName=RootManageSharedAccessKey;" +
"SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true;";
private const string Queue = "queue.1";
private static string Tag() => Guid.NewGuid().ToString("N")[..8];
private static AzureServiceBusEndpoint CreateEndpoint(string? extraParams = null)
{
var qs = $"connectionString={ConnectionString}";
if (extraParams is not null) qs += $"&{extraParams}";
return (AzureServiceBusEndpoint)new AzureServiceBusComponent().CreateEndpoint(EndpointUriParser.Parse($"asb://{Queue}?{qs}"));
}
private static Task InTransaction(IExchange exchange, Func body) =>
new TransactedProcessor(new DelegateProcessor(body), new TransactionPolicy()).Process(exchange);
/// Everything that reaches the queue during the window, taken off it for good.
private static async Task> ReceiveFor(TimeSpan window)
{
var endpoint = CreateEndpoint("ackMode=auto&maxConcurrentCalls=10");
var received = new ConcurrentBag();
var processor = Substitute.For();
processor.Process(Arg.Any(), Arg.Any())
.Returns(callInfo =>
{
received.Add(Encoding.UTF8.GetString((byte[])callInfo.Arg().In.Body!));
return Task.CompletedTask;
});
var consumer = endpoint.CreateConsumer(processor);
await consumer.Start();
await Task.Delay(window);
await consumer.Stop();
await endpoint.Stop();
return received;
}
[Fact]
public async Task The_sends_of_a_block_leave_as_one_batch()
{
var tag = Tag();
var endpoint = CreateEndpoint("batchCommit=true");
var producer = endpoint.CreateProducer();
await producer.Start();
await InTransaction(new Exchange(new Message($"{tag}-one")), async (ex, ct) =>
{
await producer.Process(ex, ct);
ex.In.Body = $"{tag}-two";
await producer.Process(ex, ct);
});
await producer.Stop();
await endpoint.Stop();
(await ReceiveFor(TimeSpan.FromSeconds(3))).Where(b => b.StartsWith(tag)).Order()
.Should().Equal($"{tag}-one", $"{tag}-two");
}
[Fact]
public async Task A_block_whose_messages_do_not_fit_in_one_batch_sends_nothing()
{
var tag = Tag();
var endpoint = CreateEndpoint("batchCommit=true&batchMaxSizeBytes=2048");
var producer = endpoint.CreateProducer();
await producer.Start();
var block = () => InTransaction(new Exchange(new Message($"{tag}-small")), async (ex, ct) =>
{
await producer.Process(ex, ct);
ex.In.Body = tag + new string('x', 3_000);
await producer.Process(ex, ct);
});
await block.Should().ThrowAsync().WithMessage("*do not fit in one Service Bus batch*");
await producer.Stop();
await endpoint.Stop();
(await ReceiveFor(TimeSpan.FromSeconds(3))).Where(b => b.StartsWith(tag))
.Should().BeEmpty("the small message is not sent without the one that did not fit");
}
[Fact]
public async Task A_list_body_over_batchMaxSizeBytes_fails_instead_of_dropping_the_rest()
{
var tag = Tag();
var endpoint = CreateEndpoint("batchMaxSizeBytes=2048");
var producer = endpoint.CreateProducer();
await producer.Start();
var big = new string('x', 1_500);
var send = () => producer.Process(new Exchange(new Message(new[] { $"{tag}-{big}", $"{tag}-{big}" })));
await send.Should().ThrowAsync().WithMessage("*do not fit in one Service Bus batch*");
await producer.Stop();
await endpoint.Stop();
(await ReceiveFor(TimeSpan.FromSeconds(3))).Where(b => b.Contains(tag)).Should().BeEmpty();
}
[Fact]
public void BatchCommit_contradicts_transacted_false()
{
var act = () => CreateEndpoint("batchCommit=true&transacted=false");
act.Should().Throw().WithMessage("*batchCommit*transacted=false*");
}
}