Files
lmxopcua/tests/Core/ZB.MOM.WW.OtOpcUa.Core.Tests/GenericDriverNodeManagerTests.cs
Joseph Doherty 8be6afbda4 fix(core): resolve Low code-review findings (Core-004,008,009,010,011,012)
- Core-004: add ConfigureAwait(false) to DriverHost.RegisterAsync /
  UnregisterAsync / DisposeAsync.
- Core-008: rewrite the BuildAddressSpaceAsync XML doc to correctly name
  the caller (OpcUaApplicationHost.PopulateAddressSpaces) that owns the
  per-driver isolation.
- Core-009: snapshot DriverResilienceOptions once per non-idempotent write
  in CapabilityInvoker.ExecuteWriteAsync.
- Core-010: switch DriverResilienceOptions.Resolve to TryGetValue with a
  diagnostic error message when a tier table is missing a capability.
- Core-011: add an optional diagnostic callback to PermissionTrieBuilder
  so production callers can surface scope-path mismatches.
- Core-012: correct the stale WedgeDetector ctor summary and add the
  Reconnecting row to DriverHealthReport's state matrix.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-23 05:38:09 -04:00

254 lines
12 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Core.OpcUa;
namespace ZB.MOM.WW.OtOpcUa.Core.Tests;
[Trait("Category", "Unit")]
public sealed class GenericDriverNodeManagerTests
{
/// <summary>
/// BuildAddressSpaceAsync walks the driver's discovery through the caller's builder. Every
/// variable marked with MarkAsAlarmCondition captures its sink in the node manager; later,
/// IAlarmSource.OnAlarmEvent payloads are routed by SourceNodeId to the matching sink.
/// This is the plumbing that PR 16's concrete OPC UA builder will use to update the actual
/// AlarmConditionState nodes.
/// </summary>
[Fact]
public async Task Alarm_events_are_routed_to_the_sink_registered_for_the_matching_source_node_id()
{
var driver = new FakeDriver();
var builder = new RecordingBuilder();
using var nm = new GenericDriverNodeManager(driver);
await nm.BuildAddressSpaceAsync(builder, CancellationToken.None);
builder.Alarms.Count.ShouldBe(2);
nm.TrackedAlarmSources.Count.ShouldBe(2);
// Simulate the driver raising a transition for one of the alarms.
var args = new AlarmEventArgs(
SubscriptionHandle: new FakeHandle("s1"),
SourceNodeId: "Tank.HiHi",
ConditionId: "cond-1",
AlarmType: "Tank.HiHi",
Message: "Level exceeded",
Severity: AlarmSeverity.High,
SourceTimestampUtc: DateTime.UtcNow);
driver.RaiseAlarm(args);
builder.Alarms["Tank.HiHi"].Received.Count.ShouldBe(1);
builder.Alarms["Tank.HiHi"].Received[0].Message.ShouldBe("Level exceeded");
// The other alarm sink never received a payload — fan-out is tag-scoped.
builder.Alarms["Heater.OverTemp"].Received.Count.ShouldBe(0);
}
[Fact]
public async Task Non_alarm_variables_do_not_register_sinks()
{
var driver = new FakeDriver();
var builder = new RecordingBuilder();
using var nm = new GenericDriverNodeManager(driver);
await nm.BuildAddressSpaceAsync(builder, CancellationToken.None);
// FakeDriver registers 2 alarm-bearing variables + 1 plain variable.
nm.TrackedAlarmSources.ShouldNotContain("Tank.Level"); // the plain one
}
[Fact]
public async Task Unknown_source_node_id_is_dropped_silently()
{
var driver = new FakeDriver();
var builder = new RecordingBuilder();
using var nm = new GenericDriverNodeManager(driver);
await nm.BuildAddressSpaceAsync(builder, CancellationToken.None);
driver.RaiseAlarm(new AlarmEventArgs(
new FakeHandle("s1"), "Unknown.Source", "c", "t", "m", AlarmSeverity.Low, DateTime.UtcNow));
builder.Alarms.Values.All(s => s.Received.Count == 0).ShouldBeTrue();
}
[Fact]
public async Task Dispose_unsubscribes_from_OnAlarmEvent()
{
var driver = new FakeDriver();
var builder = new RecordingBuilder();
var nm = new GenericDriverNodeManager(driver);
await nm.BuildAddressSpaceAsync(builder, CancellationToken.None);
nm.Dispose();
driver.RaiseAlarm(new AlarmEventArgs(
new FakeHandle("s1"), "Tank.HiHi", "c", "t", "m", AlarmSeverity.Low, DateTime.UtcNow));
// No sink should have received it — the forwarder was detached.
builder.Alarms["Tank.HiHi"].Received.Count.ShouldBe(0);
}
/// <summary>
/// Core-006 regression: a second call to BuildAddressSpaceAsync (e.g. on Galaxy redeploy)
/// must unsubscribe the old alarm forwarder and clear the sink registry before re-walking,
/// so alarm transitions are not delivered twice.
/// </summary>
[Fact]
public async Task Second_BuildAddressSpaceAsync_Does_Not_Double_Fire_Alarms()
{
var driver = new FakeDriver();
var builder1 = new RecordingBuilder();
var builder2 = new RecordingBuilder();
using var nm = new GenericDriverNodeManager(driver);
await nm.BuildAddressSpaceAsync(builder1, CancellationToken.None);
await nm.BuildAddressSpaceAsync(builder2, CancellationToken.None); // redeploy
driver.RaiseAlarm(new AlarmEventArgs(
new FakeHandle("s1"), "Tank.HiHi", "c", "t", "m", AlarmSeverity.High, DateTime.UtcNow));
// Only the second builder's sink should have received the event.
builder2.Alarms["Tank.HiHi"].Received.Count.ShouldBe(1,
"second BuildAddressSpaceAsync must replace the subscription — not add to it");
// The first builder's sink should NOT have received it (old forwarder was detached).
builder1.Alarms.TryGetValue("Tank.HiHi", out var oldSink);
(oldSink?.Received.Count ?? 0).ShouldBe(0,
"the original alarm forwarder must be unsubscribed on the second build");
}
[Fact]
public async Task Second_BuildAddressSpaceAsync_Clears_Old_Sink_Registry()
{
var driver = new FakeDriver();
using var nm = new GenericDriverNodeManager(driver);
await nm.BuildAddressSpaceAsync(new RecordingBuilder(), CancellationToken.None);
var countAfterFirst = nm.TrackedAlarmSources.Count;
await nm.BuildAddressSpaceAsync(new RecordingBuilder(), CancellationToken.None);
var countAfterSecond = nm.TrackedAlarmSources.Count;
countAfterFirst.ShouldBe(2, "FakeDriver registers 2 alarm sources");
countAfterSecond.ShouldBe(2, "second build must re-register exactly the same sources, not accumulate");
}
[Fact]
public async Task BuildAddressSpaceAsync_After_Dispose_Throws_ObjectDisposedException()
{
var driver = new FakeDriver();
var nm = new GenericDriverNodeManager(driver);
nm.Dispose();
await Should.ThrowAsync<ObjectDisposedException>(() =>
nm.BuildAddressSpaceAsync(new RecordingBuilder(), CancellationToken.None));
}
/// <summary>
/// Core-008 regression: the XML doc states exception isolation is the caller's
/// responsibility — exceptions from <see cref="ITagDiscovery.DiscoverAsync"/> must propagate
/// out of <c>BuildAddressSpaceAsync</c> unhandled so the Server layer's per-driver try/catch
/// (<c>OpcUaApplicationHost.PopulateAddressSpaces</c>) can mark the subtree Faulted.
/// </summary>
[Fact]
public async Task BuildAddressSpaceAsync_Propagates_Discovery_Exceptions_To_Caller()
{
var driver = new ThrowingDiscoveryDriver();
using var nm = new GenericDriverNodeManager(driver);
var ex = await Should.ThrowAsync<InvalidOperationException>(() =>
nm.BuildAddressSpaceAsync(new RecordingBuilder(), CancellationToken.None));
ex.Message.ShouldBe("discovery boom",
"exceptions from DiscoverAsync must propagate unhandled — exception isolation is the caller's responsibility (e.g. OpcUaApplicationHost)");
}
/// <summary>Driver whose DiscoverAsync throws — exercises the exception-isolation boundary.</summary>
private sealed class ThrowingDiscoveryDriver : IDriver, ITagDiscovery
{
public string DriverInstanceId => "throwing";
public string DriverType => "Throwing";
public Task InitializeAsync(string _, CancellationToken __) => Task.CompletedTask;
public Task ReinitializeAsync(string _, CancellationToken __) => Task.CompletedTask;
public Task ShutdownAsync(CancellationToken _) => Task.CompletedTask;
public DriverHealth GetHealth() => new(DriverState.Healthy, null, null);
public long GetMemoryFootprint() => 0;
public Task FlushOptionalCachesAsync(CancellationToken _) => Task.CompletedTask;
public Task DiscoverAsync(IAddressSpaceBuilder builder, CancellationToken ct)
=> throw new InvalidOperationException("discovery boom");
}
// --- test doubles ---
private sealed class FakeDriver : IDriver, ITagDiscovery, IAlarmSource
{
public string DriverInstanceId => "fake";
public string DriverType => "Fake";
public event EventHandler<AlarmEventArgs>? OnAlarmEvent;
public Task InitializeAsync(string driverConfigJson, CancellationToken ct) => Task.CompletedTask;
public Task ReinitializeAsync(string driverConfigJson, CancellationToken ct) => Task.CompletedTask;
public Task ShutdownAsync(CancellationToken ct) => Task.CompletedTask;
public DriverHealth GetHealth() => new(DriverState.Healthy, DateTime.UtcNow, null);
public long GetMemoryFootprint() => 0;
public Task FlushOptionalCachesAsync(CancellationToken ct) => Task.CompletedTask;
public Task DiscoverAsync(IAddressSpaceBuilder builder, CancellationToken ct)
{
var folder = builder.Folder("Tank", "Tank");
var lvl = folder.Variable("Level", "Level", new DriverAttributeInfo(
"Tank.Level", DriverDataType.Float64, false, null, SecurityClassification.FreeAccess, false, IsAlarm: false));
var hiHi = folder.Variable("HiHi", "HiHi", new DriverAttributeInfo(
"Tank.HiHi", DriverDataType.Boolean, false, null, SecurityClassification.FreeAccess, false, IsAlarm: true));
hiHi.MarkAsAlarmCondition(new AlarmConditionInfo("Tank.HiHi", AlarmSeverity.High, "High-high alarm"));
var heater = builder.Folder("Heater", "Heater");
var ot = heater.Variable("OverTemp", "OverTemp", new DriverAttributeInfo(
"Heater.OverTemp", DriverDataType.Boolean, false, null, SecurityClassification.FreeAccess, false, IsAlarm: true));
ot.MarkAsAlarmCondition(new AlarmConditionInfo("Heater.OverTemp", AlarmSeverity.Critical, "Over-temperature"));
return Task.CompletedTask;
}
public void RaiseAlarm(AlarmEventArgs args) => OnAlarmEvent?.Invoke(this, args);
public Task<IAlarmSubscriptionHandle> SubscribeAlarmsAsync(IReadOnlyList<string> _, CancellationToken __)
=> Task.FromResult<IAlarmSubscriptionHandle>(new FakeHandle("sub"));
public Task UnsubscribeAlarmsAsync(IAlarmSubscriptionHandle _, CancellationToken __) => Task.CompletedTask;
public Task AcknowledgeAsync(IReadOnlyList<AlarmAcknowledgeRequest> _, CancellationToken __) => Task.CompletedTask;
}
private sealed class FakeHandle(string diagnosticId) : IAlarmSubscriptionHandle
{
public string DiagnosticId { get; } = diagnosticId;
}
private sealed class RecordingBuilder : IAddressSpaceBuilder
{
public Dictionary<string, RecordingSink> Alarms { get; } = new(StringComparer.OrdinalIgnoreCase);
public IAddressSpaceBuilder Folder(string _, string __) => this;
public IVariableHandle Variable(string _, string __, DriverAttributeInfo info)
=> new Handle(info.FullName, Alarms);
public void AddProperty(string _, DriverDataType __, object? ___) { }
public sealed class Handle(string fullRef, Dictionary<string, RecordingSink> alarms) : IVariableHandle
{
public string FullReference { get; } = fullRef;
public IAlarmConditionSink MarkAsAlarmCondition(AlarmConditionInfo _)
{
var sink = new RecordingSink();
alarms[FullReference] = sink;
return sink;
}
}
public sealed class RecordingSink : IAlarmConditionSink
{
public List<AlarmEventArgs> Received { get; } = new();
public void OnTransition(AlarmEventArgs args) => Received.Add(args);
}
}
}