Skip to content

Commit

Permalink
Add some unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Dec 4, 2024
1 parent 13e8190 commit 4bfd77d
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 1 deletion.
49 changes: 49 additions & 0 deletions Source/MQTTnet.Tests/ASP/MqttBufferWriterPoolTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MQTTnet.AspNetCore;
using MQTTnet.Formatter;
using MQTTnet.Server;
using System;
using System.Threading.Tasks;

namespace MQTTnet.Tests.ASP
{
[TestClass]
public class MqttBufferWriterPoolTest
{
[TestMethod]
public async Task RentReturnTest()
{
var services = new ServiceCollection();
services.AddMqttServer().ConfigureMqttBufferWriterPool(p =>
{
p.PoolingItemMaxLifeTime = TimeSpan.FromSeconds(1d);
});

var s = services.BuildServiceProvider();
var pool = s.GetRequiredService<MqttBufferWriterPool>();
var options = s.GetRequiredService<MqttServerOptions>();

var bufferWriter = pool.Rent();
Assert.AreEqual(0, pool.Count);

pool.Return(bufferWriter);
Assert.AreEqual(1, pool.Count);

bufferWriter = pool.Rent();
Assert.AreEqual(0, pool.Count);

await Task.Delay(TimeSpan.FromSeconds(2d));

pool.Return(bufferWriter);
Assert.AreEqual(0, pool.Count);

MqttBufferWriter writer = bufferWriter;
writer.Seek(options.WriterBufferSize + 1);
Assert.IsTrue(bufferWriter.BufferSize > options.WriterBufferSize);

pool.Return(bufferWriter);
Assert.AreEqual(1, pool.Count);
}
}
}
31 changes: 31 additions & 0 deletions Source/MQTTnet.Tests/ASP/MqttBuilderTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MQTTnet.AspNetCore;
using MQTTnet.Diagnostics.Logger;

namespace MQTTnet.Tests.ASP
{
[TestClass]
public class MqttBuilderTest
{
[TestMethod]
public void UseMqttNetNullLoggerTest()
{
var services = new ServiceCollection();
services.AddMqttServer().UseMqttNetNullLogger();
var s = services.BuildServiceProvider();
var logger = s.GetRequiredService<IMqttNetLogger>();
Assert.IsInstanceOfType<MqttNetNullLogger>(logger);
}

[TestMethod]
public void UseAspNetCoreMqttNetLoggerTest()
{
var services = new ServiceCollection();
services.AddMqttServer().UseAspNetCoreMqttNetLogger();
var s = services.BuildServiceProvider();
var logger = s.GetRequiredService<IMqttNetLogger>();
Assert.IsInstanceOfType<AspNetCoreMqttNetLogger>(logger);
}
}
}
50 changes: 50 additions & 0 deletions Source/MQTTnet.Tests/ASP/MqttClientBuilderTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MQTTnet.Adapter;
using MQTTnet.AspNetCore;
using MQTTnet.Implementations;

namespace MQTTnet.Tests.ASP
{
[TestClass]
public class MqttClientBuilderTest
{
[TestMethod]
public void AddMqttClientTest()
{
var services = new ServiceCollection();
services.AddMqttClient();
var s = services.BuildServiceProvider();

var mqttClientFactory1 = s.GetRequiredService<IMqttClientFactory>();
var mqttClientFactory2 = s.GetRequiredService<MqttClientFactory>();
Assert.IsTrue(ReferenceEquals(mqttClientFactory2, mqttClientFactory2));

Assert.IsInstanceOfType<AspNetCoreMqttClientFactory>(mqttClientFactory1);
Assert.IsInstanceOfType<MqttClientFactory>(mqttClientFactory1);
}

[TestMethod]
public void UseMQTTnetMqttClientAdapterFactoryTest()
{
var services = new ServiceCollection();
services.AddMqttClient().UseMQTTnetMqttClientAdapterFactory();
var s = services.BuildServiceProvider();
var adapterFactory = s.GetRequiredService<IMqttClientAdapterFactory>();

Assert.IsInstanceOfType<MqttClientAdapterFactory>(adapterFactory);
}


[TestMethod]
public void UseAspNetCoreMqttClientAdapterFactoryTest()
{
var services = new ServiceCollection();
services.AddMqttClient().UseAspNetCoreMqttClientAdapterFactory();
var s = services.BuildServiceProvider();
var adapterFactory = s.GetRequiredService<IMqttClientAdapterFactory>();

Assert.IsInstanceOfType<AspNetCoreMqttClientAdapterFactory>(adapterFactory);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace MQTTnet.Tests.ASP;

[TestClass]
public sealed class ReaderExtensionsTest
public sealed class MqttPacketFormatterAdapterExtensionsTest
{
[TestMethod]
public void TestTryDeserialize()
Expand Down
81 changes: 81 additions & 0 deletions Source/MQTTnet.Tests/ASP/MqttServerBuilderTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MQTTnet.AspNetCore;
using MQTTnet.Server;
using MQTTnet.Server.Internal.Adapter;
using System.Collections.Generic;
using System.Linq;

namespace MQTTnet.Tests.ASP
{
[TestClass]
public class MqttServerBuilderTest
{
[TestMethod]
public void AddMqttServerTest()
{
var services = new ServiceCollection();
services.AddMqttServer();
var s = services.BuildServiceProvider();

var mqttServer1 = s.GetRequiredService<MqttServer>();
var mqttServer2 = s.GetRequiredService<MqttServer>();
Assert.IsInstanceOfType<AspNetCoreMqttServer>(mqttServer1);
Assert.AreEqual(mqttServer1, mqttServer2);
}

[TestMethod]
public void ConfigureMqttServerTest()
{
const int TcpKeepAliveTime1 = 19;
const int TcpKeepAliveTime2 = 20;

var services = new ServiceCollection();
services.AddMqttServer().ConfigureMqttServer(
b => b.WithTcpKeepAliveTime(TcpKeepAliveTime1),
o =>
{
Assert.AreEqual(TcpKeepAliveTime1, o.DefaultEndpointOptions.TcpKeepAliveTime);
o.DefaultEndpointOptions.TcpKeepAliveTime = TcpKeepAliveTime2;
});

var s = services.BuildServiceProvider();
var options = s.GetRequiredService<MqttServerOptions>();
Assert.AreEqual(TcpKeepAliveTime2, options.DefaultEndpointOptions.TcpKeepAliveTime);
}

[TestMethod]
public void ConfigureMqttServerStopTest()
{
const string ReasonString1 = "ReasonString1";
const string ReasonString2 = "ReasonString2";

var services = new ServiceCollection();
services.AddMqttServer().ConfigureMqttServerStop(
b => b.WithDefaultClientDisconnectOptions(c => c.WithReasonString(ReasonString1)),
o =>
{
Assert.AreEqual(ReasonString1, o.DefaultClientDisconnectOptions.ReasonString);
o.DefaultClientDisconnectOptions.ReasonString = ReasonString2;
});

var s = services.BuildServiceProvider();
var options = s.GetRequiredService<MqttServerStopOptions>();
Assert.AreEqual(ReasonString2, options.DefaultClientDisconnectOptions.ReasonString);
}

[TestMethod]
public void AddMqttServerAdapterTest()
{
var services = new ServiceCollection();
services.AddMqttServer().AddMqttServerAdapter<MqttTcpServerAdapter>();
services.AddMqttServer().AddMqttServerAdapter<MqttTcpServerAdapter>();

var s = services.BuildServiceProvider();
var adapters = s.GetRequiredService<IEnumerable<IMqttServerAdapter>>().ToArray();
Assert.AreEqual(2, adapters.Length);
Assert.IsInstanceOfType<AspNetCoreMqttServerAdapter>(adapters[0]);
Assert.IsInstanceOfType<MqttTcpServerAdapter>(adapters[1]);
}
}
}

0 comments on commit 4bfd77d

Please sign in to comment.