Files
lmxopcua/tests/ZB.MOM.WW.OtOpcUa.Server.Tests/ServerRedundancyNodeWriterTests.cs
Joseph Doherty 69e0d02c72 task-galaxy-e2e branch — non-FOCAS work-in-progress snapshot
Catch-all commit for pending work on the task-galaxy-e2e branch that
wasn't part of the FOCAS migration. Grouping by topic so future per-topic
commits can be cherry-picked if needed.

TwinCAT
- src/.../Driver.TwinCAT/AdsTwinCATClient.cs + TwinCATDriverFactoryExtensions.cs:
  factory-registration extensions + ADS client refinements.
- src/.../Driver.TwinCAT.Cli/Commands/BrowseCommand.cs: new browse command
  for the TwinCAT test-client CLI.
- tests/.../Driver.TwinCAT.IntegrationTests/TwinCAT3SmokeTests.cs + TwinCatProject/:
  fixture scaffold with a minimal POU + README pointing at the TCBSD/ESXi
  VM for e2e.
- docs/Driver.TwinCAT.Cli.md + docs/drivers/TwinCAT-Test-Fixture.md:
  documentation for the above.
- docs/v3/twincat-backlog.md: forward-looking backlog seed.

Admin UI + fleet status
- src/.../Admin/Components/Pages/Clusters/DriversTab.razor + Hosts.razor:
  UI refresh for fleet-status rendering.
- src/.../Admin/Hubs/FleetStatusHub.cs + FleetStatusPoller.cs +
  Admin/Program.cs: SignalR hub + poller plumbing for live fleet data.
- tests/.../Admin.Tests/FleetStatusPollerTests.cs: poller coverage.

Server + redundancy runtime (Phase 6.3 follow-ups)
- src/.../Server/Hosting/RedundancyPublisherHostedService.cs: HostedService
  that owns the RedundancyStatePublisher lifecycle + wires peer reachability.
- src/.../Server/Redundancy/ServerRedundancyNodeWriter.cs: OPC UA
  variable-node writer binding ServiceLevel + ServerUriArray to the
  publisher's events.
- src/.../Server/Program.cs + Server.csproj: hosted-service registration.
- tests/.../Server.Tests/ServerRedundancyNodeWriterTests.cs +
  Server.Tests.csproj: coverage for the above.

Configuration
- src/.../Configuration/Validation/DraftValidator.cs +
  tests/.../Configuration.Tests/DraftValidatorTests.cs: draft-validation
  refinements.

E2E scripts (shared infrastructure)
- scripts/e2e/README.md + _common.ps1 + test-all.ps1: shared helpers + the
  all-drivers test-all runner.
- scripts/e2e/test-opcuaclient.ps1: OPC UA Client e2e runner.

Docs
- docs/v2/implementation/phase-6-{1,2,3,4}*.md + exit-gate-phase-{3,7}.md:
  phase-gate + implementation doc updates.
- docs/v2/plan.md: top-level plan refresh.
- docs/v2/redundancy-interop-playbook.md: client interop playbook for the
  Phase 6.3 redundancy-runtime work.

Two orphan FOCAS docs remain on disk but deliberately unstaged —
docs/v2/focas-deployment.md and docs/v2/implementation/focas-simulator-plan.md
describe the now-retired Tier-C topology and should either be rewritten
or deleted in a follow-up.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-24 14:12:19 -04:00

126 lines
4.8 KiB
C#

using System.Reflection;
using Microsoft.Extensions.Logging.Abstractions;
using Opc.Ua;
using Opc.Ua.Server;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Server.Redundancy;
using ConfigRedundancyMode = ZB.MOM.WW.OtOpcUa.Configuration.Enums.RedundancyMode;
namespace ZB.MOM.WW.OtOpcUa.Server.Tests;
/// <summary>
/// Unit coverage for <see cref="ServerRedundancyNodeWriter"/>. Uses a <see cref="DispatchProxy"/>
/// stand-in for <see cref="IServerInternal"/> — the writer only needs <c>ServerObject</c> +
/// <c>DefaultSystemContext</c>, so we stub just those and let every other member return
/// null (the writer never touches anything else).
/// </summary>
public sealed class ServerRedundancyNodeWriterTests
{
[Fact]
public void ApplyServiceLevel_sets_node_value_and_dedupes_unchanged()
{
var env = BuildEnv();
env.Writer.ApplyServiceLevel(200);
env.ServerObject.ServiceLevel.Value.ShouldBe((byte)200);
var timestampAfterFirst = env.ServerObject.ServiceLevel.Timestamp;
// Same value — writer should early-out without touching Timestamp.
Thread.Sleep(5);
env.Writer.ApplyServiceLevel(200);
env.ServerObject.ServiceLevel.Timestamp.ShouldBe(timestampAfterFirst);
env.Writer.ApplyServiceLevel(150);
env.ServerObject.ServiceLevel.Value.ShouldBe((byte)150);
env.ServerObject.ServiceLevel.Timestamp.ShouldBeGreaterThan(timestampAfterFirst);
}
[Fact]
public void ApplyRedundancySupport_maps_config_enum()
{
var env = BuildEnv();
env.Writer.ApplyRedundancySupport(ConfigRedundancyMode.Warm);
env.ServerObject.ServerRedundancy.RedundancySupport.Value.ShouldBe(RedundancySupport.Warm);
env.Writer.ApplyRedundancySupport(ConfigRedundancyMode.Hot);
env.ServerObject.ServerRedundancy.RedundancySupport.Value.ShouldBe(RedundancySupport.Hot);
env.Writer.ApplyRedundancySupport(ConfigRedundancyMode.None);
env.ServerObject.ServerRedundancy.RedundancySupport.Value.ShouldBe(RedundancySupport.None);
}
[Fact]
public void ApplyServerUriArray_writes_when_non_transparent_state_present()
{
var env = BuildEnv(nonTransparent: true);
env.Writer.ApplyServerUriArray(["urn:self", "urn:peer"]);
var ntr = (NonTransparentRedundancyState)env.ServerObject.ServerRedundancy;
ntr.ServerUriArray.Value.ShouldBe(new[] { "urn:self", "urn:peer" });
var ts = ntr.ServerUriArray.Timestamp;
Thread.Sleep(5);
env.Writer.ApplyServerUriArray(["urn:self", "urn:peer"]); // dedupe
ntr.ServerUriArray.Timestamp.ShouldBe(ts);
env.Writer.ApplyServerUriArray(["urn:self", "urn:peer", "urn:peer2"]);
ntr.ServerUriArray.Value.Length.ShouldBe(3);
}
[Fact]
public void ApplyServerUriArray_skips_silently_on_base_redundancy_type()
{
var env = BuildEnv(nonTransparent: false);
Should.NotThrow(() => env.Writer.ApplyServerUriArray(["urn:self"]));
env.ServerObject.ServerRedundancy.ShouldBeOfType<ServerRedundancyState>();
}
private static Env BuildEnv(bool nonTransparent = false)
{
var serverObject = new ServerObjectState(parent: null)
{
ServiceLevel = new PropertyState<byte>(null),
};
serverObject.ServerRedundancy = nonTransparent
? new NonTransparentRedundancyState(serverObject)
{
RedundancySupport = new PropertyState<RedundancySupport>(null),
ServerUriArray = new PropertyState<string[]>(null),
}
: new ServerRedundancyState(serverObject)
{
RedundancySupport = new PropertyState<RedundancySupport>(null),
};
var proxy = DispatchProxy.Create<IServerInternal, FakeServerInternalProxy>();
var fake = (FakeServerInternalProxy)(object)proxy;
fake.ServerObjectValue = serverObject;
fake.DefaultSystemContextValue = new ServerSystemContext(proxy);
var writer = new ServerRedundancyNodeWriter(proxy, NullLogger<ServerRedundancyNodeWriter>.Instance);
return new Env(proxy, serverObject, writer);
}
private sealed record Env(
IServerInternal Server,
ServerObjectState ServerObject,
ServerRedundancyNodeWriter Writer);
public class FakeServerInternalProxy : DispatchProxy
{
public ServerObjectState? ServerObjectValue;
public ISystemContext? DefaultSystemContextValue;
protected override object? Invoke(MethodInfo? targetMethod, object?[]? args) =>
targetMethod?.Name switch
{
"get_ServerObject" => ServerObjectValue,
"get_DefaultSystemContext" => DefaultSystemContextValue,
_ => null,
};
}
}