Files
lmxopcua/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.AbCip.Tests/AbCipPollErrorHealthTests.cs
T
Joseph Doherty 6ee74d26fc
v2-ci / build (pull_request) Successful in 3m32s
v2-ci / unit-tests (pull_request) Failing after 9m27s
docs(r2-09): record execution deviations; clear CS8604 nullable warning in the 5 PollErrorHealthTests
2026-07-13 12:45:50 -04:00

61 lines
2.3 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Driver.AbCip;
namespace ZB.MOM.WW.OtOpcUa.Driver.AbCip.Tests;
/// <summary>
/// 05/STAB-9 — the poll engine's <c>onError</c> sink must be wired to the driver health
/// surface: a poll-loop reader failure degrades health while preserving
/// <see cref="DriverHealth.LastSuccessfulRead"/>, and must never downgrade a pre-existing
/// <see cref="DriverState.Faulted"/>.
/// </summary>
[Trait("Category", "Unit")]
public sealed class AbCipPollErrorHealthTests
{
/// <summary>A poll reader failure degrades health, preserving the last successful read timestamp.</summary>
[Fact]
public async Task PollReaderFailure_DegradesHealth_PreservesLastSuccessfulRead()
{
var opts = new AbCipDriverOptions
{
Devices = [new AbCipDeviceOptions("ab://10.0.0.5/1,0")],
Probe = new AbCipProbeOptions { Enabled = false },
};
var drv = new AbCipDriver(opts, "abcip-1", new FakeAbCipTagFactory());
await drv.InitializeAsync("{}", CancellationToken.None);
var before = drv.GetHealth();
before.State.ShouldBe(DriverState.Healthy);
before.LastSuccessfulRead.ShouldNotBeNull();
drv.HandlePollError(new InvalidOperationException("poll boom"));
var after = drv.GetHealth();
after.State.ShouldBe(DriverState.Degraded);
after.LastSuccessfulRead.ShouldBe(before.LastSuccessfulRead);
after.LastError.ShouldNotBeNull();
after.LastError.ShouldContain("poll boom");
}
/// <summary>A poll reader failure never downgrades a pre-existing Faulted state.</summary>
[Fact]
public async Task PollReaderFailure_NeverDowngradesFaulted()
{
var opts = new AbCipDriverOptions
{
Devices = [new AbCipDeviceOptions("not-a-valid-address")],
Probe = new AbCipProbeOptions { Enabled = false },
};
var drv = new AbCipDriver(opts, "abcip-1", new FakeAbCipTagFactory());
await Should.ThrowAsync<Exception>(() => drv.InitializeAsync("{}", CancellationToken.None));
drv.GetHealth().State.ShouldBe(DriverState.Faulted);
drv.HandlePollError(new InvalidOperationException("poll boom"));
drv.GetHealth().State.ShouldBe(DriverState.Faulted);
}
}