Skip to content

Commit 094b235

Browse files
Mohammad DehghanMohammad Dehghan
authored andcommitted
Add timeout for connecting to coverage recorder (default of 10 seconds)
1 parent c20526a commit 094b235

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

SG.CodeCoverage/Collection/RecordingController.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ public sealed class RecordingController : IDisposable
1010
public string Host { get; }
1111
public int PortNumber { get; }
1212
private TcpClient _tcpClient;
13+
public int ConnectionTimeoutSeconds { get; set; }
1314

1415
public RecordingController(int portNumber)
1516
: this("localhost", portNumber)
1617
{
1718
}
1819

19-
public RecordingController(string host, int portNumber)
20+
public RecordingController(string host, int portNumber, int connectionTimeoutSeconds = 10)
2021
{
2122
Host = host;
2223
PortNumber = portNumber;
24+
ConnectionTimeoutSeconds = connectionTimeoutSeconds;
2325
}
2426

2527
public void SaveHitsAndReset(string outputHitsFilePath)
@@ -47,7 +49,8 @@ private void ValidateResponse((bool successful, string result) response, string
4749
if (_tcpClient == null)
4850
_tcpClient = new TcpClient();
4951
if (!_tcpClient.Connected)
50-
_tcpClient.Connect(Host, PortNumber);
52+
if (!_tcpClient.ConnectAsync(Host, PortNumber).Wait(ConnectionTimeoutSeconds * 1000))
53+
throw new TimeoutException($"Cannot connect to '{Host}:{PortNumber}'. Connection timed out.");
5154

5255
string result;
5356
using (var nstream = _tcpClient.GetStream())

0 commit comments

Comments
 (0)