Browse Source

fixed unittests and addressed todo

release/3.x.x
Jan Eggers 7 years ago
parent
commit
466b6a25cd
4 changed files with 61 additions and 13 deletions
  1. +10
    -0
      Frameworks/MQTTnet.NetStandard/MqttFactory.cs
  2. +5
    -1
      MQTTnet.Core/Server/IMqttServerFactory.cs
  3. +40
    -10
      Tests/MQTTnet.Core.Tests/MqttServerTests.cs
  4. +6
    -2
      Tests/MQTTnet.Core.Tests/TestLogger.cs

+ 10
- 0
Frameworks/MQTTnet.NetStandard/MqttFactory.cs View File

@@ -4,6 +4,7 @@ using MQTTnet.Core.Client;
using MQTTnet.Core.Serializer; using MQTTnet.Core.Serializer;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using MQTTnet.Implementations; using MQTTnet.Implementations;
using MQTTnet.Core.ManagedClient; using MQTTnet.Core.ManagedClient;
using MQTTnet.Core.Server; using MQTTnet.Core.Server;
@@ -109,5 +110,14 @@ namespace MQTTnet
{ {
return _serviceProvider.GetRequiredService<IMqttServer>(); return _serviceProvider.GetRequiredService<IMqttServer>();
} }

public IMqttServer CreateMqttServer(Action<MqttServerOptions> configure)
{
var options = _serviceProvider.GetRequiredService<IOptions<MqttServerOptions>>();

configure(options.Value);

return _serviceProvider.GetRequiredService<IMqttServer>();
}
} }
} }

+ 5
- 1
MQTTnet.Core/Server/IMqttServerFactory.cs View File

@@ -1,7 +1,11 @@
namespace MQTTnet.Core.Server
using System;

namespace MQTTnet.Core.Server
{ {
public interface IMqttServerFactory public interface IMqttServerFactory
{ {
IMqttServer CreateMqttServer(); IMqttServer CreateMqttServer();

IMqttServer CreateMqttServer(Action<MqttServerOptions> configure);
} }
} }

+ 40
- 10
Tests/MQTTnet.Core.Tests/MqttServerTests.cs View File

@@ -51,7 +51,13 @@ namespace MQTTnet.Core.Tests
public async Task MqttServer_WillMessage() public async Task MqttServer_WillMessage()
{ {
var serverAdapter = new TestMqttServerAdapter(); var serverAdapter = new TestMqttServerAdapter();
var s = new MqttFactory().CreateMqttServer();
var services = new ServiceCollection()
.AddLogging()
.AddMqttServer()
.AddSingleton<IMqttServerAdapter>(serverAdapter)
.BuildServiceProvider();

var s = new MqttFactory(services).CreateMqttServer();
var receivedMessagesCount = 0; var receivedMessagesCount = 0;
try try
{ {
@@ -79,7 +85,13 @@ namespace MQTTnet.Core.Tests
public async Task MqttServer_Unsubscribe() public async Task MqttServer_Unsubscribe()
{ {
var serverAdapter = new TestMqttServerAdapter(); var serverAdapter = new TestMqttServerAdapter();
var s = new MqttFactory().CreateMqttServer();
var services = new ServiceCollection()
.AddLogging()
.AddMqttServer()
.AddSingleton<IMqttServerAdapter>(serverAdapter)
.BuildServiceProvider();

var s = new MqttFactory(services).CreateMqttServer();


var receivedMessagesCount = 0; var receivedMessagesCount = 0;


@@ -121,7 +133,13 @@ namespace MQTTnet.Core.Tests
public async Task MqttServer_Publish() public async Task MqttServer_Publish()
{ {
var serverAdapter = new TestMqttServerAdapter(); var serverAdapter = new TestMqttServerAdapter();
var s = new MqttFactory().CreateMqttServer();
var services = new ServiceCollection()
.AddLogging()
.AddMqttServer()
.AddSingleton<IMqttServerAdapter>(serverAdapter)
.BuildServiceProvider();

var s = new MqttFactory(services).CreateMqttServer();
var receivedMessagesCount = 0; var receivedMessagesCount = 0;


try try
@@ -150,7 +168,13 @@ namespace MQTTnet.Core.Tests
public async Task MqttServer_NoRetainedMessage() public async Task MqttServer_NoRetainedMessage()
{ {
var serverAdapter = new TestMqttServerAdapter(); var serverAdapter = new TestMqttServerAdapter();
var s = new MqttFactory().CreateMqttServer();
var services = new ServiceCollection()
.AddLogging()
.AddMqttServer()
.AddSingleton<IMqttServerAdapter>(serverAdapter)
.BuildServiceProvider();

var s = new MqttFactory(services).CreateMqttServer();
var receivedMessagesCount = 0; var receivedMessagesCount = 0;


try try
@@ -179,7 +203,13 @@ namespace MQTTnet.Core.Tests
public async Task MqttServer_RetainedMessage() public async Task MqttServer_RetainedMessage()
{ {
var serverAdapter = new TestMqttServerAdapter(); var serverAdapter = new TestMqttServerAdapter();
var s = new MqttFactory().CreateMqttServer();
var services = new ServiceCollection()
.AddLogging()
.AddMqttServer()
.AddSingleton<IMqttServerAdapter>(serverAdapter)
.BuildServiceProvider();

var s = new MqttFactory(services).CreateMqttServer();


var receivedMessagesCount = 0; var receivedMessagesCount = 0;
try try
@@ -210,7 +240,7 @@ namespace MQTTnet.Core.Tests
var serverAdapter = new TestMqttServerAdapter(); var serverAdapter = new TestMqttServerAdapter();
var services = new ServiceCollection() var services = new ServiceCollection()
.AddLogging() .AddLogging()
.AddMqttServer() // TODO: Is there maybe an easier way for the library user to set the options?
.AddMqttServer()
.AddSingleton<IMqttServerAdapter>(serverAdapter) .AddSingleton<IMqttServerAdapter>(serverAdapter)
.BuildServiceProvider(); .BuildServiceProvider();


@@ -248,11 +278,11 @@ namespace MQTTnet.Core.Tests
var serverAdapter = new TestMqttServerAdapter(); var serverAdapter = new TestMqttServerAdapter();
var services = new ServiceCollection() var services = new ServiceCollection()
.AddLogging() .AddLogging()
.AddMqttServer(options => options.Storage = storage) // TODO: Is there maybe an easier way for the library user to set the options?
.AddMqttServer()
.AddSingleton<IMqttServerAdapter>(serverAdapter) .AddSingleton<IMqttServerAdapter>(serverAdapter)
.BuildServiceProvider(); .BuildServiceProvider();


var s = new MqttFactory(services).CreateMqttServer(); // TODO: Like here?
var s = new MqttFactory(services).CreateMqttServer(options => options.Storage = storage);


try try
{ {
@@ -300,11 +330,11 @@ namespace MQTTnet.Core.Tests
var serverAdapter = new TestMqttServerAdapter(); var serverAdapter = new TestMqttServerAdapter();
var services = new ServiceCollection() var services = new ServiceCollection()
.AddLogging() .AddLogging()
.AddMqttServer(options => options.ApplicationMessageInterceptor = Interceptor)
.AddMqttServer()
.AddSingleton<IMqttServerAdapter>(serverAdapter) .AddSingleton<IMqttServerAdapter>(serverAdapter)
.BuildServiceProvider(); .BuildServiceProvider();


var s = services.GetRequiredService<IMqttServer>();
var s = new MqttFactory(services).CreateMqttServer(options => options.ApplicationMessageInterceptor = Interceptor);
try try
{ {
await s.StartAsync(); await s.StartAsync();


+ 6
- 2
Tests/MQTTnet.Core.Tests/TestLogger.cs View File

@@ -3,11 +3,11 @@ using System;


namespace MQTTnet.Core.Tests namespace MQTTnet.Core.Tests
{ {
public class TestLogger<T> : ILogger<T>
public class TestLogger<T> : IDisposable, ILogger<T>
{ {
public IDisposable BeginScope<TState>(TState state) public IDisposable BeginScope<TState>(TState state)
{ {
throw new NotImplementedException();
return this;
} }


public bool IsEnabled(LogLevel logLevel) public bool IsEnabled(LogLevel logLevel)
@@ -18,5 +18,9 @@ namespace MQTTnet.Core.Tests
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{ {
} }

public void Dispose()
{
}
} }
} }

Loading…
Cancel
Save