Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.ControlPlane.Tests/AdminOperationsActorDriverOperationTests.cs
T
Joseph Doherty ea976ffa81 fix(perf): #996 Wave 4 — blob-free health probes, narrow data-connections load, off-thread coordinator acks + AdminOps audit, events page cap, capped reservations page
C-6: DeploymentQueries projects Deployment without ArtifactBlob (DbHealthProbe, readiness, ClusterOverview, DriverHost boot).
C-11/A-5: DraftSnapshotFactory.FromConfigDbForDataConnectionsAsync + one-parse-per-artifact LoadAppliedAsync + capped pickers.
H-6: ConfigPublishCoordinator persists acks off the actor thread (AckPersisted); deadline backlog kicked in parallel.
H-3: Restart/Reconnect reply = dispatch; ConfigEdit audit written off-mailbox, failure logged not surfaced.
U-6: ServerHistorian:MaxEventsPerRead (default 10000) pages HistoryReadEvents; advertised as MaxReturnEventValues.
A-8: /reservations active-only by default, projected, capped at 200.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-12-03 10:58:50 -05:00

115 lines
5.6 KiB
C#

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;
/// <summary>
/// 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.
/// </summary>
public sealed class AdminOperationsActorDriverOperationTests : ControlPlaneActorTestBase
{
private static readonly TimeSpan AckBudget = TimeSpan.FromSeconds(1);
private (IActorRef Actor, Akka.TestKit.TestProbe Router) Create(IDbContextFactory<OtOpcUaConfigDbContext> factory)
{
var router = CreateTestProbe("router");
var coordinator = CreateTestProbe("coord");
var actor = Sys.ActorOf(AdminOperationsActor.Props(factory, coordinator.Ref, Enumerable.Empty<IDriverProbe>(), meshRouter: router.Ref));
return (actor, router);
}
/// <summary>The audit write throws: the command still went out and the reply says so (success).</summary>
[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<MeshCommand>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
if (restart) command.Message.ShouldBeOfType<RestartDriver>();
else command.Message.ShouldBeOfType<ReconnectDriver>();
if (restart)
{
var reply = ExpectMsg<RestartDriverResult>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
reply.Ok.ShouldBeTrue();
reply.CorrelationId.ShouldBe(correlation);
}
else
{
var reply = ExpectMsg<ReconnectDriverResult>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
reply.Ok.ShouldBeTrue();
reply.CorrelationId.ShouldBe(correlation);
}
}
/// <summary>
/// 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).
/// </summary>
[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<RestartDriverResult>(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<ReconnectDriverResult>(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<MeshCommand>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
}
private sealed class ThrowingAsyncFactory(IDbContextFactory<OtOpcUaConfigDbContext> inner) : IDbContextFactory<OtOpcUaConfigDbContext>
{
public OtOpcUaConfigDbContext CreateDbContext() => inner.CreateDbContext();
public Task<OtOpcUaConfigDbContext> CreateDbContextAsync(CancellationToken cancellationToken = default) =>
throw new InvalidOperationException("ConfigDb unreachable (simulated)");
}
private sealed class GatedAsyncFactory(IDbContextFactory<OtOpcUaConfigDbContext> inner) : IDbContextFactory<OtOpcUaConfigDbContext>
{
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<OtOpcUaConfigDbContext> CreateDbContextAsync(CancellationToken cancellationToken = default)
{
Entered.Set();
if (_gate is { } gate) await gate.Task.WaitAsync(cancellationToken);
return inner.CreateDbContext();
}
}
}