From 73d33e2200b69a6867d6a83db9aed69ca385ecc9 Mon Sep 17 00:00:00 2001 From: JanEggers Date: Sun, 7 Oct 2018 14:40:57 +0200 Subject: [PATCH] fixed #421 --- Build/MQTTnet.AspNetCore.nuspec | 3 +- .../MqttConnectionContext.cs | 3 +- .../MQTTnet.AspNetCore.Tests.csproj | 8 ++--- .../Mockups/ConnectionContextMockup.cs | 22 ++++++++++++++ .../Mockups/DuplexPipeMockup.cs | 16 ++++++++++ .../MqttConnectionContextTest.cs | 29 +++++++++++++++++++ .../MQTTnet.Core.Tests.csproj | 2 +- 7 files changed, 76 insertions(+), 7 deletions(-) create mode 100644 Tests/MQTTnet.AspNetCore.Tests/Mockups/ConnectionContextMockup.cs create mode 100644 Tests/MQTTnet.AspNetCore.Tests/Mockups/DuplexPipeMockup.cs create mode 100644 Tests/MQTTnet.AspNetCore.Tests/MqttConnectionContextTest.cs diff --git a/Build/MQTTnet.AspNetCore.nuspec b/Build/MQTTnet.AspNetCore.nuspec index cb1718f..8809fac 100644 --- a/Build/MQTTnet.AspNetCore.nuspec +++ b/Build/MQTTnet.AspNetCore.nuspec @@ -10,7 +10,8 @@ https://raw.githubusercontent.com/chkr1011/MQTTnet/master/Images/Logo_128x128.png false This is a support library to integrate MQTTnet into AspNetCore. - * For more release notes please check the MQTTnet release notes. + * For more release notes please check the MQTTnet release notes. + * [Server] fixed cpu spike in case a client disconnectes (issue 421) Copyright Christian Kratky 2016-2018 MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation Xamarin diff --git a/Source/MQTTnet.AspnetCore/MqttConnectionContext.cs b/Source/MQTTnet.AspnetCore/MqttConnectionContext.cs index 8a8469d..361eb6a 100644 --- a/Source/MQTTnet.AspnetCore/MqttConnectionContext.cs +++ b/Source/MQTTnet.AspnetCore/MqttConnectionContext.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Connections; using MQTTnet.Adapter; using MQTTnet.AspNetCore.Client.Tcp; +using MQTTnet.Exceptions; using MQTTnet.Packets; using MQTTnet.Serializer; using System; @@ -83,7 +84,7 @@ namespace MQTTnet.AspNetCore } else if (readResult.IsCompleted) { - break; + throw new MqttCommunicationException("Connection Aborted"); } } finally diff --git a/Tests/MQTTnet.AspNetCore.Tests/MQTTnet.AspNetCore.Tests.csproj b/Tests/MQTTnet.AspNetCore.Tests/MQTTnet.AspNetCore.Tests.csproj index 77dfff9..f45b4fb 100644 --- a/Tests/MQTTnet.AspNetCore.Tests/MQTTnet.AspNetCore.Tests.csproj +++ b/Tests/MQTTnet.AspNetCore.Tests/MQTTnet.AspNetCore.Tests.csproj @@ -1,4 +1,4 @@ - + netcoreapp2.1 @@ -7,9 +7,9 @@ - - - + + + diff --git a/Tests/MQTTnet.AspNetCore.Tests/Mockups/ConnectionContextMockup.cs b/Tests/MQTTnet.AspNetCore.Tests/Mockups/ConnectionContextMockup.cs new file mode 100644 index 0000000..296959e --- /dev/null +++ b/Tests/MQTTnet.AspNetCore.Tests/Mockups/ConnectionContextMockup.cs @@ -0,0 +1,22 @@ +using System.Collections.Generic; +using System.IO.Pipelines; +using Microsoft.AspNetCore.Connections; +using Microsoft.AspNetCore.Http.Features; + +namespace MQTTnet.AspNetCore.Tests.Mockups +{ + public class ConnectionContextMockup : ConnectionContext + { + public override string ConnectionId { get; set; } + + public override IFeatureCollection Features { get; } + + public override IDictionary Items { get; set; } + public override IDuplexPipe Transport { get; set; } + + public ConnectionContextMockup() + { + //Transport = new DefaultConnectionContext + } + } +} diff --git a/Tests/MQTTnet.AspNetCore.Tests/Mockups/DuplexPipeMockup.cs b/Tests/MQTTnet.AspNetCore.Tests/Mockups/DuplexPipeMockup.cs new file mode 100644 index 0000000..1774f18 --- /dev/null +++ b/Tests/MQTTnet.AspNetCore.Tests/Mockups/DuplexPipeMockup.cs @@ -0,0 +1,16 @@ +using System.IO.Pipelines; + +namespace MQTTnet.AspNetCore.Tests.Mockups +{ + public class DuplexPipeMockup : IDuplexPipe + { + PipeReader IDuplexPipe.Input => Receive.Reader; + + PipeWriter IDuplexPipe.Output => Send.Writer; + + + public Pipe Receive { get; set; } = new Pipe(); + + public Pipe Send { get; set; } = new Pipe(); + } +} diff --git a/Tests/MQTTnet.AspNetCore.Tests/MqttConnectionContextTest.cs b/Tests/MQTTnet.AspNetCore.Tests/MqttConnectionContextTest.cs new file mode 100644 index 0000000..7498015 --- /dev/null +++ b/Tests/MQTTnet.AspNetCore.Tests/MqttConnectionContextTest.cs @@ -0,0 +1,29 @@ +using Microsoft.AspNetCore.Connections; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using MQTTnet.AspNetCore.Tests.Mockups; +using MQTTnet.Exceptions; +using MQTTnet.Serializer; +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace MQTTnet.AspNetCore.Tests +{ + [TestClass] + public class MqttConnectionContextTest + { + [TestMethod] + public async Task TestReceivePacketAsyncThrowsWhenReaderCompleted() + { + var serializer = new MqttPacketSerializer {}; + var pipe = new DuplexPipeMockup(); + var connection = new DefaultConnectionContext(); + connection.Transport = pipe; + var ctx = new MqttConnectionContext(serializer, connection); + + pipe.Receive.Writer.Complete(); + + await Assert.ThrowsExceptionAsync(() => ctx.ReceivePacketAsync(TimeSpan.FromSeconds(1), CancellationToken.None)); + } + } +} diff --git a/Tests/MQTTnet.Core.Tests/MQTTnet.Core.Tests.csproj b/Tests/MQTTnet.Core.Tests/MQTTnet.Core.Tests.csproj index 40697e9..ff820b2 100644 --- a/Tests/MQTTnet.Core.Tests/MQTTnet.Core.Tests.csproj +++ b/Tests/MQTTnet.Core.Tests/MQTTnet.Core.Tests.csproj @@ -9,7 +9,7 @@ - +