From 3eabc7713e79fb6f07473358cb1c1ab98fdd370a Mon Sep 17 00:00:00 2001 From: Alberto Balbo Date: Tue, 1 Dec 2026 15:16:39 +0100 Subject: [PATCH] Let the tests ride out a busy server On a loaded runner a forced DROP DATABASE gave up on an autovacuum slow to leave, and failed a test that had already passed: the cleanup is tried again, as the creation already is. The feed test gets time for its slow reader on a computer at full load. refs #67 Co-Authored-By: Claude Opus 5.5 --- tests/Anubis.Tests/OkxTests.cs | 4 ++-- tests/Anubis.Tests/TestSupport.cs | 31 ++++++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/tests/Anubis.Tests/OkxTests.cs b/tests/Anubis.Tests/OkxTests.cs index ae5210c..3f6c8de 100644 --- a/tests/Anubis.Tests/OkxTests.cs +++ b/tests/Anubis.Tests/OkxTests.cs @@ -236,7 +236,7 @@ public sealed class OkxTests return socket; }); - using var stop = new CancellationTokenSource(TimeSpan.FromSeconds(20)); + using var stop = new CancellationTokenSource(TimeSpan.FromSeconds(60)); var reading = Task.Run(async () => { await foreach (var item in okx.StreamAsync(["BTC-EUR"], stop.Token).ConfigureAwait(false)) @@ -262,7 +262,7 @@ public sealed class OkxTests publicSocket.Push(ticker); } - await reading.WaitAsync(TimeSpan.FromSeconds(10)).ConfigureAwait(false); + await reading.WaitAsync(TimeSpan.FromSeconds(30)).ConfigureAwait(false); Assert.IsTrue(sockets.All(s => s.Disposed)); } diff --git a/tests/Anubis.Tests/TestSupport.cs b/tests/Anubis.Tests/TestSupport.cs index 36ee6d7..0da9475 100644 --- a/tests/Anubis.Tests/TestSupport.cs +++ b/tests/Anubis.Tests/TestSupport.cs @@ -178,16 +178,37 @@ internal sealed class TestDb : IAsyncDisposable ConnectTimeoutSeconds = 30, }; + /// + /// Forced, a drop still waits only five seconds for the sessions it ends, and on a busy server an autovacuum can + /// take longer to leave ("is being accessed by other users"), or the answer can arrive late. That is the server, + /// not the code under test: the drop is tried again. + /// + private static async Task DropAsync(string name) + { + for (var attempt = 1; ; attempt++) + { + try + { + await using var admin = Anubis.Db.Database.Open(AdminSettings()); + await using var connection = await admin.OpenConnectionAsync(CancellationToken.None).ConfigureAwait(false); + await using var command = connection.CreateCommand(); + command.CommandText = $"DROP DATABASE IF EXISTS {name} WITH (FORCE)"; + await command.ExecuteNonQueryAsync().ConfigureAwait(false); + return; + } + catch (Npgsql.NpgsqlException ex) when (attempt < 4 && (ex.IsTransient || ex.SqlState == Npgsql.PostgresErrorCodes.ObjectInUse)) + { + await Task.Delay(TimeSpan.FromSeconds(attempt)).ConfigureAwait(false); + } + } + } + public async ValueTask DisposeAsync() { await Db.DisposeAsync().ConfigureAwait(false); if (_postgresName is not null) { - await using var admin = Anubis.Db.Database.Open(AdminSettings()); - await using var connection = await admin.OpenConnectionAsync(CancellationToken.None).ConfigureAwait(false); - await using var command = connection.CreateCommand(); - command.CommandText = $"DROP DATABASE IF EXISTS {_postgresName} WITH (FORCE)"; - await command.ExecuteNonQueryAsync().ConfigureAwait(false); + await DropAsync(_postgresName).ConfigureAwait(false); } if (_folder is not null)