Path: redb-route/tests/redb.Route.Tests.Sql/Batch/BatchSourceTests.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.Sql/Batch/BatchSourceTests.cs
----
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Xml;
using redb.Route.Core;
using redb.Route.Sql;
using redb.Route.Tests.Sql.Batch.Fakes;
using redb.Route.Tests.Sql.E2E.Infrastructure;
namespace redb.Route.Tests.Sql.Batch;
///
/// Which message bodies are batch sources, and how a streamed source is read: at most one chunk ahead of the writes, no
/// connection for an empty stream, the source's own failure ending the batch.
///
public sealed class BatchSourceTests : IDisposable
{
private const string Insert = "INSERT INTO source_log (val) VALUES (:#val)";
private readonly SqliteTestHelper _db = new();
public BatchSourceTests() => _db.Execute("CREATE TABLE source_log (val TEXT)");
public void Dispose() => _db.Dispose();
[Fact]
public async Task Batch_ByteArrayBody_IsNotExpandedIntoItems()
{
var exchange = new Exchange(new Message(new byte[] { 1, 2, 3 }));
exchange.In.Headers["val"] = "h";
await ProduceAsync(exchange);
Values().Should().Equal(new object?[] { "h" }, "a byte[] body is one value, not a list of batch items");
}
public static TheoryData ByteAndCharSequences() => new()
{
new ArraySegment([1, 2, 3]),
new List { 1, 2, 3 },
new[] { 'a', 'b', 'c' },
new ReadOnlyMemory([1, 2, 3]),
};
[Theory]
[MemberData(nameof(ByteAndCharSequences))]
public async Task Batch_ByteOrCharSequenceBody_IsNotExpandedIntoItems(object body)
{
var exchange = new Exchange(new Message(body));
exchange.In.Headers["val"] = "h";
var thrown = await ProduceAsync(exchange);
thrown.Should().BeNull(Outcome.Describe(thrown));
Values().Should().Equal(new object?[] { "h" }, $"a {body.GetType().Name} is one value like a byte[], not a list of batch items");
}
// ── Bodies that are batch sources ───────────────────────────────────────
[Fact]
public async Task Source_EnumerableNotList_IsBatched()
{
var body = Enumerable.Range(1, 3).Select(i => new Dictionary { ["val"] = "v" + i });
var thrown = await ProduceAsync(new Exchange(new Message(body)));
thrown.Should().BeNull(Outcome.Describe(thrown));
Values().Should().Equal(new object?[] { "v1", "v2", "v3" }, "a lazy sequence is a batch source like a list");
}
[Fact]
public async Task Source_AsyncEnumerableOfReferenceType_IsBatched()
{
var source = new CountingAsyncSource>(
[new() { ["val"] = "a" }, new() { ["val"] = "b" }, new() { ["val"] = "c" }]);
var thrown = await ProduceAsync(new Exchange(new Message(source)));
thrown.Should().BeNull(Outcome.Describe(thrown));
Values().Should().Equal(new object?[] { "a", "b", "c" });
source.Disposed.Should().BeTrue("the connector disposes the enumerator it opened");
}
[Fact]
public async Task Source_JsonElementArray_IsBatched()
{
using var document = JsonDocument.Parse("""[{"val":"a"},{"val":"b"}]""");
var thrown = await ProduceAsync(new Exchange(new Message(document.RootElement.Clone())));
thrown.Should().BeNull(Outcome.Describe(thrown));
Values().Should().Equal(new object?[] { "a", "b" }, "unmarshalling a JSON array to object gives a JsonElement array");
}
[Fact]
public async Task Source_JsonDocumentArray_IsBatched()
{
using var document = JsonDocument.Parse("""[{"val":"a"},{"val":"b"}]""");
var thrown = await ProduceAsync(new Exchange(new Message(document)));
thrown.Should().BeNull(Outcome.Describe(thrown));
Values().Should().Equal(new object?[] { "a", "b" });
}
[Fact]
public async Task Source_JsonArray_IsBatched()
{
var array = JsonNode.Parse("""[{"val":"a"},{"val":"b"}]""")!.AsArray();
var thrown = await ProduceAsync(new Exchange(new Message(array)));
thrown.Should().BeNull(Outcome.Describe(thrown));
Values().Should().Equal(new object?[] { "a", "b" });
}
// ── Bodies that stay a single statement ────────────────────────────────
[Fact]
public async Task Source_AsyncEnumerableOfValueType_UsesSingleStatementPath()
{
var source = new CountingAsyncSource([1, 2, 3]);
var exchange = new Exchange(new Message(source));
exchange.In.Headers["val"] = "h";
var thrown = await ProduceAsync(exchange);
thrown.Should().BeNull(Outcome.Describe(thrown));
Values().Should().Equal(new object?[] { "h" }, "IAsyncEnumerable is not IAsyncEnumerable: covariance needs reference types");
source.ItemsRead.Should().Be(0);
}
[Fact]
public async Task Source_StringDictionaryJsonObjectXmlDocument_NotBatched()
{
var xml = new XmlDocument();
xml.LoadXml("|
|
");
var bodies = new object[]
{
"text",
new Dictionary { ["val"] = "dictionary" },
JsonNode.Parse("""{"val":"json-object"}""")!.AsObject(),
xml,
};
foreach (var body in bodies)
{
var exchange = new Exchange(new Message(body));
exchange.In.Headers.TryAdd("val", "header");
if (body is IDictionary or JsonObject)
exchange.In.Headers.Remove("val");
var thrown = await ProduceAsync(exchange);
thrown.Should().BeNull($"{body.GetType().Name}: {Outcome.Describe(thrown)}");
}
Values().Should().Equal(new object?[] { "header", "dictionary", "json-object", "header" },
"a string, a map and an XML document are each one value: one statement per body");
}
// ── Streaming ───────────────────────────────────────────────────────────
[Fact]
public async Task Stream_EmptyAsyncSource_OpensNoConnection()
{
var connection = new FakeBatchConnection();
var factory = new FixedConnectionFactory(connection);
var exchange = new Exchange(new Message(new CountingAsyncSource>([])));
var thrown = await ProduceOnFakeAsync(factory, exchange, batchSize: 10);
thrown.Should().BeNull(Outcome.Describe(thrown));
factory.Requests.Should().Be(0, "an empty stream writes nothing, so no connection is opened");
exchange.In.Headers[SqlHeaders.UpdateCount].Should().Be(0);
}
[Fact]
public async Task Stream_DbBatch_ReadsAtMostOneChunkAheadOfWrites()
{
var source = new CountingAsyncSource>(Items(10));
var readAtEachRoundTrip = new List();
var connection = new FakeBatchConnection
{
SupportsBatch = true,
OnExecuteBatch = (_, batch) =>
{
readAtEachRoundTrip.Add(source.ItemsRead);
return batch.BatchCommands.Count;
},
};
var thrown = await ProduceOnFakeAsync(new FixedConnectionFactory(connection), new Exchange(new Message(source)), batchSize: 3);
thrown.Should().BeNull(Outcome.Describe(thrown));
readAtEachRoundTrip.Should().Equal(new[] { 3, 6, 9, 10 }, "a chunk is read, then written, before the next one is read");
}
[Fact]
public async Task Stream_Commands_ReadsOneItemAheadOfWrites()
{
var source = new CountingAsyncSource>(Items(4));
var readAtEachStatement = new List();
var connection = new FakeBatchConnection
{
OnExecute = (_, _) =>
{
readAtEachStatement.Add(source.ItemsRead);
return 1;
},
};
var thrown = await ProduceOnFakeAsync(new FixedConnectionFactory(connection), new Exchange(new Message(source)), batchSize: 3);
thrown.Should().BeNull(Outcome.Describe(thrown));
readAtEachStatement.Should().Equal(1, 2, 3, 4);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task Stream_SourceThrows_RollsBackAndRethrowsSourceException(bool breakOnError)
{
var source = new CountingAsyncSource>(Items(10), failAfter: 4);
var connection = new FakeBatchConnection { Savepoints = SavepointBehavior.Works };
var exchange = new Exchange(new Message(source));
var thrown = await ProduceOnFakeAsync(new FixedConnectionFactory(connection), exchange, batchSize: 3, breakOnError);
thrown.Should().BeOfType("the source's own failure ends the batch in either mode");
thrown!.Data[SqlHeaders.BatchFailedIndex].Should().Be(4, "four items were read before the source failed");
connection.Transaction!.Committed.Should().BeFalse();
connection.Transaction.RolledBack.Should().BeTrue();
source.Disposed.Should().BeTrue();
}
[Fact]
public async Task Stream_SourceThrowsAfterCancellation_IsNotReportedAsASourceFailure()
{
using var cts = new CancellationTokenSource();
// A streamed source whose driver answers the stop with its own exception (SQL Server: "Operation cancelled by user").
var source = new CancelThenThrowSource(cts);
var connection = new FakeBatchConnection();
var exchange = new Exchange(new Message(source));
var thrown = await ProduceOnFakeAsync(new FixedConnectionFactory(connection), exchange, batchSize: 3, ct: cts.Token);
thrown.Should().BeOfType(Outcome.Describe(thrown));
thrown!.Data.Contains(SqlHeaders.BatchFailedIndex).Should().BeFalse("under a cancellation the writers do not report an item, and neither does the source");
exchange.In.Headers.Should().NotContainKey(SqlHeaders.BatchFailedIndex);
connection.Transaction!.RolledBack.Should().BeTrue();
}
/// Yields one item; on the next read cancels the token and throws as a driver would.
private sealed class CancelThenThrowSource(CancellationTokenSource cts) : IAsyncEnumerable>
{
public IAsyncEnumerator> GetAsyncEnumerator(CancellationToken cancellationToken = default) =>
new Enumerator(cts);
private sealed class Enumerator(CancellationTokenSource cts) : IAsyncEnumerator>
{
private int _reads;
public Dictionary Current { get; } = new() { ["val"] = "v0" };
public ValueTask MoveNextAsync()
{
if (_reads++ == 0)
return ValueTask.FromResult(true);
cts.Cancel();
throw new FakeSourceException("fake: the source was interrupted by the stop");
}
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
}
}
[Fact]
public async Task Stream_Cancellation_DisposesEnumerator()
{
using var cts = new CancellationTokenSource();
var source = new CountingAsyncSource>(Items(10));
var connection = new FakeBatchConnection
{
OnExecute = (index, _) =>
{
if (index == 1)
{
cts.Cancel();
throw new OperationCanceledException(cts.Token);
}
return 1;
},
};
var thrown = await ProduceOnFakeAsync(new FixedConnectionFactory(connection), new Exchange(new Message(source)),
batchSize: 3, ct: cts.Token);
thrown.Should().BeAssignableTo(Outcome.Describe(thrown));
source.Disposed.Should().BeTrue("the enumerator is released when the batch is cancelled");
}
// ── Helpers ─────────────────────────────────────────────────────────────
private static List> Items(int count) =>
Enumerable.Range(0, count).Select(i => new Dictionary { ["val"] = "v" + i }).ToList();
private async Task ProduceAsync(Exchange exchange)
{
await using var context = new RouteContext();
var endpoint = SqlEndpointHarness.CreateEndpoint(context, _db.CreateFactory(), Insert, new()
{
["outputType"] = "None",
["batchSize"] = "10",
});
return await Outcome.Of(() => endpoint.CreateProducer().Process(exchange, CancellationToken.None));
}
private static async Task ProduceOnFakeAsync(
FixedConnectionFactory factory, Exchange exchange, int batchSize, bool breakOnError = true, CancellationToken ct = default)
{
await using var context = new RouteContext();
var endpoint = SqlEndpointHarness.CreateEndpoint(context, factory, Insert, new()
{
["outputType"] = "None",
["batchSize"] = batchSize.ToString(),
["breakBatchOnError"] = breakOnError ? "true" : "false",
});
return await Outcome.Of(() => endpoint.CreateProducer().Process(exchange, ct));
}
private List Values() => _db.Query("SELECT val FROM source_log ORDER BY rowid").Select(r => r["val"]).ToList();
}