Files
lmxopcua/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.OpcUaClient.IntegrationTests/OpcUaClientSmokeTests.cs
T

202 lines
9.4 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
namespace ZB.MOM.WW.OtOpcUa.Driver.OpcUaClient.IntegrationTests;
/// <summary>
/// End-to-end smoke against a live <c>opc-plc</c> (task #215). Drives the real
/// OPC UA Secure Channel + Session + MonitoredItem exchange — no mocks. Every
/// test here proves a capability surface that loopback against our own server
/// couldn't exercise cleanly: real cert negotiation, real endpoint descriptions,
/// real simulated nodes that change without a write.
/// </summary>
[Collection(OpcPlcCollection.Name)]
[Trait("Category", "Integration")]
[Trait("Simulator", "opc-plc")]
public sealed class OpcUaClientSmokeTests(OpcPlcFixture sim)
{
/// <summary>Verifies that the client can connect and read a node through the real OPC UA stack.</summary>
[Fact]
public async Task Client_connects_and_reads_StepUp_node_through_real_OPC_UA_stack()
{
if (sim.SkipReason is not null) Assert.Skip(sim.SkipReason);
var options = OpcPlcProfile.BuildOptions(sim.EndpointUrl);
await using var drv = new OpcUaClientDriver(options, driverInstanceId: "opcua-smoke-read");
await drv.InitializeAsync("{}", TestContext.Current.CancellationToken);
var snapshots = await drv.ReadAsync(
[OpcPlcProfile.StepUpRef], TestContext.Current.CancellationToken);
snapshots.Count.ShouldBe(1);
snapshots[0].StatusCode.ShouldBe(0u, "opc-plc StepUp read must succeed end-to-end");
snapshots[0].Value.ShouldNotBeNull("StepUp always has a current value");
}
/// <summary>Verifies that the client can read a batch of varied types from the simulator.</summary>
[Fact]
public async Task Client_reads_batch_of_varied_types_from_live_simulator()
{
if (sim.SkipReason is not null) Assert.Skip(sim.SkipReason);
var options = OpcPlcProfile.BuildOptions(sim.EndpointUrl);
await using var drv = new OpcUaClientDriver(options, driverInstanceId: "opcua-smoke-batch");
await drv.InitializeAsync("{}", TestContext.Current.CancellationToken);
var snapshots = await drv.ReadAsync(
[OpcPlcProfile.StepUpRef, OpcPlcProfile.RandomSignedInt32Ref, OpcPlcProfile.AlternatingBooleanRef],
TestContext.Current.CancellationToken);
snapshots.Count.ShouldBe(3);
foreach (var s in snapshots)
{
s.StatusCode.ShouldBe(0u);
s.Value.ShouldNotBeNull();
}
// AlternatingBoolean should decode as a bool specifically — catches a common
// attribute-mapping regression where the driver stringifies variant values.
snapshots[2].Value.ShouldBeOfType<bool>();
}
/// <summary>Verifies that the client can subscribe to data changes from the live server.</summary>
[Fact]
public async Task Client_subscribe_receives_StepUp_data_changes_from_live_server()
{
if (sim.SkipReason is not null) Assert.Skip(sim.SkipReason);
var options = OpcPlcProfile.BuildOptions(sim.EndpointUrl);
await using var drv = new OpcUaClientDriver(options, driverInstanceId: "opcua-smoke-sub");
await drv.InitializeAsync("{}", TestContext.Current.CancellationToken);
var observed = new List<DataChangeEventArgs>();
var gate = new SemaphoreSlim(0);
drv.OnDataChange += (_, e) =>
{
lock (observed) observed.Add(e);
gate.Release();
};
var handle = await drv.SubscribeAsync(
[OpcPlcProfile.FastUInt1Ref], TimeSpan.FromMilliseconds(250),
TestContext.Current.CancellationToken);
// FastUInt1 ticks every 100 ms — one publishing interval (250 ms) should deliver.
// Wait up to 3 s to tolerate container warm-up + first-publish delay.
var got = await gate.WaitAsync(TimeSpan.FromSeconds(3), TestContext.Current.CancellationToken);
got.ShouldBeTrue("opc-plc FastUInt1 must publish at least one data change within 3s");
int observedCount;
lock (observed) observedCount = observed.Count;
observedCount.ShouldBeGreaterThan(0);
await drv.UnsubscribeAsync(handle, TestContext.Current.CancellationToken);
}
/// <summary>
/// #992 G-12 through the real SDK: a ref whose upstream node does not exist (the server refuses the monitored
/// item with <c>BadNodeIdUnknown</c> in the create RESULT, not an exception) used to get no publish, no log and
/// no status. It now gets exactly one Bad publish carrying the upstream's code, while the good ref beside it
/// still delivers values.
/// </summary>
[Fact]
public async Task A_subscribed_ref_the_server_refuses_gets_one_Bad_publish_with_the_upstream_code()
{
if (sim.SkipReason is not null) Assert.Skip(sim.SkipReason);
var ct = TestContext.Current.CancellationToken;
const string missingRef = "Sim/OpcUaClient/plc/DoesNotExist";
var options = OpcPlcProfile.BuildOptions(sim.EndpointUrl) with
{
RawTags =
[
.. OpcPlcProfile.RawTags,
new RawTagEntry(missingRef, System.Text.Json.JsonSerializer.Serialize(new { nodeId = "ns=3;s=DoesNotExist" }), WriteIdempotent: true),
],
};
await using var drv = new OpcUaClientDriver(options, driverInstanceId: "opcua-smoke-refused");
await drv.InitializeAsync("{}", ct);
var observed = new List<DataChangeEventArgs>();
drv.OnDataChange += (_, e) => { lock (observed) observed.Add(e); };
var handle = await drv.SubscribeAsync(
[OpcPlcProfile.FastUInt1Ref, missingRef], TimeSpan.FromMilliseconds(250), ct);
// Positive control: the good ref beside the refused one is live.
var deadline = DateTime.UtcNow.AddSeconds(5);
while (DateTime.UtcNow < deadline)
{
lock (observed)
if (observed.Any(o => o.FullReference == OpcPlcProfile.FastUInt1Ref && o.Snapshot.StatusCode == 0u)) break;
await Task.Delay(100, ct);
}
DataChangeEventArgs[] snapshot;
lock (observed) snapshot = [.. observed];
snapshot.ShouldContain(o => o.FullReference == OpcPlcProfile.FastUInt1Ref && o.Snapshot.StatusCode == 0u);
var refused = snapshot.Where(o => o.FullReference == missingRef).ToList();
refused.Count.ShouldBe(1, "exactly one Bad publish for the refused ref");
refused[0].Snapshot.StatusCode.ShouldBe(Opc.Ua.StatusCodes.BadNodeIdUnknown.Code);
refused[0].SubscriptionHandle.ShouldBe(handle);
await drv.UnsubscribeAsync(handle, ct);
}
/// <summary>
/// Review 2026-10-21 O-9 + lifecycle generation, through the real SDK: a shutdown with several live
/// subscriptions completes inside the one teardown budget, lands Unknown, and a re-initialise of the same
/// instance reconnects and reads.
/// </summary>
[Fact]
public async Task Shutdown_with_live_subscriptions_is_bounded_and_the_instance_reinitialises()
{
if (sim.SkipReason is not null) Assert.Skip(sim.SkipReason);
var ct = TestContext.Current.CancellationToken;
var options = OpcPlcProfile.BuildOptions(sim.EndpointUrl);
await using var drv = new OpcUaClientDriver(options, driverInstanceId: "opcua-smoke-lifecycle");
await drv.InitializeAsync("{}", ct);
for (var i = 0; i < 3; i++)
await drv.SubscribeAsync([OpcPlcProfile.FastUInt1Ref], TimeSpan.FromMilliseconds(250), ct);
var sw = System.Diagnostics.Stopwatch.StartNew();
await drv.ShutdownAsync(ct);
sw.Stop();
sw.Elapsed.ShouldBeLessThan(TimeSpan.FromSeconds(10), "one 4 s budget bounds the whole teardown");
drv.GetHealth().State.ShouldBe(DriverState.Unknown);
await drv.InitializeAsync("{}", ct);
var read = await drv.ReadAsync([OpcPlcProfile.StepUpRef], ct);
read.Single().StatusCode.ShouldBe(0u, "the re-initialised instance must be fully usable");
drv.GetHealth().State.ShouldBe(DriverState.Healthy);
}
/// <summary>
/// The health contract against a real server: a write the server REFUSES (a string into the UInt32 StepUp —
/// BadTypeMismatch) is a reply — the refusal status passes through and the driver stays Healthy with its stamp
/// advanced.
/// </summary>
[Fact]
public async Task A_server_refused_write_is_a_reply_and_keeps_the_driver_healthy()
{
if (sim.SkipReason is not null) Assert.Skip(sim.SkipReason);
var ct = TestContext.Current.CancellationToken;
var options = OpcPlcProfile.BuildOptions(sim.EndpointUrl);
await using var drv = new OpcUaClientDriver(options, driverInstanceId: "opcua-smoke-refused-write");
await drv.InitializeAsync("{}", ct);
var before = drv.GetHealth().LastSuccessfulRead.ShouldNotBeNull();
var deadline = DateTime.UtcNow + TimeSpan.FromSeconds(5);
while (DateTime.UtcNow <= before && DateTime.UtcNow < deadline) await Task.Yield();
var result = await drv.WriteAsync([new WriteRequest(OpcPlcProfile.StepUpRef, "not-a-number")], ct);
(result.Single().StatusCode & 0x80000000u).ShouldNotBe(0u, "opc-plc refuses a string written into a UInt32");
result.Single().StatusCode.ShouldNotBe(0x80050000u, "a server refusal is not a local comms error");
var h = drv.GetHealth();
h.State.ShouldBe(DriverState.Healthy);
h.LastSuccessfulRead.ShouldNotBeNull().ShouldBeGreaterThan(before);
}
}