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.
89 lines
3.6 KiB
C#
89 lines
3.6 KiB
C#
using Akka.Actor;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Microsoft.Extensions.Options;
|
|
using NSubstitute;
|
|
using ZB.MOM.WW.ScadaBridge.ClusterInfrastructure;
|
|
using ZB.MOM.WW.ScadaBridge.Communication;
|
|
using ZB.MOM.WW.ScadaBridge.Host.Actors;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.Host.Tests;
|
|
|
|
/// <summary>
|
|
/// With SBR down-if-alone +
|
|
/// run-coordinated-shutdown-when-down live, a self-downed node terminates its
|
|
/// ActorSystem out from under the still-running Host process. Unless the process
|
|
/// then exits, the node idles forever — serving nothing, restarted by nobody.
|
|
/// The recovery contract: unexpected ActorSystem termination ⇒ StopApplication()
|
|
/// ⇒ the service supervisor restarts the process ⇒ it rejoins as a fresh
|
|
/// incarnation. A termination that IS the host's own StopAsync must NOT trip it.
|
|
/// </summary>
|
|
public sealed class UnexpectedTerminationTests
|
|
{
|
|
private static int FreePort()
|
|
{
|
|
var l = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Loopback, 0);
|
|
l.Start();
|
|
var p = ((System.Net.IPEndPoint)l.LocalEndpoint).Port;
|
|
l.Stop();
|
|
return p;
|
|
}
|
|
|
|
private static AkkaHostedService CreateAkkaHostedService(IHostApplicationLifetime lifetime)
|
|
{
|
|
var port = FreePort();
|
|
var self = $"akka.tcp://scadabridge@127.0.0.1:{port}";
|
|
return new AkkaHostedService(
|
|
new ServiceCollection().BuildServiceProvider(),
|
|
Options.Create(new NodeOptions { Role = "Site", NodeHostname = "127.0.0.1", RemotingPort = port, SiteId = "site-a" }),
|
|
Options.Create(new ClusterOptions
|
|
{
|
|
SeedNodes = new List<string> { self },
|
|
AllowSingleNodeCluster = true,
|
|
SplitBrainResolverStrategy = "keep-oldest",
|
|
StableAfter = TimeSpan.FromSeconds(3),
|
|
HeartbeatInterval = TimeSpan.FromMilliseconds(500),
|
|
FailureDetectionThreshold = TimeSpan.FromSeconds(2),
|
|
MinNrOfMembers = 1,
|
|
DownIfAlone = true,
|
|
}),
|
|
Options.Create(new CommunicationOptions()),
|
|
NullLogger<AkkaHostedService>.Instance,
|
|
lifetime);
|
|
}
|
|
|
|
/// <summary>Verifies that the host application is stopped when the actor system terminates outside of a stop request.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task ActorSystemTerminatedOutsideStopAsync_StopsHostApplication()
|
|
{
|
|
var lifetime = Substitute.For<IHostApplicationLifetime>();
|
|
var service = CreateAkkaHostedService(lifetime);
|
|
// Create the system (StartAsync registers actors; the system is created
|
|
// by GetOrCreateActorSystem, which wires the WhenTerminated watchdog).
|
|
service.GetOrCreateActorSystem();
|
|
var system = service.ActorSystem!;
|
|
|
|
await system.Terminate(); // simulated self-down, NOT StopAsync
|
|
await Task.Delay(500);
|
|
|
|
lifetime.Received(1).StopApplication();
|
|
}
|
|
|
|
/// <summary>Verifies that a normal stop does not trigger an application stop.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task StopAsync_DoesNotTriggerStopApplication()
|
|
{
|
|
var lifetime = Substitute.For<IHostApplicationLifetime>();
|
|
var service = CreateAkkaHostedService(lifetime);
|
|
service.GetOrCreateActorSystem();
|
|
|
|
await service.StopAsync(CancellationToken.None);
|
|
await Task.Delay(500);
|
|
|
|
lifetime.DidNotReceive().StopApplication();
|
|
}
|
|
}
|