Got rid of the PortPicker utility

pull/516/head
Jan Tattermusch 10 years ago
parent c061a2fcef
commit 07fadea2ff
  1. 6
      src/csharp/GrpcApiTests/MathClientServerTests.cs
  2. 1
      src/csharp/GrpcCore/GrpcCore.csproj
  3. 50
      src/csharp/GrpcCore/Utils/PortPicker.cs
  4. 6
      src/csharp/GrpcCoreTests/ClientServerTest.cs
  5. 2
      src/csharp/GrpcCoreTests/ServerTest.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);
}

@ -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" />

@ -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;
}
}
}

@ -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);

@ -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();

Loading…
Cancel
Save