02d227cc32
S1: ApplyAsync looked its alarm up before taking _evalGate, so a command racing a LoadAsync applied to the load's interim Fresh entry (an ack no-op that then saved Fresh over the carried Active condition — the #935 re-Activate on the next push) or wrote an old-generation state over the new one. The lookup is now under the gate: a command queued behind a load applies to the generation that load produced. A command that changes nothing (value-equal result) writes nothing. S2: a load re-saved every alarm's post-predicate state, so a node that missed an operator command upserted its stale carried state over the LocalDb last-writer-wins row on every deploy. A held seed (carried or persisted) left value-equal is not written. The push path's ReferenceEquals check (always false: ApplyPredicate returns a clone) is now value equality too, so a no-op re-evaluation no longer writes/replicates. S3: a failed load ends the generation. The engine is blind until the next good load, so (decision, same rule as removed/disabled alarms) that load reads the store — a still-true alarm is a new occurrence — instead of restating the pre-failure snapshot. While blind, operator commands throw ScriptedAlarmCommandRejectedException (the host logs it distinctly; the command topic is fire-and-forget, so that Warning is the record) and queued re-evaluations / shelving checks are dropped. N5: a timed shelve that expired before a load is unshelved through the shelving check (AutoUnshelve audit + Unshelved emission), not silently by ApplyPredicate. Tests (each red on revert, verified): ack racing a held load (S1), idempotent command (S1), restatement vs real change saves (S2), no-op push saves (S2), command rejected after failed load (S3), re-evaluation after failed load (S3), failed load -> new occurrence + store fault mid-seed -> one Activated (S3/S4), runner Unavailable at reload freezes carried state (S4), expired timed shelve at load (N5). N4: the Bad-input reload test now waits on a Good-bucket QualityChanged.
102 lines
5.0 KiB
C#
102 lines
5.0 KiB
C#
using System.Collections.Concurrent;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
using ZB.MOM.WW.OtOpcUa.Core.ScriptedAlarms;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Core.ScriptedAlarms.Tests;
|
|
|
|
/// <summary>Test fake implementation of <see cref="ITagUpstreamSource"/> for verifying subscription behavior.</summary>
|
|
public sealed class FakeUpstream : ITagUpstreamSource
|
|
{
|
|
private readonly ConcurrentDictionary<string, DataValueSnapshot> _values = new(StringComparer.Ordinal);
|
|
private readonly ConcurrentDictionary<string, List<Action<string, DataValueSnapshot>>> _subs
|
|
= new(StringComparer.Ordinal);
|
|
/// <summary>Gets the current count of active subscriptions.</summary>
|
|
public int ActiveSubscriptionCount { get; private set; }
|
|
|
|
/// <summary>Sets a tag value without notifying subscribers.</summary>
|
|
/// <param name="path">The tag path to set.</param>
|
|
/// <param name="value">The value to set for the tag.</param>
|
|
/// <param name="statusCode">The OPC UA status code for the value.</param>
|
|
public void Set(string path, object? value, uint statusCode = 0u)
|
|
{
|
|
var now = DateTime.UtcNow;
|
|
_values[path] = new DataValueSnapshot(value, statusCode, now, now);
|
|
}
|
|
|
|
/// <summary>Sets a tag value and notifies all current subscribers.</summary>
|
|
/// <param name="path">The tag path to set.</param>
|
|
/// <param name="value">The value to set for the tag.</param>
|
|
/// <param name="statusCode">The OPC UA status code for the value.</param>
|
|
public void Push(string path, object? value, uint statusCode = 0u)
|
|
{
|
|
Set(path, value, statusCode);
|
|
if (_subs.TryGetValue(path, out var list))
|
|
{
|
|
Action<string, DataValueSnapshot>[] snap;
|
|
lock (list) { snap = list.ToArray(); }
|
|
foreach (var obs in snap) obs(path, _values[path]);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// OPC UA <c>BadWaitingForInitialData</c> — the status a never-pushed path reports, matching what
|
|
/// the production <c>DependencyMuxTagUpstreamSource</c> stamps.
|
|
/// <para>This used to be <c>0x80340000</c> (<c>BadNotConnected</c>), a different Bad code from the
|
|
/// one production uses. That drift was invisible while <c>WorstInputStatus</c> skipped every
|
|
/// null-valued entry — any Bad code behaved the same. #481 made the specific code load-bearing (it
|
|
/// is how cold start is told apart from a driver reporting Bad), so a fake disagreeing with
|
|
/// production here would let every alarm with an unset input report Bad in tests and Good in the
|
|
/// real runtime, or vice versa.</para>
|
|
/// </summary>
|
|
public const uint StatusNeverPushed = 0x80320000u;
|
|
|
|
/// <summary>Reads the current value of a tag, or returns a never-pushed snapshot if not set.</summary>
|
|
/// <param name="path">The tag path to read.</param>
|
|
public DataValueSnapshot ReadTag(string path)
|
|
{
|
|
OnRead?.Invoke(path);
|
|
return _values.TryGetValue(path, out var v) ? v
|
|
: new DataValueSnapshot(null, StatusNeverPushed, null, DateTime.UtcNow);
|
|
}
|
|
|
|
/// <summary>Optional hook run synchronously at the start of every <see cref="ReadTag"/> — lets a test hold a
|
|
/// <c>LoadAsync</c> at its value-cache seed, after every alarm entry is in place but before any is seeded.</summary>
|
|
public Action<string>? OnRead { get; set; }
|
|
|
|
/// <summary>Subscribes an observer to tag changes for the given path.</summary>
|
|
/// <param name="path">The tag path to subscribe to.</param>
|
|
/// <param name="observer">The observer callback to invoke on tag changes.</param>
|
|
public IDisposable SubscribeTag(string path, Action<string, DataValueSnapshot> observer)
|
|
{
|
|
var list = _subs.GetOrAdd(path, _ => []);
|
|
lock (list) { list.Add(observer); }
|
|
ActiveSubscriptionCount++;
|
|
return new Unsub(this, path, observer);
|
|
}
|
|
|
|
/// <summary>Disposable subscription handle that unsubscribes the observer when disposed.</summary>
|
|
private sealed class Unsub : IDisposable
|
|
{
|
|
private readonly FakeUpstream _up;
|
|
private readonly string _path;
|
|
private readonly Action<string, DataValueSnapshot> _observer;
|
|
/// <summary>Initializes the unsubscription handle with references needed to clean up the subscription.</summary>
|
|
/// <param name="up">The upstream source containing the subscription list.</param>
|
|
/// <param name="path">The tag path to unsubscribe from.</param>
|
|
/// <param name="observer">The observer to remove from the subscription list.</param>
|
|
public Unsub(FakeUpstream up, string path, Action<string, DataValueSnapshot> observer)
|
|
{ _up = up; _path = path; _observer = observer; }
|
|
/// <summary>Removes the observer from the subscription list.</summary>
|
|
public void Dispose()
|
|
{
|
|
if (_up._subs.TryGetValue(_path, out var list))
|
|
{
|
|
lock (list)
|
|
{
|
|
if (list.Remove(_observer)) _up.ActiveSubscriptionCount--;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|