using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections; using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections; namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections; [Trait("Feature", "GrpcData"), Trait("Category", "Unit"), Trait("Phase", "P06")] public sealed class GrpcDataDemandSharingTests { private static readonly DataSubscriptionSettings Slow = new(TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(2), DataChangeTrigger.StatusValue, null); private static readonly DataSubscriptionSettings Fast = Slow with { SamplingInterval = TimeSpan.FromMilliseconds(100) }; [Fact] public void Ref_counts_are_shared_only_within_the_full_upstream_authority_key() { var registry = new UpstreamDemandRegistry(maxKeys: 8, maxInterests: 8); var key = Key(); registry.TryAdd(key, Slow, out var first).ShouldBeTrue(); registry.TryAdd(key, Fast, out var second).ShouldBeTrue(); var otherPrincipal = key with { PrincipalId = "other" }; registry.TryAdd(otherPrincipal, Slow, out var isolated).ShouldBeTrue(); registry.GetState(key)!.InterestCount.ShouldBe(2); registry.GetState(otherPrincipal)!.InterestCount.ShouldBe(1); registry.GetState(key)!.Desired.SamplingInterval.ShouldBe(Fast.SamplingInterval); second!.Dispose(); registry.GetState(key)!.InterestCount.ShouldBe(1); first!.Dispose(); registry.GetState(key).ShouldBeNull(); registry.GetState(otherPrincipal)!.InterestCount.ShouldBe(1); isolated!.Dispose(); registry.GetState(otherPrincipal).ShouldBeNull(); } [Fact] public void Principal_case_variants_share_the_same_demand_and_limits() { var registry = new UpstreamDemandRegistry(maxKeys: 1, maxInterests: 2); var key = Key(); registry.TryAdd(key, Slow, out var first).ShouldBeTrue(); registry.TryAdd(key with { PrincipalId = "PRINCIPAL" }, Fast, out var second).ShouldBeTrue(); registry.GetState(key)!.InterestCount.ShouldBe(2); registry.GetState(key with { PrincipalId = "PRINCIPAL" })!.Desired.SamplingInterval.ShouldBe(Fast.SamplingInterval); first!.Dispose(); second!.Dispose(); } [Fact] public void Adding_and_removing_interests_does_not_copy_the_whole_existing_registration_set() { var registry = new UpstreamDemandRegistry(maxKeys: 1, maxInterests: 2048); var key = Key(); var leases = new List(1100); for (var i = 0; i < 1024; i++) { registry.TryAdd(key, Slow, out var lease).ShouldBeTrue(); leases.Add(lease!); } var beforeAdd = GC.GetAllocatedBytesForCurrentThread(); for (var i = 0; i < 64; i++) { if (!registry.TryAdd(key, Slow, out var lease)) throw new InvalidOperationException(); leases.Add(lease!); } var addBytes = GC.GetAllocatedBytesForCurrentThread() - beforeAdd; var beforeRemove = GC.GetAllocatedBytesForCurrentThread(); for (var i = 1024; i < 1088; i++) leases[i].Dispose(); var removeBytes = GC.GetAllocatedBytesForCurrentThread() - beforeRemove; addBytes.ShouldBeLessThan(128 * 1024, "an add must not copy all 1,000 existing interests"); removeBytes.ShouldBeLessThan(128 * 1024, "a remove must not copy all 1,000 existing interests"); foreach (var lease in leases) lease.Dispose(); } [Fact] public void Removing_interests_recomputes_each_extreme_only_after_its_last_holder_leaves() { var registry = new UpstreamDemandRegistry(maxKeys: 1, maxInterests: 4); var key = Key(); var first = new DataSubscriptionSettings(TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(100), DataChangeTrigger.Status, 5); var second = new DataSubscriptionSettings(TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(200), DataChangeTrigger.StatusValueTimestamp, 10); var third = new DataSubscriptionSettings(TimeSpan.FromMilliseconds(30), TimeSpan.FromMilliseconds(100), DataChangeTrigger.StatusValueTimestamp, null); registry.TryAdd(key, first, out var firstLease).ShouldBeTrue(); registry.TryAdd(key, second, out var secondLease).ShouldBeTrue(); registry.TryAdd(key, third, out var thirdLease).ShouldBeTrue(); var combined = registry.GetState(key)!; combined.Desired.ShouldBe(new DataSubscriptionSettings(TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(100), DataChangeTrigger.StatusValueTimestamp, null)); secondLease!.Dispose(); var afterSecond = registry.GetState(key)!; afterSecond.Desired.ShouldBe(combined.Desired); afterSecond.DesiredRevision.ShouldBe(combined.DesiredRevision); firstLease!.Dispose(); var afterFirst = registry.GetState(key)!; afterFirst.Desired.ShouldBe(third with { Deadband = null }); afterFirst.DesiredRevision.ShouldBeGreaterThan(afterSecond.DesiredRevision); thirdLease!.Dispose(); registry.GetState(key).ShouldBeNull(); } [Fact] public void Removing_the_last_strong_trigger_reduces_the_shared_trigger() { var registry = new UpstreamDemandRegistry(maxKeys: 1, maxInterests: 3); var key = Key(); var low = Slow with { Trigger = DataChangeTrigger.Status }; var high = Slow with { Trigger = DataChangeTrigger.StatusValueTimestamp }; registry.TryAdd(key, low, out var lowLease).ShouldBeTrue(); registry.TryAdd(key, Slow, out var middleLease).ShouldBeTrue(); registry.TryAdd(key, high, out var highLease).ShouldBeTrue(); var strongRevision = registry.GetState(key)!.DesiredRevision; highLease!.Dispose(); registry.GetState(key)!.Desired.Trigger.ShouldBe(DataChangeTrigger.StatusValue); registry.GetState(key)!.DesiredRevision.ShouldBeGreaterThan(strongRevision); middleLease!.Dispose(); registry.GetState(key)!.Desired.Trigger.ShouldBe(DataChangeTrigger.Status); lowLease!.Dispose(); } [Fact] public void A_late_physical_ack_cannot_claim_a_newer_stronger_demand_is_applied() { var registry = new UpstreamDemandRegistry(maxKeys: 4, maxInterests: 4); var key = Key(); registry.TryAdd(key, Slow, out var slow).ShouldBeTrue(); var oldRevision = registry.GetState(key)!.DesiredRevision; registry.TryAdd(key, Fast, out var fast).ShouldBeTrue(); var current = registry.GetState(key)!; current.DesiredRevision.ShouldBeGreaterThan(oldRevision); registry.TryAcknowledge(key, oldRevision, Slow, 1).ShouldBeFalse(); registry.GetState(key)!.AppliedRevision.ShouldNotBe(current.DesiredRevision); registry.TryAcknowledge(key, current.DesiredRevision, Fast, 2).ShouldBeTrue(); registry.GetState(key)!.AppliedRevision.ShouldBe(current.DesiredRevision); fast!.Dispose(); slow!.Dispose(); } private static DemandSharingKey Key() => new(HubTest.Ticket("driver", "tag").Key, new DataTarget(DataTargetMode.Node, "cluster", "node-a"), DataConsistency.Primary, "principal", "export", 1, 1); }