fix(dcl): forget an OPC UA condition's last state once it returns to normal (review 2026-10-21 02/L3)

This commit is contained in:
Joseph Doherty
2026-10-22 06:28:00 -04:00
parent d51ea5136a
commit f18d3adbfc
3 changed files with 102 additions and 3 deletions
@@ -0,0 +1,89 @@
using System.Collections.Concurrent;
using System.Reflection;
using Opc.Ua;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Alarms;
using ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Adapters;
namespace ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Tests.Adapters;
/// <summary>
/// Review 2026-10-21 02/L3: the per-condition (active, acked) memory behind
/// <c>DeriveKind</c> must not grow for the life of a session. A condition that returns to
/// inactive + acknowledged is indistinguishable from one never seen (the default prior state), so
/// its key is dropped — otherwise a server whose condition names carry a per-occurrence component
/// (batch, lot, counter) adds a key per occurrence until the next reconnect.
/// </summary>
public class RealOpcUaAlarmLastStateTests
{
private const string Handle = "h1";
private static readonly MethodInfo Handler =
typeof(RealOpcUaClient).GetMethod("HandleAlarmEvent", BindingFlags.Instance | BindingFlags.NonPublic)!;
private static Dictionary<string, (bool Active, bool Acked)> SeedLastState(RealOpcUaClient client)
{
var field = typeof(RealOpcUaClient).GetField("_alarmLastState", BindingFlags.Instance | BindingFlags.NonPublic)!;
var map = (ConcurrentDictionary<string, Dictionary<string, (bool Active, bool Acked)>>)field.GetValue(client)!;
var perHandle = new Dictionary<string, (bool Active, bool Acked)>(StringComparer.Ordinal);
map[Handle] = perHandle;
return perHandle;
}
private static void Emit(RealOpcUaClient client, string condition, bool active, bool acked, List<NativeAlarmTransition> sink)
{
var fields = new VariantCollection
{
new(ObjectTypeIds.AlarmConditionType), // 0 EventType
new(new NodeId("Tank01", 2)), // 1 SourceNode
new("Tank01"), // 2 SourceName
new(DateTime.UtcNow), // 3 Time
new(new LocalizedText("msg")), // 4 Message
new((ushort)500), // 5 Severity
new(active), // 6 ActiveState/Id
new(acked), // 7 AckedState/Id
Variant.Null, // 8 ConfirmedState/Id
new(false), // 9 SuppressedOrShelved
Variant.Null, // 10 ShelvingState
new(condition), // 11 ConditionName
};
Handler.Invoke(client, [Handle, "ns=2;s=Tank01", new EventFieldList { EventFields = fields },
(Action<NativeAlarmTransition>)sink.Add]);
}
/// <summary>A per-occurrence condition that clears and is acknowledged leaves no key behind.</summary>
[Fact]
public void ReturnToNormal_DropsTheConditionKey()
{
var client = new RealOpcUaClient();
var lastState = SeedLastState(client);
var sink = new List<NativeAlarmTransition>();
for (var lot = 1; lot <= 50; lot++)
{
Emit(client, $"Hi.Lot{lot}", active: true, acked: false, sink);
Emit(client, $"Hi.Lot{lot}", active: false, acked: true, sink);
}
Assert.Empty(lastState);
Assert.Equal(100, sink.Count);
}
/// <summary>
/// Dropping the key does not change the transition kinds derived for a condition that comes
/// back — the dropped state equals the default prior state.
/// </summary>
[Fact]
public void ReRaiseAfterReturnToNormal_StillDerivesARaise()
{
var client = new RealOpcUaClient();
var lastState = SeedLastState(client);
var sink = new List<NativeAlarmTransition>();
Emit(client, "Hi", active: true, acked: false, sink);
Emit(client, "Hi", active: false, acked: true, sink);
Emit(client, "Hi", active: true, acked: false, sink);
Assert.Equal(sink[0].Kind, sink[2].Kind);
Assert.Single(lastState); // the re-raised condition is tracked again
}
}