111adc92b6
The continuous-historization recorder conflated two identifiers into one string: the dependency mux fans DependencyValueChanged keyed by the driver FullName (the mux ref), but a value must be historized under the resolved historian name (HistorianTagname override, else FullName). In the common no-override case the two are equal, so it worked; with an override they diverge and the recorder registered mux interest under a key the mux never fans — that tag's values were never captured (and, had they been, would have been written under the mux ref). Carry BOTH identifiers through the seam: a new HistorizedTagRef(MuxRef, HistorianName) record on IHistorizedTagSubscriptionSink. The applier resolves MuxRef = FullName and HistorianName = override-or-FullName. The recorder now keeps a muxRef->historianName map: it registers/filters mux interest by MuxRef but writes the outbox entry (and drains) under HistorianName. The convergence handler re-registers the mux only when the registered key-set changes, so an override-only rename (same FullName) updates the write target without mux churn. Tests: a divergent-override recorder test (interest by mux ref, value written under the override name, never the mux ref) + an override-rename no-churn test; the applier feed tests now assert the full (mux ref, historian name) pairs. Runtime 348/0, OpcUaServer 327/0; 0 warnings. Closes FU-3. Claude-Session: https://claude.ai/code/session_012SDSQ3AcaXqPcBtDESBRii
275 lines
13 KiB
C#
275 lines
13 KiB
C#
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests;
|
|
|
|
/// <summary>
|
|
/// T15 — verifies the non-blocking historian-provisioning hook in
|
|
/// <see cref="AddressSpaceApplier.Apply"/>. The hook fires AFTER the address-space work and
|
|
/// dispatches <see cref="IHistorianProvisioning.EnsureTagsAsync"/> fire-and-forget, so a slow or
|
|
/// throwing provisioner can never block or break a deploy on the OPC UA publish actor's pinned
|
|
/// thread.
|
|
/// </summary>
|
|
public sealed class AddressSpaceApplierProvisioningTests
|
|
{
|
|
/// <summary>Capturing <see cref="IHistorianProvisioning"/> double. Records the requests it was
|
|
/// handed and signals a <see cref="TaskCompletionSource"/> when invoked, so a test can await the
|
|
/// fire-and-forget dispatch deterministically (never poll/sleep). A <see cref="Throw"/> flag
|
|
/// simulates a synchronous provisioner fault.</summary>
|
|
private sealed class CapturingProvisioner : IHistorianProvisioning
|
|
{
|
|
private readonly TaskCompletionSource _called = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
|
|
/// <summary>Gets the requests the hook handed to <see cref="EnsureTagsAsync"/>.</summary>
|
|
public List<HistorianTagProvisionRequest> Seen { get; } = new();
|
|
|
|
/// <summary>When true, <see cref="EnsureTagsAsync"/> throws synchronously (a fault before any await).</summary>
|
|
public bool Throw { get; init; }
|
|
|
|
/// <summary>Completes once <see cref="EnsureTagsAsync"/> has been invoked.</summary>
|
|
public Task Called => _called.Task;
|
|
|
|
/// <inheritdoc />
|
|
public Task<HistorianProvisionResult> EnsureTagsAsync(
|
|
IReadOnlyList<HistorianTagProvisionRequest> requests, CancellationToken ct)
|
|
{
|
|
if (Throw)
|
|
{
|
|
_called.TrySetResult();
|
|
throw new InvalidOperationException("boom");
|
|
}
|
|
|
|
Seen.AddRange(requests);
|
|
_called.TrySetResult();
|
|
return Task.FromResult(new HistorianProvisionResult(requests.Count, requests.Count, 0, 0));
|
|
}
|
|
}
|
|
|
|
/// <summary>The hook provisions ONLY historized added tags, with the resolved historian name
|
|
/// (override when set, else the driver-side FullName).</summary>
|
|
[Fact]
|
|
public async Task Apply_provisions_only_historized_added_tags()
|
|
{
|
|
var prov = new CapturingProvisioner();
|
|
var applier = new AddressSpaceApplier(NullOpcUaAddressSpaceSink.Instance, NullLogger<AddressSpaceApplier>.Instance, prov);
|
|
|
|
// Leaf display name "Temp"; historian override "Pump1.Temp".
|
|
var plan = PlanWithAddedTags(
|
|
HistorizedTag(displayName: "Temp", historianName: "Pump1.Temp", dataType: "Float32"),
|
|
NonHistorizedTag(displayName: "Run", dataType: "Boolean"));
|
|
|
|
var outcome = applier.Apply(plan);
|
|
|
|
outcome.RebuildCalled.ShouldBeTrue();
|
|
|
|
// Fire-and-forget: await the capturing double's signal so the assertion is deterministic.
|
|
await prov.Called.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
|
prov.Seen.Count.ShouldBe(1);
|
|
prov.Seen[0].TagName.ShouldBe("Pump1.Temp"); // resolved historian name (override)
|
|
prov.Seen[0].DataType.ShouldBe(DriverDataType.Float32);
|
|
prov.Seen[0].Description.ShouldBe("Temp"); // leaf display name
|
|
}
|
|
|
|
/// <summary>A null/blank historian-name override resolves to the driver-side FullName — mirroring
|
|
/// the materialiser's resolution exactly.</summary>
|
|
[Fact]
|
|
public async Task Apply_resolves_historian_name_from_fullname_when_override_blank()
|
|
{
|
|
var prov = new CapturingProvisioner();
|
|
var applier = new AddressSpaceApplier(NullOpcUaAddressSpaceSink.Instance, NullLogger<AddressSpaceApplier>.Instance, prov);
|
|
|
|
// IsHistorized but no override → historian name falls back to FullName ("40001").
|
|
var plan = PlanWithAddedTags(
|
|
HistorizedTag(displayName: "Speed", historianName: null, dataType: "Int32", fullName: "40001"));
|
|
|
|
applier.Apply(plan);
|
|
|
|
await prov.Called.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
|
prov.Seen.Count.ShouldBe(1);
|
|
prov.Seen[0].TagName.ShouldBe("40001");
|
|
prov.Seen[0].DataType.ShouldBe(DriverDataType.Int32);
|
|
}
|
|
|
|
/// <summary>A synchronously-throwing provisioner must NOT block or break the publish: the
|
|
/// synchronous <see cref="AddressSpaceApplier.Apply"/> still completes its address-space work and
|
|
/// returns its normal outcome.</summary>
|
|
[Fact]
|
|
public void Provisioner_throw_does_not_block_publish()
|
|
{
|
|
var applier = new AddressSpaceApplier(
|
|
NullOpcUaAddressSpaceSink.Instance,
|
|
NullLogger<AddressSpaceApplier>.Instance,
|
|
new CapturingProvisioner { Throw = true });
|
|
|
|
var outcome = applier.Apply(PlanWithAddedTags(
|
|
HistorizedTag(displayName: "Temp", historianName: "Pump1.Temp", dataType: "Float32")));
|
|
|
|
outcome.RebuildCalled.ShouldBeTrue(); // address-space work still completed
|
|
}
|
|
|
|
/// <summary>The default ctor (no provisioner) binds the no-op <see cref="NullHistorianProvisioning"/>
|
|
/// and never faults a deploy — preserving every existing call site.</summary>
|
|
[Fact]
|
|
public void Default_ctor_uses_null_provisioning_and_does_not_throw()
|
|
{
|
|
var applier = new AddressSpaceApplier(NullOpcUaAddressSpaceSink.Instance, NullLogger<AddressSpaceApplier>.Instance);
|
|
|
|
var outcome = applier.Apply(PlanWithAddedTags(
|
|
HistorizedTag(displayName: "Temp", historianName: "Pump1.Temp", dataType: "Float32")));
|
|
|
|
outcome.RebuildCalled.ShouldBeTrue();
|
|
}
|
|
|
|
/// <summary>An added historized tag whose DataType string is not a <see cref="DriverDataType"/> is
|
|
/// skipped (no request) — the hook never throws on an unparseable type.</summary>
|
|
[Fact]
|
|
public async Task Apply_skips_added_tag_with_unparseable_datatype()
|
|
{
|
|
var prov = new CapturingProvisioner();
|
|
var applier = new AddressSpaceApplier(NullOpcUaAddressSpaceSink.Instance, NullLogger<AddressSpaceApplier>.Instance, prov);
|
|
|
|
// "Float" is NOT a DriverDataType member (the members are Float32/Float64); it must be skipped.
|
|
var plan = PlanWithAddedTags(
|
|
HistorizedTag(displayName: "Bad", historianName: "Pump1.Bad", dataType: "Float"),
|
|
HistorizedTag(displayName: "Good", historianName: "Pump1.Good", dataType: "Float32"));
|
|
|
|
applier.Apply(plan);
|
|
|
|
await prov.Called.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
|
prov.Seen.Count.ShouldBe(1);
|
|
prov.Seen[0].TagName.ShouldBe("Pump1.Good");
|
|
}
|
|
|
|
/// <summary>Capturing <see cref="IHistorizedTagSubscriptionSink"/> double. Records the add/remove
|
|
/// ref deltas the applier feeds it. A <see cref="Throw"/> flag simulates a faulting feed.</summary>
|
|
private sealed class CapturingSubscriptionSink : IHistorizedTagSubscriptionSink
|
|
{
|
|
/// <summary>Ref pairs the applier fed as ADDED (mux ref + historian name).</summary>
|
|
public List<HistorizedTagRef> Added { get; } = new();
|
|
|
|
/// <summary>Ref pairs the applier fed as REMOVED.</summary>
|
|
public List<HistorizedTagRef> Removed { get; } = new();
|
|
|
|
/// <summary>When true, <see cref="UpdateHistorizedRefs"/> throws synchronously.</summary>
|
|
public bool Throw { get; init; }
|
|
|
|
/// <inheritdoc />
|
|
public void UpdateHistorizedRefs(IReadOnlyList<HistorizedTagRef> added, IReadOnlyList<HistorizedTagRef> removed)
|
|
{
|
|
if (Throw) throw new InvalidOperationException("boom");
|
|
Added.AddRange(added);
|
|
Removed.AddRange(removed);
|
|
}
|
|
}
|
|
|
|
/// <summary>The feed pushes ONLY historized added refs, resolved (override-or-FullName) exactly like
|
|
/// the provisioning hook — non-historized tags never reach the recorder.</summary>
|
|
[Fact]
|
|
public void Apply_feeds_historized_added_refs_to_the_subscription_sink()
|
|
{
|
|
var sink = new CapturingSubscriptionSink();
|
|
var applier = new AddressSpaceApplier(
|
|
NullOpcUaAddressSpaceSink.Instance, NullLogger<AddressSpaceApplier>.Instance,
|
|
historizedSubscriptions: sink);
|
|
|
|
var plan = PlanWithAddedTags(
|
|
HistorizedTag(displayName: "Temp", historianName: "Pump1.Temp", dataType: "Float32"),
|
|
HistorizedTag(displayName: "Speed", historianName: null, dataType: "Int32", fullName: "40001"),
|
|
NonHistorizedTag(displayName: "Run", dataType: "Boolean"));
|
|
|
|
applier.Apply(plan);
|
|
|
|
// Each ref carries BOTH identifiers: the override tag feeds (mux ref = FullName "ref", historian
|
|
// name = override "Pump1.Temp"); the no-override tag feeds (FullName "40001" as both).
|
|
sink.Added.ShouldBe(new[]
|
|
{
|
|
new HistorizedTagRef("ref", "Pump1.Temp"),
|
|
new HistorizedTagRef("40001", "40001"),
|
|
}, ignoreOrder: true);
|
|
sink.Removed.ShouldBeEmpty();
|
|
}
|
|
|
|
/// <summary>Removed historized tags are fed as REMOVED refs; a changed tag whose historian override is
|
|
/// renamed feeds the old ref removed + the new ref added (the recorder converges the full set).</summary>
|
|
[Fact]
|
|
public void Apply_feeds_removed_and_renamed_historized_refs()
|
|
{
|
|
var sink = new CapturingSubscriptionSink();
|
|
var applier = new AddressSpaceApplier(
|
|
NullOpcUaAddressSpaceSink.Instance, NullLogger<AddressSpaceApplier>.Instance,
|
|
historizedSubscriptions: sink);
|
|
|
|
var removedTag = HistorizedTag(displayName: "Old", historianName: "Pump1.Old", dataType: "Float32");
|
|
// Same TagId ("tag-T"), historian override renamed A → B (both historized) → remove A, add B.
|
|
var prev = HistorizedTag(displayName: "T", historianName: "Pump1.A", dataType: "Float32");
|
|
var cur = HistorizedTag(displayName: "T", historianName: "Pump1.B", dataType: "Float32");
|
|
|
|
var plan = new AddressSpacePlan(
|
|
AddedEquipment: Array.Empty<EquipmentNode>(),
|
|
RemovedEquipment: Array.Empty<EquipmentNode>(),
|
|
ChangedEquipment: Array.Empty<AddressSpacePlan.EquipmentDelta>(),
|
|
AddedDrivers: Array.Empty<DriverInstancePlan>(),
|
|
RemovedDrivers: Array.Empty<DriverInstancePlan>(),
|
|
ChangedDrivers: Array.Empty<AddressSpacePlan.DriverDelta>(),
|
|
AddedAlarms: Array.Empty<ScriptedAlarmPlan>(),
|
|
RemovedAlarms: Array.Empty<ScriptedAlarmPlan>(),
|
|
ChangedAlarms: Array.Empty<AddressSpacePlan.AlarmDelta>())
|
|
{
|
|
RemovedEquipmentTags = new[] { removedTag },
|
|
ChangedEquipmentTags = new[] { new AddressSpacePlan.EquipmentTagDelta(prev, cur) },
|
|
};
|
|
|
|
applier.Apply(plan);
|
|
|
|
// All three tags default FullName "ref" (the mux ref); the override rename changes only the
|
|
// historian name, so the changed tag feeds removed (ref, Pump1.A) + added (ref, Pump1.B).
|
|
sink.Added.ShouldBe(new[] { new HistorizedTagRef("ref", "Pump1.B") }, ignoreOrder: true);
|
|
sink.Removed.ShouldBe(new[]
|
|
{
|
|
new HistorizedTagRef("ref", "Pump1.Old"),
|
|
new HistorizedTagRef("ref", "Pump1.A"),
|
|
}, ignoreOrder: true);
|
|
}
|
|
|
|
/// <summary>A synchronously-throwing subscription sink must NOT block or break the publish — the
|
|
/// address-space work still completes and <see cref="AddressSpaceApplier.Apply"/> returns its outcome.</summary>
|
|
[Fact]
|
|
public void Subscription_sink_throw_does_not_block_publish()
|
|
{
|
|
var applier = new AddressSpaceApplier(
|
|
NullOpcUaAddressSpaceSink.Instance,
|
|
NullLogger<AddressSpaceApplier>.Instance,
|
|
historizedSubscriptions: new CapturingSubscriptionSink { Throw = true });
|
|
|
|
var outcome = applier.Apply(PlanWithAddedTags(
|
|
HistorizedTag(displayName: "Temp", historianName: "Pump1.Temp", dataType: "Float32")));
|
|
|
|
outcome.RebuildCalled.ShouldBeTrue(); // address-space work still completed
|
|
}
|
|
|
|
private static EquipmentTagPlan HistorizedTag(string displayName, string? historianName, string dataType, string fullName = "ref")
|
|
=> new("tag-" + displayName, "eq-1", "drv", FolderPath: "", Name: displayName, DataType: dataType, FullName: fullName,
|
|
Writable: false, Alarm: null, IsHistorized: true, HistorianTagname: historianName);
|
|
|
|
private static EquipmentTagPlan NonHistorizedTag(string displayName, string dataType)
|
|
=> new("tag-" + displayName, "eq-1", "drv", FolderPath: "", Name: displayName, DataType: dataType, FullName: "ref",
|
|
Writable: false, Alarm: null, IsHistorized: false, HistorianTagname: null);
|
|
|
|
private static AddressSpacePlan PlanWithAddedTags(params EquipmentTagPlan[] tags) => new(
|
|
AddedEquipment: Array.Empty<EquipmentNode>(),
|
|
RemovedEquipment: Array.Empty<EquipmentNode>(),
|
|
ChangedEquipment: Array.Empty<AddressSpacePlan.EquipmentDelta>(),
|
|
AddedDrivers: Array.Empty<DriverInstancePlan>(),
|
|
RemovedDrivers: Array.Empty<DriverInstancePlan>(),
|
|
ChangedDrivers: Array.Empty<AddressSpacePlan.DriverDelta>(),
|
|
AddedAlarms: Array.Empty<ScriptedAlarmPlan>(),
|
|
RemovedAlarms: Array.Empty<ScriptedAlarmPlan>(),
|
|
ChangedAlarms: Array.Empty<AddressSpacePlan.AlarmDelta>())
|
|
{
|
|
AddedEquipmentTags = tags,
|
|
};
|
|
}
|