mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-19 02:53:06 +08:00
Add UnitTests.
This commit is contained in:
@@ -20,6 +20,61 @@ namespace MQTTnet.Tests
|
||||
[TestClass]
|
||||
public class Client_Tests
|
||||
{
|
||||
[TestMethod]
|
||||
public async Task Send_Reply_In_Message_Handler()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServerAsync();
|
||||
var client1 = await testEnvironment.ConnectClientAsync();
|
||||
var client2 = await testEnvironment.ConnectClientAsync();
|
||||
|
||||
await client1.SubscribeAsync("#");
|
||||
await client2.SubscribeAsync("#");
|
||||
|
||||
var replyReceived = false;
|
||||
|
||||
client1.UseApplicationMessageReceivedHandler(async c =>
|
||||
{
|
||||
if (c.ApplicationMessage.Topic == "reply")
|
||||
{
|
||||
replyReceived = true;
|
||||
}
|
||||
});
|
||||
|
||||
client2.UseApplicationMessageReceivedHandler(async c =>{ await client2.PublishAsync("reply", null, MqttQualityOfServiceLevel.AtLeastOnce); });
|
||||
|
||||
await client1.PublishAsync("request", null, MqttQualityOfServiceLevel.AtLeastOnce);
|
||||
|
||||
SpinWait.SpinUntil(() => replyReceived, TimeSpan.FromSeconds(10));
|
||||
|
||||
Assert.IsTrue(replyReceived);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Reconnect()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment())
|
||||
{
|
||||
var server = await testEnvironment.StartServerAsync();
|
||||
var client = await testEnvironment.ConnectClientAsync();
|
||||
|
||||
await Task.Delay(500);
|
||||
Assert.IsTrue(client.IsConnected);
|
||||
|
||||
await server.StopAsync();
|
||||
await Task.Delay(500);
|
||||
Assert.IsFalse(client.IsConnected);
|
||||
|
||||
await server.StartAsync(new MqttServerOptionsBuilder().WithDefaultEndpointPort(testEnvironment.ServerPort).Build());
|
||||
await Task.Delay(500);
|
||||
|
||||
await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1", testEnvironment.ServerPort).Build());
|
||||
Assert.IsTrue(client.IsConnected);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task PacketIdentifier_In_Publish_Result()
|
||||
{
|
||||
@@ -89,6 +144,8 @@ namespace MQTTnet.Tests
|
||||
{
|
||||
}
|
||||
|
||||
await Task.Delay(500);
|
||||
|
||||
Assert.IsNotNull(ex);
|
||||
Assert.IsInstanceOfType(ex, typeof(MqttCommunicationException));
|
||||
Assert.IsInstanceOfType(ex.InnerException, typeof(SocketException));
|
||||
|
||||
Reference in New Issue
Block a user