test(grpc-data): A2a — condition stream R tests (priority, overflow reset, hub reset, revocation, deploy deltas, total first, slice cut, UNS no RawPath); delivered-snapshot overflow rule

This commit is contained in:
Joseph Doherty
2026-11-24 12:15:02 -05:00
parent 6272c7cfa0
commit 14ab6be37f
3 changed files with 580 additions and 10 deletions
@@ -6,7 +6,9 @@ using ZB.MOM.WW.OtOpcUa.OpcUaServer;
namespace ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
/// <summary>One chunk of a root-scoped condition item, ready to become a <see cref="DataSubscriptionConditions"/> frame.</summary>
internal sealed record ConditionChunk(bool IsSnapshot, int? Total, IReadOnlyList<DataConditionState> Items, bool SnapshotComplete);
/// <param name="EndsSnapshot">The last chunk of snapshot number <paramref name="Snapshot"/> (its delivery is reported back).</param>
internal sealed record ConditionChunk(bool IsSnapshot, int? Total, IReadOnlyList<DataConditionState> Items, bool SnapshotComplete,
int Snapshot = 0, bool EndsSnapshot = false);
/// <summary>The condition set an item may currently see: the catalog it was listed from and the listing (null = revoked).</summary>
internal sealed record ConditionItemScope(object Catalog, IReadOnlyList<ScopedCondition>? Conditions);
@@ -54,7 +56,8 @@ internal sealed class ConditionItemStream
private long[] _cursors = [];
private object? _catalog;
private State _state = State.NeedSnapshot;
private bool _beginSent, _snapshotComplete, _retakeWhenReady, _readSinceSnapshot;
private bool _beginSent, _snapshotComplete, _retakeWhenReady;
private int _deliveredSnapshot;
private int _total;
private long _lastSnapshotAt = long.MinValue;
@@ -93,6 +96,14 @@ internal sealed class ConditionItemStream
/// </summary>
internal bool Overflowed { get; private set; }
/// <summary>The session delivered the last chunk of snapshot <paramref name="snapshot"/> (any thread).</summary>
internal void SnapshotDelivered(int snapshot)
{
int current;
do current = Volatile.Read(ref _deliveredSnapshot);
while (snapshot > current && Interlocked.CompareExchange(ref _deliveredSnapshot, snapshot, current) != current);
}
/// <summary>Produce the next chunk, or null when there is nothing to send now.</summary>
internal ConditionChunk? Next()
{
@@ -135,7 +146,7 @@ internal sealed class ConditionItemStream
_lastSnapshotAt = _clock.GetTimestamp();
_catalog = scope.Catalog;
_snapshotRecords.Clear(); _sliceWatermarks.Clear(); _pendingDeltas.Clear(); _sent.Clear(); _inScope.Clear();
_beginSent = false; _snapshotComplete = false; _retakeWhenReady = false; _readSinceSnapshot = false;
_beginSent = false; _snapshotComplete = false; _retakeWhenReady = false;
var listing = scope.Conditions!;
if (listing.Count > _limit)
{
@@ -181,14 +192,15 @@ internal sealed class ConditionItemStream
{
_beginSent = true;
// The begin chunk carries only the count, so the consumer decides capacity before materialising anything.
if (_snapshotRecords.Count == 0) { _state = State.Live; return new ConditionChunk(true, _total, [], _snapshotComplete); }
if (_snapshotRecords.Count == 0)
{ _state = State.Live; return new ConditionChunk(true, _total, [], _snapshotComplete, Snapshots, EndsSnapshot: true); }
return new ConditionChunk(true, _total, [], false);
}
var batch = new List<DataConditionState>(Math.Min(MaxRecordsPerChunk, _snapshotRecords.Count));
while (batch.Count < MaxRecordsPerChunk && _snapshotRecords.TryDequeue(out var record)) batch.Add(record);
var last = _snapshotRecords.Count == 0;
if (last) _state = State.Live;
return new ConditionChunk(true, null, batch, last && _snapshotComplete);
return new ConditionChunk(true, null, batch, last && _snapshotComplete, Snapshots, EndsSnapshot: last);
}
/// <summary>Reads every partition after its cursor into the pending deltas. False when a reader reset is required.</summary>
@@ -200,8 +212,8 @@ internal sealed class ConditionItemStream
var read = _hub.Read(p, _cursors[p], MaxRecordsPerChunk);
if (read.ResetRequired)
{
// Evicted before the first read after a snapshot: the consumer could not drain one snapshot in time.
if (!_readSinceSnapshot) Overflowed = true;
// Evicted while the last snapshot was still undelivered: the consumer cannot drain one snapshot in time.
if (Volatile.Read(ref _deliveredSnapshot) < Snapshots) Overflowed = true;
return false;
}
foreach (var record in read.Records)
@@ -222,7 +234,6 @@ internal sealed class ConditionItemStream
_pendingDeltas.Enqueue(Map(record, scoped, includeInfo: first && _includeInfo));
}
}
_readSinceSnapshot = true;
return true;
}
@@ -11,6 +11,14 @@ namespace ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
/// Production is pull-based (<see cref="ConditionItemStream"/>): an item produces only while the queue has room, so a slow
/// consumer back-pressures into the hub, whose eviction resets the item to a fresh snapshot.
/// </summary>
internal static class ConditionTestAssertions
{
internal static void ShouldBeTrueForTests(this bool value)
{
if (!value) throw new InvalidOperationException("Test probe could not be queued.");
}
}
internal sealed partial class DataSubscriptionSession
{
private sealed class ConditionItem(string clientItemId, DataItemHandle handle, ConditionItemAuthority authority,
@@ -152,6 +160,7 @@ internal sealed partial class DataSubscriptionSession
chunk.IsSnapshot, chunk.Total, chunk.Items, chunk.SnapshotComplete),
() => Volatile.Read(ref _closed) == 0 && Volatile.Read(ref _closing) == 0
&& _services.CanUseSubscription(_context, _target, serving) && IsConditionItemCurrent(item) && item.Authority.CanPublish(),
commit: () => { if (chunk.EndsSnapshot) item.Stream.SnapshotDelivered(chunk.Snapshot); return true; },
obsolete: () => Volatile.Read(ref _closing) != 0 || !IsConditionItemCurrent(item));
}
@@ -161,8 +170,10 @@ internal sealed partial class DataSubscriptionSession
}
/// <summary>
/// The next delivery by priority: a queued control or ack first, then a condition frame, then the queued value. Null
/// when the stream's queues are completed and drained.
/// The next delivery by priority: a control or ack at the head of the v1 queue first, then a condition frame, then the
/// v1 queue in FIFO order (a v1 control never overtakes a value queued before it: snapshot begin/end ordering depends
/// on that). So conditions overtake queued values, and never a control or ack that is next. Null when the stream's
/// queues are completed and drained.
/// </summary>
private async ValueTask<PendingDelivery?> NextPendingAsync(CancellationToken token)
{
@@ -195,6 +206,26 @@ internal sealed partial class DataSubscriptionSession
}
}
/// <summary>Test seam: the stream has processed its Open.</summary>
internal bool IsOpenForTests => Volatile.Read(ref _handles) is not null;
/// <summary>
/// Test seam (stream priority): queues an empty value frame and a heartbeat control on the v1 queue (control first
/// when <paramref name="controlFirst"/>), then a condition frame.
/// </summary>
internal async Task EnqueuePriorityProbesForTests(DataItemHandle conditionHandle, bool controlFirst)
{
var serving = _serving!;
PendingDelivery Value() => new(sequence => new DataSubscriptionValues(_subscriptionId!, _epoch, sequence, false, []),
() => true, isValue: true);
PendingDelivery Control() => new(sequence => new DataSubscriptionControl(_subscriptionId!, _epoch, sequence,
DataStreamControlKind.Heartbeat, [], serving.CatalogEpoch, serving.AuthorityEpoch, null), () => true);
await EnqueueAsync(controlFirst ? Control() : Value()).ConfigureAwait(false);
await EnqueueAsync(controlFirst ? Value() : Control()).ConfigureAwait(false);
ConditionOutbound.Writer.TryWrite(new PendingDelivery(sequence => new DataSubscriptionConditions(_subscriptionId!, _epoch,
sequence, conditionHandle, false, null, [], false), () => true)).ShouldBeTrueForTests();
}
/// <summary>Called from the worker's teardown: completes and drains the condition queue, releases condition items.</summary>
private void TearDownConditions()
{