Skip to content
Snippets Groups Projects
Commit 07fadea2 authored by Jan Tattermusch's avatar Jan Tattermusch
Browse files

Got rid of the PortPicker utility

parent c061a2fc
No related branches found
No related tags found
No related merge requests found
......@@ -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" />
......
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();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment