feat: add SiteStreamGrpcClient and SiteStreamGrpcClientFactory

Per-site gRPC client for central-side streaming subscriptions to site
servers. SiteStreamGrpcClient manages server-streaming calls with
keepalive, converts proto events to domain types, and supports
cancellation via Unsubscribe. SiteStreamGrpcClientFactory caches one
client per site identifier.

Includes InternalsVisibleTo for test access to conversion helpers and
comprehensive unit tests for event mapping, quality/alarm-state
conversion, unsubscribe behavior, and factory caching.
This commit is contained in:
Joseph Doherty
2026-03-21 12:06:38 -04:00
parent 55a05914d0
commit 25a6022f7b
5 changed files with 446 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using ScadaLink.Communication.Grpc;
namespace ScadaLink.Communication.Tests.Grpc;
public class SiteStreamGrpcClientFactoryTests
{
private readonly ILoggerFactory _loggerFactory = NullLoggerFactory.Instance;
[Fact]
public void GetOrCreate_ReturnsSameClientForSameSite()
{
using var factory = new SiteStreamGrpcClientFactory(_loggerFactory);
var client1 = factory.GetOrCreate("site-a", "http://localhost:5100");
var client2 = factory.GetOrCreate("site-a", "http://localhost:5100");
Assert.Same(client1, client2);
}
[Fact]
public void GetOrCreate_ReturnsDifferentClientsForDifferentSites()
{
using var factory = new SiteStreamGrpcClientFactory(_loggerFactory);
var client1 = factory.GetOrCreate("site-a", "http://localhost:5100");
var client2 = factory.GetOrCreate("site-b", "http://localhost:5200");
Assert.NotSame(client1, client2);
}
[Fact]
public async Task RemoveSite_DisposesClient()
{
var factory = new SiteStreamGrpcClientFactory(_loggerFactory);
var client1 = factory.GetOrCreate("site-a", "http://localhost:5100");
await factory.RemoveSiteAsync("site-a");
// After removal, GetOrCreate should return a new instance
var client2 = factory.GetOrCreate("site-a", "http://localhost:5100");
Assert.NotSame(client1, client2);
}
[Fact]
public async Task RemoveSite_NonExistent_DoesNotThrow()
{
var factory = new SiteStreamGrpcClientFactory(_loggerFactory);
await factory.RemoveSiteAsync("does-not-exist"); // Should not throw
}
[Fact]
public async Task DisposeAsync_DisposesAllClients()
{
var factory = new SiteStreamGrpcClientFactory(_loggerFactory);
factory.GetOrCreate("site-a", "http://localhost:5100");
factory.GetOrCreate("site-b", "http://localhost:5200");
await factory.DisposeAsync();
// After dispose, creating new clients should work (new instances)
// This tests that Dispose doesn't throw
}
}

View File

@@ -0,0 +1,141 @@
using Google.Protobuf.WellKnownTypes;
using ScadaLink.Communication.Grpc;
using ScadaLink.Commons.Messages.Streaming;
using ScadaLink.Commons.Types.Enums;
namespace ScadaLink.Communication.Tests.Grpc;
public class SiteStreamGrpcClientTests
{
[Fact]
public void ConvertToDomainEvent_AttributeChanged_MapsCorrectly()
{
var ts = DateTimeOffset.UtcNow;
var evt = new SiteStreamEvent
{
CorrelationId = "corr-1",
AttributeChanged = new AttributeValueUpdate
{
InstanceUniqueName = "Site1.Pump01",
AttributePath = "Modules.IO",
AttributeName = "Temperature",
Value = "42.5",
Quality = Quality.Good,
Timestamp = Timestamp.FromDateTimeOffset(ts)
}
};
var result = SiteStreamGrpcClient.ConvertToDomainEvent(evt);
var attr = Assert.IsType<AttributeValueChanged>(result);
Assert.Equal("Site1.Pump01", attr.InstanceUniqueName);
Assert.Equal("Modules.IO", attr.AttributePath);
Assert.Equal("Temperature", attr.AttributeName);
Assert.Equal("42.5", attr.Value);
Assert.Equal("Good", attr.Quality);
Assert.Equal(ts, attr.Timestamp);
}
[Fact]
public void ConvertToDomainEvent_AlarmChanged_MapsCorrectly()
{
var ts = DateTimeOffset.UtcNow;
var evt = new SiteStreamEvent
{
CorrelationId = "corr-2",
AlarmChanged = new AlarmStateUpdate
{
InstanceUniqueName = "Site1.Motor01",
AlarmName = "OverTemp",
State = AlarmStateEnum.AlarmStateActive,
Priority = 3,
Timestamp = Timestamp.FromDateTimeOffset(ts)
}
};
var result = SiteStreamGrpcClient.ConvertToDomainEvent(evt);
var alarm = Assert.IsType<AlarmStateChanged>(result);
Assert.Equal("Site1.Motor01", alarm.InstanceUniqueName);
Assert.Equal("OverTemp", alarm.AlarmName);
Assert.Equal(AlarmState.Active, alarm.State);
Assert.Equal(3, alarm.Priority);
Assert.Equal(ts, alarm.Timestamp);
}
[Fact]
public void ConvertToDomainEvent_UnknownEvent_ReturnsNull()
{
var evt = new SiteStreamEvent
{
CorrelationId = "corr-3"
// No oneof case set
};
var result = SiteStreamGrpcClient.ConvertToDomainEvent(evt);
Assert.Null(result);
}
[Theory]
[InlineData(Quality.Good, "Good")]
[InlineData(Quality.Uncertain, "Uncertain")]
[InlineData(Quality.Bad, "Bad")]
[InlineData(Quality.Unspecified, "Unknown")]
public void MapQuality_AllValues(Quality input, string expected)
{
var result = SiteStreamGrpcClient.MapQuality(input);
Assert.Equal(expected, result);
}
[Theory]
[InlineData(AlarmStateEnum.AlarmStateNormal, AlarmState.Normal)]
[InlineData(AlarmStateEnum.AlarmStateActive, AlarmState.Active)]
[InlineData(AlarmStateEnum.AlarmStateUnspecified, AlarmState.Normal)]
public void MapAlarmState_AllValues(AlarmStateEnum input, AlarmState expected)
{
var result = SiteStreamGrpcClient.MapAlarmState(input);
Assert.Equal(expected, result);
}
[Fact]
public void Unsubscribe_CancelsSubscription()
{
// We can't easily test the full Subscribe flow without a real gRPC server,
// but we can test the Unsubscribe path by registering a CTS directly.
// Use the internal AddSubscription helper for testability.
var client = SiteStreamGrpcClient.CreateForTesting();
var cts = new CancellationTokenSource();
client.AddSubscriptionForTesting("corr-test", cts);
Assert.False(cts.IsCancellationRequested);
client.Unsubscribe("corr-test");
Assert.True(cts.IsCancellationRequested);
}
[Fact]
public void Unsubscribe_NonExistent_DoesNotThrow()
{
var client = SiteStreamGrpcClient.CreateForTesting();
client.Unsubscribe("does-not-exist"); // Should not throw
}
[Fact]
public async Task DisposeAsync_CancelsAllSubscriptions()
{
var client = SiteStreamGrpcClient.CreateForTesting();
var cts1 = new CancellationTokenSource();
var cts2 = new CancellationTokenSource();
client.AddSubscriptionForTesting("corr-1", cts1);
client.AddSubscriptionForTesting("corr-2", cts2);
await client.DisposeAsync();
Assert.True(cts1.IsCancellationRequested);
Assert.True(cts2.IsCancellationRequested);
}
}