test: fake time for dashboard snapshot publisher; signal-based waits (mxaccessgw#205)
DashboardSnapshotPublisher takes an optional TimeProvider (default TimeProvider.System) driving the idle poll, the reconnect delay and, via BoundedHubSend, the send timeout. Its tests now run on a fake clock with exact assertions (reconnect gap == delay, 25 idle polls pull nothing, a wedged send is abandoned at the fake send timeout). DashboardSnapshotFeed has no time dependence, so no seam was added; its tests lose the 100 ms sleep and poll loops in favour of CountSignal signals. The Task.Delay(25) poll copies in the alarm-monitor tests and several 5-10 ms poll loops now share TestSupport/Eventually. Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
@@ -22,6 +22,7 @@ internal static class BoundedHubSend
|
||||
/// <param name="argument">The single message argument.</param>
|
||||
/// <param name="timeout">Upper bound on the send.</param>
|
||||
/// <param name="stoppingToken">Host shutdown token; its cancellation propagates.</param>
|
||||
/// <param name="timeProvider">Clock the timeout runs on; defaults to <see cref="TimeProvider.System"/>.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true"/> when the send completed in time; <see langword="false"/>
|
||||
/// when it was abandoned at the timeout.
|
||||
@@ -32,10 +33,11 @@ internal static class BoundedHubSend
|
||||
string method,
|
||||
object argument,
|
||||
TimeSpan timeout,
|
||||
CancellationToken stoppingToken)
|
||||
CancellationToken stoppingToken,
|
||||
TimeProvider? timeProvider = null)
|
||||
{
|
||||
using CancellationTokenSource bounded = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken);
|
||||
bounded.CancelAfter(timeout);
|
||||
using CancellationTokenSource timeoutSource = new(timeout, timeProvider ?? TimeProvider.System);
|
||||
using CancellationTokenSource bounded = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken, timeoutSource.Token);
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
@@ -48,18 +48,21 @@ public sealed class DashboardSnapshotPublisher : BackgroundService
|
||||
private readonly TimeSpan _reconnectDelay;
|
||||
private readonly TimeSpan _idlePollInterval;
|
||||
private readonly TimeSpan _sendTimeout;
|
||||
private readonly TimeProvider _timeProvider;
|
||||
|
||||
/// <summary>Initializes a new instance of the DashboardSnapshotPublisher class.</summary>
|
||||
/// <param name="snapshotService">The snapshot service to subscribe to.</param>
|
||||
/// <param name="hubContext">The SignalR hub context for broadcasting.</param>
|
||||
/// <param name="connectionCounter">Live snapshot hub connection count used to idle-gate the tick.</param>
|
||||
/// <param name="logger">The logger instance.</param>
|
||||
/// <param name="timeProvider">Clock for the idle poll, reconnect delay and send timeout; defaults to <see cref="TimeProvider.System"/>.</param>
|
||||
public DashboardSnapshotPublisher(
|
||||
IDashboardSnapshotService snapshotService,
|
||||
IHubContext<DashboardSnapshotHub> hubContext,
|
||||
DashboardSnapshotHubConnectionCounter connectionCounter,
|
||||
ILogger<DashboardSnapshotPublisher> logger)
|
||||
: this(snapshotService, hubContext, connectionCounter, logger, DefaultReconnectDelay, DefaultIdlePollInterval)
|
||||
ILogger<DashboardSnapshotPublisher> logger,
|
||||
TimeProvider? timeProvider = null)
|
||||
: this(snapshotService, hubContext, connectionCounter, logger, DefaultReconnectDelay, DefaultIdlePollInterval, null, timeProvider)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -75,6 +78,7 @@ public sealed class DashboardSnapshotPublisher : BackgroundService
|
||||
/// <param name="reconnectDelay">The delay before reconnecting after a subscription failure.</param>
|
||||
/// <param name="idlePollInterval">How often the idle publisher re-checks for a connected viewer.</param>
|
||||
/// <param name="sendTimeout">Upper bound on one broadcast; defaults to one second.</param>
|
||||
/// <param name="timeProvider">Clock for the idle poll, reconnect delay and send timeout; defaults to <see cref="TimeProvider.System"/>.</param>
|
||||
internal DashboardSnapshotPublisher(
|
||||
IDashboardSnapshotService snapshotService,
|
||||
IHubContext<DashboardSnapshotHub> hubContext,
|
||||
@@ -82,8 +86,10 @@ public sealed class DashboardSnapshotPublisher : BackgroundService
|
||||
ILogger<DashboardSnapshotPublisher> logger,
|
||||
TimeSpan reconnectDelay,
|
||||
TimeSpan idlePollInterval,
|
||||
TimeSpan? sendTimeout = null)
|
||||
TimeSpan? sendTimeout = null,
|
||||
TimeProvider? timeProvider = null)
|
||||
{
|
||||
_timeProvider = timeProvider ?? TimeProvider.System;
|
||||
_snapshotService = snapshotService;
|
||||
_hubContext = hubContext;
|
||||
_connectionCounter = connectionCounter;
|
||||
@@ -117,7 +123,7 @@ public sealed class DashboardSnapshotPublisher : BackgroundService
|
||||
// Nobody is watching: leave the producer suspended and re-check
|
||||
// shortly. The first viewer to connect resumes the tick, and is
|
||||
// seeded directly by the hub's OnConnectedAsync meanwhile.
|
||||
await Task.Delay(_idlePollInterval, stoppingToken).ConfigureAwait(false);
|
||||
await Task.Delay(_idlePollInterval, _timeProvider, stoppingToken).ConfigureAwait(false);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -135,7 +141,8 @@ public sealed class DashboardSnapshotPublisher : BackgroundService
|
||||
DashboardSnapshotHub.SnapshotMessage,
|
||||
snapshot,
|
||||
_sendTimeout,
|
||||
stoppingToken)
|
||||
stoppingToken,
|
||||
_timeProvider)
|
||||
.ConfigureAwait(false);
|
||||
if (!sent)
|
||||
{
|
||||
@@ -159,7 +166,7 @@ public sealed class DashboardSnapshotPublisher : BackgroundService
|
||||
_logger.LogWarning(ex, "Snapshot subscription faulted; reconnecting in {DelaySeconds:F1}s.", _reconnectDelay.TotalSeconds);
|
||||
try
|
||||
{
|
||||
await Task.Delay(_reconnectDelay, stoppingToken).ConfigureAwait(false);
|
||||
await Task.Delay(_reconnectDelay, _timeProvider, stoppingToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user