Files
lmxopcua/tests/Core/ZB.MOM.WW.OtOpcUa.Commons.Tests/AddressSpaceSinkBatchTests.cs
T
Joseph Doherty 52708bff52 fix(address-space): numeric array writes convert (checked) to the node element type; array-to-scalar refusal only for Modbus/S7 (#610 review R6)
MAJOR-1: R4's exact element-type match refused ScadaBridge's CLR-list
writes (its element types are only Boolean/Int32/Float/Double/String/
DateTime) to every Int16/UInt16/Byte/UInt32/Int64/UInt64 array node and a
Double list to a Float array. EvaluateEquipmentWriteStructure now converts
a numeric array of another numeric element type element-wise, CHECKED, into
the node's element type and hands the converted array back through
OnEquipmentTagWrite's ref value, so the node stores and the driver receives
the node's own type. Any out-of-range, fractional-to-integer, NaN/Inf-to-
integer or Float-overflowing element refuses the whole write BadTypeMismatch
(nothing dispatched) - so the UInt32 -> Int32 bit reinterpretation stays
closed. Non-numeric mismatches stay refused. The wrong 'no client relies on
a looser rule' comment is corrected.

MINOR-1: the array-value-to-scalar-node refusal broke writes back to
browse-committed array tags (committed scalar, isArray never set) on
pass-through drivers. It now applies only when the owning driver's scalar
encoder cannot take an array (DriverArrayWriteSupport.
RefusesArrayValueOnScalarTag: Modbus, S7), carried RawTagPlan.
ScalarRefusesArrayValue / UnsReferenceVariable.BackingScalarRefusesArrayValue
-> EnsureVariable(refuseArrayOnScalar) -> a per-node set in the node
manager. OpcUaClient/Galaxy/others keep pre-#610 dispatch. Not on the
classifier whitelist (driver-type edit rebuilds); a mirror whose flag
changes is recreated, not updated in place.

Claude-Session: https://claude.ai/code/session_01Ef2ozxS7QPanGT8uoATbTe
2026-10-08 08:31:46 -04:00

113 lines
7.5 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
namespace ZB.MOM.WW.OtOpcUa.Commons.Tests;
/// <summary>
/// The batched <see cref="IOpcUaAddressSpaceSink.WriteValues"/> 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 <b>default
/// interface method</b> so every existing sink and test fake keeps compiling unchanged — this suite
/// pins both halves of that bargain: the default loops <see cref="IOpcUaAddressSpaceSink.WriteValue"/>
/// verbatim (#519: the status code rides through untouched), and
/// <see cref="DeferredAddressSpaceSink"/> forwards the <i>batch</i> to an overriding inner rather than
/// silently degrading it to per-item calls on every driver-role host.
/// </summary>
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 ----
/// <summary>Implements only the members that existed before the batch seam, so the interface's
/// default <c>WriteValues</c> is what runs.</summary>
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<string> 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") { }
}
/// <summary>Overrides <c>WriteValues</c> 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.</summary>
private sealed class BatchingSpySink : IOpcUaAddressSpaceSink
{
public List<IReadOnlyList<ValueWrite>> 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<ValueWrite> 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<string> 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") { }
}
}