test(data): cover sessions, SQLite binding and PostgreSQL wire client

Shared session tests run on SQLite and PostgreSQL (types, nulls,
placeholders, transactions, constraint errors, COPY/bulk escaping,
cancellation). PostgreSQL tests cover SCRAM/MD5/cleartext/trust auth,
TLS, result decoding, bytea, LISTEN/NOTIFY, error mapping, pooling and
broken-connection recovery, and the statement cache. In-memory SQLite
now pools shared-cache connections and retries SQLITE_LOCKED.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 09:50:31 +00:00
1 parent 46cee365dc
commit 14567ecac3
8 files changed
+1015 -22

No files matched your search

+3 -3
View File
@@ -26,7 +26,8 @@ internal sealed record SqliteSettings(string DataSource, int PoolSize)
/// <summary>
/// SQLite through the system library (BRIEF §6.2). Up to <c>Pool Size</c> connections serve concurrent
/// readers (WAL); writes are serialised in-process by one async write lock, so writers wait in a queue instead
/// of spinning on SQLITE_BUSY. In-memory databases use a single shared-cache connection.
/// of spinning on SQLITE_BUSY. An in-memory database is a shared-cache URI that lives while the pool holds a
/// connection; its table locks (SQLITE_LOCKED) are retried by the sessions.
/// </summary>
public sealed class SqliteDatabase : IDatabase
{
@@ -51,7 +52,6 @@ public sealed class SqliteDatabase : IDatabase
if (_memory)
{
_filename = $"file:titano-{Guid.NewGuid():N}?mode=memory&cache=shared";
_maxConnections = 1;
}
else
{
@@ -59,8 +59,8 @@ public sealed class SqliteDatabase : IDatabase
var directory = System.IO.Path.GetDirectoryName(Path);
if (!string.IsNullOrEmpty(directory)) Directory.CreateDirectory(directory);
_filename = Path;
_maxConnections = settings.PoolSize;
}
_maxConnections = settings.PoolSize;
_slots = new SemaphoreSlim(_maxConnections, _maxConnections);
}
+28 -19
View File
@@ -35,7 +35,7 @@ internal sealed class SqliteSession(SqliteDatabase database, SqliteConnection co
}
using var interrupt = RegisterInterrupt(cancellationToken);
var before = connection.TotalChanges;
while (await StepAsync(statement, _transaction is null, cancellationToken).ConfigureAwait(false) == SQLITE_ROW)
while (await StepAsync(statement, canRestart: true, cancellationToken).ConfigureAwait(false) == SQLITE_ROW)
{
}
// sqlite3_changes keeps the last DML count across DDL; only trust it when this statement changed rows.
@@ -64,10 +64,10 @@ internal sealed class SqliteSession(SqliteDatabase database, SqliteConnection co
}
using var interrupt = RegisterInterrupt(cancellationToken);
DbColumns? columns = null;
var retry = _transaction is null;
while (await StepAsync(statement, retry, cancellationToken).ConfigureAwait(false) == SQLITE_ROW)
var canRestart = true;
while (await StepAsync(statement, canRestart, cancellationToken).ConfigureAwait(false) == SQLITE_ROW)
{
retry = false; // restarting after rows were yielded would repeat them
canRestart = false; // restarting after rows were yielded would repeat them
columns ??= statement.ReadColumns();
yield return statement.ReadRow(columns);
cancellationToken.ThrowIfCancellationRequested();
@@ -108,7 +108,7 @@ internal sealed class SqliteSession(SqliteDatabase database, SqliteConnection co
await database.AcquireWriteAsync(cancellationToken).ConfigureAwait(false);
try
{
await RunAsync("BEGIN IMMEDIATE", retryBusy: true, cancellationToken).ConfigureAwait(false);
await RunAsync("BEGIN IMMEDIATE", cancellationToken).ConfigureAwait(false);
}
catch
{
@@ -188,7 +188,7 @@ internal sealed class SqliteSession(SqliteDatabase database, SqliteConnection co
if (_transaction != transaction) return;
try
{
await RunAsync(commit ? "COMMIT" : "ROLLBACK", retryBusy: commit, cancellationToken).ConfigureAwait(false);
await RunAsync(commit ? "COMMIT" : "ROLLBACK", cancellationToken).ConfigureAwait(false);
}
finally
{
@@ -225,7 +225,7 @@ internal sealed class SqliteSession(SqliteDatabase database, SqliteConnection co
try
{
for (var i = 0; i < rowCount * columns.Count; i++) statement.BindValue(i + 1, buffer[i]);
await StepAsync(statement, retryBusy: false, cancellationToken).ConfigureAwait(false);
await StepAsync(statement, canRestart: true, cancellationToken).ConfigureAwait(false);
}
finally
{
@@ -248,12 +248,12 @@ internal sealed class SqliteSession(SqliteDatabase database, SqliteConnection co
return sb.ToString();
}
private async Task RunAsync(string sql, bool retryBusy, CancellationToken cancellationToken)
private async Task RunAsync(string sql, CancellationToken cancellationToken)
{
var statement = connection.Rent(sql);
try
{
await StepAsync(statement, retryBusy, cancellationToken).ConfigureAwait(false);
await StepAsync(statement, canRestart: true, cancellationToken).ConfigureAwait(false);
}
finally
{
@@ -261,23 +261,32 @@ internal sealed class SqliteSession(SqliteDatabase database, SqliteConnection co
}
}
private async ValueTask<int> StepAsync(SqliteStatement statement, bool retryBusy, CancellationToken cancellationToken)
/// <summary>
/// Steps once. While <paramref name="canRestart"/> (no row returned yet) a statement stopped by a lock is
/// rewound and retried: SQLITE_LOCKED (shared-cache table locks, which busy_timeout does not cover) for up to
/// the busy timeout, SQLITE_BUSY (another process; busy_timeout already waited) a few times outside transactions.
/// </summary>
private async ValueTask<int> StepAsync(SqliteStatement statement, bool canRestart, CancellationToken cancellationToken)
{
for (var attempt = 0; ; attempt++)
var waitedMs = 0;
var busyRetries = 0;
while (true)
{
var rc = statement.Step();
if (rc is SQLITE_ROW or SQLITE_DONE) return rc;
var primary = rc & 0xFF;
if (primary == SQLITE_INTERRUPT && cancellationToken.IsCancellationRequested)
throw new OperationCanceledException(cancellationToken);
if (retryBusy && primary is SQLITE_BUSY or SQLITE_LOCKED && attempt < MaxBusyRetries)
{
// busy_timeout already waited; another process holds the lock for long. Back off and restart.
statement.Rewind();
await Task.Delay(50 * (attempt + 1) + Random.Shared.Next(50), cancellationToken).ConfigureAwait(false);
continue;
}
throw connection.Error(rc);
int delayMs;
if (canRestart && primary == SQLITE_LOCKED && waitedMs < SqliteDatabase.BusyTimeoutMs)
delayMs = Math.Min(50, 1 + waitedMs / 10);
else if (canRestart && primary == SQLITE_BUSY && _transaction is null && busyRetries++ < MaxBusyRetries)
delayMs = 50 * busyRetries + Random.Shared.Next(50);
else
throw connection.Error(rc);
statement.Rewind();
await Task.Delay(delayMs, cancellationToken).ConfigureAwait(false);
waitedMs += delayMs;
}
}