Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataLateWriteTests.cs
T
Joseph Doherty bcd4a9aa29 fix(grpc-data): #964 rich write releases its late-write slot before replying / freeing the tag
The late-write observer released the physical slot on its own continuation of
the driver task, while RunRichWriteAsync replied and told WriteCompleted on
another. A next write to the same target (queued, or sent right after the
reply) could reach TryReserve before the release and be rejected with
'Outstanding driver write capacity exhausted' although nothing was
outstanding. LateWriteReservation.Observe now returns a task that completes
after the slot is released; the actor awaits it (when the physical write has
finished) before replying and freeing the tag. The flaky unit test waited on
the lifetime counter, which is disposed just before the release - it now
awaits the settled task.

Repro: 200 sequential same-target actor writes failed 3/3 runs (no load);
0 failures in 15 runs at load avg ~468. Unit signal: lifetime-wait 22-39 /
2M false 'slot busy', settled-wait 0/2M.
2026-12-02 13:10:20 -05:00

115 lines
5.2 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
using ZB.MOM.WW.OtOpcUa.Runtime.Tests.Harness;
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
[Trait("Feature", "GrpcData")]
[Trait("Phase", "P05")]
[Trait("Category", "Unit")]
public sealed class GrpcDataLateWriteTests
{
private static readonly DataWriteOutcome Unknown = new(DataWriteState.OutcomeUnknown, 0, null);
private static readonly DataWriteOutcome Success = new(DataWriteState.DriverConfirmedSuccess, 0, null);
private static WriteClaim DispatchedClaim()
{
var registry = new WriteOperationRegistry("inc", 2, 4096, TimeSpan.FromMinutes(1));
var claim = registry.Claim("p", "op", "inc", new byte[32]).Claim!;
claim.TryMarkDispatched().ShouldBeTrue();
claim.Complete(Unknown).ShouldBeTrue();
return claim;
}
[Fact]
public async Task Reserved_observer_records_actual_late_result_and_releases_owned_lifetime()
{
var observer = new LateWriteObserver(1, TimeSpan.FromSeconds(10));
var claim = DispatchedClaim();
var source = new TaskCompletionSource<DataWriteOutcome>(TaskCreationOptions.RunContinuationsAsynchronously);
var lifetime = new CountingLifetime();
observer.TryReserve(out var reservation).ShouldBeTrue();
var settled = reservation!.Observe(source.Task, claim, lifetime);
observer.TryReserve(out _).ShouldBeFalse();
source.SetResult(Success);
// #964: the settled task, not the lifetime, is the "slot is free" signal — the slot is released after
// the lifetime, on the same continuation.
await settled.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
claim.CurrentOutcome.ShouldBe(Success);
lifetime.Count.ShouldBe(1);
(await claim.Completion).ShouldBe(Unknown);
observer.TryReserve(out var next).ShouldBeTrue();
next!.Dispose();
}
[Fact]
public async Task Expired_observer_retains_physical_capacity_and_lifetime_until_driver_really_finishes()
{
var clock = new ManualTimeProvider();
var observer = new LateWriteObserver(1, TimeSpan.FromSeconds(10), clock);
var claim = DispatchedClaim();
var source = new TaskCompletionSource<DataWriteOutcome>(TaskCreationOptions.RunContinuationsAsynchronously);
var lifetime = new CountingLifetime();
observer.TryReserve(out var reservation).ShouldBeTrue();
var settled = reservation!.Observe(source.Task, claim, lifetime);
clock.Advance(TimeSpan.FromSeconds(11));
observer.TryReserve(out _).ShouldBeFalse();
lifetime.Count.ShouldBe(0);
settled.IsCompleted.ShouldBeFalse();
source.SetResult(Success);
await settled.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
lifetime.Count.ShouldBe(1);
claim.CurrentOutcome.ShouldBe(Unknown);
observer.TryReserve(out var next).ShouldBeTrue();
next!.Dispose();
}
[Fact]
public async Task Already_completed_driver_is_observed_and_reservation_dispose_is_idempotent()
{
var observer = new LateWriteObserver(1, TimeSpan.FromSeconds(10));
var claim = DispatchedClaim();
var lifetime = new CountingLifetime();
observer.TryReserve(out var reservation).ShouldBeTrue();
var settled = reservation!.Observe(Task.FromResult(Success), claim, lifetime);
reservation.Dispose();
await settled.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
lifetime.Count.ShouldBe(1);
claim.CurrentOutcome.ShouldBe(Success);
observer.TryReserve(out var next).ShouldBeTrue();
next!.Dispose();
next.Dispose();
}
[Fact]
public async Task Confirmation_after_unknown_retention_gets_its_own_retry_window()
{
var clock = new ManualTimeProvider();
var registry = new WriteOperationRegistry("inc", 1, 4096, TimeSpan.FromMinutes(1), clock);
var claim = registry.Claim("p", "op", "inc", new byte[32]).Claim!;
claim.TryMarkDispatched().ShouldBeTrue();
claim.Complete(Unknown).ShouldBeTrue();
var observer = new LateWriteObserver(1, TimeSpan.FromMinutes(5), clock);
var source = new TaskCompletionSource<DataWriteOutcome>(TaskCreationOptions.RunContinuationsAsynchronously);
var lifetime = new CountingLifetime();
observer.TryReserve(out var reservation).ShouldBeTrue();
var settled = reservation!.Observe(source.Task, claim, lifetime);
clock.Advance(TimeSpan.FromMinutes(2));
source.SetResult(Success);
await settled.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
lifetime.Count.ShouldBe(1);
var duplicate = registry.Claim("p", "op", "inc", new byte[32]);
duplicate.Kind.ShouldBe(WriteClaimKind.Existing);
duplicate.Claim!.CurrentOutcome.ShouldBe(Success);
(await duplicate.Claim.Completion).ShouldBe(Unknown);
}
private sealed class CountingLifetime : IDisposable
{
private int _count;
public int Count => Volatile.Read(ref _count);
public void Dispose() => Interlocked.Increment(ref _count);
}
}