diff --git a/src/ZB.MOM.WW.MxGateway.Server/Dashboard/Hubs/DashboardSnapshotPublisher.cs b/src/ZB.MOM.WW.MxGateway.Server/Dashboard/Hubs/DashboardSnapshotPublisher.cs
index bfbfdf85..4128b4ef 100644
--- a/src/ZB.MOM.WW.MxGateway.Server/Dashboard/Hubs/DashboardSnapshotPublisher.cs
+++ b/src/ZB.MOM.WW.MxGateway.Server/Dashboard/Hubs/DashboardSnapshotPublisher.cs
@@ -156,6 +156,15 @@ public sealed class DashboardSnapshotPublisher : BackgroundService
_logger.LogWarning(ex, "Snapshot broadcast failed; will retry on the next snapshot tick.");
}
}
+
+ // The watch completed cleanly (or the host is stopping). Apply the same
+ // reconnect delay as the fault path; without it a source that completes
+ // immediately turns this loop into a hot spin.
+ if (!stoppingToken.IsCancellationRequested)
+ {
+ _logger.LogInformation("Snapshot subscription completed; reconnecting in {DelaySeconds:F1}s.", _reconnectDelay.TotalSeconds);
+ await Task.Delay(_reconnectDelay, _timeProvider, stoppingToken).ConfigureAwait(false);
+ }
}
catch (OperationCanceledException)
{
diff --git a/src/ZB.MOM.WW.MxGateway.Tests/Gateway/Dashboard/DashboardSnapshotPublisherTests.cs b/src/ZB.MOM.WW.MxGateway.Tests/Gateway/Dashboard/DashboardSnapshotPublisherTests.cs
index 27210cd2..e3867e15 100644
--- a/src/ZB.MOM.WW.MxGateway.Tests/Gateway/Dashboard/DashboardSnapshotPublisherTests.cs
+++ b/src/ZB.MOM.WW.MxGateway.Tests/Gateway/Dashboard/DashboardSnapshotPublisherTests.cs
@@ -68,13 +68,16 @@ public sealed class DashboardSnapshotPublisherTests
}
///
- /// Sanity: a normal completion of WatchSnapshotsAsync (no exception)
- /// is re-subscribed; the publisher exits only on host shutdown.
+ /// A clean completion of WatchSnapshotsAsync (no exception) is re-subscribed only after
+ /// the reconnect delay; without it the publisher hot-spins re-opening a source that
+ /// completes immediately. Driven by a fake clock, so nothing sleeps.
///
/// A task that represents the asynchronous operation.
[Fact]
- public async Task ExecuteAsync_WhenSnapshotServiceCompletes_Resubscribes()
+ public async Task ExecuteAsync_WhenSnapshotServiceCompletes_ResubscribesOnlyAfterReconnectDelay()
{
+ TimeSpan reconnectDelay = TimeSpan.FromMilliseconds(50);
+ TickObservingTimeProvider clock = new() { WatchedDueTime = reconnectDelay };
CompleteImmediatelySnapshotService snapshotService = new();
RecordingHubContext hubContext = new();
DashboardSnapshotPublisher publisher = new(
@@ -82,19 +85,25 @@ public sealed class DashboardSnapshotPublisherTests
hubContext,
ConnectedCounter(),
NullLogger.Instance,
- TimeSpan.FromMilliseconds(50),
+ reconnectDelay,
IdlePollInterval,
- timeProvider: new FakeTimeProvider());
+ timeProvider: clock);
using CancellationTokenSource cts = new();
await publisher.StartAsync(cts.Token).WaitAsync(TestTimeout);
+ await snapshotService.Subscribes.WaitForAsync(1);
+ await clock.WaitForWatchedTimersAsync(1, TestTimeout);
+
+ // Just short of the delay no re-subscribe may happen (a hot spin would have made many).
+ clock.Advance(reconnectDelay - TimeSpan.FromMilliseconds(1));
+ Assert.Equal(1, snapshotService.Subscribes.Count);
+
+ clock.Advance(TimeSpan.FromMilliseconds(1));
await snapshotService.Subscribes.WaitForAsync(2);
await cts.CancelAsync();
await publisher.StopAsync(CancellationToken.None);
-
- Assert.True(snapshotService.Subscribes.Count >= 2);
}
///