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 @@ +