Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/AdminOperationsClientDeployTimeoutTests.cs
T

135 lines
6.6 KiB
C#

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>
/// #1024: the deploy trigger has its own, longer ask timeout — a cold Roslyn compile on a large fleet can
/// outlast the 10 s used for ack/shelve. A compose slower than the short timeout but inside the deploy one
/// is a normal Accepted, not an OutcomeUnknown.
/// </summary>
[Fact]
public async Task Deploy_uses_its_own_longer_timeout_so_a_slow_compose_is_still_Accepted()
{
var gated = new GatedFactory(AclScopeTestDb.Factory($"deploy-longer-{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(300), TimeSpan.FromSeconds(30));
gated.Hold();
var pending = client.StartDeploymentAsync("ci", CancellationToken.None);
await Task.Delay(800, TestContext.Current.CancellationToken); // past the short timeout
pending.IsCompleted.ShouldBeFalse();
gated.Release();
(await pending).Outcome.ShouldBe(StartDeploymentOutcome.Accepted);
}
/// <summary>The production default for the deploy trigger is longer than the general ask timeout.</summary>
[Fact]
public void Default_deploy_timeout_exceeds_the_general_ask_timeout()
{
AdminOperationsClient.DefaultDeployAskTimeout.ShouldBeGreaterThan(AdminOperationsClient.DefaultAskTimeout);
AdminOperationsClient.DefaultDeployAskTimeout.ShouldBeGreaterThanOrEqualTo(TimeSpan.FromSeconds(30));
}
/// <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();
}
}
}