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 { /// A successful ping is healthy. /// A task that represents the asynchronous operation. [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); } /// A false ping and a thrown ping are both unhealthy. /// A task that represents the asynchronous operation. [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(threw.Exception); } /// A hung ping is cut off by the timeout (fake clock) and reported unhealthy. /// A task that represents the asynchronous operation. [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 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); } /// Caller cancellation propagates instead of being reported as a timeout. /// A task that represents the asynchronous operation. [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 pending = check.CheckHealthAsync(new HealthCheckContext(), caller.Token); await caller.CancelAsync(); await Assert.ThrowsAnyAsync(async () => await pending.WaitAsync(TimeSpan.FromSeconds(5))); } private sealed class PingRepository(Func> ping) : IGalaxyRepository { public Task TestConnectionAsync(CancellationToken ct = default) => ping(0, ct); public Task GetLastDeployTimeAsync(CancellationToken ct = default) => throw new NotSupportedException(); public Task> GetHierarchyAsync(CancellationToken ct = default) => throw new NotSupportedException(); public Task> GetAttributesAsync(CancellationToken ct = default) => throw new NotSupportedException(); public Task> GetAlarmAttributesAsync(CancellationToken ct = default) => throw new NotSupportedException(); } }