diff options
author | Jan Tattermusch <jtattermusch@google.com> | 2015-02-13 10:03:37 -0800 |
---|---|---|
committer | Jan Tattermusch <jtattermusch@google.com> | 2015-02-13 10:03:37 -0800 |
commit | 07fadea2ff9f0ca3925e82fa84fb8e38131ce656 (patch) | |
tree | b93e362eb780ae66e2b1e40ec57527ce22a36626 /src | |
parent | c061a2fcef04572fadeb106a4dd0e0f05ac567f1 (diff) |
Got rid of the PortPicker utility
Diffstat (limited to 'src')
-rw-r--r-- | src/csharp/GrpcApiTests/MathClientServerTests.cs | 6 | ||||
-rw-r--r-- | src/csharp/GrpcCore/GrpcCore.csproj | 1 | ||||
-rw-r--r-- | src/csharp/GrpcCore/Utils/PortPicker.cs | 50 | ||||
-rw-r--r-- | src/csharp/GrpcCoreTests/ClientServerTest.cs | 6 | ||||
-rw-r--r-- | src/csharp/GrpcCoreTests/ServerTest.cs | 2 |
5 files changed, 7 insertions, 58 deletions
diff --git a/src/csharp/GrpcApiTests/MathClientServerTests.cs b/src/csharp/GrpcApiTests/MathClientServerTests.cs index aa78b698e8..9b51924713 100644 --- a/src/csharp/GrpcApiTests/MathClientServerTests.cs +++ b/src/csharp/GrpcApiTests/MathClientServerTests.cs @@ -13,7 +13,7 @@ namespace math.Tests /// </summary> 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 95df890917..34b9f6dfb8 100644 --- a/src/csharp/GrpcCore/GrpcCore.csproj +++ b/src/csharp/GrpcCore/GrpcCore.csproj @@ -61,7 +61,6 @@ <Compile Include="Marshaller.cs" /> <Compile Include="ServerServiceDefinition.cs" /> <Compile Include="Utils\RecordingObserver.cs" /> - <Compile Include="Utils\PortPicker.cs" /> <Compile Include="Utils\RecordingQueue.cs" /> </ItemGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> diff --git a/src/csharp/GrpcCore/Utils/PortPicker.cs b/src/csharp/GrpcCore/Utils/PortPicker.cs deleted file mode 100644 index 7c83bf3886..0000000000 --- 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 c700ffbe7b..513141f5e5 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<string, string> unaryEchoStringMethod = new Method<string, string>( 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<string, string>(unaryEchoStringMethod, channel); diff --git a/src/csharp/GrpcCoreTests/ServerTest.cs b/src/csharp/GrpcCoreTests/ServerTest.cs index 6e13bc735f..b8ec250ba7 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(); |