Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataWorkloadAccountingTests.cs
T
2026-11-23 09:55:04 -05:00

103 lines
6.0 KiB
C#

using Akka.Actor;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
using ZB.MOM.WW.OtOpcUa.Runtime.Drivers;
using ZB.MOM.WW.OtOpcUa.Runtime.Tests.Harness;
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
[Trait("Feature", "GrpcData"), Trait("Category", "Unit"), Trait("Phase", "P11")]
public sealed class GrpcDataWorkloadAccountingTests : RuntimeActorTestBase
{
[Fact]
public void Logical_sharing_counts_preserve_principal_and_pinned_target_boundaries()
{
var registry = new UpstreamDemandRegistry(8, 8);
var key = new DemandSharingKey(HubTest.Ticket("driver", "tag").Key,
new(DataTargetMode.Node, "cluster", "node-a"), DataConsistency.Primary, "principal", "export", 1, 1);
var settings = new DataSubscriptionSettings(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1), DataChangeTrigger.StatusValue, null);
registry.TryAdd(key, settings, out var first).ShouldBeTrue();
registry.TryAdd(key, settings, out var overlapping).ShouldBeTrue();
registry.TryAdd(key with { PrincipalId = "other" }, settings, out var principal).ShouldBeTrue();
registry.TryAdd(key with { Target = key.Target with { ExpectedNodeId = "node-b" } }, settings, out var pinned).ShouldBeTrue();
registry.CaptureAccounting().ShouldBe((3, 4));
first!.Dispose(); overlapping!.Dispose();
registry.CaptureAccounting().ShouldBe((2, 2));
principal!.Dispose(); pinned!.Dispose();
registry.CaptureAccounting().ShouldBe((0, 0));
}
[Fact]
public void Snapshot_disposal_releases_snapshot_charge_while_steady_interest_remains_owned()
{
using var hub = HubTest.Create(partitions: 1);
var ticket = HubTest.Ticket("driver", "tag");
HubTest.BindAndPublish(hub, ticket, 1, 42);
hub.ProcessPending();
var before = hub.CaptureAccounting();
before.TrackedSources.ShouldBe(1);
before.SnapshotItems.ShouldBe(0);
before.InterestItems.ShouldBe(0);
hub.TryCaptureInterestSnapshot([ticket], HubTest.Budget, CancellationToken.None, out var snapshot, out var interest).ShouldBeTrue();
try
{
var during = hub.CaptureAccounting();
during.SnapshotItems.ShouldBe(1); during.InterestItems.ShouldBe(1);
during.RetainedBytes.ShouldBeGreaterThan(before.RetainedBytes);
snapshot!.Dispose();
var steady = hub.CaptureAccounting();
steady.SnapshotItems.ShouldBe(0); steady.InterestItems.ShouldBe(1);
steady.RetainedBytes.ShouldBeLessThan(during.RetainedBytes);
}
finally { snapshot!.Dispose(); interest!.Dispose(); }
var after = hub.CaptureAccounting();
after.SnapshotItems.ShouldBe(0); after.InterestItems.ShouldBe(0);
after.RetainedBytes.ShouldBe(before.RetainedBytes);
}
[Fact]
public async Task Applied_physical_count_waits_for_ack_and_remains_until_cleanup_ack()
{
var probe = CreateTestProbe();
var host = Sys.ActorOf(Props.Create(() => new OwnerProxy(probe.Ref)));
using var source = new DriverHostSubscriptionDemandSource(host, new UpstreamDemandRegistry(8, 8), 8, 8,
PresenceBudget, new SubscriptionAcquisitionCapacity(8));
var view = new DataView(new("cluster", "urn:raw", "string", "tag"), DataRealm.Raw, null, 0, "subject");
var authorized = new SubscriptionAuthorizedView(view, SourceKey.ForDriver("cluster", "driver", "tag", "node", "inc"),
new("node", "inc", "Primary", 1, 1),
new(view, "tag", "Variable", DataValueKind.Int32, -1, [], false, DataOperations.Subscribe, 1, "tag", null, false),
"principal", "export", new(DataTargetMode.Node, "cluster", "node"), DataConsistency.Primary, () => true);
var settings = new DataSubscriptionSettings(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1), DataChangeTrigger.StatusValue, null);
var token = TestContext.Current.CancellationToken;
var pending = source.AcquireAsync(authorized, settings, token).AsTask();
var apply = probe.ExpectMsg<DriverHostActor.ApplyAcquisitionDemand>(PresenceBudget, cancellationToken: token);
var admitted = source.CaptureAccounting();
admitted.LogicalInterests.ShouldBe(1); admitted.DesiredPhysicalReferences.ShouldBe(1);
admitted.AppliedPhysicalReferences.ShouldBe(0);
probe.Reply(new DriverInstanceActor.AcquisitionDemandApplied(apply.DesiredRevision, 7, settings.SamplingInterval, true));
var lease = (await pending.WaitAsync(PresenceBudget, token)).Lease.ShouldNotBeNull();
source.CaptureAccounting().AppliedPhysicalReferences.ShouldBe(1);
lease.Dispose();
var removal = probe.ExpectMsg<DriverHostActor.ApplyAcquisitionDemand>(PresenceBudget, cancellationToken: token);
var retiring = source.CaptureAccounting();
retiring.LogicalInterests.ShouldBe(0); retiring.DesiredPhysicalReferences.ShouldBe(0);
retiring.AppliedPhysicalReferences.ShouldBe(1); retiring.RetiringDrivers.ShouldBe(1);
probe.Reply(new DriverInstanceActor.AcquisitionDemandApplied(removal.DesiredRevision, 7, TimeSpan.Zero, true));
await source.WaitForDriverIdleForTest("driver").WaitAsync(PresenceBudget, token);
source.CaptureAccounting().AppliedPhysicalReferences.ShouldBe(0);
}
private sealed class OwnerProxy : ReceiveActor
{
public OwnerProxy(IActorRef target)
{
Receive<DriverHostActor.RegisterAcquisitionOwner>(_ => Sender.Tell(new DriverHostActor.AcquisitionOwnerRegistered(true, 1)));
Receive<DriverHostActor.OpenAcquisitionDemand>(message => Sender.Tell(new DriverHostActor.AcquisitionDemandOpened(true, message.RequestedKeyGeneration)));
Receive<DriverHostActor.AbortOpenAcquisitionDemand>(_ => Sender.Tell(new DriverHostActor.AcquisitionOpenAborted(true)));
Receive<DriverHostActor.ApplyAcquisitionDemand>(message => target.Forward(message));
}
}
}