refactor: extract NATS.Server.Monitoring.Tests project

Move 39 monitoring, events, and system endpoint test files from
NATS.Server.Tests into a dedicated NATS.Server.Monitoring.Tests project.
Update namespaces, replace private GetFreePort/ReadUntilAsync with
TestUtilities shared helpers, add InternalsVisibleTo, and register
in the solution file. All 439 tests pass.
This commit is contained in:
Joseph Doherty
2026-03-12 15:44:12 -04:00
parent edf9ed770e
commit 0c086522a4
42 changed files with 131 additions and 257 deletions

View File

@@ -12,6 +12,7 @@
<Project Path="tests/NATS.Server.LeafNodes.Tests/NATS.Server.LeafNodes.Tests.csproj" /> <Project Path="tests/NATS.Server.LeafNodes.Tests/NATS.Server.LeafNodes.Tests.csproj" />
<Project Path="tests/NATS.Server.Clustering.Tests/NATS.Server.Clustering.Tests.csproj" /> <Project Path="tests/NATS.Server.Clustering.Tests/NATS.Server.Clustering.Tests.csproj" />
<Project Path="tests/NATS.Server.Raft.Tests/NATS.Server.Raft.Tests.csproj" /> <Project Path="tests/NATS.Server.Raft.Tests/NATS.Server.Raft.Tests.csproj" />
<Project Path="tests/NATS.Server.Monitoring.Tests/NATS.Server.Monitoring.Tests.csproj" />
<Project Path="tests/NATS.E2E.Tests/NATS.E2E.Tests.csproj" /> <Project Path="tests/NATS.E2E.Tests/NATS.E2E.Tests.csproj" />
</Folder> </Folder>
</Solution> </Solution>

View File

@@ -7,6 +7,7 @@
<InternalsVisibleTo Include="NATS.Server.LeafNodes.Tests" /> <InternalsVisibleTo Include="NATS.Server.LeafNodes.Tests" />
<InternalsVisibleTo Include="NATS.Server.Clustering.Tests" /> <InternalsVisibleTo Include="NATS.Server.Clustering.Tests" />
<InternalsVisibleTo Include="NATS.Server.Raft.Tests" /> <InternalsVisibleTo Include="NATS.Server.Raft.Tests" />
<InternalsVisibleTo Include="NATS.Server.Monitoring.Tests" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" /> <FrameworkReference Include="Microsoft.AspNetCore.App" />

View File

@@ -1,8 +1,9 @@
using System.Text.Json; using System.Text.Json;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using NATS.Server.Events; using NATS.Server.Events;
using NATS.Server.TestUtilities;
namespace NATS.Server.Tests; namespace NATS.Server.Monitoring.Tests;
public class EventSystemTests public class EventSystemTests
{ {
@@ -105,17 +106,8 @@ public class EventSystemTests
private static NatsServer CreateTestServer() private static NatsServer CreateTestServer()
{ {
var port = GetFreePort(); var port = TestPortAllocator.GetFreePort();
return new NatsServer(new NatsOptions { Port = port }, NullLoggerFactory.Instance); return new NatsServer(new NatsOptions { Port = port }, NullLoggerFactory.Instance);
} }
private static int GetFreePort()
{
using var sock = new System.Net.Sockets.Socket(
System.Net.Sockets.AddressFamily.InterNetwork,
System.Net.Sockets.SocketType.Stream,
System.Net.Sockets.ProtocolType.Tcp);
sock.Bind(new System.Net.IPEndPoint(System.Net.IPAddress.Loopback, 0));
return ((System.Net.IPEndPoint)sock.LocalEndPoint!).Port;
}
} }

View File

@@ -9,8 +9,9 @@ using System.Text.Json;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using NATS.Server; using NATS.Server;
using NATS.Server.Events; using NATS.Server.Events;
using NATS.Server.TestUtilities;
namespace NATS.Server.Tests.Events; namespace NATS.Server.Monitoring.Tests.Events;
/// <summary> /// <summary>
/// Tests for <see cref="InternalEventSystem.SendAuthErrorEvent"/>, /// Tests for <see cref="InternalEventSystem.SendAuthErrorEvent"/>,
@@ -27,7 +28,7 @@ public class AuthErrorEventTests : IAsyncLifetime
public async Task InitializeAsync() public async Task InitializeAsync()
{ {
_port = GetFreePort(); _port = TestPortAllocator.GetFreePort();
_server = new NatsServer(new NatsOptions { Port = _port }, NullLoggerFactory.Instance); _server = new NatsServer(new NatsOptions { Port = _port }, NullLoggerFactory.Instance);
_ = _server.StartAsync(CancellationToken.None); _ = _server.StartAsync(CancellationToken.None);
await _server.WaitForReadyAsync(); await _server.WaitForReadyAsync();
@@ -39,15 +40,6 @@ public class AuthErrorEventTests : IAsyncLifetime
_server.Dispose(); _server.Dispose();
} }
private static int GetFreePort()
{
using var sock = new System.Net.Sockets.Socket(
System.Net.Sockets.AddressFamily.InterNetwork,
System.Net.Sockets.SocketType.Stream,
System.Net.Sockets.ProtocolType.Tcp);
sock.Bind(new System.Net.IPEndPoint(System.Net.IPAddress.Loopback, 0));
return ((System.Net.IPEndPoint)sock.LocalEndPoint!).Port;
}
// ======================================================================== // ========================================================================
// AuthErrorEventCount // AuthErrorEventCount

View File

@@ -1,7 +1,7 @@
using System.Text.Json; using System.Text.Json;
using NATS.Server.Events; using NATS.Server.Events;
namespace NATS.Server.Tests.Events; namespace NATS.Server.Monitoring.Tests.Events;
public class EventApiAndSubjectsParityBatch2Tests public class EventApiAndSubjectsParityBatch2Tests
{ {

View File

@@ -4,7 +4,7 @@
using System.Text; using System.Text;
using NATS.Server.Events; using NATS.Server.Events;
namespace NATS.Server.Tests.Events; namespace NATS.Server.Monitoring.Tests.Events;
/// <summary> /// <summary>
/// Tests for <see cref="EventCompressor"/> — S2/Snappy compression for system event payloads. /// Tests for <see cref="EventCompressor"/> — S2/Snappy compression for system event payloads.

View File

@@ -9,7 +9,7 @@
using System.Text.Json; using System.Text.Json;
using NATS.Server.Events; using NATS.Server.Events;
namespace NATS.Server.Tests.Events; namespace NATS.Server.Monitoring.Tests.Events;
/// <summary> /// <summary>
/// Parity tests ported from Go server/events_test.go exercising /// Parity tests ported from Go server/events_test.go exercising
@@ -278,7 +278,7 @@ public class EventGoParityTests
var data = new ServerStatsData var data = new ServerStatsData
{ {
SlowConsumers = 10, SlowConsumers = 10,
SlowConsumerStats = new SlowConsumersStats SlowConsumerStats = new NATS.Server.Events.SlowConsumersStats
{ {
Clients = 5, Clients = 5,
Routes = 2, Routes = 2,
@@ -302,7 +302,7 @@ public class EventGoParityTests
var data = new ServerStatsData var data = new ServerStatsData
{ {
StaleConnections = 7, StaleConnections = 7,
StaleConnectionStats = new StaleConnectionStats StaleConnectionStats = new NATS.Server.Events.StaleConnectionStats
{ {
Clients = 3, Clients = 3,
Routes = 1, Routes = 1,

View File

@@ -1,7 +1,7 @@
using System.Text.Json; using System.Text.Json;
using NATS.Server.Events; using NATS.Server.Events;
namespace NATS.Server.Tests.Events; namespace NATS.Server.Monitoring.Tests.Events;
/// <summary> /// <summary>
/// Tests that all event DTOs have complete JSON fields matching Go's output. /// Tests that all event DTOs have complete JSON fields matching Go's output.
@@ -292,9 +292,9 @@ public class EventPayloadTests
InBytes = 40_000, InBytes = 40_000,
OutBytes = 50_000, OutBytes = 50_000,
SlowConsumers = 2, SlowConsumers = 2,
SlowConsumerStats = new SlowConsumersStats { Clients = 1, Routes = 1 }, SlowConsumerStats = new NATS.Server.Events.SlowConsumersStats { Clients = 1, Routes = 1 },
StaleConnections = 3, StaleConnections = 3,
StaleConnectionStats = new StaleConnectionStats { Clients = 2, Leafs = 1 }, StaleConnectionStats = new NATS.Server.Events.StaleConnectionStats { Clients = 2, Leafs = 1 },
ActiveServers = 3, ActiveServers = 3,
Routes = [new RouteStat { Id = 1, Name = "r1", Sent = new DataStats { Msgs = 10 }, Received = new DataStats { Msgs = 5 }, Pending = 0 }], Routes = [new RouteStat { Id = 1, Name = "r1", Sent = new DataStats { Msgs = 10 }, Received = new DataStats { Msgs = 5 }, Pending = 0 }],
Gateways = [new GatewayStat { Id = 1, Name = "gw1", Sent = new DataStats { Msgs = 20 }, Received = new DataStats { Msgs = 15 }, InboundConnections = 2 }], Gateways = [new GatewayStat { Id = 1, Name = "gw1", Sent = new DataStats { Msgs = 20 }, Received = new DataStats { Msgs = 15 }, InboundConnections = 2 }],

View File

@@ -1,7 +1,7 @@
using System.Text.Json; using System.Text.Json;
using NATS.Server.Events; using NATS.Server.Events;
namespace NATS.Server.Tests.Events; namespace NATS.Server.Monitoring.Tests.Events;
public class EventServerInfoCapabilityParityBatch1Tests public class EventServerInfoCapabilityParityBatch1Tests
{ {

View File

@@ -1,7 +1,7 @@
using System.Text.Json; using System.Text.Json;
using NATS.Server.Events; using NATS.Server.Events;
namespace NATS.Server.Tests.Events; namespace NATS.Server.Monitoring.Tests.Events;
/// <summary> /// <summary>
/// Tests that EventBuilder produces fully-populated system event messages /// Tests that EventBuilder produces fully-populated system event messages

View File

@@ -8,7 +8,7 @@
using System.Text.Json; using System.Text.Json;
using NATS.Server.Events; using NATS.Server.Events;
namespace NATS.Server.Tests.Events; namespace NATS.Server.Monitoring.Tests.Events;
/// <summary> /// <summary>
/// Tests for <see cref="OcspPeerRejectEventMsg"/>, <see cref="OcspChainValidationEvent"/>, /// Tests for <see cref="OcspPeerRejectEventMsg"/>, <see cref="OcspChainValidationEvent"/>,

View File

@@ -1,7 +1,7 @@
using System.Text.Json; using System.Text.Json;
using NATS.Server.Events; using NATS.Server.Events;
namespace NATS.Server.Tests.Events; namespace NATS.Server.Monitoring.Tests.Events;
/// <summary> /// <summary>
/// Tests for remote server and leaf node event DTOs and subject constants. /// Tests for remote server and leaf node event DTOs and subject constants.

View File

@@ -5,8 +5,9 @@ using Microsoft.Extensions.Logging.Abstractions;
using NATS.Server; using NATS.Server;
using NATS.Server.Auth; using NATS.Server.Auth;
using NATS.Server.Events; using NATS.Server.Events;
using NATS.Server.TestUtilities;
namespace NATS.Server.Tests.Events; namespace NATS.Server.Monitoring.Tests.Events;
/// <summary> /// <summary>
/// Tests for server lifecycle events, stats tracking, advisory messages, and /// Tests for server lifecycle events, stats tracking, advisory messages, and
@@ -21,7 +22,7 @@ public class ServerEventTests : IAsyncLifetime
public ServerEventTests() public ServerEventTests()
{ {
_port = GetFreePort(); _port = TestPortAllocator.GetFreePort();
_server = new NatsServer(new NatsOptions { Port = _port }, NullLoggerFactory.Instance); _server = new NatsServer(new NatsOptions { Port = _port }, NullLoggerFactory.Instance);
} }
@@ -37,12 +38,6 @@ public class ServerEventTests : IAsyncLifetime
_server.Dispose(); _server.Dispose();
} }
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;
}
private async Task<Socket> ConnectAndHandshakeAsync() private async Task<Socket> ConnectAndHandshakeAsync()
{ {
@@ -54,24 +49,10 @@ public class ServerEventTests : IAsyncLifetime
// Send CONNECT + PING // Send CONNECT + PING
await sock.SendAsync(Encoding.ASCII.GetBytes("CONNECT {}\r\nPING\r\n")); await sock.SendAsync(Encoding.ASCII.GetBytes("CONNECT {}\r\nPING\r\n"));
// Read PONG (may include -ERR or other lines) // Read PONG (may include -ERR or other lines)
await ReadUntilAsync(sock, "PONG"); await SocketTestHelper.ReadUntilAsync(sock, "PONG");
return sock; return sock;
} }
private static async Task<string> ReadUntilAsync(Socket sock, string expected, int timeoutMs = 5000)
{
using var cts = new CancellationTokenSource(timeoutMs);
var sb = new StringBuilder();
var buf = new byte[4096];
while (!sb.ToString().Contains(expected))
{
var n = await sock.ReceiveAsync(buf, SocketFlags.None, cts.Token);
if (n == 0) break;
sb.Append(Encoding.ASCII.GetString(buf, 0, n));
}
return sb.ToString();
}
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// Server lifecycle events // Server lifecycle events
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
@@ -182,7 +163,7 @@ public class ServerEventTests : IAsyncLifetime
await sock.SendAsync(pub); await sock.SendAsync(pub);
// Flush via PING/PONG // Flush via PING/PONG
await sock.SendAsync(Encoding.ASCII.GetBytes("PING\r\n")); await sock.SendAsync(Encoding.ASCII.GetBytes("PING\r\n"));
await ReadUntilAsync(sock, "PONG"); await SocketTestHelper.ReadUntilAsync(sock, "PONG");
var afterMsgs = Interlocked.Read(ref _server.Stats.InMsgs); var afterMsgs = Interlocked.Read(ref _server.Stats.InMsgs);
var afterBytes = Interlocked.Read(ref _server.Stats.InBytes); var afterBytes = Interlocked.Read(ref _server.Stats.InBytes);
@@ -203,17 +184,17 @@ public class ServerEventTests : IAsyncLifetime
// Subscribe // Subscribe
await sub.SendAsync(Encoding.ASCII.GetBytes("SUB test.out 1\r\nPING\r\n")); await sub.SendAsync(Encoding.ASCII.GetBytes("SUB test.out 1\r\nPING\r\n"));
await ReadUntilAsync(sub, "PONG"); await SocketTestHelper.ReadUntilAsync(sub, "PONG");
var beforeOut = Interlocked.Read(ref _server.Stats.OutMsgs); var beforeOut = Interlocked.Read(ref _server.Stats.OutMsgs);
var payload = "World"u8.ToArray(); var payload = "World"u8.ToArray();
await pub.SendAsync(Encoding.ASCII.GetBytes($"PUB test.out {payload.Length}\r\nWorld\r\n")); await pub.SendAsync(Encoding.ASCII.GetBytes($"PUB test.out {payload.Length}\r\nWorld\r\n"));
await pub.SendAsync(Encoding.ASCII.GetBytes("PING\r\n")); await pub.SendAsync(Encoding.ASCII.GetBytes("PING\r\n"));
await ReadUntilAsync(pub, "PONG"); await SocketTestHelper.ReadUntilAsync(pub, "PONG");
// Give delivery loop time to flush // Give delivery loop time to flush
await ReadUntilAsync(sub, "World", timeoutMs: 2000); await SocketTestHelper.ReadUntilAsync(sub, "World", timeoutMs: 2000);
var afterOut = Interlocked.Read(ref _server.Stats.OutMsgs); var afterOut = Interlocked.Read(ref _server.Stats.OutMsgs);
(afterOut - beforeOut).ShouldBeGreaterThanOrEqualTo(1L); (afterOut - beforeOut).ShouldBeGreaterThanOrEqualTo(1L);

View File

@@ -5,8 +5,9 @@ using System.Text;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using NATS.Server.Configuration; using NATS.Server.Configuration;
using NATS.Server.Monitoring; using NATS.Server.Monitoring;
using NATS.Server.TestUtilities;
namespace NATS.Server.Tests; namespace NATS.Server.Monitoring.Tests;
public class JszMonitorTests public class JszMonitorTests
{ {
@@ -36,8 +37,8 @@ internal sealed class JetStreamMonitoringFixture : IAsyncDisposable
public static async Task<JetStreamMonitoringFixture> StartWithStreamAndConsumerAsync() public static async Task<JetStreamMonitoringFixture> StartWithStreamAndConsumerAsync()
{ {
var natsPort = GetFreePort(); var natsPort = TestPortAllocator.GetFreePort();
var monitorPort = GetFreePort(); var monitorPort = TestPortAllocator.GetFreePort();
var options = new NatsOptions var options = new NatsOptions
{ {
Host = "127.0.0.1", Host = "127.0.0.1",
@@ -103,10 +104,4 @@ internal sealed class JetStreamMonitoringFixture : IAsyncDisposable
throw new TimeoutException("Monitoring endpoint did not become healthy."); throw new TimeoutException("Monitoring endpoint did not become healthy.");
} }
private static int GetFreePort()
{
using var sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Bind(new System.Net.IPEndPoint(System.Net.IPAddress.Loopback, 0));
return ((System.Net.IPEndPoint)sock.LocalEndPoint!).Port;
}
} }

View File

@@ -2,8 +2,9 @@ using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using NATS.Server.Configuration; using NATS.Server.Configuration;
using NATS.Server.TestUtilities;
namespace NATS.Server.Tests; namespace NATS.Server.Monitoring.Tests;
public class MonitorClusterEndpointTests public class MonitorClusterEndpointTests
{ {
@@ -35,7 +36,7 @@ internal sealed class MonitorFixture : IAsyncDisposable
public static async Task<MonitorFixture> StartClusterEnabledAsync() public static async Task<MonitorFixture> StartClusterEnabledAsync()
{ {
var monitorPort = GetFreePort(); var monitorPort = TestPortAllocator.GetFreePort();
var options = new NatsOptions var options = new NatsOptions
{ {
Host = "127.0.0.1", Host = "127.0.0.1",
@@ -96,10 +97,4 @@ internal sealed class MonitorFixture : IAsyncDisposable
_cts.Dispose(); _cts.Dispose();
} }
private static int GetFreePort()
{
using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(new IPEndPoint(IPAddress.Loopback, 0));
return ((IPEndPoint)socket.LocalEndPoint!).Port;
}
} }

View File

@@ -1,7 +1,7 @@
using System.Text.Json; using System.Text.Json;
using NATS.Server.Monitoring; using NATS.Server.Monitoring;
namespace NATS.Server.Tests; namespace NATS.Server.Monitoring.Tests;
public class MonitorModelTests public class MonitorModelTests
{ {

View File

@@ -7,7 +7,7 @@ using Microsoft.Extensions.Logging.Abstractions;
using NATS.Server.Monitoring; using NATS.Server.Monitoring;
using NATS.Server.TestUtilities; using NATS.Server.TestUtilities;
namespace NATS.Server.Tests; namespace NATS.Server.Monitoring.Tests;
public class MonitorTests : IAsyncLifetime public class MonitorTests : IAsyncLifetime
{ {
@@ -19,8 +19,8 @@ public class MonitorTests : IAsyncLifetime
public MonitorTests() public MonitorTests()
{ {
_natsPort = GetFreePort(); _natsPort = TestPortAllocator.GetFreePort();
_monitorPort = GetFreePort(); _monitorPort = TestPortAllocator.GetFreePort();
_server = new NatsServer( _server = new NatsServer(
new NatsOptions { Port = _natsPort, MonitorPort = _monitorPort }, new NatsOptions { Port = _natsPort, MonitorPort = _monitorPort },
NullLoggerFactory.Instance); NullLoggerFactory.Instance);
@@ -289,12 +289,6 @@ public class MonitorTests : IAsyncLifetime
varz.Mqtt.MaxAckPending.ShouldBe((ushort)0); varz.Mqtt.MaxAckPending.ShouldBe((ushort)0);
} }
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;
}
} }
public class MonitorTlsTests : IAsyncLifetime public class MonitorTlsTests : IAsyncLifetime
@@ -309,8 +303,8 @@ public class MonitorTlsTests : IAsyncLifetime
public MonitorTlsTests() public MonitorTlsTests()
{ {
_natsPort = GetFreePort(); _natsPort = TestPortAllocator.GetFreePort();
_monitorPort = GetFreePort(); _monitorPort = TestPortAllocator.GetFreePort();
(_certPath, _keyPath) = TestCertHelper.GenerateTestCertFiles(); (_certPath, _keyPath) = TestCertHelper.GenerateTestCertFiles();
_server = new NatsServer( _server = new NatsServer(
new NatsOptions new NatsOptions
@@ -375,10 +369,4 @@ public class MonitorTlsTests : IAsyncLifetime
conn.TlsCipherSuite.ShouldNotBeNullOrEmpty(); conn.TlsCipherSuite.ShouldNotBeNullOrEmpty();
} }
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;
}
} }

View File

@@ -4,7 +4,7 @@
using NATS.Server.Monitoring; using NATS.Server.Monitoring;
namespace NATS.Server.Tests.Monitoring; namespace NATS.Server.Monitoring.Tests.Monitoring;
public class ClosedConnectionRingBufferTests public class ClosedConnectionRingBufferTests
{ {

View File

@@ -5,7 +5,7 @@
using NATS.Server.Monitoring; using NATS.Server.Monitoring;
namespace NATS.Server.Tests.Monitoring; namespace NATS.Server.Monitoring.Tests.Monitoring;
public class ClosedReasonTests public class ClosedReasonTests
{ {

View File

@@ -1,6 +1,6 @@
using NATS.Server.Monitoring; using NATS.Server.Monitoring;
namespace NATS.Server.Tests.Monitoring; namespace NATS.Server.Monitoring.Tests.Monitoring;
/// <summary> /// <summary>
/// Unit tests for account-scoped /connz filtering (Gap 10.2). /// Unit tests for account-scoped /connz filtering (Gap 10.2).

View File

@@ -6,8 +6,9 @@ using Microsoft.Extensions.Logging.Abstractions;
using NATS.Server; using NATS.Server;
using NATS.Server.Auth; using NATS.Server.Auth;
using NATS.Server.Monitoring; using NATS.Server.Monitoring;
using NATS.Server.TestUtilities;
namespace NATS.Server.Tests.Monitoring; namespace NATS.Server.Monitoring.Tests.Monitoring;
/// <summary> /// <summary>
/// Tests for ConnzHandler filtering, sorting, pagination, and closed connection /// Tests for ConnzHandler filtering, sorting, pagination, and closed connection
@@ -26,7 +27,7 @@ public class ConnzFilterTests : IAsyncLifetime
{ {
_opts = new NatsOptions _opts = new NatsOptions
{ {
Port = GetFreePort(), Port = TestPortAllocator.GetFreePort(),
MaxClosedClients = 100, MaxClosedClients = 100,
Users = Users =
[ [
@@ -54,12 +55,6 @@ public class ConnzFilterTests : IAsyncLifetime
_server.Dispose(); _server.Dispose();
} }
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;
}
private async Task<Socket> ConnectAsync(string user, string? subjectToSubscribe = null) private async Task<Socket> ConnectAsync(string user, string? subjectToSubscribe = null)
{ {
@@ -79,7 +74,7 @@ public class ConnzFilterTests : IAsyncLifetime
} }
await sock.SendAsync("PING\r\n"u8.ToArray()); await sock.SendAsync("PING\r\n"u8.ToArray());
await ReadUntilAsync(sock, "PONG"); await SocketTestHelper.ReadUntilAsync(sock, "PONG");
return sock; return sock;
} }
@@ -395,26 +390,4 @@ public class ConnzFilterTests : IAsyncLifetime
single.Conns[0].Stop.ShouldNotBeNull(); single.Conns[0].Stop.ShouldNotBeNull();
} }
private static async Task ReadUntilAsync(Socket sock, string expected)
{
var buf = new byte[4096];
var all = new StringBuilder();
var deadline = DateTime.UtcNow.AddSeconds(5);
while (DateTime.UtcNow < deadline)
{
if (sock.Available > 0)
{
var n = await sock.ReceiveAsync(buf, SocketFlags.None);
all.Append(Encoding.ASCII.GetString(buf, 0, n));
if (all.ToString().Contains(expected))
return;
}
else
{
await Task.Delay(10);
}
}
throw new TimeoutException($"Did not receive '{expected}' within 5 seconds. Got: {all}");
}
} }

View File

@@ -1,7 +1,7 @@
using System.Text.Json; using System.Text.Json;
using System.Text; using System.Text;
namespace NATS.Server.Tests; namespace NATS.Server.Monitoring.Tests;
public class ConnzParityFieldTests public class ConnzParityFieldTests
{ {

View File

@@ -6,7 +6,7 @@ using Microsoft.Extensions.Logging.Abstractions;
using NATS.Server.Auth; using NATS.Server.Auth;
using NATS.Server.Monitoring; using NATS.Server.Monitoring;
namespace NATS.Server.Tests; namespace NATS.Server.Monitoring.Tests;
public class ConnzParityFilterTests public class ConnzParityFilterTests
{ {

View File

@@ -7,8 +7,9 @@ using System.Net.Http.Json;
using System.Net.Sockets; using System.Net.Sockets;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using NATS.Server.Monitoring; using NATS.Server.Monitoring;
using NATS.Server.TestUtilities;
namespace NATS.Server.Tests; namespace NATS.Server.Monitoring.Tests;
public class ConnzParityTests : IAsyncLifetime public class ConnzParityTests : IAsyncLifetime
{ {
@@ -20,8 +21,8 @@ public class ConnzParityTests : IAsyncLifetime
public ConnzParityTests() public ConnzParityTests()
{ {
_natsPort = GetFreePort(); _natsPort = TestPortAllocator.GetFreePort();
_monitorPort = GetFreePort(); _monitorPort = TestPortAllocator.GetFreePort();
_server = new NatsServer( _server = new NatsServer(
new NatsOptions { Port = _natsPort, MonitorPort = _monitorPort }, new NatsOptions { Port = _natsPort, MonitorPort = _monitorPort },
NullLoggerFactory.Instance); NullLoggerFactory.Instance);
@@ -167,10 +168,4 @@ public class ConnzParityTests : IAsyncLifetime
} }
} }
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;
}
} }

View File

@@ -1,6 +1,6 @@
using NATS.Server.Monitoring; using NATS.Server.Monitoring;
namespace NATS.Server.Tests.Monitoring; namespace NATS.Server.Monitoring.Tests.Monitoring;
/// <summary> /// <summary>
/// Unit tests for ConnzSortOption enum, ConnzSorter.Parse, and ConnzSorter.Sort. /// Unit tests for ConnzSortOption enum, ConnzSorter.Parse, and ConnzSorter.Sort.

View File

@@ -4,8 +4,9 @@
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using NATS.Server.TestUtilities;
namespace NATS.Server.Tests; namespace NATS.Server.Monitoring.Tests;
public class HealthzParityTests : IAsyncLifetime public class HealthzParityTests : IAsyncLifetime
{ {
@@ -16,7 +17,7 @@ public class HealthzParityTests : IAsyncLifetime
public HealthzParityTests() public HealthzParityTests()
{ {
_monitorPort = GetFreePort(); _monitorPort = TestPortAllocator.GetFreePort();
_server = new NatsServer( _server = new NatsServer(
new NatsOptions { Port = 0, MonitorPort = _monitorPort }, new NatsOptions { Port = 0, MonitorPort = _monitorPort },
NullLoggerFactory.Instance); NullLoggerFactory.Instance);
@@ -73,10 +74,4 @@ public class HealthzParityTests : IAsyncLifetime
body.ShouldContain("ok"); body.ShouldContain("ok");
} }
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;
}
} }

View File

@@ -26,8 +26,9 @@ using System.Net.Http.Json;
using System.Net.Sockets; using System.Net.Sockets;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using NATS.Server.Monitoring; using NATS.Server.Monitoring;
using NATS.Server.TestUtilities;
namespace NATS.Server.Tests.Monitoring; namespace NATS.Server.Monitoring.Tests.Monitoring;
/// <summary> /// <summary>
/// Tests covering /connz endpoint behavior, ported from the Go server's monitor_test.go. /// Tests covering /connz endpoint behavior, ported from the Go server's monitor_test.go.
@@ -42,8 +43,8 @@ public class MonitorConnzTests : IAsyncLifetime
public MonitorConnzTests() public MonitorConnzTests()
{ {
_natsPort = GetFreePort(); _natsPort = TestPortAllocator.GetFreePort();
_monitorPort = GetFreePort(); _monitorPort = TestPortAllocator.GetFreePort();
_server = new NatsServer( _server = new NatsServer(
new NatsOptions { Port = _natsPort, MonitorPort = _monitorPort }, new NatsOptions { Port = _natsPort, MonitorPort = _monitorPort },
NullLoggerFactory.Instance); NullLoggerFactory.Instance);
@@ -816,10 +817,4 @@ public class MonitorConnzTests : IAsyncLifetime
return sock; return sock;
} }
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;
}
} }

View File

@@ -10,8 +10,9 @@ using System.Net.Sockets;
using System.Text.Json; using System.Text.Json;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using NATS.Server.Monitoring; using NATS.Server.Monitoring;
using NATS.Server.TestUtilities;
namespace NATS.Server.Tests.Monitoring; namespace NATS.Server.Monitoring.Tests.Monitoring;
/// <summary> /// <summary>
/// Parity tests ported from Go server/monitor_test.go exercising /connz /// Parity tests ported from Go server/monitor_test.go exercising /connz
@@ -474,8 +475,8 @@ public class MonitorGoParityEndpointTests : IAsyncLifetime
public MonitorGoParityEndpointTests() public MonitorGoParityEndpointTests()
{ {
_natsPort = GetFreePort(); _natsPort = TestPortAllocator.GetFreePort();
_monitorPort = GetFreePort(); _monitorPort = TestPortAllocator.GetFreePort();
_server = new NatsServer( _server = new NatsServer(
new NatsOptions { Port = _natsPort, MonitorPort = _monitorPort }, new NatsOptions { Port = _natsPort, MonitorPort = _monitorPort },
NullLoggerFactory.Instance); NullLoggerFactory.Instance);
@@ -1808,10 +1809,4 @@ public class MonitorGoParityEndpointTests : IAsyncLifetime
} }
} }
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;
}
} }

View File

@@ -11,8 +11,9 @@ using System.Text.Json;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using NATS.Server.Configuration; using NATS.Server.Configuration;
using NATS.Server.Monitoring; using NATS.Server.Monitoring;
using NATS.Server.TestUtilities;
namespace NATS.Server.Tests.Monitoring; namespace NATS.Server.Monitoring.Tests.Monitoring;
/// <summary> /// <summary>
/// Tests covering /routez endpoint behavior, ported from the Go server's monitor_test.go. /// Tests covering /routez endpoint behavior, ported from the Go server's monitor_test.go.
@@ -184,7 +185,7 @@ internal sealed class RoutezFixture : IAsyncDisposable
public static async Task<RoutezFixture> StartAsync() public static async Task<RoutezFixture> StartAsync()
{ {
var monitorPort = GetFreePort(); var monitorPort = TestPortAllocator.GetFreePort();
var options = new NatsOptions var options = new NatsOptions
{ {
Host = "127.0.0.1", Host = "127.0.0.1",
@@ -197,7 +198,7 @@ internal sealed class RoutezFixture : IAsyncDisposable
public static async Task<RoutezFixture> StartWithClusterAsync() public static async Task<RoutezFixture> StartWithClusterAsync()
{ {
var monitorPort = GetFreePort(); var monitorPort = TestPortAllocator.GetFreePort();
var options = new NatsOptions var options = new NatsOptions
{ {
Host = "127.0.0.1", Host = "127.0.0.1",
@@ -259,10 +260,4 @@ internal sealed class RoutezFixture : IAsyncDisposable
_cts.Dispose(); _cts.Dispose();
} }
private static int GetFreePort()
{
using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(new IPEndPoint(IPAddress.Loopback, 0));
return ((IPEndPoint)socket.LocalEndPoint!).Port;
}
} }

View File

@@ -11,8 +11,9 @@ using System.Net.Sockets;
using System.Text.Json; using System.Text.Json;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using NATS.Server.Monitoring; using NATS.Server.Monitoring;
using NATS.Server.TestUtilities;
namespace NATS.Server.Tests.Monitoring; namespace NATS.Server.Monitoring.Tests.Monitoring;
/// <summary> /// <summary>
/// Tests covering miscellaneous monitoring endpoints: root, accountz, accstatz, /// Tests covering miscellaneous monitoring endpoints: root, accountz, accstatz,
@@ -29,8 +30,8 @@ public class MonitorStackszTests : IAsyncLifetime
public MonitorStackszTests() public MonitorStackszTests()
{ {
_natsPort = GetFreePort(); _natsPort = TestPortAllocator.GetFreePort();
_monitorPort = GetFreePort(); _monitorPort = TestPortAllocator.GetFreePort();
_server = new NatsServer( _server = new NatsServer(
new NatsOptions { Port = _natsPort, MonitorPort = _monitorPort }, new NatsOptions { Port = _natsPort, MonitorPort = _monitorPort },
NullLoggerFactory.Instance); NullLoggerFactory.Instance);
@@ -346,10 +347,4 @@ public class MonitorStackszTests : IAsyncLifetime
body.ShouldContain("leafs"); body.ShouldContain("leafs");
} }
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;
}
} }

View File

@@ -11,8 +11,9 @@ using System.Net.Sockets;
using System.Text.Json; using System.Text.Json;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using NATS.Server.Monitoring; using NATS.Server.Monitoring;
using NATS.Server.TestUtilities;
namespace NATS.Server.Tests.Monitoring; namespace NATS.Server.Monitoring.Tests.Monitoring;
/// <summary> /// <summary>
/// Tests covering /subz (subscriptionsz) endpoint behavior, /// Tests covering /subz (subscriptionsz) endpoint behavior,
@@ -28,8 +29,8 @@ public class MonitorSubszTests : IAsyncLifetime
public MonitorSubszTests() public MonitorSubszTests()
{ {
_natsPort = GetFreePort(); _natsPort = TestPortAllocator.GetFreePort();
_monitorPort = GetFreePort(); _monitorPort = TestPortAllocator.GetFreePort();
_server = new NatsServer( _server = new NatsServer(
new NatsOptions { Port = _natsPort, MonitorPort = _monitorPort }, new NatsOptions { Port = _natsPort, MonitorPort = _monitorPort },
NullLoggerFactory.Instance); NullLoggerFactory.Instance);
@@ -350,10 +351,4 @@ public class MonitorSubszTests : IAsyncLifetime
return sock; return sock;
} }
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;
}
} }

View File

@@ -14,8 +14,9 @@ using System.Net.Sockets;
using System.Text.Json; using System.Text.Json;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using NATS.Server.Monitoring; using NATS.Server.Monitoring;
using NATS.Server.TestUtilities;
namespace NATS.Server.Tests.Monitoring; namespace NATS.Server.Monitoring.Tests.Monitoring;
/// <summary> /// <summary>
/// Tests covering /varz endpoint behavior, ported from the Go server's monitor_test.go. /// Tests covering /varz endpoint behavior, ported from the Go server's monitor_test.go.
@@ -30,8 +31,8 @@ public class MonitorVarzTests : IAsyncLifetime
public MonitorVarzTests() public MonitorVarzTests()
{ {
_natsPort = GetFreePort(); _natsPort = TestPortAllocator.GetFreePort();
_monitorPort = GetFreePort(); _monitorPort = TestPortAllocator.GetFreePort();
_server = new NatsServer( _server = new NatsServer(
new NatsOptions { Port = _natsPort, MonitorPort = _monitorPort }, new NatsOptions { Port = _natsPort, MonitorPort = _monitorPort },
NullLoggerFactory.Instance); NullLoggerFactory.Instance);
@@ -467,7 +468,7 @@ public class MonitorVarzTests : IAsyncLifetime
[Fact] [Fact]
public async Task Monitor_not_accessible_when_port_not_configured() public async Task Monitor_not_accessible_when_port_not_configured()
{ {
var natsPort = GetFreePort(); var natsPort = TestPortAllocator.GetFreePort();
var server = new NatsServer( var server = new NatsServer(
new NatsOptions { Port = natsPort, MonitorPort = 0 }, new NatsOptions { Port = natsPort, MonitorPort = 0 },
NullLoggerFactory.Instance); NullLoggerFactory.Instance);
@@ -517,10 +518,4 @@ public class MonitorVarzTests : IAsyncLifetime
return $"{(int)ts.TotalSeconds}s"; return $"{(int)ts.TotalSeconds}s";
} }
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;
}
} }

View File

@@ -1,7 +1,7 @@
using System.Text.Json; using System.Text.Json;
using NATS.Server.Monitoring; using NATS.Server.Monitoring;
namespace NATS.Server.Tests.Monitoring; namespace NATS.Server.Monitoring.Tests.Monitoring;
public class MonitoringHealthAndSortParityBatch1Tests public class MonitoringHealthAndSortParityBatch1Tests
{ {

View File

@@ -1,8 +1,9 @@
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using NATS.Server.TestUtilities;
namespace NATS.Server.Tests; namespace NATS.Server.Monitoring.Tests;
public class PprofEndpointTests public class PprofEndpointTests
{ {
@@ -32,7 +33,7 @@ internal sealed class PprofMonitorFixture : IAsyncDisposable
public static async Task<PprofMonitorFixture> StartWithProfilingAsync() public static async Task<PprofMonitorFixture> StartWithProfilingAsync()
{ {
var monitorPort = GetFreePort(); var monitorPort = TestPortAllocator.GetFreePort();
var options = new NatsOptions var options = new NatsOptions
{ {
Host = "127.0.0.1", Host = "127.0.0.1",
@@ -83,10 +84,4 @@ internal sealed class PprofMonitorFixture : IAsyncDisposable
_cts.Dispose(); _cts.Dispose();
} }
private static int GetFreePort()
{
using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(new IPEndPoint(IPAddress.Loopback, 0));
return ((IPEndPoint)socket.LocalEndPoint!).Port;
}
} }

View File

@@ -1,6 +1,6 @@
using System.Text.Json; using System.Text.Json;
namespace NATS.Server.Tests.Monitoring; namespace NATS.Server.Monitoring.Tests.Monitoring;
public class PprofRuntimeParityTests public class PprofRuntimeParityTests
{ {

View File

@@ -3,7 +3,7 @@ using System.Security.Cryptography.X509Certificates;
using System.Text.Json; using System.Text.Json;
using NATS.Server.Monitoring; using NATS.Server.Monitoring;
namespace NATS.Server.Tests.Monitoring; namespace NATS.Server.Monitoring.Tests.Monitoring;
public class TlsPeerCertParityTests public class TlsPeerCertParityTests
{ {

View File

@@ -6,8 +6,9 @@ using System.Net.Http.Json;
using System.Net.Sockets; using System.Net.Sockets;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using NATS.Server.Monitoring; using NATS.Server.Monitoring;
using NATS.Server.TestUtilities;
namespace NATS.Server.Tests; namespace NATS.Server.Monitoring.Tests;
public class VarzParityTests : IAsyncLifetime public class VarzParityTests : IAsyncLifetime
{ {
@@ -19,8 +20,8 @@ public class VarzParityTests : IAsyncLifetime
public VarzParityTests() public VarzParityTests()
{ {
_natsPort = GetFreePort(); _natsPort = TestPortAllocator.GetFreePort();
_monitorPort = GetFreePort(); _monitorPort = TestPortAllocator.GetFreePort();
_server = new NatsServer( _server = new NatsServer(
new NatsOptions { Port = _natsPort, MonitorPort = _monitorPort }, new NatsOptions { Port = _natsPort, MonitorPort = _monitorPort },
NullLoggerFactory.Instance); NullLoggerFactory.Instance);
@@ -128,10 +129,4 @@ public class VarzParityTests : IAsyncLifetime
varz.InBytes.ShouldBeGreaterThanOrEqualTo(5L); varz.InBytes.ShouldBeGreaterThanOrEqualTo(5L);
} }
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;
}
} }

View File

@@ -1,4 +1,4 @@
namespace NATS.Server.Tests; namespace NATS.Server.Monitoring.Tests;
public class VarzSlowConsumerBreakdownTests public class VarzSlowConsumerBreakdownTests
{ {

View File

@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NATS.Client.Core" />
<PackageReference Include="NSubstitute" />
<PackageReference Include="Shouldly" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
<Using Include="Shouldly" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\NATS.Server\NATS.Server.csproj" />
<ProjectReference Include="..\NATS.Server.TestUtilities\NATS.Server.TestUtilities.csproj" />
</ItemGroup>
</Project>

View File

@@ -3,8 +3,9 @@ using System.Net.Http.Json;
using System.Net.Sockets; using System.Net.Sockets;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using NATS.Server.Monitoring; using NATS.Server.Monitoring;
using NATS.Server.TestUtilities;
namespace NATS.Server.Tests; namespace NATS.Server.Monitoring.Tests;
public class SubszTests : IAsyncLifetime public class SubszTests : IAsyncLifetime
{ {
@@ -16,8 +17,8 @@ public class SubszTests : IAsyncLifetime
public SubszTests() public SubszTests()
{ {
_natsPort = GetFreePort(); _natsPort = TestPortAllocator.GetFreePort();
_monitorPort = GetFreePort(); _monitorPort = TestPortAllocator.GetFreePort();
_server = new NatsServer( _server = new NatsServer(
new NatsOptions { Port = _natsPort, MonitorPort = _monitorPort }, new NatsOptions { Port = _natsPort, MonitorPort = _monitorPort },
NullLoggerFactory.Instance); NullLoggerFactory.Instance);
@@ -128,10 +129,4 @@ public class SubszTests : IAsyncLifetime
subz.Total.ShouldBeGreaterThanOrEqualTo(3); subz.Total.ShouldBeGreaterThanOrEqualTo(3);
} }
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;
}
} }

View File

@@ -2,8 +2,9 @@ using System.Text.Json;
using NATS.Server; using NATS.Server;
using NATS.Server.Events; using NATS.Server.Events;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using NATS.Server.TestUtilities;
namespace NATS.Server.Tests; namespace NATS.Server.Monitoring.Tests;
public class SystemEventsTests public class SystemEventsTests
{ {
@@ -117,17 +118,8 @@ public class SystemEventsTests
private static NatsServer CreateTestServer() private static NatsServer CreateTestServer()
{ {
var port = GetFreePort(); var port = TestPortAllocator.GetFreePort();
return new NatsServer(new NatsOptions { Port = port }, NullLoggerFactory.Instance); return new NatsServer(new NatsOptions { Port = port }, NullLoggerFactory.Instance);
} }
private static int GetFreePort()
{
using var sock = new System.Net.Sockets.Socket(
System.Net.Sockets.AddressFamily.InterNetwork,
System.Net.Sockets.SocketType.Stream,
System.Net.Sockets.ProtocolType.Tcp);
sock.Bind(new System.Net.IPEndPoint(System.Net.IPAddress.Loopback, 0));
return ((System.Net.IPEndPoint)sock.LocalEndPoint!).Port;
}
} }

View File

@@ -3,8 +3,9 @@ using System.Text.Json;
using NATS.Server; using NATS.Server;
using NATS.Server.Events; using NATS.Server.Events;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using NATS.Server.TestUtilities;
namespace NATS.Server.Tests; namespace NATS.Server.Monitoring.Tests;
public class SystemRequestReplyTests public class SystemRequestReplyTests
{ {
@@ -154,17 +155,8 @@ public class SystemRequestReplyTests
private static NatsServer CreateTestServer() private static NatsServer CreateTestServer()
{ {
var port = GetFreePort(); var port = TestPortAllocator.GetFreePort();
return new NatsServer(new NatsOptions { Port = port }, NullLoggerFactory.Instance); return new NatsServer(new NatsOptions { Port = port }, NullLoggerFactory.Instance);
} }
private static int GetFreePort()
{
using var sock = new System.Net.Sockets.Socket(
System.Net.Sockets.AddressFamily.InterNetwork,
System.Net.Sockets.SocketType.Stream,
System.Net.Sockets.ProtocolType.Tcp);
sock.Bind(new System.Net.IPEndPoint(System.Net.IPAddress.Loopback, 0));
return ((System.Net.IPEndPoint)sock.LocalEndPoint!).Port;
}
} }