fix(otopcua): skip redundant discovered-node re-apply + guard tests
This commit is contained in:
+124
@@ -112,6 +112,126 @@ public sealed class DriverHostActorDiscoveryTests : RuntimeActorTestBase
|
||||
update.TimestampUtc.ShouldBe(Ts);
|
||||
}
|
||||
|
||||
/// <summary>Guard: a <see cref="DriverInstanceActor.DiscoveredNodesReady"/> arriving BEFORE any deployment
|
||||
/// is applied (<c>_lastComposition</c> still null) is ignored — nothing is materialised on the publish
|
||||
/// side (the equipment can't be resolved without a composition).</summary>
|
||||
[Fact]
|
||||
public void DiscoveredNodes_before_any_apply_are_ignored()
|
||||
{
|
||||
var db = NewInMemoryDbFactory();
|
||||
var coordinator = CreateTestProbe();
|
||||
var publish = CreateTestProbe();
|
||||
var vtHost = CreateTestProbe();
|
||||
|
||||
// No deployment dispatched ⇒ Bootstrap enters Steady with no composition ⇒ _lastComposition is null.
|
||||
var actor = Sys.ActorOf(DriverHostActor.Props(
|
||||
db, TestNode, coordinator.Ref,
|
||||
driverFactory: new SubscribingDriverFactory("Modbus"),
|
||||
localRoles: new HashSet<string> { "driver" },
|
||||
opcUaPublishActor: publish.Ref,
|
||||
virtualTagHostOverride: vtHost.Ref));
|
||||
|
||||
actor.Tell(new DriverInstanceActor.DiscoveredNodesReady("d1", new[]
|
||||
{
|
||||
new DiscoveredNode(
|
||||
FolderPathSegments: new[] { "FOCAS", "10.0.0.5:8193", "Identity" },
|
||||
BrowseName: "Model", DisplayName: "Model", FullReference: "ft-ref-1",
|
||||
DataType: DriverDataType.Float64, IsArray: false, ArrayDim: null,
|
||||
Writable: false, IsHistorized: false),
|
||||
}));
|
||||
|
||||
// No composition ⇒ no materialise (and no RebuildAddressSpace either, since nothing was applied).
|
||||
publish.ExpectNoMsg(TimeSpan.FromMilliseconds(500));
|
||||
}
|
||||
|
||||
/// <summary>Dedup: a discovered node whose <c>FullReference</c> equals an authored equipment tag's
|
||||
/// FullName is NOT injected (it would shadow the authored node) — only the genuinely-new FixedTree refs
|
||||
/// are materialised.</summary>
|
||||
[Fact]
|
||||
public void Discovered_node_shadowing_an_authored_ref_is_not_injected()
|
||||
{
|
||||
var db = NewInMemoryDbFactory();
|
||||
var factory = new SubscribingDriverFactory("Modbus");
|
||||
var deploymentId = SeedDeploymentWithEquipmentTags(db, RevA,
|
||||
(Equip: "EQ-1", Driver: "d1", FullName: "40001", Folder: (string?)null, Name: "speed"));
|
||||
|
||||
var (actor, publish) = SpawnHostAndApply(db, deploymentId, factory);
|
||||
|
||||
// Two captured nodes: one SHADOWS the authored ref "40001" (must be dropped), one is genuinely new.
|
||||
actor.Tell(new DriverInstanceActor.DiscoveredNodesReady("d1", new[]
|
||||
{
|
||||
new DiscoveredNode(
|
||||
FolderPathSegments: new[] { "FOCAS", "10.0.0.5:8193", "Registers" },
|
||||
BrowseName: "speed", DisplayName: "Speed", FullReference: "40001",
|
||||
DataType: DriverDataType.Float64, IsArray: false, ArrayDim: null,
|
||||
Writable: false, IsHistorized: false),
|
||||
new DiscoveredNode(
|
||||
FolderPathSegments: new[] { "FOCAS", "10.0.0.5:8193", "Identity" },
|
||||
BrowseName: "Model", DisplayName: "Model", FullReference: "ft-ref-1",
|
||||
DataType: DriverDataType.Float64, IsArray: false, ArrayDim: null,
|
||||
Writable: false, IsHistorized: false),
|
||||
}));
|
||||
|
||||
// Exactly ONE variable materialised — the new "Model", not the authored-shadow "Speed".
|
||||
var materialise = publish.ExpectMsg<OpcUaPublishActor.MaterialiseDiscoveredNodes>(Timeout);
|
||||
materialise.Variables.Count.ShouldBe(1);
|
||||
materialise.Variables[0].DisplayName.ShouldBe("Model");
|
||||
}
|
||||
|
||||
/// <summary>Idempotency / the unchanged-plan short-circuit: re-sending the SAME discovered set (the driver
|
||||
/// re-discovers each ~2s pass) is a no-op — it materialises ONCE and does not force the child to
|
||||
/// re-subscribe. A GROWN set, however, DOES re-apply (materialise again + re-subscribe), so a stabilising
|
||||
/// FixedTree still converges.</summary>
|
||||
[Fact]
|
||||
public void Repeated_identical_discovery_does_not_reapply_but_a_grown_set_does()
|
||||
{
|
||||
var db = NewInMemoryDbFactory();
|
||||
var factory = new SubscribingDriverFactory("Modbus");
|
||||
var deploymentId = SeedDeploymentWithEquipmentTags(db, RevA,
|
||||
(Equip: "EQ-1", Driver: "d1", FullName: "40001", Folder: (string?)null, Name: "speed"));
|
||||
|
||||
var (actor, publish) = SpawnHostAndApply(db, deploymentId, factory);
|
||||
|
||||
var node1 = new DiscoveredNode(
|
||||
FolderPathSegments: new[] { "FOCAS", "10.0.0.5:8193", "Identity" },
|
||||
BrowseName: "Model", DisplayName: "Model", FullReference: "ft-ref-1",
|
||||
DataType: DriverDataType.Float64, IsArray: false, ArrayDim: null,
|
||||
Writable: false, IsHistorized: false);
|
||||
var node2 = new DiscoveredNode(
|
||||
FolderPathSegments: new[] { "FOCAS", "10.0.0.5:8193", "Status" },
|
||||
BrowseName: "Run", DisplayName: "Run", FullReference: "ft-ref-2",
|
||||
DataType: DriverDataType.Float64, IsArray: false, ArrayDim: null,
|
||||
Writable: false, IsHistorized: false);
|
||||
|
||||
// Pass 1: a new set ⇒ one materialise + a union re-subscribe.
|
||||
actor.Tell(new DriverInstanceActor.DiscoveredNodesReady("d1", new[] { node1 }));
|
||||
publish.ExpectMsg<OpcUaPublishActor.MaterialiseDiscoveredNodes>(Timeout);
|
||||
AwaitAssert(() =>
|
||||
{
|
||||
var refs = factory.LastSubscribedRefs;
|
||||
refs.ShouldNotBeNull();
|
||||
refs!.ShouldContain("ft-ref-1");
|
||||
}, duration: Timeout);
|
||||
var subscribeCountAfterFirst = factory.SubscribeCount;
|
||||
|
||||
// Pass 2: the IDENTICAL set ⇒ short-circuited (no materialise, no re-subscribe).
|
||||
actor.Tell(new DriverInstanceActor.DiscoveredNodesReady("d1", new[] { node1 }));
|
||||
publish.ExpectNoMsg(TimeSpan.FromMilliseconds(500));
|
||||
factory.SubscribeCount.ShouldBe(subscribeCountAfterFirst);
|
||||
|
||||
// Pass 3: a GROWN set (superset) ⇒ re-applies (materialise again + re-subscribe with both refs).
|
||||
actor.Tell(new DriverInstanceActor.DiscoveredNodesReady("d1", new[] { node1, node2 }));
|
||||
publish.ExpectMsg<OpcUaPublishActor.MaterialiseDiscoveredNodes>(Timeout).Variables.Count.ShouldBe(2);
|
||||
AwaitAssert(() =>
|
||||
{
|
||||
factory.SubscribeCount.ShouldBeGreaterThan(subscribeCountAfterFirst);
|
||||
var refs = factory.LastSubscribedRefs;
|
||||
refs.ShouldNotBeNull();
|
||||
refs!.ShouldContain("ft-ref-1");
|
||||
refs.ShouldContain("ft-ref-2");
|
||||
}, duration: Timeout);
|
||||
}
|
||||
|
||||
/// <summary>Spawns the host with the subscribing driver factory + a publish probe, dispatches the
|
||||
/// deployment, and waits for the Applied ACK so the apply (and thus <c>_lastComposition</c> + the live
|
||||
/// child + the initial SubscribeBulk pass) has completed before the test injects discovered nodes. A
|
||||
@@ -209,6 +329,10 @@ public sealed class DriverHostActorDiscoveryTests : RuntimeActorTestBase
|
||||
/// <summary>The reference set passed to the driver's most recent <c>SubscribeAsync</c> call.</summary>
|
||||
public IReadOnlyList<string>? LastSubscribedRefs => _driver.LastSubscribedRefs;
|
||||
|
||||
/// <summary>Number of <c>SubscribeAsync</c> calls so far — lets a test prove a redundant re-apply did
|
||||
/// NOT force a (drop-then-)re-subscribe of the whole handle.</summary>
|
||||
public int SubscribeCount => _driver.SubscribeCount;
|
||||
|
||||
/// <inheritdoc />
|
||||
public IDriver? TryCreate(string driverType, string driverInstanceId, string driverConfigJson) =>
|
||||
string.Equals(driverType, _supportedType, StringComparison.Ordinal) ? _driver : null;
|
||||
|
||||
Reference in New Issue
Block a user