feat: integrate config file loading and SIGHUP hot reload
Wire up the config parsing infrastructure into the server: - NatsServer: add ReloadConfig() with digest-based change detection, diff/validate, CLI override preservation, and side-effect triggers - Program.cs: two-pass CLI parsing — load config file first, then apply CLI args on top with InCmdLine tracking for reload precedence - SIGHUP handler upgraded from stub warning to actual reload - Remove config file "not yet supported" warning from StartAsync - Add integration tests for config loading, CLI overrides, and reload validation
This commit is contained in:
76
tests/NATS.Server.Tests/ConfigIntegrationTests.cs
Normal file
76
tests/NATS.Server.Tests/ConfigIntegrationTests.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using NATS.Server.Configuration;
|
||||
|
||||
namespace NATS.Server.Tests;
|
||||
|
||||
public class ConfigIntegrationTests
|
||||
{
|
||||
[Fact]
|
||||
public void Server_WithConfigFile_LoadsOptionsFromFile()
|
||||
{
|
||||
var dir = Path.Combine(Path.GetTempPath(), $"nats_test_{Guid.NewGuid():N}");
|
||||
Directory.CreateDirectory(dir);
|
||||
try
|
||||
{
|
||||
var confPath = Path.Combine(dir, "test.conf");
|
||||
File.WriteAllText(confPath, "port: 14222\nmax_payload: 2mb\ndebug: true");
|
||||
|
||||
var opts = ConfigProcessor.ProcessConfigFile(confPath);
|
||||
opts.Port.ShouldBe(14222);
|
||||
opts.MaxPayload.ShouldBe(2 * 1024 * 1024);
|
||||
opts.Debug.ShouldBeTrue();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(dir, true);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Server_CliOverridesConfig()
|
||||
{
|
||||
var dir = Path.Combine(Path.GetTempPath(), $"nats_test_{Guid.NewGuid():N}");
|
||||
Directory.CreateDirectory(dir);
|
||||
try
|
||||
{
|
||||
var confPath = Path.Combine(dir, "test.conf");
|
||||
File.WriteAllText(confPath, "port: 14222\ndebug: true");
|
||||
|
||||
var opts = ConfigProcessor.ProcessConfigFile(confPath);
|
||||
opts.Port.ShouldBe(14222);
|
||||
|
||||
// Simulate CLI override: user passed -p 5222 on command line
|
||||
var cliSnapshot = new NatsOptions { Port = 5222 };
|
||||
var cliFlags = new HashSet<string> { "Port" };
|
||||
ConfigReloader.MergeCliOverrides(opts, cliSnapshot, cliFlags);
|
||||
|
||||
opts.Port.ShouldBe(5222);
|
||||
opts.Debug.ShouldBeTrue(); // Config file value preserved
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(dir, true);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reload_ChangingPort_ReturnsError()
|
||||
{
|
||||
var oldOpts = new NatsOptions { Port = 4222 };
|
||||
var newOpts = new NatsOptions { Port = 5222 };
|
||||
var changes = ConfigReloader.Diff(oldOpts, newOpts);
|
||||
var errors = ConfigReloader.Validate(changes);
|
||||
errors.Count.ShouldBeGreaterThan(0);
|
||||
errors[0].ShouldContain("Port");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reload_ChangingDebug_IsValid()
|
||||
{
|
||||
var oldOpts = new NatsOptions { Debug = false };
|
||||
var newOpts = new NatsOptions { Debug = true };
|
||||
var changes = ConfigReloader.Diff(oldOpts, newOpts);
|
||||
var errors = ConfigReloader.Validate(changes);
|
||||
errors.ShouldBeEmpty();
|
||||
changes.ShouldContain(c => c.IsLoggingChange);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user