From 07fadea2ff9f0ca3925e82fa84fb8e38131ce656 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Fri, 13 Feb 2015 10:03:37 -0800 Subject: [PATCH] Got rid of the PortPicker utility --- .../GrpcApiTests/MathClientServerTests.cs | 6 +-- src/csharp/GrpcCore/GrpcCore.csproj | 1 - src/csharp/GrpcCore/Utils/PortPicker.cs | 50 ------------------- src/csharp/GrpcCoreTests/ClientServerTest.cs | 6 +-- src/csharp/GrpcCoreTests/ServerTest.cs | 2 +- 5 files changed, 7 insertions(+), 58 deletions(-) delete mode 100644 src/csharp/GrpcCore/Utils/PortPicker.cs diff --git a/src/csharp/GrpcApiTests/MathClientServerTests.cs b/src/csharp/GrpcApiTests/MathClientServerTests.cs index aa78b698e85..9b51924713d 100644 --- a/src/csharp/GrpcApiTests/MathClientServerTests.cs +++ b/src/csharp/GrpcApiTests/MathClientServerTests.cs @@ -13,7 +13,7 @@ namespace math.Tests /// public class MathClientServerTest { - string serverAddr = "localhost:" + PortPicker.PickUnusedPort(); + string host = "localhost"; Server server; Channel channel; MathGrpc.IMathServiceClient client; @@ -23,9 +23,9 @@ namespace math.Tests { server = new Server(); server.AddServiceDefinition(MathGrpc.BindService(new MathServiceImpl())); - server.AddPort(serverAddr); + int port = server.AddPort(host + ":0"); server.Start(); - channel = new Channel(serverAddr); + channel = new Channel(host + ":" + port); client = MathGrpc.NewStub(channel); } diff --git a/src/csharp/GrpcCore/GrpcCore.csproj b/src/csharp/GrpcCore/GrpcCore.csproj index 95df8909173..34b9f6dfb82 100644 --- a/src/csharp/GrpcCore/GrpcCore.csproj +++ b/src/csharp/GrpcCore/GrpcCore.csproj @@ -61,7 +61,6 @@ - diff --git a/src/csharp/GrpcCore/Utils/PortPicker.cs b/src/csharp/GrpcCore/Utils/PortPicker.cs deleted file mode 100644 index 7c83bf3886d..00000000000 --- a/src/csharp/GrpcCore/Utils/PortPicker.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.Net; -using System.Net.Sockets; - -namespace Google.GRPC.Core.Utils -{ - public class PortPicker - { - 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; - } - } -} - diff --git a/src/csharp/GrpcCoreTests/ClientServerTest.cs b/src/csharp/GrpcCoreTests/ClientServerTest.cs index c700ffbe7ba..513141f5e5a 100644 --- a/src/csharp/GrpcCoreTests/ClientServerTest.cs +++ b/src/csharp/GrpcCoreTests/ClientServerTest.cs @@ -10,7 +10,7 @@ namespace Google.GRPC.Core.Tests { public class ClientServerTest { - string serverAddr = "localhost:" + PortPicker.PickUnusedPort(); + string host = "localhost"; Method unaryEchoStringMethod = new Method( MethodType.Unary, @@ -26,10 +26,10 @@ namespace Google.GRPC.Core.Tests ServerServiceDefinition.CreateBuilder("someService") .AddMethod(unaryEchoStringMethod, HandleUnaryEchoString).Build()); - server.AddPort(serverAddr); + int port = server.AddPort(host + ":0"); server.Start(); - using (Channel channel = new Channel(serverAddr)) + using (Channel channel = new Channel(host + ":" + port)) { var call = new Call(unaryEchoStringMethod, channel); diff --git a/src/csharp/GrpcCoreTests/ServerTest.cs b/src/csharp/GrpcCoreTests/ServerTest.cs index 6e13bc735f6..b8ec250ba7d 100644 --- a/src/csharp/GrpcCoreTests/ServerTest.cs +++ b/src/csharp/GrpcCoreTests/ServerTest.cs @@ -12,7 +12,7 @@ namespace Google.GRPC.Core.Tests public void StartAndShutdownServer() { Server server = new Server(); - server.AddPort("localhost:" + PortPicker.PickUnusedPort()); + int port = server.AddPort("localhost:0"); server.Start(); server.ShutdownAsync().Wait();