Task #139 — Modbus connection-layer config knobs (keep-alive / idle / reconnect)
Promotes the previously hardcoded transport-layer settings to ModbusDriverOptions so users can tune them through DriverConfig JSON without recompiling. Three new option groups: 1. KeepAlive (ModbusKeepAliveOptions): Enabled / Time / Interval / RetryCount. Defaults preserve the historical PR 53 wire output exactly (Enabled=true, Time=30s, Interval=10s, RetryCount=3). Set Enabled=false for PLCs that reject SO_KEEPALIVE. 2. IdleDisconnectTimeout (TimeSpan?): when set, the transport tracks last-PDU- success and proactively closes + reconnects on the next request after the threshold. Defends against silent NAT / firewall socket reaping. Default null = disabled (no behaviour change). 3. Reconnect (ModbusReconnectOptions): InitialDelay / MaxDelay / BackoffMultiplier for the post-drop reconnect loop. Defaults (InitialDelay=0, MaxDelay=30s, Multiplier=2.0) preserve the historical immediate-retry behaviour for the first attempt and add geometric backoff only if the reconnect itself fails. Capped at 10 attempts before propagating. ModbusTcpTransport ctor extended with optional keepAlive / idleDisconnect / reconnect parameters; existing 4-arg call sites continue to compile. Factory DTO gains parallel KeepAlive / IdleDisconnectMs / Reconnect fields with default-aware binding. 5 new ModbusConnectionOptionsTests covering the default-preservation contract (every default field matches pre-#139) and the JSON-binding round-trip for each knob group. Existing 204 unit tests still green.
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Driver.Modbus.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// #139 connection-layer config knobs: keep-alive, idle-disconnect, reconnect backoff.
|
||||
/// Coverage focuses on default behaviour (matches pre-#139 wire output exactly) and the
|
||||
/// DTO-binding path so users can drive these from JSON without editing C#.
|
||||
/// </summary>
|
||||
[Trait("Category", "Unit")]
|
||||
public sealed class ModbusConnectionOptionsTests
|
||||
{
|
||||
[Fact]
|
||||
public void Defaults_Match_Historical_Behaviour()
|
||||
{
|
||||
var opts = new ModbusDriverOptions();
|
||||
|
||||
opts.KeepAlive.Enabled.ShouldBeTrue();
|
||||
opts.KeepAlive.Time.ShouldBe(TimeSpan.FromSeconds(30));
|
||||
opts.KeepAlive.Interval.ShouldBe(TimeSpan.FromSeconds(10));
|
||||
opts.KeepAlive.RetryCount.ShouldBe(3);
|
||||
|
||||
opts.IdleDisconnectTimeout.ShouldBeNull();
|
||||
|
||||
opts.Reconnect.InitialDelay.ShouldBe(TimeSpan.Zero);
|
||||
opts.Reconnect.MaxDelay.ShouldBe(TimeSpan.FromSeconds(30));
|
||||
opts.Reconnect.BackoffMultiplier.ShouldBe(2.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Factory_Reads_KeepAlive_Knobs_From_Json()
|
||||
{
|
||||
const string json = """
|
||||
{
|
||||
"host": "10.0.0.10",
|
||||
"tags": [],
|
||||
"keepAlive": { "enabled": false, "timeMs": 60000, "intervalMs": 5000, "retryCount": 5 }
|
||||
}
|
||||
""";
|
||||
var driver = ModbusDriverFactoryExtensions.CreateInstance("modbus-1", json);
|
||||
// Reach into options via reflection — the factory's options field is internal.
|
||||
var opts = (ModbusDriverOptions)typeof(ModbusDriver)
|
||||
.GetField("_options", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!
|
||||
.GetValue(driver)!;
|
||||
|
||||
opts.KeepAlive.Enabled.ShouldBeFalse();
|
||||
opts.KeepAlive.Time.ShouldBe(TimeSpan.FromMinutes(1));
|
||||
opts.KeepAlive.Interval.ShouldBe(TimeSpan.FromSeconds(5));
|
||||
opts.KeepAlive.RetryCount.ShouldBe(5);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Factory_Reads_IdleDisconnect_From_Json()
|
||||
{
|
||||
const string json = """{ "host": "10.0.0.10", "tags": [], "idleDisconnectMs": 120000 }""";
|
||||
var driver = ModbusDriverFactoryExtensions.CreateInstance("modbus-1", json);
|
||||
var opts = (ModbusDriverOptions)typeof(ModbusDriver)
|
||||
.GetField("_options", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!
|
||||
.GetValue(driver)!;
|
||||
|
||||
opts.IdleDisconnectTimeout.ShouldBe(TimeSpan.FromMinutes(2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Factory_Reads_Reconnect_Backoff_From_Json()
|
||||
{
|
||||
const string json = """
|
||||
{
|
||||
"host": "10.0.0.10",
|
||||
"tags": [],
|
||||
"reconnect": { "initialDelayMs": 500, "maxDelayMs": 60000, "backoffMultiplier": 1.5 }
|
||||
}
|
||||
""";
|
||||
var driver = ModbusDriverFactoryExtensions.CreateInstance("modbus-1", json);
|
||||
var opts = (ModbusDriverOptions)typeof(ModbusDriver)
|
||||
.GetField("_options", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!
|
||||
.GetValue(driver)!;
|
||||
|
||||
opts.Reconnect.InitialDelay.ShouldBe(TimeSpan.FromMilliseconds(500));
|
||||
opts.Reconnect.MaxDelay.ShouldBe(TimeSpan.FromMinutes(1));
|
||||
opts.Reconnect.BackoffMultiplier.ShouldBe(1.5);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Factory_With_Empty_Json_Uses_All_Defaults()
|
||||
{
|
||||
const string json = """{ "host": "10.0.0.10", "tags": [] }""";
|
||||
var driver = ModbusDriverFactoryExtensions.CreateInstance("modbus-1", json);
|
||||
var opts = (ModbusDriverOptions)typeof(ModbusDriver)
|
||||
.GetField("_options", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!
|
||||
.GetValue(driver)!;
|
||||
|
||||
// Every connection-layer field must match the historical defaults so existing config
|
||||
// rows stay bit-for-bit identical after #139.
|
||||
opts.KeepAlive.Enabled.ShouldBeTrue();
|
||||
opts.IdleDisconnectTimeout.ShouldBeNull();
|
||||
opts.Reconnect.InitialDelay.ShouldBe(TimeSpan.Zero);
|
||||
opts.AutoReconnect.ShouldBeTrue();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user