Browse Source

Merge branch 'develop' of https://github.com/chkr1011/MQTTnet into develop

release/3.x.x
Christian Kratky 7 years ago
parent
commit
d560cb4c9e
17 changed files with 274 additions and 127 deletions
  1. +30
    -0
      Build/MQTTnet.AspNetCore.nuspec
  2. +2
    -2
      Build/MQTTnet.nuspec
  3. +4
    -2
      Build/build.ps1
  4. +1
    -1
      Frameworks/MQTTnet.AspnetCore/ApplicationBuilderExtensions.cs
  5. +1
    -1
      Frameworks/MQTTnet.AspnetCore/MqttHostedServer.cs
  6. +1
    -1
      Frameworks/MQTTnet.AspnetCore/MqttWebSocketServerAdapter.cs
  7. +1
    -1
      Frameworks/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs
  8. +9
    -0
      Frameworks/MQTTnet.NetStandard/MqttFactory.cs
  9. +5
    -0
      MQTTnet.Core/Client/IMqttClientOptions.cs
  10. +1
    -1
      MQTTnet.Core/Client/MqttClient.cs
  11. +3
    -0
      MQTTnet.Core/Client/MqttClientOptions.cs
  12. +5
    -1
      MQTTnet.Core/Server/IMqttServerFactory.cs
  13. +2
    -1
      MQTTnet.sln
  14. +201
    -112
      Tests/MQTTnet.Core.Tests/MqttServerTests.cs
  15. +6
    -2
      Tests/MQTTnet.Core.Tests/TestLogger.cs
  16. +1
    -1
      Tests/MQTTnet.TestApp.AspNetCore2/MQTTnet.TestApp.AspNetCore2.csproj
  17. +1
    -1
      Tests/MQTTnet.TestApp.AspNetCore2/Startup.cs

+ 30
- 0
Build/MQTTnet.AspNetCore.nuspec View File

@@ -0,0 +1,30 @@
<?xml version="1.0"?>
<package >
<metadata>
<id>MQTTnet.AspNetCore</id>
<version>2.5.0</version>
<authors>Christian Kratky</authors>
<owners>Christian Kratky</owners>
<licenseUrl>https://github.com/chkr1011/MQTTnet/blob/master/LICENSE</licenseUrl>
<projectUrl>https://github.com/chkr1011/MQTTnet</projectUrl>
<iconUrl>https://raw.githubusercontent.com/chkr1011/MQTTnet/master/Images/Logo_128x128.png</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>This is a support library to integrate MQTTnet into AspNetCore.</description>
<releaseNotes>initial version
</releaseNotes>
<copyright>Copyright Christian Kratky 2016-2017</copyright>
<tags>MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation</tags>
<dependencies>

<group targetFramework="netstandard2.0">
<dependency id="MQTTnet" version="2.5" />
</group>
</dependencies>
</metadata>

<files>
<!-- .NET Standard 2.0 -->
<file src="..\Frameworks\MQTTnet.AspNetCore\bin\Release\netstandard2.0\MQTTnet.AspNetCore.*" target="lib\netstandard2.0\"/>
</files>
</package>

+ 2
- 2
Build/MQTTnet.nuspec View File

@@ -51,8 +51,8 @@
<file src="..\Frameworks\MQTTnet.UniversalWindows\bin\Release\MQTTnet.*" target="lib\uap10.0\"/> <file src="..\Frameworks\MQTTnet.UniversalWindows\bin\Release\MQTTnet.*" target="lib\uap10.0\"/>


<!-- .NET Framework --> <!-- .NET Framework -->
<file src="..\Frameworks\MQTTnet.Netstandard\bin\Release\net45\MQTTnet.Core.*" target="lib\net45\"/>
<file src="..\Frameworks\MQTTnet.Netstandard\bin\Release\net45\MQTTnet.*" target="lib\net45\"/>
<file src="..\Frameworks\MQTTnet.Netstandard\bin\Release\net451\MQTTnet.Core.*" target="lib\net451\"/>
<file src="..\Frameworks\MQTTnet.Netstandard\bin\Release\net451\MQTTnet.*" target="lib\net451\"/>
</files> </files>
</package> </package>

+ 4
- 2
Build/build.ps1 View File

@@ -3,9 +3,11 @@ param([string]$version)
if ([string]::IsNullOrEmpty($version)) {$version = "0.0.1"} if ([string]::IsNullOrEmpty($version)) {$version = "0.0.1"}


$msbuild = "MSBuild.exe" $msbuild = "MSBuild.exe"
&dotnet build ..\Frameworks\MQTTnet.Netstandard\MQTTnet.Netstandard.csproj -c="Release"
&dotnet build ..\Frameworks\MQTTnet.Netstandard\MQTTnet.Netstandard.csproj -c="Release" /p:FileVersion=$version /p:AssemblyVersion=$version
&dotnet build ..\Frameworks\MQTTnet.AspNetCore\MQTTnet.AspNetCore.csproj -c="Release" /p:FileVersion=$version /p:AssemblyVersion=$version
&$msbuild ..\Frameworks\MQTTnet.UniversalWindows\MQTTnet.UniversalWindows.csproj /t:Build /p:Configuration="Release" &$msbuild ..\Frameworks\MQTTnet.UniversalWindows\MQTTnet.UniversalWindows.csproj /t:Build /p:Configuration="Release"


Remove-Item .\NuGet -Force -Recurse Remove-Item .\NuGet -Force -Recurse
New-Item -ItemType Directory -Force -Path .\NuGet New-Item -ItemType Directory -Force -Path .\NuGet
.\NuGet.exe pack MQTTnet.nuspec -Verbosity detailed -Symbols -OutputDir "NuGet" -Version $version
.\NuGet.exe pack MQTTnet.nuspec -Verbosity detailed -Symbols -OutputDir "NuGet" -Version $version
.\NuGet.exe pack MQTTnet.AspNetCore.nuspec -Verbosity detailed -Symbols -OutputDir "NuGet" -Version $version

+ 1
- 1
Frameworks/MQTTnet.AspnetCore/ApplicationBuilderExtensions.cs View File

@@ -3,7 +3,7 @@ using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using MQTTnet.Core.Server; using MQTTnet.Core.Server;


namespace MQTTnet.AspnetCore
namespace MQTTnet.AspNetCore
{ {
public static class ApplicationBuilderExtensions public static class ApplicationBuilderExtensions
{ {


+ 1
- 1
Frameworks/MQTTnet.AspnetCore/MqttHostedServer.cs View File

@@ -7,7 +7,7 @@ using Microsoft.Extensions.Options;
using MQTTnet.Core.Adapter; using MQTTnet.Core.Adapter;
using MQTTnet.Core.Server; using MQTTnet.Core.Server;


namespace MQTTnet.AspnetCore
namespace MQTTnet.AspNetCore
{ {
public class MqttHostedServer : MqttServer, IHostedService public class MqttHostedServer : MqttServer, IHostedService
{ {


+ 1
- 1
Frameworks/MQTTnet.AspnetCore/MqttWebSocketServerAdapter.cs View File

@@ -8,7 +8,7 @@ using MQTTnet.Core.Channel;
using MQTTnet.Core.Server; using MQTTnet.Core.Server;
using MQTTnet.Implementations; using MQTTnet.Implementations;


namespace MQTTnet.AspnetCore
namespace MQTTnet.AspNetCore
{ {
public class MqttWebSocketServerAdapter : IMqttServerAdapter, IDisposable public class MqttWebSocketServerAdapter : IMqttServerAdapter, IDisposable
{ {


+ 1
- 1
Frameworks/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs View File

@@ -3,7 +3,7 @@ using Microsoft.Extensions.Hosting;
using MQTTnet.Core.Adapter; using MQTTnet.Core.Adapter;
using MQTTnet.Core.Server; using MQTTnet.Core.Server;


namespace MQTTnet.AspnetCore
namespace MQTTnet.AspNetCore
{ {
public static class ServiceCollectionExtensions public static class ServiceCollectionExtensions
{ {


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

@@ -116,5 +116,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
- 0
MQTTnet.Core/Client/IMqttClientOptions.cs View File

@@ -6,6 +6,11 @@ namespace MQTTnet.Core.Client
public interface IMqttClientOptions public interface IMqttClientOptions
{ {
string ClientId { get; } string ClientId { get; }

/// <summary>
/// The LogId is used to create a scope to correlate logging. If no value is provided the ClientId is used instead
/// </summary>
string LogId { get; }
IMqttClientCredentials Credentials { get; } IMqttClientCredentials Credentials { get; }
bool CleanSession { get; } bool CleanSession { get; }
MqttApplicationMessage WillMessage { get; } MqttApplicationMessage WillMessage { get; }


+ 1
- 1
MQTTnet.Core/Client/MqttClient.cs View File

@@ -55,7 +55,7 @@ namespace MQTTnet.Core.Client


_adapter = _communicationAdapterFactory.CreateClientMqttCommunicationAdapter(options); _adapter = _communicationAdapterFactory.CreateClientMqttCommunicationAdapter(options);


_scopeHandle = _logger.BeginScope(options.ClientId);
_scopeHandle = _logger.BeginScope(options.LogId ?? options.ClientId);
_logger.LogTrace("Trying to connect with server."); _logger.LogTrace("Trying to connect with server.");
await _adapter.ConnectAsync(_options.CommunicationTimeout).ConfigureAwait(false); await _adapter.ConnectAsync(_options.CommunicationTimeout).ConfigureAwait(false);
_logger.LogTrace("Connection with server established."); _logger.LogTrace("Connection with server established.");


+ 3
- 0
MQTTnet.Core/Client/MqttClientOptions.cs View File

@@ -9,6 +9,9 @@ namespace MQTTnet.Core.Client


public string ClientId { get; set; } = Guid.NewGuid().ToString("N"); public string ClientId { get; set; } = Guid.NewGuid().ToString("N");


/// <inheritdoc />
public string LogId { get; set; }

public bool CleanSession { get; set; } = true; public bool CleanSession { get; set; } = true;


public IMqttClientCredentials Credentials { get; set; } = new MqttClientCredentials(); public IMqttClientCredentials Credentials { get; set; } = new MqttClientCredentials();


+ 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);
} }
} }

+ 2
- 1
MQTTnet.sln View File

@@ -20,6 +20,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{67C28AC1-BC3A-420A-BE9C-FA2401431CF9}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{67C28AC1-BC3A-420A-BE9C-FA2401431CF9}"
ProjectSection(SolutionItems) = preProject ProjectSection(SolutionItems) = preProject
Build\build.ps1 = Build\build.ps1 Build\build.ps1 = Build\build.ps1
Build\MQTTnet.AspNetCore.nuspec = Build\MQTTnet.AspNetCore.nuspec
Build\MQTTnet.nuspec = Build\MQTTnet.nuspec Build\MQTTnet.nuspec = Build\MQTTnet.nuspec
EndProjectSection EndProjectSection
EndProject EndProject
@@ -33,7 +34,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MQTTnet.TestApp.NetCore", "
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MQTTnet.TestApp.AspNetCore2", "Tests\MQTTnet.TestApp.AspNetCore2\MQTTnet.TestApp.AspNetCore2.csproj", "{C6FF8AEA-0855-41EC-A1F3-AC262225BAB9}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MQTTnet.TestApp.AspNetCore2", "Tests\MQTTnet.TestApp.AspNetCore2\MQTTnet.TestApp.AspNetCore2.csproj", "{C6FF8AEA-0855-41EC-A1F3-AC262225BAB9}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MQTTnet.AspnetCore", "Frameworks\MQTTnet.AspnetCore\MQTTnet.AspnetCore.csproj", "{F10C4060-F7EE-4A83-919F-FF723E72F94A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MQTTnet.AspNetCore", "Frameworks\MQTTnet.AspnetCore\MQTTnet.AspNetCore.csproj", "{F10C4060-F7EE-4A83-919F-FF723E72F94A}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution


+ 201
- 112
Tests/MQTTnet.Core.Tests/MqttServerTests.cs View File

@@ -51,23 +51,33 @@ 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();
await s.StartAsync();

var willMessage = new MqttApplicationMessageBuilder().WithTopic("My/last/will").WithAtMostOnceQoS().Build();
var c1 = await serverAdapter.ConnectTestClient(s, "c1");
var c2 = await serverAdapter.ConnectTestClient(s, "c2", willMessage);
var services = new ServiceCollection()
.AddLogging()
.AddMqttServer()
.AddSingleton<IMqttServerAdapter>(serverAdapter)
.BuildServiceProvider();


var s = new MqttFactory(services).CreateMqttServer();
var receivedMessagesCount = 0; var receivedMessagesCount = 0;
c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
await c1.SubscribeAsync(new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce));
try
{
await s.StartAsync();


await c2.DisconnectAsync();
var willMessage = new MqttApplicationMessageBuilder().WithTopic("My/last/will").WithAtMostOnceQoS().Build();
var c1 = await serverAdapter.ConnectTestClient(s, "c1");
var c2 = await serverAdapter.ConnectTestClient(s, "c2", willMessage);


await Task.Delay(1000);
c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
await c1.SubscribeAsync(new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce));


await s.StopAsync();
await c2.DisconnectAsync();


await Task.Delay(1000);
}
finally
{
await s.StopAsync();
}
Assert.AreEqual(1, receivedMessagesCount); Assert.AreEqual(1, receivedMessagesCount);
} }


@@ -75,33 +85,45 @@ 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();
await s.StartAsync();
var services = new ServiceCollection()
.AddLogging()
.AddMqttServer()
.AddSingleton<IMqttServerAdapter>(serverAdapter)
.BuildServiceProvider();


var c1 = await serverAdapter.ConnectTestClient(s, "c1");
var c2 = await serverAdapter.ConnectTestClient(s, "c2");
var s = new MqttFactory(services).CreateMqttServer();


var receivedMessagesCount = 0; var receivedMessagesCount = 0;
c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;


var message = new MqttApplicationMessageBuilder().WithTopic("a").WithAtLeastOnceQoS().Build();
try
{
await s.StartAsync();


await c2.PublishAsync(message);
Assert.AreEqual(0, receivedMessagesCount);
var c1 = await serverAdapter.ConnectTestClient(s, "c1");
var c2 = await serverAdapter.ConnectTestClient(s, "c2");
c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;


await c1.SubscribeAsync(new TopicFilter("a", MqttQualityOfServiceLevel.AtLeastOnce));
await c2.PublishAsync(message);
var message = new MqttApplicationMessageBuilder().WithTopic("a").WithAtLeastOnceQoS().Build();


await Task.Delay(500);
Assert.AreEqual(1, receivedMessagesCount);
await c2.PublishAsync(message);
Assert.AreEqual(0, receivedMessagesCount);


await c1.UnsubscribeAsync("a");
await c2.PublishAsync(message);
await c1.SubscribeAsync(new TopicFilter("a", MqttQualityOfServiceLevel.AtLeastOnce));
await c2.PublishAsync(message);


await Task.Delay(500);
Assert.AreEqual(1, receivedMessagesCount);
await Task.Delay(500);
Assert.AreEqual(1, receivedMessagesCount);

await c1.UnsubscribeAsync("a");
await c2.PublishAsync(message);


await s.StopAsync();
await Task.Delay(500);
Assert.AreEqual(1, receivedMessagesCount);
}
finally
{
await s.StopAsync();
}
await Task.Delay(500); await Task.Delay(500);


Assert.AreEqual(1, receivedMessagesCount); Assert.AreEqual(1, receivedMessagesCount);
@@ -111,22 +133,34 @@ 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();
await s.StartAsync();

var c1 = await serverAdapter.ConnectTestClient(s, "c1");
var services = new ServiceCollection()
.AddLogging()
.AddMqttServer()
.AddSingleton<IMqttServerAdapter>(serverAdapter)
.BuildServiceProvider();


var s = new MqttFactory(services).CreateMqttServer();
var receivedMessagesCount = 0; var receivedMessagesCount = 0;
c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;


var message = new MqttApplicationMessageBuilder().WithTopic("a").WithAtLeastOnceQoS().Build();
await c1.SubscribeAsync(new TopicFilter("a", MqttQualityOfServiceLevel.AtLeastOnce));
try
{
await s.StartAsync();

var c1 = await serverAdapter.ConnectTestClient(s, "c1");


s.PublishAsync(message).Wait();
await Task.Delay(500);
c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;


await s.StopAsync();
var message = new MqttApplicationMessageBuilder().WithTopic("a").WithAtLeastOnceQoS().Build();
await c1.SubscribeAsync(new TopicFilter("a", MqttQualityOfServiceLevel.AtLeastOnce));


s.PublishAsync(message).Wait();
await Task.Delay(500);
}
finally
{
await s.StopAsync();
}
Assert.AreEqual(1, receivedMessagesCount); Assert.AreEqual(1, receivedMessagesCount);
} }


@@ -134,21 +168,33 @@ 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();
await s.StartAsync();

var c1 = await serverAdapter.ConnectTestClient(s, "c1");
await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).Build());
await c1.DisconnectAsync();
var services = new ServiceCollection()
.AddLogging()
.AddMqttServer()
.AddSingleton<IMqttServerAdapter>(serverAdapter)
.BuildServiceProvider();


var c2 = await serverAdapter.ConnectTestClient(s, "c2");
var s = new MqttFactory(services).CreateMqttServer();
var receivedMessagesCount = 0; var receivedMessagesCount = 0;
c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtMostOnce));


await Task.Delay(500);
try
{
await s.StartAsync();

var c1 = await serverAdapter.ConnectTestClient(s, "c1");
await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).Build());
await c1.DisconnectAsync();


await s.StopAsync();
var c2 = await serverAdapter.ConnectTestClient(s, "c2");
c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtMostOnce));

await Task.Delay(500);
}
finally
{
await s.StopAsync();
}


Assert.AreEqual(0, receivedMessagesCount); Assert.AreEqual(0, receivedMessagesCount);
} }
@@ -157,22 +203,34 @@ 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();
await s.StartAsync();
var services = new ServiceCollection()
.AddLogging()
.AddMqttServer()
.AddSingleton<IMqttServerAdapter>(serverAdapter)
.BuildServiceProvider();


var c1 = await serverAdapter.ConnectTestClient(s, "c1");
await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).WithRetainFlag().Build());
await c1.DisconnectAsync();
var s = new MqttFactory(services).CreateMqttServer();


var c2 = await serverAdapter.ConnectTestClient(s, "c2");
var receivedMessagesCount = 0; var receivedMessagesCount = 0;
c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtMostOnce));
try
{
await s.StartAsync();


await Task.Delay(500);
var c1 = await serverAdapter.ConnectTestClient(s, "c1");
await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).WithRetainFlag().Build());
await c1.DisconnectAsync();


await s.StopAsync();
var c2 = await serverAdapter.ConnectTestClient(s, "c2");
c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtMostOnce));


await Task.Delay(500);
}
finally
{
await s.StopAsync();
}
Assert.AreEqual(1, receivedMessagesCount); Assert.AreEqual(1, receivedMessagesCount);
} }


@@ -182,26 +240,32 @@ 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();


var s = new MqttFactory(services).CreateMqttServer(); var s = new MqttFactory(services).CreateMqttServer();
await s.StartAsync();
var receivedMessagesCount = 0;
try
{
await s.StartAsync();


var c1 = await serverAdapter.ConnectTestClient(s, "c1");
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 c1 = await serverAdapter.ConnectTestClient(s, "c1");
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.AtMostOnce));
var c2 = await serverAdapter.ConnectTestClient(s, "c2");
c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtMostOnce));


await Task.Delay(500);
await Task.Delay(500);
}
finally
{
await s.StopAsync();
}


await s.StopAsync();


Assert.AreEqual(0, receivedMessagesCount); Assert.AreEqual(0, receivedMessagesCount);
} }
@@ -214,30 +278,42 @@ 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?
await s.StartAsync();
var s = new MqttFactory(services).CreateMqttServer(options => options.Storage = storage);


var c1 = await serverAdapter.ConnectTestClient(s, "c1");
await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).WithRetainFlag().Build());
await c1.DisconnectAsync();
try
{
await s.StartAsync();


await s.StopAsync();
var c1 = await serverAdapter.ConnectTestClient(s, "c1");
await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).WithRetainFlag().Build());
await c1.DisconnectAsync();
}
finally
{
await s.StopAsync();
}


s = services.GetRequiredService<IMqttServer>(); s = services.GetRequiredService<IMqttServer>();
await s.StartAsync();


var c2 = await serverAdapter.ConnectTestClient(s, "c2");
var receivedMessagesCount = 0; var receivedMessagesCount = 0;
c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtMostOnce));
try
{
await s.StartAsync();


await Task.Delay(500);
var c2 = await serverAdapter.ConnectTestClient(s, "c2");
c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtMostOnce));


await s.StopAsync();
await Task.Delay(500);
}
finally
{
await s.StopAsync();
}


Assert.AreEqual(1, receivedMessagesCount); Assert.AreEqual(1, receivedMessagesCount);
} }
@@ -253,30 +329,37 @@ 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>();
await s.StartAsync();
var s = new MqttFactory(services).CreateMqttServer(options => options.ApplicationMessageInterceptor = Interceptor);
try
{
await s.StartAsync();


var c1 = await serverAdapter.ConnectTestClient(s, "c1");
var c2 = await serverAdapter.ConnectTestClient(s, "c2");
await c2.SubscribeAsync(new TopicFilterBuilder().WithTopic("test").Build());
var c1 = await serverAdapter.ConnectTestClient(s, "c1");
var c2 = await serverAdapter.ConnectTestClient(s, "c2");
await c2.SubscribeAsync(new TopicFilterBuilder().WithTopic("test").Build());


var isIntercepted = false;
c2.ApplicationMessageReceived += (sender, args) =>
{
isIntercepted = string.Compare("extended", Encoding.UTF8.GetString(args.ApplicationMessage.Payload), StringComparison.Ordinal) == 0;
};
var isIntercepted = false;
c2.ApplicationMessageReceived += (sender, args) =>
{
isIntercepted = string.Compare("extended", Encoding.UTF8.GetString(args.ApplicationMessage.Payload), StringComparison.Ordinal) == 0;
};


var m = new MqttApplicationMessageBuilder().WithTopic("test").Build();
await c1.PublishAsync(m);
await c1.DisconnectAsync();
var m = new MqttApplicationMessageBuilder().WithTopic("test").Build();
await c1.PublishAsync(m);
await c1.DisconnectAsync();


await Task.Delay(500);
await Task.Delay(500);


Assert.IsTrue(isIntercepted);
Assert.IsTrue(isIntercepted);
}
finally
{
await s.StopAsync();
}
} }


private class TestStorage : IMqttServerStorage private class TestStorage : IMqttServerStorage
@@ -305,28 +388,34 @@ namespace MQTTnet.Core.Tests
var serverAdapter = new TestMqttServerAdapter(); var serverAdapter = new TestMqttServerAdapter();
var services = new ServiceCollection() var services = new ServiceCollection()
.AddMqttServer() .AddMqttServer()
.AddLogging()
.AddSingleton<IMqttServerAdapter>(serverAdapter) .AddSingleton<IMqttServerAdapter>(serverAdapter)
.BuildServiceProvider(); .BuildServiceProvider();


var s = services.GetRequiredService<IMqttServer>(); var s = services.GetRequiredService<IMqttServer>();
await s.StartAsync();

var c1 = await serverAdapter.ConnectTestClient(s, "c1");
var c2 = await serverAdapter.ConnectTestClient(s, "c2");

var receivedMessagesCount = 0; var receivedMessagesCount = 0;
c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
try
{
await s.StartAsync();


await c1.SubscribeAsync(new TopicFilterBuilder().WithTopic(topicFilter).WithQualityOfServiceLevel(filterQualityOfServiceLevel).Build());
await c2.PublishAsync(new MqttApplicationMessageBuilder().WithTopic(topic).WithPayload(new byte[0]).WithQualityOfServiceLevel(qualityOfServiceLevel).Build());
var c1 = await serverAdapter.ConnectTestClient(s, "c1");
var c2 = await serverAdapter.ConnectTestClient(s, "c2");


await Task.Delay(500);
await c1.UnsubscribeAsync(topicFilter);
c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;


await Task.Delay(500);
await c1.SubscribeAsync(new TopicFilterBuilder().WithTopic(topicFilter).WithQualityOfServiceLevel(filterQualityOfServiceLevel).Build());
await c2.PublishAsync(new MqttApplicationMessageBuilder().WithTopic(topic).WithPayload(new byte[0]).WithQualityOfServiceLevel(qualityOfServiceLevel).Build());


await s.StopAsync();
await Task.Delay(500);
await c1.UnsubscribeAsync(topicFilter);


await Task.Delay(500);
}
finally
{
await s.StopAsync();
}
Assert.AreEqual(expectedReceivedMessagesCount, receivedMessagesCount); Assert.AreEqual(expectedReceivedMessagesCount, receivedMessagesCount);
} }
} }


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

+ 1
- 1
Tests/MQTTnet.TestApp.AspNetCore2/MQTTnet.TestApp.AspNetCore2.csproj View File

@@ -14,7 +14,7 @@
</ItemGroup> </ItemGroup>


<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\Frameworks\MQTTnet.AspnetCore\MQTTnet.AspnetCore.csproj" />
<ProjectReference Include="..\..\Frameworks\MQTTnet.AspnetCore\MQTTnet.AspNetCore.csproj" />
</ItemGroup> </ItemGroup>


<ItemGroup> <ItemGroup>


+ 1
- 1
Tests/MQTTnet.TestApp.AspNetCore2/Startup.cs View File

@@ -6,7 +6,7 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using MQTTnet.AspnetCore;
using MQTTnet.AspNetCore;
using MQTTnet.Core; using MQTTnet.Core;
using MQTTnet.Core.Client; using MQTTnet.Core.Client;




Loading…
Cancel
Save