Added tests for max connection limitation.

Changed ConnectionError to an event instead of a delegate.
This commit is contained in:
Daniel Gosnell 2019-09-29 18:39:50 -04:00
Родитель 5fc826c84d
Коммит 9d9a781cf0
8 изменённых файлов: 64 добавлений и 10 удалений

Просмотреть файл

@ -60,7 +60,7 @@ namespace DtronixMessageQueue.Tests.Transports
var (listener, connector) = CreateClientServer(type);
ClientConfig.ConnectionTimeout = 100;
connector.ConnectionError = () =>
connector.ConnectionError += (sender, args) =>
{
TestComplete.Set();
};

Просмотреть файл

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using NUnit.Framework;
@ -171,5 +172,57 @@ namespace DtronixMessageQueue.Tests.Transports
WaitTestComplete();
}
[TestCase(Protocol.Tcp)]
[TestCase(Protocol.TcpTransparent)]
[TestCase(Protocol.TcpTls)]
public void ServerAcceptsMultipleConnections(Protocol type)
{
var (listener, connector) = CreateClientServer(type);
var connector2 = CreateClient(type);
var totalConnections = 0;
listener.Started += (sender, args) =>
{
connector.Connect();
connector2.Connect();
};
listener.Connected += (sender, args) =>
{
var currentTotal = Interlocked.Increment(ref totalConnections);
if(currentTotal == 2)
TestComplete.Set();
};
listener.Start();
WaitTestComplete();
}
[TestCase(Protocol.Tcp)]
[TestCase(Protocol.TcpTransparent)]
[TestCase(Protocol.TcpTls)]
public void ServerLimitsMultipleConnections(Protocol type)
{
ServerConfig.MaxConnections = 1;
var (listener, connector) = CreateClientServer(type);
var connector2 = CreateClient(type);
listener.Started += (sender, args) =>
{
connector.Connect();
};
connector.Connected += (sender, args) => { connector2.Connect(); };
connector2.Connected += (sender, args) =>
{
args.Session.Disconnected += (o, eventArgs) => TestComplete.Set();
};
listener.Start();
WaitTestComplete();
}
}
}

Просмотреть файл

@ -228,7 +228,7 @@ namespace DtronixMessageQueue.Tests.Transports
}
protected void WaitTestComplete(int time = 200000)
protected void WaitTestComplete(int time = 2000)
{
if (!TestComplete.Wait(time))
throw new TimeoutException($"Test timed out at {time}ms");

Просмотреть файл

@ -9,8 +9,8 @@ namespace DtronixMessageQueue.Layers.Application
protected IClientConnector Connector;
public event EventHandler<SessionEventArgs> Connected;
public event EventHandler ConnectionError;
public Action ConnectionError { get; set; }
public ISession Session { get; private set; }
@ -20,12 +20,12 @@ namespace DtronixMessageQueue.Layers.Application
{
Connector = factory.CreateConnector(OnSessionCreated);
Connector.ConnectionError = OnConnectorConnectionError;
Connector.ConnectionError += OnConnectorConnectionError;
}
private void OnConnectorConnectionError()
private void OnConnectorConnectionError(object sender, EventArgs e)
{
ConnectionError?.Invoke();
ConnectionError?.Invoke(this, EventArgs.Empty);
}
protected abstract ApplicationSession CreateSession([NotNull] ITransportSession session);

Просмотреть файл

@ -58,7 +58,7 @@ namespace DtronixMessageQueue.Layers.Application.Tls
{
await _tlsStream.AuthenticateAsClientAsync(new SslClientAuthenticationOptions
{
TargetHost = "tlstest" // TODO: Change!
TargetHost = "tlstest2" // TODO: Change!
},
_cancellationTokenSource.Token);
}

Просмотреть файл

@ -12,7 +12,7 @@ namespace DtronixMessageQueue.Layers
/// <summary>
/// Fired when the connecting client fails to connect to the server.
/// </summary>
Action ConnectionError { get; set; }
event EventHandler ConnectionError;
ISession Session { get; }

Просмотреть файл

@ -22,8 +22,8 @@ namespace DtronixMessageQueue.Layers.Transports.Tcp
}
public event EventHandler<SessionEventArgs> Connected;
public event EventHandler ConnectionError;
public Action ConnectionError { get; set; }
public ISession Session { get; private set; }
@ -103,7 +103,7 @@ namespace DtronixMessageQueue.Layers.Transports.Tcp
timedOut = true;
_connecting = false;
session?.Disconnect();
ConnectionError?.Invoke();
ConnectionError?.Invoke(this, EventArgs.Empty);
}, connectionTimeoutCancellation.Token);
}
}

Просмотреть файл

@ -160,6 +160,7 @@ namespace DtronixMessageQueue.Layers.Transports.Tcp
{
try
{
Config.Logger.Info($"Listener rejected connection due to being at max allowed sessions: {Config.MaxConnections}.");
e.AcceptSocket.Disconnect(false);
}
catch