Skip to content

Commit

Permalink
Add tests for NetClientConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
Filipe GOMES PEIXOTO committed Feb 21, 2018
1 parent 7546d1c commit 4fd1ade
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 9 deletions.
18 changes: 18 additions & 0 deletions src/Ether.Network/Client/NetClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using Ether.Network.Exceptions;

namespace Ether.Network.Client
{
Expand Down Expand Up @@ -73,6 +74,8 @@ public void Connect()
if (this.IsConnected)
throw new InvalidOperationException("Client is already connected to remote.");

this.CheckConfiguration();

this.Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this._socketSendArgs = NetUtils.CreateSocketAsync(this.Socket, this.IO_Completed);
this._socketReceiveArgs = NetUtils.CreateSocketAsync(this, this.IO_Completed, this.Configuration.BufferSize);
Expand Down Expand Up @@ -238,6 +241,21 @@ private void ProcessReceive(SocketAsyncEventArgs e)
}
}

/// <summary>
/// Checks the configuration.
/// </summary>
private void CheckConfiguration()
{
if (this.Configuration.Port <= 0)
throw new EtherConfigurationException($"{this.Configuration.Port} is not a valid port.");

if (this.Configuration.Address == null)
throw new EtherConfigurationException($"Invalid host : {this.Configuration.Host}.");

if (this.Configuration.BufferSize <= 0)
throw new EtherConfigurationException("BufferSize cannot less or equal to 0.");
}

/// <summary>
/// Triggered when a <see cref="SocketAsyncEventArgs"/> async operation is completed.
/// </summary>
Expand Down
28 changes: 21 additions & 7 deletions src/Ether.Network/Server/NetServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,7 @@ public void Start()
if (this.IsRunning)
throw new InvalidOperationException("Server is already running.");

if (this.Configuration.Port <= 0)
throw new EtherConfigurationException($"{this.Configuration.Port} is not a valid port.");

IPAddress address = this.Configuration.Host == AllInterfaces ? IPAddress.Any : this.Configuration.Address;
if (address == null)
throw new EtherConfigurationException($"Invalid host : {this.Configuration.Host}");
this.CheckConfiguration();

for (var i = 0; i < this.Configuration.MaximumNumberOfConnections; i++)
{
Expand All @@ -90,7 +85,7 @@ public void Start()
this.Initialize();
this.Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
this.Socket.Bind(new IPEndPoint(address, this.Configuration.Port));
this.Socket.Bind(NetUtils.CreateIpEndPoint(this.Configuration.Host, this.Configuration.Port));
this.Socket.Listen(this.Configuration.Backlog);
this.IsRunning = true;
this.StartAccept(NetUtils.CreateSocketAsync(null, this.IO_Completed));
Expand Down Expand Up @@ -358,6 +353,25 @@ private void ClearResources()
this._messageQueue.Clear();
}

/// <summary>
/// Checks the configuration.
/// </summary>
private void CheckConfiguration()
{
if (this.Configuration.Port <= 0)
throw new EtherConfigurationException($"{this.Configuration.Port} is not a valid port.");

IPAddress address = this.Configuration.Host == AllInterfaces ? IPAddress.Any : this.Configuration.Address;
if (address == null)
throw new EtherConfigurationException($"Invalid host : {this.Configuration.Host}.");

if (this.Configuration.BufferSize <= 0)
throw new EtherConfigurationException("BufferSize cannot less or equal to 0.");

if (this.Configuration.Backlog <= 0)
throw new EtherConfigurationException("Backlog cannot be less or equal to 0.");
}

/// <summary>
/// Triggered when a <see cref="SocketAsyncEventArgs"/> async operation is completed.
/// </summary>
Expand Down
27 changes: 27 additions & 0 deletions test/Ether.Network.Tests/Contexts/NetConfig/ConfigClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Net.Sockets;
using Ether.Network.Client;

namespace Ether.Network.Tests.Contexts.NetConfig
{
public class ConfigClient : NetClient
{
protected override void OnConnected()
{
}

protected override void OnDisconnected()
{
}

protected override void OnSocketError(SocketError socketError)
{
}

public void SetupConfiguration()
{
this.Configuration.Host = "127.0.0.1";
this.Configuration.Port = 4445;
this.Configuration.BufferSize = 512;
}
}
}
2 changes: 0 additions & 2 deletions test/Ether.Network.Tests/Contexts/NetConfig/ConfigServer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using Ether.Network.Common;
using Ether.Network.Server;

Expand Down
66 changes: 66 additions & 0 deletions test/Ether.Network.Tests/NetClientConfigurationTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using Ether.Network.Exceptions;
using Ether.Network.Tests.Contexts.NetConfig;
using Xunit;

namespace Ether.Network.Tests
{
public class NetClientConfigurationTest
{
[Fact]
public void StartClientWihtoutConfiguration()
{
using (var server = new ConfigServer())
{
server.SetupConfiguration();
server.Start();

using (var client = new ConfigClient())
{
Exception ex = Assert.Throws<EtherConfigurationException>(() => client.Connect());
Assert.IsType<EtherConfigurationException>(ex);

client.Disconnect();
}
}
}

[Fact]
public void StartClientWithConfiguration()
{
using (var server = new ConfigServer())
{
server.SetupConfiguration();
server.Start();

using (var client = new ConfigClient())
{
client.SetupConfiguration();
client.Connect();
client.Disconnect();
}
}
}

[Fact]
public void SetupClientConfigurationAfterConnected()
{
using (var server = new ConfigServer())
{
server.SetupConfiguration();
server.Start();

using (var client = new ConfigClient())
{
client.SetupConfiguration();
client.Connect();

Exception ex = Assert.Throws<EtherConfigurationException>(() => client.SetupConfiguration());
Assert.IsType<EtherConfigurationException>(ex);

client.Disconnect();
}
}
}
}
}

0 comments on commit 4fd1ade

Please sign in to comment.