From 2a75ee534aa6244742105e26c5ce0c1908d7740d Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Thu, 12 Mar 2026 14:29:14 -0400 Subject: [PATCH] feat: create NATS.Server.TestUtilities with shared helpers Add shared test utility library with TestPortAllocator.GetFreePort() and SocketTestHelper.ReadUntilAsync() to deduplicate helpers across test projects. This is the foundation for splitting the monolithic test project into feature-focused test projects. --- NatsDotNet.slnx | 1 + .../NATS.Server.TestUtilities.csproj | 20 ++++++++++++++++++ .../SocketTestHelper.cs | 21 +++++++++++++++++++ .../TestPortAllocator.cs | 14 +++++++++++++ .../NATS.Server.Tests.csproj | 1 + 5 files changed, 57 insertions(+) create mode 100644 tests/NATS.Server.TestUtilities/NATS.Server.TestUtilities.csproj create mode 100644 tests/NATS.Server.TestUtilities/SocketTestHelper.cs create mode 100644 tests/NATS.Server.TestUtilities/TestPortAllocator.cs diff --git a/NatsDotNet.slnx b/NatsDotNet.slnx index 52b8dc1..5dc31f2 100644 --- a/NatsDotNet.slnx +++ b/NatsDotNet.slnx @@ -4,6 +4,7 @@ + diff --git a/tests/NATS.Server.TestUtilities/NATS.Server.TestUtilities.csproj b/tests/NATS.Server.TestUtilities/NATS.Server.TestUtilities.csproj new file mode 100644 index 0000000..f3bea37 --- /dev/null +++ b/tests/NATS.Server.TestUtilities/NATS.Server.TestUtilities.csproj @@ -0,0 +1,20 @@ + + + false + + + + + + + + + + + + + + + + + diff --git a/tests/NATS.Server.TestUtilities/SocketTestHelper.cs b/tests/NATS.Server.TestUtilities/SocketTestHelper.cs new file mode 100644 index 0000000..fbb3ee6 --- /dev/null +++ b/tests/NATS.Server.TestUtilities/SocketTestHelper.cs @@ -0,0 +1,21 @@ +using System.Net.Sockets; +using System.Text; + +namespace NATS.Server.TestUtilities; + +public static class SocketTestHelper +{ + public static async Task 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, StringComparison.Ordinal)) + { + 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(); + } +} diff --git a/tests/NATS.Server.TestUtilities/TestPortAllocator.cs b/tests/NATS.Server.TestUtilities/TestPortAllocator.cs new file mode 100644 index 0000000..09f7d14 --- /dev/null +++ b/tests/NATS.Server.TestUtilities/TestPortAllocator.cs @@ -0,0 +1,14 @@ +using System.Net; +using System.Net.Sockets; + +namespace NATS.Server.TestUtilities; + +public static class TestPortAllocator +{ + public 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; + } +} diff --git a/tests/NATS.Server.Tests/NATS.Server.Tests.csproj b/tests/NATS.Server.Tests/NATS.Server.Tests.csproj index 813b551..c5c8a2b 100644 --- a/tests/NATS.Server.Tests/NATS.Server.Tests.csproj +++ b/tests/NATS.Server.Tests/NATS.Server.Tests.csproj @@ -28,6 +28,7 @@ +