Browse Source

Fix unit tests

release/3.x.x
Christian Kratky 7 years ago
parent
commit
8fa91736b6
7 changed files with 31 additions and 62 deletions
  1. +2
    -2
      Frameworks/MQTTnet.NetStandard/MqttFactory.cs
  2. +2
    -1
      Frameworks/MQTTnet.NetStandard/ServiceCollectionExtensions.cs
  3. +1
    -1
      Frameworks/MQTTnet.UniversalWindows/MqttFactory.cs
  4. +2
    -1
      Frameworks/MQTTnet.UniversalWindows/ServiceCollectionExtensions.cs
  5. +1
    -1
      MQTTnet.Core/Client/IMqttClientFactory.cs
  6. +1
    -0
      MQTTnet.Core/Server/MqttServer.cs
  7. +22
    -56
      Tests/MQTTnet.Core.Tests/MqttServerTests.cs

+ 2
- 2
Frameworks/MQTTnet.NetStandard/MqttFactory.cs View File

@@ -100,9 +100,9 @@ namespace MQTTnet
return _serviceProvider.GetRequiredService<IMqttClient>(); return _serviceProvider.GetRequiredService<IMqttClient>();
} }


public ManagedMqttClient CreateManagedMqttClient()
public IManagedMqttClient CreateManagedMqttClient()
{ {
return _serviceProvider.GetRequiredService<ManagedMqttClient>();
return _serviceProvider.GetRequiredService<IManagedMqttClient>();
} }


public IMqttServer CreateMqttServer() public IMqttServer CreateMqttServer()


+ 2
- 1
Frameworks/MQTTnet.NetStandard/ServiceCollectionExtensions.cs View File

@@ -52,8 +52,9 @@ namespace MQTTnet


services.AddTransient<IMqttClient, MqttClient>(); services.AddTransient<IMqttClient, MqttClient>();
services.AddTransient<MqttClient>(); services.AddTransient<MqttClient>();
services.AddTransient<IMqttPacketSerializer, MqttPacketSerializer>();
services.AddTransient<IManagedMqttClient, ManagedMqttClient>();
services.AddTransient<ManagedMqttClient>(); services.AddTransient<ManagedMqttClient>();
services.AddTransient<IMqttPacketSerializer, MqttPacketSerializer>();
services.AddTransient<MqttPacketDispatcher>(); services.AddTransient<MqttPacketDispatcher>();


return services; return services;


+ 1
- 1
Frameworks/MQTTnet.UniversalWindows/MqttFactory.cs View File

@@ -95,7 +95,7 @@ namespace MQTTnet
return _serviceProvider.GetRequiredService<IMqttClient>(); return _serviceProvider.GetRequiredService<IMqttClient>();
} }


public ManagedMqttClient CreateManagedMqttClient()
public IManagedMqttClient CreateManagedMqttClient()
{ {
return _serviceProvider.GetRequiredService<ManagedMqttClient>(); return _serviceProvider.GetRequiredService<ManagedMqttClient>();
} }


+ 2
- 1
Frameworks/MQTTnet.UniversalWindows/ServiceCollectionExtensions.cs View File

@@ -46,8 +46,9 @@ namespace MQTTnet


services.AddTransient<IMqttClient, MqttClient>(); services.AddTransient<IMqttClient, MqttClient>();
services.AddTransient<MqttClient>(); services.AddTransient<MqttClient>();
services.AddTransient<IMqttPacketSerializer, MqttPacketSerializer>();
services.AddTransient<IManagedMqttClient, ManagedMqttClient>();
services.AddTransient<ManagedMqttClient>(); services.AddTransient<ManagedMqttClient>();
services.AddTransient<IMqttPacketSerializer, MqttPacketSerializer>();
services.AddTransient<MqttPacketDispatcher>(); services.AddTransient<MqttPacketDispatcher>();


return services; return services;


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

@@ -6,6 +6,6 @@ namespace MQTTnet.Core.Client
{ {
IMqttClient CreateMqttClient(); IMqttClient CreateMqttClient();


ManagedMqttClient CreateManagedMqttClient();
IManagedMqttClient CreateManagedMqttClient();
} }
} }

+ 1
- 0
MQTTnet.Core/Server/MqttServer.cs View File

@@ -28,6 +28,7 @@ namespace MQTTnet.Core.Server
{ {
throw new ArgumentNullException(nameof(adapters)); throw new ArgumentNullException(nameof(adapters));
} }

_adapters = adapters.ToList(); _adapters = adapters.ToList();


_clientSessionsManager.ApplicationMessageReceived += (s, e) => ApplicationMessageReceived?.Invoke(s, e); _clientSessionsManager.ApplicationMessageReceived += (s, e) => ApplicationMessageReceived?.Invoke(s, e);


+ 22
- 56
Tests/MQTTnet.Core.Tests/MqttServerTests.cs View File

@@ -5,7 +5,6 @@ using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using MQTTnet.Core.Adapter; using MQTTnet.Core.Adapter;
using MQTTnet.Core.Client; using MQTTnet.Core.Client;
using MQTTnet.Core.Packets;
using MQTTnet.Core.Protocol; using MQTTnet.Core.Protocol;
using MQTTnet.Core.Server; using MQTTnet.Core.Server;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@@ -52,12 +51,7 @@ namespace MQTTnet.Core.Tests
public async Task MqttServer_WillMessage() public async Task MqttServer_WillMessage()
{ {
var serverAdapter = new TestMqttServerAdapter(); var serverAdapter = new TestMqttServerAdapter();
var services = new ServiceCollection()
.AddMqttServer()
.AddSingleton<IMqttServerAdapter>(serverAdapter)
.BuildServiceProvider();

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


var willMessage = new MqttApplicationMessageBuilder().WithTopic("My/last/will").WithAtMostOnceQoS().Build(); var willMessage = new MqttApplicationMessageBuilder().WithTopic("My/last/will").WithAtMostOnceQoS().Build();
@@ -81,12 +75,7 @@ namespace MQTTnet.Core.Tests
public async Task MqttServer_Unsubscribe() public async Task MqttServer_Unsubscribe()
{ {
var serverAdapter = new TestMqttServerAdapter(); var serverAdapter = new TestMqttServerAdapter();
var services = new ServiceCollection()
.AddMqttServer()
.AddSingleton<IMqttServerAdapter>(serverAdapter)
.BuildServiceProvider();

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


var c1 = await serverAdapter.ConnectTestClient(s, "c1"); var c1 = await serverAdapter.ConnectTestClient(s, "c1");
@@ -122,12 +111,7 @@ namespace MQTTnet.Core.Tests
public async Task MqttServer_Publish() public async Task MqttServer_Publish()
{ {
var serverAdapter = new TestMqttServerAdapter(); var serverAdapter = new TestMqttServerAdapter();
var services = new ServiceCollection()
.AddMqttServer()
.AddSingleton<IMqttServerAdapter>(serverAdapter)
.BuildServiceProvider();

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


var c1 = await serverAdapter.ConnectTestClient(s, "c1"); var c1 = await serverAdapter.ConnectTestClient(s, "c1");
@@ -150,22 +134,17 @@ namespace MQTTnet.Core.Tests
public async Task MqttServer_NoRetainedMessage() public async Task MqttServer_NoRetainedMessage()
{ {
var serverAdapter = new TestMqttServerAdapter(); var serverAdapter = new TestMqttServerAdapter();
var services = new ServiceCollection()
.AddMqttServer()
.AddSingleton<IMqttServerAdapter>(serverAdapter)
.BuildServiceProvider();

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


var c1 = await serverAdapter.ConnectTestClient(s, "c1"); 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(); await c1.DisconnectAsync();


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


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


@@ -178,22 +157,17 @@ namespace MQTTnet.Core.Tests
public async Task MqttServer_RetainedMessage() public async Task MqttServer_RetainedMessage()
{ {
var serverAdapter = new TestMqttServerAdapter(); var serverAdapter = new TestMqttServerAdapter();
var services = new ServiceCollection()
.AddMqttServer()
.AddSingleton<IMqttServerAdapter>(serverAdapter)
.BuildServiceProvider();

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


var c1 = await serverAdapter.ConnectTestClient(s, "c1"); 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 c1.DisconnectAsync();


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


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


@@ -206,23 +180,18 @@ namespace MQTTnet.Core.Tests
public async Task MqttServer_ClearRetainedMessage() public async Task MqttServer_ClearRetainedMessage()
{ {
var serverAdapter = new TestMqttServerAdapter(); var serverAdapter = new TestMqttServerAdapter();
var services = new ServiceCollection()
.AddMqttServer()
.AddSingleton<IMqttServerAdapter>(serverAdapter)
.BuildServiceProvider();

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


var c1 = await serverAdapter.ConnectTestClient(s, "c1"); 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(); await c1.DisconnectAsync();


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


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


@@ -238,15 +207,15 @@ namespace MQTTnet.Core.Tests


var serverAdapter = new TestMqttServerAdapter(); var serverAdapter = new TestMqttServerAdapter();
var services = new ServiceCollection() var services = new ServiceCollection()
.AddMqttServer()
.AddMqttServer(options => options.Storage = storage)
.AddSingleton<IMqttServerAdapter>(serverAdapter) .AddSingleton<IMqttServerAdapter>(serverAdapter)
.BuildServiceProvider(); .BuildServiceProvider();


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


var c1 = await serverAdapter.ConnectTestClient(s, "c1"); 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 c1.DisconnectAsync();


await s.StopAsync(); await s.StopAsync();
@@ -257,7 +226,7 @@ namespace MQTTnet.Core.Tests
var c2 = await serverAdapter.ConnectTestClient(s, "c2"); var c2 = await serverAdapter.ConnectTestClient(s, "c2");
var receivedMessagesCount = 0; var receivedMessagesCount = 0;
c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++; c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtLeastOnce));
await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtMostOnce));


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


@@ -269,18 +238,15 @@ namespace MQTTnet.Core.Tests
[TestMethod] [TestMethod]
public async Task MqttServer_InterceptMessage() 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 serverAdapter = new TestMqttServerAdapter();
var services = new ServiceCollection() var services = new ServiceCollection()
.AddMqttServer()
.AddMqttServer(options => options.ApplicationMessageInterceptor = Interceptor)
.AddSingleton<IMqttServerAdapter>(serverAdapter) .AddSingleton<IMqttServerAdapter>(serverAdapter)
.BuildServiceProvider(); .BuildServiceProvider();




Loading…
Cancel
Save