fix(alarms): AdminUI /alerts ack/shelve reports the scripted engine's verdict (#950)
The /alerts buttons went central->node fire-and-forget and always showed "Ack sent". AdminOperationsActor now spawns a per-request AlarmCommandVerdictCollector (7 s window, under the client's 10 s ask) that sends the command with AlarmCommand.VerdictRequested and gathers each node's answer, finishing early once every node it sent to has answered. - gRPC plane: CentralCommunicationActor tells the collector the dialled ClusterNode set and one verdict per node. A node handles a verdict_requested AlarmCommandMessage by asking its driver host (ScriptedAlarmHostActor.ExecuteAlarmCommand, the #937 path, no ask deadline, bounded 5 s) and returns it in the ADDITIVE ExecuteAck.alarm_verdict. Transport failure / timeout / empty ack (pre-#950 node) => that node unknown. - DPS: published with the collector as sender; each ScriptedAlarmHostActor answers (non-owners NotOwned); expected set = Up driver members. - Aggregation: any owner applied => Applied (partial noted); else any unknown/silent node => Outcome unknown (never "refused"); else owner refusal => Refused: <reason>; else no owner => Not applied. - UI: green Applied / red Refused (reason) / amber Outcome unknown, per-node detail in the details pane; a verdict-less reply (validation, server fault, pre-#950 singleton) renders as before. Wire is back-compatible both ways (new proto fields only; VerdictRequested and NotOwned default false). Real-wire test through the real router, dialer, interceptor and NodeCommandGrpcService (red without the node change). Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using Akka.Actor;
|
||||
using Akka.Hosting;
|
||||
using Akka.TestKit.Xunit;
|
||||
using Shouldly;
|
||||
@@ -28,9 +29,22 @@ namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests;
|
||||
/// </remarks>
|
||||
public sealed class AlertsAckPathTests : TestKit
|
||||
{
|
||||
private (AdminOperationsClient Client, Akka.TestKit.TestProbe Mesh) Wire()
|
||||
private (AdminOperationsClient Client, Akka.TestKit.TestProbe Mesh) Wire(
|
||||
Func<AlarmCommand, AlarmCommandOutcome>? answer = null)
|
||||
{
|
||||
var mesh = CreateTestProbe("mesh-router");
|
||||
// #950: the router's one node answers each alarm command (Applied by default), as the gRPC fan reports it.
|
||||
answer ??= cmd => new AlarmCommandOutcome(cmd.AlarmId, cmd.Operation, AlarmCommandVerdict.Applied);
|
||||
mesh.SetAutoPilot(new Akka.TestKit.DelegateAutoPilot((_, msg) =>
|
||||
{
|
||||
if (msg is MeshCommand { VerdictReplyTo: { } collector, Message: AlarmCommand cmd })
|
||||
{
|
||||
collector.Tell(new AlarmVerdictFanOut(["node-1"]));
|
||||
collector.Tell(new AlarmNodeVerdict("node-1", answer(cmd)));
|
||||
}
|
||||
|
||||
return Akka.TestKit.AutoPilot.KeepRunning;
|
||||
}));
|
||||
var actor = Sys.ActorOf(AdminOperationsActor.Props(
|
||||
AclScopeTestDb.Factory($"alerts-ack-{Guid.NewGuid():N}"),
|
||||
coordinator: CreateTestProbe("coordinator").Ref,
|
||||
@@ -42,16 +56,18 @@ public sealed class AlertsAckPathTests : TestKit
|
||||
}
|
||||
|
||||
/// <summary>The page's Acknowledge reaches the cluster as an <c>Acknowledge</c> AlarmCommand on the
|
||||
/// <c>alarm-commands</c> topic carrying the alarm id and the operator, and the page is told it was sent.</summary>
|
||||
/// <c>alarm-commands</c> topic carrying the alarm id and the operator, and the page is told the engine applied it
|
||||
/// (#950 — it used to be told only that it was sent).</summary>
|
||||
[Fact]
|
||||
public async Task Acknowledge_from_the_alerts_page_dispatches_an_Acknowledge_command_and_reports_sent()
|
||||
public async Task Acknowledge_from_the_alerts_page_dispatches_an_Acknowledge_command_and_reports_applied()
|
||||
{
|
||||
var (client, mesh) = Wire();
|
||||
|
||||
var result = await client.AcknowledgeAlarmAsync("alarm-42", "operator-jo", comment: null, CancellationToken.None);
|
||||
|
||||
result.Ok.ShouldBeTrue();
|
||||
result.Message.ShouldBeNull();
|
||||
result.Verdict.ShouldBe(AlarmCommandVerdict.Applied);
|
||||
result.Summary.ShouldBe("Applied");
|
||||
var sent = mesh.ExpectMsg<MeshCommand>(TimeSpan.FromSeconds(5), cancellationToken: TestContext.Current.CancellationToken);
|
||||
sent.Topic.ShouldBe(AlarmCommandsTopic.Name);
|
||||
var command = sent.Message.ShouldBeOfType<AlarmCommand>();
|
||||
@@ -78,4 +94,18 @@ public sealed class AlertsAckPathTests : TestKit
|
||||
command.Operation.ShouldBe("TimedShelve");
|
||||
command.UnshelveAtUtc.ShouldBe(until);
|
||||
}
|
||||
|
||||
/// <summary>#950: the engine's refusal reaches the page as a refusal with its reason, not as "sent".</summary>
|
||||
[Fact]
|
||||
public async Task A_refused_acknowledge_reaches_the_page_with_the_engines_reason()
|
||||
{
|
||||
var (client, _) = Wire(cmd => new AlarmCommandOutcome(cmd.AlarmId, cmd.Operation, AlarmCommandVerdict.NotApplied,
|
||||
"The last configuration load failed; the command was not applied."));
|
||||
|
||||
var result = await client.AcknowledgeAlarmAsync("alarm-42", "operator-jo", comment: null, CancellationToken.None);
|
||||
|
||||
result.Ok.ShouldBeFalse();
|
||||
result.Verdict.ShouldBe(AlarmCommandVerdict.NotApplied);
|
||||
result.Summary.ShouldBe("Refused: The last configuration load failed; the command was not applied.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,6 +83,18 @@ public sealed class AlarmOperationFormTests:BunitContext
|
||||
cut.WaitForAssertion(()=>cut.FindAll("[role=status]").Count(x=>x.TextContent.Contains("Ack sent")).ShouldBe(1));
|
||||
cut.FindAll("details").ShouldContain(x=>x.TextContent.Contains("Request details"));
|
||||
}
|
||||
// #950: the chip shows the engine's verdict — green Applied, red Refused (reason), amber Outcome unknown.
|
||||
[Theory]
|
||||
[InlineData(ZB.MOM.WW.OtOpcUa.Commons.OpcUa.AlarmCommandVerdict.Applied,true,"Applied","chip-ok")]
|
||||
[InlineData(ZB.MOM.WW.OtOpcUa.Commons.OpcUa.AlarmCommandVerdict.NotApplied,false,"Refused: the last load failed","chip-bad")]
|
||||
[InlineData(ZB.MOM.WW.OtOpcUa.Commons.OpcUa.AlarmCommandVerdict.OutcomeUnknown,false,"Outcome unknown","chip-warn")]
|
||||
public void AcknowledgeOutcome_ShowsTheEngineVerdict(ZB.MOM.WW.OtOpcUa.Commons.OpcUa.AlarmCommandVerdict verdict,bool ok,string summary,string chip)
|
||||
{
|
||||
Service.Ack=_=>Task.FromResult(new AcknowledgeAlarmResult(ok,"site-a-1: per-node detail",new(Guid.Empty),verdict,summary));var cut=Page("alarm-a");
|
||||
cut.Find("tbody button").Click();
|
||||
cut.WaitForAssertion(()=>{var c=cut.Find("[role=status] .chip");c.TextContent.ShouldBe(summary);c.ClassList.ShouldContain(chip);});
|
||||
cut.Markup.ShouldContain("site-a-1: per-node detail");cut.Markup.ShouldNotContain("Ack sent");
|
||||
}
|
||||
[Fact]
|
||||
public async Task DisposedPage_DoesNotRenderLateAcknowledgementOutcome()
|
||||
{
|
||||
|
||||
+24
-2
@@ -22,6 +22,27 @@ public sealed class AdminOperationsActorMailboxTests : ControlPlaneActorTestBase
|
||||
{
|
||||
private static readonly TimeSpan AckBudget = TimeSpan.FromSeconds(1);
|
||||
|
||||
/// <summary>
|
||||
/// #950: a mesh router whose one node applies every alarm command at once — so the ack's reply measures the
|
||||
/// admin mailbox, not a verdict window.
|
||||
/// </summary>
|
||||
private IActorRef AnsweringRouter()
|
||||
{
|
||||
var router = CreateTestProbe();
|
||||
router.SetAutoPilot(new Akka.TestKit.DelegateAutoPilot((_, msg) =>
|
||||
{
|
||||
if (msg is Commons.Messages.Mesh.MeshCommand { VerdictReplyTo: { } collector, Message: Commons.OpcUa.AlarmCommand cmd })
|
||||
{
|
||||
collector.Tell(new AlarmVerdictFanOut(["node-1"]));
|
||||
collector.Tell(new AlarmNodeVerdict("node-1",
|
||||
new Commons.OpcUa.AlarmCommandOutcome(cmd.AlarmId, cmd.Operation, Commons.OpcUa.AlarmCommandVerdict.Applied)));
|
||||
}
|
||||
|
||||
return Akka.TestKit.AutoPilot.KeepRunning;
|
||||
}));
|
||||
return router.Ref;
|
||||
}
|
||||
|
||||
/// <summary>An alarm ack completes in under a second while a 60 s Test Connect is still pending.</summary>
|
||||
[Fact]
|
||||
public void Alarm_ack_completes_while_a_60s_test_connect_is_pending()
|
||||
@@ -29,7 +50,8 @@ public sealed class AdminOperationsActorMailboxTests : ControlPlaneActorTestBase
|
||||
var dbFactory = NewInMemoryDbFactory();
|
||||
var coordinator = CreateTestProbe("coord");
|
||||
var probe = new BlockingProbe();
|
||||
var actor = Sys.ActorOf(AdminOperationsActor.Props(dbFactory, coordinator.Ref, new IDriverProbe[] { probe }));
|
||||
var actor = Sys.ActorOf(AdminOperationsActor.Props(dbFactory, coordinator.Ref, new IDriverProbe[] { probe },
|
||||
meshRouter: AnsweringRouter()));
|
||||
var connectClient = CreateTestProbe("connect-client");
|
||||
|
||||
connectClient.Send(actor, new TestDriverConnect("Blocking", "{}", TimeoutSeconds: 60, Guid.NewGuid()));
|
||||
@@ -74,7 +96,7 @@ public sealed class AdminOperationsActorMailboxTests : ControlPlaneActorTestBase
|
||||
{
|
||||
var gated = new GatedDbFactory(NewInMemoryDbFactory());
|
||||
var coordinator = CreateTestProbe("coord");
|
||||
var actor = Sys.ActorOf(AdminOperationsActor.Props(gated, coordinator.Ref, Enumerable.Empty<IDriverProbe>()));
|
||||
var actor = Sys.ActorOf(AdminOperationsActor.Props(gated, coordinator.Ref, Enumerable.Empty<IDriverProbe>(), meshRouter: AnsweringRouter()));
|
||||
var deployer = CreateTestProbe("deployer");
|
||||
|
||||
gated.Hold();
|
||||
|
||||
@@ -29,9 +29,19 @@ public sealed class AdminOperationsActorTests : ControlPlaneActorTestBase
|
||||
// message sender, not to the subscribed ref). Mirrors ScriptedAlarmHostActor's self-subscribe.
|
||||
probe.Send(mediator, new Subscribe(AlarmCommandsTopic.Name, probe.Ref));
|
||||
probe.ExpectMsg<SubscribeAck>(TimeSpan.FromSeconds(5));
|
||||
// #950: play a node's ScriptedAlarmHostActor — answer the publish's sender (the verdict collector) Applied.
|
||||
probe.SetAutoPilot(new DelegateAutoPilot((sender, msg) =>
|
||||
{
|
||||
if (msg is AlarmCommand { VerdictRequested: true } cmd)
|
||||
sender.Tell(new AlarmCommandOutcome(cmd.AlarmId, cmd.Operation, AlarmCommandVerdict.Applied), probe.Ref);
|
||||
return AutoPilot.KeepRunning;
|
||||
}));
|
||||
return probe;
|
||||
}
|
||||
|
||||
/// <summary>#950: the verdict window; the test system has no driver members, so the collector waits it out.</summary>
|
||||
private static readonly TimeSpan VerdictWindow = TimeSpan.FromSeconds(1);
|
||||
|
||||
/// <summary>Verifies an <see cref="AcknowledgeAlarmCommand"/> publishes a correctly-mapped
|
||||
/// <see cref="AlarmCommand"/> (Operation="Acknowledge", AlarmId/User/Comment threaded, no
|
||||
/// UnshelveAtUtc) onto the <c>alarm-commands</c> topic and replies Ok.</summary>
|
||||
@@ -40,7 +50,7 @@ public sealed class AdminOperationsActorTests : ControlPlaneActorTestBase
|
||||
{
|
||||
var dbFactory = NewInMemoryDbFactory();
|
||||
var coordinator = CreateTestProbe("coord");
|
||||
var actor = Sys.ActorOf(AdminOperationsActor.Props(dbFactory, coordinator.Ref, Enumerable.Empty<IDriverProbe>()));
|
||||
var actor = Sys.ActorOf(AdminOperationsActor.Props(dbFactory, coordinator.Ref, Enumerable.Empty<IDriverProbe>(), alarmVerdictWindow: VerdictWindow));
|
||||
var topicProbe = SubscribeAlarmCommandsProbe();
|
||||
|
||||
var correlationId = CorrelationId.NewId();
|
||||
@@ -55,7 +65,7 @@ public sealed class AdminOperationsActorTests : ControlPlaneActorTestBase
|
||||
|
||||
var reply = ExpectMsg<AcknowledgeAlarmResult>(TimeSpan.FromSeconds(3), cancellationToken: TestContext.Current.CancellationToken);
|
||||
reply.Ok.ShouldBeTrue();
|
||||
reply.Message.ShouldBeNull();
|
||||
reply.Verdict.ShouldBe(AlarmCommandVerdict.Applied);
|
||||
reply.CorrelationId.ShouldBe(correlationId);
|
||||
}
|
||||
|
||||
@@ -66,7 +76,7 @@ public sealed class AdminOperationsActorTests : ControlPlaneActorTestBase
|
||||
{
|
||||
var dbFactory = NewInMemoryDbFactory();
|
||||
var coordinator = CreateTestProbe("coord");
|
||||
var actor = Sys.ActorOf(AdminOperationsActor.Props(dbFactory, coordinator.Ref, Enumerable.Empty<IDriverProbe>()));
|
||||
var actor = Sys.ActorOf(AdminOperationsActor.Props(dbFactory, coordinator.Ref, Enumerable.Empty<IDriverProbe>(), alarmVerdictWindow: VerdictWindow));
|
||||
var topicProbe = SubscribeAlarmCommandsProbe();
|
||||
|
||||
var correlationId = CorrelationId.NewId();
|
||||
@@ -90,7 +100,7 @@ public sealed class AdminOperationsActorTests : ControlPlaneActorTestBase
|
||||
{
|
||||
var dbFactory = NewInMemoryDbFactory();
|
||||
var coordinator = CreateTestProbe("coord");
|
||||
var actor = Sys.ActorOf(AdminOperationsActor.Props(dbFactory, coordinator.Ref, Enumerable.Empty<IDriverProbe>()));
|
||||
var actor = Sys.ActorOf(AdminOperationsActor.Props(dbFactory, coordinator.Ref, Enumerable.Empty<IDriverProbe>(), alarmVerdictWindow: VerdictWindow));
|
||||
var topicProbe = SubscribeAlarmCommandsProbe();
|
||||
|
||||
var unshelveAt = DateTime.UtcNow.AddMinutes(15);
|
||||
@@ -113,7 +123,7 @@ public sealed class AdminOperationsActorTests : ControlPlaneActorTestBase
|
||||
{
|
||||
var dbFactory = NewInMemoryDbFactory();
|
||||
var coordinator = CreateTestProbe("coord");
|
||||
var actor = Sys.ActorOf(AdminOperationsActor.Props(dbFactory, coordinator.Ref, Enumerable.Empty<IDriverProbe>()));
|
||||
var actor = Sys.ActorOf(AdminOperationsActor.Props(dbFactory, coordinator.Ref, Enumerable.Empty<IDriverProbe>(), alarmVerdictWindow: VerdictWindow));
|
||||
var topicProbe = SubscribeAlarmCommandsProbe();
|
||||
|
||||
actor.Tell(new ShelveAlarmCommand("alarm-3", "op-sam", ShelveKind.Unshelve, UnshelveAtUtc: null, Comment: null, CorrelationId.NewId()));
|
||||
@@ -132,7 +142,7 @@ public sealed class AdminOperationsActorTests : ControlPlaneActorTestBase
|
||||
{
|
||||
var dbFactory = NewInMemoryDbFactory();
|
||||
var coordinator = CreateTestProbe("coord");
|
||||
var actor = Sys.ActorOf(AdminOperationsActor.Props(dbFactory, coordinator.Ref, Enumerable.Empty<IDriverProbe>()));
|
||||
var actor = Sys.ActorOf(AdminOperationsActor.Props(dbFactory, coordinator.Ref, Enumerable.Empty<IDriverProbe>(), alarmVerdictWindow: VerdictWindow));
|
||||
var topicProbe = SubscribeAlarmCommandsProbe();
|
||||
|
||||
actor.Tell(new ShelveAlarmCommand("alarm-1", "op-zoe", ShelveKind.Timed, UnshelveAtUtc: null, Comment: null, CorrelationId.NewId()));
|
||||
|
||||
@@ -0,0 +1,252 @@
|
||||
using Akka.Actor;
|
||||
using Akka.Cluster.Tools.PublishSubscribe;
|
||||
using Akka.TestKit;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.Cluster;
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Admin;
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Mesh;
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.Types;
|
||||
using ZB.MOM.WW.OtOpcUa.ControlPlane.AdminOperations;
|
||||
using ZB.MOM.WW.OtOpcUa.ControlPlane.Communication;
|
||||
using ZB.MOM.WW.OtOpcUa.ControlPlane.Tests.Harness;
|
||||
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.ControlPlane.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// #950 — the AdminUI <c>/alerts</c> ack/shelve reports the scripted-alarm engine's verdict instead of "sent": the
|
||||
/// per-request <see cref="AlarmCommandVerdictCollector"/>, the fleet aggregation, and both router legs.
|
||||
/// </summary>
|
||||
public sealed class AlarmCommandVerdictTests : ControlPlaneActorTestBase
|
||||
{
|
||||
private static readonly TimeSpan Window = TimeSpan.FromSeconds(7);
|
||||
|
||||
private static AlarmCommandOutcome Applied => new("alarm-1", "Acknowledge", AlarmCommandVerdict.Applied);
|
||||
|
||||
private static AlarmCommandOutcome Refused(string reason) =>
|
||||
new("alarm-1", "Acknowledge", AlarmCommandVerdict.NotApplied, reason);
|
||||
|
||||
private static AlarmCommandOutcome NotOwned =>
|
||||
new("alarm-1", "Acknowledge", AlarmCommandVerdict.NotApplied, "not loaded", NotOwned: true);
|
||||
|
||||
private static AlarmCommandOutcome Unknown => new("alarm-1", "Acknowledge", AlarmCommandVerdict.OutcomeUnknown, "store stalled", TimedOut: true);
|
||||
|
||||
private static AlarmCommandFleetOutcome Aggregate(
|
||||
Dictionary<string, AlarmCommandOutcome> answers, string[]? unanswered = null, bool expectedKnown = true,
|
||||
string? notDelivered = null) =>
|
||||
AlarmCommandVerdictAggregator.Aggregate(answers, unanswered ?? [], expectedKnown, notDelivered, Window);
|
||||
|
||||
// ------------------------------------------------------------------ aggregation
|
||||
|
||||
[Fact]
|
||||
public void Every_owner_applied_is_Applied()
|
||||
{
|
||||
var o = Aggregate(new() { ["a1"] = Applied, ["a2"] = Applied, ["b1"] = NotOwned });
|
||||
o.Verdict.ShouldBe(AlarmCommandVerdict.Applied);
|
||||
o.Summary.ShouldBe("Applied");
|
||||
o.Detail.ShouldContain("b1: does not own this alarm.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void One_owner_applied_and_its_peer_refused_is_a_partial_Applied()
|
||||
{
|
||||
var o = Aggregate(new() { ["a1"] = Applied, ["a2"] = Refused("last load failed") });
|
||||
o.Verdict.ShouldBe(AlarmCommandVerdict.Applied);
|
||||
o.Summary.ShouldBe("Applied on 1 of 2 servers");
|
||||
o.Detail.ShouldContain("a2: refused — last load failed");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void An_owner_refusal_with_no_applier_and_no_unknown_is_Refused_with_its_reason()
|
||||
{
|
||||
var o = Aggregate(new() { ["a1"] = Refused("last load failed"), ["b1"] = NotOwned });
|
||||
o.Verdict.ShouldBe(AlarmCommandVerdict.NotApplied);
|
||||
o.Summary.ShouldBe("Refused: last load failed");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_refusal_beside_an_unknown_is_unknown_never_refused()
|
||||
{
|
||||
// The unknown node may still apply it: claiming "refused" would be the exact lie #937 removed from the OPC UA path.
|
||||
var o = Aggregate(new() { ["a1"] = Refused("last load failed"), ["a2"] = Unknown });
|
||||
o.Verdict.ShouldBe(AlarmCommandVerdict.OutcomeUnknown);
|
||||
o.Summary.ShouldBe("Outcome unknown");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_silent_expected_node_is_unknown()
|
||||
{
|
||||
var o = Aggregate(new() { ["b1"] = NotOwned }, unanswered: ["a1"]);
|
||||
o.Verdict.ShouldBe(AlarmCommandVerdict.OutcomeUnknown);
|
||||
o.Detail.ShouldContain("a1: no answer within 7 s");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void No_owner_anywhere_is_NotApplied()
|
||||
{
|
||||
var o = Aggregate(new() { ["a1"] = NotOwned, ["b1"] = NotOwned });
|
||||
o.Verdict.ShouldBe(AlarmCommandVerdict.NotApplied);
|
||||
o.Summary.ShouldBe("Not applied: no server owns this alarm");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void No_owner_among_answers_from_an_unknown_node_set_is_unknown()
|
||||
{
|
||||
// DPS with no readable membership: a silent pre-#950 owner is indistinguishable from no owner.
|
||||
var o = Aggregate(new() { ["b1"] = NotOwned }, expectedKnown: false);
|
||||
o.Verdict.ShouldBe(AlarmCommandVerdict.OutcomeUnknown);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_command_that_reached_no_node_is_NotApplied_Not_delivered()
|
||||
{
|
||||
var o = Aggregate(new(), notDelivered: "no enabled, non-maintenance ClusterNode row carries a GrpcPort.");
|
||||
o.Verdict.ShouldBe(AlarmCommandVerdict.NotApplied);
|
||||
o.Summary.ShouldBe("Not delivered");
|
||||
o.Detail.ShouldContain("GrpcPort");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ collector, via a router
|
||||
|
||||
private (IActorRef Admin, TestProbe Router) WireAdmin(TimeSpan? window = null)
|
||||
{
|
||||
var router = CreateTestProbe("router");
|
||||
var admin = Sys.ActorOf(AdminOperationsActor.Props(
|
||||
NewInMemoryDbFactory(), CreateTestProbe().Ref, Enumerable.Empty<IDriverProbe>(),
|
||||
TagConfigValidationMode.Warn, router.Ref, alarmVerdictWindow: window ?? TimeSpan.FromSeconds(10)));
|
||||
return (admin, router);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ack_reports_Applied_as_soon_as_every_dialled_node_has_answered()
|
||||
{
|
||||
var (admin, router) = WireAdmin();
|
||||
var correlation = CorrelationId.NewId();
|
||||
|
||||
admin.Tell(new AcknowledgeAlarmCommand("alarm-1", "alice", null, correlation));
|
||||
|
||||
var sent = router.ExpectMsg<MeshCommand>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
|
||||
sent.Topic.ShouldBe(AlarmCommandsTopic.Name);
|
||||
sent.Message.ShouldBeOfType<AlarmCommand>().VerdictRequested.ShouldBeTrue();
|
||||
var collector = sent.VerdictReplyTo.ShouldNotBeNull();
|
||||
collector.Tell(new AlarmVerdictFanOut(["site-a-1", "site-a-2", "main-1"]));
|
||||
collector.Tell(new AlarmNodeVerdict("site-a-1", Applied));
|
||||
collector.Tell(new AlarmNodeVerdict("main-1", NotOwned));
|
||||
ExpectNoMsg(TimeSpan.FromMilliseconds(200), TestContext.Current.CancellationToken); // site-a-2 still owed
|
||||
collector.Tell(new AlarmNodeVerdict("site-a-2", Applied));
|
||||
|
||||
// Well inside the 10 s window: the reply does not wait it out once everyone answered.
|
||||
var reply = ExpectMsg<AcknowledgeAlarmResult>(TimeSpan.FromSeconds(3), cancellationToken: TestContext.Current.CancellationToken);
|
||||
reply.Ok.ShouldBeTrue();
|
||||
reply.Verdict.ShouldBe(AlarmCommandVerdict.Applied);
|
||||
reply.Summary.ShouldBe("Applied");
|
||||
reply.CorrelationId.ShouldBe(correlation);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Shelve_reports_the_owners_refusal_reason()
|
||||
{
|
||||
var (admin, router) = WireAdmin();
|
||||
|
||||
admin.Tell(new ShelveAlarmCommand("alarm-1", "alice", ShelveKind.OneShot, null, null, CorrelationId.NewId()));
|
||||
|
||||
var collector = router.ExpectMsg<MeshCommand>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken).VerdictReplyTo!;
|
||||
collector.Tell(new AlarmVerdictFanOut(["a1", "b1"]));
|
||||
collector.Tell(new AlarmNodeVerdict("a1", Refused("The last configuration load failed; the command was not applied.")));
|
||||
collector.Tell(new AlarmNodeVerdict("b1", NotOwned));
|
||||
|
||||
var reply = ExpectMsg<ShelveAlarmResult>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
|
||||
reply.Ok.ShouldBeFalse();
|
||||
reply.Verdict.ShouldBe(AlarmCommandVerdict.NotApplied);
|
||||
reply.Summary.ShouldBe("Refused: The last configuration load failed; the command was not applied.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_node_that_never_answers_makes_the_outcome_unknown_at_the_window()
|
||||
{
|
||||
var (admin, router) = WireAdmin(TimeSpan.FromSeconds(1));
|
||||
|
||||
admin.Tell(new AcknowledgeAlarmCommand("alarm-1", "alice", null, CorrelationId.NewId()));
|
||||
|
||||
var collector = router.ExpectMsg<MeshCommand>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken).VerdictReplyTo!;
|
||||
collector.Tell(new AlarmVerdictFanOut(["a1", "a2"]));
|
||||
collector.Tell(new AlarmNodeVerdict("a2", NotOwned));
|
||||
|
||||
var reply = ExpectMsg<AcknowledgeAlarmResult>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
|
||||
reply.Ok.ShouldBeFalse();
|
||||
reply.Verdict.ShouldBe(AlarmCommandVerdict.OutcomeUnknown);
|
||||
reply.Message.ShouldNotBeNull().ShouldContain("a1: no answer within 1 s");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_command_the_router_could_not_deliver_is_reported_at_once()
|
||||
{
|
||||
var (admin, router) = WireAdmin();
|
||||
|
||||
admin.Tell(new AcknowledgeAlarmCommand("alarm-1", "alice", null, CorrelationId.NewId()));
|
||||
|
||||
router.ExpectMsg<MeshCommand>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken)
|
||||
.VerdictReplyTo!.Tell(new AlarmVerdictFanOut([], "no enabled, non-maintenance ClusterNode row carries a GrpcPort."));
|
||||
|
||||
var reply = ExpectMsg<AcknowledgeAlarmResult>(TimeSpan.FromSeconds(3), cancellationToken: TestContext.Current.CancellationToken);
|
||||
reply.Verdict.ShouldBe(AlarmCommandVerdict.NotApplied);
|
||||
reply.Summary.ShouldBe("Not delivered");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ DistributedPubSub legs
|
||||
|
||||
/// <summary>Subscribes a probe that plays a node's ScriptedAlarmHostActor: answers <paramref name="answer"/> to the publish's sender.</summary>
|
||||
private TestProbe SubscribeAnsweringHost(Func<AlarmCommand, AlarmCommandOutcome?> answer)
|
||||
{
|
||||
var host = CreateTestProbe("host");
|
||||
host.SetAutoPilot(new DelegateAutoPilot((sender, msg) =>
|
||||
{
|
||||
if (msg is AlarmCommand cmd && answer(cmd) is { } outcome) sender.Tell(outcome, host.Ref);
|
||||
return AutoPilot.KeepRunning;
|
||||
}));
|
||||
var mediator = DistributedPubSub.Get(Sys).Mediator;
|
||||
var subscriber = CreateTestProbe();
|
||||
mediator.Tell(new Subscribe(AlarmCommandsTopic.Name, host.Ref), subscriber.Ref);
|
||||
subscriber.ExpectMsg<SubscribeAck>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
|
||||
return host;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void With_no_router_the_publish_carries_the_collector_as_sender_and_the_hosts_answer_counts()
|
||||
{
|
||||
AlarmCommand? seen = null;
|
||||
SubscribeAnsweringHost(cmd =>
|
||||
{
|
||||
seen = cmd;
|
||||
return cmd.VerdictRequested ? Applied : null;
|
||||
});
|
||||
var admin = Sys.ActorOf(AdminOperationsActor.Props(
|
||||
NewInMemoryDbFactory(), CreateTestProbe().Ref, Enumerable.Empty<IDriverProbe>(),
|
||||
alarmVerdictWindow: TimeSpan.FromSeconds(1)));
|
||||
|
||||
admin.Tell(new AcknowledgeAlarmCommand("alarm-1", "alice", null, CorrelationId.NewId()));
|
||||
|
||||
var reply = ExpectMsg<AcknowledgeAlarmResult>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
|
||||
reply.Verdict.ShouldBe(AlarmCommandVerdict.Applied);
|
||||
seen.ShouldNotBeNull().VerdictRequested.ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dps_router_publishes_with_the_collector_as_sender()
|
||||
{
|
||||
SubscribeAnsweringHost(cmd => cmd.VerdictRequested ? Refused("nope") : null);
|
||||
var router = Sys.ActorOf(CentralCommunicationActor.Props(
|
||||
NewInMemoryDbFactory(), new MeshTransportOptions(), () => null));
|
||||
var collector = CreateTestProbe("collector");
|
||||
|
||||
router.Tell(new MeshCommand(AlarmCommandsTopic.Name,
|
||||
new AlarmCommand("alarm-1", "Acknowledge", "alice", null, null, VerdictRequested: true), collector.Ref));
|
||||
|
||||
collector.ExpectMsg<AlarmVerdictFanOut>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken)
|
||||
.NotDeliveredReason.ShouldBeNull();
|
||||
collector.ExpectMsg<AlarmCommandOutcome>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken)
|
||||
.Reason.ShouldBe("nope");
|
||||
}
|
||||
}
|
||||
+79
@@ -414,6 +414,75 @@ public class CentralCommunicationActorTests : ControlPlaneActorTestBase
|
||||
rig.Dialer.Calls[0].Envelope.AlarmCommand.AlarmId.ShouldBe("alarm-1");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// #950: an alarm command with a verdict collector tells it the dialled set first, then one verdict per node —
|
||||
/// the node's own (from <c>ExecuteAck.alarm_verdict</c>), "no verdict" (an empty ack: a pre-#950 node), or the
|
||||
/// transport failure, each of the last two UNKNOWN, never refused.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void An_alarm_command_with_a_collector_reports_each_nodes_verdict()
|
||||
{
|
||||
var factory = NewInMemoryDbFactory();
|
||||
SeedGrpcCluster(
|
||||
factory,
|
||||
"C1",
|
||||
("node-a", "host-a", 4056, true, false),
|
||||
("node-b", "host-b", 4056, true, false),
|
||||
("node-c", "host-c", 4056, true, false));
|
||||
var dialer = new RecordingDialer
|
||||
{
|
||||
FailEndpointsContaining = "host-c",
|
||||
Fault = new Grpc.Core.RpcException(new Grpc.Core.Status(Grpc.Core.StatusCode.Unavailable, "connection refused")),
|
||||
Answer = endpoint => endpoint.Contains("host-a")
|
||||
? new ExecuteAck { AlarmVerdict = new AlarmVerdictMessage { Verdict = AlarmVerdict.Applied } }
|
||||
: new ExecuteAck(),
|
||||
};
|
||||
var rig = SpawnGrpcRig(factory, dialer);
|
||||
AwaitContactsLoaded(rig);
|
||||
var collector = CreateTestProbe("collector");
|
||||
|
||||
rig.Actor.Tell(new MeshCommand(
|
||||
"alarm-commands",
|
||||
new AlarmCommand("alarm-1", "Acknowledge", "operator", null, null, VerdictRequested: true),
|
||||
collector.Ref));
|
||||
|
||||
collector.ExpectMsg<ZB.MOM.WW.OtOpcUa.ControlPlane.AdminOperations.AlarmVerdictFanOut>(
|
||||
PresenceBudget, cancellationToken: TestContext.Current.CancellationToken)
|
||||
.Nodes.ShouldBe(new[] { "node-a", "node-b", "node-c" }, ignoreOrder: true);
|
||||
var verdicts = Enumerable.Range(0, 3)
|
||||
.Select(_ => collector.ExpectMsg<ZB.MOM.WW.OtOpcUa.ControlPlane.AdminOperations.AlarmNodeVerdict>(
|
||||
PresenceBudget, cancellationToken: TestContext.Current.CancellationToken))
|
||||
.ToDictionary(v => v.Node, v => v.Outcome);
|
||||
|
||||
verdicts["node-a"].Verdict.ShouldBe(AlarmCommandVerdict.Applied);
|
||||
verdicts["node-b"].Verdict.ShouldBe(AlarmCommandVerdict.OutcomeUnknown);
|
||||
verdicts["node-b"].Reason.ShouldNotBeNull().ShouldContain("older than #950");
|
||||
verdicts["node-c"].Verdict.ShouldBe(AlarmCommandVerdict.OutcomeUnknown);
|
||||
verdicts["node-c"].Reason.ShouldNotBeNull().ShouldContain("Unavailable");
|
||||
rig.Dialer.Calls.ShouldAllBe(c => c.Envelope.AlarmCommand.VerdictRequested);
|
||||
}
|
||||
|
||||
/// <summary>#950: with nothing to dial, the collector hears "not delivered" at once rather than waiting out its window.</summary>
|
||||
[Fact]
|
||||
public void An_alarm_command_with_a_collector_and_no_targets_is_reported_not_delivered()
|
||||
{
|
||||
var factory = NewInMemoryDbFactory();
|
||||
SeedGrpcCluster(factory, "C1", ("node-a", "host-a", null, true, false));
|
||||
var rig = SpawnGrpcRig(factory);
|
||||
AwaitContactsLoaded(rig);
|
||||
var collector = CreateTestProbe("collector");
|
||||
|
||||
rig.Actor.Tell(new MeshCommand(
|
||||
"alarm-commands",
|
||||
new AlarmCommand("alarm-1", "Acknowledge", "operator", null, null, VerdictRequested: true),
|
||||
collector.Ref));
|
||||
|
||||
var fan = collector.ExpectMsg<ZB.MOM.WW.OtOpcUa.ControlPlane.AdminOperations.AlarmVerdictFanOut>(
|
||||
PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
|
||||
fan.Nodes.ShouldBeEmpty();
|
||||
fan.NotDeliveredReason.ShouldNotBeNull().ShouldContain("GrpcPort");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_contact_refresh_publishes_the_known_cluster_set_and_drops_cached_psks()
|
||||
{
|
||||
@@ -1042,6 +1111,16 @@ public class CentralCommunicationActorTests : ControlPlaneActorTestBase
|
||||
: Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>#950: what a successful dial answers, per endpoint; null ⇒ an empty ack.</summary>
|
||||
public Func<string, ExecuteAck?>? Answer { get; set; }
|
||||
|
||||
public async Task<ExecuteAck> ExecuteAsync(
|
||||
string nodeEndpoint, CommandEnvelope envelope, string clusterId, CancellationToken ct)
|
||||
{
|
||||
await DispatchAsync(nodeEndpoint, envelope, clusterId, ct);
|
||||
return Answer?.Invoke(nodeEndpoint) ?? new ExecuteAck();
|
||||
}
|
||||
|
||||
/// <summary>Every endpoint set handed to <see cref="RetainOnly"/>, in order.</summary>
|
||||
public IReadOnlyList<IReadOnlyCollection<string>> RetainCalls
|
||||
{
|
||||
|
||||
+4
-2
@@ -113,7 +113,8 @@ public class MeshRouterDispatchTests : ControlPlaneActorTestBase
|
||||
|
||||
var cmd = router.ExpectMsg<MeshCommand>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
|
||||
cmd.Topic.ShouldBe(AlarmCommandsTopic.Name);
|
||||
cmd.Message.ShouldBeOfType<AlarmCommand>();
|
||||
cmd.Message.ShouldBeOfType<AlarmCommand>().VerdictRequested.ShouldBeTrue();
|
||||
cmd.VerdictReplyTo.ShouldNotBeNull(); // #950: the per-request verdict collector
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -131,6 +132,7 @@ public class MeshRouterDispatchTests : ControlPlaneActorTestBase
|
||||
|
||||
var cmd = router.ExpectMsg<MeshCommand>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
|
||||
cmd.Topic.ShouldBe(AlarmCommandsTopic.Name);
|
||||
cmd.Message.ShouldBeOfType<AlarmCommand>();
|
||||
cmd.Message.ShouldBeOfType<AlarmCommand>().VerdictRequested.ShouldBeTrue();
|
||||
cmd.VerdictReplyTo.ShouldNotBeNull(); // #950: the per-request verdict collector
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,7 +85,8 @@ public sealed class MeshGrpcBoundaryTests : IAsyncLifetime
|
||||
public async ValueTask InitializeAsync()
|
||||
{
|
||||
_central = StartSelfSeededSystem();
|
||||
_siteA = await SiteNode.StartAsync("SITE-A", SiteAKey);
|
||||
// #950: site-A runs a scripted-alarm owner that applies every command; site-B runs no driver host at all.
|
||||
_siteA = await SiteNode.StartAsync("SITE-A", SiteAKey, withAlarmOwner: true);
|
||||
_siteB = await SiteNode.StartAsync("SITE-B", SiteBKey);
|
||||
|
||||
await Task.WhenAll(
|
||||
@@ -214,6 +215,43 @@ public sealed class MeshGrpcBoundaryTests : IAsyncLifetime
|
||||
await _siteA.Inbox.Task.WaitAsync(Arrival, TestContext.Current.CancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// #950, end to end over the real wire: the AdminUI ack path (real <c>AdminOperationsActor</c> → real
|
||||
/// <c>CentralCommunicationActor</c> → real dialer → real interceptor → real <c>NodeCommandGrpcService</c>) comes back
|
||||
/// with each node's engine verdict in <c>ExecuteAck.alarm_verdict</c>: site-A's owner applied it, site-B owns
|
||||
/// nothing — so the operator is told Applied, not "sent".
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task An_AdminUI_ack_reports_the_owning_nodes_verdict_across_the_gRPC_boundary()
|
||||
{
|
||||
var dbFactory = SeedTwoClusterDb(_siteA, _siteB);
|
||||
var router = _central.ActorOf(CentralCommunicationActor.Props(
|
||||
dbFactory, GrpcOptions(), () => null, () => Dialer(), () => new FixedPskProvider()));
|
||||
var admin = _central.ActorOf(ZB.MOM.WW.OtOpcUa.ControlPlane.AdminOperations.AdminOperationsActor.Props(
|
||||
dbFactory, _central.DeadLetters, [], meshRouter: router));
|
||||
|
||||
AcknowledgeAlarmResult? result = null;
|
||||
var deadline = DateTime.UtcNow + Arrival;
|
||||
while (DateTime.UtcNow < deadline)
|
||||
{
|
||||
// Re-asked only while central has not loaded its contacts yet ("Not delivered") — see the dispatch test above.
|
||||
result = await admin.Ask<AcknowledgeAlarmResult>(
|
||||
new AcknowledgeAlarmCommand("alarm-1", "alice", null, Commons.Types.CorrelationId.NewId()),
|
||||
TimeSpan.FromSeconds(15), TestContext.Current.CancellationToken);
|
||||
if (result.Summary != "Not delivered") break;
|
||||
await Task.Delay(250, TestContext.Current.CancellationToken);
|
||||
}
|
||||
|
||||
result.ShouldNotBeNull();
|
||||
result.Verdict.ShouldBe(Commons.OpcUa.AlarmCommandVerdict.Applied, result.Message);
|
||||
result.Ok.ShouldBeTrue();
|
||||
result.Summary.ShouldBe("Applied");
|
||||
result.Message.ShouldNotBeNull().ShouldContain("node-a: applied.");
|
||||
result.Message.ShouldContain("node-b: does not own this alarm.");
|
||||
// Delivered by the verdict path only — never ALSO re-emitted on site-A's EventStream (one copy per engine).
|
||||
_siteA.AlarmEventStreamCopies.ShouldBe(0);
|
||||
}
|
||||
|
||||
// ---- harness ---------------------------------------------------------------------------------
|
||||
|
||||
private static MeshGrpcCommandDialer Dialer(IMeshPskProvider? psk = null) =>
|
||||
@@ -316,7 +354,12 @@ public sealed class MeshGrpcBoundaryTests : IAsyncLifetime
|
||||
public TaskCompletionSource<object> Inbox { get; } =
|
||||
new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
|
||||
public static async Task<SiteNode> StartAsync(string clusterId, string psk)
|
||||
/// <summary>#950: how many AlarmCommands reached this node's EventStream (the one-way path).</summary>
|
||||
public int AlarmEventStreamCopies => _alarmCopies;
|
||||
|
||||
private int _alarmCopies;
|
||||
|
||||
public static async Task<SiteNode> StartAsync(string clusterId, string psk, bool withAlarmOwner = false)
|
||||
{
|
||||
var node = new SiteNode { System = StartSelfSeededSystem(), Port = GetFreePort() };
|
||||
|
||||
@@ -329,6 +372,11 @@ public sealed class MeshGrpcBoundaryTests : IAsyncLifetime
|
||||
|
||||
var registry = new ActorRegistry();
|
||||
registry.Register<NodeCommunicationActorKey>(comm);
|
||||
node.System.EventStream.Subscribe(
|
||||
node.System.ActorOf(Akka.Actor.Props.Create(() => new CountActor(() => Interlocked.Increment(ref node._alarmCopies)))),
|
||||
typeof(Commons.OpcUa.AlarmCommand));
|
||||
if (withAlarmOwner)
|
||||
registry.Register<DriverHostActorKey>(node.System.ActorOf(Akka.Actor.Props.Create(() => new ApplyingDriverHost())));
|
||||
|
||||
var builder = WebApplication.CreateBuilder();
|
||||
builder.Environment.ApplicationName = typeof(MeshGrpcBoundaryTests).Assembly.GetName().Name!;
|
||||
@@ -411,6 +459,21 @@ public sealed class MeshGrpcBoundaryTests : IAsyncLifetime
|
||||
return port;
|
||||
}
|
||||
|
||||
/// <summary>#950: stands in for a driver host whose scripted-alarm engine owns and applies every command.</summary>
|
||||
private sealed class ApplyingDriverHost : ReceiveActor
|
||||
{
|
||||
public ApplyingDriverHost() =>
|
||||
Receive<ZB.MOM.WW.OtOpcUa.Runtime.ScriptedAlarms.ScriptedAlarmHostActor.ExecuteAlarmCommand>(m =>
|
||||
Sender.Tell(new Commons.OpcUa.AlarmCommandOutcome(
|
||||
m.Command.AlarmId, m.Command.Operation, Commons.OpcUa.AlarmCommandVerdict.Applied)));
|
||||
}
|
||||
|
||||
/// <summary>Counts what it receives.</summary>
|
||||
private sealed class CountActor : ReceiveActor
|
||||
{
|
||||
public CountActor(Action onMessage) => ReceiveAny(_ => onMessage());
|
||||
}
|
||||
|
||||
/// <summary>Completes an inbox with the first message it receives.</summary>
|
||||
private sealed class CaptureActor : ReceiveActor
|
||||
{
|
||||
|
||||
@@ -66,6 +66,96 @@ public sealed class NodeCommandGrpcServiceTests : TestKit
|
||||
probe.ExpectMsg<AlarmCommand>(cancellationToken: TestContext.Current.CancellationToken).ShouldBe(alarm);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// #950: an alarm command asking for a verdict goes to the driver host as an <c>ExecuteAlarmCommand</c> (no ask
|
||||
/// deadline — nothing else carries it here), NOT to the comm actor, and the host's answer rides the ack.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task A_verdict_request_asks_the_driver_host_and_returns_its_answer()
|
||||
{
|
||||
var comm = CreateTestProbe();
|
||||
var driverHost = CreateTestProbe();
|
||||
driverHost.SetAutoPilot(new Akka.TestKit.DelegateAutoPilot((sender, msg) =>
|
||||
{
|
||||
if (msg is ZB.MOM.WW.OtOpcUa.Runtime.ScriptedAlarms.ScriptedAlarmHostActor.ExecuteAlarmCommand ask)
|
||||
{
|
||||
sender.Tell(new AlarmCommandOutcome(ask.Command.AlarmId, ask.Command.Operation,
|
||||
AlarmCommandVerdict.NotApplied, "last load failed"), Akka.Actor.ActorRefs.NoSender);
|
||||
}
|
||||
|
||||
return Akka.TestKit.AutoPilot.KeepRunning;
|
||||
}));
|
||||
var registry = new ActorRegistry();
|
||||
registry.Register<NodeCommunicationActorKey>(comm.Ref);
|
||||
registry.Register<DriverHostActorKey>(driverHost.Ref);
|
||||
var service = NewService(registry);
|
||||
var alarm = new AlarmCommand("alarm-7", "Acknowledge", "carol", null, null, VerdictRequested: true);
|
||||
|
||||
var ack = await service.Execute(MeshTransportDtoMapper.ToEnvelope(alarm, AlarmCorrelation), Ctx());
|
||||
|
||||
var asked = driverHost.ExpectMsg<ZB.MOM.WW.OtOpcUa.Runtime.ScriptedAlarms.ScriptedAlarmHostActor.ExecuteAlarmCommand>(
|
||||
cancellationToken: TestContext.Current.CancellationToken);
|
||||
asked.Command.AlarmId.ShouldBe("alarm-7");
|
||||
asked.DeadlineUtc.ShouldBeNull();
|
||||
ack.AlarmVerdict.ShouldNotBeNull();
|
||||
var outcome = MeshTransportDtoMapper.FromProto(ack.AlarmVerdict, "alarm-7", "Acknowledge");
|
||||
outcome.Verdict.ShouldBe(AlarmCommandVerdict.NotApplied);
|
||||
outcome.Reason.ShouldBe("last load failed");
|
||||
comm.ExpectNoMsg(TimeSpan.FromMilliseconds(200), TestContext.Current.CancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>#950: an engine that does not answer in time is an unknown (timed-out) verdict, never a failed call.</summary>
|
||||
[Fact]
|
||||
public async Task A_silent_driver_host_is_a_timed_out_unknown()
|
||||
{
|
||||
var registry = new ActorRegistry();
|
||||
registry.Register<NodeCommunicationActorKey>(CreateTestProbe().Ref);
|
||||
registry.Register<DriverHostActorKey>(CreateTestProbe().Ref);
|
||||
var service = new NodeCommandGrpcService(registry, NullLogger<NodeCommandGrpcService>.Instance)
|
||||
{
|
||||
VerdictWait = TimeSpan.FromMilliseconds(300),
|
||||
};
|
||||
var alarm = new AlarmCommand("alarm-7", "Acknowledge", "carol", null, null, VerdictRequested: true);
|
||||
|
||||
var ack = await service.Execute(MeshTransportDtoMapper.ToEnvelope(alarm, AlarmCorrelation), Ctx());
|
||||
|
||||
ack.AlarmVerdict.Verdict.ShouldBe(AlarmVerdict.OutcomeUnknown);
|
||||
ack.AlarmVerdict.TimedOut.ShouldBeTrue();
|
||||
}
|
||||
|
||||
/// <summary>#950: a node with no driver host owns no scripted alarm — answered NotApplied/NotOwned.</summary>
|
||||
[Fact]
|
||||
public async Task A_verdict_request_on_a_node_without_a_driver_host_is_not_owned()
|
||||
{
|
||||
var registry = new ActorRegistry();
|
||||
registry.Register<NodeCommunicationActorKey>(CreateTestProbe().Ref);
|
||||
var service = NewService(registry);
|
||||
var alarm = new AlarmCommand("alarm-7", "Acknowledge", "carol", null, null, VerdictRequested: true);
|
||||
|
||||
var ack = await service.Execute(MeshTransportDtoMapper.ToEnvelope(alarm, AlarmCorrelation), Ctx());
|
||||
|
||||
ack.AlarmVerdict.Verdict.ShouldBe(AlarmVerdict.NotApplied);
|
||||
ack.AlarmVerdict.NotOwned.ShouldBeTrue();
|
||||
}
|
||||
|
||||
/// <summary>#950: without the request (a pre-#950 central) the one-way path is untouched — an empty ack.</summary>
|
||||
[Fact]
|
||||
public async Task An_alarm_command_without_a_verdict_request_stays_one_way()
|
||||
{
|
||||
var comm = CreateTestProbe();
|
||||
var driverHost = CreateTestProbe();
|
||||
var registry = new ActorRegistry();
|
||||
registry.Register<NodeCommunicationActorKey>(comm.Ref);
|
||||
registry.Register<DriverHostActorKey>(driverHost.Ref);
|
||||
var alarm = new AlarmCommand("alarm-7", "Acknowledge", "carol", null, null);
|
||||
|
||||
var ack = await NewService(registry).Execute(MeshTransportDtoMapper.ToEnvelope(alarm, AlarmCorrelation), Ctx());
|
||||
|
||||
ack.AlarmVerdict.ShouldBeNull();
|
||||
comm.ExpectMsg<AlarmCommand>(cancellationToken: TestContext.Current.CancellationToken).ShouldBe(alarm);
|
||||
driverHost.ExpectNoMsg(TimeSpan.FromMilliseconds(200), TestContext.Current.CancellationToken);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_call_in_the_boot_window_before_the_actor_is_registered_is_unavailable()
|
||||
{
|
||||
|
||||
+116
@@ -0,0 +1,116 @@
|
||||
using Akka.Actor;
|
||||
using Akka.TestKit;
|
||||
using Serilog;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
|
||||
using ZB.MOM.WW.OtOpcUa.Core.ScriptedAlarms;
|
||||
using ZB.MOM.WW.OtOpcUa.Core.Scripting;
|
||||
using ZB.MOM.WW.OtOpcUa.OpcUaServer;
|
||||
using ZB.MOM.WW.OtOpcUa.Runtime.ScriptedAlarms;
|
||||
using ZB.MOM.WW.OtOpcUa.Runtime.Tests.Harness;
|
||||
using ZB.MOM.WW.OtOpcUa.Runtime.VirtualTags;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.ScriptedAlarms;
|
||||
|
||||
/// <summary>
|
||||
/// Gitea #950 — a broadcast <see cref="AlarmCommand"/> from central's AdminUI path (<c>VerdictRequested</c>) is answered
|
||||
/// to its sender with the engine's verdict, non-owners included (<c>NotOwned</c>); every other broadcast stays one-way.
|
||||
/// </summary>
|
||||
public sealed class ScriptedAlarmBroadcastVerdictTests : RuntimeActorTestBase
|
||||
{
|
||||
private static readonly TimeSpan Timeout = TimeSpan.FromSeconds(30);
|
||||
private static CancellationToken Ct => TestContext.Current.CancellationToken;
|
||||
|
||||
private static EquipmentScriptedAlarmPlan Plan(string id, string source = "return (int)ctx.GetTag(\"M.T\").Value > 90;") =>
|
||||
new(ScriptedAlarmId: id, EquipmentId: "Plant/Line1/M", Name: id, AlarmType: "AlarmCondition", Severity: 800,
|
||||
MessageTemplate: "condition", PredicateScriptId: $"{id}-script", PredicateSource: source,
|
||||
DependencyRefs: ["M.T"], HistorizeToAveva: false, Retain: true, Enabled: true);
|
||||
|
||||
private (IActorRef Host, ScriptedAlarmEngine Engine) SpawnLoaded(params EquipmentScriptedAlarmPlan[] plans)
|
||||
{
|
||||
var publish = CreateTestProbe();
|
||||
var mux = CreateTestProbe();
|
||||
var upstream = new DependencyMuxTagUpstreamSource();
|
||||
var logger = new LoggerConfiguration().CreateLogger();
|
||||
var engine = new ScriptedAlarmEngine(upstream, new InMemoryAlarmStateStore(), new ScriptLoggerFactory(logger), logger);
|
||||
var host = Sys.ActorOf(ScriptedAlarmHostActor.Props(publish.Ref, mux.Ref, () => (upstream, engine),
|
||||
readiness: new ScriptedAlarmCommandReadinessView()));
|
||||
host.Tell(new ScriptedAlarmHostActor.ApplyScriptedAlarms(plans));
|
||||
mux.ExpectMsg<DependencyMuxActor.RegisterInterest>(Timeout, cancellationToken: Ct);
|
||||
return (host, engine);
|
||||
}
|
||||
|
||||
private static AlarmCommand Broadcast(string alarmId, string text, bool verdictRequested) =>
|
||||
new(alarmId, "AddComment", "alice", text, null, VerdictRequested: verdictRequested);
|
||||
|
||||
[Fact]
|
||||
public void An_owner_answers_its_verdict_to_the_publisher()
|
||||
{
|
||||
var (host, engine) = SpawnLoaded(Plan("alm-1"));
|
||||
var collector = CreateTestProbe();
|
||||
|
||||
host.Tell(Broadcast("alm-1", "from the AdminUI", verdictRequested: true), collector.Ref);
|
||||
|
||||
var outcome = collector.ExpectMsg<AlarmCommandOutcome>(Timeout, cancellationToken: Ct);
|
||||
outcome.Verdict.ShouldBe(AlarmCommandVerdict.Applied);
|
||||
outcome.NotOwned.ShouldBeFalse();
|
||||
engine.GetState("alm-1")!.Comments.Select(c => c.Text).ShouldBe(["from the AdminUI"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_non_owner_answers_not_owned_and_applies_nothing()
|
||||
{
|
||||
var (host, _) = SpawnLoaded(Plan("alm-1"));
|
||||
var collector = CreateTestProbe();
|
||||
|
||||
host.Tell(Broadcast("someone-elses", "x", verdictRequested: true), collector.Ref);
|
||||
|
||||
var outcome = collector.ExpectMsg<AlarmCommandOutcome>(Timeout, cancellationToken: Ct);
|
||||
outcome.Verdict.ShouldBe(AlarmCommandVerdict.NotApplied);
|
||||
outcome.NotOwned.ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void An_owner_refusal_is_answered_as_a_refusal_not_as_not_owned()
|
||||
{
|
||||
var (host, _) = SpawnLoaded(Plan("alm-1"));
|
||||
var collector = CreateTestProbe();
|
||||
|
||||
// AddComment without text is refused by the host before the engine runs.
|
||||
host.Tell(Broadcast("alm-1", " ", verdictRequested: true), collector.Ref);
|
||||
|
||||
var outcome = collector.ExpectMsg<AlarmCommandOutcome>(Timeout, cancellationToken: Ct);
|
||||
outcome.Verdict.ShouldBe(AlarmCommandVerdict.NotApplied);
|
||||
outcome.NotOwned.ShouldBeFalse();
|
||||
outcome.Reason.ShouldNotBeNull().ShouldContain("comment text");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_broadcast_that_did_not_ask_for_a_verdict_is_not_answered()
|
||||
{
|
||||
var (host, engine) = SpawnLoaded(Plan("alm-1"));
|
||||
var publisher = CreateTestProbe();
|
||||
|
||||
host.Tell(Broadcast("alm-1", "one-way", verdictRequested: false), publisher.Ref);
|
||||
host.Tell(Broadcast("someone-elses", "one-way", verdictRequested: false), publisher.Ref);
|
||||
// Positive control queued behind both: a verdict request IS answered, so the silence above is not a slow mailbox.
|
||||
host.Tell(Broadcast("alm-1", "asked", verdictRequested: true), publisher.Ref);
|
||||
|
||||
publisher.ExpectMsg<AlarmCommandOutcome>(Timeout, cancellationToken: Ct).Verdict.ShouldBe(AlarmCommandVerdict.Applied);
|
||||
publisher.ExpectNoMsg(TimeSpan.FromMilliseconds(200), Ct);
|
||||
engine.GetState("alm-1")!.Comments.Select(c => c.Text).ShouldBe(["one-way", "asked"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_direct_ask_for_an_unowned_alarm_is_flagged_not_owned()
|
||||
{
|
||||
var (host, _) = SpawnLoaded(Plan("alm-1"));
|
||||
|
||||
var outcome = await host.Ask<AlarmCommandOutcome>(new ScriptedAlarmHostActor.ExecuteAlarmCommand(
|
||||
new AlarmCommand("someone-elses", "Acknowledge", "alice", null, null)), Timeout, Ct);
|
||||
|
||||
outcome.Verdict.ShouldBe(AlarmCommandVerdict.NotApplied);
|
||||
outcome.NotOwned.ShouldBeTrue();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user