From edda667da65e8f0dc0be9333f6d442cef2426ddf Mon Sep 17 00:00:00 2001 From: pzajaczkowski Date: Wed, 15 Jan 2025 19:03:51 +0100 Subject: [PATCH] Add test for ldm --- .../NatsConnectionTest.cs | 26 +++++++++++++++++ tests/NATS.Client.TestUtilities/NatsServer.cs | 28 +++++++++++++++++++ 2 files changed, 54 insertions(+) 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..f2ca1e514 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();