using System.Net; using System.Net.Http.Json; using Anubis.Server; namespace Anubis.Tests; [TestClass] public sealed class HealthEndpointTests { [TestMethod] [TestCategory(Category.Integration)] public async Task HealthAnswersOkWithNameAndVersion() { // The real server on a free local port with its own data folder: no test host package needed (ADR 0004). await using var server = await ServerHarness.StartAsync("docker", ServerHarness.DockerSqlite()); using var response = await server.GetAsync("/health"); var body = await response.Content.ReadFromJsonAsync(); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); Assert.IsNotNull(body); Assert.AreEqual("ok", body.Status); Assert.AreEqual(AnubisInfo.Name, body.Name); Assert.AreEqual(AnubisInfo.Version, body.Version); } [TestMethod] [TestCategory(Category.Integration)] public async Task AnEngineThatCouldNotStartIsNotHealthyAndIsTriedAgain() { var (env, postgres) = await ServerHarness.DockerOnEngineAsync(); await using var engine = postgres; await using var server = await ServerHarness.StartAsync("docker", env, trading: true); async Task HealthAsync() { using var response = await server.GetAsync("/health"); return (await response.Content.ReadFromJsonAsync())!; } async Task UntilAsync(string status, string loop) { HealthResponse? last = null; for (var i = 0; i < 100; i++) { last = await HealthAsync(); if (last.Status == status && last.Checks!.GetValueOrDefault("motore") == loop) { return; } await Task.Delay(100); } Assert.Fail($"{last!.Status}, {string.Join(", ", last.Checks!)}"); } await UntilAsync("ok", "ok"); // The services start again and the engine's startup fails at its first steps: ready, but nothing watches the stops. var db = server.Host.Services!.Db; await db.WriteAsync(s => s.ExecuteAsync("ALTER TABLE instruments RENAME TO instruments_away")); await server.Host.RestartServicesAsync(CancellationToken.None); await server.WaitForAsync(HostMode.Ready); await UntilAsync("unavailable", "non avviato"); // The cause goes away: the start is tried again by itself. await server.Host.Services!.Db.WriteAsync(s => s.ExecuteAsync("ALTER TABLE instruments_away RENAME TO instruments")); await UntilAsync("ok", "ok"); Assert.IsTrue(server.Host.Services!.Trading!.IsStarted); } }