Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.SiteRuntime.Tests/TestSupport/FakeSiteEventLogger.cs
T
Joseph Doherty e74c3aef23 feat(siteeventlog): emit script started/completed Info events (M1.8)
ScriptExecutionActor previously emitted only an Error 'script' event on failure.
It now also fire-and-forgets an Info 'script' event when execution starts (right
before RunAsync) and when it completes successfully — giving the operational log
the full started/completed/failed lifecycle. Uses the already-resolved
siteEventLogger; fire-and-forget so the event log can never block or fault the
script's own run.

Extends the SingleServiceProvider test helper to also serve IServiceScopeFactory
(returning a self-scope) so ScriptExecutionActor's serviceProvider.CreateScope()
reaches the logging hot path in tests instead of throwing into the catch.
2026-06-15 12:33:31 -04:00

84 lines
2.9 KiB
C#

using System.Collections.Concurrent;
using Microsoft.Extensions.DependencyInjection;
using ZB.MOM.WW.ScadaBridge.SiteEventLogging;
namespace ZB.MOM.WW.ScadaBridge.SiteRuntime.Tests.TestSupport;
/// <summary>
/// M1 Site Event Logging categories: 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>
public IReadOnlyList<Entry> OfType(string eventType) =>
_entries.Where(e => e.EventType == eventType).ToArray();
/// <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;
}
/// <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&lt;ISiteEventLogger&gt;()</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>
public sealed class SingleServiceProvider(ISiteEventLogger logger)
: IServiceProvider, IServiceScopeFactory, IServiceScope
{
private readonly ISiteEventLogger _logger = logger;
/// <inheritdoc />
public object? GetService(Type serviceType)
{
if (serviceType == typeof(ISiteEventLogger)) return _logger;
if (serviceType == typeof(IServiceScopeFactory)) return this;
return null;
}
/// <inheritdoc />
public IServiceScope CreateScope() => this;
/// <inheritdoc />
public IServiceProvider ServiceProvider => this;
/// <inheritdoc />
public void Dispose() { }
}