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.
111 lines
4.7 KiB
C#
111 lines
4.7 KiB
C#
using System.Collections.Concurrent;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Options;
|
|
using ZB.MOM.WW.ScadaBridge.SiteEventLogging;
|
|
using ZB.MOM.WW.ScadaBridge.SiteRuntime.Scripts;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.SiteRuntime.Tests.TestSupport;
|
|
|
|
/// <summary>
|
|
/// A capturing fake <see cref="ISiteEventLogger"/>
|
|
/// used by the actor tests to assert that the right operational events are emitted.
|
|
/// Thread-safe — the actors fire-and-forget <c>LogEventAsync</c> from background
|
|
/// tasks, so multiple captures can land concurrently.
|
|
/// </summary>
|
|
public sealed class FakeSiteEventLogger : ISiteEventLogger
|
|
{
|
|
/// <summary>One captured <see cref="ISiteEventLogger.LogEventAsync"/> invocation.</summary>
|
|
public sealed record Entry(
|
|
string EventType,
|
|
string Severity,
|
|
string? InstanceId,
|
|
string Source,
|
|
string Message,
|
|
string? Details);
|
|
|
|
private readonly ConcurrentQueue<Entry> _entries = new();
|
|
|
|
/// <summary>All captured events, in arrival order.</summary>
|
|
public IReadOnlyList<Entry> Entries => _entries.ToArray();
|
|
|
|
/// <summary>Captured events filtered to a single category.</summary>
|
|
/// <param name="eventType">The event category to filter on.</param>
|
|
/// <returns>The captured events with the given category.</returns>
|
|
public IReadOnlyList<Entry> OfType(string eventType) =>
|
|
_entries.Where(e => e.EventType == eventType).ToArray();
|
|
|
|
// Captures the event so tests can assert on it.
|
|
/// <inheritdoc />
|
|
public Task LogEventAsync(
|
|
string eventType,
|
|
string severity,
|
|
string? instanceId,
|
|
string source,
|
|
string message,
|
|
string? details = null)
|
|
{
|
|
_entries.Enqueue(new Entry(eventType, severity, instanceId, source, message, details));
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
// Always zero for this fake.
|
|
/// <inheritdoc />
|
|
public long FailedWriteCount => 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Minimal <see cref="IServiceProvider"/> that resolves a single
|
|
/// <see cref="ISiteEventLogger"/> — enough for the actors' optional
|
|
/// <c>_serviceProvider?.GetService<ISiteEventLogger>()</c> resolution
|
|
/// without pulling a full DI container into the actor tests.
|
|
/// <para>
|
|
/// Also serves <see cref="IServiceScopeFactory"/> (returning a scope that just
|
|
/// re-exposes this provider) so callers that do
|
|
/// <c>serviceProvider.CreateScope()</c> — e.g. <c>ScriptExecutionActor</c> —
|
|
/// don't throw before they reach the logging hot path.
|
|
/// </para>
|
|
/// </summary>
|
|
/// <param name="logger">The event logger to resolve for <see cref="ISiteEventLogger"/>.</param>
|
|
/// <param name="optionsMonitor">
|
|
/// Optional <see cref="IOptionsMonitor{TOptions}"/> for <see cref="SiteRuntimeOptions"/>
|
|
/// — resolves for the hot-toggle tests; null (the default) exercises the launcher's
|
|
/// constructor-passed-options fallback, matching every earlier caller of this class.
|
|
/// </param>
|
|
/// <param name="summaryRecorder">
|
|
/// Optional <see cref="ScriptRunSummaryRecorder"/> — resolves so a test can assert on
|
|
/// the aggregate counters a run produced; null (the default) exercises the launcher's
|
|
/// null-safe skip path.
|
|
/// </param>
|
|
public sealed class SingleServiceProvider(
|
|
ISiteEventLogger logger,
|
|
IOptionsMonitor<SiteRuntimeOptions>? optionsMonitor = null,
|
|
ScriptRunSummaryRecorder? summaryRecorder = null)
|
|
: IServiceProvider, IServiceScopeFactory, IServiceScope
|
|
{
|
|
private readonly ISiteEventLogger _logger = logger;
|
|
private readonly IOptionsMonitor<SiteRuntimeOptions>? _optionsMonitor = optionsMonitor;
|
|
private readonly ScriptRunSummaryRecorder? _summaryRecorder = summaryRecorder;
|
|
|
|
/// <summary>Resolves the logger, the scope factory and the optional options monitor and summary recorder.</summary>
|
|
/// <param name="serviceType">The service type to resolve.</param>
|
|
/// <returns>The matching service, or null when the type is not served.</returns>
|
|
public object? GetService(Type serviceType)
|
|
{
|
|
if (serviceType == typeof(ISiteEventLogger)) return _logger;
|
|
if (serviceType == typeof(IServiceScopeFactory)) return this;
|
|
if (serviceType == typeof(IOptionsMonitor<SiteRuntimeOptions>)) return _optionsMonitor;
|
|
if (serviceType == typeof(ScriptRunSummaryRecorder)) return _summaryRecorder;
|
|
return null;
|
|
}
|
|
|
|
/// <summary>Creates a scope that re-exposes this provider.</summary>
|
|
/// <returns>This instance as the scope.</returns>
|
|
public IServiceScope CreateScope() => this;
|
|
|
|
/// <summary>Gets the provider for this scope, which is this instance.</summary>
|
|
public IServiceProvider ServiceProvider => this;
|
|
|
|
/// <summary>Does nothing; the scope owns no resources.</summary>
|
|
public void Dispose() { }
|
|
}
|