Files
Anubis/tests/Anubis.Tests/HealthEndpointTests.cs
T
Alby96andClaude Opus 5.5 7b162aa4bc
ci / test (postgres) (push) Successful in 9m39s
ci / test (sqlite) (push) Successful in 8m6s
ci / test (postgres) (pull_request) Successful in 10m13s
ci / test (sqlite) (pull_request) Successful in 7m36s
ci / image (push) Successful in 1m55s
ci / image (pull_request) Successful in 58s
Give the engine health test thirty seconds instead of a hundred polls
The test waited for the engine to start for at most a hundred polls, about
ten seconds, and passed in eleven on PostgreSQL in CI. Phase 13 adds
CPU-heavy tournament and control tests that run in parallel with it, and
on a busy runner the engine took longer to start, so the test failed with
the engine still starting. It now waits up to thirty seconds of wall time,
polling as before; it still requires the engine to reach ok, then not
started, then ok again.

refs #126

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-04 14:45:36 +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);
}
}