Extract historian into a runtime-loaded plugin so hosts without the Wonderware SDK can run with Historian.Enabled=false
The aahClientManaged SDK is now isolated in ZB.MOM.WW.LmxOpcUa.Historian.Aveva and loaded via HistorianPluginLoader from a Historian/ subfolder only when enabled, removing the SDK from Host's compile-time and deploy-time surface. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using ArchestrA;
|
||||
using ZB.MOM.WW.LmxOpcUa.Historian.Aveva;
|
||||
using ZB.MOM.WW.LmxOpcUa.Host.Configuration;
|
||||
|
||||
namespace ZB.MOM.WW.LmxOpcUa.Historian.Aveva.Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// Fake Historian connection factory for tests. Controls whether connections
|
||||
/// succeed, fail, or timeout without requiring the real Historian SDK runtime.
|
||||
/// </summary>
|
||||
internal sealed class FakeHistorianConnectionFactory : IHistorianConnectionFactory
|
||||
{
|
||||
public Exception? ConnectException { get; set; }
|
||||
|
||||
public int ConnectCallCount { get; private set; }
|
||||
|
||||
public Action<int>? OnConnect { get; set; }
|
||||
|
||||
public HistorianAccess CreateAndConnect(HistorianConfiguration config, HistorianConnectionType type)
|
||||
{
|
||||
ConnectCallCount++;
|
||||
|
||||
if (OnConnect != null)
|
||||
{
|
||||
OnConnect(ConnectCallCount);
|
||||
}
|
||||
else if (ConnectException != null)
|
||||
{
|
||||
throw ConnectException;
|
||||
}
|
||||
|
||||
// Return a HistorianAccess that is not actually connected.
|
||||
// ReadRawAsync etc. will fail when they try to use it, which exercises
|
||||
// the HandleConnectionError → reconnect path.
|
||||
return new HistorianAccess();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
using System;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.LmxOpcUa.Historian.Aveva;
|
||||
using ZB.MOM.WW.LmxOpcUa.Host.Configuration;
|
||||
using ZB.MOM.WW.LmxOpcUa.Host.Historian;
|
||||
|
||||
namespace ZB.MOM.WW.LmxOpcUa.Historian.Aveva.Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// Verifies Historian data source lifecycle behavior: dispose safety,
|
||||
/// post-dispose rejection, connection failure handling, and reconnect-after-error.
|
||||
/// </summary>
|
||||
public class HistorianDataSourceLifecycleTests
|
||||
{
|
||||
private static HistorianConfiguration DefaultConfig => new()
|
||||
{
|
||||
Enabled = true,
|
||||
ServerName = "test-historian",
|
||||
Port = 32568,
|
||||
IntegratedSecurity = true,
|
||||
CommandTimeoutSeconds = 5
|
||||
};
|
||||
|
||||
[Fact]
|
||||
public void ReadRawAsync_AfterDispose_ThrowsObjectDisposedException()
|
||||
{
|
||||
var ds = new HistorianDataSource(DefaultConfig);
|
||||
ds.Dispose();
|
||||
|
||||
Should.Throw<ObjectDisposedException>(() =>
|
||||
ds.ReadRawAsync("Tag1", DateTime.UtcNow.AddHours(-1), DateTime.UtcNow, 100)
|
||||
.GetAwaiter().GetResult());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadAggregateAsync_AfterDispose_ThrowsObjectDisposedException()
|
||||
{
|
||||
var ds = new HistorianDataSource(DefaultConfig);
|
||||
ds.Dispose();
|
||||
|
||||
Should.Throw<ObjectDisposedException>(() =>
|
||||
ds.ReadAggregateAsync("Tag1", DateTime.UtcNow.AddHours(-1), DateTime.UtcNow, 60000, "Average")
|
||||
.GetAwaiter().GetResult());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadAtTimeAsync_AfterDispose_ThrowsObjectDisposedException()
|
||||
{
|
||||
var ds = new HistorianDataSource(DefaultConfig);
|
||||
ds.Dispose();
|
||||
|
||||
Should.Throw<ObjectDisposedException>(() =>
|
||||
ds.ReadAtTimeAsync("Tag1", new[] { DateTime.UtcNow })
|
||||
.GetAwaiter().GetResult());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadEventsAsync_AfterDispose_ThrowsObjectDisposedException()
|
||||
{
|
||||
var ds = new HistorianDataSource(DefaultConfig);
|
||||
ds.Dispose();
|
||||
|
||||
Should.Throw<ObjectDisposedException>(() =>
|
||||
ds.ReadEventsAsync(null, DateTime.UtcNow.AddHours(-1), DateTime.UtcNow, 100)
|
||||
.GetAwaiter().GetResult());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dispose_CalledTwice_DoesNotThrow()
|
||||
{
|
||||
var ds = new HistorianDataSource(DefaultConfig);
|
||||
ds.Dispose();
|
||||
Should.NotThrow(() => ds.Dispose());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HistorianAggregateMap_UnknownColumn_ReturnsNull()
|
||||
{
|
||||
HistorianAggregateMap.MapAggregateToColumn(new Opc.Ua.NodeId(99999)).ShouldBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadRawAsync_WhenConnectionFails_ReturnsEmptyResults()
|
||||
{
|
||||
var factory = new FakeHistorianConnectionFactory
|
||||
{
|
||||
ConnectException = new InvalidOperationException("Connection refused")
|
||||
};
|
||||
var ds = new HistorianDataSource(DefaultConfig, factory);
|
||||
|
||||
var results = ds.ReadRawAsync("Tag1", DateTime.UtcNow.AddHours(-1), DateTime.UtcNow, 100)
|
||||
.GetAwaiter().GetResult();
|
||||
|
||||
results.Count.ShouldBe(0);
|
||||
factory.ConnectCallCount.ShouldBe(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadRawAsync_WhenConnectionTimesOut_ReturnsEmptyResults()
|
||||
{
|
||||
var factory = new FakeHistorianConnectionFactory
|
||||
{
|
||||
ConnectException = new TimeoutException("Connection timed out")
|
||||
};
|
||||
var ds = new HistorianDataSource(DefaultConfig, factory);
|
||||
|
||||
var results = ds.ReadRawAsync("Tag1", DateTime.UtcNow.AddHours(-1), DateTime.UtcNow, 100)
|
||||
.GetAwaiter().GetResult();
|
||||
|
||||
results.Count.ShouldBe(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadRawAsync_AfterConnectionError_AttemptsReconnect()
|
||||
{
|
||||
var factory = new FakeHistorianConnectionFactory();
|
||||
var ds = new HistorianDataSource(DefaultConfig, factory);
|
||||
|
||||
// First call: factory returns a HistorianAccess that isn't actually connected,
|
||||
// so the query will fail and HandleConnectionError will reset the connection.
|
||||
ds.ReadRawAsync("Tag1", DateTime.UtcNow.AddHours(-1), DateTime.UtcNow, 100)
|
||||
.GetAwaiter().GetResult();
|
||||
|
||||
// Second call: should attempt reconnection via the factory
|
||||
ds.ReadRawAsync("Tag1", DateTime.UtcNow.AddHours(-1), DateTime.UtcNow, 100)
|
||||
.GetAwaiter().GetResult();
|
||||
|
||||
// Factory should have been called twice — once for initial connect, once for reconnect
|
||||
factory.ConnectCallCount.ShouldBe(2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadRawAsync_ConnectionFailure_DoesNotCorruptState()
|
||||
{
|
||||
var callCount = 0;
|
||||
var factory = new FakeHistorianConnectionFactory
|
||||
{
|
||||
OnConnect = count =>
|
||||
{
|
||||
callCount = count;
|
||||
if (count == 1)
|
||||
throw new InvalidOperationException("First connection fails");
|
||||
// Second call succeeds (returns unconnected HistorianAccess, but that's OK for lifecycle testing)
|
||||
}
|
||||
};
|
||||
var ds = new HistorianDataSource(DefaultConfig, factory);
|
||||
|
||||
// First read: connection fails
|
||||
var r1 = ds.ReadRawAsync("Tag1", DateTime.UtcNow.AddHours(-1), DateTime.UtcNow, 100)
|
||||
.GetAwaiter().GetResult();
|
||||
r1.Count.ShouldBe(0);
|
||||
|
||||
// Second read: should attempt new connection without throwing from internal state corruption
|
||||
var r2 = ds.ReadRawAsync("Tag1", DateTime.UtcNow.AddHours(-1), DateTime.UtcNow, 100)
|
||||
.GetAwaiter().GetResult();
|
||||
|
||||
callCount.ShouldBe(2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dispose_DuringConnectionFailure_DoesNotThrow()
|
||||
{
|
||||
var factory = new FakeHistorianConnectionFactory
|
||||
{
|
||||
ConnectException = new InvalidOperationException("Connection refused")
|
||||
};
|
||||
var ds = new HistorianDataSource(DefaultConfig, factory);
|
||||
|
||||
// Trigger a failed connection attempt
|
||||
ds.ReadRawAsync("Tag1", DateTime.UtcNow.AddHours(-1), DateTime.UtcNow, 100)
|
||||
.GetAwaiter().GetResult();
|
||||
|
||||
// Dispose should handle the null connection gracefully
|
||||
Should.NotThrow(() => ds.Dispose());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net48</TargetFramework>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<LangVersion>9.0</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
<RootNamespace>ZB.MOM.WW.LmxOpcUa.Historian.Aveva.Tests</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0"/>
|
||||
<PackageReference Include="xunit" Version="2.9.3"/>
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Shouldly" Version="4.2.1"/>
|
||||
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Server" Version="1.5.374.126"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\ZB.MOM.WW.LmxOpcUa.Historian.Aveva\ZB.MOM.WW.LmxOpcUa.Historian.Aveva.csproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<!-- Tests reference aahClientManaged so the FakeHistorianConnectionFactory can
|
||||
implement the SDK-typed IHistorianConnectionFactory interface. -->
|
||||
<Reference Include="aahClientManaged">
|
||||
<HintPath>..\..\lib\aahClientManaged.dll</HintPath>
|
||||
<EmbedInteropTypes>false</EmbedInteropTypes>
|
||||
</Reference>
|
||||
<Reference Include="aahClientCommon">
|
||||
<HintPath>..\..\lib\aahClientCommon.dll</HintPath>
|
||||
<EmbedInteropTypes>false</EmbedInteropTypes>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user