using System.Runtime.CompilerServices;
using System.Threading.Channels;
using ZB.MOM.WW.MxGateway.Server.Dashboard;
using ZB.MOM.WW.MxGateway.Tests.TestSupport;
namespace ZB.MOM.WW.MxGateway.Tests.Gateway.Dashboard;
///
/// Covers the in-process snapshot fan-out that replaced the dashboard pages'
/// loopback /hubs/snapshot connections. The invariants under test are the
/// ones that make the feed cheaper than the hub hop: exactly one underlying
/// enumeration for any
/// number of viewers, nothing at all while nobody is watching, and a slow viewer
/// that can neither buffer without bound nor stall the others.
///
public sealed class DashboardSnapshotFeedTests
{
private static readonly TimeSpan TestTimeout = TimeSpan.FromSeconds(5);
///
/// With no page subscribed, the feed must not touch the snapshot service at
/// all — no timer, no snapshot build. This is the whole point of the idle
/// gate: an unattended gateway does no dashboard work.
///
/// A task that represents the asynchronous operation.
[Fact]
public Task WatchAsync_WithNoSubscribers_NeverEnumeratesUnderlyingWatch()
{
FakeSnapshotService service = new();
DashboardSnapshotFeed feed = new(service);
// Obtaining the enumerable without enumerating it must not subscribe
// either: the pump starts on the first MoveNextAsync, not before.
_ = feed.WatchAsync(CancellationToken.None);
Assert.Equal(0, service.EnumerationCount);
return Task.CompletedTask;
}
///
/// Two viewers share one underlying enumeration and both see the same
/// pushed snapshot. Before the feed, each page opened its own SignalR
/// connection and the publisher pulled its own snapshot stream.
///
/// A task that represents the asynchronous operation.
[Fact]
public async Task WatchAsync_WithTwoSubscribers_SharesASingleUnderlyingEnumeration()
{
FakeSnapshotService service = new();
DashboardSnapshotFeed feed = new(service);
using CancellationTokenSource firstCancellation = new();
using CancellationTokenSource secondCancellation = new();
IAsyncEnumerator first =
feed.WatchAsync(firstCancellation.Token).GetAsyncEnumerator(firstCancellation.Token);
Task firstMove = first.MoveNextAsync().AsTask();
await service.Enumerations.WaitForAsync(1);
IAsyncEnumerator second =
feed.WatchAsync(secondCancellation.Token).GetAsyncEnumerator(secondCancellation.Token);
Task secondMove = second.MoveNextAsync().AsTask();
// Push until both have observed a snapshot: a subscriber only becomes
// visible to the pump once its MoveNextAsync has registered the channel,
// so a single push could race the second registration.
await PushUntilAsync(service, Task.WhenAll(firstMove, secondMove));
Assert.True(await firstMove.WaitAsync(TestTimeout));
Assert.True(await secondMove.WaitAsync(TestTimeout));
Assert.StartsWith("push-", first.Current.GatewayVersion, StringComparison.Ordinal);
Assert.StartsWith("push-", second.Current.GatewayVersion, StringComparison.Ordinal);
Assert.Equal(1, service.EnumerationCount);
await firstCancellation.CancelAsync();
await secondCancellation.CancelAsync();
await DrainAsync(first, firstMove);
await DrainAsync(second, secondMove);
}
///
/// The last viewer leaving must cancel the underlying enumeration (idle
/// gate re-armed), and the next viewer must restart it — the rapid
/// unsubscribe/resubscribe path a page navigation exercises.
///
/// A task that represents the asynchronous operation.
[Fact]
public async Task WatchAsync_WhenLastSubscriberLeaves_CancelsPumpAndRestartsForTheNext()
{
FakeSnapshotService service = new();
DashboardSnapshotFeed feed = new(service);
using CancellationTokenSource firstCancellation = new();
IAsyncEnumerator first =
feed.WatchAsync(firstCancellation.Token).GetAsyncEnumerator(firstCancellation.Token);
Task firstMove = first.MoveNextAsync().AsTask();
await service.Enumerations.WaitForAsync(1);
await firstCancellation.CancelAsync();
await DrainAsync(first, firstMove);
await service.Completions.WaitForAsync(1);
Assert.True(service.LastEnumerationWasCancelled);
using CancellationTokenSource secondCancellation = new();
IAsyncEnumerator second =
feed.WatchAsync(secondCancellation.Token).GetAsyncEnumerator(secondCancellation.Token);
Task secondMove = second.MoveNextAsync().AsTask();
await service.Enumerations.WaitForAsync(2);
Assert.Equal(2, service.EnumerationCount);
await secondCancellation.CancelAsync();
await DrainAsync(second, secondMove);
}
///
/// A viewer that is not reading must not stall the pump or accumulate
/// snapshots: its bounded channel drops the oldest, so its next read is the
/// newest snapshot the pump has broadcast, not a backlog head. The fast
/// reader's progress is what makes the assertion deterministic — once it has
/// seen the third snapshot the pump has provably broadcast all three.
///
/// A task that represents the asynchronous operation.
[Fact]
public async Task WatchAsync_WithASlowSubscriber_KeepsOnlyTheNewestSnapshot()
{
FakeSnapshotService service = new();
DashboardSnapshotFeed feed = new(service);
using CancellationTokenSource fastCancellation = new();
using CancellationTokenSource slowCancellation = new();
IAsyncEnumerator fast =
feed.WatchAsync(fastCancellation.Token).GetAsyncEnumerator(fastCancellation.Token);
Task fastMove = fast.MoveNextAsync().AsTask();
await service.Enumerations.WaitForAsync(1);
// The slow subscriber registers but never advances until the very end.
IAsyncEnumerator slow =
feed.WatchAsync(slowCancellation.Token).GetAsyncEnumerator(slowCancellation.Token);
Task slowMove = slow.MoveNextAsync().AsTask();
await PushUntilAsync(service, Task.WhenAll(fastMove, slowMove));
Assert.True(await fastMove.WaitAsync(TestTimeout));
Assert.True(await slowMove.WaitAsync(TestTimeout));
service.Push(CreateSnapshot("s1"));
service.Push(CreateSnapshot("s2"));
service.Push(CreateSnapshot("s3"));
// Drain the fast reader until it sees s3; that proves the pump broadcast
// all three to every subscriber, so the slow channel now holds exactly s3.
string fastLatest = fast.Current.GatewayVersion;
while (fastLatest != "s3")
{
Assert.True(await fast.MoveNextAsync().AsTask().WaitAsync(TestTimeout));
fastLatest = fast.Current.GatewayVersion;
}
Assert.True(await slow.MoveNextAsync().AsTask().WaitAsync(TestTimeout));
Assert.Equal("s3", slow.Current.GatewayVersion);
await fastCancellation.CancelAsync();
await slowCancellation.CancelAsync();
await DrainAsync(fast, Task.FromResult(true));
await DrainAsync(slow, Task.FromResult(true));
}
///
/// A fault in the underlying watch is surfaced to the current viewers rather
/// than silently hanging them, and it resets the feed so the next viewer
/// starts a fresh pump instead of attaching to a dead one.
///
/// A task that represents the asynchronous operation.
[Fact]
public async Task WatchAsync_WhenUnderlyingWatchFaults_PropagatesAndRestartsForTheNextSubscriber()
{
FakeSnapshotService service = new();
DashboardSnapshotFeed feed = new(service);
using CancellationTokenSource firstCancellation = new();
IAsyncEnumerator first =
feed.WatchAsync(firstCancellation.Token).GetAsyncEnumerator(firstCancellation.Token);
Task firstMove = first.MoveNextAsync().AsTask();
await service.Enumerations.WaitForAsync(1);
service.Fault(new InvalidOperationException("simulated snapshot source failure"));
InvalidOperationException failure =
await Assert.ThrowsAsync(() => firstMove.WaitAsync(TestTimeout));
Assert.Equal("simulated snapshot source failure", failure.Message);
await first.DisposeAsync();
using CancellationTokenSource secondCancellation = new();
IAsyncEnumerator second =
feed.WatchAsync(secondCancellation.Token).GetAsyncEnumerator(secondCancellation.Token);
Task secondMove = second.MoveNextAsync().AsTask();
await service.Enumerations.WaitForAsync(2);
await PushUntilAsync(service, secondMove);
Assert.True(await secondMove.WaitAsync(TestTimeout));
await secondCancellation.CancelAsync();
await DrainAsync(second, secondMove);
}
///
/// The race the generation tagging exists for: a page subscribes in the window between
/// the source failing and the dying pump detaching its subscribers. Without generations
/// the newcomer joined the doomed pump, was detached with its error, and no pump ever
/// restarted (only a first subscriber started one) — that page froze for good.
///
/// A task that represents the asynchronous operation.
[Fact]
public async Task WatchAsync_WhenASubscriberJoinsWhileAFaultedPumpUnwinds_IsServedByAFreshPump()
{
FakeSnapshotService service = new();
service.HoldDisposal();
DashboardSnapshotFeed feed = new(service);
using CancellationTokenSource firstCancellation = new();
IAsyncEnumerator first =
feed.WatchAsync(firstCancellation.Token).GetAsyncEnumerator(firstCancellation.Token);
Task firstMove = first.MoveNextAsync().AsTask();
await service.Enumerations.WaitForAsync(1);
service.Fault(new InvalidOperationException("simulated snapshot source failure"));
// The pump has observed the failure and is parked disposing the enumerator — the
// exact window in which a page used to attach itself to a doomed pump.
await service.DisposalReached.WaitAsync(TestTimeout);
using CancellationTokenSource secondCancellation = new();
IAsyncEnumerator second =
feed.WatchAsync(secondCancellation.Token).GetAsyncEnumerator(secondCancellation.Token);
// An async iterator body runs synchronously up to its first await, so the
// subscription is registered by the time MoveNextAsync hands back its task.
Task secondMove = second.MoveNextAsync().AsTask();
service.ReleaseDisposal();
// The subscriber that was there when the source broke still learns about it...
InvalidOperationException failure =
await Assert.ThrowsAsync(() => firstMove.WaitAsync(TestTimeout));
Assert.Equal("simulated snapshot source failure", failure.Message);
await first.DisposeAsync();
// ...and the one that joined mid-unwind is served by a restarted enumeration
// instead of inheriting the failure.
await service.Enumerations.WaitForAsync(2);
await PushUntilAsync(service, secondMove);
Assert.True(await secondMove.WaitAsync(TestTimeout));
Assert.StartsWith("push-", second.Current.GatewayVersion, StringComparison.Ordinal);
await secondCancellation.CancelAsync();
await DrainAsync(second, secondMove);
}
///
/// The idle gate is per generation, not per subscriber count. A viewer that joins while a
/// pump unwinds starts a new generation, and the old generation's viewers linger in the
/// list until that pump's reset runs — so a global "is the list empty" check let the new
/// viewer leave without cancelling the generation it had just started, leaving a pump
/// enumerating the snapshot source with nobody watching it.
///
/// A task that represents the asynchronous operation.
[Fact]
public async Task WatchAsync_WhenAJoinerLeavesWhileAnOldGenerationLingers_LeavesNoPumpRunning()
{
FakeSnapshotService service = new();
service.HoldDisposal();
DashboardSnapshotFeed feed = new(service);
using CancellationTokenSource oldCancellation = new();
IAsyncEnumerator oldSubscriber =
feed.WatchAsync(oldCancellation.Token).GetAsyncEnumerator(oldCancellation.Token);
Task oldMove = oldSubscriber.MoveNextAsync().AsTask();
await service.Enumerations.WaitForAsync(1);
service.Fault(new InvalidOperationException("simulated snapshot source failure"));
await service.DisposalReached.WaitAsync(TestTimeout);
// Joins mid-unwind (starting a fresh generation) and leaves again before the dying
// pump has detached the subscriber that is still lingering in the list.
using CancellationTokenSource joinerCancellation = new();
IAsyncEnumerator joiner =
feed.WatchAsync(joinerCancellation.Token).GetAsyncEnumerator(joinerCancellation.Token);
Task joinerMove = joiner.MoveNextAsync().AsTask();
await joinerCancellation.CancelAsync();
// 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();
await Assert.ThrowsAsync(() => oldMove.WaitAsync(TestTimeout));
await oldSubscriber.DisposeAsync();
// Completes only once the joiner's unsubscribe has awaited its generation's pump.
await DrainAsync(joiner, joinerMove);
// 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"));
Assert.Equal(1, service.PendingPushCount);
// ...and the next viewer still starts cleanly, picking up the queued snapshot.
int enumerationsBefore = service.EnumerationCount;
using CancellationTokenSource nextCancellation = new();
IAsyncEnumerator next =
feed.WatchAsync(nextCancellation.Token).GetAsyncEnumerator(nextCancellation.Token);
Task nextMove = next.MoveNextAsync().AsTask();
await service.Enumerations.WaitForAsync(enumerationsBefore + 1);
Assert.True(await nextMove.WaitAsync(TestTimeout));
Assert.Equal("orphan-check", next.Current.GatewayVersion);
await nextCancellation.CancelAsync();
await DrainAsync(next, nextMove);
}
/// Builds a snapshot whose version string identifies it in assertions.
/// Identity marker carried in GatewayVersion.
/// A snapshot carrying the supplied identity marker.
private static DashboardSnapshot CreateSnapshot(string version)
{
return new DashboardSnapshot(
GeneratedAt: DateTimeOffset.UnixEpoch,
GatewayStartedAt: DateTimeOffset.UnixEpoch,
GatewayUptime: TimeSpan.Zero,
GatewayStatus: "Healthy",
GatewayVersion: version,
Sessions: Array.Empty(),
Workers: Array.Empty(),
Metrics: Array.Empty(),
Faults: Array.Empty(),
ApiKeys: Array.Empty(),
Configuration: null!,
Galaxy: null!);
}
///
/// Pushes snapshots until the supplied task completes, so a test never
/// depends on a single push landing after a subscriber has registered.
///
/// Fake snapshot source to push through.
/// Task whose completion stops the pushes.
/// A task that represents the asynchronous operation.
private static async Task PushUntilAsync(FakeSnapshotService service, Task until)
{
using CancellationTokenSource cancellation = new(TestTimeout);
int sequence = 0;
while (!until.IsCompleted)
{
service.Push(CreateSnapshot($"push-{sequence++}"));
await Task.Delay(TimeSpan.FromMilliseconds(5), cancellation.Token);
}
}
///
/// Observes the cancellation of a pending enumeration and disposes the
/// enumerator, mirroring how await foreach unwinds a cancelled watch.
///
/// Enumerator to unwind.
/// The in-flight move, if any.
/// A task that represents the asynchronous operation.
private static async Task DrainAsync(IAsyncEnumerator enumerator, Task pending)
{
try
{
await pending.WaitAsync(TestTimeout);
}
catch (OperationCanceledException)
{
}
try
{
await enumerator.DisposeAsync();
}
catch (OperationCanceledException)
{
}
}
///
/// Snapshot source under the feed's control: counts enumerations, records how
/// each one ended, and lets the test drive snapshots (or a fault) into the
/// live enumeration.
///
private sealed class FakeSnapshotService : IDashboardSnapshotService
{
private readonly Channel