Path: redb-route/tests/redb.Route.Tests.AzureServiceBus/AzureServiceBusBatchOptionsTests.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/AzureServiceBusBatchOptionsTests.cs
----
using Azure.Messaging.ServiceBus;
using redb.Route.Abstractions;
using redb.Route.AzureServiceBus;
using redb.Route.AzureServiceBus.Fluent;
using redb.Route.Core;
namespace redb.Route.Tests.AzureServiceBus;
/// The Service Bus batch options on the core contract, every start refusal of batch mode, and lock renewal.
public sealed class AzureServiceBusBatchOptionsTests
{
private const string Connection = "connectionString=Endpoint=sb://localhost;SharedAccessKeyName=k;SharedAccessKey=v";
private static AzureServiceBusEndpoint Endpoint(string query)
=> (AzureServiceBusEndpoint)new AzureServiceBusComponent().CreateEndpoint(
EndpointUriParser.Parse($"asb://orders?{Connection}&{query}"));
private static AzureServiceBusEndpointOptions Resolved(string query)
{
var options = new AzureServiceBusEndpointOptions();
options.BindFromUri(EndpointUriParser.Parse($"asb://orders?{Connection}&{query}").RawParameters);
options.ResolveBatch("asb://orders");
return options;
}
[Fact]
public void Batch_mode_is_off_by_default_and_on_with_batching()
{
Resolved("ackMode=manual").Batch.Should().BeNull();
var batch = Resolved("batching=true").Batch!;
batch.MaxMessages.Should().Be(100);
batch.PollTimeout.Should().Be(TimeSpan.FromSeconds(1));
batch.Interval.Should().BeNull();
var tuned = Resolved("batching=true&maxBatchMessages=20&pollTimeoutMs=250&batchingIntervalMs=4000").Batch!;
tuned.MaxMessages.Should().Be(20);
tuned.PollTimeout.Should().Be(TimeSpan.FromMilliseconds(250));
tuned.Interval.Should().Be(TimeSpan.FromSeconds(4));
}
[Fact]
public void Batch_mode_creates_the_batch_consumer()
=> Endpoint("batching=true").CreateConsumer(Substitute.For()).Should().BeOfType();
[Theory]
[InlineData("maxBatchMessages=10", "*sets maxBatchMessages without batching=true*")]
[InlineData("batchingIntervalMs=10", "*sets batchingIntervalMs without batching=true*")]
[InlineData("batching=true&enableSessions=true", "*batching=true cannot be combined with enableSessions=true*")]
[InlineData("batching=true&maxBatchMessages=0", "*maxBatchMessages must be at least 1*")]
public void Batch_mode_that_cannot_work_fails_at_endpoint_creation(string query, string message)
{
var act = () => Endpoint(query);
act.Should().Throw().WithMessage(message);
}
[Fact]
public void The_builder_writes_the_batch_options()
{
var uri = Asb.Queue("orders").ConnectionString("Endpoint=sb://test").Batching().MaxBatchMessages(20)
.PollTimeoutMs(250).BatchingIntervalMs(4000).Build();
uri.Should().Contain("batching=True").And.Contain("maxBatchMessages=20").And.Contain("pollTimeoutMs=250")
.And.Contain("batchingIntervalMs=4000");
}
[Fact]
public async Task The_locks_of_a_batch_are_renewed_while_the_route_runs_and_no_longer_after_it()
{
var consumer = (AzureServiceBusBatchConsumer)Endpoint("batching=true&maxAutoLockRenewalDuration=60")
.CreateConsumer(Substitute.For());
var receiver = Substitute.For();
var renewals = 0;
receiver.RenewMessageLockAsync(Arg.Any(), Arg.Any())
.Returns(_ => { Interlocked.Increment(ref renewals); return Task.CompletedTask; });
var items = new[]
{
ServiceBusModelFactory.ServiceBusReceivedMessage(messageId: "a", lockedUntil: DateTimeOffset.UtcNow.AddSeconds(2)),
ServiceBusModelFactory.ServiceBusReceivedMessage(messageId: "b", lockedUntil: DateTimeOffset.UtcNow.AddSeconds(2)),
};
var renewal = new AzureServiceBusBatchConsumer.LockRenewal(consumer, receiver, items);
await Task.Delay(2500);
var whileRunning = Volatile.Read(ref renewals);
renewal.Dispose();
await Task.Delay(2500);
whileRunning.Should().BeGreaterThanOrEqualTo(2, "every lock of the batch is renewed half-way to its end");
Volatile.Read(ref renewals).Should().Be(whileRunning, "once the route returned, nothing is renewed");
}
}