diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcData1115SnapshotHistoryTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcData1115SnapshotHistoryTests.cs index 2f5ecdc56..dba2bbbf7 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcData1115SnapshotHistoryTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcData1115SnapshotHistoryTests.cs @@ -1,5 +1,6 @@ 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; @@ -78,4 +79,166 @@ public sealed class GrpcData1115SnapshotHistoryTests snapshot!.IsReset.ShouldBeTrue("an own source's status barrier is still a reset"); } } + + // ---- #1115 review: end-to-end through the origin session. Removing the reach check is only safe if the snapshot's interest + // lease (cursor = the capture watermark, history-gap catch-up from each source's LastChange, #994) yields every change made + // after capture. These drive the history (MaxSnapshotCatchUpItems = 1) far past the capture watermark with own AND sibling + // traffic while the snapshot is in flight, then require the client to END on the source's latest value without a reset. + + private static readonly TimeSpan Wait = TimeSpan.FromSeconds(10); + + [Theory(Timeout = 30000)] + [InlineData(false)] // only the snapshot's own source changes + [InlineData(true)] // own changes interleaved with a same-partition sibling's traffic + public async Task Session_open_snapshot_outlived_by_history_rollover_ends_on_the_latest_value(bool siblingTraffic) + { + var ct = TestContext.Current.CancellationToken; + var demand = new DeferredSubscriptionDemand(); demand.Accept(); + var capacity = new SubscriptionTransportCapacity(4, 2, 1_000_000); + var limits = new SubscriptionSessionLimits(16, 16, 100_000, TimeSpan.FromMinutes(1)); + LocalDataFixture? fixture = null; + using (fixture = new LocalDataFixture(targetSubscriptions: (context, target) => + new LocalSubscriptionService(fixture!.Services, context, target, demand, limits, capacity))) + { + var sibling = SameDriverPartitionSibling(fixture); + fixture.Hub.TryBind(sibling).ShouldBeTrue(); + HubTest.Publish(fixture.Hub, fixture.Ticket, 1, 1); HubTest.Publish(fixture.Hub, sibling, 1, -1); fixture.Hub.ProcessPending(); + await using var connection = fixture.Connection(); + await using var subscription = await connection.SubscribeAsync(ct); + var settings = new DataSubscriptionSettings(TimeSpan.Zero, TimeSpan.Zero, DataChangeTrigger.StatusValue, null); + await subscription.SendAsync(new OpenSubscription("s", 1, "open", TimeSpan.FromSeconds(10), + [new("one", fixture.Raw, settings)], false, TimeSpan.FromSeconds(30), LocalDataFixture.Budget), ct); + await using var reader = subscription.ReadAllAsync(ct).GetAsyncEnumerator(ct); + (await reader.MoveNextAsync().AsTask().WaitAsync(Wait, ct)).ShouldBeTrue(); + reader.Current.ShouldBeOfType(); // the snapshot + interest are captured before the ack is queued + + // In flight (SnapshotBegin/value/End not yet dequeued): roll the 1-entry history ~40 times past the capture. + for (var version = 2; version <= 21; version++) + { + HubTest.Publish(fixture.Hub, fixture.Ticket, version, version * 10); + if (siblingTraffic) HubTest.Publish(fixture.Hub, sibling, version, -version); + fixture.Hub.ProcessPending(); + } + + var values = await ReadToProbeAsync(subscription, reader, fixture, ct); + values[0].ShouldBe(1, "the snapshot value is the captured one"); + values[^1].ShouldBe(210, "a change made after capture must reach the client once the snapshot completes"); + } + } + + [Fact(Timeout = 30000)] + public async Task Session_modify_replacement_snapshot_outlived_by_history_rollover_ends_on_the_latest_value() + { + var ct = TestContext.Current.CancellationToken; + var demand = new DeferredSubscriptionDemand(); demand.Accept(); + var capacity = new SubscriptionTransportCapacity(4, 2, 1_000_000); + var limits = new SubscriptionSessionLimits(16, 16, 100_000, TimeSpan.FromMinutes(1)); + LocalDataFixture? fixture = null; + using (fixture = new LocalDataFixture(targetSubscriptions: (context, target) => + new LocalSubscriptionService(fixture!.Services, context, target, demand, limits, capacity))) + { + var sibling = SameDriverPartitionSibling(fixture); + fixture.Hub.TryBind(sibling).ShouldBeTrue(); + HubTest.Publish(fixture.Hub, fixture.Ticket, 1, 1); HubTest.Publish(fixture.Hub, sibling, 1, -1); fixture.Hub.ProcessPending(); + await using var connection = fixture.Connection(); + await using var subscription = await connection.SubscribeAsync(ct); + var settings = new DataSubscriptionSettings(TimeSpan.Zero, TimeSpan.Zero, DataChangeTrigger.StatusValue, null); + await subscription.SendAsync(new OpenSubscription("s", 1, "open", TimeSpan.FromSeconds(10), + [new("one", fixture.Raw, settings)], false, TimeSpan.FromSeconds(30), LocalDataFixture.Budget), ct); + await using var reader = subscription.ReadAllAsync(ct).GetAsyncEnumerator(ct); + (await ReadToProbeAsync(subscription, reader, fixture, ct)).ShouldBe([1]); + + // A trigger change replaces the item: its snapshot + interest are captured when the modify is staged. + await subscription.SendAsync(new ModifySubscriptionItems("s", 1, "modify", TimeSpan.FromSeconds(10), + [new("one", fixture.Raw, settings with { Trigger = DataChangeTrigger.StatusValueTimestamp })]), ct); + (await reader.MoveNextAsync().AsTask().WaitAsync(Wait, ct)).ShouldBeTrue(); + reader.Current.ShouldBeOfType().CommandId.ShouldBe("modify"); + for (var version = 2; version <= 21; version++) + { + HubTest.Publish(fixture.Hub, fixture.Ticket, version, version * 10); + HubTest.Publish(fixture.Hub, sibling, version, -version); + fixture.Hub.ProcessPending(); + } + var values = await ReadToProbeAsync(subscription, reader, fixture, ct); + values[^1].ShouldBe(210, "the replacement must end on the latest value, not its staged snapshot"); + } + } + + [Fact(Timeout = 30000)] + public async Task Session_freshness_challenge_snapshot_outlived_by_history_rollover_ends_on_the_latest_value() + { + // A challenge's temporary interest is never read, and a challenged item ignores dirty marks until its challenge ends; + // FinishChallengeSnapshot's re-mark is what re-considers it (so this holds even without the history-gap catch-up). + var ct = TestContext.Current.CancellationToken; + var demand = new DeferredSubscriptionDemand(); demand.Accept(); + var capacity = new SubscriptionTransportCapacity(4, 2, 1_000_000); + var limits = new SubscriptionSessionLimits(16, 16, 100_000, TimeSpan.FromMinutes(1)); + LocalDataFixture? fixture = null; + using (fixture = new LocalDataFixture(targetSubscriptions: (context, target) => + new LocalSubscriptionService(fixture!.Services, context, target, demand, limits, capacity))) + { + var sibling = SameDriverPartitionSibling(fixture); + fixture.Hub.TryBind(sibling).ShouldBeTrue(); + HubTest.Publish(fixture.Hub, fixture.Ticket, 1, 1); HubTest.Publish(fixture.Hub, sibling, 1, -1); fixture.Hub.ProcessPending(); + await using var connection = fixture.Connection(); + await using var subscription = await connection.SubscribeAsync(ct); + var settings = new DataSubscriptionSettings(TimeSpan.Zero, TimeSpan.Zero, DataChangeTrigger.StatusValue, null); + await subscription.SendAsync(new OpenSubscription("s", 1, "open", TimeSpan.FromSeconds(10), + [new("one", fixture.Raw, settings)], false, TimeSpan.FromSeconds(30), LocalDataFixture.Budget), ct); + await using var reader = subscription.ReadAllAsync(ct).GetAsyncEnumerator(ct); + (await reader.MoveNextAsync().AsTask().WaitAsync(Wait, ct)).ShouldBeTrue(); + var handle = reader.Current.ShouldBeOfType().Items.ShouldHaveSingleItem().Handle!; + (await ReadToProbeAsync(subscription, reader, fixture, ct)).ShouldBe([1]); + + await subscription.SendAsync(new ChallengeSubscriptionFreshness("s", 1, "freshness-1", TimeSpan.FromSeconds(10), [handle]), ct); + (await reader.MoveNextAsync().AsTask().WaitAsync(Wait, ct)).ShouldBeTrue(); + reader.Current.ShouldBeOfType().CommandId.ShouldBe("freshness-1"); + for (var version = 2; version <= 21; version++) + { + HubTest.Publish(fixture.Hub, fixture.Ticket, version, version * 10); + HubTest.Publish(fixture.Hub, sibling, version, -version); + fixture.Hub.ProcessPending(); + } + var values = await ReadToProbeAsync(subscription, reader, fixture, ct); + values[^1].ShouldBe(210, "the challenged item must end on the latest value, not the challenge's captured one"); + } + } + + /// A bound sibling source on the fixture ticket's partition, so its traffic rolls the same change history. + private static SourceTicket SameDriverPartitionSibling(LocalDataFixture fixture) + { + var partition = fixture.Hub.GetPartition(fixture.Ticket.Key); + for (var i = 0; ; i++) + { + var key = SourceKey.ForDriver("cluster", "driver", "Plant/Driver/Device/Busy" + i, fixture.Ticket.Key.ServingNodeId, + fixture.Ticket.Key.ServingIncarnation); + if (fixture.Hub.GetPartition(key) == partition) return new SourceTicket(key, new SourceGeneration(1, 1)); + } + } + + /// + /// Drains frames (driving ticks on the manual clock) up to a probe acknowledgement queued after them, returning the + /// item values seen in order. A Reset control or a closed stream fails the test: the property is "no reset AND no loss". + /// + private static async Task> ReadToProbeAsync(IDataSubscription subscription, IAsyncEnumerator reader, + LocalDataFixture fixture, CancellationToken ct) + { + var values = new List(); + for (var round = 0; round < 3; round++) + { + var probe = "probe-" + Guid.NewGuid().ToString("N"); + fixture.Clock.Advance(TimeSpan.FromMilliseconds(200)); + await subscription.SendAsync(new AddSubscriptionItems("s", 1, probe, TimeSpan.FromSeconds(10), []), ct); + while (true) + { + (await reader.MoveNextAsync().AsTask().WaitAsync(Wait, ct)).ShouldBeTrue("the stream closed"); + if (reader.Current is DataSubscriptionAck ack && ack.CommandId == probe) break; + if (reader.Current is DataSubscriptionControl { Kind: DataStreamControlKind.Reset } reset) + throw new Xunit.Sdk.XunitException("stream reset: " + reset.Diagnostic); + if (reader.Current is DataSubscriptionValues frame) + using (frame) foreach (var item in frame.Items) values.Add((int)item.Sample.Value.GetScalar()!); + } + } + return values; + } }