-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathThreadedSocketReactorTests.cs
66 lines (56 loc) · 1.84 KB
/
ThreadedSocketReactorTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using NUnit.Framework;
using QuickFix;
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using QuickFix.Logger;
namespace UnitTests
{
[TestFixture]
public class ThreadedSocketReactorTests
{
static TcpListener? _tcpListener;
private int OccupyAPort()
{
Random random = new();
for (int i = 0; i < 10; i++)
{
try
{
int 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,
acceptorSocketDescriptor: null,
new NonSessionLog(new ScreenLogFactory(true, true, true)));
var stdOut = GetStdOut();
var ex = Assert.Throws<SocketException>(delegate { testingObject.Start(); })!;
Assert.That(stdOut.ToString(), Does.StartWith("<event> Error starting listener:"));
Assert.That(ex.SocketErrorCode, Is.EqualTo(SocketError.AddressAlreadyInUse));
}
[TearDown]
public void TearDown() {
_tcpListener?.Stop();
}
}
}