Files
lmxopcua/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Historian.Gateway.Tests/GatewayHealthSnapshotTests.cs
T

52 lines
2.5 KiB
C#

using Microsoft.Extensions.Logging.Abstractions;
using Xunit;
using ZB.MOM.WW.HistorianGateway.Contracts.Grpc;
namespace ZB.MOM.WW.OtOpcUa.Driver.Historian.Gateway.Tests;
public sealed class GatewayHealthSnapshotTests
{
[Fact]
public async Task Counters_track_success_and_failure()
{
var fake = new FakeHistorianGatewayClient { RawSamples = Array.Empty<HistorianSample>() };
var ds = new GatewayHistorianDataSource(fake, NullLogger<GatewayHistorianDataSource>.Instance);
await ds.ReadRawAsync("T", default, default, 1, TestContext.Current.CancellationToken);
fake.ThrowOnRead = new InvalidOperationException("boom");
await Assert.ThrowsAnyAsync<Exception>(() => ds.ReadRawAsync("T", default, default, 1, TestContext.Current.CancellationToken));
var h = ds.GetHealthSnapshot();
Assert.Equal(2, h.TotalQueries);
Assert.Equal(1, h.TotalSuccesses);
Assert.Equal(1, h.TotalFailures);
Assert.Equal(1, h.ConsecutiveFailures);
Assert.Equal(h.TotalQueries, h.TotalSuccesses + h.TotalFailures); // invariant
}
[Fact]
public async Task GetHealthSnapshot_does_no_io_and_starts_with_connections_closed()
{
var fake = new FakeHistorianGatewayClient();
var ds = new GatewayHistorianDataSource(fake, NullLogger<GatewayHistorianDataSource>.Instance);
var h = ds.GetHealthSnapshot();
Assert.Equal(0, fake.GetConnectionStatusCallCount); // pure observation — never queries the gateway
Assert.False(h.ProcessConnectionOpen);
Assert.False(h.EventConnectionOpen);
await ValueTask.CompletedTask;
}
[Fact]
public async Task Connection_flags_are_derived_from_observed_reads()
{
var fake = new FakeHistorianGatewayClient { RawSamples = Array.Empty<HistorianSample>() };
var ds = new GatewayHistorianDataSource(fake, NullLogger<GatewayHistorianDataSource>.Instance);
await ds.ReadRawAsync("T", default, default, 1, TestContext.Current.CancellationToken);
Assert.True(ds.GetHealthSnapshot().ProcessConnectionOpen);
fake.ThrowOnRead = new InvalidOperationException("boom");
await Assert.ThrowsAnyAsync<Exception>(() => ds.ReadRawAsync("T", default, default, 1, TestContext.Current.CancellationToken));
var h = ds.GetHealthSnapshot();
Assert.False(h.ProcessConnectionOpen);
Assert.False(h.EventConnectionOpen);
Assert.Equal(0, fake.GetConnectionStatusCallCount); // never probes the gateway
}
}