Improved robustness of TCP connection handling + test

This commit is contained in:
Christian Köllner 2020-02-22 11:22:13 +01:00
parent 686dfeba52
commit 2ad89756e1
3 changed files with 54 additions and 5 deletions

View File

@ -36,7 +36,7 @@ namespace Capnp.Net.Runtime.Tests
Process _currentProcess;
void LaunchCompatTestProcess(string whichTest, Action<StreamReader> test)
bool TryLaunchCompatTestProcess(string whichTest, Action<StreamReader> test)
{
string myPath = Path.GetDirectoryName(typeof(TcpRpcInterop).Assembly.Location);
string config;
@ -71,6 +71,12 @@ namespace Capnp.Net.Runtime.Tests
"Problem after launching test process");
test(_currentProcess.StandardOutput);
return true;
}
catch (AssertFailedException)
{
return false;
}
finally
{
@ -85,6 +91,20 @@ namespace Capnp.Net.Runtime.Tests
}
}
void LaunchCompatTestProcess(string whichTest, Action<StreamReader> test)
{
for (int retry = 0; retry < 5; retry++)
{
if (TryLaunchCompatTestProcess(whichTest, test))
return;
if (whichTest.StartsWith("server:"))
PrepareNextTest();
}
Assert.Fail("Problem after launching test process");
}
void SendInput(string line)
{
_currentProcess.StandardInput.WriteLine(line);

View File

@ -5,6 +5,7 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
@ -21,7 +22,29 @@ namespace Capnp.Net.Runtime.Tests
protected ILogger Logger { get; set; }
protected TcpRpcClient SetupClient() => new TcpRpcClient("localhost", TcpPort);
protected TcpRpcServer SetupServer() => new TcpRpcServer(IPAddress.Any, TcpPort);
protected TcpRpcServer SetupServer()
{
int attempt = 0;
while (true)
{
try
{
return new TcpRpcServer(IPAddress.Any, TcpPort);
}
catch (SocketException)
{
// If the TCP listening port is occupied by some other process,
// retry with a different one
if (attempt == 5)
throw;
}
IncrementTcpPort();
++attempt;
}
}
protected (TcpRpcServer, TcpRpcClient) SetupClientServerPair()
{

View File

@ -284,7 +284,9 @@ namespace Capnp.Rpc
_listener = new TcpListener(localAddr, port);
_listener.ExclusiveAddressUse = false;
for (int retry = 0; retry < 5; retry++)
int attempt = 0;
while (true)
{
try
{
@ -293,9 +295,13 @@ namespace Capnp.Rpc
}
catch (SocketException socketException)
{
Logger.LogWarning($"Failed to listen on port {port}, attempt {retry}: {socketException}");
Thread.Sleep(10);
if (attempt == 5)
throw;
Logger.LogWarning($"Failed to listen on port {port}, attempt {attempt}: {socketException}");
}
++attempt;
}
_acceptorThread = new Thread(AcceptClients);