fix(health-monitoring): resolve HealthMonitoring-013,014,016 — shorter-timeout cadence, options validation, injected TimeProvider; HealthMonitoring-015 left open (cross-module design decision)

This commit is contained in:
Joseph Doherty
2026-05-17 03:18:24 -04:00
parent da8c9f171b
commit eae4077414
8 changed files with 296 additions and 12 deletions

View File

@@ -307,6 +307,36 @@ public class CentralHealthAggregatorTests
Assert.False(_aggregator.GetSiteState(CentralHealthReportLoop.CentralSiteId)!.IsOnline);
}
/// <summary>
/// HealthMonitoring-013 regression: the offline-check cadence must be derived
/// from the *shorter* of <see cref="HealthMonitoringOptions.OfflineTimeout"/>
/// and <see cref="HealthMonitoringOptions.CentralOfflineTimeout"/>, so that if
/// an operator configures <c>CentralOfflineTimeout</c> smaller than
/// <c>OfflineTimeout</c>, central offline detection is still timely instead of
/// being delayed by up to a full <c>OfflineTimeout / 2</c>.
/// </summary>
[Fact]
public void CheckInterval_IsHalfTheShorterTimeout()
{
// Default: OfflineTimeout (60s) is the shorter of the two.
Assert.Equal(
TimeSpan.FromSeconds(30),
CentralHealthAggregator.ComputeCheckInterval(new HealthMonitoringOptions
{
OfflineTimeout = TimeSpan.FromSeconds(60),
CentralOfflineTimeout = TimeSpan.FromMinutes(3)
}));
// Operator configures CentralOfflineTimeout shorter — cadence must adapt.
Assert.Equal(
TimeSpan.FromSeconds(10),
CentralHealthAggregator.ComputeCheckInterval(new HealthMonitoringOptions
{
OfflineTimeout = TimeSpan.FromSeconds(60),
CentralOfflineTimeout = TimeSpan.FromSeconds(20)
}));
}
[Fact]
public void SequenceNumberReset_RejectedUntilExceedsPrevMax()
{

View File

@@ -0,0 +1,73 @@
using Microsoft.Extensions.Options;
namespace ScadaLink.HealthMonitoring.Tests;
/// <summary>
/// HealthMonitoring-014 regression: <see cref="HealthMonitoringOptions"/> intervals
/// are fed straight into <c>new PeriodicTimer(...)</c>, which throws
/// <see cref="ArgumentOutOfRangeException"/> for a zero/negative period. A
/// misconfigured <c>appsettings.json</c> must be rejected by an
/// <see cref="IValidateOptions{TOptions}"/> with a clear, key-naming message
/// rather than crashing the hosted service with an opaque exception.
/// </summary>
public class HealthMonitoringOptionsValidatorTests
{
private static ValidateOptionsResult Validate(HealthMonitoringOptions options) =>
new HealthMonitoringOptionsValidator().Validate(Options.DefaultName, options);
[Fact]
public void DefaultOptions_AreValid()
{
var result = Validate(new HealthMonitoringOptions());
Assert.True(result.Succeeded, result.FailureMessage);
}
[Fact]
public void ZeroReportInterval_IsRejected()
{
var result = Validate(new HealthMonitoringOptions { ReportInterval = TimeSpan.Zero });
Assert.True(result.Failed);
Assert.Contains("ReportInterval", result.FailureMessage);
}
[Fact]
public void NegativeReportInterval_IsRejected()
{
var result = Validate(new HealthMonitoringOptions { ReportInterval = TimeSpan.FromSeconds(-1) });
Assert.True(result.Failed);
Assert.Contains("ReportInterval", result.FailureMessage);
}
[Fact]
public void ZeroOfflineTimeout_IsRejected()
{
var result = Validate(new HealthMonitoringOptions { OfflineTimeout = TimeSpan.Zero });
Assert.True(result.Failed);
Assert.Contains("OfflineTimeout", result.FailureMessage);
}
[Fact]
public void ZeroCentralOfflineTimeout_IsRejected()
{
var result = Validate(new HealthMonitoringOptions { CentralOfflineTimeout = TimeSpan.Zero });
Assert.True(result.Failed);
Assert.Contains("CentralOfflineTimeout", result.FailureMessage);
}
[Fact]
public void CentralOfflineTimeout_ShorterThanOfflineTimeout_IsRejected()
{
var result = Validate(new HealthMonitoringOptions
{
OfflineTimeout = TimeSpan.FromSeconds(60),
CentralOfflineTimeout = TimeSpan.FromSeconds(30)
});
Assert.True(result.Failed);
Assert.Contains("CentralOfflineTimeout", result.FailureMessage);
}
}

View File

@@ -131,6 +131,24 @@ public class SiteHealthCollectorTests
Assert.InRange(report.ReportTimestamp, before, after);
}
/// <summary>
/// HealthMonitoring-016 regression: <see cref="SiteHealthCollector.CollectReport"/>
/// must stamp <c>ReportTimestamp</c> from an injected <see cref="TimeProvider"/>
/// (consistent with the rest of the module), not directly from
/// <c>DateTimeOffset.UtcNow</c>, so the report timestamp is deterministically
/// testable against a known instant.
/// </summary>
[Fact]
public void CollectReport_StampsTimestamp_FromInjectedTimeProvider()
{
var fixedInstant = new DateTimeOffset(2026, 5, 17, 9, 30, 0, TimeSpan.Zero);
var collector = new SiteHealthCollector(new TestTimeProvider(fixedInstant));
var report = collector.CollectReport("site-1");
Assert.Equal(fixedInstant, report.ReportTimestamp);
}
[Fact]
public void CollectReport_SequenceNumberIsZero_CallerAssignsIt()
{