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;