using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Core.Abstractions; using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections; using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections; using ZB.MOM.WW.OtOpcUa.Runtime.Tests.Drivers; using ZB.MOM.WW.OtOpcUa.Runtime.Tests.Harness; namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections; [Trait("Feature", "GrpcData")] [Trait("Phase", "P11Renewal")] public sealed class GrpcDataMetadataRenewalPublisherTests : RuntimeActorTestBase { [Theory] [InlineData(true, false)] [InlineData(false, false)] [InlineData(true, true)] public void Pre_admission_expired_predecessor_never_retires_a_proven_successor(bool predecessor, bool throws) { var clock = new ManualTimeProvider(); var actor = CreateTestProbe(); using var publisher = new ActorProjectedValuePublisher(() => actor.Ref, _ => true, 2, 100_000, clock); var id = ProjectedDataIds.Identifier("mapping", new DataAddress("cluster", "urn:raw", "string", "node")); var route = new ProjectedValueRoute("grpc-data:link", ProjectedDataIds.DependencyReference(DataRealm.Raw, id), DataRealm.Raw, id, 1, new ServingIdentity("node", "boot", "Primary", 1, 1, 1), Guid.NewGuid()); var proof = new TestProof(clock) { SampleValid = false, Obsolete = predecessor, ThrowCapability = throws }; publisher.TryBind(route).ShouldBeTrue(); publisher.TryPublish([new(route, new DataValueSnapshot(7, 0, null, null), new(TimeSpan.Zero, TimeSpan.Zero), clock.GetTimestamp(), proof, true, TimeSpan.FromSeconds(1), 1)]).ShouldBeFalse(); publisher.IsCurrent(route).ShouldBe(predecessor && !throws); proof.Invalidations.ShouldBe(predecessor && !throws ? 0 : 1); } [Theory] [InlineData(true)] [InlineData(false)] public void Only_an_explicitly_retired_predecessor_may_be_dropped_without_retiring_the_live_route(bool predecessor) { var clock = new ManualTimeProvider(); var actor = CreateTestProbe(); using var publisher = new ActorProjectedValuePublisher(() => actor.Ref, _ => true, 2, 100_000, clock); var id = ProjectedDataIds.Identifier("mapping", new DataAddress("cluster", "urn:raw", "string", "node")); var route = new ProjectedValueRoute("grpc-data:link", ProjectedDataIds.DependencyReference(DataRealm.Raw, id), DataRealm.Raw, id, 1, new ServingIdentity("node", "boot", "Primary", 1, 1, 1), Guid.NewGuid()); var proof = new TestProof(clock); publisher.TryBind(route).ShouldBeTrue(); publisher.TryPublish([new(route, new DataValueSnapshot(7, 0, null, null), new(TimeSpan.Zero, TimeSpan.Zero), clock.GetTimestamp(), proof, true, TimeSpan.FromSeconds(1), 1)]).ShouldBeTrue(); proof.Obsolete = predecessor; if (predecessor) proof.SampleValid = false; clock.Advance(TimeSpan.FromSeconds(2)); publisher.PendingBytes.ShouldBe(0); publisher.IsCurrent(route).ShouldBe(predecessor); proof.Invalidations.ShouldBe(predecessor ? 0 : 1); } private sealed class TestProof(TimeProvider clock) : IProjectedSamplePublicationProof, IObsoleteProjectedSample { internal bool SampleValid = true, Obsolete, ThrowCapability; internal int Invalidations; public TimeProvider Clock => clock; public bool IsValid => SampleValid; public bool IsSourceValid => true; public bool CanDropExpiredPredecessor => ThrowCapability ? throw new InvalidOperationException("authority callback failed") : Obsolete; public void Invalidate() => Invalidations++; } }