Files
Anubis/tests/Anubis.Tests/HealthEndpointTests.cs
T
Alby96andClaude Opus 5.5 cfa2a4da0e
ci / test (postgres) (push) Successful in 9m13s
ci / test (sqlite) (push) Successful in 7m33s
ci / image (push) Successful in 2m48s
Fase 13 — Campionato: unità di lavoro, piano e statistica (#188)
The tournament judge before the contenders (ADR 0029): the trial judge split into resumable work units on a pool of low-priority workers, the common count of trials and field statistics, the control against chance on surrogate prices, and the immutable tournament plan with a final tract read once.

refs #122, refs #123, refs #124, refs #125, refs #126

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-12-04 15:53:40 +01:00

75 lines
2.9 KiB
C#

using System.Diagnostics;
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<HealthResponse>();
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<HealthResponse> HealthAsync()
{
using var response = await server.GetAsync("/health");
return (await response.Content.ReadFromJsonAsync<HealthResponse>())!;
}
// Up to 30 seconds of wall time: on a busy test runner the engine can take more than ten to start.
async Task UntilAsync(string status, string loop)
{
HealthResponse? last = null;
var deadline = Stopwatch.StartNew();
while (last is null || deadline.Elapsed < TimeSpan.FromSeconds(30))
{
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);
}
}