Files
natsnet/dotnet/tests/ZB.MOM.NatsNet.Server.Tests/ImplBacklog/ConfigReloaderTests.cs
2026-02-28 10:04:45 -05:00

152 lines
4.4 KiB
C#

using Shouldly;
using ZB.MOM.NatsNet.Server;
namespace ZB.MOM.NatsNet.Server.Tests.ImplBacklog;
public sealed class ConfigReloaderTests
{
[Fact] // T:2766
public void ConfigReloadBoolFlags_ShouldSucceed()
{
static string WriteConfig(string body)
{
var tempFile = Path.GetTempFileName();
File.WriteAllText(tempFile, body);
return tempFile;
}
try
{
var cases = new[]
{
new
{
Config = """
{
"host": "127.0.0.1",
"port": 4222,
"logtime": false
}
""",
Args = new[] { "-T" },
Validate = (Action<ServerOptions>)(opts => opts.Logtime.ShouldBeTrue()),
},
new
{
Config = """
{
"host": "127.0.0.1",
"port": 4222,
"debug": true
}
""",
Args = new[] { "-D=false" },
Validate = (Action<ServerOptions>)(opts => opts.Debug.ShouldBeFalse()),
},
new
{
Config = """
{
"host": "127.0.0.1",
"port": 4222,
"trace": true
}
""",
Args = new[] { "-V=false" },
Validate = (Action<ServerOptions>)(opts => opts.Trace.ShouldBeFalse()),
},
new
{
Config = """
{
"host": "127.0.0.1",
"port": 4222,
"cluster": { "port": 6222, "no_advertise": true }
}
""",
Args = new[] { "--no_advertise=false" },
Validate = (Action<ServerOptions>)(opts => opts.Cluster.NoAdvertise.ShouldBeFalse()),
},
new
{
Config = """
{
"host": "127.0.0.1",
"port": 4222,
"jetstream": true
}
""",
Args = new[] { "--js=false" },
Validate = (Action<ServerOptions>)(opts => opts.JetStream.ShouldBeFalse()),
},
};
foreach (var testCase in cases)
{
var configPath = WriteConfig(testCase.Config);
var args = new List<string> { "-c", configPath };
args.AddRange(testCase.Args);
var (options, error) = ServerOptions.ConfigureOptions(args, null, null, null);
error.ShouldBeNull();
options.ShouldNotBeNull();
testCase.Validate(options!);
File.Delete(configPath);
}
}
finally
{
ServerOptions.FlagSnapshot = null;
}
}
[Fact]
public void ParseCluster_WithUnknownFieldAndStrictMode_ReturnsError()
{
ServerOptions.NoErrOnUnknownFields(false);
var options = new ServerOptions();
var errors = new List<Exception>();
ServerOptions.ParseCluster(
new Dictionary<string, object?>
{
["unknown_cluster_field"] = true,
},
options,
errors,
warnings: null);
errors.Count.ShouldBe(1);
errors[0].Message.ShouldContain("unknown field");
}
[Fact]
public void ParseCluster_WithUnknownFieldAndRelaxedMode_IgnoresUnknownField()
{
ServerOptions.NoErrOnUnknownFields(true);
try
{
var options = new ServerOptions();
var errors = new List<Exception>();
ServerOptions.ParseCluster(
new Dictionary<string, object?>
{
["unknown_cluster_field"] = true,
},
options,
errors,
warnings: null);
errors.ShouldBeEmpty();
}
finally
{
ServerOptions.NoErrOnUnknownFields(false);
}
}
}