mirror of
https://github.com/FabInfra/capnproto-dotnetcore_Runtime.git
synced 2025-03-12 23:01:44 +01:00
Improved robustness of TCP connection handling + test
This commit is contained in:
parent
686dfeba52
commit
2ad89756e1
@ -36,7 +36,7 @@ namespace Capnp.Net.Runtime.Tests
|
|||||||
|
|
||||||
Process _currentProcess;
|
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 myPath = Path.GetDirectoryName(typeof(TcpRpcInterop).Assembly.Location);
|
||||||
string config;
|
string config;
|
||||||
@ -71,6 +71,12 @@ namespace Capnp.Net.Runtime.Tests
|
|||||||
"Problem after launching test process");
|
"Problem after launching test process");
|
||||||
|
|
||||||
test(_currentProcess.StandardOutput);
|
test(_currentProcess.StandardOutput);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (AssertFailedException)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
finally
|
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)
|
void SendInput(string line)
|
||||||
{
|
{
|
||||||
_currentProcess.StandardInput.WriteLine(line);
|
_currentProcess.StandardInput.WriteLine(line);
|
||||||
|
@ -5,6 +5,7 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -21,7 +22,29 @@ namespace Capnp.Net.Runtime.Tests
|
|||||||
protected ILogger Logger { get; set; }
|
protected ILogger Logger { get; set; }
|
||||||
|
|
||||||
protected TcpRpcClient SetupClient() => new TcpRpcClient("localhost", TcpPort);
|
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()
|
protected (TcpRpcServer, TcpRpcClient) SetupClientServerPair()
|
||||||
{
|
{
|
||||||
|
@ -284,7 +284,9 @@ namespace Capnp.Rpc
|
|||||||
_listener = new TcpListener(localAddr, port);
|
_listener = new TcpListener(localAddr, port);
|
||||||
_listener.ExclusiveAddressUse = false;
|
_listener.ExclusiveAddressUse = false;
|
||||||
|
|
||||||
for (int retry = 0; retry < 5; retry++)
|
int attempt = 0;
|
||||||
|
|
||||||
|
while (true)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -293,9 +295,13 @@ namespace Capnp.Rpc
|
|||||||
}
|
}
|
||||||
catch (SocketException socketException)
|
catch (SocketException socketException)
|
||||||
{
|
{
|
||||||
Logger.LogWarning($"Failed to listen on port {port}, attempt {retry}: {socketException}");
|
if (attempt == 5)
|
||||||
Thread.Sleep(10);
|
throw;
|
||||||
|
|
||||||
|
Logger.LogWarning($"Failed to listen on port {port}, attempt {attempt}: {socketException}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
++attempt;
|
||||||
}
|
}
|
||||||
|
|
||||||
_acceptorThread = new Thread(AcceptClients);
|
_acceptorThread = new Thread(AcceptClients);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user