-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
212 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} | ||
} |