ccba570840
H-1: ActorSystemTerminationWatchdog flushes logs and exits the process with code 1 instead of StopApplication (clean SCM stop never triggers sc failure recovery). H-2: AuditWriterActor re-buffers a batch when the DB did not answer; bounded 10k drop-oldest with counter; threshold flush suppressed while failing. H-5: DedicatedListenerGuard throws instead of log-and-zero when an HTTPS binding blocks a requested dedicated h2c listener.
149 lines
6.5 KiB
C#
149 lines
6.5 KiB
C#
using Akka.Actor;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Host;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Host.IntegrationTests;
|
|
|
|
/// <summary>
|
|
/// Unit coverage for the #459 down-if-alone recovery watchdog: an unexpected
|
|
/// <see cref="ActorSystem"/> termination (SBR self-down) must stop the host so the supervisor
|
|
/// restarts it, while a graceful host shutdown must stay silent and not re-trigger stop.
|
|
/// </summary>
|
|
[Trait("Category", "Unit")]
|
|
public sealed class ActorSystemTerminationWatchdogTests
|
|
{
|
|
/// <summary>
|
|
/// An unexpected ActorSystem termination ends the process with a NON-ZERO exit code (#980 H-1):
|
|
/// a graceful <c>StopApplication</c> reports a clean service stop and SCM never restarts it.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task Unexpected_termination_exits_the_process_non_zero_after_flushing_logs()
|
|
{
|
|
var system = ActorSystem.Create("watchdog-unexpected");
|
|
var lifetime = new FakeHostApplicationLifetime();
|
|
var exits = new ExitRecorder();
|
|
var watchdog = Create(system, lifetime, exits);
|
|
|
|
await watchdog.StartAsync(CancellationToken.None);
|
|
|
|
// Simulate the SBR self-down: the ActorSystem terminates while the host is still running.
|
|
await system.Terminate();
|
|
|
|
await WaitForAsync(() => exits.ExitCodes.Count > 0, TimeSpan.FromSeconds(5));
|
|
exits.ExitCodes.ShouldBe([ActorSystemTerminationWatchdog.FailureExitCode],
|
|
"an out-of-band ActorSystem termination must end the process once, non-zero");
|
|
ActorSystemTerminationWatchdog.FailureExitCode.ShouldNotBe(0);
|
|
exits.FlushCalls.ShouldBe(1, "logs are flushed before the process exits");
|
|
exits.FlushedBeforeExit.ShouldBeTrue();
|
|
lifetime.StopApplicationCalls.ShouldBe(0,
|
|
"StopApplication is a CLEAN stop (SERVICE_STOPPED, exit 0) and would defeat SCM recovery");
|
|
}
|
|
|
|
/// <summary>A throwing log flush must never prevent the exit.</summary>
|
|
[Fact]
|
|
public async Task A_failing_log_flush_still_exits_the_process()
|
|
{
|
|
var system = ActorSystem.Create("watchdog-flush-throws");
|
|
var lifetime = new FakeHostApplicationLifetime();
|
|
var exits = new ExitRecorder { FlushThrows = true };
|
|
var watchdog = Create(system, lifetime, exits);
|
|
|
|
await watchdog.StartAsync(CancellationToken.None);
|
|
await system.Terminate();
|
|
|
|
await WaitForAsync(() => exits.ExitCodes.Count > 0, TimeSpan.FromSeconds(5));
|
|
exits.ExitCodes.ShouldBe([ActorSystemTerminationWatchdog.FailureExitCode]);
|
|
}
|
|
|
|
/// <summary>A graceful host stop (StopAsync ran) leaves the termination continuation silent.</summary>
|
|
[Fact]
|
|
public async Task Graceful_stop_does_not_stop_the_application()
|
|
{
|
|
var system = ActorSystem.Create("watchdog-graceful");
|
|
var lifetime = new FakeHostApplicationLifetime();
|
|
var exits = new ExitRecorder();
|
|
var watchdog = Create(system, lifetime, exits);
|
|
|
|
await watchdog.StartAsync(CancellationToken.None);
|
|
|
|
// Host is shutting down normally: StopAsync runs, THEN the system terminates.
|
|
await watchdog.StopAsync(CancellationToken.None);
|
|
await system.Terminate();
|
|
|
|
// Give any continuation a chance to run, then assert it stayed silent.
|
|
await Task.Delay(500, TestContext.Current.CancellationToken);
|
|
lifetime.StopApplicationCalls.ShouldBe(0, "a graceful shutdown must not re-trigger StopApplication");
|
|
exits.ExitCodes.ShouldBeEmpty("a graceful shutdown must not exit the process as a failure");
|
|
}
|
|
|
|
/// <summary>Termination while the host is already stopping (ApplicationStopping fired) stays silent.</summary>
|
|
[Fact]
|
|
public async Task Termination_while_already_stopping_stays_silent()
|
|
{
|
|
var system = ActorSystem.Create("watchdog-already-stopping");
|
|
var lifetime = new FakeHostApplicationLifetime();
|
|
var exits = new ExitRecorder();
|
|
var watchdog = Create(system, lifetime, exits);
|
|
|
|
await watchdog.StartAsync(CancellationToken.None);
|
|
|
|
lifetime.TriggerStopping(); // the host is already tearing down (ApplicationStopping cancelled)
|
|
await system.Terminate();
|
|
|
|
await Task.Delay(500, TestContext.Current.CancellationToken);
|
|
lifetime.StopApplicationCalls.ShouldBe(0, "termination during an in-progress host shutdown must stay silent");
|
|
exits.ExitCodes.ShouldBeEmpty("termination during an in-progress host shutdown must not exit as a failure");
|
|
}
|
|
|
|
private static ActorSystemTerminationWatchdog Create(
|
|
ActorSystem system, IHostApplicationLifetime lifetime, ExitRecorder exits) =>
|
|
new(() => system, lifetime, NullLogger<ActorSystemTerminationWatchdog>.Instance, exits.Exit, exits.Flush);
|
|
|
|
/// <summary>Records the exit + flush the watchdog performs instead of killing the test process.</summary>
|
|
private sealed class ExitRecorder
|
|
{
|
|
private readonly object _gate = new();
|
|
public List<int> ExitCodes { get; } = [];
|
|
public int FlushCalls { get; private set; }
|
|
public bool FlushedBeforeExit { get; private set; }
|
|
public bool FlushThrows { get; init; }
|
|
|
|
public void Flush()
|
|
{
|
|
lock (_gate) FlushCalls++;
|
|
if (FlushThrows) throw new InvalidOperationException("sink down");
|
|
}
|
|
|
|
public void Exit(int code)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
FlushedBeforeExit = FlushCalls > 0;
|
|
ExitCodes.Add(code);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static async Task WaitForAsync(Func<bool> condition, TimeSpan timeout)
|
|
{
|
|
var deadline = DateTime.UtcNow + timeout;
|
|
while (!condition() && DateTime.UtcNow < deadline)
|
|
await Task.Delay(20);
|
|
}
|
|
|
|
/// <summary>Minimal <see cref="IHostApplicationLifetime"/> that records <see cref="StopApplication"/> calls.</summary>
|
|
private sealed class FakeHostApplicationLifetime : IHostApplicationLifetime
|
|
{
|
|
private readonly CancellationTokenSource _stopping = new();
|
|
public int StopApplicationCalls { get; private set; }
|
|
public CancellationToken ApplicationStarted => CancellationToken.None;
|
|
public CancellationToken ApplicationStopping => _stopping.Token;
|
|
public CancellationToken ApplicationStopped => CancellationToken.None;
|
|
public void StopApplication() => StopApplicationCalls++;
|
|
public void TriggerStopping() => _stopping.Cancel();
|
|
}
|
|
}
|