mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-26 16:24:10 +08:00
Add Unit Tests.
This commit is contained in:
@@ -96,28 +96,32 @@ namespace MQTTnet.Tests.Mockups
|
|||||||
return ConnectClient(Factory.CreateClientOptionsBuilder().WithProtocolVersion(_protocolVersion));
|
return ConnectClient(Factory.CreateClientOptionsBuilder().WithProtocolVersion(_protocolVersion));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<MqttClient> ConnectClient(Action<MqttClientOptionsBuilder> optionsBuilder, TimeSpan timeout = default)
|
public async Task<MqttClient> ConnectClient(Action<MqttClientOptionsBuilder> configureOptions, TimeSpan timeout = default)
|
||||||
{
|
{
|
||||||
if (optionsBuilder == null)
|
if (configureOptions == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(optionsBuilder));
|
throw new ArgumentNullException(nameof(configureOptions));
|
||||||
}
|
}
|
||||||
|
|
||||||
var options = Factory.CreateClientOptionsBuilder().WithProtocolVersion(_protocolVersion).WithTcpServer("127.0.0.1", ServerPort);
|
// Start with initial default values.
|
||||||
|
var optionsBuilder = Factory.CreateClientOptionsBuilder().WithProtocolVersion(_protocolVersion).WithTcpServer("127.0.0.1", ServerPort);
|
||||||
|
|
||||||
optionsBuilder.Invoke(options);
|
// Let the caller override settings. Do not touch the options after this.
|
||||||
|
configureOptions.Invoke(optionsBuilder);
|
||||||
|
|
||||||
|
var options = optionsBuilder.Build();
|
||||||
|
|
||||||
var client = CreateClient();
|
var client = CreateClient();
|
||||||
|
|
||||||
if (timeout == TimeSpan.Zero)
|
if (timeout == TimeSpan.Zero)
|
||||||
{
|
{
|
||||||
await client.ConnectAsync(options.Build()).ConfigureAwait(false);
|
await client.ConnectAsync(options).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
using (var timeoutToken = new CancellationTokenSource(timeout))
|
using (var timeoutToken = new CancellationTokenSource(timeout))
|
||||||
{
|
{
|
||||||
await client.ConnectAsync(options.Build(), timeoutToken.Token).ConfigureAwait(false);
|
await client.ConnectAsync(options, timeoutToken.Token).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
using MQTTnet.Client;
|
||||||
|
using MQTTnet.Formatter;
|
||||||
|
|
||||||
|
namespace MQTTnet.Tests.Server
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public sealed class Cross_Version_Tests : BaseTestClass
|
||||||
|
{
|
||||||
|
[TestMethod]
|
||||||
|
public async Task Send_V311_Receive_V500()
|
||||||
|
{
|
||||||
|
using (var testEnvironment = CreateTestEnvironment())
|
||||||
|
{
|
||||||
|
await testEnvironment.StartServer();
|
||||||
|
|
||||||
|
var receiver = await testEnvironment.ConnectClient(o => o.WithProtocolVersion(MqttProtocolVersion.V500));
|
||||||
|
var receivedApplicationMessages = testEnvironment.CreateApplicationMessageHandler(receiver);
|
||||||
|
await receiver.SubscribeAsync("#");
|
||||||
|
|
||||||
|
var sender = await testEnvironment.ConnectClient();
|
||||||
|
|
||||||
|
var applicationMessage = new MqttApplicationMessageBuilder().WithTopic("My/Message").WithPayload("My_Payload").Build();
|
||||||
|
await sender.PublishAsync(applicationMessage);
|
||||||
|
|
||||||
|
await LongTestDelay();
|
||||||
|
|
||||||
|
Assert.AreEqual(1, receivedApplicationMessages.ReceivedEventArgs.Count);
|
||||||
|
Assert.AreEqual("My/Message", receivedApplicationMessages.ReceivedEventArgs.First().ApplicationMessage.Topic);
|
||||||
|
Assert.AreEqual("My_Payload", receivedApplicationMessages.ReceivedEventArgs.First().ApplicationMessage.ConvertPayloadToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task Send_V500_Receive_V311()
|
||||||
|
{
|
||||||
|
using (var testEnvironment = CreateTestEnvironment(MqttProtocolVersion.V500))
|
||||||
|
{
|
||||||
|
await testEnvironment.StartServer();
|
||||||
|
|
||||||
|
var receiver = await testEnvironment.ConnectClient(o => o.WithProtocolVersion(MqttProtocolVersion.V311));
|
||||||
|
var receivedApplicationMessages = testEnvironment.CreateApplicationMessageHandler(receiver);
|
||||||
|
await receiver.SubscribeAsync("#");
|
||||||
|
|
||||||
|
var sender = await testEnvironment.ConnectClient();
|
||||||
|
|
||||||
|
var applicationMessage = new MqttApplicationMessageBuilder().WithTopic("My/Message")
|
||||||
|
.WithPayload("My_Payload")
|
||||||
|
.WithUserProperty("A", "B")
|
||||||
|
.WithResponseTopic("Response")
|
||||||
|
.WithCorrelationData(Encoding.UTF8.GetBytes("Correlation"))
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
await sender.PublishAsync(applicationMessage);
|
||||||
|
|
||||||
|
await LongTestDelay();
|
||||||
|
|
||||||
|
Assert.AreEqual(1, receivedApplicationMessages.ReceivedEventArgs.Count);
|
||||||
|
Assert.AreEqual("My/Message", receivedApplicationMessages.ReceivedEventArgs.First().ApplicationMessage.Topic);
|
||||||
|
Assert.AreEqual("My_Payload", receivedApplicationMessages.ReceivedEventArgs.First().ApplicationMessage.ConvertPayloadToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user