diff --git a/VigilCare.WardGateway.Tests/Fixtures/GatewayApiFixture.cs b/VigilCare.WardGateway.Tests/Fixtures/GatewayApiFixture.cs index f8325ca..6e45054 100644 --- a/VigilCare.WardGateway.Tests/Fixtures/GatewayApiFixture.cs +++ b/VigilCare.WardGateway.Tests/Fixtures/GatewayApiFixture.cs @@ -81,15 +81,19 @@ public class GatewayApiFixture : WebApplicationFactory, IAsyncLifetime await GatewayDbResetHelper.ResetAsync(migrateDb); } - using var scope = Services.CreateScope(); - var redis = scope.ServiceProvider.GetRequiredService(); - var server = redis.GetServer(redis.GetEndPoints().First()); // Flush only the gateway test DB index — do not FlushAll (would wipe API test DB 1). + // Must run before Services is touched: host startup saturates the thread pool and + // causes StackExchange.Redis TimeoutException on FLUSHDB under CI load. var dbIndex = 2; var cfg = RedisConnection.Split(',').FirstOrDefault(p => p.StartsWith("defaultDatabase=", StringComparison.OrdinalIgnoreCase)); if (cfg is not null && int.TryParse(cfg.Split('=')[1], out var parsed)) dbIndex = parsed; - await server.FlushDatabaseAsync(dbIndex); + + await using (var redis = await ConnectionMultiplexer.ConnectAsync(RedisConnection)) + { + var server = redis.GetServer(redis.GetEndPoints().First()); + await server.FlushDatabaseAsync(dbIndex); + } } protected override void ConfigureClient(HttpClient client) diff --git a/VigilCareClinicalAPI.Tests/Fixtures/ApiFixture.cs b/VigilCareClinicalAPI.Tests/Fixtures/ApiFixture.cs index b3449d0..f043eae 100644 --- a/VigilCareClinicalAPI.Tests/Fixtures/ApiFixture.cs +++ b/VigilCareClinicalAPI.Tests/Fixtures/ApiFixture.cs @@ -129,10 +129,15 @@ public class ApiFixture : WebApplicationFactory, IAsyncLifetime VirtualHost = "vigilcare_test", }); - using var scope = Services.CreateScope(); - var redis = scope.ServiceProvider.GetRequiredService(); - var server = redis.GetServer(redis.GetEndPoints().First()); - await server.FlushDatabaseAsync(1); + // Flush Redis before starting the host. Accessing Services boots many hosted + // services that saturate the thread pool; FLUSHDB on the shared multiplexer + // then times out waiting for a reply that already arrived (see SE.Redis + // TimeoutException: last-in/cur-in stuck, QueuedItems > 0). + await using (var redis = await ConnectionMultiplexer.ConnectAsync(RedisConnection)) + { + var server = redis.GetServer(redis.GetEndPoints().First()); + await server.FlushDatabaseAsync(1); + } } protected override void ConfigureClient(HttpClient client)