Преглед изворни кода

fixed unittests and addressed todo

release/3.x.x
Jan Eggers пре 7 година
родитељ
комит
466b6a25cd
4 измењених фајлова са 61 додато и 13 уклоњено
  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 Прегледај датотеку

@@ -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<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 Прегледај датотеку

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

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

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

+ 40
- 10
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<IMqttServerAdapter>(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<IMqttServerAdapter>(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<IMqttServerAdapter>(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<IMqttServerAdapter>(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<IMqttServerAdapter>(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<IMqttServerAdapter>(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<IMqttServerAdapter>(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<IMqttServerAdapter>(serverAdapter)
.BuildServiceProvider();

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


+ 6
- 2
Tests/MQTTnet.Core.Tests/TestLogger.cs Прегледај датотеку

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

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

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 Dispose()
{
}
}
}

Loading…
Откажи
Сачувај