feat: port session 04 — Logging, Signals & Services

- NatsLogger.cs: INatsLogger interface (Noticef/Warnf/Fatalf/Errorf/Debugf/Tracef),
  ServerLogging state class with atomic debug/trace flags, rate-limited logging
  (RateLimitWarnf/RateLimitDebugf), error variants (Errors/Errorc/Errorsc),
  MicrosoftLoggerAdapter bridging to ILogger
- SignalHandler.cs: ProcessSignal (Unix kill via Process), CommandToUnixSignal mapping
  (Stop→SIGKILL, Quit→SIGINT, Reopen→SIGUSR1, Reload→SIGHUP), ResolvePids via pgrep,
  SetProcessName, Run/IsWindowsService stubs for non-Windows
- 11 tests (6 logger, 5 signal/service)
- WASM/Windows signal stubs already n/a
- All 141 tests pass (140 unit + 1 integration)
- DB: features 368/3673 complete, tests 155/3257 complete (9.6% overall)
This commit is contained in:
Joseph Doherty
2026-02-26 11:54:25 -05:00
parent f08fc5d6a7
commit b8f2f66d45
7 changed files with 581 additions and 9 deletions

View File

@@ -0,0 +1,70 @@
// Copyright 2012-2025 The NATS Authors
// Licensed under the Apache License, Version 2.0
using System.Runtime.InteropServices;
using Shouldly;
using ZB.MOM.NatsNet.Server.Internal;
namespace ZB.MOM.NatsNet.Server.Tests.Internal;
/// <summary>
/// Tests for SignalHandler — mirrors tests from server/signal_test.go.
/// </summary>
public class SignalHandlerTests
{
/// <summary>
/// Mirrors CommandToSignal mapping tests.
/// </summary>
[Fact] // T:3158
public void CommandToUnixSignal_ShouldMapCorrectly()
{
SignalHandler.CommandToUnixSignal(ServerCommand.Stop).ShouldBe(UnixSignal.SigKill);
SignalHandler.CommandToUnixSignal(ServerCommand.Quit).ShouldBe(UnixSignal.SigInt);
SignalHandler.CommandToUnixSignal(ServerCommand.Reopen).ShouldBe(UnixSignal.SigUsr1);
SignalHandler.CommandToUnixSignal(ServerCommand.Reload).ShouldBe(UnixSignal.SigHup);
}
/// <summary>
/// Mirrors SetProcessName test.
/// </summary>
[Fact] // T:3155
public void SetProcessName_ShouldNotThrow()
{
Should.NotThrow(() => SignalHandler.SetProcessName("test-server"));
}
/// <summary>
/// Verify IsWindowsService returns false on non-Windows.
/// </summary>
[Fact] // T:3149
public void IsWindowsService_ShouldReturnFalse()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return; // Skip on Windows
SignalHandler.IsWindowsService().ShouldBeFalse();
}
/// <summary>
/// Mirrors Run — service.go Run() simply invokes the start function.
/// </summary>
[Fact] // T:3148
public void Run_ShouldInvokeStartAction()
{
var called = false;
SignalHandler.Run(() => called = true);
called.ShouldBeTrue();
}
/// <summary>
/// ProcessSignal with invalid PID expression should return error.
/// </summary>
[Fact] // T:3157
public void ProcessSignal_InvalidPid_ShouldReturnError()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return; // Skip on Windows
var err = SignalHandler.ProcessSignal(ServerCommand.Stop, "not-a-pid");
err.ShouldNotBeNull();
}
}