Fix second-pass review findings: subscription leak on rebuild, metrics accuracy, and MxAccess startup recovery
- Preserve and replay subscription ref counts across address space rebuilds to prevent MXAccess subscription leaks - Mark read timeouts and write failures as unsuccessful in PerformanceMetrics for accurate health reporting - Add deferred MxAccess reconnect path when initial connection fails at startup - Update code review document with verified completions and new findings - Add covering tests for all fixes Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -29,6 +29,7 @@ namespace ZB.MOM.WW.LmxOpcUa.Tests.Helpers
|
||||
public int UnregisterCallCount { get; private set; }
|
||||
public bool ShouldFailRegister { get; set; }
|
||||
public bool ShouldFailWrite { get; set; }
|
||||
public bool SkipWriteCompleteCallback { get; set; }
|
||||
public int WriteCompleteStatus { get; set; } = 0; // 0 = success
|
||||
|
||||
public int Register(string clientName)
|
||||
@@ -87,7 +88,8 @@ namespace ZB.MOM.WW.LmxOpcUa.Tests.Helpers
|
||||
status[0].success = 0;
|
||||
status[0].detail = (short)WriteCompleteStatus;
|
||||
}
|
||||
OnWriteComplete?.Invoke(_connectionHandle, itemHandle, ref status);
|
||||
if (!SkipWriteCompleteCallback)
|
||||
OnWriteComplete?.Invoke(_connectionHandle, itemHandle, ref status);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -259,5 +259,32 @@ namespace ZB.MOM.WW.LmxOpcUa.Tests.Integration
|
||||
await fixture.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Rebuild_PreservesSubscriptionBookkeeping_ForSurvivingNodes()
|
||||
{
|
||||
var fixture = OpcUaServerFixture.WithFakeMxAccessClient();
|
||||
await fixture.InitializeAsync();
|
||||
try
|
||||
{
|
||||
var nodeManager = fixture.Service.NodeManagerInstance!;
|
||||
var mxClient = fixture.MxAccessClient!;
|
||||
|
||||
nodeManager.SubscribeTag("TestMachine_001.MachineID");
|
||||
mxClient.ActiveSubscriptionCount.ShouldBe(1);
|
||||
|
||||
fixture.Service.TriggerRebuild();
|
||||
await Task.Delay(200);
|
||||
|
||||
mxClient.ActiveSubscriptionCount.ShouldBe(1);
|
||||
|
||||
nodeManager.UnsubscribeTag("TestMachine_001.MachineID");
|
||||
mxClient.ActiveSubscriptionCount.ShouldBe(0);
|
||||
}
|
||||
finally
|
||||
{
|
||||
await fixture.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,6 +69,20 @@ namespace ZB.MOM.WW.LmxOpcUa.Tests.MxAccess
|
||||
result.Quality.ShouldBe(Quality.BadCommFailure);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Read_Timeout_RecordsFailedMetrics()
|
||||
{
|
||||
await _client.ConnectAsync();
|
||||
|
||||
var result = await _client.ReadAsync("TestTag.Attr");
|
||||
result.Quality.ShouldBe(Quality.BadCommFailure);
|
||||
|
||||
var stats = _metrics.GetStatistics();
|
||||
stats.ShouldContainKey("Read");
|
||||
stats["Read"].TotalCount.ShouldBe(1);
|
||||
stats["Read"].SuccessCount.ShouldBe(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Write_NotConnected_ReturnsFalse()
|
||||
{
|
||||
@@ -97,6 +111,21 @@ namespace ZB.MOM.WW.LmxOpcUa.Tests.MxAccess
|
||||
result.ShouldBe(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Write_Timeout_ReturnsFalse_AndRecordsFailedMetrics()
|
||||
{
|
||||
await _client.ConnectAsync();
|
||||
_proxy.SkipWriteCompleteCallback = true;
|
||||
|
||||
var result = await _client.WriteAsync("TestTag.Attr", 42);
|
||||
result.ShouldBe(false);
|
||||
|
||||
var stats = _metrics.GetStatistics();
|
||||
stats.ShouldContainKey("Write");
|
||||
stats["Write"].TotalCount.ShouldBe(1);
|
||||
stats["Write"].SuccessCount.ShouldBe(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Read_RecordsMetrics()
|
||||
{
|
||||
|
||||
@@ -102,6 +102,42 @@ namespace ZB.MOM.WW.LmxOpcUa.Tests.MxAccess
|
||||
callbackInvoked.ShouldBe(true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task OneShotRead_DoesNotRemovePersistentSubscription_OnReconnect()
|
||||
{
|
||||
await _client.ConnectAsync();
|
||||
var callbackInvoked = false;
|
||||
await _client.SubscribeAsync("TestTag.Attr", (_, _) => callbackInvoked = true);
|
||||
|
||||
var readTask = _client.ReadAsync("TestTag.Attr");
|
||||
await Task.Delay(50);
|
||||
_proxy.SimulateDataChangeByAddress("TestTag.Attr", 42, 192);
|
||||
(await readTask).Value.ShouldBe(42);
|
||||
callbackInvoked = false;
|
||||
|
||||
await _client.ReconnectAsync();
|
||||
|
||||
_proxy.SimulateDataChangeByAddress("TestTag.Attr", "after_reconnect", 192);
|
||||
callbackInvoked.ShouldBe(true);
|
||||
_client.ActiveSubscriptionCount.ShouldBe(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task OneShotWrite_DoesNotBreakPersistentUnsubscribe()
|
||||
{
|
||||
await _client.ConnectAsync();
|
||||
await _client.SubscribeAsync("TestTag.Attr", (_, _) => { });
|
||||
_proxy.Items.Values.ShouldContain("TestTag.Attr");
|
||||
|
||||
var writeResult = await _client.WriteAsync("TestTag.Attr", 7);
|
||||
writeResult.ShouldBe(true);
|
||||
|
||||
await _client.UnsubscribeAsync("TestTag.Attr");
|
||||
|
||||
_client.ActiveSubscriptionCount.ShouldBe(0);
|
||||
_proxy.Items.Values.ShouldNotContain("TestTag.Attr");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ProbeTag_SubscribedOnConnect()
|
||||
{
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.LmxOpcUa.Host;
|
||||
@@ -69,5 +70,60 @@ namespace ZB.MOM.WW.LmxOpcUa.Tests.Wiring
|
||||
service.Stop();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Start_WhenMxAccessIsInitiallyDown_MonitorReconnectsInBackground()
|
||||
{
|
||||
var config = new AppConfiguration
|
||||
{
|
||||
OpcUa = new OpcUaConfiguration
|
||||
{
|
||||
Port = 14841,
|
||||
GalaxyName = "TestGalaxy",
|
||||
EndpointPath = "/LmxOpcUa"
|
||||
},
|
||||
MxAccess = new MxAccessConfiguration
|
||||
{
|
||||
ClientName = "Test",
|
||||
MonitorIntervalSeconds = 1,
|
||||
AutoReconnect = true
|
||||
},
|
||||
GalaxyRepository = new GalaxyRepositoryConfiguration(),
|
||||
Dashboard = new DashboardConfiguration { Enabled = false }
|
||||
};
|
||||
|
||||
var proxy = new FakeMxProxy { ShouldFailRegister = true };
|
||||
var repo = new FakeGalaxyRepository
|
||||
{
|
||||
Hierarchy = new List<GalaxyObjectInfo>
|
||||
{
|
||||
new GalaxyObjectInfo { GobjectId = 1, TagName = "TestObj", BrowseName = "TestObj", ParentGobjectId = 0, IsArea = false }
|
||||
},
|
||||
Attributes = new List<GalaxyAttributeInfo>
|
||||
{
|
||||
new GalaxyAttributeInfo { GobjectId = 1, TagName = "TestObj", AttributeName = "TestAttr", FullTagReference = "TestObj.TestAttr", MxDataType = 5, IsArray = false }
|
||||
}
|
||||
};
|
||||
|
||||
var service = new OpcUaService(config, proxy, repo);
|
||||
service.Start();
|
||||
|
||||
try
|
||||
{
|
||||
service.ServerHost.ShouldNotBeNull();
|
||||
service.MxClient.ShouldNotBeNull();
|
||||
service.MxClient!.State.ShouldBe(ConnectionState.Error);
|
||||
|
||||
proxy.ShouldFailRegister = false;
|
||||
await Task.Delay(2500);
|
||||
|
||||
service.MxClient.State.ShouldBe(ConnectionState.Connected);
|
||||
proxy.RegisterCallCount.ShouldBeGreaterThan(1);
|
||||
}
|
||||
finally
|
||||
{
|
||||
service.Stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user