From 9d9a781cf01385245708f09e3adbc2db1e2de65d Mon Sep 17 00:00:00 2001 From: Daniel Gosnell Date: Sun, 29 Sep 2019 18:39:50 -0400 Subject: [PATCH] Added tests for max connection limitation. Changed ConnectionError to an event instead of a delegate. --- .../TransportClientConnectorTests.cs | 2 +- .../Transports/TransportListenerTests.cs | 53 +++++++++++++++++++ .../Transports/TransportTestBase.cs | 2 +- .../Application/ApplicationClientConnector.cs | 8 +-- .../Application/Tls/TlsApplicationSession.cs | 2 +- .../Layers/IClientConnector.cs | 2 +- .../Tcp/TcpTransportClientConnector.cs | 4 +- .../Transports/Tcp/TcpTransportListener.cs | 1 + 8 files changed, 64 insertions(+), 10 deletions(-) diff --git a/src/DtronixMessageQueue.Tests/Transports/TransportClientConnectorTests.cs b/src/DtronixMessageQueue.Tests/Transports/TransportClientConnectorTests.cs index 5ddc583..a399b29 100644 --- a/src/DtronixMessageQueue.Tests/Transports/TransportClientConnectorTests.cs +++ b/src/DtronixMessageQueue.Tests/Transports/TransportClientConnectorTests.cs @@ -60,7 +60,7 @@ namespace DtronixMessageQueue.Tests.Transports var (listener, connector) = CreateClientServer(type); ClientConfig.ConnectionTimeout = 100; - connector.ConnectionError = () => + connector.ConnectionError += (sender, args) => { TestComplete.Set(); }; diff --git a/src/DtronixMessageQueue.Tests/Transports/TransportListenerTests.cs b/src/DtronixMessageQueue.Tests/Transports/TransportListenerTests.cs index 74d81ff..aaf0ced 100644 --- a/src/DtronixMessageQueue.Tests/Transports/TransportListenerTests.cs +++ b/src/DtronixMessageQueue.Tests/Transports/TransportListenerTests.cs @@ -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(); + } } } diff --git a/src/DtronixMessageQueue.Tests/Transports/TransportTestBase.cs b/src/DtronixMessageQueue.Tests/Transports/TransportTestBase.cs index 305e42e..de1cab3 100644 --- a/src/DtronixMessageQueue.Tests/Transports/TransportTestBase.cs +++ b/src/DtronixMessageQueue.Tests/Transports/TransportTestBase.cs @@ -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"); diff --git a/src/DtronixMessageQueue/Layers/Application/ApplicationClientConnector.cs b/src/DtronixMessageQueue/Layers/Application/ApplicationClientConnector.cs index 88d447e..0feb8c4 100644 --- a/src/DtronixMessageQueue/Layers/Application/ApplicationClientConnector.cs +++ b/src/DtronixMessageQueue/Layers/Application/ApplicationClientConnector.cs @@ -9,8 +9,8 @@ namespace DtronixMessageQueue.Layers.Application protected IClientConnector Connector; public event EventHandler 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); diff --git a/src/DtronixMessageQueue/Layers/Application/Tls/TlsApplicationSession.cs b/src/DtronixMessageQueue/Layers/Application/Tls/TlsApplicationSession.cs index 453f856..e2cd6b7 100644 --- a/src/DtronixMessageQueue/Layers/Application/Tls/TlsApplicationSession.cs +++ b/src/DtronixMessageQueue/Layers/Application/Tls/TlsApplicationSession.cs @@ -58,7 +58,7 @@ namespace DtronixMessageQueue.Layers.Application.Tls { await _tlsStream.AuthenticateAsClientAsync(new SslClientAuthenticationOptions { - TargetHost = "tlstest" // TODO: Change! + TargetHost = "tlstest2" // TODO: Change! }, _cancellationTokenSource.Token); } diff --git a/src/DtronixMessageQueue/Layers/IClientConnector.cs b/src/DtronixMessageQueue/Layers/IClientConnector.cs index 143e0b6..346bfae 100644 --- a/src/DtronixMessageQueue/Layers/IClientConnector.cs +++ b/src/DtronixMessageQueue/Layers/IClientConnector.cs @@ -12,7 +12,7 @@ namespace DtronixMessageQueue.Layers /// /// Fired when the connecting client fails to connect to the server. /// - Action ConnectionError { get; set; } + event EventHandler ConnectionError; ISession Session { get; } diff --git a/src/DtronixMessageQueue/Layers/Transports/Tcp/TcpTransportClientConnector.cs b/src/DtronixMessageQueue/Layers/Transports/Tcp/TcpTransportClientConnector.cs index 016f917..1049b4a 100644 --- a/src/DtronixMessageQueue/Layers/Transports/Tcp/TcpTransportClientConnector.cs +++ b/src/DtronixMessageQueue/Layers/Transports/Tcp/TcpTransportClientConnector.cs @@ -22,8 +22,8 @@ namespace DtronixMessageQueue.Layers.Transports.Tcp } public event EventHandler 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); } } diff --git a/src/DtronixMessageQueue/Layers/Transports/Tcp/TcpTransportListener.cs b/src/DtronixMessageQueue/Layers/Transports/Tcp/TcpTransportListener.cs index 6991729..4bfdaaa 100644 --- a/src/DtronixMessageQueue/Layers/Transports/Tcp/TcpTransportListener.cs +++ b/src/DtronixMessageQueue/Layers/Transports/Tcp/TcpTransportListener.cs @@ -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