388291d18a
S-1: ModbusRtuOverTcpTransport.SendOnceAsync tears the socket down on any exit after the write without a validated reply (caller cancellation included), so a late reply can no longer answer the next request at Good. S-2: SubscribeAsync/SubscribeAcquisitionAsync drop the deadband baselines of their refs before polling starts. S-8: a family-recognised prefix whose body the family parser rejects no longer falls through to the generic mnemonic table.
102 lines
5.4 KiB
C#
102 lines
5.4 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.Modbus.Tests;
|
|
|
|
/// <summary>
|
|
/// Review 07 H2: the deadband baseline is keyed on (value, status). A Good-after-Bad recovery carrying
|
|
/// the IDENTICAL value must publish — the value-only dedup left the node Bad until the value drifted.
|
|
/// </summary>
|
|
[Trait("Category", "Unit")]
|
|
public sealed class ModbusDeadbandRecoveryTests
|
|
{
|
|
private const uint Good = 0u;
|
|
private const uint Uncertain = 0x40000000u;
|
|
private const uint BadComm = 0x80050000u;
|
|
|
|
private static async Task<ModbusDriver> NewDriverAsync()
|
|
{
|
|
var tag = new ModbusTagDefinition("Temp", ModbusRegion.HoldingRegisters, 0, ModbusDataType.Int16, Deadband: 5.0);
|
|
var opts = new ModbusDriverOptions
|
|
{
|
|
Host = "f", RawTags = ModbusRawTags.Entries([tag]), Probe = new ModbusProbeOptions { Enabled = false },
|
|
};
|
|
var drv = new ModbusDriver(opts, "m1", _ => new ModbusDriverTests.FakeTransport());
|
|
await drv.InitializeAsync("{}", TestContext.Current.CancellationToken);
|
|
return drv;
|
|
}
|
|
|
|
private static DataValueSnapshot Snap(object? v, uint status) => new(v, status, DateTime.UtcNow, DateTime.UtcNow);
|
|
|
|
[Fact]
|
|
public async Task Good_after_Bad_with_the_same_value_publishes()
|
|
{
|
|
await using var drv = await NewDriverAsync();
|
|
drv.ShouldPublish("Temp", Snap((short)10, Good)).ShouldBeTrue(); // baseline
|
|
drv.ShouldPublish("Temp", Snap(null, BadComm)).ShouldBeTrue(); // comms drop
|
|
drv.ShouldPublish("Temp", Snap((short)10, Good)).ShouldBeTrue("the recovery publish must not be swallowed");
|
|
// Positive control: the deadband is still active after the recovery re-seeded the baseline.
|
|
drv.ShouldPublish("Temp", Snap((short)11, Good)).ShouldBeFalse();
|
|
drv.ShouldPublish("Temp", Snap((short)16, Good)).ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_status_change_with_the_same_value_publishes()
|
|
{
|
|
await using var drv = await NewDriverAsync();
|
|
drv.ShouldPublish("Temp", Snap((short)10, Good)).ShouldBeTrue();
|
|
drv.ShouldPublish("Temp", Snap((short)10, Uncertain)).ShouldBeTrue("a quality change must publish");
|
|
drv.ShouldPublish("Temp", Snap((short)12, Uncertain)).ShouldBeFalse("within deadband at the same quality");
|
|
drv.ShouldPublish("Temp", Snap((short)10, Good)).ShouldBeTrue("back to Good must publish");
|
|
}
|
|
|
|
/// <summary>
|
|
/// #981 / S-2: a re-subscribe (a full rebuild keeps the driver and re-subscribes onto fresh nodes at
|
|
/// BadWaitingForInitialData) must publish its initial value even when it is inside the deadband of what
|
|
/// an EARLIER subscription published. Replays the IDENTICAL device value on purpose.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task Resubscribe_publishes_the_initial_value_even_inside_the_deadband_of_the_previous_subscription()
|
|
{
|
|
var ct = TestContext.Current.CancellationToken;
|
|
var tag = new ModbusTagDefinition("Temp", ModbusRegion.HoldingRegisters, 0, ModbusDataType.Int16, Deadband: 5.0);
|
|
var opts = new ModbusDriverOptions
|
|
{
|
|
Host = "f", RawTags = ModbusRawTags.Entries([tag]), Probe = new ModbusProbeOptions { Enabled = false },
|
|
};
|
|
var fake = new ModbusDriverTests.FakeTransport();
|
|
fake.HoldingRegisters[0] = 10;
|
|
await using var drv = new ModbusDriver(opts, "m1", _ => fake);
|
|
await drv.InitializeAsync("{}", ct);
|
|
|
|
var changes = new System.Collections.Concurrent.ConcurrentQueue<DataChangeEventArgs>();
|
|
var batches = new System.Collections.Concurrent.ConcurrentQueue<DataChangeBatchEventArgs>();
|
|
drv.OnDataChange += (_, e) => changes.Enqueue(e);
|
|
((IBatchSubscribable)drv).OnDataChangeBatch += (_, e) => batches.Enqueue(e);
|
|
|
|
static async Task WaitAsync(Func<bool> cond, CancellationToken ct)
|
|
{
|
|
var until = DateTime.UtcNow.AddSeconds(5);
|
|
while (!cond() && DateTime.UtcNow < until) await Task.Delay(20, ct);
|
|
cond().ShouldBeTrue("timed out waiting for the initial publish");
|
|
}
|
|
|
|
var first = await drv.SubscribeAsync(["Temp"], TimeSpan.FromMilliseconds(100), ct);
|
|
await WaitAsync(() => changes.Any(c => ReferenceEquals(c.SubscriptionHandle, first)) && batches.Any(b => ReferenceEquals(b.SubscriptionHandle, first)), ct);
|
|
|
|
await drv.UnsubscribeAsync(first, ct);
|
|
// Same device value (still inside the deadband) on a brand-new subscription: the initial publish must arrive.
|
|
var second = await drv.SubscribeAsync(["Temp"], TimeSpan.FromMilliseconds(100), ct);
|
|
await WaitAsync(() => changes.Any(c => ReferenceEquals(c.SubscriptionHandle, second)) && batches.Any(b => ReferenceEquals(b.SubscriptionHandle, second)), ct);
|
|
changes.First(c => ReferenceEquals(c.SubscriptionHandle, second)).Snapshot.StatusCode.ShouldBe(Good);
|
|
|
|
// Positive control: once past the initial raise the deadband still suppresses an in-band drift.
|
|
var before = changes.Count(c => ReferenceEquals(c.SubscriptionHandle, second));
|
|
fake.HoldingRegisters[0] = 11;
|
|
await Task.Delay(400, ct);
|
|
changes.Count(c => ReferenceEquals(c.SubscriptionHandle, second)).ShouldBe(before, "the deadband must still apply after the initial raise");
|
|
await drv.UnsubscribeAsync(second, ct);
|
|
}
|
|
}
|