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
@@ -4,6 +4,7 @@ using Microsoft.Extensions.Options;
using ZB.MOM.WW.MxGateway.Contracts.Proto;
using ZB.MOM.WW.MxGateway.Server.Configuration;
using ZB.MOM.WW.MxGateway.Server.Dashboard.Hubs;
using ZB.MOM.WW.MxGateway.Tests.TestSupport;
namespace ZB.MOM.WW.MxGateway.Tests.Gateway.Dashboard;
@@ -448,19 +449,8 @@ public sealed class DashboardEventBroadcasterTests
() => 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 static Task WaitUntilAsync(Func<bool> condition, string description)
=> Eventually.WaitUntilAsync(condition, TimeSpan.FromSeconds(10), description);
}
private sealed class CapturingHubClients : IHubClients
@@ -287,11 +287,12 @@ public sealed class DashboardSnapshotFeedTests
Task<bool> joinerMove = joiner.MoveNextAsync().AsTask();
await joinerCancellation.CancelAsync();
// The joiner's unwind is a continuation of its cancelled channel read; give it time to
// run its unsubscribe before the dying pump is released. The unsubscribe cannot be
// awaited (it waits on the parked pump) and exposes no signal, so this stays a real
// delay; only the interleaving depends on it — the assertions below hold either way.
await Task.Delay(TimeSpan.FromMilliseconds(150));
// The joiner's unwind is a continuation of its cancelled channel read, and its unsubscribe
// cannot be awaited directly (it then waits on the parked pump). Wait instead for the idle
// gate it closes: the joiner started the live generation, so the gate closes only once its
// unsubscribe has cancelled that generation. A global "is the list empty" check — the
// defect under test — never closes it, because the old subscriber still lingers.
await feed.WhenIdleGateClosedAsync(CancellationToken.None).WaitAsync(TestTimeout);
service.ReleaseDisposal();
@@ -301,10 +302,12 @@ public sealed class DashboardSnapshotFeedTests
// Completes only once the joiner's unsubscribe has awaited its generation's pump.
await DrainAsync(joiner, joinerMove);
// Nobody is watching, so nothing may consume a snapshot: a surviving pump would drain
// this push within its first read.
// Nobody is watching, so no pump may be left enumerating the source...
Assert.True(feed.IsPumpIdle);
// ...and so nothing consumes a snapshot pushed now. With no pump running the push stays
// queued, so the count is checked without a settling delay.
service.Push(CreateSnapshot("orphan-check"));
await Task.Delay(TimeSpan.FromMilliseconds(150));
Assert.Equal(1, service.PendingPushCount);
// ...and the next viewer still starts cleanly, picking up the queued snapshot.