Skip to content

Commit

Permalink
Merge pull request #674 from gbirchmeier/668
Browse files Browse the repository at this point in the history
log a message if error occurs when starting TCP listener
  • Loading branch information
gbirchmeier authored Jan 25, 2021
2 parents 0ee3efb + 83a7477 commit e56e691
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
12 changes: 11 additions & 1 deletion QuickFIXn/ThreadedSocketReactor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,17 @@ public void Run()
lock (sync_)
{
if (State.SHUTDOWN_REQUESTED != state_)
tcpListener_.Start();
{
try
{
tcpListener_.Start();
}
catch(Exception e)
{
this.Log("Error starting listener: " + e.Message);
throw;
}
}
}

while (State.RUNNING == ReactorState)
Expand Down
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ What's New
* (patch) #663/#657 - fix call to Directory.GetFiles for case-sensitive filesystems (johna1203/jatrofka)
* (patch) #626 - Dynamic SocketAcceptPort bugfix (akamyshanov)
* (patch) #662 - fix NullRefEx in AbstractInitiator.IsLoggedOn (gbirchmeier, thanks to akpwhg)
* (patch) #542 - log before crash when port is not available (rodrigopscampos)

### v1.10.0:
* (patch) #505 - Fix ObjectDisposedException when SocketInitiator is stopped before connection attempt fails (musashibg)
Expand Down
79 changes: 79 additions & 0 deletions UnitTests/ThreadedSocketReactorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using NUnit.Framework;
using QuickFix;
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace UnitTests
{
[TestFixture]
public class ThreadedSocketReactorTests
{
static readonly Random _random = new Random();
static TcpListener _tcpListener;

private int OccupyAPort()
{
int randomPort;
for (int i = 0; i < 10; i++)
{
try
{
randomPort = _random.Next(5000, 6000);
_tcpListener = new TcpListener(IPAddress.Loopback, randomPort);
_tcpListener.Start();

return randomPort;
}
catch { }
}

throw new Exception("Could not occupy a port in 10 attempts");
}

static StringWriter GetStdOut()
{
var stringWriter = new StringWriter();
Console.SetOut(stringWriter);
return stringWriter;
}

[Test]
public void TestStartOnBusyPort()
{
var port = OccupyAPort();

var settings = new SocketSettings();
var testingObject = new ThreadedSocketReactor(new IPEndPoint(IPAddress.Loopback, port), settings, sessionDict: null);

var stdOut = GetStdOut();

Exception exceptionResult = null;
string stdOutResult = null;

try
{
testingObject.Run();
}
catch (Exception ex)
{
exceptionResult = ex;
stdOutResult = stdOut.ToString();
}

Assert.IsNotNull(exceptionResult);
Assert.IsNotNull(stdOutResult);

Assert.AreEqual(typeof(SocketException), exceptionResult.GetType());
Assert.IsTrue(stdOutResult.StartsWith("Error starting listener: "));
}

[TearDown]
public void TearDown()
{
if (_tcpListener != null)
_tcpListener.Stop();
}
}
}

0 comments on commit e56e691

Please sign in to comment.