198 lines
9.6 KiB
C#
198 lines
9.6 KiB
C#
using System.Reflection;
|
|
using System.Security.Claims;
|
|
using Bunit;
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Microsoft.Extensions.Options;
|
|
using NSubstitute;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Deployment;
|
|
using ZB.MOM.WW.ScadaBridge.Communication;
|
|
using ZB.MOM.WW.ScadaBridge.DeploymentManager;
|
|
using ZB.MOM.WW.ScadaBridge.Security;
|
|
using ZB.MOM.WW.ScadaBridge.TemplateEngine.Services;
|
|
using TopologyPage = ZB.MOM.WW.ScadaBridge.CentralUI.Components.Pages.Deployment.Topology;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.CentralUI.Tests;
|
|
|
|
/// <summary>
|
|
/// Regression tests for register row 103 (the P6-6 lesser instance in
|
|
/// <c>Topology</c>). <c>OnInitializedAsync</c> awaits <c>LoadDataAsync</c> and only then
|
|
/// calls <c>StartLiveUpdatesTimer</c>. If the circuit was torn down inside that await
|
|
/// (user navigates away during the initial page load), the pre-fix <c>Dispose</c> would
|
|
/// have already run and found <c>_liveUpdatesTimer</c> still null — a no-op — so the
|
|
/// timer <c>StartLiveUpdatesTimer</c> creates once the await resumes is never disposed:
|
|
/// it ticks against a torn-down component every 15s until process restart. The fix
|
|
/// (mirroring <c>DebugView</c>'s P6-6 fix and <c>TransportImport</c>'s row-103 sibling
|
|
/// fix) creates/replaces the timer and observes disposal atomically under
|
|
/// <c>_timerLock</c>, so a (re)start that resumes after <c>Dispose</c> already ran
|
|
/// refuses to create a new timer instead of leaking one.
|
|
///
|
|
/// <para>
|
|
/// The race is reproduced deterministically by driving the exact call ORDER the async
|
|
/// gap allows — <c>Dispose()</c> then a late-arriving <c>StartLiveUpdatesTimer()</c> —
|
|
/// rather than by gating I/O and sleeping: <c>StartLiveUpdatesTimer</c> itself is
|
|
/// synchronous, so once we control ordering there is nothing left to wait on. This is
|
|
/// the "deterministic observation, never sleep-then-assert" style the shared plan
|
|
/// constraints require: the assertion is a direct field-state check, not a timer tick.
|
|
/// </para>
|
|
/// </summary>
|
|
public class TopologyLiveUpdatesTimerDisposeRaceTests : BunitContext
|
|
{
|
|
private readonly ITemplateEngineRepository _repo = Substitute.For<ITemplateEngineRepository>();
|
|
private readonly ISiteRepository _siteRepo = Substitute.For<ISiteRepository>();
|
|
private readonly IDeploymentManagerRepository _deployRepo = Substitute.For<IDeploymentManagerRepository>();
|
|
private readonly IAuditService _audit = Substitute.For<IAuditService>();
|
|
|
|
public TopologyLiveUpdatesTimerDisposeRaceTests()
|
|
{
|
|
Services.AddSingleton(_repo);
|
|
Services.AddSingleton(_siteRepo);
|
|
Services.AddSingleton(_deployRepo);
|
|
Services.AddSingleton(Substitute.For<ZB.MOM.WW.ScadaBridge.DeploymentManager.IFlatteningPipeline>());
|
|
// DeploymentService takes IStaleInstanceProbe since the Low-wave P7-6 pre-filter (T13).
|
|
Services.AddSingleton(Substitute.For<ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Transport.IStaleInstanceProbe>());
|
|
Services.AddSingleton(_audit);
|
|
|
|
var comms = new CommunicationService(
|
|
Options.Create(new CommunicationOptions()),
|
|
NullLogger<CommunicationService>.Instance);
|
|
Services.AddSingleton(comms);
|
|
|
|
Services.AddSingleton(new OperationLockManager());
|
|
Services.AddSingleton(Options.Create(new DeploymentManagerOptions
|
|
{
|
|
OperationLockTimeout = TimeSpan.FromSeconds(5)
|
|
}));
|
|
Services.AddSingleton(Options.Create(new CommunicationOptions()));
|
|
Services.AddSingleton<ILogger<DeploymentService>>(NullLogger<DeploymentService>.Instance);
|
|
Services.AddScoped<ZB.MOM.WW.ScadaBridge.TemplateEngine.Flattening.DiffService>();
|
|
Services.AddScoped<ZB.MOM.WW.ScadaBridge.TemplateEngine.Flattening.RevisionHashService>();
|
|
Services.AddSingleton<IDeploymentStatusNotifier>(
|
|
new DeploymentStatusNotifier(NullLogger<DeploymentStatusNotifier>.Instance));
|
|
Services.AddScoped<DeploymentService>();
|
|
Services.AddScoped<AreaService>();
|
|
Services.AddScoped<InstanceService>();
|
|
|
|
AddTestAuth();
|
|
|
|
Services.AddScoped<ZB.MOM.WW.ScadaBridge.CentralUI.Components.Shared.IDialogService,
|
|
ZB.MOM.WW.ScadaBridge.CentralUI.Components.Shared.DialogService>();
|
|
Services.AddScoped<ZB.MOM.WW.ScadaBridge.CentralUI.Auth.SiteScopeService>();
|
|
|
|
JSInterop.Setup<string?>("treeviewStorage.load", _ => true).SetResult(null);
|
|
JSInterop.SetupVoid("treeviewStorage.save", _ => true).SetVoidResult();
|
|
|
|
SeedEmptyRepos();
|
|
}
|
|
|
|
private void AddTestAuth()
|
|
{
|
|
var claims = new[]
|
|
{
|
|
new Claim(JwtTokenService.UsernameClaimType, "tester"),
|
|
new Claim(JwtTokenService.RoleClaimType, "Deployer")
|
|
};
|
|
var user = new ClaimsPrincipal(new ClaimsIdentity(claims, "TestAuth"));
|
|
Services.AddSingleton<AuthenticationStateProvider>(new TestAuthStateProvider(user));
|
|
Services.AddAuthorizationCore();
|
|
Services.AddTestConfigurationMutations();
|
|
}
|
|
|
|
private void SeedEmptyRepos()
|
|
{
|
|
_siteRepo.GetAllSitesAsync(Arg.Any<CancellationToken>())
|
|
.Returns(Task.FromResult<IReadOnlyList<Site>>(new List<Site>()));
|
|
_repo.GetAllTemplatesAsync(Arg.Any<CancellationToken>())
|
|
.Returns(Task.FromResult<IReadOnlyList<Template>>(new List<Template>()));
|
|
_repo.GetAllInstancesAsync(Arg.Any<CancellationToken>())
|
|
.Returns(Task.FromResult<IReadOnlyList<Instance>>(new List<Instance>()));
|
|
_repo.GetAreasBySiteIdAsync(Arg.Any<int>(), Arg.Any<CancellationToken>())
|
|
.Returns(Task.FromResult<IReadOnlyList<Area>>(new List<Area>()));
|
|
}
|
|
|
|
private static void InvokePrivate(object obj, string name) =>
|
|
obj.GetType().GetMethod(name, BindingFlags.Instance | BindingFlags.NonPublic)!
|
|
.Invoke(obj, null);
|
|
|
|
private static T? GetField<T>(object obj, string name) =>
|
|
(T?)obj.GetType().GetField(name, BindingFlags.Instance | BindingFlags.NonPublic)!.GetValue(obj);
|
|
|
|
[Fact]
|
|
public void Render_StartsALiveTimer_Sanity()
|
|
{
|
|
// Baseline: the normal init path (fast/empty repos complete synchronously) leaves
|
|
// a running timer, so the race tests below start from a known non-null state.
|
|
var cut = Render<TopologyPage>();
|
|
Assert.NotNull(GetField<Timer?>(cut.Instance, "_liveUpdatesTimer"));
|
|
cut.Instance.Dispose();
|
|
}
|
|
|
|
[Fact]
|
|
public void StartLiveUpdatesTimer_ResumingAfterDispose_LeavesNoLiveTimer()
|
|
{
|
|
// Simulates: OnInitializedAsync is suspended awaiting LoadDataAsync; the user
|
|
// navigates away, so Dispose() runs (and, since no timer exists yet, has nothing
|
|
// to stop); the await then resumes and calls StartLiveUpdatesTimer() — the exact
|
|
// shape register row 103 flags for Topology (the P6-6 lesser instance).
|
|
var cut = Render<TopologyPage>();
|
|
|
|
// The normal init path already started a timer; dispose it first so the field is
|
|
// null going into the race exactly as it would be if OnInitializedAsync's await
|
|
// was still pending when Dispose ran (no timer created yet).
|
|
cut.Instance.Dispose();
|
|
Assert.Null(GetField<Timer?>(cut.Instance, "_liveUpdatesTimer"));
|
|
|
|
// The late-arriving continuation's call, happening strictly AFTER Dispose().
|
|
InvokePrivate(cut.Instance, "StartLiveUpdatesTimer");
|
|
|
|
// Post-fix: refuses to create a timer once disposed — nothing is leaked.
|
|
// Pre-fix: this assignment is unconditional, so the field would be non-null here
|
|
// (a live Timer nothing will ever dispose).
|
|
Assert.Null(GetField<Timer?>(cut.Instance, "_liveUpdatesTimer"));
|
|
}
|
|
|
|
[Fact]
|
|
public void Dispose_StopsTheLiveTimer_ExactlyOnce()
|
|
{
|
|
var cut = Render<TopologyPage>();
|
|
Assert.NotNull(GetField<Timer?>(cut.Instance, "_liveUpdatesTimer"));
|
|
|
|
cut.Instance.Dispose();
|
|
Assert.Null(GetField<Timer?>(cut.Instance, "_liveUpdatesTimer"));
|
|
|
|
// A second Dispose (bUnit tears the component down again at test end) must not
|
|
// throw (e.g. from double-disposing the same Timer) and must leave the slot null.
|
|
var ex = Record.Exception(() => cut.Instance.Dispose());
|
|
Assert.Null(ex);
|
|
Assert.Null(GetField<Timer?>(cut.Instance, "_liveUpdatesTimer"));
|
|
}
|
|
|
|
[Fact]
|
|
public void OnLiveUpdatesToggled_Off_ThenDispose_LeavesNoLiveTimer()
|
|
{
|
|
var cut = Render<TopologyPage>();
|
|
Assert.NotNull(GetField<Timer?>(cut.Instance, "_liveUpdatesTimer"));
|
|
|
|
// Turn live updates off via the toolbar handler (StopLiveUpdatesTimer path).
|
|
var toggleMethod = typeof(TopologyPage).GetMethod(
|
|
"OnLiveUpdatesToggled", BindingFlags.Instance | BindingFlags.NonPublic)!;
|
|
var changeArgsType = typeof(Microsoft.AspNetCore.Components.ChangeEventArgs);
|
|
var changeArgs = (Microsoft.AspNetCore.Components.ChangeEventArgs)Activator.CreateInstance(changeArgsType)!;
|
|
changeArgs.Value = false;
|
|
toggleMethod.Invoke(cut.Instance, new object?[] { changeArgs });
|
|
|
|
Assert.Null(GetField<Timer?>(cut.Instance, "_liveUpdatesTimer"));
|
|
|
|
var ex = Record.Exception(() => cut.Instance.Dispose());
|
|
Assert.Null(ex);
|
|
Assert.Null(GetField<Timer?>(cut.Instance, "_liveUpdatesTimer"));
|
|
}
|
|
}
|