Skip to content

Commit f459d3c

Browse files
committed
refactor tests and add powershell script to run tests
1 parent bac7aa4 commit f459d3c

File tree

6 files changed

+32
-22
lines changed

6 files changed

+32
-22
lines changed

.config/dotnet-tools.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": 1,
3+
"isRoot": true,
4+
"tools": {
5+
"coverlet.console": {
6+
"version": "3.2.0",
7+
"commands": [
8+
"coverlet"
9+
]
10+
}
11+
}
12+
}

Socksy.Core.Test/Fixtures.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
namespace Socksy.Core.Test;
62

7-
namespace Socksy.Core.Test
3+
internal static class Fixtures
84
{
9-
internal static class Fixtures
5+
public static IPEndPoint GetLocalendpointWithTemplatePortNumber()
106
{
11-
private static int _lastTempPort = 54678;
7+
return new IPEndPoint(IPAddress.Loopback, GetNextFreePort());
8+
}
129

13-
public static IPEndPoint GetLocalendpointWithTemplatePortNumber()
14-
{
15-
return new IPEndPoint(IPAddress.Loopback, _lastTempPort++);
16-
}
10+
private static int GetNextFreePort()
11+
{
12+
using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
13+
socket.Bind(new IPEndPoint(IPAddress.Loopback, 0));
14+
return ((IPEndPoint)socket.LocalEndPoint!).Port;
1715
}
1816
}

Socksy.Core.Test/Network/NetHelperTests.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,11 @@ namespace Socksy.Core.Test.Network;
44

55
public class NetHelperTests
66
{
7-
private static int _lastPort = 54000;
8-
private static int TempPort => _lastPort++;
9-
107
[Fact]
118
public async void CreateTcpConnectionTo_WithValidEndpoint_EstablishesTcpConnection()
129
{
1310
//Arrange
14-
var randomPort = TempPort;
15-
var localIP = IPAddress.Loopback;
16-
var endpoint = new IPEndPoint(localIP, randomPort);
11+
var endpoint = Fixtures.GetLocalendpointWithTemplatePortNumber();
1712
var listener = new TcpListener(endpoint);
1813

1914
//Act
@@ -34,9 +29,7 @@ public async void CreateTcpConnectionTo_WithValidEndpointAndEnoughTimeout_Establ
3429
{
3530
//Arrange
3631
var timeout = 2000;
37-
var randomPort = TempPort;
38-
var localIP = IPAddress.Loopback;
39-
var endpoint = new IPEndPoint(localIP, randomPort);
32+
var endpoint = Fixtures.GetLocalendpointWithTemplatePortNumber();
4033
var listener = new TcpListener(endpoint);
4134

4235
//Act

Socksy.Core/Network/TcpServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public void Start()
3333

3434
public void Stop()
3535
{
36-
_cancellationTokenSource!.Cancel();
3736
_listener.Stop();
3837
_isListening = false;
38+
_cancellationTokenSource?.Cancel();
3939
}
4040

4141
private async Task MainLoop()

global.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"sdk": {
3+
"version": "7.0.100"
4+
}
5+
}

test.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dotnet build
2+
dotnet coverlet .\Socksy.Core.Test\bin\Debug\net7.0\Socksy.Core.Test.dll --target "dotnet" --targetargs "test .\Socksy.Core.Test --no-build --nologo"

0 commit comments

Comments
 (0)