Preserve Fleet and Hosts readings through refresh failures
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
using System.Reflection;
|
||||
using Akka.Actor;
|
||||
using Akka.Configuration;
|
||||
using Bunit;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Hosts;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Hubs;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Uns;
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.Interfaces;
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.Types;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using FleetPage = ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Fleet;
|
||||
using HostsPage = ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Hosts;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Pages;
|
||||
|
||||
/// <summary>Actual page reads retain independently acquired data and support retry.</summary>
|
||||
[Trait("Category", "Unit")]
|
||||
public sealed class FleetHostsRefreshTests : BunitContext
|
||||
{
|
||||
private ControlledFactory Configure()
|
||||
{
|
||||
AddAuthorization().SetAuthorized("fixture");
|
||||
var factory = new ControlledFactory();
|
||||
Services.AddSingleton<IDbContextFactory<OtOpcUaConfigDbContext>>(factory);
|
||||
Services.AddSingleton(DispatchProxy.Create<IClusterRoleInfo, ClusterProxy>());
|
||||
return factory;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Fleet_FailedInitialReadIsUnavailableAndCanRetry()
|
||||
{
|
||||
var factory=Configure();factory.Fail=true;
|
||||
var cut=Render<FleetPage>();
|
||||
cut.WaitForAssertion(()=>cut.Find("[role=status]").TextContent.ShouldContain("unavailable"));
|
||||
cut.Markup.ShouldNotContain("No driver-role nodes");
|
||||
cut.Markup.ShouldNotContain("PRIVATE failure details");
|
||||
factory.Fail=false;cut.Find("button").Click();
|
||||
cut.WaitForAssertion(()=>cut.Markup.ShouldContain("fixture-driver"));
|
||||
cut.FindAll("[role=status]").Count.ShouldBe(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Fleet_FailedRefreshPreservesRowsAndSuccessfulTimestamp()
|
||||
{
|
||||
var factory=Configure();var cut=Render<FleetPage>();
|
||||
var fetched=cut.Find("time").GetAttribute("datetime");
|
||||
factory.Fail=true;cut.Find("button").Click();
|
||||
cut.WaitForAssertion(()=>cut.Find("[role=status]").TextContent.ShouldContain("last successful reading"));
|
||||
cut.Markup.ShouldContain("fixture-driver");
|
||||
cut.Find("time").GetAttribute("datetime").ShouldBe(fetched);
|
||||
factory.Fail=false;cut.Find("button").Click();
|
||||
cut.WaitForAssertion(()=>cut.FindAll("[role=status]").Count.ShouldBe(0));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Fleet_QueuedRefreshDoesNotOverlapAndDisposalCancelsRead()
|
||||
{
|
||||
var factory=Configure();var cut=Render<FleetPage>();
|
||||
var entered=new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var canceled=new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
factory.Pending=async ct=> {entered.TrySetResult();try {await Task.Delay(Timeout.Infinite,ct);}catch(OperationCanceledException){canceled.TrySetResult();throw;}return factory.CreateDbContext();};
|
||||
var pending=cut.Find("button").TriggerEventAsync("onclick",new Microsoft.AspNetCore.Components.Web.MouseEventArgs());
|
||||
await entered.Task.WaitAsync(TimeSpan.FromSeconds(10),Xunit.TestContext.Current.CancellationToken);
|
||||
await cut.Find("button").TriggerEventAsync("onclick",new Microsoft.AspNetCore.Components.Web.MouseEventArgs());
|
||||
factory.Attempts.ShouldBe(2);
|
||||
await DisposeComponentsAsync();await pending;
|
||||
await canceled.Task.WaitAsync(TimeSpan.FromSeconds(10),Xunit.TestContext.Current.CancellationToken);
|
||||
Renderer.UnhandledException.IsCompletedSuccessfully.ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Hosts_ConfigFailureRetainsEnrichmentAndItsOwnSuccessTime()
|
||||
{
|
||||
var factory=Configure();
|
||||
using(var db=factory.CreateDbContext()) {db.ClusterNodes.Add(new(){ClusterId="fixture",NodeId="fixture-node",Host="fixture-host",OpcUaPort=4840,ApplicationUri="urn:fixture",CreatedBy="fixture"});db.DriverInstances.Add(new(){DriverInstanceId="fixture-driver",ClusterId="fixture",Name="Retained human name",DriverType="Modbus",DriverConfig="{}"});db.SaveChanges();}
|
||||
using var actors=ConfigureHosts(factory);
|
||||
var cut=Render<HostsPage>();
|
||||
cut.WaitForAssertion(()=>cut.FindAll("time").Count.ShouldBe(2));
|
||||
cut.Markup.ShouldContain("Retained human name");
|
||||
var fetched=cut.Find("[data-testid=config-fetched]").GetAttribute("datetime");
|
||||
factory.Fail=true;cut.Find("button").Click();
|
||||
cut.WaitForAssertion(()=>cut.Find("[role=status]").TextContent.ShouldContain("last successful configuration"));
|
||||
cut.Find("[data-testid=config-fetched]").GetAttribute("datetime").ShouldBe(fetched);
|
||||
var nodes=(System.Collections.IList)typeof(HostsPage).GetField("_nodes",BindingFlags.NonPublic|BindingFlags.Instance)!.GetValue(cut.Instance)!;
|
||||
var instances=(System.Collections.IList)typeof(HostsPage).GetField("_instances",BindingFlags.NonPublic|BindingFlags.Instance)!.GetValue(cut.Instance)!;
|
||||
nodes.Count.ShouldBe(1);instances.Count.ShouldBe(1);
|
||||
cut.Markup.ShouldContain("Retained human name");cut.Markup.ShouldContain("Synthetic fault");
|
||||
factory.Fail=false;cut.Find("button").Click();
|
||||
cut.WaitForAssertion(()=>cut.FindAll("[role=status]").Count.ShouldBe(0));
|
||||
await DisposeComponentsAsync();await actors.Terminate();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Hosts_InitialUnavailableIsHonestAndRecovers()
|
||||
{
|
||||
var factory=Configure();factory.Fail=true;
|
||||
using var actors=ConfigureHosts(factory);
|
||||
var cut=Render<HostsPage>();
|
||||
cut.WaitForAssertion(()=>cut.Find("[role=status]").TextContent.ShouldContain("No successful configuration reading"));
|
||||
cut.FindAll("[data-testid=config-fetched]").Count.ShouldBe(0);
|
||||
cut.Markup.ShouldNotContain("PRIVATE failure details");
|
||||
factory.Fail=false;cut.Find("button").Click();
|
||||
cut.WaitForAssertion(()=>cut.FindAll("[role=status]").Count.ShouldBe(0));
|
||||
cut.Find("[data-testid=config-fetched]").TextContent.ShouldContain("UTC");
|
||||
await DisposeComponentsAsync();await actors.Terminate();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Hosts_InitialReadSharesOverlapGuardAndDisposalCancellation()
|
||||
{
|
||||
var factory=Configure();using var actors=ConfigureHosts(factory);
|
||||
var entered=new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var canceled=new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
factory.Pending=async ct=>{entered.TrySetResult();try{await Task.Delay(Timeout.Infinite,ct);}catch(OperationCanceledException){canceled.TrySetResult();throw;}return factory.CreateDbContext();};
|
||||
var cut=Render<HostsPage>();
|
||||
await entered.Task.WaitAsync(TimeSpan.FromSeconds(10),Xunit.TestContext.Current.CancellationToken);
|
||||
await cut.Find("button").TriggerEventAsync("onclick",new Microsoft.AspNetCore.Components.Web.MouseEventArgs());
|
||||
factory.Attempts.ShouldBe(1);
|
||||
await DisposeComponentsAsync();
|
||||
await canceled.Task.WaitAsync(TimeSpan.FromSeconds(10),Xunit.TestContext.Current.CancellationToken);
|
||||
Renderer.UnhandledException.IsCompletedSuccessfully.ShouldBeFalse();
|
||||
await actors.Terminate();
|
||||
}
|
||||
|
||||
private ActorSystem ConfigureHosts(ControlledFactory factory)
|
||||
{
|
||||
var actors=ActorSystem.Create("refresh-"+Guid.NewGuid().ToString("N"),ConfigurationFactory.ParseString("""
|
||||
akka.actor.provider = cluster
|
||||
akka.remote.dot-netty.tcp.hostname = "127.0.0.1"
|
||||
akka.remote.dot-netty.tcp.port = 0
|
||||
akka.cluster.seed-nodes = []
|
||||
akka.loglevel = ERROR
|
||||
"""));
|
||||
Services.AddSingleton(actors);
|
||||
var store = new InMemoryDriverStatusSnapshotStore();
|
||||
store.Upsert(new("fixture", "fixture-driver", "Faulted", null, "Synthetic fault", 1, DateTime.UtcNow));
|
||||
Services.AddSingleton<IDriverStatusSnapshotStore>(store);
|
||||
Services.AddSingleton<IDriverResilienceStatusStore,InMemoryDriverResilienceStatusStore>();
|
||||
Services.AddSingleton(new DeployedDriverSet(factory));
|
||||
return actors;
|
||||
}
|
||||
|
||||
public sealed class ControlledFactory : IDbContextFactory<OtOpcUaConfigDbContext>
|
||||
{
|
||||
private readonly string _name="refresh-"+Guid.NewGuid().ToString("N");
|
||||
public bool Fail {get;set;}
|
||||
public int Attempts {get;private set;}
|
||||
public Func<CancellationToken,Task<OtOpcUaConfigDbContext>>? Pending {get;set;}
|
||||
public OtOpcUaConfigDbContext CreateDbContext()=>RawTreeTestDb.CreateNamed(_name);
|
||||
public Task<OtOpcUaConfigDbContext> CreateDbContextAsync(CancellationToken ct=default)
|
||||
{Attempts++;return Pending?.Invoke(ct)??(Fail?Task.FromException<OtOpcUaConfigDbContext>(new IOException("PRIVATE failure details")):Task.FromResult(CreateDbContext()));}
|
||||
}
|
||||
public class ClusterProxy : DispatchProxy
|
||||
{
|
||||
protected override object? Invoke(MethodInfo? method,object?[]? args)=>method!.Name=="MembersWithRole"?(IReadOnlyList<NodeId>)[new("fixture-driver")]:throw new NotSupportedException(method.Name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using System.Data.Common;
|
||||
using System.Reflection;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Hosts;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Hubs;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using HostsPage=ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Hosts;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Pages;
|
||||
|
||||
/// <summary>A later query failure cannot partially install an earlier query's data.</summary>
|
||||
[Collection(nameof(AdminUiSqlServerCollection))]
|
||||
public sealed class HostsPartialReadSqlServerTests(AdminUiSqlServerFixture sql)
|
||||
{
|
||||
[SqlServerFact]
|
||||
public async Task SecondConfigQueryFailure_PreservesBothCachedListsAndSkipsEviction()
|
||||
{
|
||||
var reads=new FailDriverInstancesRead();
|
||||
var options=new DbContextOptionsBuilder<OtOpcUaConfigDbContext>().UseSqlServer(sql.ConnectionString).AddInterceptors(reads).Options;
|
||||
var factory=new ReadFactory(options);
|
||||
var page=new HostsPage();
|
||||
SetProperty(page,"DbFactory",factory);
|
||||
SetProperty(page,"Logger",NullLogger<HostsPage>.Instance);
|
||||
SetProperty(page,"DriverStore",new InMemoryDriverStatusSnapshotStore());
|
||||
// These deliberate cached values differ from the owned SQL fixture's actual rows.
|
||||
var nodes=new List<HostsNodeInfo>{new("cached", "cached-node", "cached-host",4840)};
|
||||
var instances=new List<HostsDriverInstanceInfo>{new("cached-driver","cached","Cached name","Modbus",true)};
|
||||
SetField(page,"_nodes",nodes);SetField(page,"_instances",instances);
|
||||
var acquired=new DateTime(2026,1,1,0,0,0,DateTimeKind.Utc);SetField(page,"_lastConfigUtc",acquired);
|
||||
// Deployed/resilience store intentionally absent: reaching eviction would fail before the scripted
|
||||
// second-query exception, and the exact query observations below would no longer hold.
|
||||
var changed=await (Task<bool>)typeof(HostsPage).GetMethod("LoadConfigAsync",BindingFlags.Instance|BindingFlags.NonPublic)!.Invoke(page,null)!;
|
||||
changed.ShouldBeFalse();reads.NodesRead.ShouldBeTrue();reads.InstancesRejected.ShouldBeTrue();
|
||||
GetField(page,"_nodes").ShouldBeSameAs(nodes);GetField(page,"_instances").ShouldBeSameAs(instances);
|
||||
GetField(page,"_lastConfigUtc").ShouldBe(acquired);GetField(page,"_configFailedAtUtc").ShouldNotBeNull();
|
||||
await page.DisposeAsync();
|
||||
}
|
||||
private static void SetProperty(object target,string name,object value)=>target.GetType().GetProperty(name,BindingFlags.Instance|BindingFlags.NonPublic)!.SetValue(target,value);
|
||||
private static void SetField(object target,string name,object value)=>target.GetType().GetField(name,BindingFlags.Instance|BindingFlags.NonPublic)!.SetValue(target,value);
|
||||
private static object? GetField(object target,string name)=>target.GetType().GetField(name,BindingFlags.Instance|BindingFlags.NonPublic)!.GetValue(target);
|
||||
private sealed class ReadFactory(DbContextOptions<OtOpcUaConfigDbContext> options):IDbContextFactory<OtOpcUaConfigDbContext>
|
||||
{
|
||||
public OtOpcUaConfigDbContext CreateDbContext()=>new(options);
|
||||
public Task<OtOpcUaConfigDbContext> CreateDbContextAsync(CancellationToken ct=default)=>Task.FromResult(CreateDbContext());
|
||||
}
|
||||
private sealed class FailDriverInstancesRead:DbCommandInterceptor
|
||||
{
|
||||
public bool NodesRead {get;private set;}
|
||||
public bool InstancesRejected {get;private set;}
|
||||
public override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(DbCommand command,CommandEventData data,InterceptionResult<DbDataReader> result,CancellationToken ct=default)
|
||||
{
|
||||
if(command.CommandText.Contains("[ClusterNode]",StringComparison.Ordinal)) NodesRead=true;
|
||||
if(command.CommandText.Contains("[DriverInstance]",StringComparison.Ordinal))
|
||||
{InstancesRejected=true;throw new IOException("Synthetic second-query failure");}
|
||||
return ValueTask.FromResult(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user