Path: redb-route/tests/redb.Route.Tests.Sql/E2E/Infrastructure/RecordingConnectionFactory.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/E2E/Infrastructure/RecordingConnectionFactory.cs
----
using System.Data;
using System.Data.Common;
using redb.Route.Sql.Connection;
namespace redb.Route.Tests.Sql.E2E.Infrastructure;
///
/// over , so connector connections get the
/// provider's session settings. Records the read-only hint of every request so routing decisions can be asserted, and counts
/// the connections it handed out that are still open, so a test can see a connection that was never returned.
///
public sealed class RecordingConnectionFactory : ISqlConnectionFactory
{
private readonly SqlE2EDatabase _database;
private readonly List _readOnlyRequests = [];
private int _openConnections;
internal RecordingConnectionFactory(SqlE2EDatabase database) => _database = database;
/// The readOnly argument of every request, in order.
public IReadOnlyList ReadOnlyRequests
{
get { lock (_readOnlyRequests) return [.. _readOnlyRequests]; }
}
/// Connections handed out and not closed yet.
public int OpenConnections => Volatile.Read(ref _openConnections);
///
public async Task CreateConnectionAsync(bool readOnly = false, CancellationToken ct = default)
{
lock (_readOnlyRequests) _readOnlyRequests.Add(readOnly);
var connection = await _database.OpenAsync(ct);
Interlocked.Increment(ref _openConnections);
connection.StateChange += (_, change) =>
{
if (change.OriginalState == ConnectionState.Open && change.CurrentState == ConnectionState.Closed)
Interlocked.Decrement(ref _openConnections);
};
return connection;
}
}