Files
scadalink-design/tests/ScadaLink.Communication.Tests/Grpc/SiteStreamGrpcClientFactoryTests.cs
Joseph Doherty 25a6022f7b 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.
2026-03-21 12:06:38 -04:00

67 lines
2.1 KiB
C#

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
}
}