Files
Alby96andClaude Opus 5.5 16be51e175
ci / test (postgres) (push) Successful in 3m40s
ci / test (sqlite) (push) Successful in 2m53s
ci / test (postgres) (pull_request) Successful in 4m31s
ci / test (sqlite) (pull_request) Successful in 2m59s
ci / image (push) Successful in 13s
ci / image (pull_request) Successful in 15s
Retry test database creation on any transient PostgreSQL error (refs #33)
CI run 188 failed once on the SQLite job: a cross-engine test lost its
connection (EndOfStreamException) while creating its PostgreSQL database,
right after the service container started. The retry covered only
timeouts. It now covers every transient Npgsql error and picks a new
database name on each attempt, in case the lost answer had already
created the database. Test helper only; no product code changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 22:09:16 +01:00

235 lines
8.9 KiB
C#

using System.Diagnostics;
using System.Net.Sockets;
[assembly: Parallelize(Scope = ExecutionScope.MethodLevel)]
namespace Anubis.Tests;
/// <summary>Test categories (ADR 0003). <see cref="LiveDemo"/> never runs in the normal suite.</summary>
internal static class Category
{
public const string Unit = "Unit";
public const string Integration = "Integration";
public const string Simulation = "Simulation";
public const string LiveDemo = "LiveDemo";
public const string Golden = "Golden";
}
/// <summary>Locates the repository so policy tests can read its files.</summary>
internal static class Repo
{
public static DirectoryInfo Root { get; } = FindRoot();
private static DirectoryInfo FindRoot()
{
for (var dir = new DirectoryInfo(AppContext.BaseDirectory); dir is not null; dir = dir.Parent)
{
if (File.Exists(Path.Combine(dir.FullName, "Anubis.slnx")))
{
return dir;
}
}
throw new InvalidOperationException("Repository root (Anubis.slnx) not found above " + AppContext.BaseDirectory);
}
private static readonly Lazy<IReadOnlyList<FileInfo>> _files = new(ListFiles);
/// <summary>
/// The files git would commit: tracked ones plus new ones that .gitignore does not exclude.
/// The local .env, /data and build output stay out, so a key the user puts there never fails the suite.
/// </summary>
public static IEnumerable<FileInfo> Files() => _files.Value;
private static List<FileInfo> ListFiles()
{
var info = new ProcessStartInfo("git")
{
WorkingDirectory = Root.FullName,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
};
foreach (var argument in new[] { "ls-files", "-z", "--cached", "--others", "--exclude-standard" })
{
info.ArgumentList.Add(argument);
}
using var process = Process.Start(info) ?? throw new InvalidOperationException("git could not be started.");
var errors = process.StandardError.ReadToEndAsync();
var output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
if (process.ExitCode != 0)
{
throw new InvalidOperationException("git ls-files failed: " + errors.Result);
}
return output.Split('\0', StringSplitOptions.RemoveEmptyEntries)
.Select(path => new FileInfo(Path.Combine(Root.FullName, path)))
.Where(file => file.Exists)
.ToList();
}
public static string Relative(FileInfo file) =>
Path.GetRelativePath(Root.FullName, file.FullName).Replace('\\', '/');
}
/// <summary>Database engine the suite runs against: <c>ANUBIS_TEST_DB</c> = sqlite (default) or postgres.</summary>
internal static class TestDatabase
{
public static string Engine { get; } =
(Environment.GetEnvironmentVariable("ANUBIS_TEST_DB") ?? "sqlite").Trim().ToLowerInvariant();
/// <summary>Host and port of the PostgreSQL used by the tests, from <c>ANUBIS_TEST_PG_HOST</c> and <c>ANUBIS_TEST_PG_PORT</c>.</summary>
public static (string Host, int Port) Postgres { get; } = (
Environment.GetEnvironmentVariable("ANUBIS_TEST_PG_HOST") ?? "127.0.0.1",
int.TryParse(Environment.GetEnvironmentVariable("ANUBIS_TEST_PG_PORT"), out var port) ? port : 55432);
public static async Task<bool> PostgresReachableAsync(TimeSpan timeout)
{
using var client = new TcpClient();
using var cts = new CancellationTokenSource(timeout);
try
{
await client.ConnectAsync(Postgres.Host, Postgres.Port, cts.Token).ConfigureAwait(false);
return true;
}
catch (Exception ex) when (ex is SocketException or OperationCanceledException)
{
return false;
}
}
}
/// <summary>
/// A fresh, empty database for one test: a SQLite file in a temporary folder, or a new PostgreSQL database
/// created on the test server and dropped afterwards. Never the developer's or the user's database.
/// </summary>
internal sealed class TestDb : IAsyncDisposable
{
private readonly string? _folder;
private readonly string? _postgresName;
private TestDb(Anubis.Platform.DbSettings settings, string? folder, string? postgresName)
{
Settings = settings;
_folder = folder;
_postgresName = postgresName;
Db = Anubis.Db.Database.Open(settings);
}
public Anubis.Platform.DbSettings Settings { get; }
public Anubis.Db.Database Db { get; }
public static string PostgresPassword { get; } = Environment.GetEnvironmentVariable("ANUBIS_TEST_PG_PASSWORD") ?? "anubis-dev";
/// <summary>A database on the engine of this run (ANUBIS_TEST_DB).</summary>
public static Task<TestDb> CreateAsync() =>
CreateAsync(TestDatabase.Engine == "postgres" ? Anubis.Platform.DbEngine.Postgres : Anubis.Platform.DbEngine.Sqlite);
public static async Task<TestDb> CreateAsync(Anubis.Platform.DbEngine engine)
{
if (engine == Anubis.Platform.DbEngine.Sqlite)
{
var folder = TempFolder.Create("db");
return new TestDb(new() { Engine = engine, SqlitePath = Path.Combine(folder, "test.sqlite3") }, folder, null);
}
string name;
for (var attempt = 1; ; attempt++)
{
// A new name each time: a dropped connection may have created the database before the answer was lost.
name = "anubis_t_" + Guid.NewGuid().ToString("N");
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 = $"CREATE DATABASE {name}";
await command.ExecuteNonQueryAsync().ConfigureAwait(false);
break;
}
catch (Npgsql.NpgsqlException ex) when (ex.IsTransient && attempt < 4)
{
// Many tests create their database at once and a busy CI runner can be slow to accept, or drop a
// connection while its PostgreSQL service is still settling. That is the runner, not the code under test.
await Task.Delay(TimeSpan.FromSeconds(attempt)).ConfigureAwait(false);
}
}
return new TestDb(AdminSettings() with { Database = name }, null, name);
}
/// <summary>True when a PostgreSQL server for tests answers: cross-engine tests run only then.</summary>
public static Task<bool> PostgresAvailableAsync() => TestDatabase.PostgresReachableAsync(TimeSpan.FromSeconds(5));
private static Anubis.Platform.DbSettings AdminSettings() => new()
{
Engine = Anubis.Platform.DbEngine.Postgres,
Host = TestDatabase.Postgres.Host,
Port = TestDatabase.Postgres.Port,
Database = "anubis",
User = "anubis",
Password = PostgresPassword,
// Twelve test workers open connections at once: on a busy CI runner PostgreSQL can take longer than the
// application's 5 seconds to accept one. Waiting longer here tests the same code without the runner's noise.
ConnectTimeoutSeconds = 30,
};
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);
}
if (_folder is not null)
{
TempFolder.Delete(_folder);
}
}
}
/// <summary>Temporary folders for tests, under the system temp directory.</summary>
internal static class TempFolder
{
public static string Create(string purpose)
{
var path = Path.Combine(Path.GetTempPath(), "anubis-tests", purpose + "-" + Guid.NewGuid().ToString("N")[..12]);
Directory.CreateDirectory(path);
return path;
}
public static void Delete(string path)
{
Microsoft.Data.Sqlite.SqliteConnection.ClearAllPools();
for (var attempt = 0; attempt < 5; attempt++)
{
try
{
if (Directory.Exists(path))
{
Directory.Delete(path, recursive: true);
}
return;
}
catch (IOException) when (attempt < 4)
{
Thread.Sleep(100);
}
catch (UnauthorizedAccessException) when (attempt < 4)
{
Thread.Sleep(100);
}
}
}
}