Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.ControlPlane.Tests/Communication/MeshRouterDispatchTests.cs
T
Joseph Doherty 09aaabbeef 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>
2026-12-01 19:45:17 -05:00

139 lines
5.9 KiB
C#

using Akka.Actor;
using Akka.Cluster.Tools.PublishSubscribe;
using Shouldly;
using Xunit;
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.Coordinators;
using ZB.MOM.WW.OtOpcUa.ControlPlane.Tests.Harness;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
namespace ZB.MOM.WW.OtOpcUa.ControlPlane.Tests.Communication;
/// <summary>
/// Proves the five central→node publish sites go through the transport router when one is
/// wired, and fall back to the mediator when it is not.
/// </summary>
/// <remarks>
/// The fallback is what keeps the ~12 pre-existing coordinator and admin-operations tests
/// meaningful: they construct these actors without a router and must still exercise the real
/// DistributedPubSub path, not a silently-disabled one.
/// </remarks>
public class MeshRouterDispatchTests : ControlPlaneActorTestBase
{
private const string DeploymentsTopic = "deployments";
[Fact]
public void Deploy_dispatch_goes_to_the_router_when_one_is_wired()
{
var router = CreateTestProbe();
var dbFactory = NewInMemoryDbFactory();
var coordinator = Sys.ActorOf(
ConfigPublishCoordinator.Props(dbFactory, applyDeadline: null, meshRouter: router.Ref));
var deploymentId = new DeploymentId(Guid.NewGuid());
coordinator.Tell(new Commons.Messages.Deploy.DispatchDeployment(
deploymentId, new RevisionHash("rev-1"), CorrelationId.NewId()));
var cmd = router.ExpectMsg<MeshCommand>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
cmd.Topic.ShouldBe(DeploymentsTopic);
cmd.Message.ShouldBeOfType<Commons.Messages.Deploy.DispatchDeployment>();
}
[Fact]
public void Deploy_dispatch_falls_back_to_the_mediator_when_no_router_is_wired()
{
var subscriber = CreateTestProbe();
DistributedPubSub.Get(Sys).Mediator.Tell(
new Subscribe(DeploymentsTopic, subscriber.Ref), subscriber.Ref);
subscriber.ExpectMsg<SubscribeAck>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
var coordinator = Sys.ActorOf(ConfigPublishCoordinator.Props(NewInMemoryDbFactory()));
var deploymentId = new DeploymentId(Guid.NewGuid());
coordinator.Tell(new Commons.Messages.Deploy.DispatchDeployment(
deploymentId, new RevisionHash("rev-1"), CorrelationId.NewId()));
// Presence budget, not 5 s: the coordinator's first ConfigDb (EF InMemory) read sits in front
// of the dispatch and is a cold multi-second cost on a loaded CI runner (#606).
subscriber.ExpectMsg<Commons.Messages.Deploy.DispatchDeployment>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
}
[Fact]
public void Restart_driver_goes_to_the_router_on_the_driver_control_topic()
{
var router = CreateTestProbe();
var admin = Sys.ActorOf(AdminOperationsActor.Props(
NewInMemoryDbFactory(),
CreateTestProbe().Ref,
Enumerable.Empty<IDriverProbe>(),
TagConfigValidationMode.Warn,
router.Ref));
admin.Tell(new RestartDriver("C1", "driver-1", "alice", Guid.NewGuid()));
var cmd = router.ExpectMsg<MeshCommand>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
cmd.Topic.ShouldBe(DriverControlTopic.Name);
cmd.Message.ShouldBeOfType<RestartDriver>();
}
[Fact]
public void Reconnect_driver_goes_to_the_router_on_the_driver_control_topic()
{
var router = CreateTestProbe();
var admin = Sys.ActorOf(AdminOperationsActor.Props(
NewInMemoryDbFactory(),
CreateTestProbe().Ref,
Enumerable.Empty<IDriverProbe>(),
TagConfigValidationMode.Warn,
router.Ref));
admin.Tell(new ReconnectDriver("C1", "driver-1", "alice", Guid.NewGuid()));
var cmd = router.ExpectMsg<MeshCommand>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
cmd.Topic.ShouldBe(DriverControlTopic.Name);
cmd.Message.ShouldBeOfType<ReconnectDriver>();
}
[Fact]
public void Alarm_acknowledge_goes_to_the_router_on_the_alarm_commands_topic()
{
var router = CreateTestProbe();
var admin = Sys.ActorOf(AdminOperationsActor.Props(
NewInMemoryDbFactory(),
CreateTestProbe().Ref,
Enumerable.Empty<IDriverProbe>(),
TagConfigValidationMode.Warn,
router.Ref));
admin.Tell(new AcknowledgeAlarmCommand("alarm-1", "alice", "c", CorrelationId.NewId()));
var cmd = router.ExpectMsg<MeshCommand>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
cmd.Topic.ShouldBe(AlarmCommandsTopic.Name);
cmd.Message.ShouldBeOfType<AlarmCommand>().VerdictRequested.ShouldBeTrue();
cmd.VerdictReplyTo.ShouldNotBeNull(); // #950: the per-request verdict collector
}
[Fact]
public void Alarm_shelve_goes_to_the_router_on_the_alarm_commands_topic()
{
var router = CreateTestProbe();
var admin = Sys.ActorOf(AdminOperationsActor.Props(
NewInMemoryDbFactory(),
CreateTestProbe().Ref,
Enumerable.Empty<IDriverProbe>(),
TagConfigValidationMode.Warn,
router.Ref));
admin.Tell(new ShelveAlarmCommand("alarm-1", "alice", ShelveKind.OneShot, null, "c", CorrelationId.NewId()));
var cmd = router.ExpectMsg<MeshCommand>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
cmd.Topic.ShouldBe(AlarmCommandsTopic.Name);
cmd.Message.ShouldBeOfType<AlarmCommand>().VerdictRequested.ShouldBeTrue();
cmd.VerdictReplyTo.ShouldNotBeNull(); // #950: the per-request verdict collector
}
}