diff --git a/tests/NATS.Client.Core.Tests/NatsConnectionTest.cs b/tests/NATS.Client.Core.Tests/NatsConnectionTest.cs index da4a5c405..6cdb585f9 100644 --- a/tests/NATS.Client.Core.Tests/NatsConnectionTest.cs +++ b/tests/NATS.Client.Core.Tests/NatsConnectionTest.cs @@ -505,6 +505,32 @@ public async Task ReconnectOnOpenConnection_ShouldDisconnectAndOpenNewConnection openedCount.ShouldBe(1); disconnectedCount.ShouldBe(1); } + + [Fact] + public async Task LameDuckModeActivated_EventHandlerShouldBeInvokedWhenInfoWithLDMRecievied() + { + // Arrange + await using var server = NatsServer.Start(_output, _transportType); + await using var connection = server.CreateClientConnection(); + await connection.ConnectAsync(); + + var invocationCount = 0; + var ldmSignal = new WaitSignal(); + + connection.LameDuckModeActivated += (_, _) => + { + Interlocked.Increment(ref invocationCount); + ldmSignal.Pulse(); + return default; + }; + + // Act + server.SignalLameDuckMode(); + await ldmSignal; + + // Assert + invocationCount.ShouldBe(1); + } } [JsonSerializable(typeof(SampleClass))] diff --git a/tests/NATS.Client.TestUtilities/NatsServer.cs b/tests/NATS.Client.TestUtilities/NatsServer.cs index 43bd0b2f5..8cabe8f99 100644 --- a/tests/NATS.Client.TestUtilities/NatsServer.cs +++ b/tests/NATS.Client.TestUtilities/NatsServer.cs @@ -438,6 +438,34 @@ public void LogMessage(string categoryName, LogLevel logLevel, EventId e } } + public void SignalLameDuckMode() + { + if (ServerProcess == null || ServerProcess.HasExited) + { + throw new Exception("Cannot signal LDM, server process is not running."); + } + + var signalProcess = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = NatsServerPath, + Arguments = $"--signal ldm={ServerProcess.Id}", + RedirectStandardOutput = true, + UseShellExecute = false, + }, + }; + + signalProcess.Start(); + signalProcess.WaitForExit(); + + if (signalProcess.ExitCode != 0) + { + var error = signalProcess.StandardError.ReadToEnd(); + throw new Exception($"Failed to signal lame duck mode: {error}"); + } + } + private static (string configFileName, string config, string cmd) GetCmd(NatsServerOpts opts) { var configFileName = Path.GetTempFileName();