using System.Text.Json;
using Akka.Actor;
using Microsoft.EntityFrameworkCore;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Commons.Interfaces;
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Deploy;
using ZB.MOM.WW.OtOpcUa.Commons.Types;
using ZB.MOM.WW.OtOpcUa.Configuration;
using ZB.MOM.WW.OtOpcUa.Runtime.DeploymentCache;
using ZB.MOM.WW.OtOpcUa.Runtime.Drivers;
using ZB.MOM.WW.OtOpcUa.Runtime.OpcUa;
using ZB.MOM.WW.OtOpcUa.Runtime.Tests.Harness;
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.Drivers;
///
/// Gitea #1107 — the node side of the coordinator's dispatch re-send. A re-send names the nodes that have not
/// acked; it must never make a node apply the same deployment twice (under #601 a second apply of a degraded
/// revision is a full re-apply), and a late or duplicate copy must be harmless.
///
[Collection("opcua-publish-meters")]
public sealed class DriverHostActorRedispatchTests : RuntimeActorTestBase
{
private static readonly NodeId TestNode = NodeId.Parse("driver-test");
private static readonly RevisionHash RevA = RevisionHash.Parse(new string('a', 64));
private static readonly TimeSpan Quiet = TimeSpan.FromMilliseconds(400);
[Fact]
public void A_resend_of_a_deployment_already_acked_degraded_re_sends_the_ack_instead_of_re_applying()
{
var (db, deploymentId) = SeedOneTagDeployment();
var coordinator = CreateTestProbe();
var publish = CreateTestProbe();
var actor = Sys.ActorOf(DriverHostActor.Props(
db, TestNode, coordinator.Ref, localRoles: new HashSet { "driver" },
opcUaPublishActor: publish.Ref));
var dispatch = new DispatchDeployment(deploymentId, RevA, CorrelationId.NewId());
actor.Tell(dispatch);
var rebuild = publish.FishForMessage(_ => true, PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
publish.Reply(new OpcUaPublishActor.RebuildCompleted(rebuild.Correlation, rebuild.DeploymentId, true, "failedNodes=1"));
var first = coordinator.ExpectMsg(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
first.Degraded.ShouldBeTrue();
// The coordinator never saw that ack and re-sends to this node (node id case differs, as SQL/DNS may).
actor.Tell(dispatch with { TargetNodes = ["DRIVER-TEST"] });
var again = coordinator.ExpectMsg(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
again.ShouldBe(first); // the SAME answer, not a new apply's
publish.ExpectNoMsg(Quiet, TestContext.Current.CancellationToken);
// Positive control: an ORIGINAL dispatch of a degraded revision still re-applies (#601) — so the silence above
// is the re-send being recognised, not a host that has stopped applying.
actor.Tell(new DispatchDeployment(deploymentId, RevA, CorrelationId.NewId()));
publish.FishForMessage(_ => true, PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
}
[Fact]
public void A_resend_addressed_to_another_node_is_ignored()
{
var (db, deploymentId) = SeedOneTagDeployment();
var coordinator = CreateTestProbe();
var publish = CreateTestProbe();
var actor = Sys.ActorOf(DriverHostActor.Props(
db, TestNode, coordinator.Ref, localRoles: new HashSet { "driver" },
opcUaPublishActor: publish.Ref));
actor.Tell(new DispatchDeployment(deploymentId, RevA, CorrelationId.NewId(), TargetNodes: ["driver-other"]));
publish.ExpectNoMsg(Quiet, TestContext.Current.CancellationToken);
coordinator.ExpectNoMsg(TimeSpan.Zero, TestContext.Current.CancellationToken);
// Positive control: the same re-send naming THIS node (never acked here) is a first apply.
actor.Tell(new DispatchDeployment(deploymentId, RevA, CorrelationId.NewId(), TargetNodes: ["driver-other", "driver-test"]));
var rebuild = publish.FishForMessage(_ => true, PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
publish.Reply(new OpcUaPublishActor.RebuildCompleted(rebuild.Correlation, rebuild.DeploymentId, false, null));
coordinator.ExpectMsg(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken)
.Outcome.ShouldBe(ApplyAckOutcome.Applied);
}
[Fact]
public void A_duplicate_while_the_ack_waits_on_the_address_space_outcome_does_not_apply_twice()
{
var (db, deploymentId) = SeedOneTagDeployment();
var coordinator = CreateTestProbe();
var publish = CreateTestProbe();
var actor = Sys.ActorOf(DriverHostActor.Props(
db, TestNode, coordinator.Ref, localRoles: new HashSet { "driver" },
opcUaPublishActor: publish.Ref));
var dispatch = new DispatchDeployment(deploymentId, RevA, CorrelationId.NewId());
actor.Tell(dispatch);
var rebuild = publish.FishForMessage(_ => true, PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
// The ack is held on the outcome. Neither a re-send nor a plain duplicate may start a second apply.
actor.Tell(dispatch with { TargetNodes = [TestNode.Value] });
actor.Tell(dispatch);
publish.ExpectNoMsg(Quiet, TestContext.Current.CancellationToken);
coordinator.ExpectNoMsg(TimeSpan.Zero, TestContext.Current.CancellationToken);
// Exactly one ack, when the one apply's outcome lands.
actor.Tell(new OpcUaPublishActor.RebuildCompleted(rebuild.Correlation, rebuild.DeploymentId, false, null), publish.Ref);
coordinator.ExpectMsg(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken)
.Outcome.ShouldBe(ApplyAckOutcome.Applied);
coordinator.ExpectNoMsg(Quiet, TestContext.Current.CancellationToken);
publish.ExpectNoMsg(TimeSpan.Zero, TestContext.Current.CancellationToken);
}
[Fact]
public void A_resend_arriving_mid_apply_does_not_start_a_second_apply()
{
var db = NewInMemoryDbFactory();
var fetcher = new GatedFetcher(JsonSerializer.SerializeToUtf8Bytes(new { DriverInstances = Array.Empty