Skip to content

Commit

Permalink
Merge pull request #82 from Eastrall/feature/tests
Browse files Browse the repository at this point in the history
Feature/tests
  • Loading branch information
Filipe GP authored Feb 21, 2018
2 parents d917192 + 4fd1ade commit 853b805
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 24 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
6 changes: 5 additions & 1 deletion src/Ether.Network/Common/NetConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,16 @@ protected virtual void Dispose(bool disposing)
{
if (this.Socket != null)
{
#if !NETSTANDARD1_3
this.Socket.Close();
#endif
this.Socket.Dispose();
this.Socket = null;
}

this._disposedValue = true;
}

this._disposedValue = true;
}
}
}
62 changes: 39 additions & 23 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 @@ -89,8 +84,8 @@ public void Start()

this.Initialize();
this.Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
this.Socket.Bind(new IPEndPoint(address, this.Configuration.Port));
this.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
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 All @@ -105,22 +100,21 @@ public void Stop()
if (!this.IsRunning)
return;

this.IsRunning = false;

this.ClearClients();
this._readPool.Clear();
this._writePool.Clear();

if (this.Configuration.Blocking)
this._manualResetEvent.Set();
this.ClearResources();

if (this.Socket != null)
{
#if !NETSTANDARD1_3
this.Socket.Close();
#endif
this.Socket.Dispose();
this.Socket = null;
}

this._messageQueue.Clear();
if (this.Configuration.Blocking)
this._manualResetEvent.Set();

this.IsRunning = false;
}

/// <inheritdoc />
Expand Down Expand Up @@ -346,14 +340,36 @@ private void HandleIncomingMessages(T user, byte[] messageData)
}

/// <summary>
/// Clear client's list.
/// Clear NetServer's resources.
/// </summary>
private void ClearClients()
private void ClearResources()
{
foreach (T client in this.Clients)
client.Dispose();

this._clients.Clear();
this._readPool.Clear();
this._writePool.Clear();
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>
Expand Down Expand Up @@ -393,11 +409,11 @@ protected override void Dispose(bool disposing)
if (disposing)
{
this._sendQueueTaskCancelTokenSource.Cancel(false);
this._readPool?.Dispose();
this._writePool?.Dispose();
this.ClearClients();
this._messageQueue.Clear();
this.ClearResources();
this._readPool.Dispose();
this._writePool.Dispose();
this._messageQueue.Dispose();

this._isDisposed = true;
}
}
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;
}
}
}
40 changes: 40 additions & 0 deletions test/Ether.Network.Tests/Contexts/NetConfig/ConfigServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using Ether.Network.Common;
using Ether.Network.Server;

namespace Ether.Network.Tests.Contexts.NetConfig
{
public class ConfigServer : NetServer<Client>
{
protected override void Initialize()
{
}

protected override void OnClientConnected(Client connection)
{
}

protected override void OnClientDisconnected(Client connection)
{
}

protected override void OnError(Exception exception)
{
}

public void SetupConfiguration()
{
this.Configuration.BufferSize = 512;
this.Configuration.MaximumNumberOfConnections = 10;
this.Configuration.Host = "127.0.0.1";
this.Configuration.Port = 4445;
this.Configuration.Backlog = 10;
this.Configuration.Blocking = false;
}
}

public class Client : NetUser
{

}
}
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();
}
}
}
}
}
60 changes: 60 additions & 0 deletions test/Ether.Network.Tests/NetServerConfigurationTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using Ether.Network.Exceptions;
using Ether.Network.Tests.Contexts.NetConfig;
using System;
using Xunit;

namespace Ether.Network.Tests
{
public class NetServerConfigurationTest
{
[Fact]
public void StartServerWithoutConfiguration()
{
using (var server = new ConfigServer())
{
Exception ex = Assert.Throws<EtherConfigurationException>(() => server.Start());

Assert.IsType<EtherConfigurationException>(ex);
}
}

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

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

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

Assert.IsType<EtherConfigurationException>(ex);
}
}

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

server.SetupConfiguration();
server.Start();
server.Stop();
}
}
}
}

0 comments on commit 853b805

Please sign in to comment.