using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Commons.OpcUa; namespace ZB.MOM.WW.OtOpcUa.Commons.Tests; /// /// The batched seam. It exists so an SDK-backed sink /// can take its internal lock ONCE per batch instead of once per value, and it ships as a default /// interface method so every existing sink and test fake keeps compiling unchanged — this suite /// pins both halves of that bargain: the default loops /// verbatim (#519: the status code rides through untouched), and /// forwards the batch to an overriding inner rather than /// silently degrading it to per-item calls on every driver-role host. /// public class AddressSpaceSinkBatchTests { [Fact] public void Default_WriteValues_loops_WriteValue() { // A sink that implements ONLY the pre-existing interface members — exactly the shape of every // sink and fake that predates the batch seam. The default implementation must fan the batch out // to WriteValue, in order, with every field carried through unchanged. var fake = new PerItemOnlySink(); var t0 = new DateTime(2026, 8, 15, 10, 0, 0, DateTimeKind.Utc); ((IOpcUaAddressSpaceSink)fake).WriteValues(new[] { new ValueWrite("Plant/Modbus/dev1/Speed", 42, OpcUaStatus.Good, t0, AddressSpaceRealm.Raw), new ValueWrite("Area/Line/Eq/Speed", 43.5, OpcUaStatus.BadNoCommunication, t0.AddSeconds(1), AddressSpaceRealm.Uns), new ValueWrite("Plant/Modbus/dev1/Temp", null, OpcUaStatus.Bad, t0.AddSeconds(2), AddressSpaceRealm.Raw), }); fake.Calls.Count.ShouldBe(3); fake.Calls[0].ShouldBe(("Plant/Modbus/dev1/Speed", (object?)42, OpcUaStatus.Good, t0, AddressSpaceRealm.Raw)); fake.Calls[1].ShouldBe(("Area/Line/Eq/Speed", (object?)43.5, OpcUaStatus.BadNoCommunication, t0.AddSeconds(1), AddressSpaceRealm.Uns)); fake.Calls[2].ShouldBe(("Plant/Modbus/dev1/Temp", (object?)null, OpcUaStatus.Bad, t0.AddSeconds(2), AddressSpaceRealm.Raw)); } [Fact] public void Deferred_sink_forwards_WriteValues_to_inner_once() { // The whole point of the seam is the ONE-lock-per-batch win, and actors inject the deferred // wrapper — not the inner SdkAddressSpaceSink. A missing forward here would fall back to the // interface default and re-explode the batch into per-item WriteValue calls on every // driver-role host: the batching would ship inert, exactly the F10b / PR#423 trap class. var inner = new BatchingSpySink(); var sink = new DeferredAddressSpaceSink(); sink.SetSink(inner); var t0 = new DateTime(2026, 8, 15, 11, 0, 0, DateTimeKind.Utc); var batch = new[] { new ValueWrite("a", 1, OpcUaStatus.Good, t0, AddressSpaceRealm.Raw), new ValueWrite("b", 2, OpcUaStatus.Good, t0, AddressSpaceRealm.Uns), new ValueWrite("c", 3, OpcUaStatus.Good, t0, AddressSpaceRealm.Raw), }; sink.WriteValues(batch); inner.Batches.Count.ShouldBe(1, "the batch must reach the inner sink as ONE call, not be re-looped"); inner.Batches[0].Count.ShouldBe(3); inner.Batches[0].Select(v => v.NodeId).ShouldBe(new[] { "a", "b", "c" }); inner.PerItemCalls.ShouldBe(0, "the inner sink's per-item WriteValue must not be reached at all"); } // ---- test doubles ---- /// Implements only the members that existed before the batch seam, so the interface's /// default WriteValues is what runs. private sealed class PerItemOnlySink : IOpcUaAddressSpaceSink { public List<(string NodeId, object? Value, uint StatusCode, DateTime SourceTimestampUtc, AddressSpaceRealm Realm)> Calls { get; } = new(); public void WriteValue(string nodeId, object? value, uint statusCode, DateTime sourceTimestampUtc, AddressSpaceRealm realm) => Calls.Add((nodeId, value, statusCode, sourceTimestampUtc, realm)); public void WriteAlarmCondition(string alarmNodeId, AlarmConditionSnapshot state, DateTime sourceTimestampUtc, AddressSpaceRealm realm) { } public void WriteAlarmQuality(string alarmNodeId, uint statusCode, DateTime sourceTimestampUtc, AddressSpaceRealm realm) { } public void MaterialiseAlarmCondition(string alarmNodeId, string equipmentNodeId, string displayName, string alarmType, int severity, AddressSpaceRealm realm, bool isNative = false) { } public void WireAlarmNotifiers(string alarmNodeId, AddressSpaceRealm alarmRealm, IReadOnlyList notifierFolderNodeIds, AddressSpaceRealm notifierFolderRealm) { } public void EnsureFolder(string folderNodeId, string? parentNodeId, string displayName, AddressSpaceRealm realm) { } public void EnsureVariable(string variableNodeId, string? parentFolderNodeId, string displayName, string dataType, bool writable, AddressSpaceRealm realm, string? historianTagname = null, bool isArray = false, uint? arrayLength = null, bool refuseArrayOnScalar = false) { } public void RebuildAddressSpace() { } public void RaiseNodesAddedModelChange(string affectedNodeId, AddressSpaceRealm realm) { } public void AddReference(string sourceNodeId, AddressSpaceRealm sourceRealm, string targetNodeId, AddressSpaceRealm targetRealm, string referenceType = "Organizes") { } } /// Overrides WriteValues the way a real SDK-backed sink does — records the batch and /// does NOT loop — so a wrapper that dropped the forward is distinguishable from one that kept it. private sealed class BatchingSpySink : IOpcUaAddressSpaceSink { public List> Batches { get; } = new(); public int PerItemCalls { get; private set; } public void WriteValue(string nodeId, object? value, uint statusCode, DateTime sourceTimestampUtc, AddressSpaceRealm realm) => PerItemCalls++; public void WriteValues(IReadOnlyList values) => Batches.Add(values); public void WriteAlarmCondition(string alarmNodeId, AlarmConditionSnapshot state, DateTime sourceTimestampUtc, AddressSpaceRealm realm) { } public void WriteAlarmQuality(string alarmNodeId, uint statusCode, DateTime sourceTimestampUtc, AddressSpaceRealm realm) { } public void MaterialiseAlarmCondition(string alarmNodeId, string equipmentNodeId, string displayName, string alarmType, int severity, AddressSpaceRealm realm, bool isNative = false) { } public void WireAlarmNotifiers(string alarmNodeId, AddressSpaceRealm alarmRealm, IReadOnlyList notifierFolderNodeIds, AddressSpaceRealm notifierFolderRealm) { } public void EnsureFolder(string folderNodeId, string? parentNodeId, string displayName, AddressSpaceRealm realm) { } public void EnsureVariable(string variableNodeId, string? parentFolderNodeId, string displayName, string dataType, bool writable, AddressSpaceRealm realm, string? historianTagname = null, bool isArray = false, uint? arrayLength = null, bool refuseArrayOnScalar = false) { } public void RebuildAddressSpace() { } public void RaiseNodesAddedModelChange(string affectedNodeId, AddressSpaceRealm realm) { } public void AddReference(string sourceNodeId, AddressSpaceRealm sourceRealm, string targetNodeId, AddressSpaceRealm targetRealm, string referenceType = "Organizes") { } } }