fix(centralui): pending-updates rail refreshes as soon as a deployment finishes — cache drops its snapshot on Success/Failed and signals the indicator, instead of serving the pre-deploy count for up to TTL + one poll
This commit is contained in:
@@ -126,6 +126,26 @@ public class PendingUpdatesIndicatorTests : BunitContext
|
||||
return host.FindComponent<PendingUpdatesIndicator>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the rail re-reads as soon as the cache reports it was invalidated (a
|
||||
/// deployment finished), rather than waiting out its 30 s poll: after a successful Deploy
|
||||
/// all the old count used to linger for up to the cache TTL plus one poll interval.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void CacheInvalidated_RereadsWithoutWaitingForThePoll()
|
||||
{
|
||||
var cache = Substitute.For<IPendingUpdatesCache>();
|
||||
cache.GetAsync(Arg.Any<bool>(), Arg.Any<CancellationToken>()).Returns(
|
||||
Task.FromResult(Snapshot(SiteRow(1, staleInstances: 9))),
|
||||
Task.FromResult(Snapshot(SiteRow(1, staleInstances: 0))));
|
||||
var cut = RenderFor(cache, Role(Roles.Deployer));
|
||||
cut.WaitForAssertion(() => Assert.Contains("9 instances", cut.Markup));
|
||||
|
||||
cache.Invalidated += Raise.Event<Action>();
|
||||
|
||||
cut.WaitForAssertion(() => Assert.Contains("Up to date", cut.Markup), TimeSpan.FromSeconds(5));
|
||||
}
|
||||
|
||||
/// <summary>Verifies that a user with only the Viewer role sees no pending-updates indicator.</summary>
|
||||
[Fact]
|
||||
public void Viewer_RendersNothing()
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using NSubstitute;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
|
||||
using ZB.MOM.WW.ScadaBridge.CentralUI.Services;
|
||||
using ZB.MOM.WW.ScadaBridge.DeploymentManager;
|
||||
using ZB.MOM.WW.ScadaBridge.DeploymentManager.PendingUpdates;
|
||||
@@ -21,6 +23,8 @@ public class PendingUpdatesCacheTests : IDisposable
|
||||
|
||||
private readonly IPendingUpdatesEvaluator _evaluator = Substitute.For<IPendingUpdatesEvaluator>();
|
||||
private readonly ServiceProvider _provider;
|
||||
private readonly DeploymentStatusNotifier _statusNotifier =
|
||||
new(NullLogger<DeploymentStatusNotifier>.Instance);
|
||||
|
||||
/// <summary>Initializes a service provider that resolves the substitute pending-updates evaluator.</summary>
|
||||
public PendingUpdatesCacheTests()
|
||||
@@ -35,8 +39,15 @@ public class PendingUpdatesCacheTests : IDisposable
|
||||
Microsoft.Extensions.Options.Options.Create(
|
||||
new DeploymentManagerOptions { PendingUpdatesCacheTtl = ttl ?? TimeSpan.FromHours(1) });
|
||||
|
||||
private PendingUpdatesCache CreateSut(TimeSpan? ttl = null) =>
|
||||
new(_provider.GetRequiredService<IServiceScopeFactory>(), Options(ttl));
|
||||
private PendingUpdatesCache CreateSut(TimeSpan? ttl = null, TimeSpan? invalidationQuietPeriod = null) =>
|
||||
new(
|
||||
_provider.GetRequiredService<IServiceScopeFactory>(),
|
||||
Options(ttl),
|
||||
_statusNotifier,
|
||||
invalidationQuietPeriod ?? TimeSpan.FromMilliseconds(20));
|
||||
|
||||
private void Finish(int instanceId, DeploymentStatus status) =>
|
||||
_statusNotifier.NotifyStatusChanged(new DeploymentStatusChange($"dep-{instanceId}", instanceId, status));
|
||||
|
||||
private static PendingUpdatesSnapshot Snapshot() =>
|
||||
new(T0, Array.Empty<SitePendingUpdates>());
|
||||
@@ -138,6 +149,86 @@ public class PendingUpdatesCacheTests : IDisposable
|
||||
await _evaluator.Received(2).EvaluateAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that a finished deployment drops the memoized snapshot — a deploy is exactly the
|
||||
/// event that changes the answer, so serving the pre-deploy count for the rest of the TTL
|
||||
/// window left the rail showing stale "pending" counts after a successful Deploy all.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Theory]
|
||||
[InlineData(DeploymentStatus.Success)]
|
||||
[InlineData(DeploymentStatus.Failed)]
|
||||
public async Task FinishedDeployment_InvalidatesTheMemoizedSnapshot(DeploymentStatus status)
|
||||
{
|
||||
_evaluator.EvaluateAsync(Arg.Any<CancellationToken>()).Returns(_ => Snapshot());
|
||||
using var sut = CreateSut();
|
||||
|
||||
await sut.GetAsync();
|
||||
Finish(1, status);
|
||||
await sut.GetAsync();
|
||||
|
||||
await _evaluator.Received(2).EvaluateAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
/// <summary>Verifies that a deployment that has only started does not invalidate the snapshot.</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task StartedDeployment_DoesNotInvalidate()
|
||||
{
|
||||
_evaluator.EvaluateAsync(Arg.Any<CancellationToken>()).Returns(_ => Snapshot());
|
||||
using var sut = CreateSut();
|
||||
|
||||
await sut.GetAsync();
|
||||
Finish(1, DeploymentStatus.InProgress);
|
||||
await sut.GetAsync();
|
||||
|
||||
await _evaluator.Received(1).EvaluateAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that a burst of completions — one per instance in a bulk deploy — raises
|
||||
/// <c>Invalidated</c> once, after the burst goes quiet, so every open rail re-reads once
|
||||
/// instead of once per instance.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task BurstOfFinishedDeployments_RaisesInvalidatedOnceAfterItGoesQuiet()
|
||||
{
|
||||
using var sut = CreateSut(invalidationQuietPeriod: TimeSpan.FromMilliseconds(100));
|
||||
var raised = 0;
|
||||
var signal = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
sut.Invalidated += () =>
|
||||
{
|
||||
Interlocked.Increment(ref raised);
|
||||
signal.TrySetResult();
|
||||
};
|
||||
|
||||
for (var i = 1; i <= 9; i++)
|
||||
{
|
||||
Finish(i, DeploymentStatus.Success);
|
||||
}
|
||||
|
||||
await signal.Task.WaitAsync(TimeSpan.FromSeconds(5));
|
||||
await Task.Delay(300);
|
||||
Assert.Equal(1, Volatile.Read(ref raised));
|
||||
}
|
||||
|
||||
/// <summary>Verifies that the cache stops listening to deployment status changes once disposed.</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task Dispose_UnsubscribesFromStatusChanges()
|
||||
{
|
||||
var sut = CreateSut();
|
||||
var raised = 0;
|
||||
sut.Invalidated += () => Interlocked.Increment(ref raised);
|
||||
|
||||
sut.Dispose();
|
||||
Finish(1, DeploymentStatus.Success);
|
||||
await Task.Delay(150);
|
||||
|
||||
Assert.Equal(0, Volatile.Read(ref raised));
|
||||
}
|
||||
|
||||
/// <summary>Releases the resources held by the test fixture.</summary>
|
||||
public void Dispose() => _provider.Dispose();
|
||||
}
|
||||
|
||||
@@ -199,4 +199,47 @@ public class SingleFlightMemoTests
|
||||
released.SetResult(99);
|
||||
Assert.Equal(99, await patient);
|
||||
}
|
||||
|
||||
/// <summary>Verifies that <c>Invalidate</c> makes a still-fresh value stale, so the next call queries again.</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task Invalidate_DiscardsAFreshValue()
|
||||
{
|
||||
var invocations = 0;
|
||||
var memo = new SingleFlightMemo<int>(TimeSpan.FromHours(1), () => T0);
|
||||
|
||||
var first = await memo.GetAsync(() => Task.FromResult(++invocations));
|
||||
memo.Invalidate();
|
||||
var second = await memo.GetAsync(() => Task.FromResult(++invocations));
|
||||
|
||||
Assert.Equal(1, first);
|
||||
Assert.Equal(2, second);
|
||||
Assert.Equal(2, memo.FlightCount);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that a flight already running when <c>Invalidate</c> is called still answers its
|
||||
/// callers but is not memoized: it may have read the state the invalidation says changed.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task Invalidate_DuringAFlight_ResultIsServedButNotMemoized()
|
||||
{
|
||||
var released = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var invocations = 0;
|
||||
var memo = new SingleFlightMemo<int>(TimeSpan.FromHours(1), () => T0);
|
||||
|
||||
var inFlight = memo.GetAsync(() =>
|
||||
{
|
||||
invocations++;
|
||||
return released.Task;
|
||||
});
|
||||
memo.Invalidate();
|
||||
released.SetResult(7);
|
||||
|
||||
Assert.Equal(7, await inFlight);
|
||||
var next = await memo.GetAsync(() => Task.FromResult(++invocations));
|
||||
Assert.Equal(2, next);
|
||||
Assert.Equal(2, memo.FlightCount);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user