test: pump-idle signal for DashboardSnapshotFeed; fold private wait helpers into Eventually (mxaccessgw#223)

The MxGateway.Tests part of #223 (the Worker.Tests items are handled separately).

- DashboardSnapshotFeed gains two internal test seams: WhenIdleGateClosedAsync, a
  TCS-backed signal that completes once no pump generation is accepting subscribers,
  and IsPumpIdle (no live generation and the latest pump has exited). The TCS is created
  lazily by a waiter, so production never allocates it.
- WatchAsync_WhenAJoinerLeavesWhileAnOldGenerationLingers_LeavesNoPumpRunning drops both
  150 ms real sleeps: it waits on the idle-gate signal for the joiner's unsubscribe, and
  asserts IsPumpIdle before the orphan-push check, which is then immediate. Reverting the
  per-generation idle check to the old global "list is empty" check now fails the test
  (5 s TimeoutException on the gate), where before the sleep simply elapsed.
- Eventually.WaitUntilAsync/WaitForAsync take an optional description (defaulting to the
  condition's source text via CallerArgumentExpression) for the timeout message, and gain an
  async-condition overload. The hand-rolled polling loops in EventStreamServiceTests,
  GatewaySessionDashboardMirrorTests, DashboardEventBroadcasterTests and
  AlarmsPageTruncationBannerTests now delegate to it, keeping their timeouts and messages.

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 13:26:38 -05:00
parent 22964e192b
commit 09b3ecddf2
7 changed files with 123 additions and 77 deletions
@@ -53,6 +53,12 @@ public sealed class DashboardSnapshotFeed : IDashboardSnapshotFeed
/// <summary>Last generation handed out; only ever incremented under <c>_gate</c>.</summary>
private long _lastGeneration = NoGeneration;
/// <summary>
/// Completed (and cleared) the next time the live generation stops accepting subscribers;
/// created lazily by <see cref="WhenIdleGateClosedAsync"/>, so production never allocates it.
/// </summary>
private TaskCompletionSource? _idleGateClosed;
/// <summary>Initializes a new instance of the <see cref="DashboardSnapshotFeed"/> class.</summary>
/// <param name="snapshotService">Snapshot source to multicast.</param>
/// <param name="logger">Optional logger for pump faults.</param>
@@ -64,6 +70,44 @@ public sealed class DashboardSnapshotFeed : IDashboardSnapshotFeed
_logger = logger ?? NullLogger<DashboardSnapshotFeed>.Instance;
}
/// <summary>
/// Gets a value indicating whether the feed is fully idle: no generation is accepting
/// subscribers and the most recent pump has exited, so nothing is enumerating the
/// snapshot source. Test seam for the idle gate.
/// </summary>
internal bool IsPumpIdle
{
get
{
lock (_gate)
{
return _pumpCancellation is null && _pumpTask.IsCompleted;
}
}
}
/// <summary>
/// Completes once no generation is accepting subscribers — the idle gate has closed, either
/// because the last subscriber of the live generation left or because its source ended. The
/// pump that served the generation may still be unwinding; see <see cref="IsPumpIdle"/>.
/// Test seam: it lets a test wait for an unsubscribe it cannot await directly.
/// </summary>
/// <param name="cancellationToken">Bounds the wait.</param>
/// <returns>A task that completes when no generation is live.</returns>
internal Task WhenIdleGateClosedAsync(CancellationToken cancellationToken)
{
lock (_gate)
{
if (_pumpCancellation is null)
{
return Task.CompletedTask;
}
_idleGateClosed ??= new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
return _idleGateClosed.Task.WaitAsync(cancellationToken);
}
}
/// <inheritdoc />
public async IAsyncEnumerable<DashboardSnapshot> WatchAsync(
[EnumeratorCancellation] CancellationToken cancellationToken)
@@ -140,8 +184,7 @@ public sealed class DashboardSnapshotFeed : IDashboardSnapshotFeed
}
cancellation = _pumpCancellation;
_pumpCancellation = null;
_generation = NoGeneration;
CloseIdleGateLocked();
pump = _pumpTask;
}
@@ -311,8 +354,19 @@ public sealed class DashboardSnapshotFeed : IDashboardSnapshotFeed
return;
}
CloseIdleGateLocked();
}
/// <summary>
/// Stops the live generation from accepting subscribers and wakes any
/// <see cref="WhenIdleGateClosedAsync"/> waiter. Must be called while holding <c>_gate</c>.
/// </summary>
private void CloseIdleGateLocked()
{
_generation = NoGeneration;
_pumpCancellation = null;
_idleGateClosed?.TrySetResult();
_idleGateClosed = null;
}
/// <summary>