Files
scadaproj/components/ui-theme/validation/ot-ui/host/DeploymentFixtures.cs
T

65 lines
3.4 KiB
C#

using Microsoft.EntityFrameworkCore;
using ZB.MOM.WW.OtOpcUa.Configuration;
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Admin;
/// <summary>Fixture-only memory-provider reads with controlled failure; no external DB is configured.</summary>
public sealed class FixtureDbReadControl(DbContextOptions<OtOpcUaConfigDbContext> options) : IDbContextFactory<OtOpcUaConfigDbContext>
{
public bool FailReads {get;set;}
public int DelayMilliseconds {get;set;}
public OtOpcUaConfigDbContext CreateDbContext()=>new(options);
public async Task<OtOpcUaConfigDbContext> CreateDbContextAsync(CancellationToken ct=default)
{
var fail=FailReads;var delay=DelayMilliseconds;
if(delay>0)await Task.Delay(delay,ct);
if(fail)throw new IOException("Synthetic fixture-only history read failure");
return CreateDbContext();
}
}
/// <summary>A fabricated deployment response and memory-only record, never an actor/fleet deployment.</summary>
public sealed class FixtureDeploymentState(FixtureDbReadControl reads)
{
public const string Revision="cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc";
public string Mode {get;set;}="accept";
public int Requests {get;private set;}
public Guid? LastAccepted {get;private set;}
private readonly SemaphoreSlim _resetGate=new(1,1);
private string? _run;
public async Task ResetAsync(string run,bool failInitial)
{
await _resetGate.WaitAsync();
try
{
// SSR and interactive initialization share this synthetic run; never reset a running scenario twice.
if(_run==run)return;
reads.FailReads=false;reads.DelayMilliseconds=0;Requests=0;LastAccepted=null;
await using var db=reads.CreateDbContext();
db.NodeDeploymentStates.RemoveRange(db.NodeDeploymentStates);db.Deployments.RemoveRange(db.Deployments);
await db.SaveChangesAsync();reads.FailReads=failInitial;_run=run;
}
finally {_resetGate.Release();}
}
public async Task<StartDeploymentResult> StartAsync(string actor,CancellationToken ct)
{
Requests++;var mode=Mode;
await Task.Delay(700,ct);
if(mode=="unknown")throw new IOException("Synthetic fixture-only missing actor reply");
if(mode=="nochanges")return new(StartDeploymentOutcome.NoChanges,null,null,null,new(Guid.NewGuid()));
if(mode=="rejected")return new(StartDeploymentOutcome.Rejected,null,null,"Synthetic preflight rejection",new(Guid.NewGuid()));
if(mode=="blocked")return new(StartDeploymentOutcome.AnotherDeploymentInFlight,null,null,"Synthetic other deployment in progress",new(Guid.NewGuid()));
var id=Guid.NewGuid();LastAccepted=id;
await using var db=reads.CreateDbContext();
db.Deployments.Add(new Deployment {DeploymentId=id,RevisionHash=Revision,CreatedBy=actor,Status=DeploymentStatus.AwaitingApplyAcks});
await db.SaveChangesAsync(ct);
return new(StartDeploymentOutcome.Accepted,new(id),new(Revision),null,new(Guid.NewGuid()));
}
public async Task SealAsync()
{
if(LastAccepted is not {} id)return;
await using var db=reads.CreateDbContext();var row=await db.Deployments.SingleAsync(d=>d.DeploymentId==id);
row.Status=DeploymentStatus.Sealed;row.SealedAtUtc=DateTime.UtcNow;await db.SaveChangesAsync();
}
}