Path: redb-route/tests/redb.Route.Tests.Sql/SqlProducerStreamListLifetimeTests.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/SqlProducerStreamListLifetimeTests.cs
----
using System.Data;
using System.Data.Common;
using System.Transactions;
using Microsoft.Data.Sqlite;
using redb.Route.Components;
using redb.Route.Core;
using redb.Route.Sql;
using redb.Route.Sql.Connection;
using redb.Route.Tests.Sql.E2E.Infrastructure;
using ConnectionState = System.Data.ConnectionState;
namespace redb.Route.Tests.Sql;
///
/// A streamed result (outputType=StreamList) holds a reader, its command, a transaction and a connection past the
/// endpoint. As in Apache Camel, they are released when the exchange ends, whether or not the route read the stream and
/// whatever became of the body; the stream reads once; inside a route transaction it is refused, because the transaction
/// cannot commit while the reader is open.
///
public sealed class SqlProducerStreamListLifetimeTests : IDisposable
{
private const string Select = "SELECT id, val FROM stream_items ORDER BY id";
private readonly SqliteTestHelper _db = new();
public SqlProducerStreamListLifetimeTests()
{
_db.Execute("CREATE TABLE stream_items (id INTEGER NOT NULL, val TEXT NOT NULL)");
_db.Execute("INSERT INTO stream_items (id, val) VALUES (1, 'a'), (2, 'b'), (3, 'c')");
}
public void Dispose() => _db.Dispose();
[Fact]
public async Task StreamList_NotConsumed_ExchangeDisposed_ConnectionClosed()
{
var factory = new TrackingFactory(_db.ConnectionString);
var exchange = await ProduceAsync(factory);
factory.Open.Should().Be(1, "the stream keeps its connection while the route runs");
await exchange.DisposeAsync();
factory.Open.Should().Be(0, "the exchange ended: the stream nobody read releases its connection");
}
[Fact]
public async Task StreamList_CopyOfTheExchangeDisposed_TheStreamStaysWithTheOriginal()
{
var factory = new TrackingFactory(_db.ConnectionString);
var exchange = await ProduceAsync(factory);
// WireTap, RecipientList, Enrich, Threads: a copy shares the body and is disposed when its own work is done.
var copy = exchange.CloneLinked();
copy.In.Body.Should().BeSameAs(exchange.In.Body);
await copy.DisposeAsync();
factory.Open.Should().Be(1, "the stream belongs to the exchange that registered it, not to a copy that shares the body");
var ids = new List