Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.Host.Tests/OpcUa/ScriptedAlarmCommandDispatchTests.cs
T
Joseph Doherty 068124d1ae fix(alarms): #951 #952 #953 — late refusal reverts, never-started command is NotApplied, superseded load failure ignored
#951: the hosted-service Ask lives LateVerdictWindow (30 s) past the caller's bound
(ExecuteAlarmCommand deadline unchanged); the OutcomeUnknown it returns carries the
still-running ask as AlarmCommandOutcome.LateOutcome (process-local). The node manager
watches it (or the asker task itself when its backstop expired, following unknown
chains) and reverts the node on a late NotApplied, under the existing per-condition
mutation-counter guard. Both CallAsync and remote ExecuteConditionMethodAsync paths.

#952: ScriptedAlarmEngine throws ScriptedAlarmCommandNotStartedException (an OCE) when a
command is cancelled before it acquires _evalGate; a cancellation inside SaveAsync stays
a plain OCE (unknown). The host maps not-started to NotApplied and remembers it, except on
the direct ask once its asker's deadline passed (asker already answered unknown and
broadcast): then unknown + not remembered, so the broadcast copy decides (as review r2 M2).

#953: a faulted load pipes back AlarmsLoadFailed(cause, generation); a failure from a
generation a newer apply superseded publishes no readiness (it would refuse every command
while the newer load is in flight).

Red/green: each fix reverted individually turns its tests red (Core 1, Runtime 3,
Host 1, OpcUaServer.IntegrationTests 4).

Co-Authored-By: Claude <noreply@anthropic.com>
2026-12-01 19:42:24 -05:00

84 lines
4.4 KiB
C#

using Akka.Actor;
using Microsoft.Extensions.Logging.Abstractions;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
using ZB.MOM.WW.OtOpcUa.Host.OpcUa;
namespace ZB.MOM.WW.OtOpcUa.Host.Tests.OpcUa;
/// <summary>
/// #937 review r1: the redundant peer receives a Part 9 command only after THIS node's engine applied it (or did not
/// answer) — a local refusal must reach no engine anywhere, or the caller's definite failure and the node revert lie.
/// </summary>
public sealed class ScriptedAlarmCommandDispatchTests
{
private static readonly AlarmCommand Cmd = new("alm-1", "Acknowledge", "alice", null, null, "cmd-1");
[Theory]
[InlineData(AlarmCommandVerdict.Applied, true)]
[InlineData(AlarmCommandVerdict.OutcomeUnknown, true)]
[InlineData(AlarmCommandVerdict.NotApplied, false)]
public async Task The_peer_broadcast_follows_the_local_verdict(AlarmCommandVerdict verdict, bool broadcast)
{
var order = new List<string>();
var outcome = await ScriptedAlarmCommandDispatch.AskThenBroadcastAsync(Cmd, TimeSpan.FromSeconds(1),
(c, _) => { order.Add("ask"); return Task.FromResult(new AlarmCommandOutcome(c.AlarmId, c.Operation, verdict)); },
c => order.Add("broadcast:" + c.CommandId), NullLogger.Instance);
outcome.Verdict.ShouldBe(verdict);
order.ShouldBe(broadcast ? ["ask", "broadcast:cmd-1"] : ["ask"]);
}
[Fact]
public async Task No_answer_is_outcome_unknown_and_still_broadcast_and_no_engine_is_a_refusal_with_none()
{
var sent = 0;
var timedOut = await ScriptedAlarmCommandDispatch.AskThenBroadcastAsync(Cmd, TimeSpan.FromMilliseconds(10),
(_, _) => Task.FromException<AlarmCommandOutcome>(new AskTimeoutException("no answer")), _ => sent++, NullLogger.Instance);
timedOut.Verdict.ShouldBe(AlarmCommandVerdict.OutcomeUnknown);
timedOut.TimedOut.ShouldBeTrue();
sent.ShouldBe(1, "the local engine may still apply it, so the peer must not be left behind");
var none = await ScriptedAlarmCommandDispatch.AskThenBroadcastAsync(Cmd, TimeSpan.FromSeconds(1), null, _ => sent++, NullLogger.Instance);
none.Verdict.ShouldBe(AlarmCommandVerdict.NotApplied);
sent.ShouldBe(1);
// A failing broadcast never changes the caller's answer.
var applied = await ScriptedAlarmCommandDispatch.AskThenBroadcastAsync(Cmd, TimeSpan.FromSeconds(1),
(c, _) => Task.FromResult(new AlarmCommandOutcome(c.AlarmId, c.Operation, AlarmCommandVerdict.Applied)),
_ => throw new InvalidOperationException("mediator down"), NullLogger.Instance);
applied.Verdict.ShouldBe(AlarmCommandVerdict.Applied);
}
/// <summary>
/// #951: the caller's bound expiring does not drop the host's answer — the OutcomeUnknown carries the still-running ask
/// as its <see cref="AlarmCommandOutcome.LateOutcome"/>, so a refusal that arrives later can still revert the node.
/// </summary>
[Fact]
public async Task An_answer_after_the_bound_rides_the_unknown_outcome_as_its_late_outcome()
{
var sent = 0;
var host = new TaskCompletionSource<AlarmCommandOutcome>(TaskCreationOptions.RunContinuationsAsynchronously);
TimeSpan? askedBound = null;
var unknown = await ScriptedAlarmCommandDispatch.AskThenBroadcastAsync(Cmd, TimeSpan.FromMilliseconds(50),
(_, bound) => { askedBound = bound; return host.Task; }, _ => sent++, NullLogger.Instance);
askedBound.ShouldBe(TimeSpan.FromMilliseconds(50), "the host's deadline is still the caller's bound");
unknown.Verdict.ShouldBe(AlarmCommandVerdict.OutcomeUnknown);
unknown.TimedOut.ShouldBeTrue();
sent.ShouldBe(1);
var late = unknown.LateOutcome;
(late is not null).ShouldBeTrue("the host's answer is still collected");
late!.IsCompleted.ShouldBeFalse();
host.SetResult(new AlarmCommandOutcome(Cmd.AlarmId, Cmd.Operation, AlarmCommandVerdict.NotApplied, "load failed"));
(await late).Verdict.ShouldBe(AlarmCommandVerdict.NotApplied);
// An ask that FAILED inside the bound has no later answer.
var faulted = await ScriptedAlarmCommandDispatch.AskThenBroadcastAsync(Cmd, TimeSpan.FromSeconds(1),
(_, _) => Task.FromException<AlarmCommandOutcome>(new AskTimeoutException("no answer")), _ => sent++, NullLogger.Instance);
(faulted.LateOutcome is null).ShouldBeTrue();
}
}