From 4d94273a00e143090ea0f59b355cfa63cd0d1c95 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Thu, 8 Oct 2026 19:49:09 -0400 Subject: [PATCH] fix(test): release transient SQLite handles for Windows server tests --- OpenNest.Server.Tests/ServerFactory.cs | 5 +---- OpenNest.Server/NestDatabase.cs | 9 +++++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/OpenNest.Server.Tests/ServerFactory.cs b/OpenNest.Server.Tests/ServerFactory.cs index 31d81c0..b114d11 100644 --- a/OpenNest.Server.Tests/ServerFactory.cs +++ b/OpenNest.Server.Tests/ServerFactory.cs @@ -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 builder.ConfigureTestServices(services => { services.RemoveAll(); - services.AddSingleton(_ => new NestDatabase(DatabasePath)); + services.AddSingleton(_ => new NestDatabase(DatabasePath, pooling: false)); }); } @@ -45,8 +44,6 @@ public sealed class ServerFactory : WebApplicationFactory 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); } diff --git a/OpenNest.Server/NestDatabase.cs b/OpenNest.Server/NestDatabase.cs index a7e67a0..1aaad3f 100644 --- a/OpenNest.Server/NestDatabase.cs +++ b/OpenNest.Server/NestDatabase.cs @@ -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;