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

383 lines
16 KiB
C#

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", "P04")]
public sealed class GrpcDataIngressOverflowTests
{
[Theory]
[InlineData(true, true)]
[InlineData(true, false)]
[InlineData(false, true)]
[InlineData(false, false)]
public void Repartition_retains_old_source_quota_until_the_last_owner_releases_it(bool holdRead, bool limitSourceCount)
{
// Both keys are large enough that source metadata, rather than just a scalar lease,
// determines admission under the byte-limited cases.
using var hub = HubTest.Create(partitions: 1, bytes: limitSourceCount ? 100_000 : 10_000,
maxSources: limitSourceCount ? 1 : 10);
var old = HubTest.Ticket("driver", new string('a', 2000));
var replacement = HubTest.Ticket("driver", new string('b', 2000));
HubTest.BindAndPublish(hub, old, 1, 1);
hub.ProcessPending();
using var heldRead = holdRead ? hub.Read(old) : null;
IIngressReservation? heldReservation = null;
if (holdRead) heldRead!.IsValid.ShouldBeTrue();
else hub.TryReserveIngress(old, 288, out heldReservation).ShouldBeTrue();
using (heldReservation)
{
hub.Repartition(2);
if (holdRead) heldRead!.IsValid.ShouldBeFalse();
hub.Resources.RetainedBytes.ShouldBeGreaterThan(4_000);
hub.TryBind(replacement).ShouldBeFalse();
if (holdRead) heldRead!.Dispose();
else
{
using var stale = new DataSample(DataValue.FromScalar(DataValueKind.Int32, 2), 0, null, null, HubTest.Serving, 2);
heldReservation!.TryCommit(stale, 2).ShouldBeFalse();
}
hub.ProcessPending();
hub.TryBind(replacement).ShouldBeTrue();
}
}
[Fact]
public void Repartition_retires_the_old_ticket_even_after_its_partition_is_disposed()
{
using var hub = HubTest.Create(partitions: 1, maxSources: 10);
var old = HubTest.Ticket("driver", new string('a', 1000));
HubTest.BindAndPublish(hub, old, 1, 1);
hub.ProcessPending();
hub.Repartition(2);
hub.TryBind(old).ShouldBeFalse();
hub.TryReserveIngress(old, 288, out _).ShouldBeFalse();
var fresh = new SourceTicket(old.Key, old.Generation);
hub.TryBind(fresh).ShouldBeTrue();
}
[Fact]
public void Removed_source_churn_reclaims_bounded_registry_without_resurrecting_old_tickets()
{
using var hub = HubTest.Create(partitions: 1, bytes: 100_000, maxSources: 2);
var retired = new List<SourceTicket>();
for (var i = 0; i < 6; i++)
{
var source = HubTest.Ticket("driver", $"churn-{i}");
hub.TryBind(source).ShouldBeTrue();
HubTest.Publish(hub, source, 1, i);
hub.ProcessPending();
hub.TryRemove(source, 2).ShouldBeTrue();
hub.ProcessPending(); // worker has an opportunity to reclaim safe tombstones
retired.Add(source);
}
foreach (var old in retired)
hub.TryReserveIngress(old, 288, out _).ShouldBeFalse();
hub.TryBind(HubTest.Ticket("driver", "live")).ShouldBeTrue();
}
[Fact]
public void Reclaimed_binding_cannot_be_repaired_or_target_a_fresh_binding_with_the_same_generation()
{
using var hub = HubTest.Create(partitions: 1, maxSources: 1);
var old = HubTest.Ticket("driver", "tag");
HubTest.BindAndPublish(hub, old, 1, 1);
hub.ProcessPending();
using var oldRead = hub.Read(old);
hub.TryRemove(old, 2).ShouldBeTrue();
oldRead.IsValid.ShouldBeFalse();
oldRead.Dispose();
hub.ProcessPending();
hub.TryBind(old).ShouldBeFalse(); // even while no replacement source exists
var replacement = new SourceTicket(old.Key, old.Generation);
hub.TryBind(replacement).ShouldBeTrue();
hub.TryGetCurrentTicket(old.Key, out var resolved).ShouldBeTrue();
resolved.ShouldBe(replacement);
resolved.ShouldNotBe(old);
HubTest.Publish(hub, replacement, 1, 2);
hub.ProcessPending();
hub.TryBind(old).ShouldBeFalse();
hub.TryReserveIngress(old, 288, out _).ShouldBeFalse();
hub.Invalidate(old, "late callback");
using var current = hub.Read(replacement);
current.Readiness.ShouldBe(DataReadiness.Ready);
current.Sample!.Value.GetScalar().ShouldBe(2);
}
[Fact]
public void Reclamation_waits_for_snapshot_references_to_release()
{
using var hub = HubTest.Create(partitions: 1, maxSources: 1);
var old = HubTest.Ticket("driver", "tag");
HubTest.BindAndPublish(hub, old, 1, 1);
hub.ProcessPending();
hub.TryCaptureSnapshot([old], HubTest.Budget, CancellationToken.None, out var snapshot).ShouldBeTrue();
hub.TryRemove(old, 2).ShouldBeTrue();
hub.ProcessPending();
var other = HubTest.Ticket("driver", "other");
hub.TryBind(other).ShouldBeFalse();
snapshot!.IsReset.ShouldBeTrue();
snapshot.Dispose();
hub.ProcessPending();
hub.TryBind(other).ShouldBeTrue();
}
[Fact]
public void Reclamation_waits_for_disposal_of_outstanding_ingress_and_read_leases()
{
using var hub = HubTest.Create(partitions: 1, maxSources: 1);
var old = HubTest.Ticket("driver", "tag");
HubTest.BindAndPublish(hub, old, 1, 1);
hub.ProcessPending();
var read = hub.Read(old);
hub.TryReserveIngress(old, 288, out var reservation).ShouldBeTrue();
hub.TryRemove(old, 2).ShouldBeTrue();
hub.ProcessPending();
var other = HubTest.Ticket("driver", "other");
hub.TryBind(other).ShouldBeFalse();
read.Dispose();
reservation!.Dispose();
hub.ProcessPending();
hub.TryBind(other).ShouldBeTrue();
}
[Fact]
public void Registry_overflow_invalidates_in_constant_producer_work_and_defers_cache_cleanup()
{
using var hub = HubTest.Create(partitions: 1, bytes: 100_000, maxSources: 32);
var sources = Enumerable.Range(0, 32).Select(i => HubTest.Ticket("driver", $"tag-{i}")).ToArray();
foreach (var source in sources)
{
HubTest.BindAndPublish(hub, source, 1, 1);
hub.ProcessPending();
}
var chargedBeforeOverflow = hub.Resources.RetainedBytes;
hub.TryBind(HubTest.Ticket("driver", "overflow")).ShouldBeFalse();
// No source cache was scanned or released by the producer's failed bind.
hub.Resources.RetainedBytes.ShouldBe(chargedBeforeOverflow);
hub.ProcessPending();
hub.Resources.RetainedBytes.ShouldBeLessThan(chargedBeforeOverflow);
foreach (var source in sources)
{
using var read = hub.Read(source);
read.Readiness.ShouldNotBe(DataReadiness.Ready);
}
}
[Fact]
public void Deferred_partition_cleanup_preserves_a_fresh_post_overflow_sample()
{
using var hub = HubTest.Create(partitions: 1);
var source = HubTest.Ticket("driver", "tag");
HubTest.BindAndPublish(hub, source, 1, 1);
hub.ProcessPending();
hub.InvalidatePartitionFor(source.Key, "bounded recovery overflow");
HubTest.Publish(hub, source, 2, 2);
hub.ProcessPending();
using var read = hub.Read(source);
read.Readiness.ShouldBe(DataReadiness.Ready);
read.Sample!.Value.GetScalar().ShouldBe(2);
}
[Fact]
public async Task Admission_and_retirement_do_not_wait_for_a_busy_partition()
{
using var hub = HubTest.Create(partitions: 1);
var source = HubTest.Ticket("driver", "tag");
HubTest.BindAndPublish(hub, source, 1, 1);
hub.ProcessPending();
using var entered = new ManualResetEventSlim();
using var release = new ManualResetEventSlim();
var cancellationToken = TestContext.Current.CancellationToken;
var owner = Task.Run(() => hub.WithPartitionLockForTest(source.Key, () => { entered.Set(); release.Wait(cancellationToken); }), cancellationToken);
try
{
entered.Wait(TimeSpan.FromSeconds(5), cancellationToken).ShouldBeTrue();
var producer = Task.Run(() =>
{
hub.TryReserveIngress(source, 288, out _).ShouldBeFalse();
hub.TryRemove(source, 2).ShouldBeFalse();
hub.IsTicketCurrentNonBlocking(source).ShouldBeFalse();
}, cancellationToken);
await producer.WaitAsync(TimeSpan.FromSeconds(5), cancellationToken);
}
finally
{
release.Set();
await owner.WaitAsync(TimeSpan.FromSeconds(5), cancellationToken);
}
using var read = hub.Read(source);
read.Readiness.ShouldNotBe(DataReadiness.Ready);
}
[Fact]
public void In_flight_copy_cannot_restore_readiness_after_an_overflow_fence()
{
using var hub = HubTest.Create();
var source = HubTest.Ticket("driver", "tag");
HubTest.BindAndPublish(hub, source, 1, 1);
hub.ProcessPending();
hub.TryReserveIngress(source, 288, out var oldReservation).ShouldBeTrue();
hub.Invalidate(source, "overflow");
HubTest.Publish(hub, source, 3, 3);
hub.ProcessPending();
using (oldReservation!)
using (var old = new DataSample(DataValue.FromScalar(DataValueKind.Int32, 2), 0, null, null, HubTest.Serving, 2))
oldReservation!.TryCommit(old, 2).ShouldBeFalse();
using var current = hub.Read(source);
current.Readiness.ShouldBe(DataReadiness.Ready);
current.Sample!.Value.GetScalar().ShouldBe(3);
}
[Fact]
public void Understated_reservation_is_rejected_without_transferring_sample_ownership()
{
using var hub = HubTest.Create();
var source = HubTest.Ticket("driver", "tag");
hub.TryBind(source).ShouldBeTrue();
hub.TryReserveIngress(source, 287, out var reservation).ShouldBeTrue();
using (reservation!)
using (var sample = new DataSample(DataValue.FromScalar(DataValueKind.Int32, 7), 0, null, null, HubTest.Serving, 1))
{
reservation!.TryCommit(sample, 1).ShouldBeFalse();
sample.Value.GetScalar().ShouldBe(7); // failed commit leaves ownership with caller
}
using var read = hub.Read(source);
read.Readiness.ShouldNotBe(DataReadiness.Ready);
}
[Fact]
public void Source_registry_reuses_capacity_after_tombstone_cleanup()
{
using var hub = HubTest.Create(partitions: 1, maxSources: 1);
var first = HubTest.Ticket("driver", "first");
var second = HubTest.Ticket("driver", "second");
HubTest.BindAndPublish(hub, first, 1, 1);
hub.ProcessPending();
hub.TryRemove(first, 2).ShouldBeTrue();
hub.ProcessPending();
hub.TryBind(second).ShouldBeTrue();
hub.TryGetCurrentTicket(first.Key, out _).ShouldBeFalse();
}
[Fact]
public void Admission_rejects_before_copy_and_synchronously_invalidates_previously_good_cache()
{
using var hub = HubTest.Create(bytes: 1700);
var source = HubTest.Ticket("driver", "tag");
HubTest.BindAndPublish(hub, source, 1, 7);
hub.ProcessPending();
var copied = false;
if (hub.TryReserveIngress(source, 288, out var reservation))
{
using (reservation!) copied = true; // production copies only after successful admission
}
copied.ShouldBeFalse();
using var read = hub.Read(source);
read.Readiness.ShouldNotBe(DataReadiness.Ready);
read.Status.ShouldNotBe(DataItemStatus.Good);
// Recreating a consumer cannot turn the old cache into a fresh Good snapshot.
hub.TryCaptureSnapshot([source], HubTest.Budget, CancellationToken.None, out var snapshot).ShouldBeTrue();
using (snapshot!) snapshot!.Items[source].Readiness.ShouldNotBe(DataReadiness.Ready);
}
[Fact]
public void Abandoned_reservation_releases_global_quota_exactly_once()
{
using var hub = HubTest.Create(bytes: 1700);
var source = HubTest.Ticket("driver", "tag");
hub.TryBind(source).ShouldBeTrue();
var before = hub.Resources.RetainedBytes;
hub.TryReserveIngress(source, 288, out var reservation).ShouldBeTrue();
hub.Resources.RetainedBytes.ShouldBeGreaterThan(before);
reservation!.Dispose();
reservation.Dispose();
hub.Resources.RetainedBytes.ShouldBe(before);
hub.TryReserveIngress(source, 288, out var replacement).ShouldBeTrue();
replacement!.Dispose();
}
[Fact]
public void Dirty_table_exhaustion_invalidates_partition_and_resets_existing_consumers()
{
using var hub = HubTest.Create(partitions: 1, ingress: 1, dirty: 1);
var a = HubTest.Ticket("driver", "a");
var b = HubTest.Ticket("driver", "b");
var c = HubTest.Ticket("driver", "c");
foreach (var source in new[] { a, b, c })
{
HubTest.BindAndPublish(hub, source, 1, 1);
hub.ProcessPending();
}
hub.TryCaptureSnapshot([a, b, c], HubTest.Budget, CancellationToken.None, out var snapshot).ShouldBeTrue();
using (snapshot!)
{
HubTest.Publish(hub, a, 2, 2); // fills ingress
HubTest.Publish(hub, b, 2, 2); // fills the one-entry coalescing table
var admitted = hub.TryReserveIngress(c, 288, out var reservation);
if (admitted)
{
using (reservation!)
{
var sample = new DataSample(DataValue.FromScalar(DataValueKind.Int32, 2), 0, null, null, HubTest.Serving, 2);
var committed = reservation!.TryCommit(sample, 2);
if (!committed) sample.Dispose();
committed.ShouldBeFalse();
}
}
snapshot!.IsReset.ShouldBeTrue();
foreach (var source in new[] { a, b, c })
{
using var read = hub.Read(source);
read.Readiness.ShouldNotBe(DataReadiness.Ready);
}
hub.ProcessPending();
// Only a fresh retained sample after invalidation may restore readiness.
using var stale = hub.Read(c);
stale.Readiness.ShouldNotBe(DataReadiness.Ready);
}
}
[Fact]
public void Status_control_overtaking_queued_value_never_resurrects_good_quality()
{
using var hub = HubTest.Create(partitions: 1);
var source = HubTest.Ticket("driver", "tag");
HubTest.BindAndPublish(hub, source, 1, 1);
hub.ProcessPending();
HubTest.Publish(hub, source, 2, 2);
hub.TryPublishStatus(source, 3, 0x80310000u, null, null).ShouldBeTrue();
hub.ProcessPending();
using var read = hub.Read(source);
if (read.Sample is not null) read.Sample.StatusCode.ShouldBe(0x80310000u);
else read.Readiness.ShouldNotBe(DataReadiness.Ready);
}
[Fact]
public void New_dirty_generation_is_seen_after_a_previous_delta_was_consumed()
{
using var hub = HubTest.Create();
var source = HubTest.Ticket("driver", "tag");
HubTest.BindAndPublish(hub, source, 1, 1);
hub.ProcessPending();
hub.TryCaptureSnapshot([source], HubTest.Budget, CancellationToken.None, out var snapshot).ShouldBeTrue();
using (snapshot!)
{
HubTest.Publish(hub, source, 2, 2);
hub.ProcessPending();
snapshot!.TryReadDelta(source, out var first).ShouldBeTrue();
using (first!) first!.Sample!.Value.GetScalar().ShouldBe(2);
HubTest.Publish(hub, source, 3, 3);
hub.ProcessPending();
snapshot.TryReadDelta(source, out var second).ShouldBeTrue();
using (second!) second!.Sample!.Value.GetScalar().ShouldBe(3);
}
}
}