49 lines
2.0 KiB
C#
49 lines
2.0 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.TwinCAT.Tests;
|
|
|
|
/// <summary>
|
|
/// #630 (same pattern) — <c>InitializeAsync</c> on a live driver overwrote <c>_devices[host]</c> and
|
|
/// orphaned the previous state's probe loop + client. It now releases them first.
|
|
/// </summary>
|
|
[Trait("Category", "Unit")]
|
|
public sealed class TwinCATReinitializeReleasesStateTests
|
|
{
|
|
/// <summary>Initialize twice: the first state's probe loop has finished and its client is disposed.</summary>
|
|
[Fact]
|
|
public async Task SecondInitialize_StopsFirstStatesLoop_AndDisposesItsClient()
|
|
{
|
|
const string host = "ads://5.23.91.23.1.1:851";
|
|
var factory = new FakeTwinCATClientFactory();
|
|
var drv = new TwinCATDriver(new TwinCATDriverOptions
|
|
{
|
|
Devices = [new TwinCATDeviceOptions(host)],
|
|
Probe = new TwinCATProbeOptions { Enabled = true, Interval = TimeSpan.FromMilliseconds(20), Timeout = TimeSpan.FromSeconds(1) },
|
|
}, "drv-reinit", factory);
|
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
|
|
|
var first = drv.GetDeviceState(host)!;
|
|
// Positive control: the first probe loop is running and holds a connected client.
|
|
var deadline = DateTime.UtcNow.AddSeconds(5);
|
|
while (!(factory.Clients.Count > 0 && factory.Clients[0].IsConnected))
|
|
{
|
|
if (DateTime.UtcNow > deadline) throw new TimeoutException("probe never connected");
|
|
await Task.Delay(10);
|
|
}
|
|
var firstClient = factory.Clients[0];
|
|
var firstLoop = first.ProbeTask;
|
|
_ = firstLoop.ShouldNotBeNull();
|
|
firstLoop!.IsCompleted.ShouldBeFalse();
|
|
|
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
|
|
|
firstLoop.IsCompleted.ShouldBeTrue(); // joined
|
|
firstClient.DisposeCount.ShouldBeGreaterThanOrEqualTo(1);
|
|
firstClient.IsConnected.ShouldBeFalse();
|
|
drv.GetDeviceState(host).ShouldNotBeSameAs(first);
|
|
|
|
await drv.ShutdownAsync(CancellationToken.None);
|
|
}
|
|
}
|