Skip to content

Commit

Permalink
Add test for ldm
Browse files Browse the repository at this point in the history
  • Loading branch information
pzajaczkowski committed Jan 15, 2025
1 parent ee4710c commit edda667
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/NATS.Client.Core.Tests/NatsConnectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down
28 changes: 28 additions & 0 deletions tests/NATS.Client.TestUtilities/NatsServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,34 @@ public void LogMessage<TState>(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

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / Windows (v2.9)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / Windows (v2.9)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / Windows (v2.9)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / Windows (v2.9)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / test (v2.9)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / test (v2.9)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / check

Use trailing comma in multi-line initializers

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / check

Use trailing comma in multi-line initializers

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / Linux (v2.9)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / Linux (v2.9)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / Linux (v2.9)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / Linux (v2.9)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / test (latest)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / test (latest)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / Windows (latest)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / Windows (latest)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / Windows (latest)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / Windows (latest)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / test (main)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / test (main)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / Linux (latest)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / Linux (latest)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / Linux (latest)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / Linux (latest)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / Windows (main)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / Windows (main)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / Windows (main)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / Windows (main)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / Linux (main)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / Linux (main)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / Linux (main)

Check warning on line 450 in tests/NATS.Client.TestUtilities/NatsServer.cs

View workflow job for this annotation

GitHub Actions / Linux (main)

{
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();
Expand Down

0 comments on commit edda667

Please sign in to comment.