test(dataconn): #1115 review — session-level pins that a snapshot outlived by history rollover still ends on the latest value

Drives Open, Modify-replacement and freshness-challenge snapshots on the origin session while the 1-entry change
history rolls ~20-40 times past the capture watermark (own and same-partition sibling traffic), and requires the
client to end on the source's latest value with no reset. Fails before 0dfe8371b (reset) and fails if the #994
history-gap catch-up drops own changes (silent loss), so it pins both halves of the safety property.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Joseph Doherty
2026-12-05 04:10:52 -05:00
parent 0dfe8371b7
commit 2b0a0bc029
@@ -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<DataSubscriptionAck>(); // 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<DataSubscriptionAck>().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<DataSubscriptionAck>().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<DataSubscriptionAck>().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");
}
}
/// <summary>A bound sibling source on the fixture ticket's partition, so its traffic rolls the same change history.</summary>
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));
}
}
/// <summary>
/// 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".
/// </summary>
private static async Task<List<int>> ReadToProbeAsync(IDataSubscription subscription, IAsyncEnumerator<DataSubscriptionFrame> reader,
LocalDataFixture fixture, CancellationToken ct)
{
var values = new List<int>();
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;
}
}