Files
natsdotnet/tests/NATS.Server.TestUtilities/TestPortAllocator.cs
Joseph Doherty 2a75ee534a 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.
2026-03-12 14:29:14 -04:00

15 lines
388 B
C#

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;
}
}