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; /// /// #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. /// 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(); 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(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); } }