Merge #1102: a commit re-considers the source when the client filter suppressed a newer version against the baseline that commit replaced — client no longer holds a superseded value

# Conflicts:
#	docs/DataConnections.md
This commit is contained in:
Joseph Doherty
2026-12-04 20:28:31 -05:00
4 changed files with 144 additions and 1 deletions
@@ -0,0 +1,120 @@
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;
/// <summary>
/// #1102: a client filter's baseline is the last COMMITTED value, but a tick may consider a newer source version while an
/// earlier value is still in flight (dequeued and past its eligibility check, not yet committed). A newer value equal to the
/// old baseline was dropped against it, then the in-flight value committed, and the client held that superseded value until
/// the source changed again. Each test parks the in-flight value at the reader's pre-commit seam and drives the tick that
/// considers the newer version to completion before releasing it: no wall-clock waits decide the interleaving.
/// </summary>
[Trait("Feature", "GrpcData"), Trait("Category", "Unit")]
public sealed class GrpcData1102FilterBaselineTests
{
private static readonly TimeSpan Wait = TimeSpan.FromSeconds(10);
[Theory(Timeout = 30000)]
[InlineData(false)]
[InlineData(true)]
public async Task A_newer_value_equal_to_the_old_baseline_is_delivered_after_an_in_flight_value_commits(bool deadband)
{
var frames = await RunInFlightRaceAsync(newerValue: 1, deadband ? 0.5 : null);
// v2 (value 2) was in flight while v3 (value 1, equal to the committed baseline) was considered. The client must end on
// the source's current value 1, not hold 2.
frames.ShouldBe([2, 1]);
}
[Fact(Timeout = 30000)]
public async Task A_newer_value_queued_behind_an_in_flight_value_is_delivered_once()
{
// Control: the newer value differs from the baseline, so it is queued while v2 is in flight. Re-evaluating at commit
// must not deliver it twice.
var frames = await RunInFlightRaceAsync(newerValue: 3, null);
frames.ShouldBe([2, 3]);
}
/// <summary>
/// Snapshot v1=1 (the baseline), then v2=2 goes in flight; at its pre-commit seam v3=<paramref name="newerValue"/> is
/// published and a whole tick runs. Returns the live values the reader saw up to a probe acknowledgement queued after
/// the commit (the commit's own re-evaluation, if any, runs on the worker before that probe).
/// </summary>
private static async Task<int[]> RunInFlightRaceAsync(int newerValue, double? deadband)
{
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)))
{
// Every sample comes through HubTest.Publish (same status, no timestamps) so only the value differs.
HubTest.Publish(fixture.Hub, fixture.Ticket, 1, 1); fixture.Hub.ProcessPending();
await using var connection = fixture.Connection();
await using var subscription = await connection.SubscribeAsync(ct);
var session = subscription.ShouldBeOfType<DataSubscriptionSession>();
var settings = new DataSubscriptionSettings(TimeSpan.Zero, TimeSpan.Zero, DataChangeTrigger.StatusValue, deadband);
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>();
(await reader.MoveNextAsync().AsTask().WaitAsync(Wait, ct)).ShouldBeTrue();
reader.Current.ShouldBeOfType<DataSubscriptionControl>().Kind.ShouldBe(DataStreamControlKind.SnapshotBegin);
(await reader.MoveNextAsync().AsTask().WaitAsync(Wait, ct)).ShouldBeTrue();
using (var snapshot = reader.Current.ShouldBeOfType<DataSubscriptionValues>())
ValueOf(snapshot).ShouldBe(1);
(await reader.MoveNextAsync().AsTask().WaitAsync(Wait, ct)).ShouldBeTrue();
reader.Current.ShouldBeOfType<DataSubscriptionControl>().Kind.ShouldBe(DataStreamControlKind.SnapshotEnd);
// A tick that STARTS after v3 is in the hub has considered v3 by the time it ends.
var armed = 0; var started = 0;
var considered = new ManualResetEventSlim();
session.BeforeTickForTests = () => { if (Volatile.Read(ref armed) == 1) Volatile.Write(ref started, 1); };
session.AfterTickForTests = () => { if (Volatile.Read(ref started) == 1) considered.Set(); };
var parked = 0;
session.BeforeDirectCommitForTests = () =>
{
if (Interlocked.Exchange(ref parked, 1) != 0) return;
// v2 is past IsEligible and not yet committed: the committed baseline is still v1.
HubTest.Publish(fixture.Hub, fixture.Ticket, 3, newerValue); fixture.Hub.ProcessPending();
Volatile.Write(ref armed, 1);
fixture.Clock.Advance(TimeSpan.FromMilliseconds(200));
if (!considered.Wait(Wait)) throw new TimeoutException("The tick considering v3 never completed.");
};
HubTest.Publish(fixture.Hub, fixture.Ticket, 2, 2); fixture.Hub.ProcessPending();
fixture.Clock.Advance(TimeSpan.FromMilliseconds(200));
var values = new List<int>();
(await Task.Run(async () => await reader.MoveNextAsync(), ct).WaitAsync(Wait, ct)).ShouldBeTrue();
using (var live = reader.Current.ShouldBeOfType<DataSubscriptionValues>()) values.Add(ValueOf(live));
Volatile.Read(ref parked).ShouldBe(1);
considered.IsSet.ShouldBeTrue();
await subscription.SendAsync(new AddSubscriptionItems("s", 1, "probe", TimeSpan.FromSeconds(10), []), ct);
while (true)
{
(await reader.MoveNextAsync().AsTask().WaitAsync(Wait, ct)).ShouldBeTrue();
if (reader.Current is DataSubscriptionAck { CommandId: "probe" }) break;
if (reader.Current is DataSubscriptionValues more) using (more) values.Add(ValueOf(more));
}
// Nothing is left behind the probe either: a later tick must not deliver a duplicate.
await subscription.SendAsync(new AddSubscriptionItems("s", 1, "probe-2", TimeSpan.FromSeconds(10), []), ct);
fixture.Clock.Advance(TimeSpan.FromMilliseconds(200));
while (true)
{
(await reader.MoveNextAsync().AsTask().WaitAsync(Wait, ct)).ShouldBeTrue();
if (reader.Current is DataSubscriptionAck { CommandId: "probe-2" }) break;
if (reader.Current is DataSubscriptionValues more) using (more) values.Add(ValueOf(more));
}
return [.. values];
}
}
private static int ValueOf(DataSubscriptionValues values) =>
(int)values.Items.ShouldHaveSingleItem().Sample.Value.GetScalar()!;
}