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

144 lines
6.0 KiB
C#

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.Tests.Harness;
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
[Trait("Feature", "GrpcData")]
[Trait("Category", "Unit")]
[Trait("Phase", "P04")]
public sealed class GrpcDataSnapshotTests
{
[Fact]
public void Snapshot_freezes_samples_and_its_watermarks_cover_every_participating_partition()
{
using var hub = HubTest.Create(partitions: 2);
var first = HubTest.Ticket("driver", "a");
var second = Enumerable.Range(0, 1000).Select(i => HubTest.Ticket("driver", $"other-{i}"))
.First(t => hub.GetPartition(t.Key) != hub.GetPartition(first.Key));
HubTest.BindAndPublish(hub, first, 1, 10);
HubTest.BindAndPublish(hub, second, 1, 20);
hub.ProcessPending();
hub.TryCaptureSnapshot([first, second], HubTest.Budget, CancellationToken.None, out var snapshot).ShouldBeTrue();
using (snapshot!)
{
snapshot!.Watermarks.Count.ShouldBe(2);
snapshot.Items[first].Sample!.Value.GetScalar().ShouldBe(10);
snapshot.Items[second].Sample!.Value.GetScalar().ShouldBe(20);
HubTest.Publish(hub, first, 2, 11);
hub.ProcessPending();
snapshot.Items[first].Sample!.Value.GetScalar().ShouldBe(10);
snapshot.TryReadDelta(first, out var delta).ShouldBeTrue();
using (delta!) delta!.Sample!.Value.GetScalar().ShouldBe(11);
}
}
[Fact]
public void Racing_registration_and_update_cannot_lose_the_newer_value()
{
using var hub = HubTest.Create();
var source = HubTest.Ticket("driver", "race");
HubTest.BindAndPublish(hub, source, 1, 1);
hub.ProcessPending();
SnapshotLease? snapshot = null;
var captured = false;
var accepted = false;
Parallel.Invoke(
() => captured = hub.TryCaptureSnapshot([source], HubTest.Budget, CancellationToken.None, out snapshot),
() =>
{
if (hub.TryReserveIngress(source, 288, out var reservation))
{
using (reservation!)
{
var sample = new DataSample(DataValue.FromScalar(DataValueKind.Int32, 2), 0, null, null, HubTest.Serving, 2);
accepted = reservation!.TryCommit(sample, 2);
if (!accepted) sample.Dispose();
}
}
hub.ProcessPending();
});
if (!accepted)
{
if (captured) { snapshot!.IsReset.ShouldBeTrue(); snapshot.Dispose(); }
using var invalidated = hub.Read(source);
invalidated.Readiness.ShouldNotBe(DataReadiness.Ready);
return;
}
if (!captured) return; // a reset during registration is an explicit retry boundary
using (snapshot!)
{
var capturedValue = (int)snapshot!.Items[source].Sample!.Value.GetScalar();
if (capturedValue == 1)
{
snapshot.TryReadDelta(source, out var delta).ShouldBeTrue();
using (delta!) delta!.Sample!.Value.GetScalar().ShouldBe(2);
}
else capturedValue.ShouldBe(2);
}
}
[Fact]
public void Cancellation_and_deadline_actively_release_all_snapshot_resources()
{
var clock = new ManualTimeProvider();
using var hub = HubTest.Create(clock: clock);
var source = HubTest.Ticket("driver", "tag");
HubTest.BindAndPublish(hub, source, 1, 9);
hub.ProcessPending();
var baseline = hub.Resources.RetainedBytes;
using var cancellation = new CancellationTokenSource();
hub.TryCaptureSnapshot([source], HubTest.Budget, cancellation.Token, out var canceled).ShouldBeTrue();
hub.Resources.RetainedBytes.ShouldBeGreaterThan(baseline);
cancellation.Cancel();
canceled!.IsReset.ShouldBeTrue();
hub.Resources.RetainedBytes.ShouldBe(baseline);
canceled.Dispose(); // cancellation and explicit disposal are idempotent
hub.Resources.RetainedBytes.ShouldBe(baseline);
var shortBudget = HubTest.Budget with { Timeout = TimeSpan.FromSeconds(2) };
hub.TryCaptureSnapshot([source], shortBudget, CancellationToken.None, out var expired).ShouldBeTrue();
clock.Advance(TimeSpan.FromSeconds(2));
expired!.IsReset.ShouldBeTrue();
hub.Resources.RetainedBytes.ShouldBe(baseline);
expired.Dispose();
}
[Fact]
public void Snapshot_admission_is_global_and_rejects_before_retaining_more_payload()
{
using var hub = HubTest.Create(bytes: 1700);
var source = HubTest.Ticket("driver", "tag");
HubTest.BindAndPublish(hub, source, 1, 5);
hub.ProcessPending();
var before = hub.Resources.RetainedBytes;
hub.TryCaptureSnapshot([source], HubTest.Budget, CancellationToken.None, out var rejected).ShouldBeFalse();
rejected.ShouldBeNull();
hub.Resources.RetainedBytes.ShouldBe(before);
using var read = hub.Read(source);
read.Status.ShouldBe(DataItemStatus.ResourceExhausted);
read.Sample.ShouldBeNull();
}
[Fact]
public void Catch_up_overflow_resets_snapshot_instead_of_silently_losing_deltas()
{
using var hub = HubTest.Create(partitions: 1);
var first = HubTest.Ticket("driver", "first");
var second = HubTest.Ticket("driver", "second");
HubTest.BindAndPublish(hub, first, 1, 1);
HubTest.BindAndPublish(hub, second, 1, 1);
hub.ProcessPending();
hub.TryCaptureSnapshot([first, second], HubTest.Budget, CancellationToken.None, out var snapshot).ShouldBeTrue();
using (snapshot!)
{
HubTest.Publish(hub, first, 2, 2);
HubTest.Publish(hub, second, 2, 2);
hub.ProcessPending();
snapshot!.IsReset.ShouldBeTrue();
}
}
}