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)