You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Client_Tests.cs 8.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using MQTTnet.Client;
  5. using MQTTnet.Client.Options;
  6. using MQTTnet.Client.Publishing;
  7. using MQTTnet.Client.Receiving;
  8. using MQTTnet.Client.Subscribing;
  9. using MQTTnet.Client.Unsubscribing;
  10. using MQTTnet.Formatter;
  11. using MQTTnet.Protocol;
  12. using MQTTnet.Server;
  13. namespace MQTTnet.Tests.MQTTv5
  14. {
  15. [TestClass]
  16. public class Client_Tests
  17. {
  18. [TestMethod]
  19. public async Task Connect()
  20. {
  21. var server = new MqttFactory().CreateMqttServer();
  22. var client = new MqttFactory().CreateMqttClient();
  23. try
  24. {
  25. await server.StartAsync(new MqttServerOptions());
  26. await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  27. }
  28. finally
  29. {
  30. await server.StopAsync();
  31. }
  32. }
  33. [TestMethod]
  34. public async Task Connect_And_Disconnect()
  35. {
  36. var server = new MqttFactory().CreateMqttServer();
  37. var client = new MqttFactory().CreateMqttClient();
  38. try
  39. {
  40. await server.StartAsync(new MqttServerOptions());
  41. await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  42. await client.DisconnectAsync();
  43. }
  44. finally
  45. {
  46. await server.StopAsync();
  47. }
  48. }
  49. [TestMethod]
  50. public async Task Subscribe()
  51. {
  52. var server = new MqttFactory().CreateMqttServer();
  53. var client = new MqttFactory().CreateMqttClient();
  54. try
  55. {
  56. await server.StartAsync(new MqttServerOptions());
  57. await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  58. var result = await client.SubscribeAsync("a", MqttQualityOfServiceLevel.AtLeastOnce);
  59. await client.DisconnectAsync();
  60. Assert.AreEqual(1, result.Items.Count);
  61. Assert.AreEqual(MqttClientSubscribeResultCode.GrantedQoS1, result.Items[0].ResultCode);
  62. }
  63. finally
  64. {
  65. await server.StopAsync();
  66. }
  67. }
  68. [TestMethod]
  69. public async Task Unsubscribe()
  70. {
  71. var server = new MqttFactory().CreateMqttServer();
  72. var client = new MqttFactory().CreateMqttClient();
  73. try
  74. {
  75. await server.StartAsync(new MqttServerOptions());
  76. await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  77. await client.SubscribeAsync("a");
  78. var result = await client.UnsubscribeAsync("a");
  79. await client.DisconnectAsync();
  80. Assert.AreEqual(1, result.Items.Count);
  81. Assert.AreEqual(MqttClientUnsubscribeResultCode.Success, result.Items[0].ReasonCode);
  82. }
  83. finally
  84. {
  85. await server.StopAsync();
  86. }
  87. }
  88. [TestMethod]
  89. public async Task Publish_QoS_0()
  90. {
  91. var server = new MqttFactory().CreateMqttServer();
  92. var client = new MqttFactory().CreateMqttClient();
  93. try
  94. {
  95. await server.StartAsync(new MqttServerOptions());
  96. await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  97. var result = await client.PublishAsync("a", "b");
  98. await client.DisconnectAsync();
  99. Assert.AreEqual(MqttClientPublishReasonCode.Success, result.ReasonCode);
  100. }
  101. finally
  102. {
  103. await server.StopAsync();
  104. }
  105. }
  106. [TestMethod]
  107. public async Task Publish_QoS_1()
  108. {
  109. var server = new MqttFactory().CreateMqttServer();
  110. var client = new MqttFactory().CreateMqttClient();
  111. try
  112. {
  113. await server.StartAsync(new MqttServerOptions());
  114. await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  115. var result = await client.PublishAsync("a", "b", MqttQualityOfServiceLevel.AtLeastOnce);
  116. await client.DisconnectAsync();
  117. Assert.AreEqual(MqttClientPublishReasonCode.Success, result.ReasonCode);
  118. }
  119. finally
  120. {
  121. await server.StopAsync();
  122. }
  123. }
  124. [TestMethod]
  125. public async Task Publish_QoS_2()
  126. {
  127. var server = new MqttFactory().CreateMqttServer();
  128. var client = new MqttFactory().CreateMqttClient();
  129. try
  130. {
  131. await server.StartAsync(new MqttServerOptions());
  132. await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  133. var result = await client.PublishAsync("a", "b", MqttQualityOfServiceLevel.ExactlyOnce);
  134. await client.DisconnectAsync();
  135. Assert.AreEqual(MqttClientPublishReasonCode.Success, result.ReasonCode);
  136. }
  137. finally
  138. {
  139. await server.StopAsync();
  140. }
  141. }
  142. [TestMethod]
  143. public async Task Publish_With_Properties()
  144. {
  145. var server = new MqttFactory().CreateMqttServer();
  146. var client = new MqttFactory().CreateMqttClient();
  147. try
  148. {
  149. await server.StartAsync(new MqttServerOptions());
  150. var applicationMessage = new MqttApplicationMessageBuilder()
  151. .WithTopic("Hello")
  152. .WithPayload("World")
  153. .WithAtMostOnceQoS()
  154. .WithUserProperty("x", "1")
  155. .WithUserProperty("y", "2")
  156. .WithResponseTopic("response")
  157. .WithContentType("text")
  158. .WithMessageExpiryInterval(50)
  159. .WithCorrelationData(new byte[12])
  160. .WithTopicAlias(2)
  161. .Build();
  162. await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  163. var result = await client.PublishAsync(applicationMessage);
  164. await client.DisconnectAsync();
  165. Assert.AreEqual(MqttClientPublishReasonCode.Success, result.ReasonCode);
  166. }
  167. finally
  168. {
  169. await server.StopAsync();
  170. }
  171. }
  172. [TestMethod]
  173. public async Task Subscribe_And_Publish()
  174. {
  175. var server = new MqttFactory().CreateMqttServer();
  176. var client1 = new MqttFactory().CreateMqttClient();
  177. var client2 = new MqttFactory().CreateMqttClient();
  178. try
  179. {
  180. await server.StartAsync(new MqttServerOptions());
  181. var receivedMessages = new List<MqttApplicationMessageReceivedEventArgs>();
  182. await client1.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithClientId("client1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  183. client1.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(e =>
  184. {
  185. lock (receivedMessages)
  186. {
  187. receivedMessages.Add(e);
  188. }
  189. });
  190. await client1.SubscribeAsync("a");
  191. await client2.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithClientId("client2").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  192. await client2.PublishAsync("a", "b");
  193. await Task.Delay(500);
  194. await client2.DisconnectAsync();
  195. await client1.DisconnectAsync();
  196. Assert.AreEqual(1, receivedMessages.Count);
  197. Assert.AreEqual("client1", receivedMessages[0].ClientId);
  198. Assert.AreEqual("a", receivedMessages[0].ApplicationMessage.Topic);
  199. Assert.AreEqual("b", receivedMessages[0].ApplicationMessage.ConvertPayloadToString());
  200. }
  201. finally
  202. {
  203. await server.StopAsync();
  204. }
  205. }
  206. }
  207. }