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:
@@ -1,6 +1,7 @@
|
||||
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;
|
||||
|
||||
@@ -23,7 +24,7 @@ public sealed class DashboardSnapshotFeedTests
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task WatchAsync_WithNoSubscribers_NeverEnumeratesUnderlyingWatch()
|
||||
public Task WatchAsync_WithNoSubscribers_NeverEnumeratesUnderlyingWatch()
|
||||
{
|
||||
FakeSnapshotService service = new();
|
||||
DashboardSnapshotFeed feed = new(service);
|
||||
@@ -31,9 +32,9 @@ public sealed class DashboardSnapshotFeedTests
|
||||
// Obtaining the enumerable without enumerating it must not subscribe
|
||||
// either: the pump starts on the first MoveNextAsync, not before.
|
||||
_ = feed.WatchAsync(CancellationToken.None);
|
||||
await Task.Delay(TimeSpan.FromMilliseconds(100));
|
||||
|
||||
Assert.Equal(0, service.EnumerationCount);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -53,7 +54,7 @@ public sealed class DashboardSnapshotFeedTests
|
||||
IAsyncEnumerator<DashboardSnapshot> first =
|
||||
feed.WatchAsync(firstCancellation.Token).GetAsyncEnumerator(firstCancellation.Token);
|
||||
Task<bool> firstMove = first.MoveNextAsync().AsTask();
|
||||
await WaitUntilAsync(() => service.EnumerationCount >= 1);
|
||||
await service.Enumerations.WaitForAsync(1);
|
||||
|
||||
IAsyncEnumerator<DashboardSnapshot> second =
|
||||
feed.WatchAsync(secondCancellation.Token).GetAsyncEnumerator(secondCancellation.Token);
|
||||
@@ -92,19 +93,19 @@ public sealed class DashboardSnapshotFeedTests
|
||||
IAsyncEnumerator<DashboardSnapshot> first =
|
||||
feed.WatchAsync(firstCancellation.Token).GetAsyncEnumerator(firstCancellation.Token);
|
||||
Task<bool> firstMove = first.MoveNextAsync().AsTask();
|
||||
await WaitUntilAsync(() => service.EnumerationCount >= 1);
|
||||
await service.Enumerations.WaitForAsync(1);
|
||||
|
||||
await firstCancellation.CancelAsync();
|
||||
await DrainAsync(first, firstMove);
|
||||
|
||||
await WaitUntilAsync(() => service.CompletedEnumerationCount >= 1);
|
||||
await service.Completions.WaitForAsync(1);
|
||||
Assert.True(service.LastEnumerationWasCancelled);
|
||||
|
||||
using CancellationTokenSource secondCancellation = new();
|
||||
IAsyncEnumerator<DashboardSnapshot> second =
|
||||
feed.WatchAsync(secondCancellation.Token).GetAsyncEnumerator(secondCancellation.Token);
|
||||
Task<bool> secondMove = second.MoveNextAsync().AsTask();
|
||||
await WaitUntilAsync(() => service.EnumerationCount >= 2);
|
||||
await service.Enumerations.WaitForAsync(2);
|
||||
|
||||
Assert.Equal(2, service.EnumerationCount);
|
||||
|
||||
@@ -131,7 +132,7 @@ public sealed class DashboardSnapshotFeedTests
|
||||
IAsyncEnumerator<DashboardSnapshot> fast =
|
||||
feed.WatchAsync(fastCancellation.Token).GetAsyncEnumerator(fastCancellation.Token);
|
||||
Task<bool> fastMove = fast.MoveNextAsync().AsTask();
|
||||
await WaitUntilAsync(() => service.EnumerationCount >= 1);
|
||||
await service.Enumerations.WaitForAsync(1);
|
||||
|
||||
// The slow subscriber registers but never advances until the very end.
|
||||
IAsyncEnumerator<DashboardSnapshot> slow =
|
||||
@@ -179,7 +180,7 @@ public sealed class DashboardSnapshotFeedTests
|
||||
IAsyncEnumerator<DashboardSnapshot> first =
|
||||
feed.WatchAsync(firstCancellation.Token).GetAsyncEnumerator(firstCancellation.Token);
|
||||
Task<bool> firstMove = first.MoveNextAsync().AsTask();
|
||||
await WaitUntilAsync(() => service.EnumerationCount >= 1);
|
||||
await service.Enumerations.WaitForAsync(1);
|
||||
|
||||
service.Fault(new InvalidOperationException("simulated snapshot source failure"));
|
||||
|
||||
@@ -192,7 +193,7 @@ public sealed class DashboardSnapshotFeedTests
|
||||
IAsyncEnumerator<DashboardSnapshot> second =
|
||||
feed.WatchAsync(secondCancellation.Token).GetAsyncEnumerator(secondCancellation.Token);
|
||||
Task<bool> secondMove = second.MoveNextAsync().AsTask();
|
||||
await WaitUntilAsync(() => service.EnumerationCount >= 2);
|
||||
await service.Enumerations.WaitForAsync(2);
|
||||
|
||||
await PushUntilAsync(service, secondMove);
|
||||
Assert.True(await secondMove.WaitAsync(TestTimeout));
|
||||
@@ -219,7 +220,7 @@ public sealed class DashboardSnapshotFeedTests
|
||||
IAsyncEnumerator<DashboardSnapshot> first =
|
||||
feed.WatchAsync(firstCancellation.Token).GetAsyncEnumerator(firstCancellation.Token);
|
||||
Task<bool> firstMove = first.MoveNextAsync().AsTask();
|
||||
await WaitUntilAsync(() => service.EnumerationCount >= 1);
|
||||
await service.Enumerations.WaitForAsync(1);
|
||||
|
||||
service.Fault(new InvalidOperationException("simulated snapshot source failure"));
|
||||
|
||||
@@ -245,7 +246,7 @@ public sealed class DashboardSnapshotFeedTests
|
||||
|
||||
// ...and the one that joined mid-unwind is served by a restarted enumeration
|
||||
// instead of inheriting the failure.
|
||||
await WaitUntilAsync(() => service.EnumerationCount >= 2);
|
||||
await service.Enumerations.WaitForAsync(2);
|
||||
await PushUntilAsync(service, secondMove);
|
||||
Assert.True(await secondMove.WaitAsync(TestTimeout));
|
||||
Assert.StartsWith("push-", second.Current.GatewayVersion, StringComparison.Ordinal);
|
||||
@@ -273,7 +274,7 @@ public sealed class DashboardSnapshotFeedTests
|
||||
IAsyncEnumerator<DashboardSnapshot> oldSubscriber =
|
||||
feed.WatchAsync(oldCancellation.Token).GetAsyncEnumerator(oldCancellation.Token);
|
||||
Task<bool> oldMove = oldSubscriber.MoveNextAsync().AsTask();
|
||||
await WaitUntilAsync(() => service.EnumerationCount >= 1);
|
||||
await service.Enumerations.WaitForAsync(1);
|
||||
|
||||
service.Fault(new InvalidOperationException("simulated snapshot source failure"));
|
||||
await service.DisposalReached.WaitAsync(TestTimeout);
|
||||
@@ -287,8 +288,9 @@ public sealed class DashboardSnapshotFeedTests
|
||||
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. Only the interleaving depends
|
||||
// on this delay — the assertions below hold either way.
|
||||
// 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));
|
||||
|
||||
service.ReleaseDisposal();
|
||||
@@ -312,7 +314,7 @@ public sealed class DashboardSnapshotFeedTests
|
||||
feed.WatchAsync(nextCancellation.Token).GetAsyncEnumerator(nextCancellation.Token);
|
||||
Task<bool> nextMove = next.MoveNextAsync().AsTask();
|
||||
|
||||
await WaitUntilAsync(() => service.EnumerationCount > enumerationsBefore);
|
||||
await service.Enumerations.WaitForAsync(enumerationsBefore + 1);
|
||||
Assert.True(await nextMove.WaitAsync(TestTimeout));
|
||||
Assert.Equal("orphan-check", next.Current.GatewayVersion);
|
||||
|
||||
@@ -384,15 +386,6 @@ public sealed class DashboardSnapshotFeedTests
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task WaitUntilAsync(Func<bool> predicate)
|
||||
{
|
||||
using CancellationTokenSource cancellation = new(TestTimeout);
|
||||
while (!predicate())
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromMilliseconds(5), cancellation.Token);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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
|
||||
@@ -403,13 +396,17 @@ public sealed class DashboardSnapshotFeedTests
|
||||
private readonly Channel<object> _pushes = Channel.CreateUnbounded<object>();
|
||||
private readonly TaskCompletionSource _disposalReached = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
private readonly TaskCompletionSource _disposalRelease = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
private int _enumerationCount;
|
||||
private int _completedEnumerationCount;
|
||||
private volatile bool _lastEnumerationWasCancelled;
|
||||
private volatile bool _holdDisposal;
|
||||
|
||||
/// <summary>Gets the number of times the feed started enumerating this source.</summary>
|
||||
public int EnumerationCount => Volatile.Read(ref _enumerationCount);
|
||||
public int EnumerationCount => Enumerations.Count;
|
||||
|
||||
/// <summary>Gets the signal raised each time the feed starts enumerating this source.</summary>
|
||||
public CountSignal Enumerations { get; } = new();
|
||||
|
||||
/// <summary>Gets the signal raised each time an enumeration finishes.</summary>
|
||||
public CountSignal Completions { get; } = new();
|
||||
|
||||
/// <summary>Gets a task that completes when a held enumerator disposal is reached.</summary>
|
||||
public Task DisposalReached => _disposalReached.Task;
|
||||
@@ -421,7 +418,7 @@ public sealed class DashboardSnapshotFeedTests
|
||||
public int PendingPushCount => _pushes.Reader.Count;
|
||||
|
||||
/// <summary>Gets the number of enumerations that have finished (cancelled, faulted, or completed).</summary>
|
||||
public int CompletedEnumerationCount => Volatile.Read(ref _completedEnumerationCount);
|
||||
public int CompletedEnumerationCount => Completions.Count;
|
||||
|
||||
/// <summary>Gets a value indicating whether the most recently finished enumeration ended cancelled.</summary>
|
||||
public bool LastEnumerationWasCancelled => _lastEnumerationWasCancelled;
|
||||
@@ -464,7 +461,7 @@ public sealed class DashboardSnapshotFeedTests
|
||||
private async IAsyncEnumerable<DashboardSnapshot> EnumerateAsync(
|
||||
[EnumeratorCancellation] CancellationToken cancellationToken)
|
||||
{
|
||||
Interlocked.Increment(ref _enumerationCount);
|
||||
Enumerations.Signal();
|
||||
try
|
||||
{
|
||||
await foreach (object item in _pushes.Reader.ReadAllAsync(cancellationToken).ConfigureAwait(false))
|
||||
@@ -480,7 +477,7 @@ public sealed class DashboardSnapshotFeedTests
|
||||
finally
|
||||
{
|
||||
_lastEnumerationWasCancelled = cancellationToken.IsCancellationRequested;
|
||||
Interlocked.Increment(ref _completedEnumerationCount);
|
||||
Completions.Signal();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user