fix(alarms): atomic snapshot+truncation read; direct tests for the flag plumbing (review)
Review found AlarmDispatcher.SnapshotActiveAlarms reading the snapshot and the truncation verdict through two independent lock acquisitions, defended by a comment claiming read-order made a race "widen only, never narrow". That claim was false: a not-truncated -> truncated poll landing between the two reads pairs a stale false with a capped snapshot, which is exactly the false all-clear the feature exists to prevent. It was safe only because AlarmCommandHandler STA-serializes consumer calls — an accident of the call graph, not an invariant. Made the invariant structural instead of documented. IMxAccessAlarmConsumer now exposes ONE accessor, `IReadOnlyList<MxAlarmSnapshotRecord> SnapshotActiveAlarms( out bool truncated)`, which implementations must satisfy from a single acquisition of the lock guarding the retained snapshot — mirroring the write side, where FoldFetch already updates snapshot and verdict together. The separate LastSnapshotTruncated property is gone from every layer, so there is no second read left to pair badly. `out` over a result struct follows the file's established idiom (FoldFetch, ParseSnapshotXml). The same threading applies one level up: IAlarmCommandHandler.QueryActive now carries `out bool snapshotTruncated`, so MxAccessCommandExecutor stamps the reply payload from the value the records were stamped with rather than reading the state a second time. Direct tests for the three hops that were only covered end-to-end: - AlarmDispatcherTests: truncated consumer snapshot stamps FromTruncatedSnapshot on every mapped record, with a complete-snapshot control, plus an assertion that the independent per-record Degraded flag is not dragged along. - AlarmCommandHandlerTests: the verdict delegates through the dispatcher (Theory over both values), and survives a prefix filter that removes every record — the case the per-record flag cannot cover. - AlarmCommandExecutorTests: the reply payload's SnapshotTruncated comes from the handler (Theory over both values), including the zero-record case. The WnWrapAlarmConsumer truncation tests now assert through SnapshotActiveAlarms(out ...) rather than an internal field, because the pairing is the contract. Also: GatewayAlarmMonitor's _snapshotTruncated comment now says "as of the last full reconcile" rather than implying it tracks the current _alarms contents, which live transitions keep moving via ApplyTransition between passes. Detection heuristic still untouched (fetchedRecordCount >= maxAlarmsPerFetch); no @COUNT parsing, per docs/AlarmProbeFindings.md. Still additive gateway metadata about our fetch mechanics, not MXAccess behavior — not a parity deviation, and no event is synthesized. Gateway: NonWindows.slnx builds clean (0 warnings); ~Alarm filter 107/107 pass. Worker + Worker.Tests are windev-gated; the signature change was reviewed by inspection across all 7 IMxAccessAlarmConsumer implementers, all 3 IAlarmCommandHandler implementers, and every call site.
This commit is contained in:
@@ -267,7 +267,7 @@ public sealed class AlarmDispatcherTests
|
||||
new MxAccessAlarmEventSink(new MxAccessEventQueue(), new MxAccessEventMapper()),
|
||||
SessionId);
|
||||
|
||||
IReadOnlyList<ActiveAlarmSnapshot> snapshots = dispatcher.SnapshotActiveAlarms();
|
||||
IReadOnlyList<ActiveAlarmSnapshot> snapshots = dispatcher.SnapshotActiveAlarms(out _);
|
||||
Assert.Equal(2, snapshots.Count);
|
||||
|
||||
Assert.Equal("Galaxy!TestArea.Tag1", snapshots[0].AlarmFullReference);
|
||||
@@ -323,7 +323,7 @@ public sealed class AlarmDispatcherTests
|
||||
new MxAccessAlarmEventSink(new MxAccessEventQueue(), new MxAccessEventMapper()),
|
||||
SessionId);
|
||||
|
||||
IReadOnlyList<ActiveAlarmSnapshot> snapshots = dispatcher.SnapshotActiveAlarms();
|
||||
IReadOnlyList<ActiveAlarmSnapshot> snapshots = dispatcher.SnapshotActiveAlarms(out _);
|
||||
Assert.Equal(2, snapshots.Count);
|
||||
|
||||
Assert.True(snapshots[0].Degraded);
|
||||
@@ -333,6 +333,82 @@ public sealed class AlarmDispatcherTests
|
||||
Assert.Equal(AlarmProviderMode.Alarmmgr, snapshots[1].SourceProvider);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A truncated consumer snapshot stamps every mapped record with
|
||||
/// <c>FromTruncatedSnapshot</c> and reports the verdict out of the same
|
||||
/// call. Every record carries it because the public QueryActiveAlarms RPC
|
||||
/// streams bare snapshots with no envelope to hold set-level status, so
|
||||
/// a client that reads only one record must still learn the set may be
|
||||
/// incomplete.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void SnapshotActiveAlarms_WhenConsumerReportsTruncated_StampsEveryRecord()
|
||||
{
|
||||
FakeAlarmConsumer consumer = new FakeAlarmConsumer
|
||||
{
|
||||
SnapshotTruncated = true,
|
||||
SnapshotResult = new[]
|
||||
{
|
||||
NewSnapshotRecord("Tag1", degraded: false),
|
||||
NewSnapshotRecord("Tag2", degraded: true),
|
||||
},
|
||||
};
|
||||
using AlarmDispatcher dispatcher = new AlarmDispatcher(
|
||||
consumer,
|
||||
new MxAccessAlarmEventSink(new MxAccessEventQueue(), new MxAccessEventMapper()),
|
||||
SessionId);
|
||||
|
||||
IReadOnlyList<ActiveAlarmSnapshot> snapshots = dispatcher.SnapshotActiveAlarms(out bool truncated);
|
||||
|
||||
Assert.True(truncated);
|
||||
Assert.Equal(2, snapshots.Count);
|
||||
Assert.All(snapshots, snapshot => Assert.True(snapshot.FromTruncatedSnapshot));
|
||||
// Truncation is about the SET; the per-record provider fidelity flag is
|
||||
// independent and must not be dragged along with it.
|
||||
Assert.False(snapshots[0].Degraded);
|
||||
Assert.True(snapshots[1].Degraded);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The control. A complete consumer snapshot must leave every record's
|
||||
/// <c>FromTruncatedSnapshot</c> unset — without this, a field hard-wired
|
||||
/// to true would satisfy the test above and every snapshot would read as
|
||||
/// possibly-incomplete.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void SnapshotActiveAlarms_WhenConsumerReportsComplete_LeavesRecordsUnstamped()
|
||||
{
|
||||
FakeAlarmConsumer consumer = new FakeAlarmConsumer
|
||||
{
|
||||
SnapshotTruncated = false,
|
||||
SnapshotResult = new[] { NewSnapshotRecord("Tag1", degraded: false) },
|
||||
};
|
||||
using AlarmDispatcher dispatcher = new AlarmDispatcher(
|
||||
consumer,
|
||||
new MxAccessAlarmEventSink(new MxAccessEventQueue(), new MxAccessEventMapper()),
|
||||
SessionId);
|
||||
|
||||
IReadOnlyList<ActiveAlarmSnapshot> snapshots = dispatcher.SnapshotActiveAlarms(out bool truncated);
|
||||
|
||||
Assert.False(truncated);
|
||||
Assert.False(Assert.Single(snapshots).FromTruncatedSnapshot);
|
||||
}
|
||||
|
||||
private static MxAlarmSnapshotRecord NewSnapshotRecord(string tagName, bool degraded)
|
||||
{
|
||||
return new MxAlarmSnapshotRecord
|
||||
{
|
||||
AlarmGuid = Guid.NewGuid(),
|
||||
ProviderName = "Galaxy",
|
||||
Group = "TestArea",
|
||||
TagName = tagName,
|
||||
Type = "DSC",
|
||||
Priority = 500,
|
||||
State = MxAlarmStateKind.UnackAlm,
|
||||
Degraded = degraded,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>Verifies that dispose unsubscribes the handler and disposes the consumer.</summary>
|
||||
[Fact]
|
||||
public void Dispose_WhenSubscribed_UnsubscribesHandlerAndDisposesConsumer()
|
||||
@@ -432,15 +508,16 @@ public sealed class AlarmDispatcherTests
|
||||
/// <summary>Gets the last acknowledge-by-name tuple (alarm name, provider, group).</summary>
|
||||
public (string Name, string Provider, string Group)? LastAckByNameTuple { get; private set; }
|
||||
|
||||
/// <summary>Gets or sets the truncation verdict this consumer reports with its snapshot.</summary>
|
||||
public bool SnapshotTruncated { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public IReadOnlyList<MxAlarmSnapshotRecord> SnapshotActiveAlarms()
|
||||
public IReadOnlyList<MxAlarmSnapshotRecord> SnapshotActiveAlarms(out bool truncated)
|
||||
{
|
||||
truncated = SnapshotTruncated;
|
||||
return SnapshotResult;
|
||||
}
|
||||
|
||||
/// <summary>Gets or sets the truncation verdict the dispatcher stamps onto snapshots.</summary>
|
||||
public bool LastSnapshotTruncated { get; set; }
|
||||
|
||||
/// <summary>Gets the count of poll operations.</summary>
|
||||
public int PollCount { get; private set; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user