fix(dcl): bind the reconnect re-subscribe loop to its session; shared supervisory advise ignores the first caller's token (DCL-4, DCL-9, WP3 task 3.3)

This commit is contained in:
Joseph Doherty
2026-12-03 09:57:13 -05:00
parent 2e8bede3b2
commit d52aff1bee
7 changed files with 317 additions and 14 deletions
@@ -0,0 +1,58 @@
using ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Adapters;
namespace ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Tests.Adapters;
/// <summary>
/// DCL-9 (2026-12-02 review, WP3 task 3.3): the per-handle supervisory advise is shared by every
/// concurrent caller, so it must not run under the token of whichever caller created it. A write
/// whose deadline fires must not cancel the advise a concurrent, deadline-free subscribe awaits.
/// </summary>
public class RealMxGatewaySharedAdviseTests
{
/// <summary>The first caller's cancellation fails only that caller; the shared advise completes for the second.</summary>
[Fact]
public async Task FirstCallersCancellation_DoesNotCancelTheSharedAdvise()
{
var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
var coreCalls = 0;
CancellationToken coreToken = default;
var client = new RealMxGatewayClient(null)
{
AdviseCoreOverride = async (_, ct) =>
{
Interlocked.Increment(ref coreCalls);
coreToken = ct;
await release.Task.WaitAsync(ct);
},
};
using var writeDeadline = new CancellationTokenSource();
var write = client.EnsureSupervisoryAdvisedAsync(42, writeDeadline.Token);
var subscribe = client.EnsureSupervisoryAdvisedAsync(42, CancellationToken.None);
writeDeadline.Cancel();
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => write);
Assert.False(coreToken.IsCancellationRequested); // pre-fix: the shared advise was cancelled
release.SetResult();
await subscribe.WaitAsync(TimeSpan.FromSeconds(5)); // pre-fix: OperationCanceledException
Assert.Equal(1, coreCalls); // advise-once still holds
}
/// <summary>A faulted shared advise is evicted so the next caller retries it.</summary>
[Fact]
public async Task FaultedSharedAdvise_IsRetriedByTheNextCaller()
{
var coreCalls = 0;
var client = new RealMxGatewayClient(null)
{
AdviseCoreOverride = (_, _) => Interlocked.Increment(ref coreCalls) == 1
? Task.FromException(new InvalidOperationException("gateway busy"))
: Task.CompletedTask,
};
await Assert.ThrowsAsync<InvalidOperationException>(() => client.EnsureSupervisoryAdvisedAsync(7, CancellationToken.None));
await client.EnsureSupervisoryAdvisedAsync(7, CancellationToken.None);
Assert.Equal(2, coreCalls);
}
}