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.
 
 
 
 

246 lines
8.7 KiB

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