using Akka.Hosting; using Akka.TestKit.Xunit; using Microsoft.EntityFrameworkCore; using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.AdminUI.Clients; using ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Acls; using ZB.MOM.WW.OtOpcUa.Commons.Messages.Admin; using ZB.MOM.WW.OtOpcUa.Configuration; using ZB.MOM.WW.OtOpcUa.ControlPlane; using ZB.MOM.WW.OtOpcUa.ControlPlane.AdminOperations; using ZB.MOM.WW.OtOpcUa.Core.Abstractions; namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests; /// /// lmxopcua#949 — an ask timeout on the deploy trigger is not a failure: the slow compose goes on to create /// the row after the client gave up. The client must report OutcomeUnknown carrying the id it /// pre-allocated, and that id must be the one the late deployment ends up with. /// public sealed class AdminOperationsClientDeployTimeoutTests : TestKit { // The deploy handler reads Cluster.Get(system).SelfAddress (the audit SourceNode), so the system needs the // cluster provider; without it the actor throws on the first StartDeployment and the message is lost. private const string ClusterHocon = """ akka { loglevel = "WARNING" actor.provider = "Akka.Cluster.ClusterActorRefProvider, Akka.Cluster" remote.dot-netty.tcp { hostname = "127.0.0.1", port = 0 } cluster { seed-nodes = [], roles = ["admin"], min-nr-of-members = 1, run-coordinated-shutdown-when-down = off } } """; public AdminOperationsClientDeployTimeoutTests() : base(ClusterHocon) { var cluster = Akka.Cluster.Cluster.Get(Sys); cluster.Join(cluster.SelfAddress); } /// A compose slower than the ask timeout yields OutcomeUnknown + the id the row later carries. [Fact] public async Task Ask_timeout_reports_OutcomeUnknown_with_the_id_the_late_deployment_carries() { var gated = new GatedFactory(AclScopeTestDb.Factory($"deploy-timeout-{Guid.NewGuid():N}")); var coordinator = CreateTestProbe("coordinator"); var actor = Sys.ActorOf(AdminOperationsActor.Props(gated, coordinator.Ref, Enumerable.Empty())); var registry = new ActorRegistry(); registry.Register(actor); var client = new AdminOperationsClient(registry, TimeSpan.FromMilliseconds(500)); gated.Hold(); var result = await client.StartDeploymentAsync("ci", CancellationToken.None); result.Outcome.ShouldBe(StartDeploymentOutcome.OutcomeUnknown); result.DeploymentId.ShouldNotBeNull(); result.Message.ShouldNotBeNull().ShouldContain(result.DeploymentId!.Value.Value.ToString()); // The compose was only slow: once it finishes, the deployment exists under exactly that id. gated.Release(); var dispatch = coordinator.ExpectMsg( TimeSpan.FromSeconds(10), cancellationToken: TestContext.Current.CancellationToken); dispatch.DeploymentId.ShouldBe(result.DeploymentId!.Value); await using var db = await gated.CreateDbContextAsync(TestContext.Current.CancellationToken); (await db.Deployments.AsNoTracking().SingleAsync(TestContext.Current.CancellationToken)) .DeploymentId.ShouldBe(result.DeploymentId.Value.Value); } /// A caller-initiated cancellation is still a cancellation, not an unknown outcome. [Fact] public async Task Caller_cancellation_still_throws() { var gated = new GatedFactory(AclScopeTestDb.Factory($"deploy-cancel-{Guid.NewGuid():N}")); var actor = Sys.ActorOf(AdminOperationsActor.Props( gated, CreateTestProbe("coordinator").Ref, Enumerable.Empty())); var registry = new ActorRegistry(); registry.Register(actor); var client = new AdminOperationsClient(registry, TimeSpan.FromSeconds(30)); using var cts = new CancellationTokenSource(); gated.Hold(); var pending = client.StartDeploymentAsync("ci", cts.Token); await cts.CancelAsync(); await Should.ThrowAsync(() => pending); gated.Release(); } private sealed class GatedFactory(IDbContextFactory inner) : IDbContextFactory { private volatile TaskCompletionSource? _gate; 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) { if (_gate is { } gate) await gate.Task.WaitAsync(cancellationToken); return inner.CreateDbContext(); } } }