513fa1aa16
Comment/XML-doc changes only: - add summaries to ~10k test methods, fixtures, fakes and helpers (+ <param> on theories, <returns> on async Task methods, <inheritdoc/> on interface implementations the checker can resolve) - strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N, #NN, Component-0NN) from comments, keeping the explanatory text; reword false positives in comments only - real docs on fakes implementing BCL/library interfaces (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime) - reword the last 5 src/ false positives (CWE-1236, failures-1, year-0001, M365) Only non-comment edits: four one-line nested classes split across lines so members can carry docs. Build 0 errors / 0 warnings.
144 lines
6.5 KiB
C#
144 lines
6.5 KiB
C#
using Akka.Actor;
|
|
using Akka.TestKit.Xunit2;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using NSubstitute;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Deployment;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Lifecycle;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
|
|
using ZB.MOM.WW.ScadaBridge.Communication.Actors;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.Communication.Tests;
|
|
|
|
/// <summary>
|
|
/// Seam tests: <see cref="CentralCommunicationActor"/> routing a <see cref="SiteEnvelope"/>
|
|
/// through an injected <see cref="ISiteCommandTransport"/> (transport-agnostic), proving the
|
|
/// envelope reaches the transport, the reply routes back to the waiting Ask, and the DB refresh
|
|
/// tick reconciles per-site transport resources.
|
|
/// </summary>
|
|
public class CentralCommunicationActorTransportTests : TestKit
|
|
{
|
|
/// <summary>Initializes the test kit with a quieter Akka log level.</summary>
|
|
public CentralCommunicationActorTransportTests() : base(@"akka.loglevel = WARNING") { }
|
|
|
|
private static Site GrpcSite(string id, string? grpcA = "http://a:8083", string? grpcB = "http://b:8083") =>
|
|
new("Test " + id, id)
|
|
{
|
|
NodeAAddress = $"akka.tcp://scadabridge@{id}-a:8081",
|
|
GrpcNodeAAddress = grpcA,
|
|
GrpcNodeBAddress = grpcB
|
|
};
|
|
|
|
private (IActorRef actor, ISiteCommandTransport transport, ISiteRepository repo) CreateActor(
|
|
IEnumerable<Site>? sites = null)
|
|
{
|
|
var repo = Substitute.For<ISiteRepository>();
|
|
repo.GetAllSitesAsync(Arg.Any<CancellationToken>()).Returns(sites?.ToList() ?? new List<Site>());
|
|
|
|
var services = new ServiceCollection();
|
|
services.AddScoped(_ => repo);
|
|
var sp = services.BuildServiceProvider();
|
|
|
|
var transport = Substitute.For<ISiteCommandTransport>();
|
|
var actor = Sys.ActorOf(Props.Create(() => new CentralCommunicationActor(sp, transport)));
|
|
return (actor, transport, repo);
|
|
}
|
|
|
|
/// <summary>Verifies that a site envelope is routed to the transport with the sender as the reply-to actor.</summary>
|
|
[Fact]
|
|
public void SiteEnvelope_IsRoutedToTheTransport_WithTheSenderAsReplyTo()
|
|
{
|
|
var (actor, transport, _) = CreateActor();
|
|
var probe = CreateTestProbe();
|
|
|
|
var cmd = new EnableInstanceCommand("cmd-1", "Site1.Pump1", DateTimeOffset.UtcNow);
|
|
actor.Tell(new SiteEnvelope("site-a", cmd), probe.Ref);
|
|
|
|
AwaitAssert(() => transport.Received(1).Send(
|
|
Arg.Is<SiteEnvelope>(e => e.SiteId == "site-a" && ReferenceEquals(e.Message, cmd)),
|
|
probe.Ref));
|
|
}
|
|
|
|
/// <summary>Verifies that a reply from the transport is routed back to the waiting Ask.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task AskReply_FromTransport_RoutesBackToTheWaitingAsk()
|
|
{
|
|
var (actor, transport, _) = CreateActor();
|
|
|
|
// The transport substitute stands in for the site: when handed the envelope it delivers a
|
|
// reply to the captured replyTo, exactly as a real transport pipes the site's reply back.
|
|
var reply = new DeploymentStatusResponse(
|
|
"dep-1", "Site1.Pump1", DeploymentStatus.Success, null, DateTimeOffset.UtcNow);
|
|
transport
|
|
.When(t => t.Send(Arg.Any<SiteEnvelope>(), Arg.Any<IActorRef>()))
|
|
.Do(ci => ci.Arg<IActorRef>().Tell(reply));
|
|
|
|
var cmd = new RefreshDeploymentCommand(
|
|
"dep-1", "Site1.Pump1", "hash", "multi-role", DateTimeOffset.UtcNow, "http://c:5000", "tok");
|
|
var result = await actor.Ask<DeploymentStatusResponse>(
|
|
new SiteEnvelope("site-a", cmd), TimeSpan.FromSeconds(3));
|
|
|
|
Assert.Equal("dep-1", result.DeploymentId);
|
|
Assert.Equal(DeploymentStatus.Success, result.Status);
|
|
}
|
|
|
|
/// <summary>Verifies that a database refresh reconciles the transport with the loaded gRPC endpoints.</summary>
|
|
[Fact]
|
|
public void DbRefresh_ReconcilesTheTransport_WithTheLoadedGrpcEndpoints()
|
|
{
|
|
var (_, transport, _) = CreateActor(new[] { GrpcSite("site-a") });
|
|
|
|
// PreStart fires the refresh at Zero; the loaded cache is handed to the transport.
|
|
AwaitAssert(() => transport.Received().ReconcileSites(
|
|
Arg.Is<SiteAddressCacheLoaded>(c =>
|
|
c.KnownSiteIds.Contains("site-a")
|
|
&& c.GrpcContacts.ContainsKey("site-a")
|
|
&& c.GrpcContacts["site-a"].NodeA == "http://a:8083"
|
|
&& c.GrpcContacts["site-a"].NodeB == "http://b:8083")),
|
|
TimeSpan.FromSeconds(3));
|
|
}
|
|
|
|
/// <summary>Verifies that a site added or removed across refreshes flows through to the transport.</summary>
|
|
[Fact]
|
|
public void SiteAddedAndRemoved_AcrossRefreshes_FlowsThroughToTheTransport()
|
|
{
|
|
var (actor, transport, repo) = CreateActor(new[] { GrpcSite("site-a") });
|
|
|
|
AwaitAssert(() => transport.Received().ReconcileSites(
|
|
Arg.Is<SiteAddressCacheLoaded>(c => c.GrpcContacts.ContainsKey("site-a"))),
|
|
TimeSpan.FromSeconds(3));
|
|
|
|
// site-a removed, site-b added.
|
|
repo.GetAllSitesAsync(Arg.Any<CancellationToken>()).Returns(new List<Site> { GrpcSite("site-b") });
|
|
actor.Tell(new RefreshSiteAddresses());
|
|
|
|
AwaitAssert(() => transport.Received().ReconcileSites(
|
|
Arg.Is<SiteAddressCacheLoaded>(c =>
|
|
c.GrpcContacts.ContainsKey("site-b")
|
|
&& !c.GrpcContacts.ContainsKey("site-a")
|
|
&& c.KnownSiteIds.Contains("site-b")
|
|
&& !c.KnownSiteIds.Contains("site-a"))),
|
|
TimeSpan.FromSeconds(3));
|
|
}
|
|
|
|
/// <summary>Verifies that a site without gRPC addresses is absent from the gRPC contacts but still known.</summary>
|
|
[Fact]
|
|
public void SiteWithoutGrpcAddresses_IsAbsentFromGrpcContacts_ButStillKnown()
|
|
{
|
|
// A site with only Akka addresses (no gRPC columns) is a known site but carries no gRPC
|
|
// endpoint — the gRPC transport must not try to dial it, the Akka one still can.
|
|
var akkaOnly = new Site("Akka only", "site-x")
|
|
{
|
|
NodeAAddress = "akka.tcp://scadabridge@site-x-a:8081"
|
|
};
|
|
var (_, transport, _) = CreateActor(new[] { akkaOnly });
|
|
|
|
AwaitAssert(() => transport.Received().ReconcileSites(
|
|
Arg.Is<SiteAddressCacheLoaded>(c =>
|
|
c.KnownSiteIds.Contains("site-x") && !c.GrpcContacts.ContainsKey("site-x"))),
|
|
TimeSpan.FromSeconds(3));
|
|
}
|
|
}
|