From 466b6a25cde8672c27f0b3f333692509081fe710 Mon Sep 17 00:00:00 2001 From: Jan Eggers Date: Thu, 26 Oct 2017 08:11:37 +0200 Subject: [PATCH] fixed unittests and addressed todo --- Frameworks/MQTTnet.NetStandard/MqttFactory.cs | 10 ++++ MQTTnet.Core/Server/IMqttServerFactory.cs | 6 ++- Tests/MQTTnet.Core.Tests/MqttServerTests.cs | 50 +++++++++++++++---- Tests/MQTTnet.Core.Tests/TestLogger.cs | 8 ++- 4 files changed, 61 insertions(+), 13 deletions(-) diff --git a/Frameworks/MQTTnet.NetStandard/MqttFactory.cs b/Frameworks/MQTTnet.NetStandard/MqttFactory.cs index 7d88480..0ff6136 100644 --- a/Frameworks/MQTTnet.NetStandard/MqttFactory.cs +++ b/Frameworks/MQTTnet.NetStandard/MqttFactory.cs @@ -4,6 +4,7 @@ using MQTTnet.Core.Client; using MQTTnet.Core.Serializer; using Microsoft.Extensions.Logging; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using MQTTnet.Implementations; using MQTTnet.Core.ManagedClient; using MQTTnet.Core.Server; @@ -109,5 +110,14 @@ namespace MQTTnet { return _serviceProvider.GetRequiredService(); } + + public IMqttServer CreateMqttServer(Action configure) + { + var options = _serviceProvider.GetRequiredService>(); + + configure(options.Value); + + return _serviceProvider.GetRequiredService(); + } } } \ No newline at end of file diff --git a/MQTTnet.Core/Server/IMqttServerFactory.cs b/MQTTnet.Core/Server/IMqttServerFactory.cs index 61d022f..500614b 100644 --- a/MQTTnet.Core/Server/IMqttServerFactory.cs +++ b/MQTTnet.Core/Server/IMqttServerFactory.cs @@ -1,7 +1,11 @@ -namespace MQTTnet.Core.Server +using System; + +namespace MQTTnet.Core.Server { public interface IMqttServerFactory { IMqttServer CreateMqttServer(); + + IMqttServer CreateMqttServer(Action configure); } } \ No newline at end of file diff --git a/Tests/MQTTnet.Core.Tests/MqttServerTests.cs b/Tests/MQTTnet.Core.Tests/MqttServerTests.cs index 97aa3f8..4dd9f3a 100644 --- a/Tests/MQTTnet.Core.Tests/MqttServerTests.cs +++ b/Tests/MQTTnet.Core.Tests/MqttServerTests.cs @@ -51,7 +51,13 @@ namespace MQTTnet.Core.Tests public async Task MqttServer_WillMessage() { var serverAdapter = new TestMqttServerAdapter(); - var s = new MqttFactory().CreateMqttServer(); + var services = new ServiceCollection() + .AddLogging() + .AddMqttServer() + .AddSingleton(serverAdapter) + .BuildServiceProvider(); + + var s = new MqttFactory(services).CreateMqttServer(); var receivedMessagesCount = 0; try { @@ -79,7 +85,13 @@ namespace MQTTnet.Core.Tests public async Task MqttServer_Unsubscribe() { var serverAdapter = new TestMqttServerAdapter(); - var s = new MqttFactory().CreateMqttServer(); + var services = new ServiceCollection() + .AddLogging() + .AddMqttServer() + .AddSingleton(serverAdapter) + .BuildServiceProvider(); + + var s = new MqttFactory(services).CreateMqttServer(); var receivedMessagesCount = 0; @@ -121,7 +133,13 @@ namespace MQTTnet.Core.Tests public async Task MqttServer_Publish() { var serverAdapter = new TestMqttServerAdapter(); - var s = new MqttFactory().CreateMqttServer(); + var services = new ServiceCollection() + .AddLogging() + .AddMqttServer() + .AddSingleton(serverAdapter) + .BuildServiceProvider(); + + var s = new MqttFactory(services).CreateMqttServer(); var receivedMessagesCount = 0; try @@ -150,7 +168,13 @@ namespace MQTTnet.Core.Tests public async Task MqttServer_NoRetainedMessage() { var serverAdapter = new TestMqttServerAdapter(); - var s = new MqttFactory().CreateMqttServer(); + var services = new ServiceCollection() + .AddLogging() + .AddMqttServer() + .AddSingleton(serverAdapter) + .BuildServiceProvider(); + + var s = new MqttFactory(services).CreateMqttServer(); var receivedMessagesCount = 0; try @@ -179,7 +203,13 @@ namespace MQTTnet.Core.Tests public async Task MqttServer_RetainedMessage() { var serverAdapter = new TestMqttServerAdapter(); - var s = new MqttFactory().CreateMqttServer(); + var services = new ServiceCollection() + .AddLogging() + .AddMqttServer() + .AddSingleton(serverAdapter) + .BuildServiceProvider(); + + var s = new MqttFactory(services).CreateMqttServer(); var receivedMessagesCount = 0; try @@ -210,7 +240,7 @@ namespace MQTTnet.Core.Tests var serverAdapter = new TestMqttServerAdapter(); var services = new ServiceCollection() .AddLogging() - .AddMqttServer() // TODO: Is there maybe an easier way for the library user to set the options? + .AddMqttServer() .AddSingleton(serverAdapter) .BuildServiceProvider(); @@ -248,11 +278,11 @@ namespace MQTTnet.Core.Tests var serverAdapter = new TestMqttServerAdapter(); var services = new ServiceCollection() .AddLogging() - .AddMqttServer(options => options.Storage = storage) // TODO: Is there maybe an easier way for the library user to set the options? + .AddMqttServer() .AddSingleton(serverAdapter) .BuildServiceProvider(); - var s = new MqttFactory(services).CreateMqttServer(); // TODO: Like here? + var s = new MqttFactory(services).CreateMqttServer(options => options.Storage = storage); try { @@ -300,11 +330,11 @@ namespace MQTTnet.Core.Tests var serverAdapter = new TestMqttServerAdapter(); var services = new ServiceCollection() .AddLogging() - .AddMqttServer(options => options.ApplicationMessageInterceptor = Interceptor) + .AddMqttServer() .AddSingleton(serverAdapter) .BuildServiceProvider(); - var s = services.GetRequiredService(); + var s = new MqttFactory(services).CreateMqttServer(options => options.ApplicationMessageInterceptor = Interceptor); try { await s.StartAsync(); diff --git a/Tests/MQTTnet.Core.Tests/TestLogger.cs b/Tests/MQTTnet.Core.Tests/TestLogger.cs index ea597a6..f57c877 100644 --- a/Tests/MQTTnet.Core.Tests/TestLogger.cs +++ b/Tests/MQTTnet.Core.Tests/TestLogger.cs @@ -3,11 +3,11 @@ using System; namespace MQTTnet.Core.Tests { - public class TestLogger : ILogger + public class TestLogger : IDisposable, ILogger { public IDisposable BeginScope(TState state) { - throw new NotImplementedException(); + return this; } public bool IsEnabled(LogLevel logLevel) @@ -18,5 +18,9 @@ namespace MQTTnet.Core.Tests public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) { } + + public void Dispose() + { + } } }