feat(scripting): root script logger + DPS publisher wired in Host
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
using Serilog;
|
||||
using Serilog.Core;
|
||||
using Serilog.Events;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Logging;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Core.Scripting.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Verifies the root script-logger fan-out: events flowing through the composed
|
||||
/// pipeline (rolling file + companion mirror + topic sink) reach the
|
||||
/// <see cref="IScriptLogPublisher"/> topic sink only when they meet the configured
|
||||
/// minimum level, carrying their <c>ScriptId</c> identity property.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Builds the identical <see cref="LoggerConfiguration"/> that the Host's
|
||||
/// <c>ScriptRootLoggerFactory.Build</c> composes (file + <see cref="ScriptLogCompanionSink"/>
|
||||
/// + <see cref="ScriptLogTopicSink"/>). The factory itself is a thin shell over this
|
||||
/// exact wiring; testing the composition here keeps the suite a pure unit test with no
|
||||
/// dependency on the Host's Web SDK executable.
|
||||
/// </remarks>
|
||||
[Trait("Category", "Unit")]
|
||||
public sealed class ScriptRootLoggerFanoutTests : IDisposable
|
||||
{
|
||||
private readonly string _tempFile = Path.Combine(
|
||||
Path.GetTempPath(), $"scripts-fanout-{Guid.NewGuid():N}-.log");
|
||||
|
||||
private sealed class FakePublisher : IScriptLogPublisher
|
||||
{
|
||||
/// <summary>Gets the entries that have been published to the topic.</summary>
|
||||
public List<ScriptLogEntry> Published { get; } = [];
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Publish(ScriptLogEntry entry) => Published.Add(entry);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds the same pipeline the Host's <c>ScriptRootLoggerFactory.Build</c> composes,
|
||||
/// so this test exercises the real production sink fan-out.
|
||||
/// </summary>
|
||||
private Logger BuildRootLogger(FakePublisher publisher, LogEventLevel topicMinLevel) =>
|
||||
new LoggerConfiguration()
|
||||
.MinimumLevel.Verbose()
|
||||
.WriteTo.File(_tempFile, rollingInterval: RollingInterval.Day)
|
||||
.WriteTo.Sink(new ScriptLogCompanionSink(Serilog.Log.Logger))
|
||||
.WriteTo.Sink(new ScriptLogTopicSink(publisher, topicMinLevel))
|
||||
.CreateLogger();
|
||||
|
||||
/// <summary>
|
||||
/// An Information event flows to the topic sink as a single <see cref="ScriptLogEntry"/>
|
||||
/// carrying the bound <c>ScriptId</c>.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Information_event_reaches_the_topic_sink_with_its_ScriptId()
|
||||
{
|
||||
var publisher = new FakePublisher();
|
||||
using var root = BuildRootLogger(publisher, LogEventLevel.Information);
|
||||
|
||||
root.ForContext(ScriptLoggerFactory.ScriptIdProperty, "S1")
|
||||
.Information("virtual tag recomputed");
|
||||
|
||||
publisher.Published.Count.ShouldBe(1);
|
||||
publisher.Published[0].ScriptId.ShouldBe("S1");
|
||||
publisher.Published[0].Level.ShouldBe("Information");
|
||||
publisher.Published[0].Message.ShouldBe("virtual tag recomputed");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A Debug event is gated out by the topic sink's Information minimum level, so the
|
||||
/// publisher receives nothing (it still lands in the file sink, which we don't assert).
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Debug_event_is_gated_out_by_the_topic_minimum_level()
|
||||
{
|
||||
var publisher = new FakePublisher();
|
||||
using var root = BuildRootLogger(publisher, LogEventLevel.Information);
|
||||
|
||||
root.ForContext(ScriptLoggerFactory.ScriptIdProperty, "S1")
|
||||
.Debug("verbose diagnostic noise");
|
||||
|
||||
publisher.Published.ShouldBeEmpty();
|
||||
}
|
||||
|
||||
/// <summary>Disposes the temp log file created by the rolling file sink.</summary>
|
||||
public void Dispose()
|
||||
{
|
||||
// Serilog appends a date stamp where the trailing '-' sits; clean any matching file.
|
||||
var dir = Path.GetDirectoryName(_tempFile)!;
|
||||
var prefix = Path.GetFileNameWithoutExtension(_tempFile);
|
||||
try
|
||||
{
|
||||
foreach (var f in Directory.EnumerateFiles(dir, prefix + "*"))
|
||||
File.Delete(f);
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
// Best-effort cleanup — a locked file on a flaky CI box must not fail the test.
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
@@ -13,6 +13,10 @@
|
||||
<PackageReference Include="xunit.v3"/>
|
||||
<PackageReference Include="Shouldly"/>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk"/>
|
||||
<!-- The root-logger fan-out test composes the same rolling-file + sink pipeline the
|
||||
Host's ScriptRootLoggerFactory builds; Core.Scripting references only Serilog core,
|
||||
so the File sink must be pulled in here for WriteTo.File. -->
|
||||
<PackageReference Include="Serilog.Sinks.File"/>
|
||||
<PackageReference Include="xunit.runner.visualstudio">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
using Akka.Actor;
|
||||
using Akka.Cluster.Tools.PublishSubscribe;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Logging;
|
||||
using ZB.MOM.WW.OtOpcUa.Runtime.Scripting;
|
||||
using ZB.MOM.WW.OtOpcUa.Runtime.Tests.Harness;
|
||||
using ZB.MOM.WW.OtOpcUa.Runtime.VirtualTags;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.Scripting;
|
||||
|
||||
/// <summary>
|
||||
/// Verifies <see cref="DpsScriptLogPublisher"/> routes a <see cref="ScriptLogEntry"/>
|
||||
/// onto the Akka DistributedPubSub <c>script-logs</c> topic and never throws back into
|
||||
/// the calling logging pipeline.
|
||||
/// </summary>
|
||||
public sealed class DpsScriptLogPublisherTests : RuntimeActorTestBase
|
||||
{
|
||||
/// <summary>Builds a representative entry for assertions.</summary>
|
||||
private static ScriptLogEntry SampleEntry() => new(
|
||||
ScriptId: "S1",
|
||||
Level: "Information",
|
||||
Message: "hello from script",
|
||||
TimestampUtc: DateTime.UtcNow,
|
||||
VirtualTagId: "V1",
|
||||
AlarmId: null,
|
||||
EquipmentId: "EQ1");
|
||||
|
||||
/// <summary>
|
||||
/// A published entry is delivered verbatim to a probe subscribed to the
|
||||
/// <see cref="VirtualTagActor.ScriptLogsTopic"/> topic via the DPS mediator.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Publish_routes_entry_to_subscribers_of_the_script_logs_topic()
|
||||
{
|
||||
var probe = CreateTestProbe();
|
||||
var mediator = DistributedPubSub.Get(Sys).Mediator;
|
||||
// Send the Subscribe FROM the probe so the SubscribeAck returns to the probe's mailbox
|
||||
// (a bare mediator.Tell would route the ack to the implicit TestActor instead).
|
||||
probe.Send(mediator, new Subscribe(VirtualTagActor.ScriptLogsTopic, probe.Ref));
|
||||
probe.ExpectMsg<SubscribeAck>(TimeSpan.FromSeconds(10));
|
||||
|
||||
var publisher = new DpsScriptLogPublisher(() => Sys);
|
||||
var entry = SampleEntry();
|
||||
|
||||
// The SubscribeAck confirms the subscribe was registered, but DPS topic membership is
|
||||
// eventually-consistent — publish in a short retry loop until the probe sees the entry
|
||||
// (mirrors the Host.IntegrationTests DPS round-trip pattern), then assert the payload.
|
||||
AwaitAssert(() =>
|
||||
{
|
||||
publisher.Publish(entry);
|
||||
probe.ExpectMsg<ScriptLogEntry>(TimeSpan.FromMilliseconds(200)).ShouldBe(entry);
|
||||
}, duration: TimeSpan.FromSeconds(5), interval: TimeSpan.FromMilliseconds(250));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A logging sink must never throw into the logging pipeline: if the system
|
||||
/// accessor throws, <see cref="DpsScriptLogPublisher.Publish"/> swallows it.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Publish_does_not_throw_when_the_system_accessor_throws()
|
||||
{
|
||||
var publisher = new DpsScriptLogPublisher(
|
||||
() => throw new InvalidOperationException("system not ready"));
|
||||
|
||||
Should.NotThrow(() => publisher.Publish(SampleEntry()));
|
||||
}
|
||||
|
||||
/// <summary>The constructor rejects a null system accessor.</summary>
|
||||
[Fact]
|
||||
public void Null_system_accessor_throws_ArgumentNullException()
|
||||
{
|
||||
Should.Throw<ArgumentNullException>(() => new DpsScriptLogPublisher(null!));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user