Merge stream D (dashboard: bounded hub fan-out, alarm reset marker, browse debounce, snapshot config once, health status, per-viewer LRU, circuit retention, doc rewrite)

This commit is contained in:
Joseph Doherty
2026-10-22 06:32:17 -04:00
29 changed files with 2426 additions and 383 deletions
@@ -0,0 +1,83 @@
using ZB.MOM.WW.MxGateway.Contracts.Proto;
using ZB.MOM.WW.MxGateway.Server.Dashboard;
namespace ZB.MOM.WW.MxGateway.Tests.Dashboard;
/// <summary>
/// Verifies the Alarms page's memoized projections: one computation per change of
/// the alarm generation or filter inputs, however many times a render asks.
/// </summary>
public sealed class DashboardAlarmViewTests
{
private static readonly DashboardAlarmFilterCriteria DefaultCriteria =
new(ShowActive: true, ShowAcked: false, Area: string.Empty, MinSeverity: 0, MaxSeverity: 1000, Search: string.Empty);
/// <summary>Repeated reads at the same generation and criteria reuse one computation.</summary>
[Fact]
public void Filtered_WithUnchangedInputs_ComputesOnce()
{
DashboardAlarmView view = new();
DashboardActiveAlarm[] alarms = [Alarm("A", 500), Alarm("B", 800)];
IReadOnlyList<DashboardActiveAlarm> first = view.Filtered(1, DefaultCriteria, alarms);
IReadOnlyList<DashboardActiveAlarm> second = view.Filtered(1, DefaultCriteria with { }, alarms);
Assert.Same(first, second);
Assert.Equal(1, view.FilterComputations);
Assert.Equal(["B", "A"], first.Select(alarm => alarm.Reference));
}
/// <summary>A changed filter input or a new alarm generation recomputes.</summary>
[Fact]
public void Filtered_WhenCriteriaOrGenerationChange_Recomputes()
{
DashboardAlarmView view = new();
DashboardActiveAlarm[] alarms = [Alarm("Tank01.Hi", 500), Alarm("Pump02.Lo", 800)];
view.Filtered(1, DefaultCriteria, alarms);
IReadOnlyList<DashboardActiveAlarm> searched = view.Filtered(1, DefaultCriteria with { Search = "tank" }, alarms);
Assert.Equal(2, view.FilterComputations);
Assert.Equal(["Tank01.Hi"], searched.Select(alarm => alarm.Reference));
DashboardActiveAlarm[] refreshed = [Alarm("Tank03.Hi", 900)];
IReadOnlyList<DashboardActiveAlarm> next = view.Filtered(2, DefaultCriteria with { Search = "tank" }, refreshed);
Assert.Equal(3, view.FilterComputations);
Assert.Equal(["Tank03.Hi"], next.Select(alarm => alarm.Reference));
}
/// <summary>The area list is computed once per alarm generation, independent of filter inputs.</summary>
[Fact]
public void Areas_ComputesOncePerGeneration()
{
DashboardAlarmView view = new();
DashboardActiveAlarm[] alarms = [Alarm("A", 1, "north"), Alarm("B", 1, "North"), Alarm("C", 1, "east"), Alarm("D", 1, " ")];
IReadOnlyList<string> first = view.Areas(1, alarms);
IReadOnlyList<string> second = view.Areas(1, alarms);
Assert.Same(first, second);
Assert.Equal(1, view.AreaComputations);
Assert.Equal(["east", "north"], first);
view.Areas(2, alarms);
Assert.Equal(2, view.AreaComputations);
}
private static DashboardActiveAlarm Alarm(string reference, int severity, string area = "")
{
return new DashboardActiveAlarm(
Reference: reference,
Provider: string.Empty,
Area: area,
Source: string.Empty,
AlarmType: string.Empty,
Severity: severity,
State: AlarmConditionState.Active,
LastTransition: null,
OperatorUser: string.Empty,
OperatorComment: string.Empty,
Description: string.Empty);
}
}
@@ -0,0 +1,95 @@
using ZB.MOM.WW.GalaxyRepository;
using ZB.MOM.WW.GalaxyRepository.Grpc;
using ZB.MOM.WW.MxGateway.Server.Dashboard;
namespace ZB.MOM.WW.MxGateway.Tests.Dashboard;
/// <summary>
/// Verifies the Browse filter's shared index: case-insensitive matching on reference
/// and attribute name, the result limit, one build per Galaxy cache sequence, and
/// cancellation of a superseded scan.
/// </summary>
public sealed class DashboardBrowseSearchIndexTests
{
/// <summary>Matches either the full reference or the attribute name, ignoring case.</summary>
[Fact]
public void Search_MatchesReferenceOrAttributeNameCaseInsensitively()
{
DashboardBrowseSearchIndex index = new();
GalaxyHierarchyCacheEntry entry = CreateEntry(sequence: 1);
IReadOnlyList<GalaxyAttribute> byReference = index.Search(entry, " PUMP_001.p ", 10, CancellationToken.None);
IReadOnlyList<GalaxyAttribute> byName = index.Search(entry, "levels", 10, CancellationToken.None);
Assert.Equal(["Pump_001.PV"], byReference.Select(attr => attr.FullTagReference));
Assert.Equal(["Tank_001.Levels[]"], byName.Select(attr => attr.FullTagReference));
Assert.Empty(index.Search(entry, " ", 10, CancellationToken.None));
}
/// <summary>Stops at the limit, in Galaxy object order.</summary>
[Fact]
public void Search_StopsAtTheLimit()
{
DashboardBrowseSearchIndex index = new();
IReadOnlyList<GalaxyAttribute> matches = index.Search(CreateEntry(sequence: 1), "_001", 2, CancellationToken.None);
Assert.Equal(["Pump_001.PV", "Pump_001.Alarm"], matches.Select(attr => attr.FullTagReference));
}
/// <summary>The index is built once per cache sequence and rebuilt when the sequence moves.</summary>
[Fact]
public void Search_RebuildsOnlyWhenTheCacheSequenceChanges()
{
DashboardBrowseSearchIndex index = new();
GalaxyHierarchyCacheEntry first = CreateEntry(sequence: 1);
index.Search(first, "pump", 10, CancellationToken.None);
index.Search(first, "tank", 10, CancellationToken.None);
Assert.Equal(1, index.BuildCount);
GalaxyHierarchyCacheEntry redeployed = CreateEntry(sequence: 2, extraTag: "Valve_001.Open");
IReadOnlyList<GalaxyAttribute> matches = index.Search(redeployed, "valve", 10, CancellationToken.None);
Assert.Equal(2, index.BuildCount);
Assert.Equal(["Valve_001.Open"], matches.Select(attr => attr.FullTagReference));
}
/// <summary>A cancelled (superseded) search throws instead of finishing the scan.</summary>
[Fact]
public void Search_WhenCancelled_Throws()
{
DashboardBrowseSearchIndex index = new();
using CancellationTokenSource cancelled = new();
cancelled.Cancel();
Assert.ThrowsAny<OperationCanceledException>(
() => index.Search(CreateEntry(sequence: 1), "pump", 10, cancelled.Token));
}
private static GalaxyHierarchyCacheEntry CreateEntry(long sequence, string? extraTag = null)
{
GalaxyObject pump = new() { GobjectId = 1, TagName = "Pump_001" };
pump.Attributes.Add(new GalaxyAttribute { AttributeName = "PV", FullTagReference = "Pump_001.PV" });
pump.Attributes.Add(new GalaxyAttribute { AttributeName = "Alarm", FullTagReference = "Pump_001.Alarm" });
GalaxyObject tank = new() { GobjectId = 2, TagName = "Tank_001" };
tank.Attributes.Add(new GalaxyAttribute { AttributeName = "Levels", FullTagReference = "Tank_001.Levels[]" });
List<GalaxyObject> objects = [pump, tank];
if (extraTag is not null)
{
GalaxyObject extra = new() { GobjectId = 3, TagName = extraTag.Split('.')[0] };
extra.Attributes.Add(new GalaxyAttribute { AttributeName = extraTag.Split('.')[1], FullTagReference = extraTag });
objects.Add(extra);
}
return GalaxyHierarchyCacheEntry.Empty with
{
Status = GalaxyCacheStatus.Healthy,
Sequence = sequence,
Objects = objects,
Index = GalaxyHierarchyIndex.Build(objects),
};
}
}
@@ -15,7 +15,9 @@ namespace ZB.MOM.WW.MxGateway.Tests.Gateway.Dashboard;
/// both value-bearing payload arms are stripped from the copy broadcast to
/// browser clients when the flag is off, present when it is on, and the source
/// <see cref="AlarmFeedMessage"/> — shared with the gRPC <c>StreamAlarms</c>
/// subscribers and the alarms page — is never mutated.
/// subscribers and the alarms page — is never mutated. Also verifies that a
/// slow client cannot stall the feed and that every (re)subscription is announced
/// with a reset marker ahead of its snapshot.
/// </summary>
public sealed class AlarmsHubPublisherTests
{
@@ -136,6 +138,81 @@ public sealed class AlarmsHubPublisherTests
Assert.True(hubContext.Sent[2].SnapshotStatus.Truncated);
}
/// <summary>
/// Every subscription — the first and each resubscribe after the monitor ends a
/// stream — is preceded by a reset marker, so a client knows to discard its
/// state before the replayed snapshot arrives instead of double-counting it.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task ExecuteAsync_OnResubscribe_SendsResetMarkerBeforeTheReplayedSnapshot()
{
AlarmFeedMessage first = BuildActiveAlarm();
AlarmFeedMessage replayed = BuildActiveAlarm();
ScriptedAlarmService alarmService = new([first], [replayed]);
CapturingHubContext hubContext = new();
AlarmsHubPublisher publisher = CreatePublisher(alarmService, hubContext, AlarmsHubPublisher.DefaultSendTimeout);
using CancellationTokenSource cts = new();
await publisher.StartAsync(cts.Token).WaitAsync(TestTimeout);
await WaitUntilAsync(() => hubContext.Sent.Count >= 2);
await cts.CancelAsync();
await publisher.StopAsync(CancellationToken.None);
IReadOnlyList<(string Method, object? Argument)> frames = hubContext.Frames;
Assert.Equal(
[AlarmsHub.ResetMessage, AlarmsHub.AlarmMessage, AlarmsHub.ResetMessage, AlarmsHub.AlarmMessage],
frames.Select(frame => frame.Method).Take(4));
Assert.Equal(1, Assert.IsType<AlarmFeedReset>(frames[0].Argument).Generation);
Assert.Equal(2, Assert.IsType<AlarmFeedReset>(frames[2].Argument).Generation);
Assert.Same(first, frames[1].Argument);
Assert.Same(replayed, frames[3].Argument);
}
/// <summary>
/// A broadcast that never completes — one client whose transport is wedged — is
/// abandoned at the send timeout, so the loop keeps draining the monitor
/// subscription instead of stalling until the monitor drops it.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task ExecuteAsync_WhenABroadcastNeverCompletes_AbandonsItAndKeepsDraining()
{
AlarmFeedMessage stuck = BuildTransition();
AlarmFeedMessage next = BuildActiveAlarm();
ScriptedAlarmService alarmService = new([stuck, next]);
CapturingHubContext hubContext = new();
hubContext.Proxy.StallFirstAlarm = true;
AlarmsHubPublisher publisher = CreatePublisher(alarmService, hubContext, TimeSpan.FromMilliseconds(50));
using CancellationTokenSource cts = new();
await publisher.StartAsync(cts.Token).WaitAsync(TestTimeout);
await WaitUntilAsync(() => hubContext.Sent.Count >= 2);
await cts.CancelAsync();
await publisher.StopAsync(CancellationToken.None);
Assert.Equal("Tank02.Level.Lo", hubContext.Sent[1].ActiveAlarm.AlarmFullReference);
}
private static AlarmsHubPublisher CreatePublisher(
IGatewayAlarmService alarmService,
CapturingHubContext hubContext,
TimeSpan sendTimeout,
bool showTagValues = true)
{
GatewayOptions gatewayOptions = new()
{
Dashboard = new DashboardOptions { ShowTagValues = showTagValues },
};
return new AlarmsHubPublisher(
alarmService,
hubContext,
Options.Create(gatewayOptions),
NullLogger<AlarmsHubPublisher>.Instance,
sendTimeout);
}
/// <summary>
/// Starts the publisher over a scripted feed, waits until every scripted
/// message has been broadcast, and stops it.
@@ -149,16 +226,11 @@ public sealed class AlarmsHubPublisherTests
{
ScriptedAlarmService alarmService = new(messages);
CapturingHubContext hubContext = new();
GatewayOptions gatewayOptions = new()
{
Dashboard = new DashboardOptions { ShowTagValues = showTagValues },
};
AlarmsHubPublisher publisher = new(
AlarmsHubPublisher publisher = CreatePublisher(
alarmService,
hubContext,
Options.Create(gatewayOptions),
NullLogger<AlarmsHubPublisher>.Instance);
AlarmsHubPublisher.DefaultSendTimeout,
showTagValues);
using CancellationTokenSource cts = new();
await publisher.StartAsync(cts.Token).WaitAsync(TestTimeout);
@@ -221,12 +293,15 @@ public sealed class AlarmsHubPublisherTests
}
/// <summary>
/// Yields a scripted message list once and then stays open until cancelled,
/// so the publisher's reconnect loop never re-subscribes mid-test.
/// Yields one scripted batch per subscription. Every batch but the last ends its
/// stream — the monitor completing a subscription — so the publisher resubscribes;
/// the last batch stays open until cancelled, so the reconnect loop then settles.
/// </summary>
/// <param name="messages">The messages to yield, in order.</param>
private sealed class ScriptedAlarmService(IReadOnlyList<AlarmFeedMessage> messages) : IGatewayAlarmService
/// <param name="batches">The messages each successive subscription yields, in order.</param>
private sealed class ScriptedAlarmService(params IReadOnlyList<AlarmFeedMessage>[] batches) : IGatewayAlarmService
{
private int _subscriptions;
/// <inheritdoc />
public GatewayAlarmMonitorState State => GatewayAlarmMonitorState.Monitoring;
@@ -247,11 +322,24 @@ public sealed class AlarmsHubPublisherTests
string? alarmFilterPrefix,
[EnumeratorCancellation] CancellationToken cancellationToken)
{
int index = Interlocked.Increment(ref _subscriptions) - 1;
IReadOnlyList<AlarmFeedMessage> messages = batches[Math.Min(index, batches.Length - 1)];
if (index >= batches.Length)
{
// Past the script: stay open and quiet rather than replaying the last batch.
messages = [];
}
foreach (AlarmFeedMessage message in messages)
{
yield return message;
}
if (index < batches.Length - 1)
{
yield break;
}
try
{
await Task.Delay(Timeout.InfiniteTimeSpan, cancellationToken).ConfigureAwait(false);
@@ -277,8 +365,14 @@ public sealed class AlarmsHubPublisherTests
/// <summary>Gets the group manager.</summary>
public IGroupManager Groups { get; } = new NoopGroupManager();
/// <summary>Gets every message the publisher broadcast, in order.</summary>
/// <summary>Gets the capturing proxy every client lookup resolves to.</summary>
public CapturingClientProxy Proxy => _clients.GroupProxy;
/// <summary>Gets every alarm message the publisher broadcast, in order.</summary>
public IReadOnlyList<AlarmFeedMessage> Sent => _clients.GroupProxy.Sent;
/// <summary>Gets every frame the publisher broadcast — alarm messages and reset markers — in order.</summary>
public IReadOnlyList<(string Method, object? Argument)> Frames => _clients.GroupProxy.Frames;
}
private sealed class CapturingHubClients : IHubClients
@@ -334,6 +428,26 @@ public sealed class AlarmsHubPublisherTests
private sealed class CapturingClientProxy : IClientProxy
{
private readonly List<AlarmFeedMessage> _sent = [];
private readonly List<(string Method, object? Argument)> _frames = [];
private bool _stalled;
/// <summary>
/// Gets or sets a value indicating whether the first alarm send never completes
/// and ignores its cancellation token — a wedged client transport.
/// </summary>
public bool StallFirstAlarm { get; set; }
/// <summary>Gets every frame sent through this proxy, in order.</summary>
public IReadOnlyList<(string Method, object? Argument)> Frames
{
get
{
lock (_sent)
{
return [.. _frames];
}
}
}
/// <summary>Gets every alarm message sent through this proxy, in order.</summary>
public IReadOnlyList<AlarmFeedMessage> Sent
@@ -347,22 +461,29 @@ public sealed class AlarmsHubPublisherTests
}
}
/// <summary>Records the broadcast message and completes synchronously.</summary>
/// <summary>Records the broadcast frame and completes synchronously unless stalled.</summary>
/// <param name="method">The SignalR method name.</param>
/// <param name="args">The method arguments.</param>
/// <param name="cancellationToken">Token to observe for cancellation.</param>
/// <returns>A completed task.</returns>
/// <returns>A completed task, or a never-completing one for the stalled send.</returns>
public Task SendCoreAsync(string method, object?[] args, CancellationToken cancellationToken = default)
{
if (args.Length > 0 && args[0] is AlarmFeedMessage message)
bool stall = false;
lock (_sent)
{
lock (_sent)
_frames.Add((method, args.Length > 0 ? args[0] : null));
if (args.Length > 0 && args[0] is AlarmFeedMessage message)
{
_sent.Add(message);
if (StallFirstAlarm && !_stalled)
{
_stalled = true;
stall = true;
}
}
}
return Task.CompletedTask;
return stall ? new TaskCompletionSource().Task : Task.CompletedTask;
}
}
@@ -12,7 +12,8 @@ namespace ZB.MOM.WW.MxGateway.Tests.Gateway.Dashboard;
/// <c>MxGateway:Dashboard:ShowTagValues</c>: tag values are stripped
/// from the mirrored copy when the flag is off, present when it is on, and the
/// shared source event is never mutated. Also verifies the viewer gate — the
/// mirror does no work at all for a session nobody is watching.
/// mirror does no work at all for a session nobody is watching, and that hub
/// delivery goes through one bounded drop-oldest pump per hub-watched session.
/// </summary>
public sealed class DashboardEventBroadcasterTests
{
@@ -22,11 +23,12 @@ public sealed class DashboardEventBroadcasterTests
{
CapturingHubContext hubContext = new();
EventsHubViewerRegistry viewers = new();
DashboardEventBroadcaster broadcaster = Create(hubContext, showTagValues: false, viewers);
using DashboardEventBroadcaster broadcaster = Create(hubContext, showTagValues: false, viewers);
MxEvent source = BuildEventWithValue();
broadcaster.Publish("session-1", source);
Assert.Equal(0, broadcaster.ActiveHubPumpCount);
Assert.Equal(0, hubContext.SendCount);
Assert.Null(hubContext.LastArgument);
}
@@ -38,40 +40,105 @@ public sealed class DashboardEventBroadcasterTests
CapturingHubContext hubContext = new();
EventsHubViewerRegistry viewers = new();
viewers.AddViewer("conn-1", "session-2");
DashboardEventBroadcaster broadcaster = Create(hubContext, showTagValues: false, viewers);
using DashboardEventBroadcaster broadcaster = Create(hubContext, showTagValues: false, viewers);
broadcaster.Publish("session-1", BuildEventWithValue());
Assert.Equal(0, broadcaster.ActiveHubPumpCount);
Assert.Equal(0, hubContext.SendCount);
}
/// <summary>Once the last viewer leaves, the mirror stops sending again.</summary>
/// <summary>Once the last hub viewer leaves, the session's pump is torn down and sending stops.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public void Publish_AfterLastViewerLeaves_StopsSending()
public async Task Publish_AfterLastViewerLeaves_StopsSending()
{
CapturingHubContext hubContext = new();
EventsHubViewerRegistry viewers = new();
viewers.AddViewer("conn-1", "session-1");
DashboardEventBroadcaster broadcaster = Create(hubContext, showTagValues: false, viewers);
using DashboardEventBroadcaster broadcaster = Create(hubContext, showTagValues: false, viewers);
broadcaster.Publish("session-1", BuildEventWithValue());
Assert.Equal(1, hubContext.SendCount);
await hubContext.WaitForSendCountAsync(1);
Assert.Equal(1, broadcaster.ActiveHubPumpCount);
viewers.RemoveViewer("conn-1", "session-1");
Assert.Equal(0, broadcaster.ActiveHubPumpCount);
broadcaster.Publish("session-1", BuildEventWithValue());
Assert.Equal(0, broadcaster.ActiveHubPumpCount);
Assert.Equal(1, hubContext.SendCount);
}
/// <summary>
/// A hub viewer that cannot keep up costs a bounded queue, not an unbounded pile of
/// pending sends: while one send is stuck, further events drop oldest-first at the
/// pump's capacity, only one send is ever in flight, and the newest event survives.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task Publish_WhenHubSendStalls_BuffersAtMostTheCapacityAndKeepsTheNewest()
{
CapturingHubContext hubContext = new();
hubContext.Proxy.Gate = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
EventsHubViewerRegistry viewers = WatchedSession1();
using DashboardEventBroadcaster broadcaster = Create(hubContext, showTagValues: true, viewers);
const int published = 2000;
broadcaster.Publish("session-1", BuildSequencedEvent(0));
await hubContext.WaitForSendCountAsync(1);
for (int i = 1; i < published; i++)
{
broadcaster.Publish("session-1", BuildSequencedEvent(i));
}
// One send stuck in flight, nothing else started behind it.
Assert.Equal(1, hubContext.SendCount);
Assert.Equal(1, hubContext.Proxy.MaxConcurrentSends);
hubContext.Proxy.Gate.SetResult();
await hubContext.WaitForLastItemHandleAsync(published - 1);
// The stuck event plus at most one full queue — the rest were dropped oldest-first.
Assert.InRange(hubContext.SendCount, 2, DashboardEventBroadcaster.HubQueueCapacity + 1);
Assert.Equal(1, hubContext.Proxy.MaxConcurrentSends);
}
/// <summary>
/// A send that never completes is abandoned at the send timeout, so one stuck
/// connection cannot stop the pump delivering later events.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task Publish_WhenHubSendNeverCompletes_AbandonsItAtTheTimeoutAndContinues()
{
CapturingHubContext hubContext = new();
hubContext.Proxy.Gate = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
hubContext.Proxy.IgnoreCancellation = true;
using DashboardEventBroadcaster broadcaster = Create(
hubContext,
showTagValues: true,
WatchedSession1(),
hubSendTimeout: TimeSpan.FromMilliseconds(50));
broadcaster.Publish("session-1", BuildSequencedEvent(1));
broadcaster.Publish("session-1", BuildSequencedEvent(2));
await hubContext.WaitForSendCountAsync(2);
}
/// <summary>Values are stripped from the mirror when ShowTagValues is off; metadata survives.</summary>
[Fact]
public void Publish_WhenShowTagValuesFalse_RedactsValuesButKeepsMetadata()
/// <returns>A task that represents the asynchronous operation.</returns>
public async Task Publish_WhenShowTagValuesFalse_RedactsValuesButKeepsMetadata()
{
CapturingHubContext hubContext = new();
DashboardEventBroadcaster broadcaster = Create(hubContext, showTagValues: false, WatchedSession1());
using DashboardEventBroadcaster broadcaster = Create(hubContext, showTagValues: false, WatchedSession1());
MxEvent source = BuildEventWithValue();
broadcaster.Publish("session-1", source);
await hubContext.WaitForSendCountAsync(1);
MxEvent sent = Assert.IsType<MxEvent>(hubContext.LastArgument);
Assert.Null(sent.Value);
@@ -88,13 +155,15 @@ public sealed class DashboardEventBroadcasterTests
/// <summary>Redaction applies to a clone, so the shared source event keeps its values.</summary>
[Fact]
public void Publish_WhenShowTagValuesFalse_DoesNotMutateSourceEvent()
/// <returns>A task that represents the asynchronous operation.</returns>
public async Task Publish_WhenShowTagValuesFalse_DoesNotMutateSourceEvent()
{
CapturingHubContext hubContext = new();
DashboardEventBroadcaster broadcaster = Create(hubContext, showTagValues: false, WatchedSession1());
using DashboardEventBroadcaster broadcaster = Create(hubContext, showTagValues: false, WatchedSession1());
MxEvent source = BuildEventWithValue();
broadcaster.Publish("session-1", source);
await hubContext.WaitForSendCountAsync(1);
// The redaction must apply to a clone; the shared source keeps its values.
Assert.NotNull(source.Value);
@@ -106,13 +175,15 @@ public sealed class DashboardEventBroadcasterTests
/// <summary>Values pass through unredacted when ShowTagValues is on.</summary>
[Fact]
public void Publish_WhenShowTagValuesTrue_KeepsValues()
/// <returns>A task that represents the asynchronous operation.</returns>
public async Task Publish_WhenShowTagValuesTrue_KeepsValues()
{
CapturingHubContext hubContext = new();
DashboardEventBroadcaster broadcaster = Create(hubContext, showTagValues: true, WatchedSession1());
using DashboardEventBroadcaster broadcaster = Create(hubContext, showTagValues: true, WatchedSession1());
MxEvent source = BuildEventWithValue();
broadcaster.Publish("session-1", source);
await hubContext.WaitForSendCountAsync(1);
MxEvent sent = Assert.IsType<MxEvent>(hubContext.LastArgument);
Assert.NotNull(sent.Value);
@@ -125,16 +196,18 @@ public sealed class DashboardEventBroadcasterTests
/// An in-process subscriber gets the same redacted clone the hub group gets,
/// and the shared source event is still left untouched.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public void Subscribe_WhenShowTagValuesFalse_DeliversRedactedCloneWithoutMutatingSource()
public async Task Subscribe_WhenShowTagValuesFalse_DeliversRedactedCloneWithoutMutatingSource()
{
CapturingHubContext hubContext = new();
EventsHubViewerRegistry viewers = new();
DashboardEventBroadcaster broadcaster = Create(hubContext, showTagValues: false, viewers);
EventsHubViewerRegistry viewers = WatchedSession1();
using DashboardEventBroadcaster broadcaster = Create(hubContext, showTagValues: false, viewers);
using IDashboardEventSubscription subscription = broadcaster.Subscribe("session-1");
MxEvent source = BuildEventWithValue();
broadcaster.Publish("session-1", source);
await hubContext.WaitForSendCountAsync(1);
MxEvent received = ReadOne(subscription);
Assert.Null(received.Value);
@@ -153,17 +226,16 @@ public sealed class DashboardEventBroadcasterTests
Assert.NotNull(source.OnAlarmTransition.LimitValue);
}
/// <summary>An in-process subscription opens the viewer gate the same way a hub client does.</summary>
/// <summary>
/// An in-process subscription opens the viewer gate the same way a hub client does,
/// but starts no hub pump: with no remote connection there is nobody to send to.
/// </summary>
[Fact]
public void Subscribe_OpensTheViewerGate()
{
CapturingHubContext hubContext = new();
EventsHubViewerRegistry viewers = new();
DashboardEventBroadcaster broadcaster = Create(hubContext, showTagValues: false, viewers);
broadcaster.Publish("session-1", BuildEventWithValue());
Assert.Equal(0, hubContext.SendCount);
Assert.Null(hubContext.LastArgument);
using DashboardEventBroadcaster broadcaster = Create(hubContext, showTagValues: false, viewers);
using IDashboardEventSubscription subscription = broadcaster.Subscribe("session-1");
@@ -171,8 +243,9 @@ public sealed class DashboardEventBroadcasterTests
broadcaster.Publish("session-1", BuildEventWithValue());
Assert.Equal(1, hubContext.SendCount);
Assert.NotNull(hubContext.LastArgument);
Assert.Equal("session-1", ReadOne(subscription).SessionId);
Assert.Equal(0, broadcaster.ActiveHubPumpCount);
Assert.Equal(0, hubContext.SendCount);
}
/// <summary>Disposing the last in-process subscription restores the no-viewers short-circuit.</summary>
@@ -181,11 +254,10 @@ public sealed class DashboardEventBroadcasterTests
{
CapturingHubContext hubContext = new();
EventsHubViewerRegistry viewers = new();
DashboardEventBroadcaster broadcaster = Create(hubContext, showTagValues: false, viewers);
using DashboardEventBroadcaster broadcaster = Create(hubContext, showTagValues: false, viewers);
IDashboardEventSubscription subscription = broadcaster.Subscribe("session-1");
broadcaster.Publish("session-1", BuildEventWithValue());
Assert.Equal(1, hubContext.SendCount);
Assert.Equal("session-1", ReadOne(subscription).SessionId);
subscription.Dispose();
@@ -194,26 +266,28 @@ public sealed class DashboardEventBroadcasterTests
broadcaster.Publish("session-1", BuildEventWithValue());
Assert.Equal(1, hubContext.SendCount);
Assert.Equal(0, hubContext.SendCount);
Assert.False(subscription.Reader.TryRead(out _));
}
/// <summary>Hub viewers and in-process subscribers are audiences of their own session only.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public void Publish_DeliversOnlyToTheSubscribedSessionsAudience()
public async Task Publish_DeliversOnlyToTheSubscribedSessionsAudience()
{
CapturingHubContext hubContext = new();
EventsHubViewerRegistry viewers = new();
viewers.AddViewer("conn-1", "session-1");
DashboardEventBroadcaster broadcaster = Create(hubContext, showTagValues: false, viewers);
using DashboardEventBroadcaster broadcaster = Create(hubContext, showTagValues: false, viewers);
using IDashboardEventSubscription subscription = broadcaster.Subscribe("session-2");
// The hub viewer's session must not spill into the in-process feed.
broadcaster.Publish("session-1", BuildEventWithValue());
Assert.Equal(1, hubContext.SendCount);
await hubContext.WaitForSendCountAsync(1);
Assert.False(subscription.Reader.TryRead(out _));
// The in-process-only session reaches its subscriber and starts no hub pump.
broadcaster.Publish("session-2", BuildEventWithValue("session-2"));
Assert.Equal("session-2", ReadOne(subscription).SessionId);
@@ -221,7 +295,8 @@ public sealed class DashboardEventBroadcasterTests
// A session with no audience at all still short-circuits.
broadcaster.Publish("session-3", BuildEventWithValue("session-3"));
Assert.Equal(2, hubContext.SendCount);
Assert.Equal(1, broadcaster.ActiveHubPumpCount);
Assert.Equal(1, hubContext.SendCount);
}
/// <summary>Disposing twice is a no-op and cannot release a sibling subscription's registration.</summary>
@@ -230,7 +305,7 @@ public sealed class DashboardEventBroadcasterTests
{
CapturingHubContext hubContext = new();
EventsHubViewerRegistry viewers = new();
DashboardEventBroadcaster broadcaster = Create(hubContext, showTagValues: false, viewers);
using DashboardEventBroadcaster broadcaster = Create(hubContext, showTagValues: false, viewers);
IDashboardEventSubscription first = broadcaster.Subscribe("session-1");
using IDashboardEventSubscription second = broadcaster.Subscribe("session-1");
@@ -241,7 +316,6 @@ public sealed class DashboardEventBroadcasterTests
broadcaster.Publish("session-1", BuildEventWithValue());
Assert.Equal(1, hubContext.SendCount);
Assert.False(first.Reader.TryRead(out _));
Assert.Equal("session-1", ReadOne(second).SessionId);
}
@@ -258,7 +332,8 @@ public sealed class DashboardEventBroadcasterTests
private static DashboardEventBroadcaster Create(
CapturingHubContext hubContext,
bool showTagValues,
EventsHubViewerRegistry viewers)
EventsHubViewerRegistry viewers,
TimeSpan? hubSendTimeout = null)
{
GatewayOptions gatewayOptions = new()
{
@@ -269,7 +344,21 @@ public sealed class DashboardEventBroadcasterTests
hubContext,
viewers,
Options.Create(gatewayOptions),
NullLogger<DashboardEventBroadcaster>.Instance);
NullLogger<DashboardEventBroadcaster>.Instance,
hubSendTimeout ?? TimeSpan.FromSeconds(30));
}
/// <summary>Builds a value-less event whose item handle carries its publish order.</summary>
/// <param name="sequence">Publish order stamped into <see cref="MxEvent.ItemHandle"/>.</param>
/// <returns>The event.</returns>
private static MxEvent BuildSequencedEvent(int sequence)
{
return new MxEvent
{
Family = MxEventFamily.OnDataChange,
SessionId = "session-1",
ItemHandle = sequence,
};
}
/// <summary>A registry with one hub connection watching <c>session-1</c>.</summary>
@@ -313,81 +402,157 @@ public sealed class DashboardEventBroadcasterTests
/// <summary>Gets the group manager.</summary>
public IGroupManager Groups { get; } = new NoopGroupManager();
/// <summary>Gets the capturing proxy every client lookup resolves to.</summary>
public CapturingClientProxy Proxy => _clients.Proxy;
/// <summary>Gets the first argument of the most recent send call.</summary>
public object? LastArgument => _clients.GroupProxy.LastArgument;
public object? LastArgument => _clients.Proxy.LastArgument;
/// <summary>Gets the number of send calls this fake has observed.</summary>
public int SendCount => _clients.GroupProxy.SendCount;
public int SendCount => _clients.Proxy.SendCount;
/// <summary>Waits until at least <paramref name="count"/> sends have started.</summary>
/// <param name="count">The send count to wait for.</param>
/// <returns>A task that completes once the count is reached.</returns>
public Task WaitForSendCountAsync(int count) =>
WaitUntilAsync(() => SendCount >= count, $"{count} sends (saw {SendCount})");
/// <summary>Waits until a send carrying the given item handle has started.</summary>
/// <param name="itemHandle">Item handle of the event to wait for.</param>
/// <returns>A task that completes once the event was sent.</returns>
public Task WaitForLastItemHandleAsync(int itemHandle) =>
WaitUntilAsync(
() => LastArgument is MxEvent sent && sent.ItemHandle == itemHandle,
$"a send of item handle {itemHandle}");
private static async Task WaitUntilAsync(Func<bool> condition, string description)
{
DateTime deadline = DateTime.UtcNow.AddSeconds(10);
while (!condition())
{
if (DateTime.UtcNow > deadline)
{
Assert.Fail($"Timed out waiting for {description}.");
}
await Task.Delay(5);
}
}
}
private sealed class CapturingHubClients : IHubClients
{
/// <summary>Gets the capturing client proxy shared by this fake.</summary>
public CapturingClientProxy GroupProxy { get; } = new();
public CapturingClientProxy Proxy { get; } = new();
/// <summary>Gets the shared capturing proxy for all connections.</summary>
public IClientProxy All => GroupProxy;
public IClientProxy All => Proxy;
/// <summary>Returns the shared capturing proxy, ignoring exclusions.</summary>
/// <param name="excludedConnectionIds">Ignored.</param>
/// <returns>The shared capturing proxy.</returns>
public IClientProxy AllExcept(IReadOnlyList<string> excludedConnectionIds) => GroupProxy;
public IClientProxy AllExcept(IReadOnlyList<string> excludedConnectionIds) => Proxy;
/// <summary>Returns the shared capturing proxy for any connection.</summary>
/// <param name="connectionId">Ignored.</param>
/// <returns>The shared capturing proxy.</returns>
public IClientProxy Client(string connectionId) => GroupProxy;
public IClientProxy Client(string connectionId) => Proxy;
/// <summary>Returns the shared capturing proxy for any connection set.</summary>
/// <param name="connectionIds">Ignored.</param>
/// <returns>The shared capturing proxy.</returns>
public IClientProxy Clients(IReadOnlyList<string> connectionIds) => GroupProxy;
public IClientProxy Clients(IReadOnlyList<string> connectionIds) => Proxy;
/// <summary>Returns the shared capturing proxy for any group.</summary>
/// <param name="groupName">Ignored.</param>
/// <returns>The shared capturing proxy.</returns>
public IClientProxy Group(string groupName) => GroupProxy;
public IClientProxy Group(string groupName) => Proxy;
/// <summary>Returns the shared capturing proxy for any group, ignoring exclusions.</summary>
/// <param name="groupName">Ignored.</param>
/// <param name="excludedConnectionIds">Ignored.</param>
/// <returns>The shared capturing proxy.</returns>
public IClientProxy GroupExcept(string groupName, IReadOnlyList<string> excludedConnectionIds) => GroupProxy;
public IClientProxy GroupExcept(string groupName, IReadOnlyList<string> excludedConnectionIds) => Proxy;
/// <summary>Returns the shared capturing proxy for any group set.</summary>
/// <param name="groupNames">Ignored.</param>
/// <returns>The shared capturing proxy.</returns>
public IClientProxy Groups(IReadOnlyList<string> groupNames) => GroupProxy;
public IClientProxy Groups(IReadOnlyList<string> groupNames) => Proxy;
/// <summary>Returns the shared capturing proxy for any user.</summary>
/// <param name="userId">Ignored.</param>
/// <returns>The shared capturing proxy.</returns>
public IClientProxy User(string userId) => GroupProxy;
public IClientProxy User(string userId) => Proxy;
/// <summary>Returns the shared capturing proxy for any user set.</summary>
/// <param name="userIds">Ignored.</param>
/// <returns>The shared capturing proxy.</returns>
public IClientProxy Users(IReadOnlyList<string> userIds) => GroupProxy;
public IClientProxy Users(IReadOnlyList<string> userIds) => Proxy;
}
private sealed class CapturingClientProxy : IClientProxy
{
private int _sendCount;
private int _inFlight;
private int _maxConcurrentSends;
private object? _lastArgument;
/// <summary>
/// Gets or sets a gate every send waits on before completing; null completes
/// sends synchronously. Set it before the first publish.
/// </summary>
public TaskCompletionSource? Gate { get; set; }
/// <summary>Gets or sets a value indicating whether a gated send ignores its cancellation token.</summary>
public bool IgnoreCancellation { get; set; }
/// <summary>Gets the first argument of the most recent send call.</summary>
public object? LastArgument { get; private set; }
public object? LastArgument => Volatile.Read(ref _lastArgument);
/// <summary>Gets the number of send calls made through this proxy.</summary>
public int SendCount { get; private set; }
public int SendCount => Volatile.Read(ref _sendCount);
/// <summary>Records the send call arguments and completes synchronously.</summary>
/// <summary>Gets the highest number of sends that were in flight at once.</summary>
public int MaxConcurrentSends => Volatile.Read(ref _maxConcurrentSends);
/// <summary>Records the send call arguments and completes once the gate (if any) opens.</summary>
/// <param name="method">The SignalR method name.</param>
/// <param name="args">The method arguments.</param>
/// <param name="cancellationToken">Token to observe for cancellation.</param>
/// <returns>A completed task.</returns>
/// <returns>A task that completes when the send is released.</returns>
public Task SendCoreAsync(string method, object?[] args, CancellationToken cancellationToken = default)
{
SendCount++;
LastArgument = args.Length > 0 ? args[0] : null;
return Task.CompletedTask;
Volatile.Write(ref _lastArgument, args.Length > 0 ? args[0] : null);
Interlocked.Increment(ref _sendCount);
TaskCompletionSource? gate = Gate;
if (gate is null || gate.Task.IsCompleted)
{
return Task.CompletedTask;
}
return TrackAsync(gate.Task, IgnoreCancellation ? CancellationToken.None : cancellationToken);
}
private async Task TrackAsync(Task gate, CancellationToken cancellationToken)
{
int inFlight = Interlocked.Increment(ref _inFlight);
int observed;
do
{
observed = Volatile.Read(ref _maxConcurrentSends);
}
while (inFlight > observed
&& Interlocked.CompareExchange(ref _maxConcurrentSends, inFlight, observed) != observed);
try
{
await gate.WaitAsync(cancellationToken);
}
finally
{
Interlocked.Decrement(ref _inFlight);
}
}
}
@@ -1,6 +1,9 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components.Server;
using Microsoft.AspNetCore.Components.Server.Circuits;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using ZB.MOM.WW.MxGateway.Server;
using ZB.MOM.WW.MxGateway.Server.Dashboard;
using ZB.MOM.WW.MxGateway.Server.Dashboard.Hubs;
@@ -61,4 +64,42 @@ public sealed class DashboardHubsRegistrationTests
Assert.Same(broadcaster, subscriber);
Assert.Same(app.Services.GetRequiredService<DashboardEventBroadcaster>(), broadcaster);
}
/// <summary>
/// A closed tab's circuit (and its Browse poll) is retained for 30 seconds, not the
/// framework's three minutes, and each circuit gets a connection tracker that is also
/// its <see cref="CircuitHandler"/>, so the Browse poll can pause while disconnected.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task Build_WhenDashboardEnabled_ShortensCircuitRetentionAndTracksConnection()
{
await using WebApplication app = GatewayApplication.Build([]);
CircuitOptions circuitOptions = app.Services.GetRequiredService<IOptions<CircuitOptions>>().Value;
Assert.Equal(TimeSpan.FromSeconds(30), circuitOptions.DisconnectedCircuitRetentionPeriod);
Assert.Equal(DashboardServiceCollectionExtensions.DisconnectedCircuitRetention, circuitOptions.DisconnectedCircuitRetentionPeriod);
using IServiceScope scope = app.Services.CreateScope();
DashboardCircuitConnectionTracker tracker = scope.ServiceProvider.GetRequiredService<DashboardCircuitConnectionTracker>();
Assert.Contains(scope.ServiceProvider.GetServices<CircuitHandler>(), handler => ReferenceEquals(handler, tracker));
Assert.True(tracker.IsConnected);
await tracker.OnConnectionDownAsync(null!, CancellationToken.None);
Assert.False(tracker.IsConnected);
await tracker.OnConnectionUpAsync(null!, CancellationToken.None);
Assert.True(tracker.IsConnected);
}
/// <summary>The Browse filter index is one shared instance, built once per Galaxy deploy.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task Build_WhenDashboardEnabled_SharesOneBrowseSearchIndex()
{
await using WebApplication app = GatewayApplication.Build([]);
Assert.Same(
app.Services.GetRequiredService<DashboardBrowseSearchIndex>(),
app.Services.GetRequiredService<DashboardBrowseSearchIndex>());
}
}
@@ -2,6 +2,7 @@ using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Time.Testing;
using ZB.MOM.WW.MxGateway.Contracts.Proto;
using ZB.MOM.WW.MxGateway.Server.Configuration;
using ZB.MOM.WW.MxGateway.Server.Dashboard;
@@ -135,6 +136,69 @@ public sealed class DashboardLiveDataServiceTests
Assert.Equal(5, worker.UnsubscribedHandles.Count);
}
/// <summary>
/// Verifies the per-viewer share: with two active circuits each owns half the cap,
/// so a second circuit's large read evicts only the first circuit's excess above
/// its share — never its whole set — and keeps every tag it read itself.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task ReadAsync_WithTwoViewers_EvictsOnlyTheOtherViewersExcessOverItsShare()
{
RecordingWorkerClient worker = new();
await using FakeSessionManager sessionManager = new(worker);
await using DashboardLiveDataService service = CreateService(sessionManager);
string[] first = CreateTagAddresses(200, "First");
string[] second = CreateTagAddresses(200, "Second");
await service.ReadAsync(first, "circuit-a", CancellationToken.None);
DashboardLiveReadResult result = await service.ReadAsync(second, "circuit-b", CancellationToken.None);
Assert.Null(result.Error);
// Two active viewers -> share of 128 each. circuit-a drops its 72 least-recently-read
// tags and keeps 128; under the old global LRU it lost 144 (400 - 256).
int share = MaxSubscribedTags / 2;
Assert.Equal(first[..(first.Length - share)].Select(worker.HandleFor), worker.UnsubscribedHandles);
Assert.DoesNotContain(worker.UnsubscribedHandles, handle => second.Select(worker.HandleFor).Contains(handle));
// circuit-a re-reading its retained tags subscribes nothing new and evicts nothing.
worker.UnsubscribedHandles.Clear();
int subscribedBefore = worker.SubscribedTags.Count;
await service.ReadAsync(first[^share..], "circuit-a", CancellationToken.None);
Assert.Equal(subscribedBefore, worker.SubscribedTags.Count);
Assert.Empty(worker.UnsubscribedHandles);
}
/// <summary>
/// Verifies an inactive viewer (a closed circuit that stopped polling) loses its
/// share: its tags are the first eviction candidates once the activity window passes.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task ReadAsync_WhenAViewerGoesInactive_ItsTagsAreEvictedFirst()
{
RecordingWorkerClient worker = new();
await using FakeSessionManager sessionManager = new(worker);
FakeTimeProvider clock = new();
await using DashboardLiveDataService service = CreateService(sessionManager, timeProvider: clock);
string[] stale = CreateTagAddresses(100, "Stale");
string[] live = CreateTagAddresses(156, "Live");
await service.ReadAsync(live, "open-circuit", CancellationToken.None);
await service.ReadAsync(stale, "closed-circuit", CancellationToken.None);
Assert.Empty(worker.UnsubscribedHandles);
clock.Advance(TimeSpan.FromMinutes(1));
// The stale tags were read after the live ones, so a global LRU would evict live[10]
// (the oldest tag this read does not touch). The stale owner is inactive, though, so
// its share is zero and its oldest tag goes first.
await service.ReadAsync([.. live[..10], "Live.New.PV"], "open-circuit", CancellationToken.None);
Assert.Equal([worker.HandleFor(stale[0])], worker.UnsubscribedHandles);
}
/// <summary>
/// Verifies a tag the worker failed to advise still occupies a slot but is evicted
/// without any unsubscribe command — there is no item handle to unadvise.
@@ -280,7 +344,8 @@ public sealed class DashboardLiveDataServiceTests
private static DashboardLiveDataService CreateService(
ISessionManager sessionManager,
bool showTagValues = false)
bool showTagValues = false,
TimeProvider? timeProvider = null)
{
GatewayOptions gatewayOptions = new()
{
@@ -291,7 +356,8 @@ public sealed class DashboardLiveDataServiceTests
sessionManager,
new FakeGatewayAlarmService(),
Options.Create(gatewayOptions),
NullLogger<DashboardLiveDataService>.Instance);
NullLogger<DashboardLiveDataService>.Instance,
timeProvider);
}
private static string[] CreateTagAddresses(int count, string prefix = "Tank")
@@ -138,6 +138,35 @@ public sealed class DashboardSnapshotPublisherTests
Assert.True(snapshotService.PullCount >= 1);
}
/// <summary>
/// A broadcast that never completes — one remote client whose transport is wedged —
/// is abandoned at the send timeout, so later ticks still go out instead of every
/// viewer waiting on the slowest one.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task ExecuteAsync_WhenABroadcastNeverCompletes_LaterTicksStillGoOut()
{
CountingSnapshotService snapshotService = new();
RecordingHubContext hubContext = new();
hubContext.Proxy.StallFirstSend = true;
DashboardSnapshotPublisher publisher = new(
snapshotService,
hubContext,
ConnectedCounter(),
NullLogger<DashboardSnapshotPublisher>.Instance,
TimeSpan.FromMilliseconds(50),
IdlePollInterval,
sendTimeout: TimeSpan.FromMilliseconds(50));
using CancellationTokenSource cts = new();
await publisher.StartAsync(cts.Token).WaitAsync(TestTimeout);
await WaitUntilAsync(() => hubContext.SendCount >= 3);
await cts.CancelAsync();
await publisher.StopAsync(CancellationToken.None);
}
/// <summary>Creates a connection counter that already has one live viewer.</summary>
/// <returns>A counter reporting a single connection.</returns>
private static DashboardSnapshotHubConnectionCounter ConnectedCounter()
@@ -271,6 +300,9 @@ public sealed class DashboardSnapshotPublisherTests
/// <summary>Gets the group manager.</summary>
public IGroupManager Groups { get; } = new NoopGroupManager();
/// <summary>Gets the recording proxy every client lookup resolves to.</summary>
public RecordingClientProxy Proxy => _clients.AllProxy;
/// <summary>Gets the number of send calls recorded.</summary>
public int SendCount => _clients.AllProxy.SendCount;
}
@@ -329,6 +361,12 @@ public sealed class DashboardSnapshotPublisherTests
{
private int _sendCount;
/// <summary>
/// Gets or sets a value indicating whether the first send never completes and
/// ignores its cancellation token — a wedged client transport.
/// </summary>
public bool StallFirstSend { get; set; }
/// <summary>Gets the number of send calls recorded.</summary>
public int SendCount => Volatile.Read(ref _sendCount);
@@ -339,8 +377,8 @@ public sealed class DashboardSnapshotPublisherTests
/// <returns>A task that represents the asynchronous operation.</returns>
public Task SendCoreAsync(string method, object?[] args, CancellationToken cancellationToken = default)
{
Interlocked.Increment(ref _sendCount);
return Task.CompletedTask;
int count = Interlocked.Increment(ref _sendCount);
return StallFirstSend && count == 1 ? new TaskCompletionSource().Task : Task.CompletedTask;
}
}
@@ -1,4 +1,6 @@
using System.Globalization;
using System.Text.Json;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Time.Testing;
using ZB.MOM.WW.Auth.Abstractions.ApiKeys;
@@ -18,7 +20,8 @@ namespace ZB.MOM.WW.MxGateway.Tests.Gateway.Dashboard;
public sealed class DashboardSnapshotServiceTests
{
/// <summary>
/// Verifies snapshot returns empty collections and healthy status when registry is empty.
/// Verifies snapshot returns empty collections when registry is empty, and an
/// <c>Unknown</c> status when no health check service is available to evaluate.
/// </summary>
[Fact]
public void GetSnapshot_WhenRegistryEmpty_ReturnsEmptyOperationalState()
@@ -32,10 +35,76 @@ public sealed class DashboardSnapshotServiceTests
Assert.Empty(snapshot.Workers);
Assert.Empty(snapshot.Faults);
Assert.Contains(snapshot.Metrics, metric => metric.Name == "mxgateway.sessions.open" && metric.Value == 0);
Assert.Equal("Healthy", snapshot.GatewayStatus);
Assert.Equal(DashboardSnapshotService.UnknownStatus, snapshot.GatewayStatus);
Assert.NotNull(snapshot.Configuration);
}
/// <summary>
/// SEC-27: the gateway status is the aggregate of the registered health checks,
/// evaluated on the snapshot tick, not a hardcoded "Healthy".
/// </summary>
/// <param name="status">The aggregate status the health checks report.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
[Theory]
[InlineData(HealthStatus.Healthy)]
[InlineData(HealthStatus.Degraded)]
[InlineData(HealthStatus.Unhealthy)]
public async Task WatchSnapshotsAsync_ReportsTheHealthCheckStatus(HealthStatus status)
{
using GatewayMetrics metrics = new();
StubHealthCheckService health = new(status);
DashboardSnapshotService service = CreateService(new SessionRegistry(), metrics, healthCheckService: health);
using CancellationTokenSource cancellation = new();
await using IAsyncEnumerator<DashboardSnapshot> enumerator = service
.WatchSnapshotsAsync(cancellation.Token)
.GetAsyncEnumerator(cancellation.Token);
Assert.True(await enumerator.MoveNextAsync().AsTask().WaitAsync(TimeSpan.FromSeconds(5)));
Assert.Equal(status.ToString(), enumerator.Current.GatewayStatus);
// Cached for the synchronous seed path too, without a second evaluation.
Assert.Equal(status.ToString(), service.GetSnapshot().GatewayStatus);
Assert.Equal(1, health.CheckCount);
}
/// <summary>A health evaluation that throws is reported as Unknown, never as Healthy.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task WatchSnapshotsAsync_WhenHealthEvaluationThrows_ReportsUnknown()
{
using GatewayMetrics metrics = new();
StubHealthCheckService health = new(HealthStatus.Healthy) { Throw = true };
DashboardSnapshotService service = CreateService(new SessionRegistry(), metrics, healthCheckService: health);
using CancellationTokenSource cancellation = new();
await using IAsyncEnumerator<DashboardSnapshot> enumerator = service
.WatchSnapshotsAsync(cancellation.Token)
.GetAsyncEnumerator(cancellation.Token);
Assert.True(await enumerator.MoveNextAsync().AsTask().WaitAsync(TimeSpan.FromSeconds(5)));
Assert.Equal(DashboardSnapshotService.UnknownStatus, enumerator.Current.GatewayStatus);
}
/// <summary>
/// The startup-static configuration is not part of the serialized snapshot, so the
/// hub tick carries only volatile state; the hub sends it once per connection.
/// </summary>
[Fact]
public void GetSnapshot_WhenSerialized_OmitsTheConfiguration()
{
using GatewayMetrics metrics = new();
DashboardSnapshotService service = CreateService(new SessionRegistry(), metrics);
DashboardSnapshot snapshot = service.GetSnapshot();
using JsonDocument json = JsonDocument.Parse(JsonSerializer.Serialize(snapshot, JsonSerializerOptions.Web));
Assert.NotNull(snapshot.Configuration);
Assert.False(json.RootElement.TryGetProperty("configuration", out _));
Assert.True(json.RootElement.TryGetProperty("gatewayStatus", out _));
Assert.True(json.RootElement.TryGetProperty("sessions", out _));
}
/// <summary>
/// Verifies snapshot projects active, faulted, and closed session states with worker and metrics data.
/// </summary>
@@ -615,7 +684,8 @@ public sealed class DashboardSnapshotServiceTests
GatewayOptions? options = null,
IGalaxyHierarchyCache? galaxyHierarchyCache = null,
IApiKeyAdminStore? apiKeyAdminStore = null,
TimeProvider? timeProvider = null)
TimeProvider? timeProvider = null,
HealthCheckService? healthCheckService = null)
{
GatewayOptions resolvedOptions = options ?? new GatewayOptions
{
@@ -633,7 +703,38 @@ public sealed class DashboardSnapshotServiceTests
galaxyHierarchyCache ?? new StubGalaxyHierarchyCache(GalaxyHierarchyCacheEntry.Empty),
apiKeyAdminStore ?? new FakeApiKeyAdminStore(),
Options.Create(resolvedOptions),
timeProvider);
timeProvider,
healthCheckService: healthCheckService);
}
private sealed class StubHealthCheckService(HealthStatus status) : HealthCheckService
{
private int _checkCount;
/// <summary>Gets or sets a value indicating whether the evaluation throws.</summary>
public bool Throw { get; set; }
/// <summary>Gets the number of evaluations requested.</summary>
public int CheckCount => Volatile.Read(ref _checkCount);
/// <inheritdoc />
public override Task<HealthReport> CheckHealthAsync(
Func<HealthCheckRegistration, bool>? predicate,
CancellationToken cancellationToken = default)
{
Interlocked.Increment(ref _checkCount);
if (Throw)
{
throw new InvalidOperationException("health evaluation failed");
}
return Task.FromResult(new HealthReport(
new Dictionary<string, HealthReportEntry>
{
["stub"] = new(status, null, TimeSpan.Zero, null, null),
},
TimeSpan.Zero));
}
}
private sealed class StubGalaxyHierarchyCache(GalaxyHierarchyCacheEntry current) : IGalaxyHierarchyCache
@@ -164,4 +164,48 @@ public sealed class EventsHubViewerRegistryTests
Assert.False(registry.HasViewers("session-1"));
}
/// <summary>In-process subscribers count as viewers but not as hub viewers.</summary>
[Fact]
public void HasHubViewers_CountsOnlyRemoteConnections()
{
EventsHubViewerRegistry registry = new();
registry.AddViewer(EventsHubViewerRegistry.InProcessConnectionIdPrefix + "a", "session-1");
Assert.True(registry.HasViewers("session-1"));
Assert.False(registry.HasHubViewers("session-1"));
registry.AddViewer("conn-1", "session-1");
Assert.True(registry.HasHubViewers("session-1"));
}
/// <summary>
/// The released event fires once, when the last hub viewer leaves — whether it
/// unsubscribes or disconnects — and never for in-process subscribers.
/// </summary>
[Fact]
public void HubViewersReleased_FiresWhenTheLastHubViewerLeaves()
{
EventsHubViewerRegistry registry = new();
List<string> released = [];
registry.HubViewersReleased += released.Add;
registry.AddViewer("conn-1", "session-1");
registry.AddViewer("conn-2", "session-1");
registry.AddViewer(EventsHubViewerRegistry.InProcessConnectionIdPrefix + "a", "session-1");
registry.RemoveViewer("conn-1", "session-1");
Assert.Empty(released);
registry.ReleaseConnection("conn-2");
Assert.Equal(["session-1"], released);
Assert.False(registry.HasHubViewers("session-1"));
// The in-process subscriber still keeps the session watched, and leaving raises nothing.
Assert.True(registry.HasViewers("session-1"));
registry.RemoveViewer(EventsHubViewerRegistry.InProcessConnectionIdPrefix + "a", "session-1");
Assert.Equal(["session-1"], released);
}
}