Selaa lähdekoodia

Fix unit tests

release/3.x.x
Christian Kratky 7 vuotta sitten
vanhempi
commit
8fa91736b6
7 muutettua tiedostoa jossa 31 lisäystä ja 62 poistoa
  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 Näytä tiedosto

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

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

public IMqttServer CreateMqttServer()


+ 2
- 1
Frameworks/MQTTnet.NetStandard/ServiceCollectionExtensions.cs Näytä tiedosto

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

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

return services;


+ 1
- 1
Frameworks/MQTTnet.UniversalWindows/MqttFactory.cs Näytä tiedosto

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

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


+ 2
- 1
Frameworks/MQTTnet.UniversalWindows/ServiceCollectionExtensions.cs Näytä tiedosto

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

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

return services;


+ 1
- 1
MQTTnet.Core/Client/IMqttClientFactory.cs Näytä tiedosto

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

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

+ 1
- 0
MQTTnet.Core/Server/MqttServer.cs Näytä tiedosto

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

_adapters = adapters.ToList();

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


+ 22
- 56
Tests/MQTTnet.Core.Tests/MqttServerTests.cs Näytä tiedosto

@@ -5,7 +5,6 @@ using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MQTTnet.Core.Adapter;
using MQTTnet.Core.Client;
using MQTTnet.Core.Packets;
using MQTTnet.Core.Protocol;
using MQTTnet.Core.Server;
using Microsoft.Extensions.DependencyInjection;
@@ -52,12 +51,7 @@ namespace MQTTnet.Core.Tests
public async Task MqttServer_WillMessage()
{
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();

var willMessage = new MqttApplicationMessageBuilder().WithTopic("My/last/will").WithAtMostOnceQoS().Build();
@@ -81,12 +75,7 @@ namespace MQTTnet.Core.Tests
public async Task MqttServer_Unsubscribe()
{
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();

var c1 = await serverAdapter.ConnectTestClient(s, "c1");
@@ -122,12 +111,7 @@ namespace MQTTnet.Core.Tests
public async Task MqttServer_Publish()
{
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();

var c1 = await serverAdapter.ConnectTestClient(s, "c1");
@@ -150,22 +134,17 @@ namespace MQTTnet.Core.Tests
public async Task MqttServer_NoRetainedMessage()
{
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();

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();

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

await Task.Delay(500);

@@ -178,22 +157,17 @@ namespace MQTTnet.Core.Tests
public async Task MqttServer_RetainedMessage()
{
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();

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();

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

await Task.Delay(500);

@@ -206,23 +180,18 @@ namespace MQTTnet.Core.Tests
public async Task MqttServer_ClearRetainedMessage()
{
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();

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();

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

await Task.Delay(500);

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

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

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

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

await Task.Delay(500);

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



Ladataan…
Peruuta
Tallenna