Path: redb-route/tests/redb.Route.Tests/Batching/BatchCollectorTests.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/Batching/BatchCollectorTests.cs ---- using AwesomeAssertions; using redb.Route.Batching; namespace redb.Route.Tests.Batching; /// /// The batch collection contract: size, idle timeout restarted by every message, hard interval from the first /// message, and a stop mid-batch. The transport is scripted on a clock of its own, so no test waits. /// public class BatchCollectorTests { /// A clock the scripted transport moves. private sealed class ManualTime : TimeProvider { public DateTimeOffset Now { get; set; } = new(2026, 11, 17, 12, 0, 0, TimeSpan.Zero); public override DateTimeOffset GetUtcNow() => Now; } /// /// Messages that arrive at given offsets from the start. A receive call takes the arrivals due within its wait, up to /// perCall of them (1 like Kafka's poll, 10 like SQS), moving the clock to the last one taken, or moves the /// clock by the whole wait when none is due. /// private sealed class ScriptedTransport(ManualTime time, int perCall, params int[] arrivalsMs) { private readonly DateTimeOffset _start = time.Now; private int _next; public List Waits { get; } = []; public ValueTask Receive(List into, int max, TimeSpan wait, CancellationToken ct) { ct.ThrowIfCancellationRequested(); Waits.Add(wait); var until = time.Now + wait; var added = 0; while (added < Math.Min(max, perCall) && _next < arrivalsMs.Length && _start.AddMilliseconds(arrivalsMs[_next]) <= until) { var at = _start.AddMilliseconds(arrivalsMs[_next]); if (at > time.Now) time.Now = at; into.Add(_next++); added++; } if (added == 0) time.Now = until; return ValueTask.FromResult(added); } } private static BatchCollector Collector(ManualTime time, ScriptedTransport transport, int max, int pollMs, int? intervalMs = null) => new(new BatchOptions(max, TimeSpan.FromMilliseconds(pollMs), intervalMs is { } i ? TimeSpan.FromMilliseconds(i) : null), transport.Receive, time); [Fact] public async Task Collect_StopsAtMaxMessages() { var time = new ManualTime(); var transport = new ScriptedTransport(time, 1, 0, 10, 20, 30, 40); var batch = await Collector(time, transport, max: 3, pollMs: 1000).CollectAsync(CancellationToken.None); batch.Items.Should().Equal(0, 1, 2); batch.Interrupted.Should().BeFalse(); } [Fact] public async Task Collect_SeveralPerCall_NeverTakesMoreThanMax() { var time = new ManualTime(); var transport = new ScriptedTransport(time, 10, 0, 0, 0, 0, 0, 0, 0); var batch = await Collector(time, transport, max: 4, pollMs: 1000).CollectAsync(CancellationToken.None); batch.Items.Should().Equal(0, 1, 2, 3); } [Fact] public async Task Collect_IdleTimeout_RestartsWithEveryMessage() { // Messages 400 ms apart under a 500 ms idle timeout keep the batch open; the 900 ms gap closes it. var time = new ManualTime(); var transport = new ScriptedTransport(time, 1, 0, 400, 800, 1200, 2100); var batch = await Collector(time, transport, max: 100, pollMs: 500).CollectAsync(CancellationToken.None); batch.Items.Should().Equal(0, 1, 2, 3); (time.Now - new DateTimeOffset(2026, 11, 17, 12, 0, 0, TimeSpan.Zero)).Should().Be(TimeSpan.FromMilliseconds(1700)); } [Fact] public async Task Collect_Interval_IsAHardLimitFromTheFirstMessage() { // A steady stream never goes idle; the 1000 ms interval from the first message closes the batch. var time = new ManualTime(); var arrivals = Enumerable.Range(0, 50).Select(i => 300 + i * 100).ToArray(); var transport = new ScriptedTransport(time, 1, arrivals); var batch = await Collector(time, transport, max: 100, pollMs: 500, intervalMs: 1000).CollectAsync(CancellationToken.None); batch.Items.Should().HaveCount(11, "the messages at 300, 400, ... 1300 ms fall within 1000 ms of the first"); transport.Waits.Should().OnlyContain(w => w <= TimeSpan.FromMilliseconds(1000)); } [Fact] public async Task Collect_NothingWithinPollTimeout_ReturnsAnEmptyBatch() { var time = new ManualTime(); var transport = new ScriptedTransport(time, 1, 5000); var batch = await Collector(time, transport, max: 10, pollMs: 1000).CollectAsync(CancellationToken.None); batch.Items.Should().BeEmpty(); batch.Interrupted.Should().BeFalse(); transport.Waits.Should().Equal(TimeSpan.FromMilliseconds(1000)); } [Fact] public async Task Collect_StopMidBatch_ReturnsWhatItHas_Interrupted() { var time = new ManualTime(); using var stop = new CancellationTokenSource(); var received = 0; BatchReceive receive = (into, _, _, ct) => { ct.ThrowIfCancellationRequested(); into.Add(received++); if (received == 2) stop.Cancel(); return ValueTask.FromResult(1); }; var batch = await new BatchCollector(new BatchOptions(10, TimeSpan.FromSeconds(1)), receive, time).CollectAsync(stop.Token); batch.Items.Should().Equal(0, 1); batch.Interrupted.Should().BeTrue(); } [Fact] public async Task Collect_StopWhileWaitingForTheFirst_IsNotInterrupted() { using var stop = new CancellationTokenSource(); BatchReceive receive = async (_, _, _, ct) => { await stop.CancelAsync(); ct.ThrowIfCancellationRequested(); return 0; }; var batch = await new BatchCollector(new BatchOptions(10, TimeSpan.FromSeconds(1)), receive).CollectAsync(stop.Token); batch.Items.Should().BeEmpty(); batch.Interrupted.Should().BeFalse("nothing was taken from the broker, so nothing is given back"); } }