Files
GmRelayBot/tests/GmRelay.Bot.Tests/Infrastructure/Health/BotHealthCheckHostedServiceTests.cs
T
Toutsu 105b3c59d7
PR Checks / test-and-build (pull_request) Successful in 8m34s
fix: address review feedback for health check endpoints
- Install wget in Web Dockerfile for compose healthcheck
- Ensure HttpListener response is always closed in BotHealthCheckHostedService
- Use ephemeral port in Bot health check test to avoid port conflicts
- Rename NpgsqlHealthCheck test to reflect actual behavior

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-13 11:16:58 +03:00

55 lines
1.6 KiB
C#

using System.Net;
using System.Net.Sockets;
using GmRelay.Bot.Infrastructure.Health;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging.Abstractions;
namespace GmRelay.Bot.Tests.Infrastructure.Health;
public sealed class BotHealthCheckHostedServiceTests : IDisposable
{
private readonly BotHealthCheckHostedService _service;
private readonly int _port;
public BotHealthCheckHostedServiceTests()
{
_port = GetAvailablePort();
var config = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?>
{
["HealthCheck:Prefix"] = $"http://localhost:{_port}/"
})
.Build();
_service = new BotHealthCheckHostedService(
NullLogger<BotHealthCheckHostedService>.Instance,
config);
}
public void Dispose()
{
_service.StopAsync(CancellationToken.None).Wait(TimeSpan.FromSeconds(5));
}
[Fact]
public async Task HealthEndpoint_ShouldReturn200_WhenServiceIsRunning()
{
await _service.StartAsync(CancellationToken.None);
using var client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(5);
var response = await client.GetAsync($"http://localhost:{_port}/health");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
private static int GetAvailablePort()
{
var listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();
var port = ((IPEndPoint)listener.LocalEndpoint).Port;
listener.Stop();
return port;
}
}