513fa1aa16
Comment/XML-doc changes only: - add summaries to ~10k test methods, fixtures, fakes and helpers (+ <param> on theories, <returns> on async Task methods, <inheritdoc/> on interface implementations the checker can resolve) - strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N, #NN, Component-0NN) from comments, keeping the explanatory text; reword false positives in comments only - real docs on fakes implementing BCL/library interfaces (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime) - reword the last 5 src/ false positives (CWE-1236, failures-1, year-0001, M365) Only non-comment edits: four one-line nested classes split across lines so members can carry docs. Build 0 errors / 0 warnings.
50 lines
2.3 KiB
C#
50 lines
2.3 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>
|
|
/// 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
|
|
{
|
|
/// <summary>Verifies that adding the site event log health metrics bridge twice registers the hosted service only once.</summary>
|
|
[Fact]
|
|
public void AddSiteEventLogHealthMetricsBridge_IsIdempotent_DoesNotDoubleRegister_HostedService()
|
|
{
|
|
// 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);
|
|
}
|
|
}
|