8c21678cb4
Adds WorkerExecutableHealthCheck (configured worker .exe exists) and GalaxySqlHealthCheck (IGalaxyRepository.TestConnectionAsync bounded by a 5 s TimeProvider-driven timeout). Both are tagged ready only, never live, so an outage takes the node out of rotation without triggering a restart; sessions stay on the active tier so zero sessions is still healthy for Overview. Registrations move into AddGatewayHealthChecks() so the tier tagging is unit-tested. LDAP is not added (sign-in already surfaces bind failures). docs/Diagnostics.md lists the two new checks. Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
91 lines
4.0 KiB
C#
91 lines
4.0 KiB
C#
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
|
using Microsoft.Extensions.Time.Testing;
|
|
using ZB.MOM.WW.GalaxyRepository;
|
|
using ZB.MOM.WW.MxGateway.Server.Diagnostics;
|
|
|
|
namespace ZB.MOM.WW.MxGateway.Tests.Diagnostics;
|
|
|
|
public sealed class GalaxySqlHealthCheckTests
|
|
{
|
|
/// <summary>A successful ping is healthy.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Healthy_WhenPingSucceeds()
|
|
{
|
|
GalaxySqlHealthCheck check = new(new PingRepository((_, _) => Task.FromResult(true)));
|
|
HealthCheckResult result = await check.CheckHealthAsync(new HealthCheckContext());
|
|
Assert.Equal(HealthStatus.Healthy, result.Status);
|
|
}
|
|
|
|
/// <summary>A false ping and a thrown ping are both unhealthy.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Unhealthy_WhenPingFailsOrThrows()
|
|
{
|
|
HealthCheckResult returnedFalse = await new GalaxySqlHealthCheck(new PingRepository((_, _) => Task.FromResult(false)))
|
|
.CheckHealthAsync(new HealthCheckContext());
|
|
HealthCheckResult threw = await new GalaxySqlHealthCheck(new PingRepository((_, _) => throw new InvalidOperationException("boom")))
|
|
.CheckHealthAsync(new HealthCheckContext());
|
|
|
|
Assert.Equal(HealthStatus.Unhealthy, returnedFalse.Status);
|
|
Assert.Equal(HealthStatus.Unhealthy, threw.Status);
|
|
Assert.IsType<InvalidOperationException>(threw.Exception);
|
|
}
|
|
|
|
/// <summary>A hung ping is cut off by the timeout (fake clock) and reported unhealthy.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Unhealthy_WhenPingHangsPastTimeout()
|
|
{
|
|
FakeTimeProvider clock = new();
|
|
PingRepository hung = new(async (_, ct) =>
|
|
{
|
|
await Task.Delay(Timeout.Infinite, ct);
|
|
return true;
|
|
});
|
|
GalaxySqlHealthCheck check = new(hung, TimeSpan.FromSeconds(5), clock);
|
|
|
|
// The timeout timer is armed synchronously inside CheckHealthAsync before the first await.
|
|
Task<HealthCheckResult> pending = check.CheckHealthAsync(new HealthCheckContext());
|
|
Assert.False(pending.IsCompleted);
|
|
|
|
clock.Advance(TimeSpan.FromSeconds(5));
|
|
HealthCheckResult result = await pending.WaitAsync(TimeSpan.FromSeconds(5));
|
|
|
|
Assert.Equal(HealthStatus.Unhealthy, result.Status);
|
|
Assert.Contains("timed out", result.Description);
|
|
}
|
|
|
|
/// <summary>Caller cancellation propagates instead of being reported as a timeout.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task CallerCancellation_Propagates()
|
|
{
|
|
PingRepository hung = new(async (_, ct) =>
|
|
{
|
|
await Task.Delay(Timeout.Infinite, ct);
|
|
return true;
|
|
});
|
|
GalaxySqlHealthCheck check = new(hung, TimeSpan.FromSeconds(5), new FakeTimeProvider());
|
|
using CancellationTokenSource caller = new();
|
|
|
|
Task<HealthCheckResult> pending = check.CheckHealthAsync(new HealthCheckContext(), caller.Token);
|
|
await caller.CancelAsync();
|
|
|
|
await Assert.ThrowsAnyAsync<OperationCanceledException>(async () => await pending.WaitAsync(TimeSpan.FromSeconds(5)));
|
|
}
|
|
|
|
private sealed class PingRepository(Func<int, CancellationToken, Task<bool>> ping) : IGalaxyRepository
|
|
{
|
|
public Task<bool> TestConnectionAsync(CancellationToken ct = default) => ping(0, ct);
|
|
|
|
public Task<DateTime?> GetLastDeployTimeAsync(CancellationToken ct = default) => throw new NotSupportedException();
|
|
|
|
public Task<List<GalaxyHierarchyRow>> GetHierarchyAsync(CancellationToken ct = default) => throw new NotSupportedException();
|
|
|
|
public Task<List<GalaxyAttributeRow>> GetAttributesAsync(CancellationToken ct = default) => throw new NotSupportedException();
|
|
|
|
public Task<List<GalaxyAlarmAttributeRow>> GetAlarmAttributesAsync(CancellationToken ct = default) => throw new NotSupportedException();
|
|
}
|
|
}
|