281 lines
13 KiB
C#
281 lines
13 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 GrpcDataPartitionTests
|
|
{
|
|
[Fact]
|
|
public void Retiring_producer_fences_all_its_sources_and_existing_snapshots_immediately()
|
|
{
|
|
using var hub = HubTest.Create();
|
|
var first = HubTest.Ticket("driver", "first");
|
|
var second = HubTest.Ticket("driver", "second");
|
|
var independent = HubTest.Ticket("other", "tag");
|
|
var fence = new ProducerFence(1);
|
|
hub.TryBind(first, fence).ShouldBeTrue();
|
|
hub.TryBind(second, fence).ShouldBeTrue();
|
|
HubTest.Publish(hub, first, 1, 10);
|
|
HubTest.Publish(hub, second, 1, 20);
|
|
HubTest.BindAndPublish(hub, independent, 1, 30);
|
|
hub.ProcessPending();
|
|
using var captured = hub.Read(first);
|
|
captured.IsValid.ShouldBeTrue();
|
|
hub.TryCaptureSnapshot([first, second], HubTest.Budget, CancellationToken.None, out var snapshot).ShouldBeTrue();
|
|
using (snapshot!)
|
|
{
|
|
fence.Retire();
|
|
captured.IsValid.ShouldBeFalse();
|
|
hub.IsTicketCurrentNonBlocking(first).ShouldBeFalse();
|
|
hub.TryGetCurrentTicket(second.Key, out _).ShouldBeFalse();
|
|
using var rejected = hub.Read(first);
|
|
rejected.Readiness.ShouldNotBe(DataReadiness.Ready);
|
|
snapshot!.IsReset.ShouldBeTrue();
|
|
hub.TryRemove(first, 2).ShouldBeTrue(); // closed producer still permits deferred tombstone cleanup
|
|
using var unaffected = hub.Read(independent);
|
|
unaffected.Readiness.ShouldBe(DataReadiness.Ready);
|
|
unaffected.Sample!.Value.GetScalar().ShouldBe(30);
|
|
hub.TryBind(first, new ProducerFence(1)).ShouldBeFalse();
|
|
var replacement = new SourceTicket(first.Key, new SourceGeneration(2, 1));
|
|
hub.TryBind(replacement, new ProducerFence(2)).ShouldBeTrue();
|
|
HubTest.Publish(hub, replacement, 1, 40);
|
|
hub.ProcessPending();
|
|
using var rebound = hub.Read(replacement);
|
|
rebound.Sample!.Value.GetScalar().ShouldBe(40);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Retained_read_validity_survives_new_values_but_not_source_invalidation_or_disposal()
|
|
{
|
|
using var hub = HubTest.Create();
|
|
var source = HubTest.Ticket("driver", "tag");
|
|
var other = HubTest.Ticket("driver", "other");
|
|
HubTest.BindAndPublish(hub, source, 1, 1);
|
|
HubTest.BindAndPublish(hub, other, 1, 1);
|
|
hub.ProcessPending();
|
|
using var read = hub.Read(source);
|
|
read.IsValid.ShouldBeTrue();
|
|
HubTest.Publish(hub, source, 2, 2);
|
|
hub.ProcessPending();
|
|
hub.Invalidate(other, "unrelated source");
|
|
read.IsValid.ShouldBeTrue();
|
|
read.Sample!.Value.GetScalar().ShouldBe(1);
|
|
hub.Invalidate(source, "source overload");
|
|
read.IsValid.ShouldBeFalse();
|
|
read.Dispose();
|
|
read.IsValid.ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void Retained_read_validity_tracks_partition_reset_removal_and_repartition()
|
|
{
|
|
using var hub = HubTest.Create(partitions: 1);
|
|
var source = HubTest.Ticket("driver", "tag");
|
|
HubTest.BindAndPublish(hub, source, 1, 1);
|
|
hub.ProcessPending();
|
|
using var beforeReset = hub.Read(source);
|
|
hub.InvalidatePartitionFor(source.Key, "overload");
|
|
beforeReset.IsValid.ShouldBeFalse();
|
|
HubTest.Publish(hub, source, 2, 2);
|
|
hub.ProcessPending();
|
|
using var beforeRemove = hub.Read(source);
|
|
beforeRemove.IsValid.ShouldBeTrue();
|
|
hub.TryRemove(source, 3).ShouldBeTrue();
|
|
beforeRemove.IsValid.ShouldBeFalse();
|
|
var next = new SourceTicket(source.Key, new SourceGeneration(1, 2));
|
|
hub.TryBind(next).ShouldBeTrue();
|
|
HubTest.Publish(hub, next, 1, 3);
|
|
hub.ProcessPending();
|
|
using var beforeRepartition = hub.Read(next);
|
|
beforeRepartition.IsValid.ShouldBeTrue();
|
|
hub.Repartition(2);
|
|
beforeRepartition.IsValid.ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void Quiet_initial_acquisition_is_retained_before_catalog_activation_without_inventing_epochs()
|
|
{
|
|
using var hub = HubTest.Create();
|
|
var source = HubTest.Ticket("driver", "quiet");
|
|
hub.TryBind(source).ShouldBeTrue();
|
|
hub.TryReserveIngress(source, 288, out var reservation).ShouldBeTrue();
|
|
using (reservation!)
|
|
{
|
|
var serving = HubTest.Serving with { CatalogEpoch = 0, AuthorityEpoch = 0 };
|
|
var sample = new DataSample(DataValue.FromScalar(DataValueKind.Int32, 23), 0, null, null, serving, 1);
|
|
reservation!.TryCommit(sample, 1).ShouldBeTrue();
|
|
}
|
|
hub.ProcessPending();
|
|
using var acquisition = hub.Read(source);
|
|
acquisition.Sample!.Value.GetScalar().ShouldBe(23);
|
|
acquisition.Sample.Serving.CatalogEpoch.ShouldBe(0);
|
|
acquisition.Sample.Serving.AuthorityEpoch.ShouldBe(0);
|
|
Should.Throw<ArgumentOutOfRangeException>(() => acquisition.Sample.Validate());
|
|
}
|
|
|
|
[Fact]
|
|
public void Views_share_the_backing_sample_but_distinct_acquisitions_and_replicas_do_not()
|
|
{
|
|
using var hub = HubTest.Create();
|
|
var source = HubTest.Ticket("driver-a", "tag");
|
|
var otherAcquisition = HubTest.Ticket("driver-b", "tag");
|
|
var otherReplica = HubTest.Ticket("driver-a", "tag", "node-b");
|
|
HubTest.BindAndPublish(hub, source, 1, 42);
|
|
hub.TryBind(otherAcquisition).ShouldBeTrue();
|
|
hub.TryBind(otherReplica).ShouldBeTrue();
|
|
hub.ProcessPending();
|
|
|
|
// Both view descriptors resolve to the same explicit catalog source; a view is never a cache key.
|
|
var raw = new DataView(HubTest.Address, DataRealm.Raw, null, 0, "raw/tag");
|
|
var uns = new DataView(HubTest.Address, DataRealm.Uns, null, 0, "uns/tag");
|
|
raw.Realm.ShouldNotBe(uns.Realm);
|
|
using var rawRead = hub.Read(source);
|
|
using var unsRead = hub.Read(source);
|
|
using var acquisitionRead = hub.Read(otherAcquisition);
|
|
using var replicaRead = hub.Read(otherReplica);
|
|
rawRead.Sample!.Value.GetScalar().ShouldBe(42);
|
|
unsRead.Sample!.Value.GetScalar().ShouldBe(42);
|
|
acquisitionRead.Readiness.ShouldBe(DataReadiness.WaitingForInitialData);
|
|
replicaRead.Readiness.ShouldBe(DataReadiness.WaitingForInitialData);
|
|
}
|
|
|
|
[Fact]
|
|
public void Removal_fences_queued_values_and_rebind_rejects_old_ticket_without_harming_new_sample()
|
|
{
|
|
using var hub = HubTest.Create();
|
|
var first = HubTest.Ticket("driver", "tag");
|
|
HubTest.BindAndPublish(hub, first, 1, 10);
|
|
hub.ProcessPending();
|
|
HubTest.Publish(hub, first, 2, 20); // remains queued while the removal is admitted
|
|
hub.TryRemove(first, 3).ShouldBeTrue();
|
|
hub.ProcessPending();
|
|
using (var removed = hub.Read(first)) removed.Readiness.ShouldNotBe(DataReadiness.Ready);
|
|
hub.TryGetCurrentTicket(first.Key, out _).ShouldBeFalse();
|
|
|
|
var next = new SourceTicket(first.Key, new SourceGeneration(1, 2));
|
|
hub.TryBind(next).ShouldBeTrue();
|
|
hub.TryGetCurrentTicket(next.Key, out var resolved).ShouldBeTrue();
|
|
resolved.ShouldBe(next);
|
|
HubTest.Publish(hub, next, 4, 30);
|
|
hub.ProcessPending();
|
|
hub.Invalidate(first, "late callback");
|
|
hub.TryReserveIngress(first, 288, out _).ShouldBeFalse();
|
|
using var current = hub.Read(next);
|
|
current.Readiness.ShouldBe(DataReadiness.Ready);
|
|
current.Sample!.Value.GetScalar().ShouldBe(30);
|
|
}
|
|
|
|
[Fact]
|
|
public void Status_is_ordered_and_retains_payload_without_manufacturing_a_good_initial_sample()
|
|
{
|
|
using var hub = HubTest.Create();
|
|
var source = HubTest.Ticket("driver", "tag");
|
|
hub.TryBind(source).ShouldBeTrue();
|
|
hub.TryPublishStatus(source, 1, 0x80000000u, null, null).ShouldBeTrue();
|
|
hub.ProcessPending();
|
|
using (var initial = hub.Read(source))
|
|
{
|
|
initial.Readiness.ShouldNotBe(DataReadiness.Ready);
|
|
initial.Sample.ShouldBeNull();
|
|
}
|
|
HubTest.Publish(hub, source, 2, 12);
|
|
hub.ProcessPending();
|
|
var timestamp = new DateTimeOffset(2026, 1, 1, 0, 0, 0, TimeSpan.Zero);
|
|
hub.TryPublishStatus(source, 3, 0x80310001u, timestamp, null).ShouldBeTrue();
|
|
hub.ProcessPending();
|
|
using var status = hub.Read(source);
|
|
status.Sample!.Value.GetScalar().ShouldBe(12);
|
|
status.Sample.StatusCode.ShouldBe(0x80310001u);
|
|
status.Sample.SourceTimestampUtc.ShouldBe(timestamp);
|
|
status.Sample.ServerTimestampUtc.ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void Synthetic_status_preserves_original_acquisition_timestamps()
|
|
{
|
|
using var hub = HubTest.Create();
|
|
var source = HubTest.Ticket("driver", "tag");
|
|
var sourceTime = new DateTimeOffset(2026, 2, 1, 2, 3, 4, TimeSpan.Zero);
|
|
var serverTime = sourceTime.AddMilliseconds(37);
|
|
hub.TryBind(source).ShouldBeTrue();
|
|
hub.TryReserveIngress(source, 288, out var reservation).ShouldBeTrue();
|
|
using (reservation!)
|
|
{
|
|
var sample = new DataSample(DataValue.FromScalar(DataValueKind.Int32, 12), 0,
|
|
sourceTime, serverTime, HubTest.Serving, 1);
|
|
reservation!.TryCommit(sample, 1).ShouldBeTrue();
|
|
}
|
|
hub.ProcessPending();
|
|
hub.TryPublishStatus(source, 2, 0x80310000u, null, null).ShouldBeTrue();
|
|
hub.ProcessPending();
|
|
using var read = hub.Read(source);
|
|
read.Sample!.Value.GetScalar().ShouldBe(12);
|
|
read.Sample.StatusCode.ShouldBe(0x80310000u);
|
|
read.Sample.SourceTimestampUtc.ShouldBe(sourceTime);
|
|
read.Sample.ServerTimestampUtc.ShouldBe(serverTime);
|
|
}
|
|
|
|
[Fact]
|
|
public void Repartition_resets_existing_snapshots_and_does_not_make_old_values_fresh()
|
|
{
|
|
using var hub = HubTest.Create();
|
|
var source = HubTest.Ticket("driver", "tag");
|
|
HubTest.BindAndPublish(hub, source, 1, 42);
|
|
hub.ProcessPending();
|
|
hub.TryCaptureSnapshot([source], HubTest.Budget, CancellationToken.None, out var snapshot).ShouldBeTrue();
|
|
using (snapshot!)
|
|
{
|
|
hub.Repartition(3);
|
|
snapshot!.IsReset.ShouldBeTrue();
|
|
using var read = hub.Read(source);
|
|
read.Readiness.ShouldNotBe(DataReadiness.Ready);
|
|
}
|
|
}
|
|
}
|
|
|
|
internal static class HubTest
|
|
{
|
|
internal static readonly DataAddress Address = new("cluster", "urn:raw", "string", "tag");
|
|
internal static readonly DataBudget Budget = new(TimeSpan.FromMinutes(1), 100, 100_000);
|
|
internal static readonly ServingIdentity Serving = new("node-a", "serving-1", "Primary", 1, 1);
|
|
|
|
internal static PartitionedSampleHub Create(int partitions = 2, long bytes = 10_000,
|
|
int ingress = 10, int dirty = 10, TimeProvider? clock = null, int maxSources = 100) =>
|
|
new(new DataResourceLimits
|
|
{
|
|
PartitionCount = partitions, MaxRetainedBytes = bytes, MaxTrackedSources = maxSources,
|
|
MaxIngressItemsPerPartition = ingress, MaxDirtySourcesPerPartition = dirty,
|
|
MaxControlItemsPerPartition = 10, MaxSnapshotItems = 100,
|
|
MaxSnapshotBytes = 10_000, MaxSnapshotCatchUpItems = 1,
|
|
MaxSnapshotLifetime = TimeSpan.FromMinutes(1)
|
|
}, clock ?? new ManualTimeProvider(), autoDrain: false);
|
|
|
|
internal static SourceTicket Ticket(string driver, string tag, string node = "node-a") =>
|
|
new(new BackingSourceKey("cluster", "driver", $"{driver.Length}:{driver}{tag.Length}:{tag}", node, "serving-1"),
|
|
new SourceGeneration(1, 1));
|
|
|
|
internal static void BindAndPublish(PartitionedSampleHub hub, SourceTicket ticket, long version, int value)
|
|
{
|
|
hub.TryBind(ticket).ShouldBeTrue();
|
|
Publish(hub, ticket, version, value);
|
|
}
|
|
|
|
internal static void Publish(PartitionedSampleHub hub, SourceTicket ticket, long version, int value)
|
|
{
|
|
hub.TryReserveIngress(ticket, 288, out var reservation).ShouldBeTrue();
|
|
using (reservation!)
|
|
{
|
|
var serving = Serving with { NodeId = ticket.Key.ServingNodeId, Incarnation = ticket.Key.ServingIncarnation };
|
|
var sample = new DataSample(DataValue.FromScalar(DataValueKind.Int32, value), 0, null, null, serving, version);
|
|
if (!reservation!.TryCommit(sample, version)) { sample.Dispose(); throw new InvalidOperationException("Commit rejected"); }
|
|
}
|
|
}
|
|
}
|