Path: redb-route/tests/Shared/TestPorts.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/Shared/TestPorts.cs ---- using System.Net; using System.Net.Sockets; namespace redb.Route.Tests.Shared; /// /// Ports for test servers. The old recipe — bind port 0, read the number, close, let Kestrel bind it later — /// took the number from the ephemeral range (Windows 49152–65535, Linux 32768–60999), the same range the OS /// hands out for the local end of every outgoing client connection. Between the close and Kestrel's bind the /// port could go to a client socket of a test running in parallel (three TFMs and several projects run at /// once), and the server failed with "address already in use". /// /// Here the ports come from 20000–32767, below both ephemeral ranges, so the OS never gives them to a client /// socket. Each process starts at its own random point and walks forward, so parallel processes rarely meet, /// and every port is checked by binding it before it is handed out (a port reserved by Hyper-V or held by a /// container fails that check and is skipped). A number is never handed out twice in one process. /// /// internal static class TestPorts { private const int First = 20000; private const int Last = 32767; private const int Span = Last - First + 1; private static readonly int Start = Random.Shared.Next(Span); private static int _taken = -1; /// A port nothing listens on at the moment, outside the ephemeral range. public static int Next() { for (var attempt = 0; attempt < Span; attempt++) { var port = First + (Start + Interlocked.Increment(ref _taken)) % Span; if (IsFree(port)) return port; } throw new InvalidOperationException($"No free port left in {First}-{Last} for this test process."); } private static bool IsFree(int port) { using var probe = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Windows otherwise lets a bind share a port someone holds on another address of the machine. if (OperatingSystem.IsWindows()) probe.ExclusiveAddressUse = true; try { // The loopback: tests listen there, and a probe on all interfaces is what makes a desktop firewall ask. // A port someone holds on 0.0.0.0 (an interop container's published port) still fails this bind. probe.Bind(new IPEndPoint(IPAddress.Loopback, port)); return true; } catch (SocketException) { // In use, or excluded by the OS: this port is not the answer, the next one is tried. return false; } } }