Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.HealthMonitoring.Tests/AddSiteEventLogHealthMetricsBridgeTests.cs
T
Joseph Doherty c9244d8bda fix(health): M2.16 review nit — real idempotency guard for SiteEventLog health bridge (#30)
AddSiteEventLogHealthMetricsBridge registered via AddHostedService(factory-lambda),
which sets ImplementationFactory and leaves ImplementationType null. The prior
ImplementationType == guard was therefore silently dead — a second call would spin
up a second SiteEventLogFailureCountReporter. Fix: add a private
SiteEventLogHealthMetricsBridgeMarker singleton and guard on its ServiceType instead.

Also corrects the cycle-path comment in both ServiceCollectionExtensions.cs and
SiteEventLogFailureCountReporter.cs: StoreAndForward.csproj does reference
SiteEventLogging.csproj, so the transitive path HealthMonitoring → StoreAndForward →
SiteEventLogging is real, but adding a direct HealthMonitoring → SiteEventLogging
reference would NOT create a cycle (SiteEventLogging has no back-edge to HealthMonitoring).
The Func<long> seam is a coupling-avoidance measure, not a cycle-breaker.

Adds AddSiteEventLogHealthMetricsBridgeTests.AddSiteEventLogHealthMetricsBridge_IsIdempotent_DoesNotDoubleRegister_HostedService
as a regression test (builds provider and asserts exactly one reporter via GetServices<IHostedService>().OfType<T>()).
2026-06-16 07:22:35 -04:00

49 lines
2.2 KiB
C#

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
namespace ZB.MOM.WW.ScadaBridge.HealthMonitoring.Tests;
/// <summary>
/// M2.16 (#30) idempotency regression — code-review finding on commit d81f747.
/// <para>
/// <see cref="ServiceCollectionExtensions.AddSiteEventLogHealthMetricsBridge"/> uses a
/// factory-lambda overload of <c>AddHostedService</c>, which sets only
/// <c>ImplementationFactory</c> and leaves <c>ImplementationType</c> null. The original
/// <c>ImplementationType ==</c> guard was therefore a silent no-op: a second call would spin
/// up a second <see cref="SiteEventLogFailureCountReporter"/> (two timers both polling).
/// The fix uses a private marker singleton whose <c>ServiceType</c> is always set.
/// </para>
/// </summary>
public class AddSiteEventLogHealthMetricsBridgeTests
{
[Fact]
public void AddSiteEventLogHealthMetricsBridge_IsIdempotent_DoesNotDoubleRegister_HostedService()
{
// M2.16 (#30): calling the bridge method twice must register exactly one
// SiteEventLogFailureCountReporter. Without the marker-type guard the
// ImplementationType == check was a no-op for factory-lambda registrations,
// so the second call would have added a second hosted service (two timers).
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory, NullLoggerFactory>();
services.AddSingleton(typeof(ILogger<>), typeof(NullLogger<>));
services.AddHealthMonitoring();
Func<IServiceProvider, Func<long>> factory = _ => () => 0L;
services.AddSiteEventLogHealthMetricsBridge(factory);
services.AddSiteEventLogHealthMetricsBridge(factory);
// Count IHostedService descriptors whose factory produces a
// SiteEventLogFailureCountReporter. Because it is factory-registered,
// ImplementationType is null — we count by resolving and checking type.
using var provider = services.BuildServiceProvider();
var reporters = provider.GetServices<IHostedService>()
.OfType<SiteEventLogFailureCountReporter>()
.ToList();
Assert.Single(reporters);
}
}