fix(test): release transient SQLite handles for Windows server tests

This commit is contained in:
aj committed 2026-10-08 19:49:09 -04:00
1 parent 028adee046
commit 4d94273a00
2 files changed
+8 -6

No files matched your search

+1 -4
View File
@@ -1,7 +1,6 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
@@ -34,7 +33,7 @@ public sealed class ServerFactory : WebApplicationFactory<global::Program>
builder.ConfigureTestServices(services =>
{
services.RemoveAll<NestDatabase>();
services.AddSingleton(_ => new NestDatabase(DatabasePath));
services.AddSingleton(_ => new NestDatabase(DatabasePath, pooling: false));
});
}
@@ -45,8 +44,6 @@ public sealed class ServerFactory : WebApplicationFactory<global::Program>
return;
_cleanedUp = true;
// Pooled connections would keep the file open on Windows after the host disposed it.
SqliteConnection.ClearAllPools();
if (Directory.Exists(DirectoryPath))
Directory.Delete(DirectoryPath, recursive: true);
}
+7 -2
View File
@@ -16,13 +16,18 @@ public sealed class NestDatabase : IDisposable
private readonly object _sync = new();
private readonly SqliteConnection _connection;
public NestDatabase(string databasePath)
public NestDatabase(string databasePath, bool pooling = true)
{
var directory = Path.GetDirectoryName(Path.GetFullPath(databasePath));
if (!string.IsNullOrEmpty(directory))
Directory.CreateDirectory(directory);
_connection = new SqliteConnection($"Data Source={databasePath}");
// Test databases are short-lived files: disabling pooling releases Windows file handles on dispose.
_connection = new SqliteConnection(new SqliteConnectionStringBuilder
{
DataSource = databasePath,
Pooling = pooling,
}.ToString());
_connection.Open();
Execute("""
PRAGMA journal_mode=WAL;