fix(deploy): #949 ask timeout on the deploy trigger is OutcomeUnknown (202 + pre-allocated id), not a failure; slow-compose phase timings

The id is allocated before the ask and travels in StartDeployment, so a timed-out
caller can name and poll the deployment that may still go ahead. REST answers 202
OutcomeUnknown with a Location; the AdminUI shows 'could not be confirmed, may still
be created'; a retry during a running compose gets 409 naming the same id. The actor
logs per-phase timings for composes slower than 3 s.
This commit is contained in:
Joseph Doherty
2026-12-01 19:27:13 -05:00
parent 6b60845931
commit 88c9766ab5
14 changed files with 333 additions and 15 deletions
@@ -0,0 +1,102 @@
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;
/// <summary>
/// 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 <c>OutcomeUnknown</c> carrying the id it
/// pre-allocated, and that id must be the one the late deployment ends up with.
/// </summary>
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);
}
/// <summary>A compose slower than the ask timeout yields OutcomeUnknown + the id the row later carries.</summary>
[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<IDriverProbe>()));
var registry = new ActorRegistry();
registry.Register<AdminOperationsActorKey>(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<ZB.MOM.WW.OtOpcUa.Commons.Messages.Deploy.DispatchDeployment>(
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);
}
/// <summary>A caller-initiated cancellation is still a cancellation, not an unknown outcome.</summary>
[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<IDriverProbe>()));
var registry = new ActorRegistry();
registry.Register<AdminOperationsActorKey>(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<OperationCanceledException>(() => pending);
gated.Release();
}
private sealed class GatedFactory(IDbContextFactory<OtOpcUaConfigDbContext> inner) : IDbContextFactory<OtOpcUaConfigDbContext>
{
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<OtOpcUaConfigDbContext> CreateDbContextAsync(CancellationToken cancellationToken = default)
{
if (_gate is { } gate) await gate.Task.WaitAsync(cancellationToken);
return inner.CreateDbContext();
}
}
}