Add a helper class to assign a random port for a test.
authorJo Shields <jo.shields@xamarin.com>
Mon, 23 Mar 2015 10:41:35 +0000 (10:41 +0000)
committerJo Shields <jo.shields@xamarin.com>
Mon, 23 Mar 2015 14:40:44 +0000 (14:40 +0000)
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.

mcs/class/test-helpers/NetworkHelpers.cs [new file with mode: 0644]

diff --git a/mcs/class/test-helpers/NetworkHelpers.cs b/mcs/class/test-helpers/NetworkHelpers.cs
new file mode 100644 (file)
index 0000000..02709af
--- /dev/null
@@ -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;
+               }
+       }
+}