From 8fa91736b64ab34e2c6f27547058e453dff4bfd7 Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Wed, 25 Oct 2017 22:46:19 +0200 Subject: [PATCH] Fix unit tests --- Frameworks/MQTTnet.NetStandard/MqttFactory.cs | 4 +- .../ServiceCollectionExtensions.cs | 3 +- .../MQTTnet.UniversalWindows/MqttFactory.cs | 2 +- .../ServiceCollectionExtensions.cs | 3 +- MQTTnet.Core/Client/IMqttClientFactory.cs | 2 +- MQTTnet.Core/Server/MqttServer.cs | 1 + Tests/MQTTnet.Core.Tests/MqttServerTests.cs | 78 ++++++------------- 7 files changed, 31 insertions(+), 62 deletions(-) diff --git a/Frameworks/MQTTnet.NetStandard/MqttFactory.cs b/Frameworks/MQTTnet.NetStandard/MqttFactory.cs index b725f03..7d88480 100644 --- a/Frameworks/MQTTnet.NetStandard/MqttFactory.cs +++ b/Frameworks/MQTTnet.NetStandard/MqttFactory.cs @@ -100,9 +100,9 @@ namespace MQTTnet return _serviceProvider.GetRequiredService(); } - public ManagedMqttClient CreateManagedMqttClient() + public IManagedMqttClient CreateManagedMqttClient() { - return _serviceProvider.GetRequiredService(); + return _serviceProvider.GetRequiredService(); } public IMqttServer CreateMqttServer() diff --git a/Frameworks/MQTTnet.NetStandard/ServiceCollectionExtensions.cs b/Frameworks/MQTTnet.NetStandard/ServiceCollectionExtensions.cs index 019f39b..fdecb32 100644 --- a/Frameworks/MQTTnet.NetStandard/ServiceCollectionExtensions.cs +++ b/Frameworks/MQTTnet.NetStandard/ServiceCollectionExtensions.cs @@ -52,8 +52,9 @@ namespace MQTTnet services.AddTransient(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); return services; diff --git a/Frameworks/MQTTnet.UniversalWindows/MqttFactory.cs b/Frameworks/MQTTnet.UniversalWindows/MqttFactory.cs index 68fecd7..24826cb 100644 --- a/Frameworks/MQTTnet.UniversalWindows/MqttFactory.cs +++ b/Frameworks/MQTTnet.UniversalWindows/MqttFactory.cs @@ -95,7 +95,7 @@ namespace MQTTnet return _serviceProvider.GetRequiredService(); } - public ManagedMqttClient CreateManagedMqttClient() + public IManagedMqttClient CreateManagedMqttClient() { return _serviceProvider.GetRequiredService(); } diff --git a/Frameworks/MQTTnet.UniversalWindows/ServiceCollectionExtensions.cs b/Frameworks/MQTTnet.UniversalWindows/ServiceCollectionExtensions.cs index f8f647a..9b8e6f7 100644 --- a/Frameworks/MQTTnet.UniversalWindows/ServiceCollectionExtensions.cs +++ b/Frameworks/MQTTnet.UniversalWindows/ServiceCollectionExtensions.cs @@ -46,8 +46,9 @@ namespace MQTTnet services.AddTransient(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); return services; diff --git a/MQTTnet.Core/Client/IMqttClientFactory.cs b/MQTTnet.Core/Client/IMqttClientFactory.cs index 7547906..c8b5ef3 100644 --- a/MQTTnet.Core/Client/IMqttClientFactory.cs +++ b/MQTTnet.Core/Client/IMqttClientFactory.cs @@ -6,6 +6,6 @@ namespace MQTTnet.Core.Client { IMqttClient CreateMqttClient(); - ManagedMqttClient CreateManagedMqttClient(); + IManagedMqttClient CreateManagedMqttClient(); } } \ No newline at end of file diff --git a/MQTTnet.Core/Server/MqttServer.cs b/MQTTnet.Core/Server/MqttServer.cs index 0d208c0..c651e82 100644 --- a/MQTTnet.Core/Server/MqttServer.cs +++ b/MQTTnet.Core/Server/MqttServer.cs @@ -28,6 +28,7 @@ namespace MQTTnet.Core.Server { throw new ArgumentNullException(nameof(adapters)); } + _adapters = adapters.ToList(); _clientSessionsManager.ApplicationMessageReceived += (s, e) => ApplicationMessageReceived?.Invoke(s, e); diff --git a/Tests/MQTTnet.Core.Tests/MqttServerTests.cs b/Tests/MQTTnet.Core.Tests/MqttServerTests.cs index e866ca8..67a6112 100644 --- a/Tests/MQTTnet.Core.Tests/MqttServerTests.cs +++ b/Tests/MQTTnet.Core.Tests/MqttServerTests.cs @@ -5,7 +5,6 @@ using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; using MQTTnet.Core.Adapter; using MQTTnet.Core.Client; -using MQTTnet.Core.Packets; using MQTTnet.Core.Protocol; using MQTTnet.Core.Server; using Microsoft.Extensions.DependencyInjection; @@ -52,12 +51,7 @@ namespace MQTTnet.Core.Tests public async Task MqttServer_WillMessage() { var serverAdapter = new TestMqttServerAdapter(); - var services = new ServiceCollection() - .AddMqttServer() - .AddSingleton(serverAdapter) - .BuildServiceProvider(); - - var s = services.GetRequiredService(); + var s = new MqttFactory().CreateMqttServer(); await s.StartAsync(); var willMessage = new MqttApplicationMessageBuilder().WithTopic("My/last/will").WithAtMostOnceQoS().Build(); @@ -81,12 +75,7 @@ namespace MQTTnet.Core.Tests public async Task MqttServer_Unsubscribe() { var serverAdapter = new TestMqttServerAdapter(); - var services = new ServiceCollection() - .AddMqttServer() - .AddSingleton(serverAdapter) - .BuildServiceProvider(); - - var s = services.GetRequiredService(); + var s = new MqttFactory().CreateMqttServer(); await s.StartAsync(); var c1 = await serverAdapter.ConnectTestClient(s, "c1"); @@ -122,12 +111,7 @@ namespace MQTTnet.Core.Tests public async Task MqttServer_Publish() { var serverAdapter = new TestMqttServerAdapter(); - var services = new ServiceCollection() - .AddMqttServer() - .AddSingleton(serverAdapter) - .BuildServiceProvider(); - - var s = services.GetRequiredService(); + var s = new MqttFactory().CreateMqttServer(); await s.StartAsync(); var c1 = await serverAdapter.ConnectTestClient(s, "c1"); @@ -150,22 +134,17 @@ namespace MQTTnet.Core.Tests public async Task MqttServer_NoRetainedMessage() { var serverAdapter = new TestMqttServerAdapter(); - var services = new ServiceCollection() - .AddMqttServer() - .AddSingleton(serverAdapter) - .BuildServiceProvider(); - - var s = services.GetRequiredService(); + var s = new MqttFactory().CreateMqttServer(); await s.StartAsync(); var c1 = await serverAdapter.ConnectTestClient(s, "c1"); - await c1.PublishAsync(new MqttApplicationMessage("retained", new byte[3], MqttQualityOfServiceLevel.AtLeastOnce, false)); + await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).Build()); await c1.DisconnectAsync(); var c2 = await serverAdapter.ConnectTestClient(s, "c2"); var receivedMessagesCount = 0; c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++; - await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtLeastOnce)); + await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtMostOnce)); await Task.Delay(500); @@ -178,22 +157,17 @@ namespace MQTTnet.Core.Tests public async Task MqttServer_RetainedMessage() { var serverAdapter = new TestMqttServerAdapter(); - var services = new ServiceCollection() - .AddMqttServer() - .AddSingleton(serverAdapter) - .BuildServiceProvider(); - - var s = services.GetRequiredService(); + var s = new MqttFactory().CreateMqttServer(); await s.StartAsync(); var c1 = await serverAdapter.ConnectTestClient(s, "c1"); - await c1.PublishAsync(new MqttApplicationMessage("retained", new byte[3], MqttQualityOfServiceLevel.AtLeastOnce, true)); + await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).WithRetainFlag().Build()); await c1.DisconnectAsync(); var c2 = await serverAdapter.ConnectTestClient(s, "c2"); var receivedMessagesCount = 0; c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++; - await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtLeastOnce)); + await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtMostOnce)); await Task.Delay(500); @@ -206,23 +180,18 @@ namespace MQTTnet.Core.Tests public async Task MqttServer_ClearRetainedMessage() { var serverAdapter = new TestMqttServerAdapter(); - var services = new ServiceCollection() - .AddMqttServer() - .AddSingleton(serverAdapter) - .BuildServiceProvider(); - - var s = services.GetRequiredService(); + var s = new MqttFactory().CreateMqttServer(); await s.StartAsync(); var c1 = await serverAdapter.ConnectTestClient(s, "c1"); - await c1.PublishAsync(new MqttApplicationMessage("retained", new byte[3], MqttQualityOfServiceLevel.AtLeastOnce, true)); - await c1.PublishAsync(new MqttApplicationMessage("retained", new byte[0], MqttQualityOfServiceLevel.AtLeastOnce, true)); + await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).WithRetainFlag().Build()); + await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[0]).WithRetainFlag().Build()); await c1.DisconnectAsync(); var c2 = await serverAdapter.ConnectTestClient(s, "c2"); var receivedMessagesCount = 0; c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++; - await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtLeastOnce)); + await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtMostOnce)); await Task.Delay(500); @@ -238,15 +207,15 @@ namespace MQTTnet.Core.Tests var serverAdapter = new TestMqttServerAdapter(); var services = new ServiceCollection() - .AddMqttServer() + .AddMqttServer(options => options.Storage = storage) .AddSingleton(serverAdapter) .BuildServiceProvider(); - var s = services.GetRequiredService(); + var s = new MqttFactory().CreateMqttServer(); await s.StartAsync(); var c1 = await serverAdapter.ConnectTestClient(s, "c1"); - await c1.PublishAsync(new MqttApplicationMessage("retained", new byte[3], MqttQualityOfServiceLevel.AtLeastOnce, true)); + await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).WithRetainFlag().Build()); await c1.DisconnectAsync(); await s.StopAsync(); @@ -257,7 +226,7 @@ namespace MQTTnet.Core.Tests var c2 = await serverAdapter.ConnectTestClient(s, "c2"); var receivedMessagesCount = 0; c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++; - await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtLeastOnce)); + await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtMostOnce)); await Task.Delay(500); @@ -269,18 +238,15 @@ namespace MQTTnet.Core.Tests [TestMethod] public async Task MqttServer_InterceptMessage() { - var options = new MqttServerOptions + MqttApplicationMessage Interceptor(MqttApplicationMessage message) { - ApplicationMessageInterceptor = message => - { - message.Payload = Encoding.ASCII.GetBytes("extended"); - return message; - } - }; + message.Payload = Encoding.ASCII.GetBytes("extended"); + return message; + } var serverAdapter = new TestMqttServerAdapter(); var services = new ServiceCollection() - .AddMqttServer() + .AddMqttServer(options => options.ApplicationMessageInterceptor = Interceptor) .AddSingleton(serverAdapter) .BuildServiceProvider();