91 lines
5.2 KiB
C#
91 lines
5.2 KiB
C#
using System.IO.Compression;
|
|
using System.Text.Json;
|
|
using Akka.Actor;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
|
using Microsoft.EntityFrameworkCore.Storage;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Admin;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Deploy;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.Types;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
|
using ZB.MOM.WW.OtOpcUa.ControlPlane.AdminOperations;
|
|
using ZB.MOM.WW.OtOpcUa.ControlPlane.Tests.Harness;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.ControlPlane.Tests.DataConnections;
|
|
|
|
[Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "P07")]
|
|
public sealed class GrpcDataScopeSealingTests : ControlPlaneActorTestBase
|
|
{
|
|
[Fact]
|
|
public void Failed_save_does_not_dispatch_or_consume_version_and_retry_seals_matching_artifact()
|
|
{
|
|
var root = new InMemoryDatabaseRoot(); var name = Guid.NewGuid().ToString("N");
|
|
var fault = new FailFirstSave(); var factory = new Factory(name, root, fault);
|
|
var coordinator = CreateTestProbe();
|
|
var actor = Sys.ActorOf(AdminOperationsActor.Props(factory, coordinator.Ref, Array.Empty<IDriverProbe>()));
|
|
actor.Tell(new StartDeployment("test", CorrelationId.NewId()));
|
|
var failed = ExpectMsg<StartDeploymentResult>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
|
|
failed.Outcome.ShouldBe(StartDeploymentOutcome.Rejected);
|
|
coordinator.ExpectNoMsg(TimeSpan.FromMilliseconds(100), TestContext.Current.CancellationToken);
|
|
using (var db = factory.CreateDbContext())
|
|
{
|
|
db.Deployments.Count().ShouldBe(0);
|
|
db.DataConnectionScopeStates.Count().ShouldBe(0);
|
|
}
|
|
actor.Tell(new StartDeployment("test", CorrelationId.NewId()));
|
|
var accepted = ExpectMsg<StartDeploymentResult>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
|
|
accepted.Outcome.ShouldBe(StartDeploymentOutcome.Accepted, accepted.Message);
|
|
coordinator.ExpectMsg<DispatchDeployment>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
|
|
using var verify = factory.CreateDbContext();
|
|
var state = verify.DataConnectionScopeStates.Single();
|
|
state.ScopeVersion.ShouldBe(1);
|
|
var deployment = verify.Deployments.Single();
|
|
using var stream = new GZipStream(new MemoryStream(deployment.ArtifactBlob), CompressionMode.Decompress);
|
|
using var json = JsonDocument.Parse(stream);
|
|
json.RootElement.GetProperty("DataConnectionScopeVersion").GetInt64().ShouldBe(state.ScopeVersion);
|
|
deployment.Status.ShouldBe(DeploymentStatus.Dispatching);
|
|
}
|
|
[Fact]
|
|
public void Unchanged_empty_scope_reseals_same_version_and_hash()
|
|
{
|
|
var factory = NewInMemoryDbFactory(); var coordinator = CreateTestProbe();
|
|
var actor = Sys.ActorOf(AdminOperationsActor.Props(factory, coordinator.Ref, Array.Empty<IDriverProbe>()));
|
|
actor.Tell(new StartDeployment("test", CorrelationId.NewId()));
|
|
var first = ExpectMsg<StartDeploymentResult>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
|
|
first.Outcome.ShouldBe(StartDeploymentOutcome.Accepted, first.Message);
|
|
coordinator.ExpectMsg<DispatchDeployment>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
|
|
string firstHash;
|
|
using (var db = factory.CreateDbContext())
|
|
{
|
|
db.DataConnectionScopeStates.Single().ScopeVersion.ShouldBe(1);
|
|
var deployment = db.Deployments.Single(); firstHash = deployment.RevisionHash;
|
|
deployment.Status = DeploymentStatus.Sealed; db.SaveChanges();
|
|
}
|
|
actor.Tell(new StartDeployment("test", CorrelationId.NewId()));
|
|
var second = ExpectMsg<StartDeploymentResult>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
|
|
second.Outcome.ShouldBe(StartDeploymentOutcome.Accepted, second.Message);
|
|
coordinator.ExpectMsg<DispatchDeployment>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
|
|
using var verify = factory.CreateDbContext();
|
|
verify.DataConnectionScopeStates.Single().ScopeVersion.ShouldBe(1);
|
|
verify.Deployments.Select(x => x.RevisionHash).ToArray().ShouldAllBe(x => x == firstHash);
|
|
}
|
|
private sealed class FailFirstSave : SaveChangesInterceptor
|
|
{
|
|
private int _failed;
|
|
public override ValueTask<InterceptionResult<int>> SavingChangesAsync(DbContextEventData eventData, InterceptionResult<int> result, CancellationToken cancellationToken = default)
|
|
{
|
|
if (Interlocked.Exchange(ref _failed, 1) == 0) throw new InvalidOperationException("injected failure");
|
|
return base.SavingChangesAsync(eventData, result, cancellationToken);
|
|
}
|
|
}
|
|
private sealed class Factory(string name, InMemoryDatabaseRoot root, IInterceptor interceptor) : IDbContextFactory<OtOpcUaConfigDbContext>
|
|
{
|
|
public OtOpcUaConfigDbContext CreateDbContext() => new(new DbContextOptionsBuilder<OtOpcUaConfigDbContext>()
|
|
.UseInMemoryDatabase(name, root).AddInterceptors(interceptor).Options);
|
|
}
|
|
}
|