Skip to content

Commit

Permalink
Add NetServerConfiguration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Filipe GOMES PEIXOTO committed Feb 21, 2018
1 parent d917192 commit 2e920e6
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Ether.Network/Server/NetServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ public void Start()
this._writePool.Push(NetUtils.CreateSocketAsync(null, this.IO_Completed, this.Configuration.BufferSize));
}

this.Initialize();
this.Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
this.Initialize();
this.Socket.Bind(new IPEndPoint(address, this.Configuration.Port));
this.Socket.Listen(this.Configuration.Backlog);
this.IsRunning = true;
Expand Down Expand Up @@ -398,6 +398,13 @@ protected override void Dispose(bool disposing)
this.ClearClients();
this._messageQueue.Clear();
this._messageQueue.Dispose();

if (this.Socket != null)
{
this.Socket.Dispose();
this.Socket = null;
}

this._isDisposed = true;
}
}
Expand Down
42 changes: 42 additions & 0 deletions test/Ether.Network.Tests/Contexts/NetConfig/ConfigServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Text;
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 = 4444;
this.Configuration.Backlog = 10;
this.Configuration.Blocking = false;
}
}

public class Client : NetUser
{

}
}
65 changes: 65 additions & 0 deletions test/Ether.Network.Tests/NetServerConfigurationTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
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);

server.Stop();
}
}

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

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

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

Assert.IsType<EtherConfigurationException>(ex);

server.Stop();
}
}

[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 2e920e6

Please sign in to comment.