using Akka.Actor;
using Akka.Cluster;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Cluster.Redundancy;
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Redundancy;
using ZB.MOM.WW.OtOpcUa.Commons.Types;
using ZB.MOM.WW.OtOpcUa.ControlPlane.Redundancy;
using ZB.MOM.WW.OtOpcUa.ControlPlane.Tests.Harness;
namespace ZB.MOM.WW.OtOpcUa.ControlPlane.Tests;
///
/// Gitea #717 spec §4.2 (ordered snapshots) and §4.3 (Warm bands): the revision the singleton stamps, its order,
/// the subscriber gate, the wire shape of the extended message, and the Warm ServiceLevel truth table.
///
public sealed class WarmRedundancyRevisionTests : ControlPlaneActorTestBase
{
[Fact]
public void The_singleton_stamps_an_increasing_revision_on_every_publish_heartbeats_included()
{
var probe = CreateTestProbe();
Sys.ActorOf(RedundancyStateActor.Props(msg => probe.Ref.Tell(msg), TimeSpan.FromMilliseconds(200)));
var first = probe.ExpectMsg(TimeSpan.FromSeconds(5), cancellationToken: TestContext.Current.CancellationToken);
var second = probe.ExpectMsg(TimeSpan.FromSeconds(5), cancellationToken: TestContext.Current.CancellationToken);
first.Revision.ShouldNotBeNull();
second.Revision.ShouldNotBeNull();
second.Revision!.CompareTo(first.Revision).ShouldBeGreaterThan(0);
second.Revision.Sequence.ShouldBe(first.Revision!.Sequence + 1);
first.Revision.PublisherUpNumber.ShouldBeGreaterThan(0L, "the publisher's Akka up-number");
}
/// Akka.NET keeps Member.UpNumber internal; an upgrade that renames it must fail here, not degrade
/// every revision to up-number 0 silently.
[Fact]
public void Akka_member_up_number_is_readable()
{
AwaitCondition(() => Akka.Cluster.Cluster.Get(Sys).SelfMember.Status == MemberStatus.Up, TimeSpan.FromSeconds(10),
cancellationToken: TestContext.Current.CancellationToken);
RedundancyStateActor.ReadUpNumber(Akka.Cluster.Cluster.Get(Sys).SelfMember).ShouldNotBeNull().ShouldBeGreaterThan(0);
}
[Fact]
public void A_singleton_restarted_in_the_same_process_outranks_its_predecessor()
{
var a = RedundancyStateActor.NextIncarnationTick();
var b = RedundancyStateActor.NextIncarnationTick();
b.ShouldBeGreaterThan(a, "strictly increasing even within one tick");
new SnapshotRevision(1, b, 1).CompareTo(new SnapshotRevision(1, a, 500)).ShouldBeGreaterThan(0);
}
[Fact]
public void Revisions_order_by_up_number_then_incarnation_then_sequence()
{
var baseline = new SnapshotRevision(2, 100, 50);
new SnapshotRevision(3, 1, 1).CompareTo(baseline).ShouldBeGreaterThan(0, "a handover to a younger member outranks");
new SnapshotRevision(1, 999, 999).CompareTo(baseline).ShouldBeLessThan(0, "a departed older member's late publish loses");
new SnapshotRevision(2, 101, 1).CompareTo(baseline).ShouldBeGreaterThan(0);
new SnapshotRevision(2, 100, 51).CompareTo(baseline).ShouldBeGreaterThan(0);
new SnapshotRevision(2, 100, 50).CompareTo(baseline).ShouldBe(0);
}
[Fact]
public void The_gate_accepts_only_newer_revisions_and_never_advances_on_an_unrevisioned_snapshot()
{
var gate = new SnapshotRevisionGate();
gate.Offer(Snap(null)).ShouldBe(SnapshotRejection.Unrevisioned);
gate.LastAccepted.ShouldBeNull();
gate.Offer(Snap(new(2, 1, 5))).ShouldBe(SnapshotRejection.None);
gate.Offer(Snap(new(1, 9, 9))).ShouldBe(SnapshotRejection.OlderRevision, "an old incarnation's late heartbeat");
gate.Offer(Snap(new(2, 1, 5))).ShouldBe(SnapshotRejection.OlderRevision, "a duplicate");
gate.Offer(Snap(new(2, 1, 6))).ShouldBe(SnapshotRejection.None);
gate.LastAccepted.ShouldBe(new SnapshotRevision(2, 1, 6));
}
[Fact]
public void The_extended_message_round_trips_and_a_pre_717_payload_decodes_with_no_revision()
{
var msg = Snap(new SnapshotRevision(3, 42, 7));
var serializer = Sys.Serialization.FindSerializerFor(msg);
var manifest = Akka.Serialization.Serialization.ManifestFor(serializer, msg);
var round = (RedundancyStateChanged)Sys.Serialization.Deserialize(serializer.ToBinary(msg), serializer.Identifier, manifest);
round.Revision.ShouldBe(msg.Revision);
round.Nodes.Single().NodeId.ShouldBe(msg.Nodes.Single().NodeId);
// A pre-#717 publisher's payload: the same message without the field.
var legacy = Snap(null);
var legacyBytes = serializer.ToBinary(legacy);
var text = System.Text.Encoding.UTF8.GetString(legacyBytes);
var stripped = System.Text.Encoding.UTF8.GetBytes(text.Replace(",\"Revision\":null", string.Empty, StringComparison.Ordinal));
var decoded = (RedundancyStateChanged)Sys.Serialization.Deserialize(stripped, serializer.Identifier, manifest);
decoded.Revision.ShouldBeNull();
decoded.Nodes.Count.ShouldBe(1);
}
private static RedundancyStateChanged Snap(SnapshotRevision? revision) =>
new(new[] { new NodeRedundancyState(NodeId.Parse("a:1"), RedundancyRole.Primary, true, true, DateTime.UtcNow) },
CorrelationId.NewId(), revision);
}
/// Gitea #717 spec §4.3: the Warm ServiceLevel bands (pure; no actor system).
public sealed class WarmServiceLevelBandTests
{
public static IEnumerable