Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.DeploymentManager.Tests/StaleInstanceProbeTests.cs
T
Joseph Doherty 9a87d7a328 Resolve all 33 medium-severity architecture review findings
Second slice of the 2026-09-09 whole-system review remediation
(docs/plans/2026-09-09-arch-review.md §10), on top of the High slice 146a1323.

Runtime/devices: SR-2 alarm on-trigger integration scope; SR-3 correlated NativeAlarmActor
subscribe retry; SR-4 immutable audited DB target; SR-5 ordered InstanceActor stop/flush;
SR-12 rehydration fence; SR-13 S&F gate recheck per delivery; DC-4 no OPC UA security
downgrade; DC-5 alarm snapshot for every new subscriber; ORCH-1 sync-dispose joins the pump.

Management/UI: MG-2 site scope on audit tree + Flow Debugger Replay; MG-3 stale-probe memo
removed; MG-4 server-owned bulk-deploy deadline; UI-5 sandbox runs in a killable child
process (new CentralUI.SandboxWorker project); UI-14 bounded sandbox output/IPC.

Edge/audit: EI-1 relay audit ownership; EI-2 SkipBodyCapture suppresses error text; EI-3/EI-11
error-field redaction + caps; EI-4 single relay deadline, pre-execution-only retry; EI-5
authenticated relay provenance; EI-10 body cap before audit read.

Persistence/contracts: DL-1 global Instance.UniqueName unique index + conflict translation;
DL-2 DataConnection protocol config encrypted at rest (migration
20260909203046_EnforceGlobalInstanceNamesAndProtectProtocolConfiguration — coordinated
central upgrade, old binaries cannot read protected rows); DL-3 bounded Replay paging;
DL-6 cross-site Overwrite of a deployed instance rejected; XC-4 proto descriptor contract test.

Config/hygiene: XC-1 central listener isolation by local port; XC-2/XC-3 inert SMTP + cron
keys removed from rig settings; CM-2 env2 bootstrap guard enabled; DC-6 deploy site exit code;
DC-7 notifier retries only explicit standby rejection; XC-5 ClusterClient purged from docs.

Gates recorded in the report: build 0/0, 9 397 passed / 0 failed, rig redeployed on all
eight nodes with the migration applied. 46 Low findings remain open.
2026-09-09 22:05:30 -04:00

180 lines
7.8 KiB
C#

using NSubstitute;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates;
using ZB.MOM.WW.ScadaBridge.Commons.Types;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Flattening;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Templates;
using ZB.MOM.WW.ScadaBridge.TemplateEngine.Flattening;
namespace ZB.MOM.WW.ScadaBridge.DeploymentManager.Tests;
/// <summary>
/// The staleness fast path: <see cref="StaleInstanceProbe"/> memoises a computed
/// revision hash against the <see cref="ITemplateGraphWatermark"/> readings it was
/// computed under, and the memo is PROCESS-STATIC. That makes the watermark the
/// only thing standing between a cached hash and a wrong answer — so these tests
/// pin that the memo is served only while the watermark has not moved, and is
/// dropped the moment it has.
///
/// <para>
/// The failure this guards against is a real one: a data-connection edit, or a
/// bundle import committing through the raw <c>DbContext</c>, changes the flattened
/// output without going through the repository path that derives bumps from the
/// change tracker. If that write does not bump, the memo below keeps answering with
/// the old hash forever.
/// </para>
/// </summary>
public class StaleInstanceProbeTests
{
private const int InstanceId = 1;
private readonly TemplateGraphWatermark _watermark = new();
private readonly IFlatteningPipeline _pipeline = Substitute.For<IFlatteningPipeline>();
private string _currentHash = "sha256:first";
private int _flattenCount;
public StaleInstanceProbeTests()
{
// Process-static memo: start every test from a clean slate.
StaleInstanceProbe.ClearMemos();
_pipeline.CreateSession().Returns(_ => new FlattenSession(_watermark));
_pipeline
.FlattenAndValidateAsync(InstanceId, Arg.Any<CancellationToken>(), Arg.Any<bool>(), Arg.Any<FlattenSession?>())
.Returns(_ =>
{
_flattenCount++;
var config = new FlattenedConfiguration { InstanceUniqueName = "Inst-01" };
return Result<FlatteningPipelineResult>.Success(
new FlatteningPipelineResult(config, _currentHash, ValidationResult.Success()));
});
}
private StaleInstanceProbe CreateProbe() => new(_pipeline, _watermark);
[Fact]
public async Task PeerEdit_WithIndependentWatermark_IsObservedByWarmActiveProbe()
{
var active = CreateProbe();
var peer = new TemplateGraphWatermark();
Assert.Equal("sha256:first", await active.GetCurrentRevisionHashAsync(InstanceId));
_currentHash = "sha256:peer-edit"; // Shared database changed through another central.
peer.BumpAll();
Assert.Equal("sha256:peer-edit", await active.GetCurrentRevisionHashAsync(InstanceId));
}
[Fact]
public async Task UnchangedWatermark_StillReflattensAuthoritatively()
{
var probe = CreateProbe();
Assert.Equal("sha256:first", await probe.GetCurrentRevisionHashAsync(InstanceId));
Assert.Equal(1, _flattenCount);
Assert.Equal("sha256:first", await probe.GetCurrentRevisionHashAsync(InstanceId));
Assert.Equal(2, _flattenCount);
}
/// <summary>
/// <c>BumpAll</c> is the unattributed fallback used by everything that changes a
/// flattening input without an owning template id — data-connection saves and
/// bundle imports both rely on it. It must invalidate the memo.
/// </summary>
[Fact]
public async Task BumpAll_InvalidatesTheMemo()
{
var probe = CreateProbe();
await probe.GetCurrentRevisionHashAsync(InstanceId);
// The underlying config drifted (e.g. a data connection was repointed).
_currentHash = "sha256:second";
// Even without a local bump, a peer edit is authoritative.
Assert.Equal("sha256:second", await probe.GetCurrentRevisionHashAsync(InstanceId));
_watermark.BumpAll();
Assert.Equal("sha256:second", await probe.GetCurrentRevisionHashAsync(InstanceId));
Assert.Equal(3, _flattenCount);
}
[Fact]
public async Task BumpInstance_InvalidatesTheMemoForThatInstance()
{
var probe = CreateProbe();
await probe.GetCurrentRevisionHashAsync(InstanceId);
_currentHash = "sha256:second";
_watermark.BumpInstance(InstanceId);
Assert.Equal("sha256:second", await probe.GetCurrentRevisionHashAsync(InstanceId));
}
/// <summary>
/// P7-4: a non-structural template edit landing DURING the flatten must not be
/// captured as if it were already included.
///
/// <para>
/// The memo pairs a revision hash with the watermark readings that produced it.
/// Reading the per-template versions AFTER the flatten pairs the PRE-edit hash
/// with the POST-edit version, and every later probe then finds the watermark
/// unchanged and answers "not stale" forever — until an unrelated edit, a
/// restart, or the 4096-entry overflow clears it. The staleness sweep and
/// <c>ImportResult.StaleInstanceIds</c> both read through this memo.
/// </para>
///
/// <para>
/// The fix is that the versions come from the session, which records each
/// template's version at the instant it READ that template — before the edit —
/// so the mismatch on the next probe invalidates the memo. Only the recording is
/// changed: the mid-flatten edit still produces the pre-edit hash on this pass,
/// which is correct, since that is the graph the flatten actually saw.
/// </para>
/// </summary>
[Fact]
public async Task TemplateEditedDuringFlatten_InvalidatesTheMemo_RatherThanPinningTheStaleHash()
{
const int TemplateId = 42;
// The flatten reads the template through the session (recording the version
// it read it at), and the edit lands before the flatten returns.
_pipeline
.FlattenAndValidateAsync(InstanceId, Arg.Any<CancellationToken>(), Arg.Any<bool>(), Arg.Any<FlattenSession?>())
.Returns(async call =>
{
_flattenCount++;
var session = call.ArgAt<FlattenSession?>(3)!;
await session.GetTemplateAsync(TemplateId, _ => Task.FromResult<Template?>(new Template($"T{TemplateId}")));
// A non-structural template edit commits while the flatten is in flight.
_watermark.BumpTemplate(TemplateId);
_currentHash = "sha256:after-edit";
var config = new FlattenedConfiguration { InstanceUniqueName = "Inst-01" };
return Result<FlatteningPipelineResult>.Success(
new FlatteningPipelineResult(config, "sha256:before-edit", ValidationResult.Success()));
});
var probe = CreateProbe();
// Pass 1 legitimately returns the hash of the graph the flatten walked.
Assert.Equal("sha256:before-edit", await probe.GetCurrentRevisionHashAsync(InstanceId));
Assert.Equal(1, _flattenCount);
// Pass 2 must see the edit. Re-recording the versions after the flatten
// would have memoised the post-edit version against the pre-edit hash, and
// this call would still answer "sha256:before-edit" without re-flattening.
_pipeline
.FlattenAndValidateAsync(InstanceId, Arg.Any<CancellationToken>(), Arg.Any<bool>(), Arg.Any<FlattenSession?>())
.Returns(_ =>
{
_flattenCount++;
var config = new FlattenedConfiguration { InstanceUniqueName = "Inst-01" };
return Result<FlatteningPipelineResult>.Success(
new FlatteningPipelineResult(config, _currentHash, ValidationResult.Success()));
});
Assert.Equal("sha256:after-edit", await probe.GetCurrentRevisionHashAsync(InstanceId));
Assert.Equal(2, _flattenCount);
}
}