|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using MQTTnet.Client;
- using MQTTnet.Client.Options;
- using MQTTnet.Client.Publishing;
- using MQTTnet.Client.Receiving;
- using MQTTnet.Client.Subscribing;
- using MQTTnet.Client.Unsubscribing;
- using MQTTnet.Formatter;
- using MQTTnet.Protocol;
- using MQTTnet.Server;
-
- namespace MQTTnet.Tests.MQTTv5
- {
- [TestClass]
- public class Client_Tests
- {
- [TestMethod]
- public async Task Connect()
- {
- var server = new MqttFactory().CreateMqttServer();
- var client = new MqttFactory().CreateMqttClient();
-
- try
- {
- await server.StartAsync(new MqttServerOptions());
- await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
- }
- finally
- {
- await server.StopAsync();
- }
- }
-
- [TestMethod]
- public async Task Connect_And_Disconnect()
- {
- var server = new MqttFactory().CreateMqttServer();
- var client = new MqttFactory().CreateMqttClient();
-
- try
- {
- await server.StartAsync(new MqttServerOptions());
-
- await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
- await client.DisconnectAsync();
- }
- finally
- {
- await server.StopAsync();
- }
- }
-
- [TestMethod]
- public async Task Subscribe()
- {
- var server = new MqttFactory().CreateMqttServer();
- var client = new MqttFactory().CreateMqttClient();
-
- try
- {
- await server.StartAsync(new MqttServerOptions());
-
- await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
- var result = await client.SubscribeAsync("a", MqttQualityOfServiceLevel.AtLeastOnce);
- await client.DisconnectAsync();
-
- Assert.AreEqual(1, result.Items.Count);
- Assert.AreEqual(MqttClientSubscribeResultCode.GrantedQoS1, result.Items[0].ResultCode);
- }
- finally
- {
- await server.StopAsync();
- }
- }
-
- [TestMethod]
- public async Task Unsubscribe()
- {
- var server = new MqttFactory().CreateMqttServer();
- var client = new MqttFactory().CreateMqttClient();
-
- try
- {
- await server.StartAsync(new MqttServerOptions());
-
- await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
- await client.SubscribeAsync("a");
- var result = await client.UnsubscribeAsync("a");
- await client.DisconnectAsync();
-
- Assert.AreEqual(1, result.Items.Count);
- Assert.AreEqual(MqttClientUnsubscribeResultCode.Success, result.Items[0].ReasonCode);
- }
- finally
- {
- await server.StopAsync();
- }
- }
-
- [TestMethod]
- public async Task Publish_QoS_0()
- {
- var server = new MqttFactory().CreateMqttServer();
- var client = new MqttFactory().CreateMqttClient();
-
- try
- {
- await server.StartAsync(new MqttServerOptions());
-
- await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
- var result = await client.PublishAsync("a", "b");
- await client.DisconnectAsync();
-
- Assert.AreEqual(MqttClientPublishReasonCode.Success, result.ReasonCode);
- }
- finally
- {
- await server.StopAsync();
- }
- }
-
- [TestMethod]
- public async Task Publish_QoS_1()
- {
- var server = new MqttFactory().CreateMqttServer();
- var client = new MqttFactory().CreateMqttClient();
-
- try
- {
- await server.StartAsync(new MqttServerOptions());
-
- await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
- var result = await client.PublishAsync("a", "b", MqttQualityOfServiceLevel.AtLeastOnce);
- await client.DisconnectAsync();
-
- Assert.AreEqual(MqttClientPublishReasonCode.Success, result.ReasonCode);
- }
- finally
- {
- await server.StopAsync();
- }
- }
-
- [TestMethod]
- public async Task Publish_QoS_2()
- {
- var server = new MqttFactory().CreateMqttServer();
- var client = new MqttFactory().CreateMqttClient();
-
- try
- {
- await server.StartAsync(new MqttServerOptions());
-
- await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
- var result = await client.PublishAsync("a", "b", MqttQualityOfServiceLevel.ExactlyOnce);
- await client.DisconnectAsync();
-
- Assert.AreEqual(MqttClientPublishReasonCode.Success, result.ReasonCode);
- }
- finally
- {
- await server.StopAsync();
- }
- }
-
- [TestMethod]
- public async Task Publish_With_Properties()
- {
- var server = new MqttFactory().CreateMqttServer();
- var client = new MqttFactory().CreateMqttClient();
-
- try
- {
- await server.StartAsync(new MqttServerOptions());
-
- var applicationMessage = new MqttApplicationMessageBuilder()
- .WithTopic("Hello")
- .WithPayload("World")
- .WithAtMostOnceQoS()
- .WithUserProperty("x", "1")
- .WithUserProperty("y", "2")
- .WithResponseTopic("response")
- .WithContentType("text")
- .WithMessageExpiryInterval(50)
- .WithCorrelationData(new byte[12])
- .WithTopicAlias(2)
- .Build();
-
- await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
- var result = await client.PublishAsync(applicationMessage);
- await client.DisconnectAsync();
-
- Assert.AreEqual(MqttClientPublishReasonCode.Success, result.ReasonCode);
- }
- finally
- {
- await server.StopAsync();
- }
- }
-
- [TestMethod]
- public async Task Subscribe_And_Publish()
- {
- var server = new MqttFactory().CreateMqttServer();
- var client1 = new MqttFactory().CreateMqttClient();
- var client2 = new MqttFactory().CreateMqttClient();
-
- try
- {
- await server.StartAsync(new MqttServerOptions());
-
- var receivedMessages = new List<MqttApplicationMessageReceivedEventArgs>();
-
- await client1.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithClientId("client1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
- client1.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(e =>
- {
- lock (receivedMessages)
- {
- receivedMessages.Add(e);
- }
- });
-
- await client1.SubscribeAsync("a");
-
- await client2.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithClientId("client2").WithProtocolVersion(MqttProtocolVersion.V500).Build());
- await client2.PublishAsync("a", "b");
-
- await Task.Delay(500);
-
- await client2.DisconnectAsync();
- await client1.DisconnectAsync();
-
- Assert.AreEqual(1, receivedMessages.Count);
- Assert.AreEqual("client1", receivedMessages[0].ClientId);
- Assert.AreEqual("a", receivedMessages[0].ApplicationMessage.Topic);
- Assert.AreEqual("b", receivedMessages[0].ApplicationMessage.ConvertPayloadToString());
- }
- finally
- {
- await server.StopAsync();
- }
- }
- }
- }
|