feat: add PID file and ports file support
This commit is contained in:
@@ -842,3 +842,62 @@ public class LameDuckTests
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class PidFileTests : IDisposable
|
||||
{
|
||||
private readonly string _tempDir = Path.Combine(Path.GetTempPath(), $"nats-test-{Guid.NewGuid():N}");
|
||||
|
||||
public PidFileTests() => Directory.CreateDirectory(_tempDir);
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (Directory.Exists(_tempDir))
|
||||
Directory.Delete(_tempDir, recursive: true);
|
||||
}
|
||||
|
||||
private static int GetFreePort()
|
||||
{
|
||||
using var sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
sock.Bind(new IPEndPoint(IPAddress.Loopback, 0));
|
||||
return ((IPEndPoint)sock.LocalEndPoint!).Port;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Server_writes_pid_file_on_startup()
|
||||
{
|
||||
var pidFile = Path.Combine(_tempDir, "nats.pid");
|
||||
var port = GetFreePort();
|
||||
var server = new NatsServer(new NatsOptions { Port = port, PidFile = pidFile }, NullLoggerFactory.Instance);
|
||||
_ = server.StartAsync(CancellationToken.None);
|
||||
await server.WaitForReadyAsync();
|
||||
|
||||
File.Exists(pidFile).ShouldBeTrue();
|
||||
var content = await File.ReadAllTextAsync(pidFile);
|
||||
int.Parse(content).ShouldBe(Environment.ProcessId);
|
||||
|
||||
await server.ShutdownAsync();
|
||||
File.Exists(pidFile).ShouldBeFalse();
|
||||
|
||||
server.Dispose();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Server_writes_ports_file_on_startup()
|
||||
{
|
||||
var port = GetFreePort();
|
||||
var server = new NatsServer(new NatsOptions { Port = port, PortsFileDir = _tempDir }, NullLoggerFactory.Instance);
|
||||
_ = server.StartAsync(CancellationToken.None);
|
||||
await server.WaitForReadyAsync();
|
||||
|
||||
var portsFiles = Directory.GetFiles(_tempDir, "*.ports");
|
||||
portsFiles.Length.ShouldBe(1);
|
||||
|
||||
var content = await File.ReadAllTextAsync(portsFiles[0]);
|
||||
content.ShouldContain($"\"client\":{port}");
|
||||
|
||||
await server.ShutdownAsync();
|
||||
Directory.GetFiles(_tempDir, "*.ports").Length.ShouldBe(0);
|
||||
|
||||
server.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user