using Akka.Actor; using Microsoft.EntityFrameworkCore; using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Commons.Messages.Admin; using ZB.MOM.WW.OtOpcUa.Commons.Messages.Mesh; using ZB.MOM.WW.OtOpcUa.Commons.Types; using ZB.MOM.WW.OtOpcUa.Configuration; 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; /// /// lmxopcua#996 (review H-3): Restart/Reconnect report the DISPATCH. A ConfigDb failure while writing the audit row /// must not turn an already-dispatched command into a "failed" reply (the operator retries ⇒ a second restart), and the /// audit write must not suspend the admin mailbox. /// public sealed class AdminOperationsActorDriverOperationTests : ControlPlaneActorTestBase { private static readonly TimeSpan AckBudget = TimeSpan.FromSeconds(1); private (IActorRef Actor, Akka.TestKit.TestProbe Router) Create(IDbContextFactory factory) { var router = CreateTestProbe("router"); var coordinator = CreateTestProbe("coord"); var actor = Sys.ActorOf(AdminOperationsActor.Props(factory, coordinator.Ref, Enumerable.Empty(), meshRouter: router.Ref)); return (actor, router); } /// The audit write throws: the command still went out and the reply says so (success). [Theory] [InlineData(true)] [InlineData(false)] public void Audit_failure_does_not_turn_a_dispatched_command_into_a_failure(bool restart) { var (actor, router) = Create(new ThrowingAsyncFactory(NewInMemoryDbFactory())); var correlation = Guid.NewGuid(); if (restart) actor.Tell(new RestartDriver("MAIN", "drv-1", "joe", correlation)); else actor.Tell(new ReconnectDriver("MAIN", "drv-1", "joe", correlation)); var command = router.ExpectMsg(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken); if (restart) command.Message.ShouldBeOfType(); else command.Message.ShouldBeOfType(); if (restart) { var reply = ExpectMsg(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken); reply.Ok.ShouldBeTrue(); reply.CorrelationId.ShouldBe(correlation); } else { var reply = ExpectMsg(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken); reply.Ok.ShouldBeTrue(); reply.CorrelationId.ShouldBe(correlation); } } /// /// While the audit write is parked on a gated DB, the reply is already out and an alarm ack is answered; releasing the /// gate writes the row (positive control: the audit is not skipped, only moved off the mailbox). /// [Fact] public void Audit_write_does_not_hold_the_mailbox_and_is_still_recorded() { var inner = NewInMemoryDbFactory(); var gated = new GatedAsyncFactory(inner); var (actor, router) = Create(gated); gated.Hold(); actor.Tell(new RestartDriver("MAIN", "drv-2", "joe", Guid.NewGuid())); ExpectMsg(AckBudget, cancellationToken: TestContext.Current.CancellationToken).Ok.ShouldBeTrue(); gated.Entered.Wait(PresenceBudget, TestContext.Current.CancellationToken).ShouldBeTrue("the audit write must be parked"); // The mailbox is free: a second operation is dispatched and answered while the first audit is still parked. actor.Tell(new ReconnectDriver("MAIN", "drv-3", "kim", Guid.NewGuid())); ExpectMsg(AckBudget, cancellationToken: TestContext.Current.CancellationToken).Ok.ShouldBeTrue(); using (var db = inner.CreateDbContext()) db.ConfigEdits.ShouldBeEmpty(); gated.Release(); AwaitAssert(() => { using var db = inner.CreateDbContext(); db.ConfigEdits.Count().ShouldBe(2); db.ConfigEdits.Select(e => e.EditedBy).OrderBy(x => x).ToList().ShouldBe(["joe", "kim"]); }, duration: PresenceBudget, cancellationToken: TestContext.Current.CancellationToken); router.ExpectMsg(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken); } private sealed class ThrowingAsyncFactory(IDbContextFactory inner) : IDbContextFactory { public OtOpcUaConfigDbContext CreateDbContext() => inner.CreateDbContext(); public Task CreateDbContextAsync(CancellationToken cancellationToken = default) => throw new InvalidOperationException("ConfigDb unreachable (simulated)"); } private sealed class GatedAsyncFactory(IDbContextFactory inner) : IDbContextFactory { private volatile TaskCompletionSource? _gate; public ManualResetEventSlim Entered { get; } = new(); public void Hold() => _gate = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); public void Release() => _gate?.TrySetResult(); public OtOpcUaConfigDbContext CreateDbContext() => inner.CreateDbContext(); public async Task CreateDbContextAsync(CancellationToken cancellationToken = default) { Entered.Set(); if (_gate is { } gate) await gate.Task.WaitAsync(cancellationToken); return inner.CreateDbContext(); } } }