aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/csharp/GrpcCoreTests/Utils.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/csharp/GrpcCoreTests/Utils.cs')
-rw-r--r--src/csharp/GrpcCoreTests/Utils.cs51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/csharp/GrpcCoreTests/Utils.cs b/src/csharp/GrpcCoreTests/Utils.cs
new file mode 100644
index 0000000000..b0c0a7b620
--- /dev/null
+++ b/src/csharp/GrpcCoreTests/Utils.cs
@@ -0,0 +1,51 @@
+using System;
+using System.Net;
+using System.Net.Sockets;
+
+namespace Google.GRPC.Core.Tests
+{
+ /// <summary>
+ /// Testing utils.
+ /// </summary>
+ public class Utils
+ {
+ static Random random = new Random();
+ // TODO: cleanup this code a bit
+ public static int PickUnusedPort()
+ {
+ int port;
+ do
+ {
+ port = random.Next(2000, 50000);
+
+ } while(!IsPortAvailable(port));
+ return port;
+ }
+ // TODO: cleanup this code a bit
+ public static bool IsPortAvailable(int port)
+ {
+ bool available = true;
+
+ TcpListener server = null;
+ try
+ {
+ IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
+ server = new TcpListener(ipAddress, port);
+ server.Start();
+ }
+ catch (Exception ex)
+ {
+ available = false;
+ }
+ finally
+ {
+ if (server != null)
+ {
+ server.Stop();
+ }
+ }
+ return available;
+ }
+ }
+}
+