From: Jo Shields Date: Mon, 23 Mar 2015 10:41:35 +0000 (+0000) Subject: Add a helper class to assign a random port for a test. X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=commitdiff_plain;h=02981d6bc070d05f21c2d1a17573b3c23202a770;p=mono.git Add a helper class to assign a random port for a test. This helps in cases where two copies of the test suite are running simultaneously, and both want to use the same hard-coded ports in tests. --- diff --git a/mcs/class/test-helpers/NetworkHelpers.cs b/mcs/class/test-helpers/NetworkHelpers.cs new file mode 100644 index 00000000000..02709af2ba7 --- /dev/null +++ b/mcs/class/test-helpers/NetworkHelpers.cs @@ -0,0 +1,18 @@ +using System; +using System.Net; +using System.Net.Sockets; + +namespace MonoTests.Helpers { + + public static class NetworkHelpers + { + public static int FindFreePort () + { + TcpListener l = new TcpListener(IPAddress.Loopback, 0); + l.Start(); + int port = ((IPEndPoint)l.LocalEndpoint).Port; + l.Stop(); + return port; + } + } +}