Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataProjectedReconnectLifetimeTests.cs
T
Joseph Doherty 5cccfd75ce fix(grpc-data): #994 projected cohorts batch, pace and scale past one item; busy origins no longer reset quiet streams
Wave 3 of the 2026-12-02 review (docs/reviews/2026-12-02/01-runtime.md R-2/3/7/8/9/10/12/13/21/22/23/24,
02-opcuaserver.md U-10/11/12/13).

Consumer (ProjectedDataAccess cohort):
- R-2: a cohort's OpenSubscription declared a snapshot budget of 1 item, which the origin uses to size the stream's
  handle table, so the 2nd item was refused LimitExceeded and the cohort re-opened in a loop (two projected items of
  one interval broke each other). Opens/adds/removes are now batched (IDataSubscriptionCommandLimits, <=256 items,
  bounded by estimated snapshot bytes) and paced (32 burst, 32/s, <=4 commands or snapshots outstanding).
- R-7: control events no longer share the bounded frame queue; a failed Remove can no longer Stop() the cohort.
- R-10: a local capacity refusal (TryPublishOrBackpressure -> CapacityExhausted) is retried for up to 2 s instead of
  invalidating healthy evidence and retiring the route.
- R-13: RemoteSubscription mutates its handle/version tables in place; the cohort caches the source per binding and
  drops the per-sample LINQ in EvidenceFor.
- R-8: only an unselectable endpoint arms the discovery backoff; a caller cancel is OperationCanceledException.
- R-24: the condition stream cancels and observes a pending frame read before disposing the enumerator.

Origin (DataSubscriptionSession / SamplePartition / SourceInterestLease):
- R-2 (found by the perf harness): the delivery index was sized MaxPendingFrames x 16 = 1024, so the 1025th item of a
  stream stopped the session after its ack; it now covers MaxMonitoredItems.
- R-3: interest reads count only own-ticket matches (scan budget separate), re-queue a lagging tick, and catch up from
  per-source last-change versions when the bounded history rolled off - no more "Source change history expired".
- R-9: a partition quiet bump resets a lease only when one of ITS sources was invalidated (per-source quiet version)
  or its binding retired; partition-wide invalidation still resets all.
- R-12: the per-item publish verdict is memoised per (catalog instance, authority generation) for <=1 s; Commit
  under the session lock re-checks identity only.
- R-21/R-22/R-23: registry prune is interval/capacity driven; a cleared history session refuses chunks; the snapshot
  barrier is decided and armed under the same gate its release is decided under.

Node manager: U-10 projected live items indexed by node; U-11 anchor removal / root-leaf swap tombstone the anchor's
own monitored node; U-12 per-parent child count; U-13 per-condition verdict index.

Also: Issue934_r1_N1 test clears its metadata failure before ending the stream (test-only sequencing race,
reproduced with a 200 ms delay; production behaviour correct).

Perf (Category=Perf, OTOPCUA_GRPCDATA_PERF=1): 10k-item cohort opens in 3.3 s / 80 commands / 1 stream (before: never
opens); 100 source versions at 1k/s -> 11,036 item values in 569 ms.
2026-12-03 11:59:26 -05:00

151 lines
9.1 KiB
C#

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;
public sealed partial class GrpcDataProjectedAccessTests
{
[Fact(Timeout = 30000), Trait("Phase", "P11")]
public async Task New_cadence_accepts_equal_version_baseline_when_cached_evidence_has_expired()
{
using var fixture = new AccessFixture(streams: true, subscribeOnly: true) { ScalarSnapshots = true };
fixture.Proof = fixture.Proof with { MaximumSampleAge = TimeSpan.FromMilliseconds(500) };
using var access = fixture.Create();
access.TryAcquire(fixture.NodeId, fixture.Caller, DataOperations.Subscribe, out var first).ShouldBeTrue();
access.TryAcquire(fixture.NodeId, fixture.Caller, DataOperations.Subscribe, out var second).ShouldBeTrue();
try
{
access.StartSubscription(first!, TimeSpan.FromSeconds(1)).ShouldBeTrue();
var original = await fixture.ReadUpdateAsync();
original.SourceVersion.ShouldBe(1);
fixture.Advance(TimeSpan.FromSeconds(1));
access.StartSubscription(second!, TimeSpan.FromSeconds(2)).ShouldBeTrue();
var fresh = await fixture.ReadUpdateAsync();
fresh.SourceVersion.ShouldBe(1);
fresh.IsCompleteSnapshot.ShouldBeTrue();
fresh.ReceivedTimestamp.ShouldNotBe(original.ReceivedTimestamp);
fresh.OriginAgeBounds.Advance(fixture.Clock.GetElapsedTime(fresh.ReceivedTimestamp, fixture.Clock.GetTimestamp()))
.IsFresh(TimeSpan.FromMilliseconds(500)).ShouldBeTrue();
}
finally { first?.Dispose(); second?.Dispose(); }
}
[Fact(Timeout = 30000), Trait("Phase", "P11")]
public async Task Pending_second_open_cannot_preserve_or_revive_the_last_proven_source_era()
{
using var fixture = new AccessFixture(streams: true, subscribeOnly: true)
{ ScalarSnapshots = true, HoldSecondOpen = true, BlockStreamDispose = true };
using var access = fixture.Create();
access.TryAcquire(fixture.NodeId, fixture.Caller, DataOperations.Subscribe, out var first).ShouldBeTrue();
access.TryAcquire(fixture.NodeId, fixture.Caller, DataOperations.Subscribe, out var second).ShouldBeTrue();
try
{
access.StartSubscription(first!, TimeSpan.FromSeconds(1)).ShouldBeTrue();
var original = await fixture.ReadUpdateAsync();
original.SourceProof.IsValid.ShouldBeTrue();
access.StartSubscription(second!, TimeSpan.FromSeconds(2)).ShouldBeTrue();
await fixture.SecondOpenEntered.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
fixture.EndFirstStream();
await fixture.StreamDisposeEntered.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
original.SourceProof.IsValid.ShouldBeFalse("a pending Open has not established source coverage");
fixture.ReleaseSecondOpen.TrySetResult();
var fresh = await fixture.ReadUpdateAsync();
fresh.IsCompleteSnapshot.ShouldBeTrue();
fresh.SourceProof.IsValid.ShouldBeTrue();
original.SourceProof.IsValid.ShouldBeFalse("a later baseline cannot revive old queued evidence");
}
finally
{
fixture.ReleaseSecondOpen.TrySetResult(); fixture.ReleaseStreamDispose.TrySetResult();
first?.Dispose(); second?.Dispose();
}
}
[Fact(Timeout = 30000), Trait("Phase", "P11")]
public async Task Removing_last_source_handle_closes_its_coverage_even_when_cohort_has_another_source()
{
using var fixture = new AccessFixture(streams: true, subscribeOnly: true) { ScalarSnapshots = true, BlockStreamDispose = true };
using var access = fixture.Create();
access.TryAcquire(fixture.NodeId, fixture.Caller, DataOperations.Subscribe, out var first).ShouldBeTrue();
access.TryAcquire(fixture.NodeId, fixture.Caller, DataOperations.Subscribe, out var second).ShouldBeTrue();
access.TryAcquire(fixture.SecondNodeId, fixture.Caller, DataOperations.Subscribe, out var otherSource).ShouldBeTrue();
try
{
access.StartSubscription(first!, TimeSpan.FromSeconds(1)).ShouldBeTrue();
var original = await fixture.ReadUpdateAsync();
access.StartSubscription(second!, TimeSpan.FromSeconds(2)).ShouldBeTrue();
access.StartSubscription(otherSource!, TimeSpan.FromSeconds(2)).ShouldBeTrue();
await fixture.TwoStreamItems.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
var secondId = fixture.ItemFor(fixture.Proof.OriginMetadata.View, interval: TimeSpan.FromSeconds(2)).ClientItemId;
var otherId = fixture.ItemFor(fixture.SecondProof!.OriginMetadata.View, interval: TimeSpan.FromSeconds(2)).ClientItemId;
fixture.EmitValue(secondId, 2, 8);
ProjectedValueUpdate observed;
do { observed = await fixture.ReadUpdateAsync(); } while (!Equals(observed.Snapshot.Value, 8));
await access.DisposeConfiguredAsync(second!);
original.SourceProof.IsValid.ShouldBeTrue("the first source stream is still alive");
fixture.EndFirstStream();
await fixture.StreamDisposeEntered.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
original.SourceProof.IsValid.ShouldBeFalse("the other open cohort now owns only a different source");
fixture.EmitValue(otherId, 2, 99);
var stillLive = await fixture.ReadUpdateAsync();
stillLive.Snapshot.Value.ShouldBe(99);
stillLive.Route.LocalIdentifier.ShouldBe(fixture.SecondProof!.LocalIdentifier);
}
finally { fixture.ReleaseStreamDispose.TrySetResult(); first?.Dispose(); second?.Dispose(); otherSource?.Dispose(); }
}
[Fact(Timeout = 30000), Trait("Phase", "P11")]
public async Task Reconnect_waits_for_uncancellable_physical_dispose_and_cancelled_demand_never_reopens()
{
using var fixture = new AccessFixture(streams: true, subscribeOnly: true) { ScalarSnapshots = true, BlockStreamDispose = true };
using var access = fixture.Create(maxDemands: 1);
access.TryAcquire(fixture.NodeId, fixture.Caller, DataOperations.Subscribe, out var demand).ShouldBeTrue();
try
{
access.StartSubscription(demand!, TimeSpan.FromSeconds(1)).ShouldBeTrue();
var original = await fixture.ReadUpdateAsync();
original.IsCompleteSnapshot.ShouldBeTrue();
fixture.EndFirstStream();
await fixture.StreamDisposeEntered.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
original.SourceProof.IsValid.ShouldBeFalse("a lost attempt cannot keep queued evidence authoritative");
demand!.Dispose();
access.TryAcquire(fixture.NodeId, fixture.Caller, DataOperations.Subscribe, out var blocked).ShouldBeFalse(
"physical ownership remains charged until ignored-cancellation disposal completes");
blocked.ShouldBeNull();
await Task.Delay(300, TestContext.Current.CancellationToken);
fixture.StreamOpens.ShouldBe(1, "a second physical stream cannot overlap the undrained first stream");
fixture.ReleaseStreamDispose.TrySetResult();
await fixture.NoPins.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
await Task.Delay(300, TestContext.Current.CancellationToken);
fixture.StreamOpens.ShouldBe(1, "disposed interest must never be reacquired");
access.TryAcquire(fixture.NodeId, fixture.Caller, DataOperations.Subscribe, out var replacement).ShouldBeTrue();
replacement!.Dispose();
}
finally { fixture.ReleaseStreamDispose.TrySetResult(); demand?.Dispose(); }
}
[Fact(Timeout = 30000), Trait("Phase", "P11")]
public async Task Revoked_mapping_during_retry_does_not_reopen_or_revive_old_evidence()
{
using var fixture = new AccessFixture(streams: true, subscribeOnly: true) { ScalarSnapshots = true, BlockStreamDispose = true };
using var access = fixture.Create();
access.TryAcquire(fixture.NodeId, fixture.Caller, DataOperations.Subscribe, out var demand).ShouldBeTrue();
try
{
access.StartSubscription(demand!, TimeSpan.FromSeconds(1)).ShouldBeTrue();
var original = await fixture.ReadUpdateAsync();
fixture.EndFirstStream();
await fixture.StreamDisposeEntered.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
fixture.InvalidateMapping();
fixture.ReleaseStreamDispose.TrySetResult();
await Task.Delay(500, TestContext.Current.CancellationToken);
fixture.StreamOpens.ShouldBe(1);
original.SourceProof.IsValid.ShouldBeFalse();
demand!.IsValid.ShouldBeFalse();
}
finally { fixture.ReleaseStreamDispose.TrySetResult(); demand?.Dispose(); }
}
}